turing.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 16 Feb 2013 09:07:07 +0000
changeset 177 1c2e639d4e54
parent 176 d2c85556d290
child 178 8248f8adf17d
permissions -rw-r--r--
added abacus programs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
abstract class Cell
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
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
     3
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
     4
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
abstract class Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
case object WBk extends Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
case object WOc extends Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case object R extends Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case object L extends Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case object Nop extends Action
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
type State = Int
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
type Prog = List[(Action, State)]
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
//some list functions
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
def tl[A](xs: List[A]) : List[A] = xs match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  case Nil => Nil
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  case x::xs => xs
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
}
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
def hd[A](xs: List[A]) : A = xs.head
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
def nth_of[A](xs: List[A], n: Int) = 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  if (n <= xs.length) Some(xs(n)) else None 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
    26
//some map functions
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
    27
def dget(m: Map[Int, Int], n: Int) = m.getOrElse(n, 0)
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
    28
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
    29
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
//tapes
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
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
    33
  def update(a: Action) = a match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
    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
    35
    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
    36
    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
    37
    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
    38
    case Nop => Tape(l, r)
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
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def read = r match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
    case Nil => Bk
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
    case x::_ => x
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  override def toString = 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
    l.reverse.map(_.toString).foldLeft("")(_+_) + 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
    ">" + r.map(_.toString).foldLeft("")(_+_)
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
}
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
// standard tapes
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
def std(n: Int) : List[Cell] = n match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  case 0 => List(Oc)
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  case n => Oc::std(n - 1)
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
}
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
class STape(ns: Int*) extends Tape(Nil, ns.map(std).reduceLeft(_ ::: List(Bk) ::: _))
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
// configurations
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
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
    61
  def is_final = s == 0
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  override def toString = tp.toString
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
}
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
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
// Turing Machines
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
    68
case class TM(p: Prog) {
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
  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
    71
    case (0, _) => (Nop, 0)
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
      case (s, Bk) => nth_of(p, 2 * (s - 1)) match {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
        case None => (Nop, 0)
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
        case Some(i) => i
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
      }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
    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
    77
      case None => (Nop, 0)
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
      case Some(i) => i
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
    }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
  } 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
  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
    83
    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
    84
  }
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 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
    87
    case 0 => cf
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
    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
    89
  } 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
  def run(cf: Config) : Config = {
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
    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
    93
  }
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
 
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
    95
  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
    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
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
// examples
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
class TMCopy(n: Int) extends 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  TM(List((WBk, 5), (R, 2), (R, 3), (R, 2), (WOc, 3), 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
          (L, 4), (L, 4), (L, 5), (R, 11), (R, 6), 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
          (R, 7), (WBk, 6), (R, 7), (R, 8), (WOc, 9), 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
          (R, 8), (L, 10), (L, 9), (L, 10), (L, 5), 
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
          (L, 0), (R, 12), (WOc, 13), (L, 14), (R, 12), 
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   107
          (R, 12), (L, 15), (WBk, 14), (R, 0), (L, 15)))
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   108
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   109
println("TM: " + (new TMCopy(0)).run(new STape(3)))
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   110
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   111
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   112
// Abacus
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   113
abstract class AInst 
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   114
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
   115
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
   116
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
   117
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   118
type AProg = List[AInst]
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   119
type Regs = Map[Int, Int]
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   120
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   121
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   122
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   123
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
   124
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   125
case class Abacus(p: AProg) {
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   126
  
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   127
  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
   128
    case (None, _) => cf
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   129
    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
   130
    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
   131
      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
   132
      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
   133
    }
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   134
    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
   135
  }  
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   136
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   137
  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
   138
    case 0 => cf
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   139
    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
   140
  } 
176
d2c85556d290 added scala file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
177
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   142
  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
   143
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   144
  def run(cf: AConfig) : AConfig = {
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   145
    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
   146
  }
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   147
 
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   148
  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
   149
}
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   150
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   151
// examples
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   152
class Copy(in: Int, out: Int) extends 
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   153
  Abacus(List(Dec(in, -1), Inc(out), Goto(0))) 
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   154
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   155
class Plus(in1: Int, in2: Int, out: Int) extends 
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   156
  Abacus(List(Dec(in1, 4), Inc(in2), Inc(out),
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   157
              Goto(0), Dec(out, -1), Inc(in1), Goto(4))) 
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   158
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   159
1c2e639d4e54 added abacus programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   160
println("Ab: " + (new Plus(0, 1, 2)).run(Map(0 -> 3, 1 -> 4)))