--- a/scala/ex.scala Fri Mar 01 23:46:02 2013 +0000
+++ b/scala/ex.scala Sat Mar 02 10:42:39 2013 +0000
@@ -100,14 +100,36 @@
println("Power(3, 4) " + test_comp2(Power, 3, 4))
println("Minus(30, 4) " + test_comp2(Minus, 30, 4))
println("Fact(5) " + test_comp2(Fact, 5))
-println("Prime(5) " + test_comp2(Prime, 5))
-println("Prime(6) " + test_comp2(Prime, 6))
+//println("Prime(5) " + test_comp2(Prime, 5))
+//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)
+}
+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(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 Fri Mar 01 23:46:02 2013 +0000
+++ b/scala/turing.scala Sat Mar 02 10:42:39 2013 +0000
@@ -46,8 +46,11 @@
// standard tapes
object Tape {
- def apply(ns: Int*) : Tape =
- Tape(Nil, ns.map(n => Oc * (n + 1)).reduceLeft(_ ::: List(Bk) ::: _))
+ def apply(ns: List[Int]) : Tape =
+ Tape(Nil, ns.map(n => Oc * (n + 1)).reduceLeft(_ ::: List(Bk) ::: _))
+
+ def apply(ns: Int*) : Tape = apply(ns.toList)
+
}
// configurations
@@ -60,8 +63,9 @@
// Turing machines
case class TM(p: Prog) {
- // simple composition
+ // composition
def ++ (that: TM) = TM(this.p ::: that.p)
+ def :+ (that: TM) = this.adjust ++ that.shift(this.p.length / 2 + 1)
def shift(n: Int) =
TM(p.map{case (a, s) => (a, if (s == 0) 0 else s + n)})