scala/ex.scala
changeset 269 fa40fd8abb54
parent 240 696081f445c2
child 270 ccec33db31d4
equal deleted inserted replaced
268:002b07ea0a57 269:fa40fd8abb54
    58 println("Compiled Plus 3 + 4: " + toTM(Plus(0, 1, 2, Int.MaxValue).p).run(Tape(3,4,0,0)))
    58 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)))
    59 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)))
    60 println("Compiled Expo 3 ^ 4: " + toTM(Expo(0, 1, 2, 3, 4, 10000).p).run(Tape(3,4,0,0,0)))
    61 
    61 
    62 // Recursive function examples 
    62 // Recursive function examples 
       
    63 println("Const 8:   " + Const(8).eval(67))
    63 println("Add 3 4:   " + Add.eval(3, 4))
    64 println("Add 3 4:   " + Add.eval(3, 4))
    64 println("Mult 3 4:  " + recs.Mult.eval(3, 4))
    65 println("Mult 3 4:  " + recs.Mult.eval(3, 4))
    65 println("Twice 4:   " + Twice.eval(4))
    66 println("Power 2 3: " + Power.eval(2, 3))
    66 println("FourTm 4:  " + Fourtimes.eval(4))
    67 println("Fact 5:    " + Fact.eval(5))
    67 println("Pred 9:    " + Pred.eval(9))
    68 println("Pred 9:    " + Pred.eval(9))
    68 println("Pred 0:    " + Pred.eval(0))
    69 println("Pred 0:    " + Pred.eval(0))
    69 println("Minus 6 2: " + Minus.eval(6, 2))
    70 println("Minus 6 2: " + Minus.eval(6, 2))
    70 println("Minus 6 8: " + Minus.eval(6, 8))
    71 println("Minus 6 8: " + Minus.eval(6, 8))
    71 println("Const 8:   " + Const(8).eval(67))
    72 
    72 println("Power 2 3: " + Power.eval(2, 3))
    73 
    73 println("Sign 8:    " + Sign.eval(8))
    74 println("Sign 8:    " + Sign.eval(8))
    74 println("Sign 0:    " + Sign.eval(0))
    75 println("Sign 0:    " + Sign.eval(0))
       
    76 println("Not 0:     " + Not.eval(0))
       
    77 println("Not 6:     " + Not.eval(6))
       
    78 println("Eq 4 4:    " + Eq.eval(4, 4))
       
    79 println("Eq 4 6:    " + Eq.eval(4, 6))
       
    80 println("Eq 6 4:    " + Eq.eval(6, 4))
       
    81 println("NotEq 4 4: " + Noteq.eval(4, 4))
       
    82 println("NotEq 4 6: " + Noteq.eval(4, 6))
       
    83 println("NotEq 6 4: " + Noteq.eval(6, 4))
       
    84 println("Conj 0 6:  " + Conj.eval(0, 6))
       
    85 println("Conj 6 4:  " + Conj.eval(6, 4))
       
    86 println("Conj 0 0:  " + Conj.eval(0, 0))
       
    87 println("Disj 0 6:  " + Disj.eval(0, 6))
       
    88 println("Disj 6 4:  " + Disj.eval(6, 4))
       
    89 println("Disj 0 0:  " + Disj.eval(0, 0))
       
    90 println("Imp  6 1:  " + Imp.eval(6, 1))
       
    91 println("Imp  6 4:  " + Imp.eval(6, 4))
       
    92 println("Imp  1 0:  " + Imp.eval(1, 0))
       
    93 println("Ifz  0 1 2:" + Ifz.eval(0, 1, 2))
       
    94 println("Ifz  1 1 2:" + Ifz.eval(1, 1, 2))
       
    95 println("If   0 1 2:" + If.eval(0, 1, 2))
       
    96 println("If   1 1 2:" + If.eval(1, 1, 2))
       
    97 
       
    98 
    75 println("Less 4 4:  " + Less.eval(4, 4))
    99 println("Less 4 4:  " + Less.eval(4, 4))
    76 println("Less 4 6:  " + Less.eval(4, 6))
   100 println("Less 4 6:  " + Less.eval(4, 6))
    77 println("Less 6 4:  " + Less.eval(6, 4))
   101 println("Less 6 4:  " + Less.eval(6, 4))
    78 println("Le 4 4:    " + recs.Le.eval(4, 4))
   102 println("Le 4 4:    " + recs.Le.eval(4, 4))
    79 println("Le 4 6:    " + recs.Le.eval(4, 6))
   103 println("Le 4 6:    " + recs.Le.eval(4, 6))
    80 println("Le 6 4:    " + recs.Le.eval(6, 4))
   104 println("Le 6 4:    " + recs.Le.eval(6, 4))
    81 println("Not 0:     " + Not.eval(0))
   105 
    82 println("Not 6:     " + Not.eval(6))
   106 println("Sigma1 Add 2 3 -> 12: " + Sigma1(Add).eval(2, 3))
    83 println("Eq 4 4:    " + Eq.eval(4, 4))
   107 println("Accum1 Add 2 3 -> 60:  " + Accum1(Add).eval(2,3))
    84 println("Eq 4 6:    " + Eq.eval(4, 6))
   108 println("Accum1 Mult 2 3 -> 0:  " + Accum1(Mult).eval(2,3))
    85 println("Eq 6 4:    " + Eq.eval(6, 4))
   109 println("Accum1 (Id(2, 1)) 2 3 -> 27:  " + Accum1(Id(2, 1)).eval(2,3))
    86 println("Conj 0 6:  " + Conj.eval(0, 6))
   110 println("Accum2 (Id(3, 1)) 2 3 3 -> 27:  " + Accum2(Id(3, 1)).eval(2,3,3))
    87 println("Conj 6 4:  " + Conj.eval(6, 4))
   111 println("Accum3 (Id(4, 1)) 2 3 3 3 -> 27:  " + Accum3(Id(4, 1)).eval(2,3,3,3))
    88 println("Conj 0 0:  " + Conj.eval(0, 0))
   112 println("All1 Add 2 0 -> 0:  " + All1(Add).eval(2,0))
    89 println("Disj 0 6:  " + Disj.eval(0, 6))
   113 println("All1 Add 2 1 -> 1:  " + All1(Add).eval(2,1))
    90 println("Disj 6 4:  " + Disj.eval(6, 4))
   114 println("All1_less Add 3 0 -> 0:  " + All1_less(Add).eval(2,0))
    91 println("Disj 0 0:  " + Disj.eval(0, 0))
   115 println("All1_less Add 3 1 -> 1:  " + All1_less(Add).eval(2,1))
    92 println("Sigma Add 2 3 -> 14:  " + Sigma(Add).eval(2,3))
   116 println("All2_less (Id 3 1) 2 3 0 -> 1:  " + All2_less(Id(3, 1)).eval(2,3,0))
    93 println("Accum Add 2 3 -> 120: " + Accum(Add).eval(2,3))
   117 println("All2_less (Id 3 0) 2 3 0 -> 0:  " + All2_less(Id(3, 0)).eval(2,3,0))
    94 println("Accum Mult 2 3 -> 0:  " + Accum(recs.Mult).eval(2,3))
   118 println("Ex1 Add 2 0 -> 1:       " + Ex1(Add).eval(2,0))
    95 println("Fact 5:  " + Fact.eval(5))
   119 println("Ex2 Id(3,1) 2 1 0 -> 1: " + Ex2(Id(3, 1)).eval(2,1,0))
    96 println("Prime 0..15: " + (0 to 15).map(n => (n, Prime.eval(n))))
   120 println("Ex2 Id(3,2) 2 1 0 -> 0: " + Ex2(Id(3, 2)).eval(2,1,0))
    97 println("NextPrime 3: " + NextPrime.eval(3))
   121 
    98 println("NthPrime 1:  " + NthPrime.eval(1))
   122 println("Quo 6 4 -> 1:   " + Quo.eval(6, 4))
    99 println("Listsum [2, 3, 4 , 5, 6]: " + Listsum(5, 4).eval(2, 3, 4, 5, 6))
   123 println("Quo 13 4 -> 3:  " + Quo.eval(13, 4))
   100 println("Strt:  " + Strt(2).eval(2, 3))
   124 
   101 println("(<=5) 1: " + (Less o (Id(1, 0), Const(5))).eval(1))
   125 println("Triangle 0 - 5: " + (0 until 5).map(Triangle.eval(_)).mkString(","))
   102 println("(<=5) 5: " + (Less o (Id(1, 0), Const(5))).eval(5))
   126 println("MaxTriangle 10 -> 4 " + MaxTriangle.eval(10))
   103 println("(<=5) 6: " + (Less o (Id(1, 0), Const(5))).eval(6))
   127 
   104 println("Max (<=9): " + Maxr(Le o (Id(1, 0), Const(9))).eval(10))
   128 println("Penc 1 2 -> 7: " + Penc.eval(1, 2))
   105 println("Max (>=9): " + Maxr(Le o (Const(9), Id(1, 0))).eval(8))
   129 println("Pdec1 7 -> 1:  " + Pdec1.eval(7))
   106 println("test")
   130 println("Pdec2 7 -> 2:  " + Pdec2.eval(7))
   107 println("Lg 4 2: " + Lg.eval(4, 2))
   131 println("Enclen 0 .. 10: " + (0 until 10).map(EncLen.eval(_)))
   108 
   132 
   109 // compilation of rec to abacus tests
   133 // compilation of rec to abacus tests
   110 def test_comp2(f: Rec, ns: Int*) = {
   134 def test_comp2(f: Rec, ns: Int*) = {
   111   val (abc_f, arity, _) = compile_rec(f)
   135   val (abc_f, arity, _) = compile_rec(f)
   112   val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
   136   val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
   123 println("Add(69, 30)   " + test_comp2(Add, 69, 30))
   147 println("Add(69, 30)   " + test_comp2(Add, 69, 30))
   124 println("Mult(13, 9)   " + test_comp2(recs.Mult, 13, 9))
   148 println("Mult(13, 9)   " + test_comp2(recs.Mult, 13, 9))
   125 println("Power(3, 4)   " + test_comp2(Power, 3, 4))
   149 println("Power(3, 4)   " + test_comp2(Power, 3, 4))
   126 println("Minus(30, 4)  " + test_comp2(Minus, 30, 4))
   150 println("Minus(30, 4)  " + test_comp2(Minus, 30, 4))
   127 println("Fact(5)       " + test_comp2(Fact, 5))
   151 println("Fact(5)       " + test_comp2(Fact, 5))
   128 println("Prime(5)      " + test_comp2(Prime, 5))
       
   129 //println("Prime(4)      " + test_comp2(Prime, 4))
       
   130 println("Strt(1)(2)    " + test_comp2(Strt(1), 2))
       
   131 
   152 
   132 
   153 
   133 def test_comp1(f: Rec, ns: Int*) = {
   154 def test_comp1(f: Rec, ns: Int*) = {
   134   val (abc_f, arity, _) = compile_rec(f)
   155   val (abc_f, arity, _) = compile_rec(f)
   135   val tm = toTM(abc_f.p) :+ TMMopup(arity)
   156   val tm = toTM(abc_f.p) :+ TMMopup(arity)