turing.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 21 Feb 2013 14:49:26 +0000
changeset 192 3c1107984b41
parent 191 98370b96c1c5
permissions -rw-r--r--
introduced sealed classes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
     1
import scala.annotation.tailrec
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
     2
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
     3
//some list functions
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
     4
def tl[A](xs: List[A]) : List[A] = xs match {
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
     5
  case Nil => Nil
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
     6
  case x::xs => xs
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
     7
}
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
     8
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
     9
def hd[A](xs: List[A]) : A = xs.head
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    10
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    11
def nth_of[A](xs: List[A], n: Int) = 
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    12
  if (n <= xs.length) Some(xs(n)) else None 
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    13
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    14
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    15
//some map functions
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    16
def dget(m: Map[Int, Int], n: Int) = m.getOrElse(n, 0)
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    17
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    18
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    19
//some string functions
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    20
def join[A](xs: List[A]) = xs.foldLeft("")(_.toString + _)
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    21
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    22
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    23
192
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
    24
sealed abstract class Cell {
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    25
  def * (n: Int) : List[Cell] = n match {
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    26
    case 0 => Nil
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    27
    case n => this :: this * (n - 1)
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    28
  }
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    29
}
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
case object Bk extends Cell { override def toString = "0" }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
case object Oc extends Cell { override def toString = "1" }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
192
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
    33
sealed abstract class Action
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
case object WBk extends Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
case object WOc extends Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
case object R extends Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
case object L extends Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
case object Nop extends Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
type State = Int
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    41
type Inst = (Action, State)
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    42
type Prog = List[Inst]
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
//tapes
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
case class Tape(l: List[Cell], r: List[Cell]) {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  def update(a: Action) = a match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
    case WBk => Tape(l, Bk::tl(r))
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
    case WOc => Tape(l, Oc::tl(r))
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
    case L => if (l == Nil) Tape(Nil, Bk::r) else Tape (tl(l), hd(l)::r)
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
    case R => if (r == Nil) Tape(Bk::l, Nil) else Tape(hd(r)::l, tl(r))
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
    case Nop => Tape(l, r)
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  def read = r match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
    case Nil => Bk
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
    case x::_ => x
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    59
  override def toString = join(l.reverse) + ">" + join(r)
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
}
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
// standard tapes
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    63
class STape(ns: Int*) extends 
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    64
  Tape(Nil, ns.map(n => Oc * (n + 1)).reduceLeft(_ ::: List(Bk) ::: _))
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    65
  
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
// configurations
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
case class Config(s: State, tp: Tape) {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  def is_final = s == 0
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  override def toString = tp.toString
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
}
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
183
4cf023ee2f4c updated exponent program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
    75
// Turing machines
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
    76
case class TM(p: Prog) {
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    77
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    78
  def ++ (that: TM) = TM(this.p ::: that.p)
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    79
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    80
  def shift(n: Int) =
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    81
    TM(p.map{case (a, s) => (a, if (s == 0) 0 else s + n)})
189
5974111de158 parts of the Abacus translation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 188
diff changeset
    82
  def adjust(n: Int) : TM =
5974111de158 parts of the Abacus translation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 188
diff changeset
    83
    TM(p.map{case (a, s) => (a, if (s == 0) n else s)})
5974111de158 parts of the Abacus translation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 188
diff changeset
    84
  def adjust : TM = adjust(p.length / 2 + 1)
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
  
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  def fetch(s: State, a: Cell) = (s, a) match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
    case (0, _) => (Nop, 0)
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    88
    case (s, Bk) => nth_of(p, 2 * (s - 1)) match {
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    89
      case None => (Nop, 0)
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    90
      case Some(i) => i
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    91
    }
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
    case (s, Oc) => nth_of(p, 2 * (s - 1) + 1) match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
      case None => (Nop, 0)
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
      case Some(i) => i
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
    }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  } 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  def step(cf: Config) : Config = fetch (cf.s, cf.tp.read) match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
    case (a, s_next) => Config(s_next, cf.tp.update(a))
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
  }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  def steps(cf: Config, n: Int) : Config = n match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
    case 0 => cf
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
    case n => steps(step(cf), n - 1)
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
  } 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   107
  @tailrec
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   108
  final def run(cf: Config) : Config = {
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
    if (cf.is_final) cf else run(step(cf))
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
  }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
 
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   112
  def run(tp: Tape) : Tape = run(Config(1, tp)).tp
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
}
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
183
4cf023ee2f4c updated exponent program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
   116
// Turing machine examples
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   117
val TMCopy = TM(List((WBk, 5), (R, 2), (R, 3), (R, 2), (WOc, 3), 
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   118
                     (L, 4), (L, 4), (L, 5), (R, 11), (R, 6), 
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   119
                     (R, 7), (WBk, 6), (R, 7), (R, 8), (WOc, 9), 
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   120
                     (R, 8), (L, 10), (L, 9), (L, 10), (L, 5), 
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   121
                     (L, 0), (R, 12), (WOc, 13), (L, 14), (R, 12), 
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   122
                     (R, 12), (L, 15), (WBk, 14), (R, 0), (L, 15)))
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   123
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   124
def TMFindnth(n: Int) : TM = n match {
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   125
  case 0 => TM(Nil)
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   126
  case n => TMFindnth(n - 1) ++ TM(List((WOc, 2 * n - 1), (R, 2 * n), (R, 2 * n + 1), (R, 2 * n)))
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   127
}
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   128
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   129
def TMMopup(n: Int) = {
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   130
  def TMMopup1(n: Int) : TM = n match {
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   131
    case 0 => TM(Nil)
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   132
    case n => TMMopup1(n - 1) ++ TM(List((R, 2 * n + 1), (WBk, 2 * n), (R, 2 * n - 1), (WOc, 2 * n)))
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   133
  }
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   134
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   135
  val TMMopup2 = TM(List((R, 2), (R, 1), (L, 5), (WBk, 3), (R, 4), (WBk, 3),
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   136
                         (R, 2), (WBk, 3), (L, 5), (L, 6), (R, 0), (L, 6)))
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   137
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   138
  TMMopup1(n) ++ TMMopup2.shift(2 * n)
178
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   139
}
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   140
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   141
println("TMCopy:    " + (TMCopy.run(new STape(3))))
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   142
println("TMfindnth: " + (TMFindnth(3).run(new STape(1,2,3,4,5))))
8248f8adf17d added some TM machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   143
println("TMMopup: "   + (TMMopup(3).run(new STape(1,2,3,4,5))))
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   144
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   145
183
4cf023ee2f4c updated exponent program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
   146
// Abacus machines
192
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   147
sealed abstract class AInst 
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   148
case class Inc(n: Int) extends AInst
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   149
case class Dec(n: Int, l: Int) extends AInst
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   150
case class Goto(l: Int) extends AInst
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   151
180
8f443f2ed1f6 added abacus machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   152
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   153
type AProg = List[AInst]
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   154
type Regs = Map[Int, Int]
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   155
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   156
case class AConfig(s: Int, regs: Regs)  
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   158
case class Abacus(p: AProg) {
180
8f443f2ed1f6 added abacus machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   159
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   160
  def ++ (that: Abacus) = Abacus(this.p ::: that.p)
189
5974111de158 parts of the Abacus translation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 188
diff changeset
   161
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   162
  def shift(offset: Int, jump: Int) = Abacus(p.map(_ match {
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   163
    case Inc(n) => Inc(n)
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   164
    case Dec(n, l) => if (l == jump) Dec(n, l) else Dec(n, l + offset)
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   165
    case Goto(l) => if (l == jump) Goto(l) else Goto(l + offset)
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   166
  }))
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   167
      
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   168
  def adjust(old_jump: Int, jump: Int) = Abacus(p.map(_ match {
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   169
    case Inc(n) => Inc(n)
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   170
    case Dec(n, l) => if (l == old_jump) Dec(n, jump) else Dec(n, l)
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   171
    case Goto(l) => if (l == old_jump) Goto(jump) else Goto(l)
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   172
  }))
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   173
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   174
  def step(cf: AConfig) : AConfig = (nth_of(p, cf.s), cf.s) match {
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   175
    case (None, _) => cf
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   176
    case (Some(Inc(n)), s) => AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) + 1)))
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   177
    case (Some(Dec(n, l)), s) => {
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   178
      if (dget(cf.regs, n) == 0) AConfig(l, cf.regs)
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   179
      else AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) - 1)))
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   180
    }
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   181
    case (Some(Goto(l)), _) => AConfig(l, cf.regs)
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   182
  }  
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   183
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   184
  def steps(cf: AConfig, n: Int) : AConfig = n match {
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   185
    case 0 => cf
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   186
    case n => steps(step(cf), n - 1)
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   187
  } 
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   189
  def steps(regs: Regs, n: Int) : AConfig = steps(AConfig(0, regs), n)
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   190
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   191
  def run(cf: AConfig) : AConfig = {
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   192
    if (cf.s >= p.length || cf.s < 0) cf else run(step(cf))
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   193
  }
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   194
 
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   195
  def run(regs: Regs): Regs = run(AConfig(0, regs)).regs
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   196
}
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   197
183
4cf023ee2f4c updated exponent program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
   198
// Abacus examples
4cf023ee2f4c updated exponent program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
   199
def Copy(in: Int, out: Int, jump: Int) = 
4cf023ee2f4c updated exponent program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
   200
  Abacus(List(Dec(in, jump), Inc(out), Goto(0))) 
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   201
180
8f443f2ed1f6 added abacus machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   202
def Plus(m: Int, n: Int, tmp: Int, jump: Int) =
189
5974111de158 parts of the Abacus translation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 188
diff changeset
   203
  Abacus(List(Dec(m, 4), Inc(n), Inc(tmp), Goto(0), Dec(tmp, jump), Inc(m), Goto(4))) 
180
8f443f2ed1f6 added abacus machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   204
189
5974111de158 parts of the Abacus translation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 188
diff changeset
   205
def Mult(in1: Int, in2: Int, out: Int, tmp: Int, jump: Int) = 
5974111de158 parts of the Abacus translation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 188
diff changeset
   206
  Abacus(List(Dec(in1, jump))) ++ Plus(in2, out, tmp, -1).shift(1, -1).adjust(-1, 0)
180
8f443f2ed1f6 added abacus machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   207
183
4cf023ee2f4c updated exponent program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
   208
def Expo(in1: Int, in2: Int, out: Int, tmp1: Int, tmp2: Int, jump: Int) = {
189
5974111de158 parts of the Abacus translation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 188
diff changeset
   209
  Abacus(List(Inc(out), Dec(in1, jump))) ++ 
5974111de158 parts of the Abacus translation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 188
diff changeset
   210
  Mult(out, in2, tmp2, tmp1, -1).shift(2, -1).adjust(-1, 10) ++
5974111de158 parts of the Abacus translation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 188
diff changeset
   211
  Copy(tmp2, out, -1).shift(10, -1). adjust(-1, 1)
180
8f443f2ed1f6 added abacus machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   212
}
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   213
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   214
println("Copy: 3     " + (Copy(0, 1, -1).run(Map(0 -> 3, 1 -> 0))))
183
4cf023ee2f4c updated exponent program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
   215
println("Plus: 3 + 4 " + (Plus(0, 1, 2, -1).run(Map(0 -> 3, 1 -> 4, 2 -> 0))))
4cf023ee2f4c updated exponent program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
   216
println("Mult: 3 * 5 " + (Mult(0, 1, 2, 3, -1).run(Map(0 -> 3, 1 -> 5, 2 -> 0, 3 -> 0))))
4cf023ee2f4c updated exponent program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
   217
println("Expo: 3 ^ 4 " + (Expo(0, 1, 2, 3, 4, -1).run(Map(0 -> 4, 1 -> 3, 2 -> 0, 3 -> 0, 4 -> 0))))
179
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   218
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   219
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   220
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   221
// Abacus to TM translation
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   222
def layout(p: AProg) = p.map(_ match {
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   223
  case Inc(n) => 2 * n + 9 
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   224
  case Dec(n, _) => 2 * n + 16 
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   225
  case Goto(n) => 1
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   226
})
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   227
192
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   228
def start(p: AProg, n: Int) = layout(p).take(n).sum + 1
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   229
192
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   230
def compile_Inc(s: Int, n: Int) = {
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   231
  val TMInc = TM(List((WOc, 1), (R, 2), (WOc, 3), (R, 2), (WOc, 3), (R, 4), 
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   232
                      (L, 7), (WBk, 5), (R, 6), (WBk, 5), (WOc, 3), (R, 6),
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   233
                      (L, 8), (L, 7), (R, 9), (L, 7), (R, 10), (WBk, 9)))
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   234
  TMFindnth(n).shift(s - 1) ++ TMInc.shift(2 * n).shift(s - 1)
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   235
}
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   236
192
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   237
def compile_Dec(s: Int, n: Int, e: Int) = {
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   238
  val TMDec = TM(List((WOc, 1), (R, 2), (L, 14), (R, 3), (L, 4), (R, 3),
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   239
                      (R, 5), (WBk, 4), (R, 6), (WBk, 5), (L, 7), (L, 8),
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   240
                      (L, 11), (WBk, 7), (WOc, 8), (R, 9), (L, 10), (R, 9),
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   241
                      (R, 5), (WBk, 10), (L, 12), (L, 11), (R, 13), (L, 11),
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   242
                      (R, 17), (WBk, 13), (L, 15), (L, 14), (R, 16), (L, 14),
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   243
                      (R, 0), (WBk, 16)))
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   244
  TMFindnth(n).shift(s - 1) ++ TMDec.shift(2 * n).shift(s - 1).adjust(e)
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   245
}
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   246
192
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   247
def compile_Goto(s: Int) = {
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   248
  val TMGoto = TM(List((Nop, 1), (Nop, 1)))
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   249
  TMGoto.shift(s - 1)
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   250
}
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   251
192
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   252
def compile(p: AProg, s: Int, i: AInst) = i match {
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   253
  case Inc(n) => compile_Inc(s, n)
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   254
  case Dec(n, e) => compile_Dec(s, n, start(p, e))
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   255
  case Goto(e) => compile_Goto(start(p, e))
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   256
}
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   257
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   258
def tms(p: AProg) = {
192
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   259
  val ss = (0 until p.length).map (start(p,_))
3c1107984b41 introduced sealed classes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   260
  (ss zip p).map{case (n, i) => compile(p, n, i)}
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   261
}
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   262
191
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   263
def toTM(p: AProg) = tms(p).reduceLeft(_ ++ _)
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   264
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   265
//examples
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   266
println("Copy 3:     " + toTM(Copy(0, 1, Int.MaxValue).p).run(new STape(3,0,0)))
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   267
println("Plus 3 + 4: " + toTM(Plus(0, 1, 2, Int.MaxValue).p).run(new STape(3,4,0,0)))
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   268
println("Mult 3 * 5: " + toTM(Mult(0, 1, 2, 3, Int.MaxValue).p).run(new STape(3,5,0,0)))
98370b96c1c5 compilation from Abacus to Turing in Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   269
println("Expo 3 ^ 4: " + toTM(Expo(0, 1, 2, 3, 4, 10000).p).run(new STape(3,4,0,0,0)))
188
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   270
8939cc9b14f9 tuned Scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   271
179
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   272
//Recursive Functions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   273
abstract class Rec {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   274
  def eval(ns: List[Int]) : Int
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   275
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   276
case object Z extends Rec {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   277
  override def eval(ns: List[Int]) = ns match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   278
    case n::Nil => 0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   279
    case _ => throw new IllegalArgumentException("Z: args")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   280
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   281
} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   282
case object S extends Rec {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   283
  override def eval(ns: List[Int]) = ns match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   284
    case n::Nil => n + 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   285
    case _ => throw new IllegalArgumentException("S: args")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   286
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   287
} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   288
case class Id(n: Int, m: Int) extends Rec {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   289
  override def eval(ns: List[Int]) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   290
    if (ns.length == n && m < n) ns(m)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   291
    else throw new IllegalArgumentException("Id: args")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   292
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   293
case class Cn(n: Int, f: Rec, gs: List[Rec]) extends Rec {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   294
  override def eval(ns: List[Int]) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   295
    if (ns.length == n) f.eval(gs.map(_.eval(ns)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   296
    else throw new IllegalArgumentException("Cn: args")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   297
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   298
case class Pr(n: Int, f: Rec, g: Rec) extends Rec {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   299
  override def eval(ns: List[Int]) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   300
    if (ns.length == n - 1) {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   301
      if (ns.last == 0) f.eval(ns.init)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   302
      else {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   303
        val r = Pr(n, f, g).eval(ns.init ::: List(ns.last - 1))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   304
        g.eval(ns.init ::: List(ns.last - 1, r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   305
      }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   306
    }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   307
    else throw new IllegalArgumentException("Cn: args")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   308
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   309
case class Mn(n: Int, f: Rec) extends Rec {
180
8f443f2ed1f6 added abacus machines
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   310
  override def eval(ns: List[Int]) = 0
179
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   311
    
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   312
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
   313