18 println("TMMopup: " + (TMMopup(3).run(Tape(1,2,3,4,5)))) |
18 println("TMMopup: " + (TMMopup(3).run(Tape(1,2,3,4,5)))) |
19 |
19 |
20 |
20 |
21 // Abacus machine examples |
21 // Abacus machine examples |
22 def Copy(in: Int, out: Int, jump: Int) = |
22 def Copy(in: Int, out: Int, jump: Int) = |
23 Abacus(List(Dec(in, jump), Inc(out), Goto(0))) |
23 Abacus(Dec(in, jump), Inc(out), Goto(0)) |
24 |
24 |
25 def Plus(m: Int, n: Int, tmp: Int, jump: Int) = |
25 def Plus(m: Int, n: Int, tmp: Int, jump: Int) = |
26 Abacus(List(Dec(m, 4), Inc(n), Inc(tmp), Goto(0), Dec(tmp, jump), Inc(m), Goto(4))) |
26 Abacus(Dec(m, 4), Inc(n), Inc(tmp), Goto(0), Dec(tmp, jump), Inc(m), Goto(4)) |
27 |
27 |
28 def Mult(in1: Int, in2: Int, out: Int, tmp: Int, jump: Int) = |
28 def Mult(in1: Int, in2: Int, out: Int, tmp: Int, jump: Int) = |
29 Abacus(List(Dec(in1, jump))) ++ Plus(in2, out, tmp, -1).shift(1, -1).adjust(-1, 0) |
29 Abacus(Dec(in1, jump)) ++ Plus(in2, out, tmp, -1).shift(1, -1).adjust(-1, 0) |
30 |
30 |
31 def Expo(in1: Int, in2: Int, out: Int, tmp1: Int, tmp2: Int, jump: Int) = { |
31 def Expo(in1: Int, in2: Int, out: Int, tmp1: Int, tmp2: Int, jump: Int) = { |
32 Abacus(List(Inc(out), Dec(in1, jump))) ++ |
32 Abacus(Inc(out), Dec(in1, jump)) ++ |
33 Mult(out, in2, tmp2, tmp1, -1).shift(2, -1).adjust(-1, 10) ++ |
33 Mult(out, in2, tmp2, tmp1, -1).shift(2, -1).adjust(-1, 10) ++ |
34 Copy(tmp2, out, -1).shift(10, -1). adjust(-1, 1) |
34 Copy(tmp2, out, -1).shift(10, -1).adjust(-1, 1) |
35 } |
35 } |
36 |
36 |
37 println("Copy 3: " + (Copy(0, 1, -1).run(Map(0 -> 3, 1 -> 0)))) |
37 println("Copy 3: " + (Copy(0, 1, -1).run(Map(0 -> 3, 1 -> 0)))) |
38 println("Plus 3 + 4: " + (Plus(0, 1, 2, -1).run(Map(0 -> 3, 1 -> 4, 2 -> 0)))) |
38 println("Plus 3 + 4: " + (Plus(0, 1, 2, -1).run(Map(0 -> 3, 1 -> 4, 2 -> 0)))) |
39 println("Mult 3 * 5: " + (Mult(0, 1, 2, 3, -1).run(Map(0 -> 3, 1 -> 5, 2 -> 0, 3 -> 0)))) |
39 println("Mult 3 * 5: " + (Mult(0, 1, 2, 3, -1).run(Map(0 -> 3, 1 -> 5, 2 -> 0, 3 -> 0)))) |
44 println("Compiled Copy 3: " + toTM(Copy(0, 1, Int.MaxValue).p).run(Tape(3,0,0))) |
44 println("Compiled Copy 3: " + toTM(Copy(0, 1, Int.MaxValue).p).run(Tape(3,0,0))) |
45 println("Compiled Plus 3 + 4: " + toTM(Plus(0, 1, 2, Int.MaxValue).p).run(Tape(3,4,0,0))) |
45 println("Compiled Plus 3 + 4: " + toTM(Plus(0, 1, 2, Int.MaxValue).p).run(Tape(3,4,0,0))) |
46 println("Compiled Mult 3 * 5: " + toTM(Mult(0, 1, 2, 3, Int.MaxValue).p).run(Tape(3,5,0,0))) |
46 println("Compiled Mult 3 * 5: " + toTM(Mult(0, 1, 2, 3, Int.MaxValue).p).run(Tape(3,5,0,0))) |
47 println("Compiled Expo 3 ^ 4: " + toTM(Expo(0, 1, 2, 3, 4, 10000).p).run(Tape(3,4,0,0,0))) |
47 println("Compiled Expo 3 ^ 4: " + toTM(Expo(0, 1, 2, 3, 4, 10000).p).run(Tape(3,4,0,0,0))) |
48 |
48 |
49 // Recursive Function examples |
49 // Recursive function examples |
50 def arity(f: Rec) = f match { |
50 println("Add 3 4: " + Add.eval(3, 4)) |
51 case Z => 1 |
51 println("Mult 3 4: " + recs.Mult.eval(3, 4)) |
52 case S => 1 |
52 println("Twice 4: " + Twice.eval(4)) |
53 case Id(n, _) => n |
53 println("FourTm 4: " + Fourtimes.eval(4)) |
54 case Cn(n, _, _) => n |
54 println("Pred 9: " + Pred.eval(9)) |
55 case Pr(n, _, _) => n + 1 |
55 println("Pred 0: " + Pred.eval(0)) |
56 case Mn(n, _) => n |
56 println("Minus 6 2: " + Minus.eval(6, 2)) |
57 } |
57 println("Minus 6 8: " + Minus.eval(6, 8)) |
58 |
58 println("Const 8: " + Const(8).eval(67)) |
59 val Add = Pr(1, Id(1, 0), Cn(3, S, List(Id(3, 2)))) |
59 println("Power 2 3: " + Power.eval(2, 3)) |
60 val Mult = Pr(1, Z, Cn(3, Add, List(Id(3, 0), Id(3, 2)))) |
60 println("Sign 8: " + Sign.eval(8)) |
61 val Pred = Cn(1, Pr(1, Z, Id(3, 1)), List(Id(1, 0), Id(1, 0))) |
61 println("Sign 0: " + Sign.eval(0)) |
62 val Minus = Pr(1, Id(1, 0), Cn(3, Pred, List(Id(3, 2)))) |
62 println("Less 4 4: " + Less.eval(4, 4)) |
63 |
63 println("Less 4 6: " + Less.eval(4, 6)) |
64 def Const(n: Int) : Rec = n match { |
64 println("Less 6 4: " + Less.eval(6, 4)) |
65 case 0 => Z |
65 println("Not 0: " + Not.eval(0)) |
66 case n => Cn(1, S, List(Const(n - 1))) |
66 println("Not 6: " + Not.eval(6)) |
67 } |
67 println("Eq 4 4: " + Eq.eval(4, 4)) |
68 |
68 println("Eq 4 6: " + Eq.eval(4, 6)) |
69 val Sign = Cn(1, Minus, List(Const(1), Cn(1, Minus, List(Const(1), Id(1, 0))))) |
69 println("Eq 6 4: " + Eq.eval(6, 4)) |
70 val Less = Cn(2, Sign, List(Cn(2, Minus, List(Id(2, 1), Id(2, 0))))) |
70 println("Conj 0 6: " + Conj.eval(0, 6)) |
71 val Not = Cn(1, Minus, List(Const(1), Id(1, 0))) |
71 println("Conj 6 4: " + Conj.eval(6, 4)) |
72 val Eq = Cn(2, Minus, List(Cn(2, Const(1), List(Id(2, 0))), |
72 println("Conj 0 0: " + Conj.eval(0, 0)) |
73 Cn(2, Add, List(Cn(2, Minus, List(Id(2, 0), Id(2, 1))), |
73 println("Disj 0 6: " + Disj.eval(0, 6)) |
74 Cn(2, Minus, List(Id(2, 1), Id(2, 0))))))) |
74 println("Disj 6 4: " + Disj.eval(6, 4)) |
75 val Conj = Cn(2, Sign, List(Cn(2, Mult, List(Id(2, 0), Id(2, 1))))) |
75 println("Disj 0 0: " + Disj.eval(0, 0)) |
76 val Disj = Cn(2, Sign, List(Cn(2, Add, List(Id(2, 0), Id(2, 1))))) |
76 println("Sigma Add 2 3 -> 14: " + Sigma(Add).eval(2,3)) |
77 |
77 println("Accum Add 2 3 -> 120: " + Accum(Add).eval(2,3)) |
78 def Nargs(n: Int, m: Int) : List[Rec] = m match { |
78 println("Accum Mult 2 3 -> 0: " + Accum(recs.Mult).eval(2,3)) |
79 case 0 => Nil |
79 println("Fact 5: " + Fact.eval(5)) |
80 case m => Nargs(n, m - 1) ::: List(Id(n, m - 1)) |
80 println("Prime 0..15: " + (0 to 15).map(n => (n, Prime.eval(n)))) |
81 } |
81 println("NextPrime 3: " + NextPrime.eval(3)) |
82 |
82 println("NthPrime 1: " + NthPrime.eval(1)) |
83 def Sigma(f: Rec) = { |
83 println("Listsum [2, 3, 4 , 5, 6]: " + Listsum(5, 4).eval(2, 3, 4, 5, 6)) |
84 val ar = arity(f) |
84 // Ask Jian |
85 Pr(ar - 1, Cn(ar - 1, f, Nargs(ar - 1, ar - 1) ::: |
85 println("Strt: " + Strt(2).eval(2, 3)) |
86 List(Cn(ar - 1, Const(0), List(Id(ar - 1, 0))))), |
|
87 Cn(ar + 1, Add, List(Id(ar + 1, ar), |
|
88 Cn(ar + 1, f, Nargs(ar + 1, ar - 1) ::: |
|
89 List(Cn(ar + 1, S, List(Id(ar + 1, ar - 1)))))))) |
|
90 } |
|
91 |
|
92 println("Add 3 4: " + Add.eval(List(3, 4))) |
|
93 println("Mult 3 4: " + Mult.eval(List(3, 4))) |
|
94 println("Pred 9: " + Pred.eval(List(9))) |
|
95 println("Pred 0: " + Pred.eval(List(0))) |
|
96 println("Minus 6 2: " + Minus.eval(List(6, 2))) |
|
97 println("Minus 6 8: " + Minus.eval(List(6, 8))) |
|
98 println("Const 8: " + Const(8).eval(List(67))) |
|
99 println("Sign 8: " + Sign.eval(List(8))) |
|
100 println("Sign 0: " + Sign.eval(List(0))) |
|
101 println("Less 4 4: " + Less.eval(List(4, 4))) |
|
102 println("Less 4 6: " + Less.eval(List(4, 6))) |
|
103 println("Less 6 4: " + Less.eval(List(6, 4))) |
|
104 println("Not 0: " + Not.eval(List(0))) |
|
105 println("Not 6: " + Not.eval(List(6))) |
|
106 println("Eq 4 4: " + Eq.eval(List(4, 4))) |
|
107 println("Eq 4 6: " + Eq.eval(List(4, 6))) |
|
108 println("Eq 6 4: " + Eq.eval(List(6, 4))) |
|
109 println("Conj 0 6: " + Conj.eval(List(0, 6))) |
|
110 println("Conj 6 4: " + Conj.eval(List(6, 4))) |
|
111 println("Conj 0 0: " + Conj.eval(List(0, 0))) |
|
112 println("Disj 0 6: " + Disj.eval(List(0, 6))) |
|
113 println("Disj 6 4: " + Disj.eval(List(6, 4))) |
|
114 println("Disj 0 0: " + Disj.eval(List(0, 0))) |
|
115 println("Sigma: " + Sigma(Add).eval(List(2,3))) |
|
116 |
86 |
117 |
87 |
118 |
88 val ABCZero = Abacus(Goto(1)) |
119 val ABCZero = Abacus(List(Goto(1))) |
89 val ABCSucc = Plus(0, 1, 2, 7) ++ Abacus(Inc(1)).shift(Plus(0, 1, 2, 7).p.length, -1) |
120 val ABCSucc = Plus(0, 1, 2, 7) ++ Abacus(List(Inc(1))).shift(Plus(0, 1, 2, 7).p.length, -1) |
|
121 def ABCId(n: Int, m: Int) = Plus(m, n, n + 1, 7) |
90 def ABCId(n: Int, m: Int) = Plus(m, n, n + 1, 7) |