scala/turing.scala
changeset 209 b16dfc467b67
parent 194 fc2a5e9fbb97
child 210 5e2e576fac7c
equal deleted inserted replaced
208:3267acc1f97f 209:b16dfc467b67
    44   override def toString = join(l.reverse) + ">" + join(r)
    44   override def toString = join(l.reverse) + ">" + join(r)
    45 }
    45 }
    46 
    46 
    47 // standard tapes
    47 // standard tapes
    48 object Tape {
    48 object Tape {
    49   def apply(ns: Int*) : Tape = 
    49   def apply(ns: List[Int]) : Tape = 
    50     Tape(Nil, ns.map(n => Oc * (n + 1)).reduceLeft(_ ::: List(Bk) ::: _))
    50    Tape(Nil, ns.map(n => Oc * (n + 1)).reduceLeft(_ ::: List(Bk) ::: _))
       
    51 
       
    52   def apply(ns: Int*) : Tape = apply(ns.toList)
       
    53     
    51 }
    54 }
    52 
    55 
    53 // configurations
    56 // configurations
    54 case class Config(s: State, tp: Tape) {
    57 case class Config(s: State, tp: Tape) {
    55   def is_final = s == 0
    58   def is_final = s == 0
    58 }
    61 }
    59 
    62 
    60 // Turing machines
    63 // Turing machines
    61 case class TM(p: Prog) {
    64 case class TM(p: Prog) {
    62 
    65 
    63   // simple composition
    66   // composition
    64   def ++ (that: TM) = TM(this.p ::: that.p)
    67   def ++ (that: TM) = TM(this.p ::: that.p)
       
    68   def :+ (that: TM) = this.adjust ++ that.shift(this.p.length / 2 + 1)
    65 
    69 
    66   def shift(n: Int) =
    70   def shift(n: Int) =
    67     TM(p.map{case (a, s) => (a, if (s == 0) 0 else s + n)})
    71     TM(p.map{case (a, s) => (a, if (s == 0) 0 else s + n)})
    68   def adjust(n: Int) : TM =
    72   def adjust(n: Int) : TM =
    69     TM(p.map{case (a, s) => (a, if (s == 0) n else s)})
    73     TM(p.map{case (a, s) => (a, if (s == 0) n else s)})