scala/ex.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 26 Feb 2013 13:24:40 +0000
changeset 198 d93cc4295306
parent 195 f06aa4e1c25b
child 200 8dde2e46c69d
permissions -rw-r--r--
tuned some files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
import lib._
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import turing._
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import abacus._
195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
     4
import recs._
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
import comp1._
195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
     6
//import comp2._
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
// Turing machine examples
194
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
     9
val TMCopy = TM((WBk, 5), (R, 2), (R, 3), (R, 2), (WOc, 3), 
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    10
                (L, 4), (L, 4), (L, 5), (R, 11), (R, 6), 
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    11
                (R, 7), (WBk, 6), (R, 7), (R, 8), (WOc, 9), 
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    12
                (R, 8), (L, 10), (L, 9), (L, 10), (L, 5), 
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    13
                (L, 0), (R, 12), (WOc, 13), (L, 14), (R, 12), 
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    14
                (R, 12), (L, 15), (WBk, 14), (R, 0), (L, 15))
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
println("TMCopy:    " + (TMCopy.run(Tape(3))))
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
println("TMfindnth: " + (TMFindnth(3).run(Tape(1,2,3,4,5))))
194
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    18
println("TMMopup:   " + (TMMopup(3).run(Tape(1,2,3,4,5))))
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
// Abacus machine examples
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
def Copy(in: Int, out: Int, jump: Int) = 
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  Abacus(List(Dec(in, jump), Inc(out), Goto(0))) 
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
def Plus(m: Int, n: Int, tmp: Int, jump: Int) =
195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    26
  Abacus(List(Dec(m, 4), Inc(n), Inc(tmp), Goto(0), Dec(tmp, jump), Inc(m), Goto(4)))
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
def Mult(in1: Int, in2: Int, out: Int, tmp: Int, jump: Int) = 
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  Abacus(List(Dec(in1, jump))) ++ Plus(in2, out, tmp, -1).shift(1, -1).adjust(-1, 0)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
def Expo(in1: Int, in2: Int, out: Int, tmp1: Int, tmp2: Int, jump: Int) = {
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  Abacus(List(Inc(out), Dec(in1, jump))) ++ 
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  Mult(out, in2, tmp2, tmp1, -1).shift(2, -1).adjust(-1, 10) ++
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  Copy(tmp2, out, -1).shift(10, -1). adjust(-1, 1)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
}
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
194
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    37
println("Copy 3:     " + (Copy(0, 1, -1).run(Map(0 -> 3, 1 -> 0))))
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    38
println("Plus 3 + 4: " + (Plus(0, 1, 2, -1).run(Map(0 -> 3, 1 -> 4, 2 -> 0))))
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    39
println("Mult 3 * 5: " + (Mult(0, 1, 2, 3, -1).run(Map(0 -> 3, 1 -> 5, 2 -> 0, 3 -> 0))))
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    40
println("Expo 3 ^ 4: " + (Expo(0, 1, 2, 3, 4, -1).run(Map(0 -> 4, 1 -> 3, 2 -> 0, 3 -> 0, 4 -> 0))))
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
// Abacus-to-TM translation examples
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
println("Compiled Copy 3:     " + toTM(Copy(0, 1, Int.MaxValue).p).run(Tape(3,0,0)))
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
println("Compiled Plus 3 + 4: " + toTM(Plus(0, 1, 2, Int.MaxValue).p).run(Tape(3,4,0,0)))
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
println("Compiled Mult 3 * 5: " + toTM(Mult(0, 1, 2, 3, Int.MaxValue).p).run(Tape(3,5,0,0)))
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
println("Compiled Expo 3 ^ 4: " + toTM(Expo(0, 1, 2, 3, 4, 10000).p).run(Tape(3,4,0,0,0)))
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    49
// Recursive Function examples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    50
def arity(f: Rec) = f match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    51
  case Z => 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    52
  case S => 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    53
  case Id(n, _) => n
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    54
  case Cn(n, _, _) => n
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    55
  case Pr(n, _, _) => n + 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    56
  case Mn(n, _) => n 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    57
}
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    59
val Add = Pr(1, Id(1, 0), Cn(3, S, List(Id(3, 2))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    60
val Mult = Pr(1, Z, Cn(3, Add, List(Id(3, 0), Id(3, 2))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    61
val Pred = Cn(1, Pr(1, Z, Id(3, 1)), List(Id(1, 0), Id(1, 0)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    62
val Minus = Pr(1, Id(1, 0), Cn(3, Pred, List(Id(3, 2))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    63
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    64
def Const(n: Int) : Rec = n match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    65
  case 0 => Z
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    66
  case n => Cn(1, S, List(Const(n - 1)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    67
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    68
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    69
val Sign = Cn(1, Minus, List(Const(1), Cn(1, Minus, List(Const(1), Id(1, 0)))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    70
val Less = Cn(2, Sign, List(Cn(2, Minus, List(Id(2, 1), Id(2, 0)))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    71
val Not = Cn(1, Minus, List(Const(1), Id(1, 0)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    72
val Eq = Cn(2, Minus, List(Cn(2, Const(1), List(Id(2, 0))), 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    73
           Cn(2, Add, List(Cn(2, Minus, List(Id(2, 0), Id(2, 1))), 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    74
             Cn(2, Minus, List(Id(2, 1), Id(2, 0)))))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    75
val Conj = Cn(2, Sign, List(Cn(2, Mult, List(Id(2, 0), Id(2, 1)))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    76
val Disj = Cn(2, Sign, List(Cn(2, Add, List(Id(2, 0), Id(2, 1)))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    77
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    78
def Nargs(n: Int, m: Int) : List[Rec] = m match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    79
  case 0 => Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    80
  case m => Nargs(n, m - 1) ::: List(Id(n, m - 1))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    81
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    82
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    83
def Sigma(f: Rec) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    84
  val ar = arity(f)  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    85
  Pr(ar - 1, Cn(ar - 1, f, Nargs(ar - 1, ar - 1) :::
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    86
                    List(Cn(ar - 1, Const(0), List(Id(ar - 1, 0))))), 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    87
             Cn(ar + 1, Add, List(Id(ar + 1, ar), 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    88
                    Cn(ar + 1, f, Nargs(ar + 1, ar - 1) :::
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    89
                        List(Cn(ar + 1, S, List(Id(ar + 1, ar - 1))))))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    90
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    92
println("Add 3 4:   " + Add.eval(List(3, 4)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    93
println("Mult 3 4:  " + Mult.eval(List(3, 4)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    94
println("Pred 9:    " + Pred.eval(List(9)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    95
println("Pred 0:    " + Pred.eval(List(0)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    96
println("Minus 6 2: " + Minus.eval(List(6, 2)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    97
println("Minus 6 8: " + Minus.eval(List(6, 8)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    98
println("Const 8:   " + Const(8).eval(List(67)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    99
println("Sign 8:    " + Sign.eval(List(8)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   100
println("Sign 0:    " + Sign.eval(List(0)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   101
println("Less 4 4:  " + Less.eval(List(4, 4)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   102
println("Less 4 6:  " + Less.eval(List(4, 6)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   103
println("Less 6 4:  " + Less.eval(List(6, 4)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   104
println("Not 0:     " + Not.eval(List(0)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   105
println("Not 6:     " + Not.eval(List(6)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   106
println("Eq 4 4:    " + Eq.eval(List(4, 4)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   107
println("Eq 4 6:    " + Eq.eval(List(4, 6)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   108
println("Eq 6 4:    " + Eq.eval(List(6, 4)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   109
println("Conj 0 6:  " + Conj.eval(List(0, 6)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   110
println("Conj 6 4:  " + Conj.eval(List(6, 4)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   111
println("Conj 0 0:  " + Conj.eval(List(0, 0)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   112
println("Disj 0 6:  " + Disj.eval(List(0, 6)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   113
println("Disj 6 4:  " + Disj.eval(List(6, 4)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   114
println("Disj 0 0:  " + Disj.eval(List(0, 0)))
198
d93cc4295306 tuned some files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 195
diff changeset
   115
println("Sigma:     " + Sigma(Add).eval(List(2,3)))
d93cc4295306 tuned some files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 195
diff changeset
   116
195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   117
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   118
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   119
val ABCZero = Abacus(List(Goto(1)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   120
val ABCSucc = Plus(0, 1, 2, 7) ++ Abacus(List(Inc(1))).shift(Plus(0, 1, 2, 7).p.length, -1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
   121
def ABCId(n: Int, m: Int) = Plus(m, n, n + 1, 7)