shen-ruby 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/.travis.yml +9 -3
- data/Gemfile +1 -4
- data/HISTORY.md +16 -0
- data/MIT_LICENSE.txt +1 -1
- data/README.md +25 -26
- data/Rakefile +3 -11
- data/bin/shen_test_suite.rb +15 -3
- data/bin/srrepl +6 -8
- data/lib/shen_ruby.rb +6 -1
- data/lib/shen_ruby/converters.rb +23 -0
- data/lib/shen_ruby/version.rb +1 -1
- data/shen-ruby.gemspec +4 -1
- data/shen/lib/shen_ruby/shen.rb +49 -33
- data/shen/release/benchmarks/N_queens.shen +45 -45
- data/shen/release/benchmarks/README.shen +14 -14
- data/shen/release/benchmarks/benchmarks.shen +52 -52
- data/shen/release/benchmarks/einstein.shen +32 -32
- data/shen/release/benchmarks/interpreter.shen +219 -219
- data/shen/release/benchmarks/jnk.shen +193 -193
- data/shen/release/benchmarks/powerset.shen +10 -10
- data/shen/release/benchmarks/prime.shen +10 -10
- data/shen/release/benchmarks/short.shen +129 -129
- data/shen/release/k_lambda/core.kl +181 -181
- data/shen/release/k_lambda/declarations.kl +131 -131
- data/shen/release/k_lambda/load.kl +84 -84
- data/shen/release/k_lambda/macros.kl +112 -112
- data/shen/release/k_lambda/prolog.kl +252 -252
- data/shen/release/k_lambda/reader.kl +222 -222
- data/shen/release/k_lambda/sequent.kl +166 -166
- data/shen/release/k_lambda/sys.kl +271 -271
- data/shen/release/k_lambda/t-star.kl +139 -139
- data/shen/release/k_lambda/toplevel.kl +135 -135
- data/shen/release/k_lambda/track.kl +103 -103
- data/shen/release/k_lambda/types.kl +324 -324
- data/shen/release/k_lambda/writer.kl +105 -105
- data/shen/release/k_lambda/yacc.kl +113 -113
- 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
- data/spec/shen_ruby/converters_spec.rb +48 -0
- data/spec/spec_helper.rb +1 -2
- metadata +55 -60
- data/k_lambda_spec/atom_spec.rb +0 -85
- data/k_lambda_spec/primitives/arithmetic_spec.rb +0 -175
- data/k_lambda_spec/primitives/assignments_spec.rb +0 -44
- data/k_lambda_spec/primitives/boolean_operations_spec.rb +0 -136
- data/k_lambda_spec/primitives/generic_functions_spec.rb +0 -120
- data/k_lambda_spec/primitives/lists_spec.rb +0 -40
- data/k_lambda_spec/primitives/strings_spec.rb +0 -77
- data/k_lambda_spec/primitives/symbols_spec.rb +0 -24
- data/k_lambda_spec/primitives/vectors_spec.rb +0 -92
- data/k_lambda_spec/spec_helper.rb +0 -29
- data/k_lambda_spec/support/shared_examples.rb +0 -124
- data/k_lambda_spec/tail_recursion_spec.rb +0 -30
- data/lib/kl.rb +0 -7
- data/lib/kl/absvector.rb +0 -12
- data/lib/kl/compiler.rb +0 -360
- data/lib/kl/cons.rb +0 -51
- data/lib/kl/empty_list.rb +0 -12
- data/lib/kl/environment.rb +0 -163
- data/lib/kl/error.rb +0 -4
- data/lib/kl/internal_error.rb +0 -7
- data/lib/kl/lexer.rb +0 -186
- data/lib/kl/primitives/arithmetic.rb +0 -60
- data/lib/kl/primitives/assignments.rb +0 -15
- data/lib/kl/primitives/booleans.rb +0 -21
- data/lib/kl/primitives/error_handling.rb +0 -13
- data/lib/kl/primitives/extensions.rb +0 -12
- data/lib/kl/primitives/generic_functions.rb +0 -29
- data/lib/kl/primitives/lists.rb +0 -23
- data/lib/kl/primitives/streams.rb +0 -28
- data/lib/kl/primitives/strings.rb +0 -63
- data/lib/kl/primitives/symbols.rb +0 -18
- data/lib/kl/primitives/time.rb +0 -17
- data/lib/kl/primitives/vectors.rb +0 -36
- data/lib/kl/reader.rb +0 -46
- data/spec/kl/cons_spec.rb +0 -12
- data/spec/kl/environment_spec.rb +0 -282
- data/spec/kl/interop_spec.rb +0 -68
- data/spec/kl/lexer_spec.rb +0 -149
- data/spec/kl/primitives/generic_functions_spec.rb +0 -29
- data/spec/kl/primitives/symbols_spec.rb +0 -21
- data/spec/kl/reader_spec.rb +0 -42
@@ -1,45 +1,45 @@
|
|
1
|
-
(define n-queens
|
2
|
-
{number --> symbol}
|
3
|
-
N -> (do (map (/. X (output "~A~%" X)) (n-queens-loop N (initialise N))) ok))
|
4
|
-
|
5
|
-
(define initialise
|
6
|
-
{number --> (list number)}
|
7
|
-
0 -> []
|
8
|
-
N -> [1 | (initialise (- N 1))])
|
9
|
-
|
10
|
-
(define n-queens-loop
|
11
|
-
{number --> (list number) --> [(list number)]}
|
12
|
-
N Config -> [] where (all_Ns? N Config)
|
13
|
-
N Config -> [Config | (n-queens-loop N (next_n N Config))]
|
14
|
-
where (and (ok_row? Config) (ok_diag? Config))
|
15
|
-
N Config -> (n-queens-loop N (next_n N Config)))
|
16
|
-
|
17
|
-
(define all_Ns?
|
18
|
-
{number --> (list number) --> boolean}
|
19
|
-
_ [] -> true
|
20
|
-
N [N | Ns] -> (all_Ns? N Ns)
|
21
|
-
_ _ -> false)
|
22
|
-
|
23
|
-
(define next_n
|
24
|
-
{number --> (list number) --> (list number)}
|
25
|
-
N [N | Ns] -> [1 | (next_n N Ns)]
|
26
|
-
_ [N | Ns] -> [(+ 1 N) | Ns])
|
27
|
-
|
28
|
-
(define ok_row?
|
29
|
-
{(list number) --> boolean}
|
30
|
-
[] -> true
|
31
|
-
[N | Ns] -> false where (element? N Ns)
|
32
|
-
[_ | Ns] -> (ok_row? Ns))
|
33
|
-
|
34
|
-
(define ok_diag?
|
35
|
-
{(list number) --> boolean}
|
36
|
-
[] -> true
|
37
|
-
[N | Ns] -> (and (ok_diag_N? (+ N 1) (- N 1) Ns)
|
38
|
-
(ok_diag? Ns)))
|
39
|
-
|
40
|
-
(define ok_diag_N?
|
41
|
-
{number --> number --> (list number) --> boolean}
|
42
|
-
_ _ [] -> true
|
43
|
-
Up Down [Up | _] -> false
|
44
|
-
Up Down [Down | _] -> false
|
45
|
-
Up Down [_ | Ns] -> (ok_diag_N? (+ 1 Up) (- Down 1) Ns))
|
1
|
+
(define n-queens
|
2
|
+
{number --> symbol}
|
3
|
+
N -> (do (map (/. X (output "~A~%" X)) (n-queens-loop N (initialise N))) ok))
|
4
|
+
|
5
|
+
(define initialise
|
6
|
+
{number --> (list number)}
|
7
|
+
0 -> []
|
8
|
+
N -> [1 | (initialise (- N 1))])
|
9
|
+
|
10
|
+
(define n-queens-loop
|
11
|
+
{number --> (list number) --> [(list number)]}
|
12
|
+
N Config -> [] where (all_Ns? N Config)
|
13
|
+
N Config -> [Config | (n-queens-loop N (next_n N Config))]
|
14
|
+
where (and (ok_row? Config) (ok_diag? Config))
|
15
|
+
N Config -> (n-queens-loop N (next_n N Config)))
|
16
|
+
|
17
|
+
(define all_Ns?
|
18
|
+
{number --> (list number) --> boolean}
|
19
|
+
_ [] -> true
|
20
|
+
N [N | Ns] -> (all_Ns? N Ns)
|
21
|
+
_ _ -> false)
|
22
|
+
|
23
|
+
(define next_n
|
24
|
+
{number --> (list number) --> (list number)}
|
25
|
+
N [N | Ns] -> [1 | (next_n N Ns)]
|
26
|
+
_ [N | Ns] -> [(+ 1 N) | Ns])
|
27
|
+
|
28
|
+
(define ok_row?
|
29
|
+
{(list number) --> boolean}
|
30
|
+
[] -> true
|
31
|
+
[N | Ns] -> false where (element? N Ns)
|
32
|
+
[_ | Ns] -> (ok_row? Ns))
|
33
|
+
|
34
|
+
(define ok_diag?
|
35
|
+
{(list number) --> boolean}
|
36
|
+
[] -> true
|
37
|
+
[N | Ns] -> (and (ok_diag_N? (+ N 1) (- N 1) Ns)
|
38
|
+
(ok_diag? Ns)))
|
39
|
+
|
40
|
+
(define ok_diag_N?
|
41
|
+
{number --> number --> (list number) --> boolean}
|
42
|
+
_ _ [] -> true
|
43
|
+
Up Down [Up | _] -> false
|
44
|
+
Up Down [Down | _] -> false
|
45
|
+
Up Down [_ | Ns] -> (ok_diag_N? (+ 1 Up) (- Down 1) Ns))
|
@@ -1,14 +1,14 @@
|
|
1
|
-
\*
|
2
|
-
|
3
|
-
This is the benchmark macro for Shen. Assuming your port to Blub is in the directory Platforms/Blub; do the
|
4
|
-
following.
|
5
|
-
|
6
|
-
1. (cd "../../Benchmarks")
|
7
|
-
2. (load "README.shen")
|
8
|
-
3. (load "benchmarks.shen")
|
9
|
-
|
10
|
-
*\
|
11
|
-
|
12
|
-
(defmacro benchmark-macro
|
13
|
-
[benchmark Message Benchmark] -> [do [nl] [output Message] [time Benchmark]])
|
14
|
-
|
1
|
+
\*
|
2
|
+
|
3
|
+
This is the benchmark macro for Shen. Assuming your port to Blub is in the directory Platforms/Blub; do the
|
4
|
+
following.
|
5
|
+
|
6
|
+
1. (cd "../../Benchmarks")
|
7
|
+
2. (load "README.shen")
|
8
|
+
3. (load "benchmarks.shen")
|
9
|
+
|
10
|
+
*\
|
11
|
+
|
12
|
+
(defmacro benchmark-macro
|
13
|
+
[benchmark Message Benchmark] -> [do [nl] [output Message] [time Benchmark]])
|
14
|
+
|
@@ -1,52 +1,52 @@
|
|
1
|
-
(benchmark "read a 10K binary file" (read-file-as-bytelist "plato.jpg"))
|
2
|
-
|
3
|
-
(benchmark "read a 105K binary file" (read-file-as-bytelist "heatwave.gif"))
|
4
|
-
|
5
|
-
(benchmark "parse a 7K Shen file" (read-file "interpreter.shen"))
|
6
|
-
|
7
|
-
(benchmark "compile a 130 LOC Qi program" (load "short.shen"))
|
8
|
-
|
9
|
-
(benchmark "compile a 27 line Prolog program" (load "einstein.shen"))
|
10
|
-
|
11
|
-
(benchmark "solve Einstein's puzzle" (prolog? (einsteins_riddle X) (return X)))
|
12
|
-
|
13
|
-
(load "powerset.shen")
|
14
|
-
|
15
|
-
(benchmark "powerset of 14 numbers" (powerset [1 2 3 4 5 6 7 8 9 10 11 12 13 14]))
|
16
|
-
|
17
|
-
(do (set *str* (hd (read-file "text.txt"))) ok)
|
18
|
-
|
19
|
-
(define remstr
|
20
|
-
"" -> 0
|
21
|
-
(@s "er" S) -> (+ 1 (remstr S))
|
22
|
-
(@s _ Ss) -> (remstr Ss))
|
23
|
-
|
24
|
-
(benchmark "count 'er' in a string" (remstr (value *str*)))
|
25
|
-
|
26
|
-
(define vectorn
|
27
|
-
0 -> <>
|
28
|
-
N -> (@v N (vectorn (- N 1))))
|
29
|
-
|
30
|
-
(define vectorp
|
31
|
-
<> -> <>
|
32
|
-
(@v X Y) -> (@v (+ X 1) (vectorp Y))
|
33
|
-
(@v X Y Z) -> (@v (+ X 1) (+ Y 2) (vectorp Z)))
|
34
|
-
|
35
|
-
(benchmark "vector of 1000 elements" (vectorn 1000))
|
36
|
-
|
37
|
-
(define tak
|
38
|
-
X Y Z -> Z where (not (< Y X))
|
39
|
-
X Y Z -> (tak (tak (- X 1) Y Z)
|
40
|
-
(tak (- Y 1) Z X)
|
41
|
-
(tak (- Z 1) X Y)))
|
42
|
-
|
43
|
-
(benchmark "(tak 18 12 6)" (tak 18 12 6))
|
44
|
-
|
45
|
-
(tc +)
|
46
|
-
|
47
|
-
(benchmark "type checking the N queens" (load "N_queens.shen"))
|
48
|
-
|
49
|
-
(benchmark "solving the N queens for N = 6" (n-queens 6))
|
50
|
-
|
51
|
-
(benchmark "load and typecheck Qi interpreter" (load "interpreter.shen"))
|
52
|
-
|
1
|
+
(benchmark "read a 10K binary file" (read-file-as-bytelist "plato.jpg"))
|
2
|
+
|
3
|
+
(benchmark "read a 105K binary file" (read-file-as-bytelist "heatwave.gif"))
|
4
|
+
|
5
|
+
(benchmark "parse a 7K Shen file" (read-file "interpreter.shen"))
|
6
|
+
|
7
|
+
(benchmark "compile a 130 LOC Qi program" (load "short.shen"))
|
8
|
+
|
9
|
+
(benchmark "compile a 27 line Prolog program" (load "einstein.shen"))
|
10
|
+
|
11
|
+
(benchmark "solve Einstein's puzzle" (prolog? (einsteins_riddle X) (return X)))
|
12
|
+
|
13
|
+
(load "powerset.shen")
|
14
|
+
|
15
|
+
(benchmark "powerset of 14 numbers" (powerset [1 2 3 4 5 6 7 8 9 10 11 12 13 14]))
|
16
|
+
|
17
|
+
(do (set *str* (hd (read-file "text.txt"))) ok)
|
18
|
+
|
19
|
+
(define remstr
|
20
|
+
"" -> 0
|
21
|
+
(@s "er" S) -> (+ 1 (remstr S))
|
22
|
+
(@s _ Ss) -> (remstr Ss))
|
23
|
+
|
24
|
+
(benchmark "count 'er' in a string" (remstr (value *str*)))
|
25
|
+
|
26
|
+
(define vectorn
|
27
|
+
0 -> <>
|
28
|
+
N -> (@v N (vectorn (- N 1))))
|
29
|
+
|
30
|
+
(define vectorp
|
31
|
+
<> -> <>
|
32
|
+
(@v X Y) -> (@v (+ X 1) (vectorp Y))
|
33
|
+
(@v X Y Z) -> (@v (+ X 1) (+ Y 2) (vectorp Z)))
|
34
|
+
|
35
|
+
(benchmark "vector of 1000 elements" (vectorn 1000))
|
36
|
+
|
37
|
+
(define tak
|
38
|
+
X Y Z -> Z where (not (< Y X))
|
39
|
+
X Y Z -> (tak (tak (- X 1) Y Z)
|
40
|
+
(tak (- Y 1) Z X)
|
41
|
+
(tak (- Z 1) X Y)))
|
42
|
+
|
43
|
+
(benchmark "(tak 18 12 6)" (tak 18 12 6))
|
44
|
+
|
45
|
+
(tc +)
|
46
|
+
|
47
|
+
(benchmark "type checking the N queens" (load "N_queens.shen"))
|
48
|
+
|
49
|
+
(benchmark "solving the N queens for N = 6" (n-queens 6))
|
50
|
+
|
51
|
+
(benchmark "load and typecheck Qi interpreter" (load "interpreter.shen"))
|
52
|
+
|
@@ -1,33 +1,33 @@
|
|
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 [L | [R | _]] <--;
|
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 [L | [R | _]] <--;
|
33
33
|
L R [_ | Rest] <-- (iright L R Rest);)
|
@@ -1,219 +1,219 @@
|
|
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 (function ==>>) 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
|
-
\* (spy +) *\
|
167
|
-
|
168
|
-
(define sub
|
169
|
-
{[(pattern * l_formula)] --> l_formula --> l_formula}
|
170
|
-
[] X -> X
|
171
|
-
[(@p Var Val) | Assoc] X -> (sub Assoc (replace Var Val X)))
|
172
|
-
|
173
|
-
(define match
|
174
|
-
{pattern --> l_formula --> (list (pattern * l_formula))}
|
175
|
-
P X -> [] where (== P X)
|
176
|
-
P X -> [(@p P X)] where (variable? P)
|
177
|
-
[cons P1 P2] [cons X Y] -> (let Match1 (match P1 X)
|
178
|
-
(if (no_match? Match1)
|
179
|
-
Match1
|
180
|
-
(let Match2 (match P2 Y)
|
181
|
-
(if (no_match? Match2)
|
182
|
-
Match2
|
183
|
-
(append Match1 Match2)))))
|
184
|
-
[@p P1 P2] [@p X Y] -> (let Match1 (match P1 X)
|
185
|
-
(if (no_match? Match1)
|
186
|
-
Match1
|
187
|
-
(let Match2 (match P2 Y)
|
188
|
-
(if (no_match? Match2)
|
189
|
-
Match2
|
190
|
-
(append Match1 Match2)))))
|
191
|
-
|
192
|
-
_ _ -> [(@p no matching)])
|
193
|
-
|
194
|
-
(define no_match?
|
195
|
-
{[(pattern * l_formula)] --> boolean}
|
196
|
-
[(@p no matching)] -> true
|
197
|
-
_ -> false)
|
198
|
-
|
199
|
-
(define replace
|
200
|
-
{pattern --> l_formula --> l_formula --> l_formula}
|
201
|
-
V W [let V* X Y] -> [let V* X Y] where (== V V*)
|
202
|
-
X Y X -> Y
|
203
|
-
V W [= X Y] -> [= (replace V W X) (replace V W Y)]
|
204
|
-
V W [/. P X] -> [/. P (replace V W X)] where (free? V P)
|
205
|
-
V W [if X Y Z] -> [if (replace V W X) (replace V W Y) (replace V W Z)]
|
206
|
-
V W [let X Y Z] -> [let X (replace V W Y) (replace V W Z)]
|
207
|
-
V W [@p X Y] -> [@p (replace V W X) (replace V W Y)]
|
208
|
-
V W [cons X Y] -> [cons (replace V W X) (replace V W Y)]
|
209
|
-
V W [cases | Xn] -> [cases | (map (/. Xi (replace V W Xi)) Xn)]
|
210
|
-
V W [where X Y] -> [where (replace V W X) (replace V W Y)]
|
211
|
-
V W [X Y] -> [(replace V W X) (replace V W Y)]
|
212
|
-
_ _ X -> X)
|
213
|
-
|
214
|
-
(define free?
|
215
|
-
{pattern --> pattern --> boolean}
|
216
|
-
P P -> false
|
217
|
-
P [cons P1 P2] -> (and (free? P P1) (free? P P2))
|
218
|
-
P [@p P1 P2] -> (and (free? P P1) (free? P P2))
|
219
|
-
_ _ -> 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 (function ==>>) 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
|
+
\* (spy +) *\
|
167
|
+
|
168
|
+
(define sub
|
169
|
+
{[(pattern * l_formula)] --> l_formula --> l_formula}
|
170
|
+
[] X -> X
|
171
|
+
[(@p Var Val) | Assoc] X -> (sub Assoc (replace Var Val X)))
|
172
|
+
|
173
|
+
(define match
|
174
|
+
{pattern --> l_formula --> (list (pattern * l_formula))}
|
175
|
+
P X -> [] where (== P X)
|
176
|
+
P X -> [(@p P X)] where (variable? P)
|
177
|
+
[cons P1 P2] [cons X Y] -> (let Match1 (match P1 X)
|
178
|
+
(if (no_match? Match1)
|
179
|
+
Match1
|
180
|
+
(let Match2 (match P2 Y)
|
181
|
+
(if (no_match? Match2)
|
182
|
+
Match2
|
183
|
+
(append Match1 Match2)))))
|
184
|
+
[@p P1 P2] [@p X Y] -> (let Match1 (match P1 X)
|
185
|
+
(if (no_match? Match1)
|
186
|
+
Match1
|
187
|
+
(let Match2 (match P2 Y)
|
188
|
+
(if (no_match? Match2)
|
189
|
+
Match2
|
190
|
+
(append Match1 Match2)))))
|
191
|
+
|
192
|
+
_ _ -> [(@p no matching)])
|
193
|
+
|
194
|
+
(define no_match?
|
195
|
+
{[(pattern * l_formula)] --> boolean}
|
196
|
+
[(@p no matching)] -> true
|
197
|
+
_ -> false)
|
198
|
+
|
199
|
+
(define replace
|
200
|
+
{pattern --> l_formula --> l_formula --> l_formula}
|
201
|
+
V W [let V* X Y] -> [let V* X Y] where (== V V*)
|
202
|
+
X Y X -> Y
|
203
|
+
V W [= X Y] -> [= (replace V W X) (replace V W Y)]
|
204
|
+
V W [/. P X] -> [/. P (replace V W X)] where (free? V P)
|
205
|
+
V W [if X Y Z] -> [if (replace V W X) (replace V W Y) (replace V W Z)]
|
206
|
+
V W [let X Y Z] -> [let X (replace V W Y) (replace V W Z)]
|
207
|
+
V W [@p X Y] -> [@p (replace V W X) (replace V W Y)]
|
208
|
+
V W [cons X Y] -> [cons (replace V W X) (replace V W Y)]
|
209
|
+
V W [cases | Xn] -> [cases | (map (/. Xi (replace V W Xi)) Xn)]
|
210
|
+
V W [where X Y] -> [where (replace V W X) (replace V W Y)]
|
211
|
+
V W [X Y] -> [(replace V W X) (replace V W Y)]
|
212
|
+
_ _ X -> X)
|
213
|
+
|
214
|
+
(define free?
|
215
|
+
{pattern --> pattern --> boolean}
|
216
|
+
P P -> false
|
217
|
+
P [cons P1 P2] -> (and (free? P P1) (free? P P2))
|
218
|
+
P [@p P1 P2] -> (and (free? P P1) (free? P P2))
|
219
|
+
_ _ -> true)
|