-
Notifications
You must be signed in to change notification settings - Fork 8
/
init.cora
374 lines (321 loc) · 10.4 KB
/
init.cora
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
(set 'null? (lambda (x) (= x ())))
(set 'cadr (lambda (x) (car (cdr x))))
(set 'caar (lambda (x) (car (car x))))
(set 'cdar (lambda (x) (cdr (car x))))
(set 'cddr (lambda (x) (cdr (cdr x))))
(set 'caddr (lambda (x) (car (cdr (cdr x)))))
(set 'cadddr (lambda (x) (car (cdr (cdr (cdr x))))))
(set 'cdddr (lambda (x) (cdr (cdr (cdr x)))))
(set 'rcons
(lambda (exp)
(if (cons? exp)
(cons 'cons
(cons (car exp)
(cons (rcons (cdr exp)) ())))
())))
(set 'pair? (lambda (x) (cons? x)))
(set '.reverse-h
(lambda (res l)
(if (cons? l)
(.reverse-h (cons (car l) res) (cdr l))
res)))
(set 'reverse (.reverse-h ()))
(set 'map-h
(lambda (res f l)
(if (cons? l)
(map-h (cons (f (car l)) res) f (cdr l))
(reverse res))))
(set 'map (lambda (f l) (map-h () f l)))
;; =============== The macro utilities ============
(set '*macros* ())
(set '*protect-symbol* (gensym 'protect))
(set '.protect (lambda (x)
(cons *protect-symbol* x)))
(set '.add-to-*macros*
(lambda (n v)
(set '*macros* (cons (cons n v) *macros*))))
;; (func macroexpand1-h
;; [x . y] [(cons m fn) . macros] => (fn [x . y]) where (= m x)
;; input [_ . macros] => (macroexpand1-h input macros)
;; input [] => input)
(set '.macroexpand1-h
(lambda (exp macros)
(if (= () macros)
exp
((lambda (item)
(if (if (cons? exp)
(if (= (car exp) (car item))
true
false)
false)
((cdr item) exp)
(.macroexpand1-h exp (cdr macros))))
(car macros)))))
(set '.macroexpand1
(lambda (exp)
(.macroexpand1-h exp *macros*)))
;; (func macroexpand
;; ['lambda args body] => ['lambda args (macroexpand body)]
;; ['quote xx] => xx
;; (cons x y) => y where (= x *protect-symbol*)
;; (cons x y) => (let exp1 (.macroexpand1 (cons x y))
;; (if (= exp1 (cons x y))
;; (map macroexpand exp1)
;; (macroexpand exp1)))
;; x => x)
(set '.macroexpand-boot
(lambda (exp)
(if (cons? exp)
(if (= (car exp) *protect-symbol*)
(cdr exp)
(if (= (car exp) (quote lambda))
(cons (quote lambda) (cons (cadr exp) (cons (.macroexpand-boot (caddr exp)) ())))
(if (= (car exp) (quote quote))
exp
((lambda (exp1)
(if (= exp1 exp)
(map .macroexpand-boot exp1)
(.macroexpand-boot exp1)))
(.macroexpand1 exp)))))
exp)))
(set 'macroexpand .macroexpand-boot)
;; (func defmacro-macro
;; ['defmacro name arg body] => ['.add-to-*macros* ['quote name] ['lambda arg body]])
(set 'defmacro-macro
(lambda (exp)
(cons '.add-to-*macros*
(cons (cons 'quote (cons (cadr exp) ()))
(cons (cons 'lambda (cons (caddr exp) (cdddr exp))) ())))))
(.add-to-*macros* 'defmacro defmacro-macro)
(.add-to-*macros* 'list (lambda (exp) (rcons (cdr exp))))
(defmacro defun (exp)
(list 'set (list 'quote (cadr exp))
(list 'lambda (caddr exp)
(cadddr exp))))
(defun elem? (x l)
(if (cons? l)
(if (= (car l) x)
true
(elem? x (cdr l)))
false))
(defun atom? (x) (not (cons? x)))
;; (defun .rewrite-let (params args exp)
;; (if (null? (cdr exp))
;; (cons (list 'lambda params (car exp)) args)
;; (.rewrite-let
;; (cons (car exp) params)
;; (cons (cadr exp) args)
;; (cddr exp))))
;; (defmacro let (exp)
;; (.rewrite-let () () (cdr exp)))
(defun .rewrite-let (exp)
(if (null? (cdr exp))
(car exp)
(cons 'let
(cons (car exp)
(cons (cadr exp)
(cons (.rewrite-let (cddr exp)) ()))))))
(defmacro let (exp)
(.rewrite-let (cdr exp)))
(defmacro cond (exp)
(if (= () (cdr exp))
(list 'error "no cond match")
(let curr (cadr exp)
(list 'if (car curr)
(cadr curr)
(cons 'cond (cddr exp))))))
(defun .rewrite-or (l)
(if (= l ())
false
(if (= (car l) true)
true
(let more (.rewrite-or (cdr l))
(if (= more true)
true
(list 'if (car l) true more))))))
(defmacro or (exp)
(.rewrite-or (cdr exp)))
(defun .rewrite-and (l)
(if (= () l)
true
(if (= (car l) false)
false
(let more (.rewrite-and (cdr l))
(if (= more false)
false
(list 'if (car l) more false))))))
(defmacro and (exp)
(.rewrite-and (cdr exp)))
(defun boolean? (x) (or (= x true) (= x false)))
(defun .rcons1 (pat)
(if (null? (cdr pat))
(car pat)
(list 'cons (car pat) (.rcons1 (cdr pat)))))
(defmacro list-rest (exp)
(.rcons1 (cdr exp)))
;; =============== pattern match utilities =================
(defun .match-cons-expander (pat expr body cc)
(let x (cadr pat)
y (caddr pat)
(if (and (cons? expr)
(= (car expr) 'cons))
(let e1 (cadr expr)
e2 (caddr expr)
(.match1 x e1
(.match1 y e2 body cc) cc))
(list 'if (list 'cons? expr)
(.match1 x (list 'car expr)
(.match1 y (list 'cdr expr)
body
cc)
cc)
(cons cc ())))))
(defun .match1 (pat expr body cc)
(let literal? (lambda (x) (and (atom? x) (not (symbol? x))))
(cond
((literal? pat) (if (= pat expr) body
(list 'if (list '= pat expr) body (list cc))))
((symbol? pat) (list 'let pat expr body))
((pair? pat) (cond
((= (car pat) 'quote) (list 'if (list '= pat expr) body (list cc)))
((= (car pat) 'cons) (.match-cons-expander pat expr body cc))))
(true (error (str "match fail " pat))))))
(defun .extract-rule-action (rules cc)
(let action (car (cdr rules))
(if (and (pair? action)
(= (car action) 'where))
(list 'if (cadr action)
(caddr action)
(list cc))
action)))
(defun .match-helper (value rules)
(cond
((null? rules) (list 'error "no match-help found!"))
((and (pair? rules) (pair? (cdr rules)))
(let pat (car rules)
cc (gensym 'cc)
(let action (.extract-rule-action rules cc)
(let curr (.match1 (macroexpand pat) value action cc)
rest (.match-helper value (cdr (cdr rules)))
(list 'let cc (list 'lambda () rest)
curr)))))))
(defun .rewrite-match (exp)
(let value (macroexpand (cadr exp))
rules (cddr exp)
(if (and (cons? value)
(not (= 'cons (car value))))
(let val (gensym 'val)
(list 'let val value (.match-helper val rules)))
(.match-helper value rules))))
(defmacro match (exp)
(.rewrite-match exp))
;; ================ the func macro ======================
(defun .extract-rules1 (input current result)
(match input
() (reverse result)
(list-rest '=> act 'where pred remain)
(let pat (cons 'list (reverse current))
(.extract-rules1 remain () (cons (list 'where pred act) (cons pat result))))
(list-rest '=> act remain)
(let pat (cons 'list (reverse current))
(.extract-rules1 remain () (cons act (cons pat result))))
(cons x y) (.extract-rules1 y (cons x current) result)))
(defun .extract-rules (input)
(.extract-rules1 input () ()))
(defun .rules-patterns (res rules)
(if (null? rules)
(reverse res)
(.rules-patterns (cons (car rules) res) (cddr rules))))
(defun .length-h (i l)
(if (= l ())
i
(.length-h (+ i 1) (cdr l))))
(defun length (l)
(.length-h 0 l))
(defun .filter-h (res fn l)
(if (cons? l)
(if (fn (car l))
(.filter-h (cons (car l) res) fn (cdr l))
(.filter-h res fn (cdr l)))
(reverse res)))
(defun filter (fn l)
(.filter-h () fn l))
(defun append (l1 l2)
(if (= l1 ())
l2
(cons (car l1) (append (cdr l1) l2))))
(defun .rules-arg-count (rules)
(let pats (.rules-patterns () rules)
len (lambda (x) (length (cdr x)))
(let counts (map len pats)
(let n (car counts)
(let dif (lambda (x) (not (= n x)))
(if (not (null? (filter dif (cdr counts))))
(error "inconsistent func rule args count")
n))))))
(defun .gen-parameters (n)
(if (= n 0)
()
(cons (gensym 'p) (.gen-parameters (- n 1)))))
(defmacro func (exp)
(let body (.extract-rules (cddr exp))
(let nargs (.rules-arg-count body)
(let args (.gen-parameters nargs)
(list 'defun (cadr exp) args
(cons 'match (cons (cons 'list args)
body)))))))
;; (func cora:walk
;; f [x . y] => (f (map (cora:walk f) [x . y]))
;; f x => (f x))
(func .propagate-boolean0
['car ['cons x _]] => x
['cdr ['cons _ x]] => x
['cons? ['cons _ _]] => true
['and true true] => true
['null? ()] => true
['null? ['cons _ _]] => false
['not true] => false
['not false] => true
['if true y z] => y
['if false y z] => z
x => x)
(func .propagate-boolean
['quote x] => ['quote x]
['cons? x] => (let x1 (.propagate-boolean x)
(.propagate-boolean0 ['cons? x1]))
['car x] => (let x1 (.propagate-boolean x)
(.propagate-boolean0 ['car x1]))
['cdr x] => (let x1 (.propagate-boolean x)
(.propagate-boolean0 ['cdr x1]))
['and x y] => (let x1 (.propagate-boolean x)
y1 (.propagate-boolean y)
(.propagate-boolean0 ['and x1 y1]))
['null? x] => (let x1 (.propagate-boolean x)
(.propagate-boolean0 ['null? x1]))
['not x] => (let x1 (.propagate-boolean x)
(.propagate-boolean0 ['not x1]))
['if x y z] => (let x1 (.propagate-boolean x)
y1 (.propagate-boolean y)
z1 (.propagate-boolean z)
(.propagate-boolean0 ['if x1 y1 z1]))
['lambda args body] => ['lambda args (.propagate-boolean body)]
[f . args] => (map .propagate-boolean [f . args])
x => x)
(defun macroexpand (exp)
(.propagate-boolean (.macroexpand-boot exp)))
;; ===============
;; begin x y z ...
;; (do x (do y z))
(func .rewrite-begin
[x] => x
[x y] => ['do x y]
[x . y] => ['do x (.rewrite-begin y)])
(defmacro begin (exp)
(.rewrite-begin (cdr exp)))
(func .rewrite-backquote
x => ['quote x] where (symbol? x)
['unquote x] => x
[x . more] => ['list . (map .rewrite-backquote [x . more])]
x => x)
(defmacro backquote (exp)
(.rewrite-backquote (cadr exp)))