-
Notifications
You must be signed in to change notification settings - Fork 6
/
script.scm
697 lines (599 loc) · 16.6 KB
/
script.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
;; http://github.com/namin/lambdajam
;;; in racket:
;;; #lang scheme
(load "test-check.scm")
(load "pmatch.scm")
(load "sugar.scm")
;; Scheme
(+ 1 2)
(+ (+ 1 2) (+ 3 4))
(+ 3 7)
((lambda (x) (+ x 1)) 1)
((lambda () 1))
(if (= 1 1) 2 (/ 1 0))
((if (= 1 1) - +) 2 1)
(((lambda () +)) 1 2)
((lambda () (display "hello")))
+
(define a 1)
a
(define inc (lambda (x) (+ x 1)))
(inc 1)
(define adder
(lambda (x)
(lambda (y)
(+ x y))))
(adder 1) ;; #<procedure>
((adder 1) 2) ;; 3
(cons 1
(cons 2 '())
)
;; (1 . '(2 . '()))
;; (1 2)
(cdr (cons 'a 'd))
(pair? (cons 1 2)) ;; #t
(list? (cons 1 2)) ;; #f
(null? '()) ;; #t
(null? (cons 1 '())) ;; #f
(cons 'a (cons 'b 'c))
;;; Proper List
;;; either:
;;; (1) empty list
;;; (2) a pair whose cdr is a proper list
(cadr '(1 2 3))
(car (cdr '(1 2 3)))
(append '(1 2 3) '(4 5 6))
(define append
(lambda (xs ys)
(if (null? xs)
ys
(cons (car xs) (append (cdr xs) ys)))))
(define append
(lambda (xs ys)
(cond
((null? xs) ys)
(else (cons (car xs) (append (cdr xs) ys))))))
(define rember
(lambda (x xs)
(cond
((null? xs) '())
((pair? (car xs))
(cons (rember x (car xs))
(rember x (cdr xs))))
((eq? x (car xs))
(rember x (cdr xs)))
(else
(cons (car xs)
(rember x (cdr xs)))))))
(rember 'a '(a b c)) ;; '(b c)
(rember 'b '(a b c b)) ;; '(a c)
(rember 'b '(a (b a) c b)) ;; '(a (a) c)
(rember 'b '(a (b b (b) a b) c b))
(define foo '(hello there))
`(,foo ,foo)
`(,(+ 1 2) ,(+ 3 4)) ;; (3 7)
`(,(+ 1 2) (+ 3 4)) ;; (3 (+ 3 4))
;;;; -------------------------------------------------
;; Interpreters
;;; Call-By-Value (same as in Scheme)
;;; x, y, z (variables)
;;; e = x (variable)
;;; (lambda (x) e_0) (abstraction)
;;; (e_1 e_2) (application)
;;; env: variable name -> value
(define empty-env
(lambda (x) (error 'env-lookup "unbound variable")))
(define eval-exp
(lambda (exp env)
(pmatch
exp
(,x (guard (symbol? x)) (env x))
(,n (guard (number? n)) n)
(,b (guard (boolean? b)) b)
((zero? ,e)
(zero? (eval-exp e env)))
((sub1 ,e)
(sub1 (eval-exp e env)))
((* ,e1 ,e2)
(* (eval-exp e1 env) (eval-exp e2 env)))
((if ,c ,a ,b) ;; '(if (zero? 1) 1 2) c will become '(zero? 1)
(if (eval-exp c env)
(eval-exp a env)
(eval-exp b env)))
((lambda (,x) ,body) ;; lambda syntax
(lambda (a) ;; closure representation
(eval-exp body
(lambda (y) ;; env extension
(if (eq? y x)
a
(env y))))))
((,e1 ,e2)
((eval-exp e1 env)
(eval-exp e2 env))))))
(define eval-top
(lambda (exp)
(eval-exp exp empty-env)))
(eg (eval-exp 'x
(lambda (y) (if (eq? y 'x) 1 (empty-env y))))
1)
(eg (eval-top '((lambda (x) 1) 2))
1)
(eg (eval-exp '((lambda (x) x) 2)
empty-env)
2)
;; (eval-exp '((lambda (x) (x x)) (lambda (x) (x x))) empty-env)
(eg (eval-exp '((lambda (y) y) x)
(lambda (y) (if (eq? y 'x) 1 (empty-env y))))
1)
(eval-top '(((lambda (fun)
((lambda (F)
(F F))
(lambda (F)
(fun (lambda (x) ((F F) x))))))
(lambda (factorial)
(lambda (n)
(if (zero? n)
1
(* n (factorial (sub1 n)))))))
6))
;;; Program Transformations
;;; CPS: continuation-passing style
;;; continuations
;;; representation of the rest of the work to be done
(+ 1 2 (+ 3 4))
((lambda (v) v) (+ 1 2 (+ 3 4)))
((lambda (HOLE) (+ 1 2 HOLE)) (+ 3 4)) ;; 12
((lambda (v) (+ 1 v (+ 3 4))) 2)
(+ (+ 1 2) (+ 3 4))
((lambda (v) (+ v (+ 3 4))) (+ 1 2))
((lambda (v) (+ (+ 1 2) v)) (+ 3 4))
(lambda (v) v) ;; identity continuation
;;; Continuation-Passing Style
;;; direct-style
(define factorial
(lambda (n)
(if (= n 0)
1
(* n (factorial (- n 1))))))
(trace factorial)
;;(factorial -1)
;;; accumulator
(define factorial-iter
(lambda (n acc)
(if (= n 0)
acc
(factorial-iter (- n 1) (* acc n)))))
(define factorial
(lambda (n)
(factorial-iter n 1)))
(trace factorial-iter)
;;(factorial -1)
;;; Steps
;;; - Change the name with -cps.
;;; - Add k argument to lambda.
;;; - Return by applying k.
;;; - Serious calls need to be transformed.
(define factorial-cps
(lambda (n k)
(if (= n 0)
(k 1)
(factorial-cps (- n 1) (lambda (v)
(k (* n v)))))))
(define factorial
(lambda (n)
(factorial-cps n (lambda (v) v))))
(factorial 5)
(trace factorial-cps)
;;; direct-style
(define fib
(lambda (n)
(if (< n 2) n
(+ (fib (- n 1)) (fib (- n 2))))))
(define fib-cps
(lambda (n k)
(if (< n 2) (k n)
(fib-cps (- n 2)
(lambda (v2)
(fib-cps (- n 1) (lambda (v1)
(k (+ v1 v2)))))))))
(define fib
(lambda (n)
(fib-cps n (lambda (v) v))))
(fib 5)
;;; Trampolining
(define factorial-cps
(lambda (n k)
(lambda ()
(if (= n 0)
(k 1)
(factorial-cps (- n 1) (lambda (v)
(k (* n v))))))))
(define factorial-cps
(lambda (n k)
(if (= n 0)
(lambda () (k 1))
(lambda () (factorial-cps (- n 1) (lambda (v)
(k (* n v))))))))
(define factorial
(lambda (n)
(let ((r (factorial-cps n (lambda (v) v))))
(while (procedure? r)
(set! r (r)) ;; body of the while
r)) ;; return after the while
))
(define factorial
(lambda (n)
(let loop ((r (factorial-cps n (lambda (v) v))))
(if (procedure? r)
(loop (r))
r))))
;;; CPS-ing interpreter
(define empty-env
(lambda (x) (error 'env-lookup "unbound variable")))
(define eval-exp-cps
(lambda (exp env k)
(pmatch
exp
(,x (guard (symbol? x)) (k (env x)))
(,n (guard (number? n)) (k n))
(,b (guard (boolean? b)) (k b))
((zero? ,e)
(eval-exp-cps e env (lambda (v)
(k (zero? v)))))
((sub1 ,e)
(eval-exp-cps e env (lambda (v)
(k (sub1 v)))))
((* ,e1 ,e2)
(eval-exp-cps e1 env (lambda (v1)
(eval-exp-cps e2 env (lambda (v2)
(k (* v1 v2)))))))
((if ,c ,a ,b)
(eval-exp-cps c env (lambda (vc)
(if vc
(eval-exp-cps a env k)
(eval-exp-cps b env k)))))
((lambda (,x) ,body)
(k (lambda (a k^)
(eval-exp-cps body
(lambda (y)
(if (eq? y x)
a
(env y)))
k^))))
((,e1 ,e2)
(eval-exp-cps e1 env (lambda (p)
(eval-exp-cps e2 env (lambda (a)
(p a k)))))))))
(define eval-top
(lambda (exp)
(eval-exp-cps exp empty-env (lambda (v) v))))
(trace eval-exp-cps)
(eg (eval-top '((lambda (x) 1) 2))
1)
(eval-top '(((lambda (fun)
((lambda (F)
(F F))
(lambda (F)
(fun (lambda (x) ((F F) x))))))
(lambda (factorial)
(lambda (n)
(if (zero? n)
1
(* n (factorial (sub1 n)))))))
6))
;;; Representation Independence wrt to Continuations
(let ()
(define apply-k
(lambda (k v)
(k v)))
(define empty-k
(lambda ()
(lambda (v) v)))
(define factorial-k
(lambda (n k)
(lambda (v)
(apply-k k (* n v)))))
(define factorial-cps
(lambda (n k)
(if (= n 0)
(apply-k k 1)
(factorial-cps (- n 1)
(factorial-k n k)))))
(define factorial
(lambda (n)
(factorial-cps n (empty-k))))
(eg (factorial 5) 120)
)
;;; First-Order Representation for continuations
;;; Defunctionalization
(let ()
(trace-define apply-k
(lambda (k^ v)
(pmatch
k^
((empty-k)
v)
((factorial-k ,n ,k)
(apply-k k (* n v)))
;(,k^ (guard (procedure? k^))
; (k^ v))
)))
(define empty-k
(lambda ()
`(empty-k)))
(define factorial-k
(lambda (n k)
`(factorial-k ,n ,k)))
(trace-define factorial-cps
(lambda (n k)
(if (= n 0)
(apply-k k 1)
(factorial-cps (- n 1)
(factorial-k n k)))))
(define factorial
(lambda (n)
(factorial-cps n (empty-k))))
(eg (factorial 5) 120)
)
;;; Representation Independence wrt environment
(let ()
(define apply-env
(lambda (env x)
(env x)))
(define empty-env
(lambda ()
(lambda (x) (error 'env-lookup "unbound variable"))))
(define extend-env
(lambda (x a env)
(lambda (y)
(if (eq? y x)
a
(apply-env env y)))))
(define eval-exp-cps
(lambda (exp env k)
(pmatch
exp
(,x (guard (symbol? x)) (k (apply-env env x)))
(,n (guard (number? n)) (k n))
(,b (guard (boolean? b)) (k b))
((zero? ,e)
(eval-exp-cps e env (lambda (v)
(k (zero? v)))))
((sub1 ,e)
(eval-exp-cps e env (lambda (v)
(k (sub1 v)))))
((* ,e1 ,e2)
(eval-exp-cps e1 env (lambda (v1)
(eval-exp-cps e2 env (lambda (v2)
(k (* v1 v2)))))))
((if ,c ,a ,b)
(eval-exp-cps c env (lambda (vc)
(if vc
(eval-exp-cps a env k)
(eval-exp-cps b env k)))))
((lambda (,x) ,body)
(k (lambda (a k^)
(eval-exp-cps body
(extend-env x a env)
k^))))
((,e1 ,e2)
(eval-exp-cps e1 env (lambda (p)
(eval-exp-cps e2 env (lambda (a)
(p a k)))))))))
(define eval-top
(lambda (exp)
(eval-exp-cps exp (empty-env) (lambda (v) v))))
(eg (eval-top '((lambda (x) 1) 2))
1)
(eval-top '(((lambda (fun)
((lambda (F)
(F F))
(lambda (F)
(fun (lambda (x) ((F F) x))))))
(lambda (factorial)
(lambda (n)
(if (zero? n)
1
(* n (factorial (sub1 n)))))))
6))
)
;;;
(let ()
(define apply-env
(lambda (env^ y)
(pmatch
env^
((empty-env)
(error 'apply-env "unbound variable"))
((extend-env ,x ,a ,env)
(if (eq? y x)
a
(apply-env env y)))
(,env^ (guard (procedure? env^))
(env^ x)))))
(define empty-env
(lambda ()
`(empty-env)))
(define extend-env
(lambda (x a env)
`(extend-env ,x ,a ,env)))
(trace-define eval-exp-cps
(lambda (exp env k)
(pmatch
exp
(,x (guard (symbol? x)) (k (apply-env env x)))
(,n (guard (number? n)) (k n))
(,b (guard (boolean? b)) (k b))
((zero? ,e)
(eval-exp-cps e env (lambda (v)
(k (zero? v)))))
((sub1 ,e)
(eval-exp-cps e env (lambda (v)
(k (sub1 v)))))
((* ,e1 ,e2)
(eval-exp-cps e1 env (lambda (v1)
(eval-exp-cps e2 env (lambda (v2)
(k (* v1 v2)))))))
((if ,c ,a ,b)
(eval-exp-cps c env (lambda (vc)
(if vc
(eval-exp-cps a env k)
(eval-exp-cps b env k)))))
((lambda (,x) ,body)
(k (lambda (a k^)
(eval-exp-cps body
(extend-env x a env)
k^))))
((,e1 ,e2)
(eval-exp-cps e1 env (lambda (p)
(eval-exp-cps e2 env (lambda (a)
(p a k)))))))))
(define eval-top
(lambda (exp)
(eval-exp-cps exp (empty-env) (lambda (v) v))))
(eg (eval-top '((lambda (x) 1) 2))
1)
(eval-top '(((lambda (fun)
((lambda (F)
(F F))
(lambda (F)
(fun (lambda (x) ((F F) x))))))
(lambda (factorial)
(lambda (n)
(if (zero? n)
1
(* n (factorial (sub1 n)))))))
6))
)
(let ()
(define apply-env
(lambda (env^ y)
(cond
((assq y env^) => cdr)
(else (error 'apply-env "unbound variable")))))
(define empty-env
(lambda ()
`()))
(define extend-env
(lambda (x a env)
(cons (cons x a) env)))
(trace-define eval-exp-cps
(lambda (exp env k)
(pmatch
exp
(,x (guard (symbol? x)) (k (apply-env env x)))
(,n (guard (number? n)) (k n))
(,b (guard (boolean? b)) (k b))
((zero? ,e)
(eval-exp-cps e env (lambda (v)
(k (zero? v)))))
((sub1 ,e)
(eval-exp-cps e env (lambda (v)
(k (sub1 v)))))
((* ,e1 ,e2)
(eval-exp-cps e1 env (lambda (v1)
(eval-exp-cps e2 env (lambda (v2)
(k (* v1 v2)))))))
((if ,c ,a ,b)
(eval-exp-cps c env (lambda (vc)
(if vc
(eval-exp-cps a env k)
(eval-exp-cps b env k)))))
((lambda (,x) ,body)
(k (lambda (a k^)
(eval-exp-cps body
(extend-env x a env)
k^))))
((,e1 ,e2)
(eval-exp-cps e1 env (lambda (p)
(eval-exp-cps e2 env (lambda (a)
(p a k)))))))))
(define eval-top
(lambda (exp)
(eval-exp-cps exp (empty-env) (lambda (v) v))))
(eg (eval-top '((lambda (x) 1) 2))
1)
(eval-top '(((lambda (fun)
((lambda (F)
(F F))
(lambda (F)
(fun (lambda (x) ((F F) x))))))
(lambda (factorial)
(lambda (n)
(if (zero? n)
1
(* n (factorial (sub1 n)))))))
6))
)
;;; adding call/cc
(let ()
(define apply-env
(lambda (env^ y)
(cond
((assq y env^) => cdr)
(else (error 'apply-env "unbound variable")))))
(define empty-env
(lambda ()
`()))
(define extend-env
(lambda (x a env)
(cons (cons x a) env)))
(trace-define eval-exp-cps
(lambda (exp env k)
(pmatch
exp
(,x (guard (symbol? x)) (k (apply-env env x)))
(,n (guard (number? n)) (k n))
(,b (guard (boolean? b)) (k b))
((zero? ,e)
(eval-exp-cps e env (lambda (v)
(k (zero? v)))))
((sub1 ,e)
(eval-exp-cps e env (lambda (v)
(k (sub1 v)))))
((* ,e1 ,e2)
(eval-exp-cps e1 env (lambda (v1)
(eval-exp-cps e2 env (lambda (v2)
(k (* v1 v2)))))))
((if ,c ,a ,b)
(eval-exp-cps c env (lambda (vc)
(if vc
(eval-exp-cps a env k)
(eval-exp-cps b env k)))))
((call/cc ,e) ;; (call/cc (lambda (cont) ...))
(eval-exp-cps e env (lambda (p)
(p (lambda (v k^) (k v)) k))))
((lambda (,x) ,body)
(k (lambda (a k^)
(eval-exp-cps body
(extend-env x a env)
k^))))
((,e1 ,e2)
(eval-exp-cps e1 env (lambda (p)
(eval-exp-cps e2 env (lambda (a)
(p a k)))))))))
(define eval-top
(lambda (exp)
(eval-exp-cps exp (empty-env) (lambda (v) v))))
(eg (eval-top '((lambda (x) 1) 2))
1)
(eval-top '(((lambda (fun)
((lambda (F)
(F F))
(lambda (F)
(fun (lambda (x) ((F F) x))))))
(lambda (factorial)
(lambda (n)
(if (zero? n)
1
(* n (factorial (sub1 n)))))))
6))
(eg (eval-top '(call/cc (lambda (k) (* 3 2))))
6)
(eg (eval-top '(call/cc (lambda (k) (* 3 (k 2)))))
2)
(eg (eval-top '(call/cc (lambda (k) (* (k 2) 0))))
2)
(eg (eval-top '(* 3 (call/cc (lambda (k) (* (k 2) 0)))))
6)
)