scala/ex.scala
changeset 239 ac3309722536
parent 226 df455e0a9f98
child 240 696081f445c2
equal deleted inserted replaced
238:6ea1062da89a 239:ac3309722536
     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 
     7 
    17 
     8 
    18 
     9 // Turing machine examples
    19 // Turing machine examples
    10 val TMCopy = TM((WBk, 5), (R, 2), (R, 3), (R, 2), (WOc, 3), 
    20 val TMCopy = TM((WBk, 5), (R, 2), (R, 3), (R, 2), (WOc, 3), 
    11                 (L, 4), (L, 4), (L, 5), (R, 11), (R, 6), 
    21                 (L, 4), (L, 4), (L, 5), (R, 11), (R, 6), 
    63 println("Sign 8:    " + Sign.eval(8))
    73 println("Sign 8:    " + Sign.eval(8))
    64 println("Sign 0:    " + Sign.eval(0))
    74 println("Sign 0:    " + Sign.eval(0))
    65 println("Less 4 4:  " + Less.eval(4, 4))
    75 println("Less 4 4:  " + Less.eval(4, 4))
    66 println("Less 4 6:  " + Less.eval(4, 6))
    76 println("Less 4 6:  " + Less.eval(4, 6))
    67 println("Less 6 4:  " + Less.eval(6, 4))
    77 println("Less 6 4:  " + Less.eval(6, 4))
       
    78 println("Le 4 4:    " + recs.Le.eval(4, 4))
       
    79 println("Le 4 6:    " + recs.Le.eval(4, 6))
       
    80 println("Le 6 4:    " + recs.Le.eval(6, 4))
    68 println("Not 0:     " + Not.eval(0))
    81 println("Not 0:     " + Not.eval(0))
    69 println("Not 6:     " + Not.eval(6))
    82 println("Not 6:     " + Not.eval(6))
    70 println("Eq 4 4:    " + Eq.eval(4, 4))
    83 println("Eq 4 4:    " + Eq.eval(4, 4))
    71 println("Eq 4 6:    " + Eq.eval(4, 6))
    84 println("Eq 4 6:    " + Eq.eval(4, 6))
    72 println("Eq 6 4:    " + Eq.eval(6, 4))
    85 println("Eq 6 4:    " + Eq.eval(6, 4))
    82 println("Fact 5:  " + Fact.eval(5))
    95 println("Fact 5:  " + Fact.eval(5))
    83 println("Prime 0..15: " + (0 to 15).map(n => (n, Prime.eval(n))))
    96 println("Prime 0..15: " + (0 to 15).map(n => (n, Prime.eval(n))))
    84 println("NextPrime 3: " + NextPrime.eval(3))
    97 println("NextPrime 3: " + NextPrime.eval(3))
    85 println("NthPrime 1:  " + NthPrime.eval(1))
    98 println("NthPrime 1:  " + NthPrime.eval(1))
    86 println("Listsum [2, 3, 4 , 5, 6]: " + Listsum(5, 4).eval(2, 3, 4, 5, 6))
    99 println("Listsum [2, 3, 4 , 5, 6]: " + Listsum(5, 4).eval(2, 3, 4, 5, 6))
    87 println("Strt:  " + Strt(2).eval(2,3))
   100 println("Strt:  " + Strt(2).eval(2, 3))
    88 
   101 println("(<=5) 1: " + (Less o (Id(1, 0), Const(5))).eval(1))
       
   102 println("(<=5) 5: " + (Less o (Id(1, 0), Const(5))).eval(5))
       
   103 println("(<=5) 6: " + (Less o (Id(1, 0), Const(5))).eval(6))
       
   104 println("Max (<=9): " + Maxr(Le o (Id(1, 0), Const(9))).eval(10))
       
   105 println("Max (>=9): " + Maxr(Le o (Const(9), Id(1, 0))).eval(8))
       
   106 println("Min (>=9): " + Minr(Le o (Const(9), Id(1, 0))).eval(10))
       
   107 println("Min (<=9): " + Minr(Le o (Id(1, 0), Const(9))).eval(10))
       
   108 println("Min (>=9): " + Minr(Le o (Const(9), Id(1, 0))).eval(8))
       
   109 //println("Lg 4 2: " + Lg.eval(4, 2))
    89 
   110 
    90 // compilation of rec to abacus tests
   111 // compilation of rec to abacus tests
    91 def test_comp2(f: Rec, ns: Int*) = {
   112 def test_comp2(f: Rec, ns: Int*) = {
    92   val (abc_f, arity, _) = compile_rec(f)
   113   val (abc_f, arity, _) = compile_rec(f)
    93   val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
   114   val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]