scala/ex.scala
changeset 271 4457185b22ef
parent 270 ccec33db31d4
equal deleted inserted replaced
270:ccec33db31d4 271:4457185b22ef
     1 import lib._
     1 import lib._
     2 import turing._
     2 import turing._
     3 import abacus._
     3 import abacus._
     4 import recs._
     4 import recs._
     5 //import comp1._
     5 import comp1._
     6 //import comp2._
     6 import comp2._
     7 
     7 
     8 
     8 
     9 // Turing machine examples
     9 // Turing machine examples
    10 val TMCopy = TM((WBk, 5), (R, 2), (R, 3), (R, 2), (WOc, 3), 
    10 val TMCopy = TM((WBk, 5), (R, 2), (R, 3), (R, 2), (WOc, 3), 
    11                 (L, 4), (L, 4), (L, 5), (R, 11), (R, 6), 
    11                 (L, 4), (L, 4), (L, 5), (R, 11), (R, 6), 
   121 println("Penc 1 2 -> 7: " + Penc.eval(1, 2))
   121 println("Penc 1 2 -> 7: " + Penc.eval(1, 2))
   122 println("Pdec1 7 -> 1:  " + Pdec1.eval(7))
   122 println("Pdec1 7 -> 1:  " + Pdec1.eval(7))
   123 println("Pdec2 7 -> 2:  " + Pdec2.eval(7))
   123 println("Pdec2 7 -> 2:  " + Pdec2.eval(7))
   124 println("Enclen 0 .. 10: " + (0 until 10).map(Enclen.eval(_)))
   124 println("Enclen 0 .. 10: " + (0 until 10).map(Enclen.eval(_)))
   125 
   125 
   126 /*
   126 println("Size of UF -> 140843: " + UF.size)
       
   127 
       
   128 
   127 // compilation of rec to abacus tests
   129 // compilation of rec to abacus tests
   128 def test_comp2(f: Rec, ns: Int*) = {
   130 def test_comp2(f: Rec, ns: Int*) = {
   129   val (abc_f, arity, _) = compile_rec(f)
   131   val (abc_f, _) = compile_rec(f)
   130   val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
   132   val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
   131   val start = System.nanoTime()
   133   //val start = System.nanoTime()
   132   val res = (abc_f.run(abc_map))(arity)
   134   //val res = (abc_f.run(abc_map))(arity)
   133   val end = System.nanoTime()
   135   //val end = System.nanoTime()
   134   val time = (end - start)/1.0e9
   136   //val time = (end - start)/1.0e9
   135   ("Result: " + res + "  length: " + abc_f.p.length + " time: " + "%.5f".format(time))
   137   //("Result: " + res + "  length: " + abc_f.p.length + " time: " + "%.5f".format(time))
       
   138   ("Length: " + abc_f.p.length)
   136 }
   139 }
       
   140 
   137 
   141 
   138 println("S(3)          " + test_comp2(S, 3))
   142 println("S(3)          " + test_comp2(S, 3))
   139 println("Const(1)      " + test_comp2(Const(1), 0))
   143 println("Const(1)      " + test_comp2(Const(1), 0))
   140 println("Const(10)     " + test_comp2(Const(10), 0))
   144 println("Const(10)     " + test_comp2(Const(10), 0))
   141 println("Add(69, 30)   " + test_comp2(Add, 69, 30))
   145 println("Add(69, 30)   " + test_comp2(Add, 69, 30))
   142 println("Mult(13, 9)   " + test_comp2(recs.Mult, 13, 9))
   146 println("Mult(13, 9)   " + test_comp2(recs.Mult, 13, 9))
   143 println("Power(3, 4)   " + test_comp2(Power, 3, 4))
   147 println("Power(3, 4)   " + test_comp2(Power, 3, 4))
   144 println("Minus(30, 4)  " + test_comp2(Minus, 30, 4))
   148 println("Minus(30, 4)  " + test_comp2(Minus, 30, 4))
   145 println("Fact(5)       " + test_comp2(Fact, 5))
   149 println("Fact(5)       " + test_comp2(Fact, 5))
   146 
   150 //println("UF(0)         " + test_comp2(UF, 0))
   147 
   151 
   148 def test_comp1(f: Rec, ns: Int*) = {
   152 def test_comp1(f: Rec, ns: Int*) = {
   149   val (abc_f, arity, _) = compile_rec(f)
   153   val (abc_f, _) = compile_rec(f)
   150   val tm = toTM(abc_f.p) :+ TMMopup(arity)
   154   println("Abacus Length: " + abc_f.p.length)
   151   val start = System.nanoTime()
   155   val tm = toTM(abc_f.p) :+ TMMopup(f.arity)
   152   val res = tm.run(Tape(ns.toList))
   156   //val start = System.nanoTime()
   153   val end = System.nanoTime()
   157   //val res = tm.run(Tape(ns.toList))
   154   val time = (end - start)/1.0e9
   158   //val end = System.nanoTime()
   155   ("length: " + tm.p.length + " tape: " + arity + " time: " + "%.5f".format(time) + "\nResult: " + res)
   159   //val time = (end - start)/1.0e9
       
   160   //("length: " + tm.p.length + " tape: " + arity + " time: " + "%.5f".format(time) + "\nResult: " + res)
       
   161   ("Length: " + abc_f.p.length + " " + tm.p.length)
   156 }
   162 }
   157 
   163 
   158 println("")
   164 //println("")
   159 println("S(3)          " + test_comp1(S, 3))
   165 //println("S(3)          " + test_comp1(S, 3))
   160 println("Const(10)     " + test_comp1(Const(10), 0))
   166 //println("Const(10)     " + test_comp1(Const(10), 0))
   161 println("Add(6, 3)     " + test_comp1(Add, 6, 3))
   167 //println("Add(6, 3)     " + test_comp1(Add, 6, 3))
   162 println("Mult(4, 5)    " + test_comp1(recs.Mult, 4, 5))
   168 //println("Mult(4, 5)    " + test_comp1(recs.Mult, 4, 5))
   163 println("Fact(4)       " + test_comp1(Fact, 4))
   169 //println("Fact(4)       " + test_comp1(Fact, 4))
       
   170 println("UF(0)         " + test_comp1(UF, 0))
   164 
   171 
   165 */