shen-ruby 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,46 +1,46 @@
|
|
1
|
-
(define defclass
|
2
|
-
Class Attributes
|
3
|
-
-> (let Assoc (map (/. Attribute [Attribute | fail]) Attributes)
|
4
|
-
ClassDef [[class | Class] | Assoc]
|
5
|
-
Store (put Class classdef ClassDef)
|
6
|
-
Class))
|
7
|
-
|
8
|
-
(define make-instance
|
9
|
-
Class -> (let ClassDef (trap-error (get Class classdef) (/. E []))
|
10
|
-
(if (empty? ClassDef)
|
11
|
-
(error "class ~A does not exist~%" Class)
|
12
|
-
ClassDef)))
|
13
|
-
|
14
|
-
(define get-value
|
15
|
-
Attribute Instance -> (let LookUp (assoc Attribute Instance)
|
16
|
-
(get-value-test LookUp)))
|
17
|
-
|
18
|
-
(define get-value-test
|
19
|
-
[ ] -> (error "no such attribute!~%")
|
20
|
-
[_ | fail] -> (error "no such value!~%")
|
21
|
-
[_ | Value] -> Value)
|
22
|
-
|
23
|
-
(define has-value?
|
24
|
-
Attribute Instance -> (let LookUp (assoc Attribute Instance)
|
25
|
-
(has-value-test LookUp)))
|
26
|
-
|
27
|
-
(define has-value-test
|
28
|
-
[ ] -> (error "no such attribute!~%")
|
29
|
-
[_ | fail] -> false
|
30
|
-
_ -> true)
|
31
|
-
|
32
|
-
(define has-attribute?
|
33
|
-
Attribute Instance -> (let LookUp (assoc Attribute Instance)
|
34
|
-
(not (empty? LookUp))))
|
35
|
-
|
36
|
-
(define change-value
|
37
|
-
_ class _ -> (error "cannot change the class of an instance!~%")
|
38
|
-
[ ] _ _ -> (error "no such attribute!~%")
|
39
|
-
[[Attribute | _] | Instance] Attribute Value
|
40
|
-
-> [[Attribute | Value] | Instance]
|
41
|
-
[Slot | Instance] Attribute Value
|
42
|
-
-> [Slot | (change-value Instance Attribute Value)])
|
43
|
-
|
44
|
-
(define instance-of
|
45
|
-
[[class | Class] | _] -> Class
|
46
|
-
_ -> (error "not a class instance!"))
|
1
|
+
(define defclass
|
2
|
+
Class Attributes
|
3
|
+
-> (let Assoc (map (/. Attribute [Attribute | fail]) Attributes)
|
4
|
+
ClassDef [[class | Class] | Assoc]
|
5
|
+
Store (put Class classdef ClassDef)
|
6
|
+
Class))
|
7
|
+
|
8
|
+
(define make-instance
|
9
|
+
Class -> (let ClassDef (trap-error (get Class classdef) (/. E []))
|
10
|
+
(if (empty? ClassDef)
|
11
|
+
(error "class ~A does not exist~%" Class)
|
12
|
+
ClassDef)))
|
13
|
+
|
14
|
+
(define get-value
|
15
|
+
Attribute Instance -> (let LookUp (assoc Attribute Instance)
|
16
|
+
(get-value-test LookUp)))
|
17
|
+
|
18
|
+
(define get-value-test
|
19
|
+
[ ] -> (error "no such attribute!~%")
|
20
|
+
[_ | fail] -> (error "no such value!~%")
|
21
|
+
[_ | Value] -> Value)
|
22
|
+
|
23
|
+
(define has-value?
|
24
|
+
Attribute Instance -> (let LookUp (assoc Attribute Instance)
|
25
|
+
(has-value-test LookUp)))
|
26
|
+
|
27
|
+
(define has-value-test
|
28
|
+
[ ] -> (error "no such attribute!~%")
|
29
|
+
[_ | fail] -> false
|
30
|
+
_ -> true)
|
31
|
+
|
32
|
+
(define has-attribute?
|
33
|
+
Attribute Instance -> (let LookUp (assoc Attribute Instance)
|
34
|
+
(not (empty? LookUp))))
|
35
|
+
|
36
|
+
(define change-value
|
37
|
+
_ class _ -> (error "cannot change the class of an instance!~%")
|
38
|
+
[ ] _ _ -> (error "no such attribute!~%")
|
39
|
+
[[Attribute | _] | Instance] Attribute Value
|
40
|
+
-> [[Attribute | Value] | Instance]
|
41
|
+
[Slot | Instance] Attribute Value
|
42
|
+
-> [Slot | (change-value Instance Attribute Value)])
|
43
|
+
|
44
|
+
(define instance-of
|
45
|
+
[[class | Class] | _] -> Class
|
46
|
+
_ -> (error "not a class instance!"))
|
@@ -1,14 +1,14 @@
|
|
1
|
-
(define depth'
|
2
|
-
{A --> (A --> (list A)) --> (A --> boolean) --> (A --> boolean) --> (list A)}
|
3
|
-
State Successors Goal? Fail? -> (depth-help' [State] Successors Goal? Fail? []))
|
4
|
-
|
5
|
-
(define depth-help'
|
6
|
-
{(list A) --> (A --> (list A)) --> (A --> boolean) --> (A --> boolean) --> (list A) --> (list A)}
|
7
|
-
[State | _] _ Goal? _ Path -> (reverse [State | Path]) where (Goal? State)
|
8
|
-
[State | _] _ _ Fail? _ -> [] where (Fail? State)
|
9
|
-
[State | _] Successors Goal? Fail? Path <- (fail-if empty?
|
10
|
-
(depth-help' (Successors State)
|
11
|
-
Successors Goal? Fail? [State | Path]))
|
12
|
-
[_ | States] Successors Goal? Fail? Path -> (depth-help' States Successors Goal? Fail? Path)
|
13
|
-
_ _ _ _ _ -> [])
|
14
|
-
|
1
|
+
(define depth'
|
2
|
+
{A --> (A --> (list A)) --> (A --> boolean) --> (A --> boolean) --> (list A)}
|
3
|
+
State Successors Goal? Fail? -> (depth-help' [State] Successors Goal? Fail? []))
|
4
|
+
|
5
|
+
(define depth-help'
|
6
|
+
{(list A) --> (A --> (list A)) --> (A --> boolean) --> (A --> boolean) --> (list A) --> (list A)}
|
7
|
+
[State | _] _ Goal? _ Path -> (reverse [State | Path]) where (Goal? State)
|
8
|
+
[State | _] _ _ Fail? _ -> [] where (Fail? State)
|
9
|
+
[State | _] Successors Goal? Fail? Path <- (fail-if empty?
|
10
|
+
(depth-help' (Successors State)
|
11
|
+
Successors Goal? Fail? [State | Path]))
|
12
|
+
[_ | States] Successors Goal? Fail? Path -> (depth-help' States Successors Goal? Fail? Path)
|
13
|
+
_ _ _ _ _ -> [])
|
14
|
+
|
@@ -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)
|