-
Notifications
You must be signed in to change notification settings - Fork 3
/
library.scm
460 lines (431 loc) · 13.2 KB
/
library.scm
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
;;;
;;; library.scm (extended library definitions)
;;;
(define current-env (vau _ e e))
(define macro (vau (frml . body) env
(eval (list vau frml '_env_ (list eval (cons seq body) '_env_)) env) ))
;current-env
;==> #fexpr@6913
;(current-env)
;==> ()
;(define f (lambda x (seq (list list x (current-env) current-env))))
;(f 1 2 3)
;==> (#actor@575 (+1 +2 +3) ((x +1 +2 +3) (_ . #?)) #fexpr@6913)
;(define m (macro x (list list x (current-env) current-env)))
;(m 1 2 3)
;==> (#? #? #fexpr@6913)
;(define v (vau x e (eval (seq (list list x (current-env) current-env)) e) ))
;(define v (vau x e (eval (list list x (current-env) current-env) e) ))
;(v 1 2 3)
;==> (#? #? #fexpr@6913)
;(define q (macro x `(list x ,x ,@x 'x)))
;(q +1 +2 +3)
;==> (#? #? +1 +2 +3 x)
;(define qq (macro x `'(list x ,x ,@x 'x)))
;(qq +1 +2 +3)
;==> (list x (+1 +2 +3) +1 +2 +3 (quote x))
;(define qlist (macro x (list quote x)))
(define qlist (vau x _ x))
(define quote (vau (x) _ x))
(define seq (macro body _ (list (list* 'lambda '_ body)))) ; WARNING! introduces spurious local scope
(define integer? number?) ; integers are currently the only number type implemented
(define equal?
(lambda (x y)
(if (pair? x)
(if (pair? y)
(if (equal? (car x) (car y))
(equal? (cdr x) (cdr y))
#f)
#f)
(eq? x y))))
(define when
(macro (cond . body)
(list if cond (cons seq body) #unit)))
(define list* (lambda (h . t) (if (pair? t) (cons h (apply list* t)) h)))
;(define list* (lambda xs (reduce cons #? xs)))
(define cond
(macro clauses
(if (null? clauses)
#unit
(apply
(lambda ((test . body) . rest)
(list if test (cons seq body) (cons cond rest)) )
clauses) )))
;(define append (lambda (x y) (if (null? x) y (cons (car x) (append (cdr x) y))))) ; two lists only
(define append
(lambda x
(if (pair? x)
(apply
(lambda (h . t)
(if (pair? t)
(if (pair? h)
(cons
(car h)
(apply append (cons (cdr h) t)))
(apply append t))
h))
x)
x)))
(define filter
(lambda (pred? xs)
(if (pair? xs)
(if (pred? (car xs))
(cons (car xs) (filter pred? (cdr xs)))
(filter pred? (cdr xs)))
())))
(define reduce
(lambda (binop zero xs)
(if (pair? xs)
(if (pair? (cdr xs))
(binop (car xs) (reduce binop zero (cdr xs)))
(car xs))
zero)))
(define foldl
(lambda (binop zero xs)
(if (pair? xs)
(foldl binop (binop zero (car xs)) (cdr xs))
zero)))
(define foldr
(lambda (binop zero xs)
(if (pair? xs)
(binop (car xs) (foldr binop zero (cdr xs)))
zero)))
;(define reverse (lambda (xs) (if (pair? xs) (append (reverse (cdr xs)) (list (car xs))) xs))) ; O(n^2) algorithm
(define reverse
(lambda (xs)
(foldl (lambda (x y) (cons y x)) () xs)))
;; An alternative using an explicit helper function.
;(define push-pop (lambda (to from)
; (if (pair? from) (push-pop (cons (car from) to) (cdr from)) to)))
(define reverse
(lambda (xs)
(define push-pop (lambda (to from)
(if (pair? from) (push-pop (cons (car from) to) (cdr from)) to)))
(push-pop () xs)))
;(define map (lambda (f xs) (if (pair? xs) (cons (f (car xs)) (map f (cdr xs))) ()))) ; f takes only 1 arg
(define map
(lambda (f . xs)
(if (pair? (car xs))
(cons
(apply f (foldr (lambda (x y) (cons (car x) y)) () xs))
(apply map (cons f (foldr (lambda (x y) (cons (cdr x) y)) () xs))))
())))
;(define expand-let (vau (kvs . body) _ (cons (list* 'lambda (map car kvs) body) (map cadr kvs)))
(define let
(macro (bindings . body)
(cons
(list* lambda (map car bindings) body)
(map cadr bindings))))
(define provide
(macro (symbols . body)
(list define symbols
(list
(list lambda ()
(cons seq body)
(cons list symbols)) ))))
(define newline
(lambda ()
(emit 10)))
;; interative (tail-recursive) tree -> sequence
(define fringe
(lambda (t s r)
(cond
((pair? t)
(if (null? (cdr t))
(fringe (car t) s r)
(fringe (car t) s (cons (cdr t) r))))
((symbol? t)
(fringe r (cons t s) ()))
((null? r)
s)
(#t
(fringe r s ())) )))
;(fringe '((a b) c . d) () ())
;==> (d c b a)
;; match flat argument list w/ dotted-tail
(define zip ; extend `env` by binding names `x` to values `y`
(lambda (x y env)
(if (pair? x)
(cons (cons (car x) (car y)) (zip (cdr x) (cdr y) env))
(if (symbol? x)
(cons (cons x y) env) ; dotted-tail binds to &rest
env))))
;; helper function to recognize valid variable names
(define var-name? (lambda (x) (if (symbol? x) (if (eq? x '_) #f #t) #f)))
;; simple tree-recursive implementation
(define zip ; extend `env` by binding names `x` to values `y`
(lambda (x y env)
(if (pair? x)
(zip (car x) (car y) (zip (cdr x) (cdr y) env))
(if (var-name? x)
(cons (cons x y) env)
env))))
;(zip '((a b) c . d) '((1 2 3) (4 5 6) (7 8 9)) global-env)
;==> ((a . +1) (b . +2) (c +4 +5 +6) (d (+7 +8 +9)) . #actor@55)
;; interative (tail-recursive) implementation
(define zip-it ; extend `env` by binding names `x` to values `y`
(lambda (x y xs ys env)
(cond
((pair? x)
(if (null? (cdr x))
(zip-it (car x) (car y) xs ys env)
(zip-it (car x) (car y) (cons (cdr x) xs) (cons (cdr y) ys) env)))
((var-name? x)
(zip-it xs ys () () (cons (cons x y) env)))
((null? xs)
env)
(#t
(zip-it xs ys () () env)) )))
;(zip-it '((a b) c . d) '((1 2 3) (4 5 6) (7 8 9)) () () global-env)
;==> ((d (+7 +8 +9)) (c +4 +5 +6) (b . +2) (a . +1) . #actor@55)
;((lambda ((a b) c . d) (list a b c d)) '(1 2 3) '(4 5 6) '(7 8 9))
;==> (+1 +2 (+4 +5 +6) ((+7 +8 +9)))
;;
;; Quasi-Quotation based on `vau`
;;
(define quasiquote
(vau (x) e
(if (pair? x)
(if (eq? (car x) 'unquote)
(eval (cadr x) e)
(quasi-list x e))
x)))
(define quasi-list
(lambda (x e)
(if (pair? x)
(if (pair? (car x))
(if (eq? (caar x) 'unquote-splicing)
(append (eval (cadar x) e) (quasi-list (cdr x) e))
(cons (apply quasiquote (list (car x)) e) (quasi-list (cdr x) e)))
(cons (car x) (quasi-list (cdr x) e)))
x)))
;((lambda (x) `(x ,x ,(car x) ,(cdr x) ,@x 'x ,(current-env))) '(1 2 3))
(define gensym
(lambda ()
(cell Symbol_T (get-x '_) (get-y '_)) ))
;;;
;;; short-circuit logical connectives
;;;
;; examples from Kernel
($define! $and?
($vau x e
($cond
((null? x) #t)
((null? (cdr x)) (eval (car x) e)) ; tail context
((eval (car x) e) (apply (wrap $and?) (cdr x) e))
(#t #f) )))
($define! $or?
($vau x e
($cond
((null? x) #f)
((null? (cdr x)) (eval (car x) e)) ; tail context
((eval (car x) e) #t)
(#t (apply (wrap $or?) (cdr x) e)) )))
;; macro definitions using explicit construction
(define expand-or
(lambda (x)
(if (pair? x)
(if (pair? (cdr x))
(list let (list (list '_test_ (car x))) ; FIXME: need (gensym) here?
(list if '_test_
'_test_
(cons 'or (cdr x))))
(car x)) ; tail-call
#f)))
(define or (macro x (expand-or x)))
;(or #f (eq? 0 1) (not 1) -1 (eq? 1 1) -no-eval-) ==> -1
(define and
(macro x
(if (pair? x)
(if (pair? (cdr x))
(list let (list (list '_test_ (car x))) ; FIXME: need (gensym) here?
(list if '_test_
(cons 'and (cdr x))
'_test_))
(car x)) ; tail-call
#t)))
(define or
(macro x
(if (pair? x)
(if (pair? (cdr x))
(list let (list (list '_test_ (car x))) ; FIXME: need (gensym) here?
(list if '_test_
'_test_
(cons 'or (cdr x))))
(car x)) ; tail-call
#f)))
;; macro definitions using quasiquote templates
(define or
(macro x
(if (pair? x)
(if (pair? (cdr x))
`(let ((_test_ ,(car x))) ; FIXME: need (gensym) here?
(if _test_
_test_
(or ,@(cdr x))))
(car x)) ; tail-call
#f)))
(define expand-or
(lambda (x)
(if (pair? x)
(if (pair? (cdr x))
`(let ((_test_ ,(car x))) ; FIXME: need (gensym) here?
(if _test_
_test_
(or ,@(cdr x))))
(car x)) ; tail-call
#f)))
(define or (macro x (expand-or x)))
(define expand-or
(lambda (x)
(cond
((pair? x)
(define t (gensym))
`(seq
(define ,t ,(car x))
(if ,t ,t (or ,@(cdr x))) ))
(#t
#f) )))
(define or
(macro x
(cond
((pair? x)
(define t (gensym))
`(,seq
(,define ,t ,(car x))
(,if ,t ,t (,or ,@(cdr x))) ))
(#t
#f) )))
;;;
;;; macro helpers
;;;
;(define expand-with-gensyms
; (lambda (syms . body)
(define with-gensyms
(macro (syms . body)
(define defsym (lambda (s) `(define ,s (gensym))))
`(seq ,@(map defsym syms) ,@body) ))
;;;
;;; encapsulated (sealed) data-types
;;;
(define new-seal
(lambda ()
(define brand (gensym))
(define seal
(lambda (payload)
(cell brand payload)))
(define unseal
(lambda (sealed)
(if (eq? (get-t sealed) brand)
(get-x sealed)
#?)))
(define sealed?
(lambda objs
(if (pair? objs)
(if (eq? (get-t (car objs)) brand)
(apply sealed? (cdr objs))
#f)
#t)))
(list seal unseal sealed?)))
;;;
;;; secure immutable polymorphic abstract data-types
;;;
(define new-adt
(lambda (dispatch)
(define new
(lambda (x y z)
(cell dispatch x y z)))
(define adt?
(lambda objs
(if (pair? objs)
(if (eq? (get-t (car objs)) dispatch)
(apply adt? (cdr objs))
#f)
#t)))
(define fields
(lambda (adt)
(if (eq? (get-t adt) dispatch)
(list (get-x adt) (get-y adt) (get-z adt))
#?)))
(list new adt? fields) ))
(define adt-call
(lambda (adt . method)
;(print 'adt-call: (get-t adt) (get-x adt) (get-y adt) (get-z adt) (cons adt method)) ;; tracing...
(if (actor? (get-t adt))
(CALL (get-t adt) (cons adt method))
#?)))
;; dict = { t:dict-dispatch, x:key, y:value, z:next }
(define dict-dispatch
(lambda (this selector . args)
(let (((key value next) (dict-fields this)))
;(print 'dict-dispatch: this (cons selector args) key value next) ;; tracing...
(cond
((eq? selector 'get) ; (get <key>)
(if (eq? key (car args))
value
(if (dict? next)
(adt-call next 'get (car args))
#?)))
((eq? selector 'has) ; (has <key>)
(if (eq? key (car args))
#t
(if (dict? next)
(adt-call next 'has (car args))
#f)))
((eq? selector 'set) ; (set <key> <value>)
(if (adt-call this 'has (car args))
(new-dict (car args) (cadr args) (adt-call this 'delete (car args)))
(new-dict (car args) (cadr args) this) ))
((eq? selector 'delete) ; (delete <key>)
(if (eq? key (car args))
next
(if (dict? next)
(new-dict key value (adt-call next 'delete (car args)))
this)))
((eq? selector 'zip) ; (zip)
(if (dict? next)
(cons (cons key value) (adt-call next 'zip))
(cons (cons key value) next) ))
(#t
#?) )) ))
(define (new-dict dict? dict-fields) (new-adt dict-dispatch))
;> (define d0 (new-dict 'foo 123 ()))
;==> #unit
;> (dict? d0)
;==> #t
;> (adt-call d0 'has 'foo)
;==> #t
;> (adt-call d0 'has 'bar)
;==> #f
;> (adt-call d0 'get 'foo)
;==> +123
;> (adt-call d0 'get 'bar)
;==> #?
;> (adt-call d0 'delete 'foo)
;==> ()
;> (eq? (adt-call d0 'delete 'bar) d0)
;==> #t
;> (define d1 (adt-call d0 'set 'bar 456))
;==> #unit
;> (dict? d1)
;==> #t
;> (adt-call d1 'get 'foo)
;==> +123
;> (adt-call d1 'get 'bar)
;==> +456
;> (list d0 d1)
;==> (^6453 ^10641)
;> (list (get-t d0) (get-x d0) (get-y d0) (get-z d0))
;==> (#actor@5038 foo +123 ())
;> (list (get-t d1) (get-x d1) (get-y d1) (get-z d1))
;==> (#actor@5038 bar +456 ^6453)
;> (eq? (caddr (dict-fields d1)) d0)
;==> #t
;;;
;;; Little Schemer (4th edition)
;;;
(define add1 (lambda (x) (+ x 1)))
(define sub1 (lambda (x) (- x 1)))
(define atom? (lambda (x) (and (not (pair? x)) (not (null? x)))))
(define list? (lambda (p) (if (pair? p) (list? (cdr p)) (null? p))))
(define member? (lambda (x xs) (if (pair? xs) (or (eq? x (car xs)) (member? x (cdr xs))) #f)))