-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbase.scm
224 lines (202 loc) · 7.15 KB
/
base.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
(define tagged? (lambda (t) (lambda (e) (and (pair? e) (eq? (car e) t)))))
(define s (lambda (e)
(cond
(((tagged? 'clo) e) `(clo _ ,(caddr e)))
((pair? e) (cons (s (car e)) (s (cdr e))))
(else e))))
(define code? (tagged? 'code))
(define code-exp cadr)
(define force-code
(lambda (f)
(if (code? f) (code-exp f)
(error 'force-code (format "expected code, not ~a" (s f))))))
(define make-let (lambda (e1 e2) `(let ,e1 ,e2)))
(define stFresh 0)
(define stBlock '()) ;; List[Exp]
(define stFun '()) ;; List[(Int,Env,Exp)]
(define reset!
(lambda ()
(set! stFresh 0)
(set! stBlock '())
(set! stFun '())))
(define run
(lambda (thunk)
(let ((sF stFresh)
(sB stBlock)
(sN stFun))
(let ((r (thunk)))
(set! stFresh sF)
(set! stBlock sB)
(set! stFun sN)
r))))
(define fresh
(lambda ()
(set! stFresh (+ 1 stFresh))
`(var ,(- stFresh 1))))
(define reify
(lambda (thunk)
(run (lambda ()
(set! stBlock '())
(let ((last (thunk)))
(fold-right make-let last stBlock))))))
(define reflect
(lambda (s)
(set! stBlock (append stBlock (list s)))
(fresh)))
(define reifyc
(lambda (thunk)
(reify (lambda () (force-code (thunk))))))
(define reflectc
(lambda (s) `(code ,(reflect s))))
(define reifyv
(lambda (thunk)
(run (lambda ()
(set! stBlock '())
(let ((res (thunk)))
(if (not (null? stBlock))
;; if we are generating code at all
;; the result must be code
(let ((last (force-code res)))
`(code ,(fold-right make-let last stBlock)))
res))))))
(define findFun
(lambda (v)
(let ((env (cadr v))
(e (caddr v)))
(define iter
(lambda (fs)
(if (null? fs)
#f
(let ((f (car fs)))
(if (and (equal? env (cadr f)) (equal? e (caddr f)))
(car f)
(iter (cdr fs)))))))
(iter stFun))))
;; NBE-style reify operator (semantics -> syntax)
(define lift
(lambda (v)
(cond
((number? v) v)
((symbol? v) v)
(((tagged? 'clo) v)
(let ((n (findFun v)))
(if n `(var ,n)
(let ((env2 (cadr v))
(e2 (caddr v)))
(set! stFun (append stFun (list (list stFresh env2 e2))))
(reflect
`(lambda ,(reify
(lambda ()
(force-code (evalms
(append env2 `((code ,(fresh)) (code ,(fresh))))
e2))))))))))
((code? v) (reflect `(lift ,(force-code v))))
((pair? v)
(let ((a (force-code (car v)))
(b (force-code (cdr v))))
(reflect `(cons ,a ,b)))))))
;; this is basically nth!
(define lookup
(lambda (env n)
(if (null? env) (error 'lookup "unbound variable")
(if (= n 0) (car env)
(lookup (cdr env) (- n 1))))))
(define binary-op
(lambda (fun)
(lambda (env e)
;; Use let* to enforce evaluation order.
(let* ((v1 (evalms env (cadr e)))
(v2 (evalms env (caddr e))))
(if (and (code? v1) (code? v2))
(reflectc (list (car e) (force-code v1) (force-code v2)))
(if (and (not (code? v1)) (not (code? v2)))
(fun v1 v2)
(error 'binary-op (format "stage error in ~a ~a ~a" (car e) (s v1) (s v2)))))))))
(define unary-op
(lambda (fun)
(lambda (env e)
(let ((v1 (evalms env (cadr e))))
(if (code? v1)
(reflectc (list (car e) (force-code v1)))
(fun v1))))))
(define b2n (lambda (r) (if r 1 0)))
(define pred-op
(lambda (fun)
(lambda (env e)
(let ((v1 (evalms env (cadr e))))
(if (code? v1)
(reflectc (list (car e) (force-code v1)))
(b2n (fun v1)))))))
(define log (lambda (e) (begin (display (s e)) (newline) e)))
(define lift-ref (lambda (e1 e2)
`(code (proc ,e1 ,(lambda (ignore) (e2))))))
(define evalms
(lambda (env e)
(cond
((number? e) e)
((symbol? e) e)
(((tagged? 'proc) e) ((caddr e) env))
(((tagged? 'var) e) (lookup env (cadr e)))
(((tagged? 'lambda) e) `(clo ,env ,(cadr e)))
(((tagged? 'let) e)
(let ((v1 (evalms env (cadr e))))
(evalms (append env (list v1)) (caddr e))))
(((tagged? 'lift) e)
`(code ,(lift (evalms env (cadr e)))))
(((tagged? 'lift-ref) e)
(lift-ref (evalms env (cadr e)) (lambda () (evalms env (caddr e)))))
(((tagged? 'run) e)
(let ((v1 (evalms env (cadr e)))
(thunk (lambda () (evalms env (caddr e)))))
(if (code? v1)
(reflectc `(run ,(force-code v1) ,(reifyc thunk)))
(reifyv (lambda () (evalms env (reifyc (lambda () (set! stFresh (length env)) (thunk)))))))))
(((tagged? 'code?) e)
(let ((v1 (evalms env (cadr e)))
(v2 (evalms env (caddr e))))
(if (code? v1)
(reflectc `(code? ,(force-code v1) ,(force-code v2)))
(b2n (code? v2)))))
(((tagged? 'log) e)
(let ((v1 (evalms env (cadr e)))
(v2 (evalms env (caddr e))))
(if (code? v1)
(reflectc `(log ,(force-code v1) ,(force-code (if (code? v2) v2 (lift-ref '_ (lambda () v2))))))
(log v2))))
(((tagged? 'if) e)
(let ((vc (evalms env (cadr e))))
(if (code? vc)
(reflectc `(if ,(force-code vc)
,(reifyc (lambda () (evalms env (caddr e))))
,(reifyc (lambda () (evalms env (cadddr e))))))
(if (number? vc)
(if (not (= vc 0)) (evalms env (caddr e)) (evalms env (cadddr e)))
(error 'evalms (format "if expects number for condition, not ~a in ~a" (s vc) (s e)))))))
(((tagged? '+) e) ((binary-op +) env e))
(((tagged? '-) e) ((binary-op -) env e))
(((tagged? '*) e) ((binary-op *) env e))
(((tagged? 'eq?) e) ((binary-op (lambda (x y) (b2n (eq? x y)))) env e))
(((tagged? 'car) e) ((unary-op car) env e))
(((tagged? 'cdr) e) ((unary-op cdr) env e))
(((tagged? 'number?) e) ((pred-op number?) env e))
(((tagged? 'symbol?) e) ((pred-op symbol?) env e))
(((tagged? 'pair?) e) ((pred-op pair?) env e))
(((tagged? 'null?) e) ((pred-op null?) env e))
;; cons is an introduction form, so needs explicit lifting
(((tagged? 'cons) e) (cons (evalms env (cadr e)) (evalms env (caddr e))))
(else ;; assume application
(let ((v1 (evalms env (car e)))
(v2 (evalms env (cadr e))))
(if (and (code? v1) (code? v2))
`(code ,(reflect `(,(force-code v1) ,(force-code v2))))
(if ((tagged? 'clo) v1)
(evalms (append (cadr v1) (list v1 v2)) (caddr v1))
(error 'evalms (format "app expects closure, not ~a in ~a" (s v1) (s e))))))))))
(define base-fac-anf
'(let (lambda
(let (if (var 1)
(let (- (var 1) 1)
(let ((var 0) (var 2)) (let (* (var 1) (var 3)) (var 4))))
1)
(var 2)))
(var 0)))