turing.scala
changeset 179 650e7a7a389b
parent 178 8248f8adf17d
child 180 8f443f2ed1f6
equal deleted inserted replaced
178:8248f8adf17d 179:650e7a7a389b
   188   Abacus(List(Dec(in1, 4), Inc(in2), Inc(out),
   188   Abacus(List(Dec(in1, 4), Inc(in2), Inc(out),
   189               Goto(0), Dec(out, -1), Inc(in1), Goto(4))) 
   189               Goto(0), Dec(out, -1), Inc(in1), Goto(4))) 
   190 
   190 
   191 
   191 
   192 println("Ab: " + (new Plus(0, 1, 2)).run(Map(0 -> 3, 1 -> 4)))
   192 println("Ab: " + (new Plus(0, 1, 2)).run(Map(0 -> 3, 1 -> 4)))
       
   193 
       
   194 
       
   195 
       
   196 //Recursive Functions
       
   197 abstract class Rec {
       
   198   def eval(ns: List[Int]) : Int
       
   199 }
       
   200 case object Z extends Rec {
       
   201   override def eval(ns: List[Int]) = ns match {
       
   202     case n::Nil => 0
       
   203     case _ => throw new IllegalArgumentException("Z: args")
       
   204   }
       
   205 } 
       
   206 case object S extends Rec {
       
   207   override def eval(ns: List[Int]) = ns match {
       
   208     case n::Nil => n + 1
       
   209     case _ => throw new IllegalArgumentException("S: args")
       
   210   }
       
   211 } 
       
   212 case class Id(n: Int, m: Int) extends Rec {
       
   213   override def eval(ns: List[Int]) = 
       
   214     if (ns.length == n && m < n) ns(m)
       
   215     else throw new IllegalArgumentException("Id: args")
       
   216 }
       
   217 case class Cn(n: Int, f: Rec, gs: List[Rec]) extends Rec {
       
   218   override def eval(ns: List[Int]) = 
       
   219     if (ns.length == n) f.eval(gs.map(_.eval(ns)))
       
   220     else throw new IllegalArgumentException("Cn: args")
       
   221 }
       
   222 case class Pr(n: Int, f: Rec, g: Rec) extends Rec {
       
   223   override def eval(ns: List[Int]) = 
       
   224     if (ns.length == n - 1) {
       
   225       if (ns.last == 0) f.eval(ns.init)
       
   226       else {
       
   227         val r = Pr(n, f, g).eval(ns.init ::: List(ns.last - 1))
       
   228         g.eval(ns.init ::: List(ns.last - 1, r))
       
   229       }
       
   230     }
       
   231     else throw new IllegalArgumentException("Cn: args")
       
   232 }
       
   233 case class Mn(n: Int, f: Rec) extends Rec {
       
   234   override def eval(ns: List[Int]) =
       
   235     
       
   236 }
       
   237