tuned abacus to turing compilation
authorChristian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 03 Mar 2013 00:18:13 +0000
changeset 210 5e2e576fac7c
parent 209 b16dfc467b67
child 211 1d6a0fd9f7f4
tuned abacus to turing compilation
scala/ex.scala
scala/turing.scala
--- a/scala/ex.scala	Sat Mar 02 10:42:39 2013 +0000
+++ b/scala/ex.scala	Sun Mar 03 00:18:13 2013 +0000
@@ -17,6 +17,8 @@
 println("TMfindnth: " + (TMFindnth(3).run(Tape(1,2,3,4,5))))
 println("TMMopup:   " + (TMMopup(3).run(Tape(1,2,3,4,5))))
 
+println("TMCopyMop: " + ((TMCopy :+ TMMopup(0))))
+println("TMCopyMop: " + ((TMCopy :+ TMMopup(1)).run(Tape(3))))
 
 // Abacus machine examples
 def Copy(in: Int, out: Int, jump: Int) = 
@@ -104,32 +106,18 @@
 //println("Prime(4)      " + test_comp2(Prime, 4))
 println("Strt(1)(2)    " + test_comp2(Strt(1), 2))
 
-def test_comp1a(f: Rec, ns: Int*) = {
-  val (abc_f, arity, _) = compile_rec(f)
-  val tm1 = toTM(abc_f.p) 
-  val res = tm1.run(Tape(ns.toList))
-  ("Result: " + res + "  length: " + tm1.p.length + " tape: " + arity)
-}
 
-println("")
-println("S(3)          " + test_comp1a(S, 3))
-println("Const(10)     " + test_comp1a(Const(10), 0))
-println("Add(6, 3)     " + test_comp1a(Add, 6, 3))
-
-/*
 def test_comp1(f: Rec, ns: Int*) = {
   val (abc_f, arity, _) = compile_rec(f)
   val tm = toTM(abc_f.p) :+ TMMopup(arity)
   val res = tm.run(Tape(ns.toList))
-  ("Result: " + res + "  length: " + tm.p.length + " tape: " + arity)
+  ("length: " + tm.p.length + " tape: " + arity + "\nResult: " + res)
 }
 
 println("")
 println("S(3)          " + test_comp1(S, 3))
 println("Const(10)     " + test_comp1(Const(10), 0))
 println("Add(6, 3)     " + test_comp1(Add, 6, 3))
+println("Mult(4, 5)    " + test_comp1(recs.Mult, 4, 5))
+println("Fact(4)       " + test_comp1(Fact, 4))
 
-*/ 
-//println(toTM(compile_rec(Add)._1.p).run(Tape(3, 4, 0)))
-//println(compile_rec(Add)._1.run(Map(0 -> 3, 1 -> 8, 2 -> 0)))
-//compile_rec(Add)._1.run(Map(0 -> 3, 1 -> 4, 2 -> 0))
--- a/scala/turing.scala	Sat Mar 02 10:42:39 2013 +0000
+++ b/scala/turing.scala	Sun Mar 03 00:18:13 2013 +0000
@@ -65,7 +65,7 @@
 
   // composition
   def ++ (that: TM) = TM(this.p ::: that.p)
-  def :+ (that: TM) = this.adjust ++ that.shift(this.p.length / 2 + 1)
+  def :+ (that: TM) = this.adjust ++ that.shift(this.p.length / 2)
 
   def shift(n: Int) =
     TM(p.map{case (a, s) => (a, if (s == 0) 0 else s + n)})