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.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -0
  3. data/.travis.yml +9 -3
  4. data/Gemfile +1 -4
  5. data/HISTORY.md +16 -0
  6. data/MIT_LICENSE.txt +1 -1
  7. data/README.md +25 -26
  8. data/Rakefile +3 -11
  9. data/bin/shen_test_suite.rb +15 -3
  10. data/bin/srrepl +6 -8
  11. data/lib/shen_ruby.rb +6 -1
  12. data/lib/shen_ruby/converters.rb +23 -0
  13. data/lib/shen_ruby/version.rb +1 -1
  14. data/shen-ruby.gemspec +4 -1
  15. data/shen/lib/shen_ruby/shen.rb +49 -33
  16. data/shen/release/benchmarks/N_queens.shen +45 -45
  17. data/shen/release/benchmarks/README.shen +14 -14
  18. data/shen/release/benchmarks/benchmarks.shen +52 -52
  19. data/shen/release/benchmarks/einstein.shen +32 -32
  20. data/shen/release/benchmarks/interpreter.shen +219 -219
  21. data/shen/release/benchmarks/jnk.shen +193 -193
  22. data/shen/release/benchmarks/powerset.shen +10 -10
  23. data/shen/release/benchmarks/prime.shen +10 -10
  24. data/shen/release/benchmarks/short.shen +129 -129
  25. data/shen/release/k_lambda/core.kl +181 -181
  26. data/shen/release/k_lambda/declarations.kl +131 -131
  27. data/shen/release/k_lambda/load.kl +84 -84
  28. data/shen/release/k_lambda/macros.kl +112 -112
  29. data/shen/release/k_lambda/prolog.kl +252 -252
  30. data/shen/release/k_lambda/reader.kl +222 -222
  31. data/shen/release/k_lambda/sequent.kl +166 -166
  32. data/shen/release/k_lambda/sys.kl +271 -271
  33. data/shen/release/k_lambda/t-star.kl +139 -139
  34. data/shen/release/k_lambda/toplevel.kl +135 -135
  35. data/shen/release/k_lambda/track.kl +103 -103
  36. data/shen/release/k_lambda/types.kl +324 -324
  37. data/shen/release/k_lambda/writer.kl +105 -105
  38. data/shen/release/k_lambda/yacc.kl +113 -113
  39. data/shen/release/test_programs/Chap13/problems.txt +26 -26
  40. data/shen/release/test_programs/README.shen +52 -52
  41. data/shen/release/test_programs/TinyLispFunctions.txt +15 -15
  42. data/shen/release/test_programs/TinyTypes.shen +55 -55
  43. data/shen/release/test_programs/binary.shen +24 -24
  44. data/shen/release/test_programs/bubble_version_1.shen +28 -28
  45. data/shen/release/test_programs/bubble_version_2.shen +22 -22
  46. data/shen/release/test_programs/calculator.shen +21 -21
  47. data/shen/release/test_programs/cartprod.shen +23 -23
  48. data/shen/release/test_programs/change.shen +25 -25
  49. data/shen/release/test_programs/classes-defaults.shen +94 -94
  50. data/shen/release/test_programs/classes-inheritance.shen +100 -100
  51. data/shen/release/test_programs/classes-typed.shen +74 -74
  52. data/shen/release/test_programs/classes-untyped.shen +46 -46
  53. data/shen/release/test_programs/depth_.shen +14 -14
  54. data/shen/release/test_programs/einstein.shen +34 -34
  55. data/shen/release/test_programs/fruit_machine.shen +46 -46
  56. data/shen/release/test_programs/interpreter.shen +217 -217
  57. data/shen/release/test_programs/metaprog.shen +85 -85
  58. data/shen/release/test_programs/minim.shen +192 -192
  59. data/shen/release/test_programs/mutual.shen +11 -11
  60. data/shen/release/test_programs/n_queens.shen +45 -45
  61. data/shen/release/test_programs/newton_version_1.shen +33 -33
  62. data/shen/release/test_programs/newton_version_2.shen +24 -24
  63. data/shen/release/test_programs/parse.prl +14 -14
  64. data/shen/release/test_programs/parser.shen +51 -51
  65. data/shen/release/test_programs/powerset.shen +10 -10
  66. data/shen/release/test_programs/prime.shen +10 -10
  67. data/shen/release/test_programs/prolog.shen +78 -78
  68. data/shen/release/test_programs/proof_assistant.shen +80 -80
  69. data/shen/release/test_programs/proplog_version_1.shen +25 -25
  70. data/shen/release/test_programs/proplog_version_2.shen +27 -27
  71. data/shen/release/test_programs/qmachine.shen +66 -66
  72. data/shen/release/test_programs/red-black.shen +54 -54
  73. data/shen/release/test_programs/search.shen +55 -55
  74. data/shen/release/test_programs/semantic_net.shen +44 -44
  75. data/shen/release/test_programs/spreadsheet.shen +34 -34
  76. data/shen/release/test_programs/stack.shen +27 -27
  77. data/shen/release/test_programs/streams.shen +20 -20
  78. data/shen/release/test_programs/strings.shen +57 -57
  79. data/shen/release/test_programs/structures-typed.shen +71 -71
  80. data/shen/release/test_programs/structures-untyped.shen +41 -41
  81. data/shen/release/test_programs/tests.shen +232 -232
  82. data/shen/release/test_programs/types.shen +11 -11
  83. data/shen/release/test_programs/whist.shen +239 -239
  84. data/shen/release/test_programs/yacc.shen +132 -132
  85. data/spec/shen_ruby/converters_spec.rb +48 -0
  86. data/spec/spec_helper.rb +1 -2
  87. metadata +55 -60
  88. data/k_lambda_spec/atom_spec.rb +0 -85
  89. data/k_lambda_spec/primitives/arithmetic_spec.rb +0 -175
  90. data/k_lambda_spec/primitives/assignments_spec.rb +0 -44
  91. data/k_lambda_spec/primitives/boolean_operations_spec.rb +0 -136
  92. data/k_lambda_spec/primitives/generic_functions_spec.rb +0 -120
  93. data/k_lambda_spec/primitives/lists_spec.rb +0 -40
  94. data/k_lambda_spec/primitives/strings_spec.rb +0 -77
  95. data/k_lambda_spec/primitives/symbols_spec.rb +0 -24
  96. data/k_lambda_spec/primitives/vectors_spec.rb +0 -92
  97. data/k_lambda_spec/spec_helper.rb +0 -29
  98. data/k_lambda_spec/support/shared_examples.rb +0 -124
  99. data/k_lambda_spec/tail_recursion_spec.rb +0 -30
  100. data/lib/kl.rb +0 -7
  101. data/lib/kl/absvector.rb +0 -12
  102. data/lib/kl/compiler.rb +0 -360
  103. data/lib/kl/cons.rb +0 -51
  104. data/lib/kl/empty_list.rb +0 -12
  105. data/lib/kl/environment.rb +0 -163
  106. data/lib/kl/error.rb +0 -4
  107. data/lib/kl/internal_error.rb +0 -7
  108. data/lib/kl/lexer.rb +0 -186
  109. data/lib/kl/primitives/arithmetic.rb +0 -60
  110. data/lib/kl/primitives/assignments.rb +0 -15
  111. data/lib/kl/primitives/booleans.rb +0 -21
  112. data/lib/kl/primitives/error_handling.rb +0 -13
  113. data/lib/kl/primitives/extensions.rb +0 -12
  114. data/lib/kl/primitives/generic_functions.rb +0 -29
  115. data/lib/kl/primitives/lists.rb +0 -23
  116. data/lib/kl/primitives/streams.rb +0 -28
  117. data/lib/kl/primitives/strings.rb +0 -63
  118. data/lib/kl/primitives/symbols.rb +0 -18
  119. data/lib/kl/primitives/time.rb +0 -17
  120. data/lib/kl/primitives/vectors.rb +0 -36
  121. data/lib/kl/reader.rb +0 -46
  122. data/spec/kl/cons_spec.rb +0 -12
  123. data/spec/kl/environment_spec.rb +0 -282
  124. data/spec/kl/interop_spec.rb +0 -68
  125. data/spec/kl/lexer_spec.rb +0 -149
  126. data/spec/kl/primitives/generic_functions_spec.rb +0 -29
  127. data/spec/kl/primitives/symbols_spec.rb +0 -21
  128. data/spec/kl/reader_spec.rb +0 -42
@@ -1,194 +1,194 @@
1
- (define kl-to-lisp
2
- Params Param -> Param where (element? Param Params)
3
- Params [type X _] -> (kl-to-lisp Params X)
4
- Params [lambda X Y] -> [FUNCTION [LAMBDA [X] (kl-to-lisp [X | Params] Y)]]
5
- Params [let X Y Z] -> [LET [[X (kl-to-lisp Params Y)]]
6
- (kl-to-lisp [X | Params] Z)]
7
- _ [defun F Params Code] -> [DEFUN F Params (kl-to-lisp Params Code)]
8
- Params [cond | Cond] -> [COND | (map (/. C (cond_code Params C)) (insert-default Cond))]
9
- Params [Param | X] -> (higher-order-code Param
10
- (map (/. Y (kl-to-lisp Params Y)) X))
11
- where (element? Param Params)
12
- Params [[X | Y] | Z] -> (higher-order-code (kl-to-lisp Params [X | Y])
13
- (map (/. W (kl-to-lisp Params W)) Z))
14
- Params [F | X] -> (assemble-application F
15
- (map (/. Y (kl-to-lisp Params Y)) X))
16
- where (symbol? F)
17
- _ [] -> []
18
- _ S -> [QUOTE S] where (or (symbol? S) (boolean? S))
19
- _ X -> X)
20
-
21
- (define insert-default
22
- [] -> [[true [ERROR "error: cond failure~%"]]]
23
- [[true X] | Y] -> [[true X] | Y]
24
- [Case | Cases] -> [Case | (insert-default Cases)])
25
-
26
- (define higher-order-code
27
- F X -> [let Args [LIST | X]
28
- [let NewF [maplispsym F]
29
- [trap-error [APPLY NewF Args]
30
- [lambda E [COND [[arity-error? F Args]
31
- [funcall [EVAL [nest-lambda F NewF]] Args]]
32
- [[EQ NewF [QUOTE or]]
33
- [funcall [lambda X1 [lambda X2 [or X1 X2]]] Args]]
34
- [[EQ NewF [QUOTE and]]
35
- [funcall [lambda X1 [lambda X2 [and X1 X2]]] Args]]
36
- [[EQ NewF [QUOTE trap-error]]
37
- [funcall [lambda X1 [lambda X2 [trap-error X1 X2]]] Args]]
38
- [[bad-lambda-call? NewF Args]
39
- [funcall NewF Args]]
40
- [T [relay-error E]]]]]]])
41
-
42
- (define bad-lambda-call?
43
- F Args -> (AND (FUNCTIONP F) (NOT (= (LIST-LENGTH Args) 1))))
44
-
45
- (define relay-error
46
- E -> (ERROR (error-to-string E)))
47
-
48
- (define funcall
49
- Lambda [] -> Lambda
50
- Lambda [X | Y] -> (funcall (FUNCALL Lambda X) Y))
51
-
52
- (define arity-error?
53
- F Args -> (AND (SYMBOLP F)
54
- (> (trap-error (arity F) (/. E -1)) (LIST-LENGTH Args)))
55
-
56
- (define nest-lambda
57
- F NewF -> (nest-lambda-help NewF (trap-error (arity F) (/. E -1))))
58
-
59
- (define nest-lambda-help
60
- F -1 -> F
61
- F 0 -> F
62
- F N -> (let X (gensym (protect Y))
63
- [lambda X (nest-lambda-help (add-p F X) (- N 1))]))
64
-
65
- (define add-p
66
- [F | X] Y -> (append [F | X] [Y])
67
- F X -> [F X])
68
-
69
- (define cond_code
70
- Params [Test Result] -> [(lisp_test Params Test)
71
- (kl-to-lisp Params Result)])
72
-
73
- (define lisp_test
74
- _ true -> T
75
- Params [and | Tests] -> [AND | (map (/. X (wrap (kl-to-lisp Params X))) Tests)]
76
- Params Test -> (wrap (kl-to-lisp Params Test)))
77
-
78
- (define wrap
79
- [cons? X] -> [CONSP X]
80
- [string? X] -> [STRINGP X]
81
- [number? X] -> [NUMBERP X]
82
- [empty? X] -> [NULL X]
83
- [and P Q] -> [AND (wrap P) (wrap Q)]
84
- [or P Q] -> [OR (wrap P) (wrap Q)]
85
- [not P] -> [NOT (wrap P)]
86
- [equal? X []] -> [NULL X]
87
- [equal? [] X] -> [NULL X]
88
- [equal? X [Quote Y]] -> [EQ X [Quote Y]]
89
- where (and (= (SYMBOLP Y) T) (= Quote QUOTE))
90
- [equal? [Quote Y] X] -> [EQ [Quote Y] X]
91
- where (and (= (SYMBOLP Y) T) (= Quote QUOTE))
92
- [equal? [fail] X] -> [EQ [fail] X]
93
- [equal? X [fail]] -> [EQ X [fail]]
94
- [equal? S X] -> [EQUAL S X] where (string? S)
95
- [equal? X S] -> [EQUAL X S] where (string? S)
96
- [equal? X Y] -> [shen-ABSEQUAL X Y]
97
- [shen-+string? [tlstr X]] -> [NOT [STRING-EQUAL [tlstr X] ""]]
98
- [shen-pvar? X] -> [AND [ARRAYP X] [NOT [STRINGP X]] [EQ [AREF X 0] [QUOTE shen-pvar]]]
99
- [tuple? X] -> [AND [ARRAYP X] [NOT [STRINGP X]] [EQ [AREF X 0] [QUOTE shen-tuple]]]
100
- [greater? X Y] -> [> X Y]
101
- [greater-than-or-equal-to? X Y] -> [>= X Y]
102
- [less? X Y] -> [< X Y]
103
- [less-than-or-equal-to? X Y] -> [<= X Y]
104
- X -> [wrapper X])
105
-
106
- (define wrapper
107
- true -> T
108
- false -> []
109
- X -> (error "boolean expected: not ~S~%" X))
110
-
111
- (define assemble-application
112
- hd [X] -> (protect [CAR X])
113
- tl [X] -> (protect [CDR X])
114
- cons [X Y] -> (protect [CONS X Y])
115
- append [X Y] -> (protect [APPEND X Y])
116
- reverse [X] -> (protect [REVERSE X])
117
- if [P Q R] -> (protect [IF (wrap P) Q R])
118
- + [1 X] -> [1+ X]
119
- + [X 1] -> [1+ X]
120
- - [X 1] -> [1- X]
121
- value [[Quote X]] -> X where (= Quote (protect QUOTE))
122
- set [[Quote X] [1+ X]] -> [INCF X] where (= Quote (protect QUOTE))
123
- set [[Quote X] [1- X]] -> [DECF X] where (= Quote (protect QUOTE))
124
- F X -> (let NewF (maplispsym F)
125
- Arity (trap-error (arity F) (/. E -1))
126
- (if (or (= Arity (length X)) (= Arity -1))
127
- [NewF | X]
128
- [funcall (nest-lambda F NewF) [(protect LIST) | X]])))
129
-
130
- (define maplispsym
131
- = -> equal?
132
- > -> greater?
133
- < -> less?
134
- >= -> greater-than-or-equal-to?
135
- <= -> less-than-or-equal-to?
136
- + -> add
137
- - -> subtract
138
- / -> divide
139
- * -> multiply
140
- F -> F)
141
-
142
- (define factorh
143
- [Defun F Params [Cond | Code]] -> [Defun F Params [BLOCK [] (process-tree (tree (map returns Code)))]]
144
- where (and (= Cond COND) (= Defun DEFUN))
145
- Code -> Code)
146
-
147
- (define returns
148
- [Test Result] -> [Test [RETURN Result]])
149
-
150
- (define process-tree
151
- (@p P Q R no-tag) -> [IF P (optimise-selectors P (process-tree Q)) (process-tree R)]
152
- (@p P Q R Tag) -> [TAGBODY [IF P (optimise-selectors P (process-tree Q))] Tag (process-tree R)]
153
- Q -> Q where (not (tuple? Q)))
154
-
155
- (define optimise-selectors
156
- Test Code -> (optimise-selectors-help (selectors-from Test) Code))
157
-
158
- (define selectors-from
159
- [Consp X] -> [[CAR X] [CDR X]] where (= Consp CONSP)
160
- [tuple? X] -> [[fst X] [snd X]]
161
- _ -> [])
162
-
163
- (define optimise-selectors-help
164
- [] Code -> Code
165
- [S1 S2] Code -> (let O1 (occurrences S1 Code)
166
- O2 (occurrences S2 Code)
167
- V1 (gensym V)
168
- V2 (gensym V)
169
- (if (and (> O1 1) (> O2 1))
170
- [LET [[V1 S1] [V2 S2]]
171
- (subst V1 S1 (subst V2 S2 Code))]
172
- (if (> O1 1)
173
- [LET [[V1 S1]] (subst V1 S1 Code)]
174
- (if (> O2 1)
175
- [LET [[V2 S2]] (subst V2 S2 Code)]
176
- Code)))))
177
-
178
- (define tree
179
- [[[And P Q] R] | S] -> (let Tag (gensym tag)
180
- Left (tree (append (branch-by P [[[And P Q] R] | S]) [[T [GO Tag]]]))
181
- Right (tree (branch-by-not P [[[And P Q] R] | S]))
182
- (@p P Left Right Tag)) where (= And AND)
183
- [[True Q] | _] -> Q where (= True T)
184
- [[P Q] | R] -> (@p P Q (tree R) no-tag))
185
-
186
- (define branch-by
187
- P [[[And P Q] R] | S] -> [[Q R] | (branch-by P S)] where (= And AND)
188
- P [[P R] | S] -> [[T R]]
189
- _ Code -> [])
190
-
191
- (define branch-by-not
192
- P [[[And P Q] R] | S] -> (branch-by-not P S) where (= And AND)
193
- P [[P R] | S] -> S
1
+ (define kl-to-lisp
2
+ Params Param -> Param where (element? Param Params)
3
+ Params [type X _] -> (kl-to-lisp Params X)
4
+ Params [lambda X Y] -> [FUNCTION [LAMBDA [X] (kl-to-lisp [X | Params] Y)]]
5
+ Params [let X Y Z] -> [LET [[X (kl-to-lisp Params Y)]]
6
+ (kl-to-lisp [X | Params] Z)]
7
+ _ [defun F Params Code] -> [DEFUN F Params (kl-to-lisp Params Code)]
8
+ Params [cond | Cond] -> [COND | (map (/. C (cond_code Params C)) (insert-default Cond))]
9
+ Params [Param | X] -> (higher-order-code Param
10
+ (map (/. Y (kl-to-lisp Params Y)) X))
11
+ where (element? Param Params)
12
+ Params [[X | Y] | Z] -> (higher-order-code (kl-to-lisp Params [X | Y])
13
+ (map (/. W (kl-to-lisp Params W)) Z))
14
+ Params [F | X] -> (assemble-application F
15
+ (map (/. Y (kl-to-lisp Params Y)) X))
16
+ where (symbol? F)
17
+ _ [] -> []
18
+ _ S -> [QUOTE S] where (or (symbol? S) (boolean? S))
19
+ _ X -> X)
20
+
21
+ (define insert-default
22
+ [] -> [[true [ERROR "error: cond failure~%"]]]
23
+ [[true X] | Y] -> [[true X] | Y]
24
+ [Case | Cases] -> [Case | (insert-default Cases)])
25
+
26
+ (define higher-order-code
27
+ F X -> [let Args [LIST | X]
28
+ [let NewF [maplispsym F]
29
+ [trap-error [APPLY NewF Args]
30
+ [lambda E [COND [[arity-error? F Args]
31
+ [funcall [EVAL [nest-lambda F NewF]] Args]]
32
+ [[EQ NewF [QUOTE or]]
33
+ [funcall [lambda X1 [lambda X2 [or X1 X2]]] Args]]
34
+ [[EQ NewF [QUOTE and]]
35
+ [funcall [lambda X1 [lambda X2 [and X1 X2]]] Args]]
36
+ [[EQ NewF [QUOTE trap-error]]
37
+ [funcall [lambda X1 [lambda X2 [trap-error X1 X2]]] Args]]
38
+ [[bad-lambda-call? NewF Args]
39
+ [funcall NewF Args]]
40
+ [T [relay-error E]]]]]]])
41
+
42
+ (define bad-lambda-call?
43
+ F Args -> (AND (FUNCTIONP F) (NOT (= (LIST-LENGTH Args) 1))))
44
+
45
+ (define relay-error
46
+ E -> (ERROR (error-to-string E)))
47
+
48
+ (define funcall
49
+ Lambda [] -> Lambda
50
+ Lambda [X | Y] -> (funcall (FUNCALL Lambda X) Y))
51
+
52
+ (define arity-error?
53
+ F Args -> (AND (SYMBOLP F)
54
+ (> (trap-error (arity F) (/. E -1)) (LIST-LENGTH Args)))
55
+
56
+ (define nest-lambda
57
+ F NewF -> (nest-lambda-help NewF (trap-error (arity F) (/. E -1))))
58
+
59
+ (define nest-lambda-help
60
+ F -1 -> F
61
+ F 0 -> F
62
+ F N -> (let X (gensym (protect Y))
63
+ [lambda X (nest-lambda-help (add-p F X) (- N 1))]))
64
+
65
+ (define add-p
66
+ [F | X] Y -> (append [F | X] [Y])
67
+ F X -> [F X])
68
+
69
+ (define cond_code
70
+ Params [Test Result] -> [(lisp_test Params Test)
71
+ (kl-to-lisp Params Result)])
72
+
73
+ (define lisp_test
74
+ _ true -> T
75
+ Params [and | Tests] -> [AND | (map (/. X (wrap (kl-to-lisp Params X))) Tests)]
76
+ Params Test -> (wrap (kl-to-lisp Params Test)))
77
+
78
+ (define wrap
79
+ [cons? X] -> [CONSP X]
80
+ [string? X] -> [STRINGP X]
81
+ [number? X] -> [NUMBERP X]
82
+ [empty? X] -> [NULL X]
83
+ [and P Q] -> [AND (wrap P) (wrap Q)]
84
+ [or P Q] -> [OR (wrap P) (wrap Q)]
85
+ [not P] -> [NOT (wrap P)]
86
+ [equal? X []] -> [NULL X]
87
+ [equal? [] X] -> [NULL X]
88
+ [equal? X [Quote Y]] -> [EQ X [Quote Y]]
89
+ where (and (= (SYMBOLP Y) T) (= Quote QUOTE))
90
+ [equal? [Quote Y] X] -> [EQ [Quote Y] X]
91
+ where (and (= (SYMBOLP Y) T) (= Quote QUOTE))
92
+ [equal? [fail] X] -> [EQ [fail] X]
93
+ [equal? X [fail]] -> [EQ X [fail]]
94
+ [equal? S X] -> [EQUAL S X] where (string? S)
95
+ [equal? X S] -> [EQUAL X S] where (string? S)
96
+ [equal? X Y] -> [shen-ABSEQUAL X Y]
97
+ [shen-+string? [tlstr X]] -> [NOT [STRING-EQUAL [tlstr X] ""]]
98
+ [shen-pvar? X] -> [AND [ARRAYP X] [NOT [STRINGP X]] [EQ [AREF X 0] [QUOTE shen-pvar]]]
99
+ [tuple? X] -> [AND [ARRAYP X] [NOT [STRINGP X]] [EQ [AREF X 0] [QUOTE shen-tuple]]]
100
+ [greater? X Y] -> [> X Y]
101
+ [greater-than-or-equal-to? X Y] -> [>= X Y]
102
+ [less? X Y] -> [< X Y]
103
+ [less-than-or-equal-to? X Y] -> [<= X Y]
104
+ X -> [wrapper X])
105
+
106
+ (define wrapper
107
+ true -> T
108
+ false -> []
109
+ X -> (error "boolean expected: not ~S~%" X))
110
+
111
+ (define assemble-application
112
+ hd [X] -> (protect [CAR X])
113
+ tl [X] -> (protect [CDR X])
114
+ cons [X Y] -> (protect [CONS X Y])
115
+ append [X Y] -> (protect [APPEND X Y])
116
+ reverse [X] -> (protect [REVERSE X])
117
+ if [P Q R] -> (protect [IF (wrap P) Q R])
118
+ + [1 X] -> [1+ X]
119
+ + [X 1] -> [1+ X]
120
+ - [X 1] -> [1- X]
121
+ value [[Quote X]] -> X where (= Quote (protect QUOTE))
122
+ set [[Quote X] [1+ X]] -> [INCF X] where (= Quote (protect QUOTE))
123
+ set [[Quote X] [1- X]] -> [DECF X] where (= Quote (protect QUOTE))
124
+ F X -> (let NewF (maplispsym F)
125
+ Arity (trap-error (arity F) (/. E -1))
126
+ (if (or (= Arity (length X)) (= Arity -1))
127
+ [NewF | X]
128
+ [funcall (nest-lambda F NewF) [(protect LIST) | X]])))
129
+
130
+ (define maplispsym
131
+ = -> equal?
132
+ > -> greater?
133
+ < -> less?
134
+ >= -> greater-than-or-equal-to?
135
+ <= -> less-than-or-equal-to?
136
+ + -> add
137
+ - -> subtract
138
+ / -> divide
139
+ * -> multiply
140
+ F -> F)
141
+
142
+ (define factorh
143
+ [Defun F Params [Cond | Code]] -> [Defun F Params [BLOCK [] (process-tree (tree (map returns Code)))]]
144
+ where (and (= Cond COND) (= Defun DEFUN))
145
+ Code -> Code)
146
+
147
+ (define returns
148
+ [Test Result] -> [Test [RETURN Result]])
149
+
150
+ (define process-tree
151
+ (@p P Q R no-tag) -> [IF P (optimise-selectors P (process-tree Q)) (process-tree R)]
152
+ (@p P Q R Tag) -> [TAGBODY [IF P (optimise-selectors P (process-tree Q))] Tag (process-tree R)]
153
+ Q -> Q where (not (tuple? Q)))
154
+
155
+ (define optimise-selectors
156
+ Test Code -> (optimise-selectors-help (selectors-from Test) Code))
157
+
158
+ (define selectors-from
159
+ [Consp X] -> [[CAR X] [CDR X]] where (= Consp CONSP)
160
+ [tuple? X] -> [[fst X] [snd X]]
161
+ _ -> [])
162
+
163
+ (define optimise-selectors-help
164
+ [] Code -> Code
165
+ [S1 S2] Code -> (let O1 (occurrences S1 Code)
166
+ O2 (occurrences S2 Code)
167
+ V1 (gensym V)
168
+ V2 (gensym V)
169
+ (if (and (> O1 1) (> O2 1))
170
+ [LET [[V1 S1] [V2 S2]]
171
+ (subst V1 S1 (subst V2 S2 Code))]
172
+ (if (> O1 1)
173
+ [LET [[V1 S1]] (subst V1 S1 Code)]
174
+ (if (> O2 1)
175
+ [LET [[V2 S2]] (subst V2 S2 Code)]
176
+ Code)))))
177
+
178
+ (define tree
179
+ [[[And P Q] R] | S] -> (let Tag (gensym tag)
180
+ Left (tree (append (branch-by P [[[And P Q] R] | S]) [[T [GO Tag]]]))
181
+ Right (tree (branch-by-not P [[[And P Q] R] | S]))
182
+ (@p P Left Right Tag)) where (= And AND)
183
+ [[True Q] | _] -> Q where (= True T)
184
+ [[P Q] | R] -> (@p P Q (tree R) no-tag))
185
+
186
+ (define branch-by
187
+ P [[[And P Q] R] | S] -> [[Q R] | (branch-by P S)] where (= And AND)
188
+ P [[P R] | S] -> [[T R]]
189
+ _ Code -> [])
190
+
191
+ (define branch-by-not
192
+ P [[[And P Q] R] | S] -> (branch-by-not P S) where (= And AND)
193
+ P [[P R] | S] -> S
194
194
  _ Code -> Code)
@@ -1,10 +1,10 @@
1
- (define powerset
2
- [] -> [[]]
3
- [X | Y] -> (let Powerset (powerset Y)
4
- (append (cons-X-to-each-set X Powerset) Powerset)))
5
-
6
- (define cons-X-to-each-set
7
- _ [ ] -> [ ]
8
- X [Y | Z] -> [[X | Y] | (cons-X-to-each-set X Z)])
9
-
10
-
1
+ (define powerset
2
+ [] -> [[]]
3
+ [X | Y] -> (let Powerset (powerset Y)
4
+ (append (cons-X-to-each-set X Powerset) Powerset)))
5
+
6
+ (define cons-X-to-each-set
7
+ _ [ ] -> [ ]
8
+ X [Y | Z] -> [[X | Y] | (cons-X-to-each-set X Z)])
9
+
10
+
@@ -1,10 +1,10 @@
1
- (define prime?
2
- X -> (prime* X (sqrt X) 2))
3
-
4
- (define prime*
5
- X Max Div -> false where (integer? (/ X Div))
6
- X Max Div -> true where (> Div Max)
7
- X Max Div -> (prime* X Max (+ 1 Div)))
8
-
9
-
10
-
1
+ (define prime?
2
+ X -> (prime* X (sqrt X) 2))
3
+
4
+ (define prime*
5
+ X Max Div -> false where (integer? (/ X Div))
6
+ X Max Div -> true where (> Div Max)
7
+ X Max Div -> (prime* X Max (+ 1 Div)))
8
+
9
+
10
+
@@ -1,129 +1,129 @@
1
- (define l_interpreter
2
- {A --> B}
3
- _ -> (read_eval_print_loop (output "~%L interpreter ~%~%~%~%l-interp --> ~A~%"
4
- (normal_form (input+ : l_formula)))))
5
-
6
- (define read_eval_print_loop
7
- {string --> A}
8
- _ -> (read_eval_print_loop
9
- (output "l-interp --> ~A~%"
10
- (normal_form (input+ : l_formula)))))
11
-
12
- (define normal_form
13
- {l_formula --> l_formula}
14
- X -> (fix ==> X))
15
-
16
- (define ==>
17
- {l_formula --> l_formula}
18
- [= X Y] -> (let X* (normal_form X)
19
- (let Y* (normal_form Y)
20
- (if (or (eval_error? X*) (eval_error? Y*))
21
- "error!"
22
- (if (= X* Y*) true false))))
23
- [[/. P X] Y] -> (let Match (match P (normal_form Y))
24
- (if (no_match? Match)
25
- "no match"
26
- (sub Match X)))
27
- [if X Y Z] -> (let X* (normal_form X)
28
- (if (= X* true)
29
- Y
30
- (if (= X* false)
31
- Z
32
- "error!")))
33
- [let X Y Z] -> [[/. X Z] Y]
34
- [@p X Y] -> (let X* (normal_form X)
35
- (let Y* (normal_form Y)
36
- (if (or (eval_error? X*) (eval_error? Y*))
37
- "error!"
38
- [@p X* Y*])))
39
- [cons X Y] -> (let X* (normal_form X)
40
- (let Y* (normal_form Y)
41
- (if (or (eval_error? X*) (eval_error? Y*))
42
- "error!"
43
- [cons X* Y*])))
44
- [++ X] -> (successor (normal_form X))
45
- [-- X] -> (predecessor (normal_form X))
46
- \*[cases X1 | Xn] -> (let Case1 (normal_form X1)
47
- (if (= Case1 "no match")
48
- [cases | Xn]
49
- Case1))
50
- [cases] -> "error!"
51
- [where X Y] -> [if X Y "no match"]
52
- [y-combinator [/. X Y]] -> (replace X [y-combinator [/. X Y]] Y)
53
- [X Y] -> (let X* (normal_form X)
54
- (let Y* (normal_form Y)
55
- (if (or (eval_error? X*) (eval_error? Y*))
56
- "error!"
57
- [X* Y*])))*\
58
- X -> X)
59
-
60
- (define eval_error?
61
- {l_formula --> boolean}
62
- "error!" -> true
63
- "no match" -> true
64
- _ -> false)
65
-
66
- (define successor
67
- {A --> l_formula}
68
- X -> (+ 1 X) where (number? X)
69
- _ -> "error!")
70
-
71
- (define predecessor
72
- {A --> l_formula}
73
- X -> (- X 1) where (number? X)
74
- _ -> "error!")
75
-
76
- \* (spy +) *\
77
-
78
- (define sub
79
- {[(pattern * l_formula)] --> l_formula --> l_formula}
80
- [] X -> X
81
- [(@p Var Val) | Assoc] X -> (sub Assoc (replace Var Val X)))
82
-
83
- (define match
84
- {pattern --> l_formula --> (list (pattern * l_formula))}
85
- P X -> [] where (== P X)
86
- P X -> [(@p P X)] where (variable? P)
87
- [cons P1 P2] [cons X Y] -> (let Match1 (match P1 X)
88
- (if (no_match? Match1)
89
- Match1
90
- (let Match2 (match P2 Y)
91
- (if (no_match? Match2)
92
- Match2
93
- (append Match1 Match2)))))
94
- [@p P1 P2] [@p X Y] -> (let Match1 (match P1 X)
95
- (if (no_match? Match1)
96
- Match1
97
- (let Match2 (match P2 Y)
98
- (if (no_match? Match2)
99
- Match2
100
- (append Match1 Match2)))))
101
-
102
- _ _ -> [(@p no matching)])
103
-
104
- (define no_match?
105
- {[(pattern * l_formula)] --> boolean}
106
- [(@p no matching)] -> true
107
- _ -> false)
108
-
109
- (define replace
110
- {pattern --> l_formula --> l_formula --> l_formula}
111
- V W [let V* X Y] -> [let V* X Y] where (== V V*)
112
- X Y X -> Y
113
- V W [= X Y] -> [= (replace V W X) (replace V W Y)]
114
- V W [/. P X] -> [/. P (replace V W X)] where (free? V P)
115
- V W [if X Y Z] -> [if (replace V W X) (replace V W Y) (replace V W Z)]
116
- V W [let X Y Z] -> [let X (replace V W Y) (replace V W Z)]
117
- V W [@p X Y] -> [@p (replace V W X) (replace V W Y)]
118
- \* V W [cons X Y] -> [cons (replace V W X) (replace V W Y)]
119
- V W [cases | Xn] -> [cases | (map (/. Xi (replace V W Xi)) Xn)]
120
- V W [where X Y] -> [where (replace V W X) (replace V W Y)]
121
- V W [X Y] -> [(replace V W X) (replace V W Y)] *\
122
- _ _ X -> X)
123
-
124
- (define free?
125
- {pattern --> pattern --> boolean}
126
- P P -> false
127
- P [cons P1 P2] -> (and (free? P P1) (free? P P2))
128
- P [@p P1 P2] -> (and (free? P P1) (free? P P2))
129
- _ _ -> true)
1
+ (define l_interpreter
2
+ {A --> B}
3
+ _ -> (read_eval_print_loop (output "~%L interpreter ~%~%~%~%l-interp --> ~A~%"
4
+ (normal_form (input+ : l_formula)))))
5
+
6
+ (define read_eval_print_loop
7
+ {string --> A}
8
+ _ -> (read_eval_print_loop
9
+ (output "l-interp --> ~A~%"
10
+ (normal_form (input+ : l_formula)))))
11
+
12
+ (define normal_form
13
+ {l_formula --> l_formula}
14
+ X -> (fix ==> X))
15
+
16
+ (define ==>
17
+ {l_formula --> l_formula}
18
+ [= X Y] -> (let X* (normal_form X)
19
+ (let Y* (normal_form Y)
20
+ (if (or (eval_error? X*) (eval_error? Y*))
21
+ "error!"
22
+ (if (= X* Y*) true false))))
23
+ [[/. P X] Y] -> (let Match (match P (normal_form Y))
24
+ (if (no_match? Match)
25
+ "no match"
26
+ (sub Match X)))
27
+ [if X Y Z] -> (let X* (normal_form X)
28
+ (if (= X* true)
29
+ Y
30
+ (if (= X* false)
31
+ Z
32
+ "error!")))
33
+ [let X Y Z] -> [[/. X Z] Y]
34
+ [@p X Y] -> (let X* (normal_form X)
35
+ (let Y* (normal_form Y)
36
+ (if (or (eval_error? X*) (eval_error? Y*))
37
+ "error!"
38
+ [@p X* Y*])))
39
+ [cons X Y] -> (let X* (normal_form X)
40
+ (let Y* (normal_form Y)
41
+ (if (or (eval_error? X*) (eval_error? Y*))
42
+ "error!"
43
+ [cons X* Y*])))
44
+ [++ X] -> (successor (normal_form X))
45
+ [-- X] -> (predecessor (normal_form X))
46
+ \*[cases X1 | Xn] -> (let Case1 (normal_form X1)
47
+ (if (= Case1 "no match")
48
+ [cases | Xn]
49
+ Case1))
50
+ [cases] -> "error!"
51
+ [where X Y] -> [if X Y "no match"]
52
+ [y-combinator [/. X Y]] -> (replace X [y-combinator [/. X Y]] Y)
53
+ [X Y] -> (let X* (normal_form X)
54
+ (let Y* (normal_form Y)
55
+ (if (or (eval_error? X*) (eval_error? Y*))
56
+ "error!"
57
+ [X* Y*])))*\
58
+ X -> X)
59
+
60
+ (define eval_error?
61
+ {l_formula --> boolean}
62
+ "error!" -> true
63
+ "no match" -> true
64
+ _ -> false)
65
+
66
+ (define successor
67
+ {A --> l_formula}
68
+ X -> (+ 1 X) where (number? X)
69
+ _ -> "error!")
70
+
71
+ (define predecessor
72
+ {A --> l_formula}
73
+ X -> (- X 1) where (number? X)
74
+ _ -> "error!")
75
+
76
+ \* (spy +) *\
77
+
78
+ (define sub
79
+ {[(pattern * l_formula)] --> l_formula --> l_formula}
80
+ [] X -> X
81
+ [(@p Var Val) | Assoc] X -> (sub Assoc (replace Var Val X)))
82
+
83
+ (define match
84
+ {pattern --> l_formula --> (list (pattern * l_formula))}
85
+ P X -> [] where (== P X)
86
+ P X -> [(@p P X)] where (variable? P)
87
+ [cons P1 P2] [cons X Y] -> (let Match1 (match P1 X)
88
+ (if (no_match? Match1)
89
+ Match1
90
+ (let Match2 (match P2 Y)
91
+ (if (no_match? Match2)
92
+ Match2
93
+ (append Match1 Match2)))))
94
+ [@p P1 P2] [@p X Y] -> (let Match1 (match P1 X)
95
+ (if (no_match? Match1)
96
+ Match1
97
+ (let Match2 (match P2 Y)
98
+ (if (no_match? Match2)
99
+ Match2
100
+ (append Match1 Match2)))))
101
+
102
+ _ _ -> [(@p no matching)])
103
+
104
+ (define no_match?
105
+ {[(pattern * l_formula)] --> boolean}
106
+ [(@p no matching)] -> true
107
+ _ -> false)
108
+
109
+ (define replace
110
+ {pattern --> l_formula --> l_formula --> l_formula}
111
+ V W [let V* X Y] -> [let V* X Y] where (== V V*)
112
+ X Y X -> Y
113
+ V W [= X Y] -> [= (replace V W X) (replace V W Y)]
114
+ V W [/. P X] -> [/. P (replace V W X)] where (free? V P)
115
+ V W [if X Y Z] -> [if (replace V W X) (replace V W Y) (replace V W Z)]
116
+ V W [let X Y Z] -> [let X (replace V W Y) (replace V W Z)]
117
+ V W [@p X Y] -> [@p (replace V W X) (replace V W Y)]
118
+ \* V W [cons X Y] -> [cons (replace V W X) (replace V W Y)]
119
+ V W [cases | Xn] -> [cases | (map (/. Xi (replace V W Xi)) Xn)]
120
+ V W [where X Y] -> [where (replace V W X) (replace V W Y)]
121
+ V W [X Y] -> [(replace V W X) (replace V W Y)] *\
122
+ _ _ X -> X)
123
+
124
+ (define free?
125
+ {pattern --> pattern --> boolean}
126
+ P P -> false
127
+ P [cons P1 P2] -> (and (free? P P1) (free? P P2))
128
+ P [@p P1 P2] -> (and (free? P P1) (free? P P2))
129
+ _ _ -> true)