--- a/turing.scala Thu Feb 21 14:27:14 2013 +0000
+++ b/turing.scala Thu Feb 21 14:49:26 2013 +0000
@@ -21,7 +21,7 @@
-abstract class Cell {
+sealed abstract class Cell {
def * (n: Int) : List[Cell] = n match {
case 0 => Nil
case n => this :: this * (n - 1)
@@ -30,7 +30,7 @@
case object Bk extends Cell { override def toString = "0" }
case object Oc extends Cell { override def toString = "1" }
-abstract class Action
+sealed abstract class Action
case object WBk extends Action
case object WOc extends Action
case object R extends Action
@@ -144,7 +144,7 @@
// Abacus machines
-abstract class AInst
+sealed abstract class AInst
case class Inc(n: Int) extends AInst
case class Dec(n: Int, l: Int) extends AInst
case class Goto(l: Int) extends AInst
@@ -181,7 +181,6 @@
case (Some(Goto(l)), _) => AConfig(l, cf.regs)
}
-
def steps(cf: AConfig, n: Int) : AConfig = n match {
case 0 => cf
case n => steps(step(cf), n - 1)
@@ -220,42 +219,45 @@
// Abacus to TM translation
-type Layout = List[Int]
-
def layout(p: AProg) = p.map(_ match {
case Inc(n) => 2 * n + 9
case Dec(n, _) => 2 * n + 16
case Goto(n) => 1
})
-def start(ly: Layout, n: Int) = ly.take(n).sum + 1
+def start(p: AProg, n: Int) = layout(p).take(n).sum + 1
-val TMInc = TM(List((WOc, 1), (R, 2), (WOc, 3), (R, 2), (WOc, 3), (R, 4),
- (L, 7), (WBk, 5), (R, 6), (WBk, 5), (WOc, 3), (R, 6),
- (L, 8), (L, 7), (R, 9), (L, 7), (R, 10), (WBk, 9)))
+def compile_Inc(s: Int, n: Int) = {
+ val TMInc = TM(List((WOc, 1), (R, 2), (WOc, 3), (R, 2), (WOc, 3), (R, 4),
+ (L, 7), (WBk, 5), (R, 6), (WBk, 5), (WOc, 3), (R, 6),
+ (L, 8), (L, 7), (R, 9), (L, 7), (R, 10), (WBk, 9)))
+ TMFindnth(n).shift(s - 1) ++ TMInc.shift(2 * n).shift(s - 1)
+}
-val TMDec = TM(List((WOc, 1), (R, 2), (L, 14), (R, 3), (L, 4), (R, 3),
- (R, 5), (WBk, 4), (R, 6), (WBk, 5), (L, 7), (L, 8),
- (L, 11), (WBk, 7), (WOc, 8), (R, 9), (L, 10), (R, 9),
- (R, 5), (WBk, 10), (L, 12), (L, 11), (R, 13), (L, 11),
- (R, 17), (WBk, 13), (L, 15), (L, 14), (R, 16), (L, 14),
- (R, 0), (WBk, 16)))
-
-def TMINC(s: Int, n: Int) = (TMFindnth(n) ++ TMInc.shift(2 * n)).shift(s - 1)
+def compile_Dec(s: Int, n: Int, e: Int) = {
+ val TMDec = TM(List((WOc, 1), (R, 2), (L, 14), (R, 3), (L, 4), (R, 3),
+ (R, 5), (WBk, 4), (R, 6), (WBk, 5), (L, 7), (L, 8),
+ (L, 11), (WBk, 7), (WOc, 8), (R, 9), (L, 10), (R, 9),
+ (R, 5), (WBk, 10), (L, 12), (L, 11), (R, 13), (L, 11),
+ (R, 17), (WBk, 13), (L, 15), (L, 14), (R, 16), (L, 14),
+ (R, 0), (WBk, 16)))
+ TMFindnth(n).shift(s - 1) ++ TMDec.shift(2 * n).shift(s - 1).adjust(e)
+}
-def TMDEC(s: Int, n: Int, e: Int) = TMFindnth(n).shift(s - 1) ++ TMDec.shift(2 * n).shift(s - 1).adjust(e)
-
-def TMGOTO(n: Int) = TM(List((Nop, n), (Nop, n)))
+def compile_Goto(s: Int) = {
+ val TMGoto = TM(List((Nop, 1), (Nop, 1)))
+ TMGoto.shift(s - 1)
+}
-def compile(ly: Layout, s: Int, i: AInst) = i match {
- case Inc(n) => TMINC(s, n)
- case Dec(n, e) => TMDEC(s, n, start(ly, e))
- case Goto(e) => TMGOTO(start(ly, e))
+def compile(p: AProg, s: Int, i: AInst) = i match {
+ case Inc(n) => compile_Inc(s, n)
+ case Dec(n, e) => compile_Dec(s, n, start(p, e))
+ case Goto(e) => compile_Goto(start(p, e))
}
def tms(p: AProg) = {
- val ss = (0 until p.length).map (start(layout(p),_))
- (ss zip p).map{case (n, i) => compile(layout(p), n, i)}
+ val ss = (0 until p.length).map (start(p,_))
+ (ss zip p).map{case (n, i) => compile(p, n, i)}
}
def toTM(p: AProg) = tms(p).reduceLeft(_ ++ _)