scala/ex.scala
changeset 270 ccec33db31d4
parent 269 fa40fd8abb54
child 271 4457185b22ef
equal deleted inserted replaced
269:fa40fd8abb54 270:ccec33db31d4
     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 
       
     8 val Lg = {
       
     9   val lgR = Le o (Power o (Id(3, 1), Id(3, 2)), Id(3, 0))
       
    10   val conR1 = Conj o (Less o (Const(1) o (Id(2, 0), Id(2, 0))), 
       
    11                      Less o (Const(1) o (Id(2, 0), Id(2, 1))))
       
    12   val conR2 = Not o (conR1)
       
    13   Add o (recs.Mult o (conR1, Maxr(lgR) o (Id(2, 0), Id(2, 1), Id(2, 0))), 
       
    14          recs.Mult o (conR2, Const(0) o (Id(2, 0)))) 
       
    15 }
       
    16 
       
    17 
     7 
    18 
     8 
    19 // Turing machine examples
     9 // Turing machine examples
    20 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), 
    21                 (L, 4), (L, 4), (L, 5), (R, 11), (R, 6), 
    11                 (L, 4), (L, 4), (L, 5), (R, 11), (R, 6), 
    22                 (R, 7), (WBk, 6), (R, 7), (R, 8), (WOc, 9), 
    12                 (R, 7), (WBk, 6), (R, 7), (R, 8), (WOc, 9), 
    23                 (R, 8), (L, 10), (L, 9), (L, 10), (L, 5), 
    13                 (R, 8), (L, 10), (L, 9), (L, 10), (L, 5), 
    24                 (L, 0), (R, 12), (WOc, 13), (L, 14), (R, 12), 
    14                 (L, 0), (R, 12), (WOc, 13), (L, 14), (R, 12), 
    25                 (R, 12), (L, 15), (WBk, 14), (R, 0), (L, 15))
    15                 (R, 12), (L, 15), (WBk, 14), (R, 0), (L, 15))
    26 
    16 
       
    17 /*
    27 println("TMCopy:    " + (TMCopy.run(Tape(3))))
    18 println("TMCopy:    " + (TMCopy.run(Tape(3))))
    28 println("TMfindnth: " + (TMFindnth(3).run(Tape(1,2,3,4,5))))
    19 println("TMfindnth: " + (TMFindnth(3).run(Tape(1,2,3,4,5))))
    29 println("TMMopup:   " + (TMMopup(3).run(Tape(1,2,3,4,5))))
    20 println("TMMopup:   " + (TMMopup(3).run(Tape(1,2,3,4,5))))
    30 
    21 
    31 println("TMCopyMop: " + ((TMCopy :+ TMMopup(0))))
    22 println("TMCopyMop: " + ((TMCopy :+ TMMopup(0))))
    32 println("TMCopyMop: " + ((TMCopy :+ TMMopup(1)).run(Tape(3))))
    23 println("TMCopyMop: " + ((TMCopy :+ TMMopup(1)).run(Tape(3))))
       
    24 */
    33 
    25 
    34 // Abacus machine examples
    26 // Abacus machine examples
    35 def Copy(in: Int, out: Int, jump: Int) = 
    27 def Copy(in: Int, out: Int, jump: Int) = 
    36   Abacus(Dec(in, jump), Inc(out), Goto(0)) 
    28   Abacus(Dec(in, jump), Inc(out), Goto(0)) 
    37 
    29 
    50 println("Copy 3:     " + (Copy(0, 1, -1).run(Map(0 -> 3, 1 -> 0))))
    42 println("Copy 3:     " + (Copy(0, 1, -1).run(Map(0 -> 3, 1 -> 0))))
    51 println("Plus 3 + 4: " + (Plus(0, 1, 2, -1).run(Map(0 -> 3, 1 -> 4, 2 -> 0))))
    43 println("Plus 3 + 4: " + (Plus(0, 1, 2, -1).run(Map(0 -> 3, 1 -> 4, 2 -> 0))))
    52 println("Mult 3 * 5: " + (Mult(0, 1, 2, 3, -1).run(Map(0 -> 3, 1 -> 5, 2 -> 0, 3 -> 0))))
    44 println("Mult 3 * 5: " + (Mult(0, 1, 2, 3, -1).run(Map(0 -> 3, 1 -> 5, 2 -> 0, 3 -> 0))))
    53 println("Expo 3 ^ 4: " + (Expo(0, 1, 2, 3, 4, -1).run(Map(0 -> 4, 1 -> 3, 2 -> 0, 3 -> 0, 4 -> 0))))
    45 println("Expo 3 ^ 4: " + (Expo(0, 1, 2, 3, 4, -1).run(Map(0 -> 4, 1 -> 3, 2 -> 0, 3 -> 0, 4 -> 0))))
    54 
    46 
    55 
    47 /*
    56 // Abacus-to-TM translation examples
    48 // Abacus-to-TM translation examples
    57 println("Compiled Copy 3:     " + toTM(Copy(0, 1, Int.MaxValue).p).run(Tape(3,0,0)))
    49 println("Compiled Copy 3:     " + toTM(Copy(0, 1, Int.MaxValue).p).run(Tape(3,0,0)))
    58 println("Compiled Plus 3 + 4: " + toTM(Plus(0, 1, 2, Int.MaxValue).p).run(Tape(3,4,0,0)))
    50 println("Compiled Plus 3 + 4: " + toTM(Plus(0, 1, 2, Int.MaxValue).p).run(Tape(3,4,0,0)))
    59 println("Compiled Mult 3 * 5: " + toTM(Mult(0, 1, 2, 3, Int.MaxValue).p).run(Tape(3,5,0,0)))
    51 println("Compiled Mult 3 * 5: " + toTM(Mult(0, 1, 2, 3, Int.MaxValue).p).run(Tape(3,5,0,0)))
    60 println("Compiled Expo 3 ^ 4: " + toTM(Expo(0, 1, 2, 3, 4, 10000).p).run(Tape(3,4,0,0,0)))
    52 println("Compiled Expo 3 ^ 4: " + toTM(Expo(0, 1, 2, 3, 4, 10000).p).run(Tape(3,4,0,0,0)))
    61 
    53 */
       
    54  
    62 // Recursive function examples 
    55 // Recursive function examples 
    63 println("Const 8:   " + Const(8).eval(67))
    56 println("Const 8:   " + Const(8).eval(67))
    64 println("Add 3 4:   " + Add.eval(3, 4))
    57 println("Add 3 4:   " + Add.eval(3, 4))
    65 println("Mult 3 4:  " + recs.Mult.eval(3, 4))
    58 println("Mult 3 4:  " + recs.Mult.eval(3, 4))
    66 println("Power 2 3: " + Power.eval(2, 3))
    59 println("Power 2 3: " + Power.eval(2, 3))
   103 println("Le 4 6:    " + recs.Le.eval(4, 6))
    96 println("Le 4 6:    " + recs.Le.eval(4, 6))
   104 println("Le 6 4:    " + recs.Le.eval(6, 4))
    97 println("Le 6 4:    " + recs.Le.eval(6, 4))
   105 
    98 
   106 println("Sigma1 Add 2 3 -> 12: " + Sigma1(Add).eval(2, 3))
    99 println("Sigma1 Add 2 3 -> 12: " + Sigma1(Add).eval(2, 3))
   107 println("Accum1 Add 2 3 -> 60:  " + Accum1(Add).eval(2,3))
   100 println("Accum1 Add 2 3 -> 60:  " + Accum1(Add).eval(2,3))
   108 println("Accum1 Mult 2 3 -> 0:  " + Accum1(Mult).eval(2,3))
   101 println("Accum1 Mult 2 3 -> 0:  " + Accum1(recs.Mult).eval(2,3))
   109 println("Accum1 (Id(2, 1)) 2 3 -> 27:  " + Accum1(Id(2, 1)).eval(2,3))
   102 println("Accum1 (Id(2, 1)) 2 3 -> 27:  " + Accum1(Id(2, 1)).eval(2,3))
   110 println("Accum2 (Id(3, 1)) 2 3 3 -> 27:  " + Accum2(Id(3, 1)).eval(2,3,3))
   103 println("Accum2 (Id(3, 1)) 2 3 3 -> 27:  " + Accum2(Id(3, 1)).eval(2,3,3))
   111 println("Accum3 (Id(4, 1)) 2 3 3 3 -> 27:  " + Accum3(Id(4, 1)).eval(2,3,3,3))
   104 println("Accum3 (Id(4, 1)) 2 3 3 3 -> 27:  " + Accum3(Id(4, 1)).eval(2,3,3,3))
   112 println("All1 Add 2 0 -> 0:  " + All1(Add).eval(2,0))
   105 println("All1 Add 2 0 -> 0:  " + All1(Add).eval(2,0))
   113 println("All1 Add 2 1 -> 1:  " + All1(Add).eval(2,1))
   106 println("All1 Add 2 1 -> 1:  " + All1(Add).eval(2,1))
   126 println("MaxTriangle 10 -> 4 " + MaxTriangle.eval(10))
   119 println("MaxTriangle 10 -> 4 " + MaxTriangle.eval(10))
   127 
   120 
   128 println("Penc 1 2 -> 7: " + Penc.eval(1, 2))
   121 println("Penc 1 2 -> 7: " + Penc.eval(1, 2))
   129 println("Pdec1 7 -> 1:  " + Pdec1.eval(7))
   122 println("Pdec1 7 -> 1:  " + Pdec1.eval(7))
   130 println("Pdec2 7 -> 2:  " + Pdec2.eval(7))
   123 println("Pdec2 7 -> 2:  " + Pdec2.eval(7))
   131 println("Enclen 0 .. 10: " + (0 until 10).map(EncLen.eval(_)))
   124 println("Enclen 0 .. 10: " + (0 until 10).map(Enclen.eval(_)))
   132 
   125 
       
   126 /*
   133 // compilation of rec to abacus tests
   127 // compilation of rec to abacus tests
   134 def test_comp2(f: Rec, ns: Int*) = {
   128 def test_comp2(f: Rec, ns: Int*) = {
   135   val (abc_f, arity, _) = compile_rec(f)
   129   val (abc_f, arity, _) = compile_rec(f)
   136   val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
   130   val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
   137   val start = System.nanoTime()
   131   val start = System.nanoTime()
   166 println("Const(10)     " + test_comp1(Const(10), 0))
   160 println("Const(10)     " + test_comp1(Const(10), 0))
   167 println("Add(6, 3)     " + test_comp1(Add, 6, 3))
   161 println("Add(6, 3)     " + test_comp1(Add, 6, 3))
   168 println("Mult(4, 5)    " + test_comp1(recs.Mult, 4, 5))
   162 println("Mult(4, 5)    " + test_comp1(recs.Mult, 4, 5))
   169 println("Fact(4)       " + test_comp1(Fact, 4))
   163 println("Fact(4)       " + test_comp1(Fact, 4))
   170 
   164 
       
   165 */