shen-ruby 0.12.1 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +5 -0
- data/README.md +8 -12
- data/Rakefile +4 -9
- data/bin/shen_test_suite.rb +0 -1
- data/bin/srrepl +2 -4
- data/lib/shen_ruby/shen.rb +98 -0
- data/lib/shen_ruby/version.rb +1 -1
- data/shen-ruby.gemspec +3 -3
- data/shen/README.txt +9 -13
- data/shen/release/BSD +24 -0
- data/shen/release/klambda/core.kl +157 -0
- data/shen/release/klambda/declarations.kl +109 -0
- data/shen/release/klambda/load.kl +59 -0
- data/shen/release/klambda/macros.kl +91 -0
- data/shen/release/klambda/prolog.kl +228 -0
- data/shen/release/klambda/reader.kl +198 -0
- data/shen/release/klambda/sequent.kl +142 -0
- data/shen/release/klambda/sys.kl +253 -0
- data/shen/release/klambda/t-star.kl +123 -0
- data/shen/release/klambda/toplevel.kl +110 -0
- data/shen/release/klambda/track.kl +79 -0
- data/shen/release/{k_lambda → klambda}/types.kl +41 -63
- data/shen/release/klambda/writer.kl +81 -0
- data/shen/release/klambda/yacc.kl +87 -0
- data/shen/release/license.pdf +0 -0
- data/shen/release/test_programs/Chap13/problems.txt +26 -26
- data/shen/release/test_programs/README.shen +52 -52
- data/shen/release/test_programs/TinyLispFunctions.txt +15 -15
- data/shen/release/test_programs/TinyTypes.shen +55 -55
- data/shen/release/test_programs/binary.shen +24 -24
- data/shen/release/test_programs/bubble_version_1.shen +28 -28
- data/shen/release/test_programs/bubble_version_2.shen +22 -22
- data/shen/release/test_programs/calculator.shen +21 -21
- data/shen/release/test_programs/cartprod.shen +23 -23
- data/shen/release/test_programs/change.shen +25 -25
- data/shen/release/test_programs/classes-defaults.shen +94 -94
- data/shen/release/test_programs/classes-inheritance.shen +100 -100
- data/shen/release/test_programs/classes-typed.shen +74 -74
- data/shen/release/test_programs/classes-untyped.shen +46 -46
- data/shen/release/test_programs/depth_.shen +14 -14
- data/shen/release/test_programs/einstein.shen +34 -34
- data/shen/release/test_programs/fruit_machine.shen +46 -46
- data/shen/release/test_programs/interpreter.shen +217 -217
- data/shen/release/test_programs/metaprog.shen +85 -85
- data/shen/release/test_programs/minim.shen +192 -192
- data/shen/release/test_programs/mutual.shen +11 -11
- data/shen/release/test_programs/n_queens.shen +45 -45
- data/shen/release/test_programs/newton_version_1.shen +33 -33
- data/shen/release/test_programs/newton_version_2.shen +24 -24
- data/shen/release/test_programs/parse.prl +14 -14
- data/shen/release/test_programs/parser.shen +51 -51
- data/shen/release/test_programs/powerset.shen +10 -10
- data/shen/release/test_programs/prime.shen +10 -10
- data/shen/release/test_programs/prolog.shen +78 -78
- data/shen/release/test_programs/proof_assistant.shen +80 -80
- data/shen/release/test_programs/proplog_version_1.shen +25 -25
- data/shen/release/test_programs/proplog_version_2.shen +27 -27
- data/shen/release/test_programs/qmachine.shen +66 -66
- data/shen/release/test_programs/red-black.shen +54 -54
- data/shen/release/test_programs/search.shen +55 -55
- data/shen/release/test_programs/semantic_net.shen +44 -44
- data/shen/release/test_programs/spreadsheet.shen +34 -34
- data/shen/release/test_programs/stack.shen +27 -27
- data/shen/release/test_programs/streams.shen +20 -20
- data/shen/release/test_programs/strings.shen +57 -57
- data/shen/release/test_programs/structures-typed.shen +71 -71
- data/shen/release/test_programs/structures-untyped.shen +41 -41
- data/shen/release/test_programs/tests.shen +232 -232
- data/shen/release/test_programs/types.shen +11 -11
- data/shen/release/test_programs/whist.shen +239 -239
- data/shen/release/test_programs/yacc.shen +132 -132
- metadata +21 -35
- data/shen/lib/shen_ruby/shen.rb +0 -160
- data/shen/license.txt +0 -34
- data/shen/release/benchmarks/N_queens.shen +0 -45
- data/shen/release/benchmarks/README.shen +0 -14
- data/shen/release/benchmarks/benchmarks.shen +0 -52
- data/shen/release/benchmarks/bigprog +0 -2173
- data/shen/release/benchmarks/einstein.shen +0 -33
- data/shen/release/benchmarks/heatwave.gif +0 -0
- data/shen/release/benchmarks/interpreter.shen +0 -219
- data/shen/release/benchmarks/jnk.shen +0 -194
- data/shen/release/benchmarks/picture.jpg +0 -0
- data/shen/release/benchmarks/plato.jpg +0 -0
- data/shen/release/benchmarks/powerset.shen +0 -10
- data/shen/release/benchmarks/prime.shen +0 -10
- data/shen/release/benchmarks/short.shen +0 -129
- data/shen/release/benchmarks/text.txt +0 -68
- data/shen/release/k_lambda/core.kl +0 -181
- data/shen/release/k_lambda/declarations.kl +0 -131
- data/shen/release/k_lambda/load.kl +0 -84
- data/shen/release/k_lambda/macros.kl +0 -112
- data/shen/release/k_lambda/prolog.kl +0 -252
- data/shen/release/k_lambda/reader.kl +0 -222
- data/shen/release/k_lambda/sequent.kl +0 -166
- data/shen/release/k_lambda/sys.kl +0 -271
- data/shen/release/k_lambda/t-star.kl +0 -139
- data/shen/release/k_lambda/toplevel.kl +0 -135
- data/shen/release/k_lambda/track.kl +0 -103
- data/shen/release/k_lambda/writer.kl +0 -105
- data/shen/release/k_lambda/yacc.kl +0 -113
@@ -1,34 +1,34 @@
|
|
1
|
-
(defprolog einsteins_riddle
|
2
|
-
Fish_Owner <-- (einstein Houses Fish_Owner);)
|
3
|
-
|
4
|
-
(defprolog einstein
|
5
|
-
Houses Fish_Owner <-- (unify Houses [[house norwegian _ _ _ _] _ [house _ _ _ milk _] _ _])
|
6
|
-
(member [house brit _ _ _ red] Houses)
|
7
|
-
(member [house swede dog _ _ _] Houses)
|
8
|
-
(member [house dane _ _ tea _] Houses)
|
9
|
-
(iright [house _ _ _ _ green] [house _ _ _ _ white] Houses)
|
10
|
-
(member [house _ _ _ coffee green] Houses)
|
11
|
-
(member [house _ bird pallmall _ _] Houses)
|
12
|
-
(member [house _ _ dunhill _ yellow] Houses)
|
13
|
-
(next_to [house _ _ dunhill _ _] [house _ horse _ _ _] Houses)
|
14
|
-
(member [house _ _ _ milk _] Houses)
|
15
|
-
(next_to [house _ _ marlboro _ _] [house _ cat _ _ _] Houses)
|
16
|
-
(next_to [house _ _ marlboro _ _] [house _ _ _ water _] Houses)
|
17
|
-
(member [house _ _ winfield beer _] Houses)
|
18
|
-
(member [house german _ rothmans _ _] Houses)
|
19
|
-
(next_to [house norwegian _ _ _ _] [house _ _ _ _ blue] Houses)
|
20
|
-
(unify Houses [[house norwegian _ _ _ _] _ [house _ _ _ milk _] _ _])
|
21
|
-
(member [house Fish_Owner fish _ _ _] Houses);)
|
22
|
-
|
23
|
-
(defprolog member
|
24
|
-
X [X | _] <--;
|
25
|
-
X [_ | Z] <-- (member X Z);)
|
26
|
-
|
27
|
-
(defprolog next_to
|
28
|
-
X Y List <-- (iright X Y List);
|
29
|
-
X Y List <-- (iright Y X List);)
|
30
|
-
|
31
|
-
(defprolog iright
|
32
|
-
L R (mode [L | [R | _]] -) <--;
|
33
|
-
L R (mode [_ | Rest] -) <-- (iright L R Rest);)
|
34
|
-
|
1
|
+
(defprolog einsteins_riddle
|
2
|
+
Fish_Owner <-- (einstein Houses Fish_Owner);)
|
3
|
+
|
4
|
+
(defprolog einstein
|
5
|
+
Houses Fish_Owner <-- (unify Houses [[house norwegian _ _ _ _] _ [house _ _ _ milk _] _ _])
|
6
|
+
(member [house brit _ _ _ red] Houses)
|
7
|
+
(member [house swede dog _ _ _] Houses)
|
8
|
+
(member [house dane _ _ tea _] Houses)
|
9
|
+
(iright [house _ _ _ _ green] [house _ _ _ _ white] Houses)
|
10
|
+
(member [house _ _ _ coffee green] Houses)
|
11
|
+
(member [house _ bird pallmall _ _] Houses)
|
12
|
+
(member [house _ _ dunhill _ yellow] Houses)
|
13
|
+
(next_to [house _ _ dunhill _ _] [house _ horse _ _ _] Houses)
|
14
|
+
(member [house _ _ _ milk _] Houses)
|
15
|
+
(next_to [house _ _ marlboro _ _] [house _ cat _ _ _] Houses)
|
16
|
+
(next_to [house _ _ marlboro _ _] [house _ _ _ water _] Houses)
|
17
|
+
(member [house _ _ winfield beer _] Houses)
|
18
|
+
(member [house german _ rothmans _ _] Houses)
|
19
|
+
(next_to [house norwegian _ _ _ _] [house _ _ _ _ blue] Houses)
|
20
|
+
(unify Houses [[house norwegian _ _ _ _] _ [house _ _ _ milk _] _ _])
|
21
|
+
(member [house Fish_Owner fish _ _ _] Houses);)
|
22
|
+
|
23
|
+
(defprolog member
|
24
|
+
X [X | _] <--;
|
25
|
+
X [_ | Z] <-- (member X Z);)
|
26
|
+
|
27
|
+
(defprolog next_to
|
28
|
+
X Y List <-- (iright X Y List);
|
29
|
+
X Y List <-- (iright Y X List);)
|
30
|
+
|
31
|
+
(defprolog iright
|
32
|
+
L R (mode [L | [R | _]] -) <--;
|
33
|
+
L R (mode [_ | Rest] -) <-- (iright L R Rest);)
|
34
|
+
|
@@ -1,46 +1,46 @@
|
|
1
|
-
(define return-fruit
|
2
|
-
0 -> cherry
|
3
|
-
1 -> cherry
|
4
|
-
2 -> cherry
|
5
|
-
3 -> cherry
|
6
|
-
4 -> cherry
|
7
|
-
5 -> pear
|
8
|
-
6 -> pear
|
9
|
-
7 -> pear
|
10
|
-
8 -> pear
|
11
|
-
9 -> orange
|
12
|
-
10 -> orange
|
13
|
-
11 -> orange
|
14
|
-
12 -> pineapple
|
15
|
-
13 -> pineapple
|
16
|
-
14 -> lemon)
|
17
|
-
|
18
|
-
(define spin-wheel
|
19
|
-
-> (return-fruit (random 14)))
|
20
|
-
|
21
|
-
(define payoff
|
22
|
-
cherry cherry cherry -> 60
|
23
|
-
pear pear pear -> 100
|
24
|
-
orange orange orange -> 200
|
25
|
-
pineapple pineapple pineapple -> 300
|
26
|
-
lemon lemon lemon -> 500
|
27
|
-
cherry cherry X -> 10
|
28
|
-
X cherry cherry -> 10
|
29
|
-
pear pear X -> 20
|
30
|
-
X pear pear -> 20
|
31
|
-
orange orange X -> 30
|
32
|
-
X orange orange -> 30
|
33
|
-
pineapple pineapple X -> 40
|
34
|
-
X pineapple pineapple -> 40
|
35
|
-
lemon lemon X -> 50
|
36
|
-
X lemon lemon -> 50
|
37
|
-
X Y Z -> 0)
|
38
|
-
|
39
|
-
(define fruit-machine
|
40
|
-
start -> (announce-payoff (spin-wheel) (spin-wheel) (spin-wheel)))
|
41
|
-
|
42
|
-
(define announce-payoff
|
43
|
-
Fruit1 Fruit2 Fruit3
|
44
|
-
-> (output "~A ~A ~A~%You win ~A pence~%"
|
45
|
-
Fruit1 Fruit2 Fruit3 (payoff Fruit1 Fruit2 Fruit3)))
|
46
|
-
|
1
|
+
(define return-fruit
|
2
|
+
0 -> cherry
|
3
|
+
1 -> cherry
|
4
|
+
2 -> cherry
|
5
|
+
3 -> cherry
|
6
|
+
4 -> cherry
|
7
|
+
5 -> pear
|
8
|
+
6 -> pear
|
9
|
+
7 -> pear
|
10
|
+
8 -> pear
|
11
|
+
9 -> orange
|
12
|
+
10 -> orange
|
13
|
+
11 -> orange
|
14
|
+
12 -> pineapple
|
15
|
+
13 -> pineapple
|
16
|
+
14 -> lemon)
|
17
|
+
|
18
|
+
(define spin-wheel
|
19
|
+
-> (return-fruit (random 14)))
|
20
|
+
|
21
|
+
(define payoff
|
22
|
+
cherry cherry cherry -> 60
|
23
|
+
pear pear pear -> 100
|
24
|
+
orange orange orange -> 200
|
25
|
+
pineapple pineapple pineapple -> 300
|
26
|
+
lemon lemon lemon -> 500
|
27
|
+
cherry cherry X -> 10
|
28
|
+
X cherry cherry -> 10
|
29
|
+
pear pear X -> 20
|
30
|
+
X pear pear -> 20
|
31
|
+
orange orange X -> 30
|
32
|
+
X orange orange -> 30
|
33
|
+
pineapple pineapple X -> 40
|
34
|
+
X pineapple pineapple -> 40
|
35
|
+
lemon lemon X -> 50
|
36
|
+
X lemon lemon -> 50
|
37
|
+
X Y Z -> 0)
|
38
|
+
|
39
|
+
(define fruit-machine
|
40
|
+
start -> (announce-payoff (spin-wheel) (spin-wheel) (spin-wheel)))
|
41
|
+
|
42
|
+
(define announce-payoff
|
43
|
+
Fruit1 Fruit2 Fruit3
|
44
|
+
-> (output "~A ~A ~A~%You win ~A pence~%"
|
45
|
+
Fruit1 Fruit2 Fruit3 (payoff Fruit1 Fruit2 Fruit3)))
|
46
|
+
|
@@ -1,217 +1,217 @@
|
|
1
|
-
(datatype num
|
2
|
-
|
3
|
-
____________________________________
|
4
|
-
(number? X) : verified >> X : number;)
|
5
|
-
|
6
|
-
(datatype primitive_object
|
7
|
-
|
8
|
-
if (variable? X)
|
9
|
-
_______________
|
10
|
-
X : variable;
|
11
|
-
|
12
|
-
X : variable;
|
13
|
-
_____________
|
14
|
-
X : primitive_object;
|
15
|
-
|
16
|
-
X : symbol;
|
17
|
-
___________
|
18
|
-
X : primitive_object;
|
19
|
-
|
20
|
-
X : string;
|
21
|
-
___________
|
22
|
-
X : primitive_object;
|
23
|
-
|
24
|
-
X : boolean;
|
25
|
-
___________
|
26
|
-
X : primitive_object;
|
27
|
-
|
28
|
-
X : number;
|
29
|
-
___________
|
30
|
-
X : primitive_object;
|
31
|
-
|
32
|
-
_____________________
|
33
|
-
[] : primitive_object;)
|
34
|
-
|
35
|
-
(datatype pattern
|
36
|
-
|
37
|
-
X : primitive_object;
|
38
|
-
___________
|
39
|
-
X : pattern;
|
40
|
-
|
41
|
-
P1 : pattern; P2 : pattern;
|
42
|
-
===========================
|
43
|
-
[cons P1 P2] : pattern;
|
44
|
-
|
45
|
-
P1 : pattern; P2 : pattern;
|
46
|
-
===========================
|
47
|
-
[@p P1 P2] : pattern;)
|
48
|
-
|
49
|
-
(datatype l_formula
|
50
|
-
|
51
|
-
X : pattern;
|
52
|
-
_____________
|
53
|
-
X : l_formula;
|
54
|
-
|
55
|
-
X : l_formula; Y : l_formula; Z : l_formula;
|
56
|
-
=================================
|
57
|
-
[if X Y Z] : l_formula;
|
58
|
-
|
59
|
-
X : variable; Y : l_formula; Z : l_formula;
|
60
|
-
================================
|
61
|
-
[let X Y Z] : l_formula;
|
62
|
-
|
63
|
-
X : l_formula; Y : l_formula;
|
64
|
-
======================
|
65
|
-
[cons X Y] : l_formula;
|
66
|
-
|
67
|
-
X : l_formula; Y : l_formula;
|
68
|
-
======================
|
69
|
-
[@p X Y] : l_formula;
|
70
|
-
|
71
|
-
X : l_formula; Y : l_formula;
|
72
|
-
======================
|
73
|
-
[where X Y] : l_formula;
|
74
|
-
|
75
|
-
X : l_formula; Y : l_formula;
|
76
|
-
======================
|
77
|
-
[= X Y] : l_formula;
|
78
|
-
|
79
|
-
X : l_formula; Y : l_formula;
|
80
|
-
======================
|
81
|
-
[X Y] : l_formula;
|
82
|
-
|
83
|
-
Xn : (list l_formula);
|
84
|
-
===================
|
85
|
-
[cases | Xn] : l_formula;
|
86
|
-
|
87
|
-
P : pattern; X : l_formula;
|
88
|
-
===========================
|
89
|
-
[/. P X] : l_formula;)
|
90
|
-
|
91
|
-
(define l_interpreter
|
92
|
-
{A --> B}
|
93
|
-
_ -> (read_eval_print_loop (output "~%L interpreter ~%~%~%~%l-interp --> ~A~%"
|
94
|
-
(normal_form (input+ l_formula)))))
|
95
|
-
|
96
|
-
(define read_eval_print_loop
|
97
|
-
{string --> A}
|
98
|
-
_ -> (read_eval_print_loop
|
99
|
-
(output "l-interp --> ~A~%"
|
100
|
-
(normal_form (input+ l_formula)))))
|
101
|
-
|
102
|
-
(define normal_form
|
103
|
-
{l_formula --> l_formula}
|
104
|
-
X -> (fix ==>> X))
|
105
|
-
|
106
|
-
(define ==>>
|
107
|
-
{l_formula --> l_formula}
|
108
|
-
[= X Y] -> (let X* (normal_form X)
|
109
|
-
(let Y* (normal_form Y)
|
110
|
-
(if (or (eval_error? X*) (eval_error? Y*))
|
111
|
-
"error!"
|
112
|
-
(if (= X* Y*) true false))))
|
113
|
-
[[/. P X] Y] -> (let Match (match P (normal_form Y))
|
114
|
-
(if (no_match? Match)
|
115
|
-
"no match"
|
116
|
-
(sub Match X)))
|
117
|
-
[if X Y Z] -> (let X* (normal_form X)
|
118
|
-
(if (= X* true)
|
119
|
-
Y
|
120
|
-
(if (= X* false)
|
121
|
-
Z
|
122
|
-
"error!")))
|
123
|
-
[let X Y Z] -> [[/. X Z] Y]
|
124
|
-
[@p X Y] -> (let X* (normal_form X)
|
125
|
-
(let Y* (normal_form Y)
|
126
|
-
(if (or (eval_error? X*) (eval_error? Y*))
|
127
|
-
"error!"
|
128
|
-
[@p X* Y*])))
|
129
|
-
[cons X Y] -> (let X* (normal_form X)
|
130
|
-
(let Y* (normal_form Y)
|
131
|
-
(if (or (eval_error? X*) (eval_error? Y*))
|
132
|
-
"error!"
|
133
|
-
[cons X* Y*])))
|
134
|
-
[++ X] -> (successor (normal_form X))
|
135
|
-
[-- X] -> (predecessor (normal_form X))
|
136
|
-
\*[cases X1 | Xn] -> (let Case1 (normal_form X1)
|
137
|
-
(if (= Case1 "no match")
|
138
|
-
[cases | Xn]
|
139
|
-
Case1))
|
140
|
-
[cases] -> "error!"
|
141
|
-
[where X Y] -> [if X Y "no match"]
|
142
|
-
[y-combinator [/. X Y]] -> (replace X [y-combinator [/. X Y]] Y)
|
143
|
-
[X Y] -> (let X* (normal_form X)
|
144
|
-
(let Y* (normal_form Y)
|
145
|
-
(if (or (eval_error? X*) (eval_error? Y*))
|
146
|
-
"error!"
|
147
|
-
[X* Y*])))*\
|
148
|
-
X -> X)
|
149
|
-
|
150
|
-
(define eval_error?
|
151
|
-
{l_formula --> boolean}
|
152
|
-
"error!" -> true
|
153
|
-
"no match" -> true
|
154
|
-
_ -> false)
|
155
|
-
|
156
|
-
(define successor
|
157
|
-
{A --> l_formula}
|
158
|
-
X -> (+ 1 X) where (number? X)
|
159
|
-
_ -> "error!")
|
160
|
-
|
161
|
-
(define predecessor
|
162
|
-
{A --> l_formula}
|
163
|
-
X -> (- X 1) where (number? X)
|
164
|
-
_ -> "error!")
|
165
|
-
|
166
|
-
(define sub
|
167
|
-
{(list (pattern * l_formula)) --> l_formula --> l_formula}
|
168
|
-
[] X -> X
|
169
|
-
[(@p Var Val) | Assoc] X -> (sub Assoc (replace Var Val X)))
|
170
|
-
|
171
|
-
(define match
|
172
|
-
{pattern --> l_formula --> (list (pattern * l_formula))}
|
173
|
-
P X -> [] where (== P X)
|
174
|
-
P X -> [(@p P X)] where (variable? P)
|
175
|
-
[cons P1 P2] [cons X Y] -> (let Match1 (match P1 X)
|
176
|
-
(if (no_match? Match1)
|
177
|
-
Match1
|
178
|
-
(let Match2 (match P2 Y)
|
179
|
-
(if (no_match? Match2)
|
180
|
-
Match2
|
181
|
-
(append Match1 Match2)))))
|
182
|
-
[@p P1 P2] [@p X Y] -> (let Match1 (match P1 X)
|
183
|
-
(if (no_match? Match1)
|
184
|
-
Match1
|
185
|
-
(let Match2 (match P2 Y)
|
186
|
-
(if (no_match? Match2)
|
187
|
-
Match2
|
188
|
-
(append Match1 Match2)))))
|
189
|
-
|
190
|
-
_ _ -> [(@p no matching)])
|
191
|
-
|
192
|
-
(define no_match?
|
193
|
-
{(list (pattern * l_formula)) --> boolean}
|
194
|
-
[(@p no matching)] -> true
|
195
|
-
_ -> false)
|
196
|
-
|
197
|
-
(define replace
|
198
|
-
{pattern --> l_formula --> l_formula --> l_formula}
|
199
|
-
V W [let V* X Y] -> [let V* X Y] where (== V V*)
|
200
|
-
X Y X -> Y
|
201
|
-
V W [= X Y] -> [= (replace V W X) (replace V W Y)]
|
202
|
-
V W [/. P X] -> [/. P (replace V W X)] where (free? V P)
|
203
|
-
V W [if X Y Z] -> [if (replace V W X) (replace V W Y) (replace V W Z)]
|
204
|
-
V W [let X Y Z] -> [let X (replace V W Y) (replace V W Z)]
|
205
|
-
V W [@p X Y] -> [@p (replace V W X) (replace V W Y)]
|
206
|
-
V W [cons X Y] -> [cons (replace V W X) (replace V W Y)]
|
207
|
-
V W [cases | Xn] -> [cases | (map (/. Xi (replace V W Xi)) Xn)]
|
208
|
-
V W [where X Y] -> [where (replace V W X) (replace V W Y)]
|
209
|
-
V W [X Y] -> [(replace V W X) (replace V W Y)]
|
210
|
-
_ _ X -> X)
|
211
|
-
|
212
|
-
(define free?
|
213
|
-
{pattern --> pattern --> boolean}
|
214
|
-
P P -> false
|
215
|
-
P [cons P1 P2] -> (and (free? P P1) (free? P P2))
|
216
|
-
P [@p P1 P2] -> (and (free? P P1) (free? P P2))
|
217
|
-
_ _ -> true)
|
1
|
+
(datatype num
|
2
|
+
|
3
|
+
____________________________________
|
4
|
+
(number? X) : verified >> X : number;)
|
5
|
+
|
6
|
+
(datatype primitive_object
|
7
|
+
|
8
|
+
if (variable? X)
|
9
|
+
_______________
|
10
|
+
X : variable;
|
11
|
+
|
12
|
+
X : variable;
|
13
|
+
_____________
|
14
|
+
X : primitive_object;
|
15
|
+
|
16
|
+
X : symbol;
|
17
|
+
___________
|
18
|
+
X : primitive_object;
|
19
|
+
|
20
|
+
X : string;
|
21
|
+
___________
|
22
|
+
X : primitive_object;
|
23
|
+
|
24
|
+
X : boolean;
|
25
|
+
___________
|
26
|
+
X : primitive_object;
|
27
|
+
|
28
|
+
X : number;
|
29
|
+
___________
|
30
|
+
X : primitive_object;
|
31
|
+
|
32
|
+
_____________________
|
33
|
+
[] : primitive_object;)
|
34
|
+
|
35
|
+
(datatype pattern
|
36
|
+
|
37
|
+
X : primitive_object;
|
38
|
+
___________
|
39
|
+
X : pattern;
|
40
|
+
|
41
|
+
P1 : pattern; P2 : pattern;
|
42
|
+
===========================
|
43
|
+
[cons P1 P2] : pattern;
|
44
|
+
|
45
|
+
P1 : pattern; P2 : pattern;
|
46
|
+
===========================
|
47
|
+
[@p P1 P2] : pattern;)
|
48
|
+
|
49
|
+
(datatype l_formula
|
50
|
+
|
51
|
+
X : pattern;
|
52
|
+
_____________
|
53
|
+
X : l_formula;
|
54
|
+
|
55
|
+
X : l_formula; Y : l_formula; Z : l_formula;
|
56
|
+
=================================
|
57
|
+
[if X Y Z] : l_formula;
|
58
|
+
|
59
|
+
X : variable; Y : l_formula; Z : l_formula;
|
60
|
+
================================
|
61
|
+
[let X Y Z] : l_formula;
|
62
|
+
|
63
|
+
X : l_formula; Y : l_formula;
|
64
|
+
======================
|
65
|
+
[cons X Y] : l_formula;
|
66
|
+
|
67
|
+
X : l_formula; Y : l_formula;
|
68
|
+
======================
|
69
|
+
[@p X Y] : l_formula;
|
70
|
+
|
71
|
+
X : l_formula; Y : l_formula;
|
72
|
+
======================
|
73
|
+
[where X Y] : l_formula;
|
74
|
+
|
75
|
+
X : l_formula; Y : l_formula;
|
76
|
+
======================
|
77
|
+
[= X Y] : l_formula;
|
78
|
+
|
79
|
+
X : l_formula; Y : l_formula;
|
80
|
+
======================
|
81
|
+
[X Y] : l_formula;
|
82
|
+
|
83
|
+
Xn : (list l_formula);
|
84
|
+
===================
|
85
|
+
[cases | Xn] : l_formula;
|
86
|
+
|
87
|
+
P : pattern; X : l_formula;
|
88
|
+
===========================
|
89
|
+
[/. P X] : l_formula;)
|
90
|
+
|
91
|
+
(define l_interpreter
|
92
|
+
{A --> B}
|
93
|
+
_ -> (read_eval_print_loop (output "~%L interpreter ~%~%~%~%l-interp --> ~A~%"
|
94
|
+
(normal_form (input+ l_formula)))))
|
95
|
+
|
96
|
+
(define read_eval_print_loop
|
97
|
+
{string --> A}
|
98
|
+
_ -> (read_eval_print_loop
|
99
|
+
(output "l-interp --> ~A~%"
|
100
|
+
(normal_form (input+ l_formula)))))
|
101
|
+
|
102
|
+
(define normal_form
|
103
|
+
{l_formula --> l_formula}
|
104
|
+
X -> (fix ==>> X))
|
105
|
+
|
106
|
+
(define ==>>
|
107
|
+
{l_formula --> l_formula}
|
108
|
+
[= X Y] -> (let X* (normal_form X)
|
109
|
+
(let Y* (normal_form Y)
|
110
|
+
(if (or (eval_error? X*) (eval_error? Y*))
|
111
|
+
"error!"
|
112
|
+
(if (= X* Y*) true false))))
|
113
|
+
[[/. P X] Y] -> (let Match (match P (normal_form Y))
|
114
|
+
(if (no_match? Match)
|
115
|
+
"no match"
|
116
|
+
(sub Match X)))
|
117
|
+
[if X Y Z] -> (let X* (normal_form X)
|
118
|
+
(if (= X* true)
|
119
|
+
Y
|
120
|
+
(if (= X* false)
|
121
|
+
Z
|
122
|
+
"error!")))
|
123
|
+
[let X Y Z] -> [[/. X Z] Y]
|
124
|
+
[@p X Y] -> (let X* (normal_form X)
|
125
|
+
(let Y* (normal_form Y)
|
126
|
+
(if (or (eval_error? X*) (eval_error? Y*))
|
127
|
+
"error!"
|
128
|
+
[@p X* Y*])))
|
129
|
+
[cons X Y] -> (let X* (normal_form X)
|
130
|
+
(let Y* (normal_form Y)
|
131
|
+
(if (or (eval_error? X*) (eval_error? Y*))
|
132
|
+
"error!"
|
133
|
+
[cons X* Y*])))
|
134
|
+
[++ X] -> (successor (normal_form X))
|
135
|
+
[-- X] -> (predecessor (normal_form X))
|
136
|
+
\*[cases X1 | Xn] -> (let Case1 (normal_form X1)
|
137
|
+
(if (= Case1 "no match")
|
138
|
+
[cases | Xn]
|
139
|
+
Case1))
|
140
|
+
[cases] -> "error!"
|
141
|
+
[where X Y] -> [if X Y "no match"]
|
142
|
+
[y-combinator [/. X Y]] -> (replace X [y-combinator [/. X Y]] Y)
|
143
|
+
[X Y] -> (let X* (normal_form X)
|
144
|
+
(let Y* (normal_form Y)
|
145
|
+
(if (or (eval_error? X*) (eval_error? Y*))
|
146
|
+
"error!"
|
147
|
+
[X* Y*])))*\
|
148
|
+
X -> X)
|
149
|
+
|
150
|
+
(define eval_error?
|
151
|
+
{l_formula --> boolean}
|
152
|
+
"error!" -> true
|
153
|
+
"no match" -> true
|
154
|
+
_ -> false)
|
155
|
+
|
156
|
+
(define successor
|
157
|
+
{A --> l_formula}
|
158
|
+
X -> (+ 1 X) where (number? X)
|
159
|
+
_ -> "error!")
|
160
|
+
|
161
|
+
(define predecessor
|
162
|
+
{A --> l_formula}
|
163
|
+
X -> (- X 1) where (number? X)
|
164
|
+
_ -> "error!")
|
165
|
+
|
166
|
+
(define sub
|
167
|
+
{(list (pattern * l_formula)) --> l_formula --> l_formula}
|
168
|
+
[] X -> X
|
169
|
+
[(@p Var Val) | Assoc] X -> (sub Assoc (replace Var Val X)))
|
170
|
+
|
171
|
+
(define match
|
172
|
+
{pattern --> l_formula --> (list (pattern * l_formula))}
|
173
|
+
P X -> [] where (== P X)
|
174
|
+
P X -> [(@p P X)] where (variable? P)
|
175
|
+
[cons P1 P2] [cons X Y] -> (let Match1 (match P1 X)
|
176
|
+
(if (no_match? Match1)
|
177
|
+
Match1
|
178
|
+
(let Match2 (match P2 Y)
|
179
|
+
(if (no_match? Match2)
|
180
|
+
Match2
|
181
|
+
(append Match1 Match2)))))
|
182
|
+
[@p P1 P2] [@p X Y] -> (let Match1 (match P1 X)
|
183
|
+
(if (no_match? Match1)
|
184
|
+
Match1
|
185
|
+
(let Match2 (match P2 Y)
|
186
|
+
(if (no_match? Match2)
|
187
|
+
Match2
|
188
|
+
(append Match1 Match2)))))
|
189
|
+
|
190
|
+
_ _ -> [(@p no matching)])
|
191
|
+
|
192
|
+
(define no_match?
|
193
|
+
{(list (pattern * l_formula)) --> boolean}
|
194
|
+
[(@p no matching)] -> true
|
195
|
+
_ -> false)
|
196
|
+
|
197
|
+
(define replace
|
198
|
+
{pattern --> l_formula --> l_formula --> l_formula}
|
199
|
+
V W [let V* X Y] -> [let V* X Y] where (== V V*)
|
200
|
+
X Y X -> Y
|
201
|
+
V W [= X Y] -> [= (replace V W X) (replace V W Y)]
|
202
|
+
V W [/. P X] -> [/. P (replace V W X)] where (free? V P)
|
203
|
+
V W [if X Y Z] -> [if (replace V W X) (replace V W Y) (replace V W Z)]
|
204
|
+
V W [let X Y Z] -> [let X (replace V W Y) (replace V W Z)]
|
205
|
+
V W [@p X Y] -> [@p (replace V W X) (replace V W Y)]
|
206
|
+
V W [cons X Y] -> [cons (replace V W X) (replace V W Y)]
|
207
|
+
V W [cases | Xn] -> [cases | (map (/. Xi (replace V W Xi)) Xn)]
|
208
|
+
V W [where X Y] -> [where (replace V W X) (replace V W Y)]
|
209
|
+
V W [X Y] -> [(replace V W X) (replace V W Y)]
|
210
|
+
_ _ X -> X)
|
211
|
+
|
212
|
+
(define free?
|
213
|
+
{pattern --> pattern --> boolean}
|
214
|
+
P P -> false
|
215
|
+
P [cons P1 P2] -> (and (free? P P1) (free? P P2))
|
216
|
+
P [@p P1 P2] -> (and (free? P P1) (free? P P2))
|
217
|
+
_ _ -> true)
|