-
Notifications
You must be signed in to change notification settings - Fork 1
/
cps.rkt
276 lines (254 loc) · 7.88 KB
/
cps.rkt
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
#lang racket
(require "library.rkt"
"components.rkt"
"mutrec.rkt"
"data.rkt")
(provide cps-tree cps? set-cps?! init-cps!)
(define init-cont-name #f)
(define init-cont-def #f)
(define init-cont #f)
(define cps? #f)
(define (set-cps?! v) (set! cps? v))
(define (init-cps!)
(define init-kvar (make-new-cont-arg))
(set-cps?! #t)
(set! init-cont (make-E (Lam (list init-kvar)
(make-E (Var init-kvar)))))
(set-E-cont?! init-cont #t)
(set-Name-binder! init-kvar init-cont)
(set! init-cont-name (make-new-cont-var))
(set! init-cont-def (make-Define init-cont-name init-cont)))
(define (trivial? exp)
(let ([triv-folder (lambda (args)
(for/and ([arg (in-list args)]) (trivial? arg)))])
(match exp
[(Define x e) (trivial? e)]
[(E: (or (? Const?) (? Var?))) #t]
[(E: (Letr bindings body))
(and (triv-folder bindings) (trivial? body))]
[(E: (Let bindings body))
(and (triv-folder bindings) (trivial? body))]
[(E: (App e0 args))
(match e0
[(E: (Var x))
(and (Name-primitive? x) (triv-folder args))]
[_ #f])]
[else #f])))
(define (gen-cont)
(gensym "K"))
(define (gen-cont-arg)
(gensym "V"))
(define (make-new-cont-var)
(let [(name (make-Name (gen-cont) '(cont)))]
(set-Name-cont?! name #t)
name))
(define (make-new-cont-arg)
(make-Name (gen-cont-arg) '(cont)))
(define (cont-var? name)
(and (Name? name) (memq 'cont (Name-context name))))
(define (make-cont-args n)
(let loop ((i 0)
(vars '()))
(cond ((= i n) vars)
(else (loop (+ i 1) (cons (make-new-cont-arg) vars))))))
(define (cont-folder f i j k)
(cond [(or (null? k) (null? j)) i]
[else (cont-folder f (f i (car j) (car k)) (cdr j) (cdr k))]))
(define (apply-cont k . e)
(cond [(eq? k init-cont) (car e)]
[else (let* ([new (make-new-cont-var)]
[var-new (make-E (Var new))]
[newk (cond [(Name? k)
(make-E (Var k))]
[else k])])
(match newk
[(E: (? Lam?))
(let ([d (make-Define new newk)])
(set-Name-binder! new d)
(make-E (Let
(list d)
(make-E (App var-new e)))))]
[else (make-E (App newk e))]))]))
(define (add-cont k args)
(cond [(eq? k init-cont) (cons (make-E (Var init-cont-name)) args)]
[else (cons (if (Name? k) (make-E (Var k)) k) args)]))
(define (filter-false l)
(filter (lambda (x) (not (eq? x #f))) l))
(define (restrict proc l)
(filter (lambda (l) (not (proc l))) l))
(define (make-continuation var body)
(let ([cont (make-E (Lam (list var) body))])
(set-Name-binder! var cont)
(set-E-cont?! cont #t)
cont))
(define (cps exp k)
(match exp
[(Define x e)
(set-Name-binder! x #f)
(make-Define x (cps e k))]
[(or (E: (? Const?)) (E: (? Var?)))
(apply-cont k exp)]
[(E: (Lam: args body))
(let* ([new-k (make-new-cont-var)]
[args (cons new-k args)]
[newBody (cps body new-k)]
[newLam (make-E (Lam args newBody))])
(for-each (lambda (n) (set-Name-binder! n newLam)) args)
(apply-cont k newLam))]
[(E: (Vlam: args rest body))
(let* ([new-k (make-new-cont-var)]
[args (cons new-k args)]
[newLam (make-E (Vlam args
rest
(cps body new-k)))])
(for-each (lambda (n) (set-Name-binder! n newLam)) args)
(set-Name-binder! rest newLam)
(apply-cont k newLam))]
[(E: (App e0 args))
(let ([folder (lambda (init args cont-vars)
(cont-folder
(lambda (exp j v)
(cps j (make-continuation v exp)))
init
args
cont-vars))]
[simple-cps (lambda (args)
(mapLR (match-lambda
[(and lam (E: (Lam: names body)))
(let ([new-k (make-new-cont-arg)])
(set-Name-binder! new-k lam)
(cps lam new-k))]
[e e])
args))]
[contName->var (lambda (k)
(if (Name? k)
(make-E (Var k))
k))])
(cond [(trivial? exp) (apply-cont k exp)]
[else
(match e0
[(E: (Var x))
(=> cont)
(if (Name-primitive? x)
(let ([cont-vars (make-cont-args (length args))])
(folder
(apply-cont k (make-E
(App
e0
(map (lambda (n)
(make-E (Var n)))
cont-vars))))
args
cont-vars))
(cont))]
[_ (let* ([terms (cons e0 args)]
[argList
(mapLR (lambda (arg)
(if (trivial? arg)
arg
(make-new-cont-arg)))
terms)]
[cont-vars
(for/fold ([acc '()]) ([arg (in-list argList)]
#:when (cont-var? arg))
(cons arg acc))])
(cond [(null? cont-vars)
(match e0
[(E: (Lam: formals body))
(make-E (App
(make-E (Lam
formals
(cps body k))))
(simple-cps args))]
[(E: (Vlam: formals rest body))
(make-E (App (make-E (Vlam
formals
rest
(cps body k)))
(simple-cps args)))]
[else
(make-E (App
e0
(cons (contName->var k)
(simple-cps args))))])]
[else (let ([argList (mapLR contName->var argList)])
(cont-folder
(lambda (e j v)
(cps j (make-continuation v e)))
(make-E (App
(car argList)
(cons (contName->var k)
(cdr argList))))
(restrict trivial? (cons e0 args))
(reverse cont-vars)))]))])]))]
[(E: (If pred then els))
(let* ([v (make-new-cont-arg)]
[newk (make-new-cont-arg)]
[var (lambda () (make-E (Var newk)))]
[b (make-Define newk (if (Name? k) (make-E (Var k)) k))])
(set-Name-binder! newk b)
(make-E (Let
(list b)
(if (trivial? pred)
(make-E (If pred
(cps then (var))
(cps els (var))))
(cps pred (make-continuation
v
(make-E (If (make-E (Var v))
(cps then (var))
(cps els (var))))))))))]
[(E: (Let bindings body))
(let* ([names (mapLR (match-lambda
[(Define name e) name])
bindings)]
[args (mapLR (match-lambda
[(Define name e) e])
bindings)]
[lam (make-E (Lam names body))])
(for-each (lambda (n) (set-Name-binder! n lam)) names)
(cps (make-E (App lam args)) k))]
[(E: (Letr bindings body))
(let* ([defs (mapLR (lambda (b) (cps b init-cont)) bindings)])
(make-components! (filter Define? defs))
(make-E (Letr defs (cps body k))))]
[(E: (Set! x e))
(let ([v (make-new-cont-arg)])
(cps e (make-continuation
v
(apply-cont k (make-E (Set! x (make-E (Var v))))))))]
[(E: (Begin exps))
(let ([names (make-cont-args (length exps))]
[defs (filter Define? exps)])
(cont-folder (lambda (e j v)
(cps j (make-continuation v e)))
(apply-cont k (make-E (Var (car names))))
(reverse exps)
names))]
[(E: (And exps))
(let* ([rexps (reverse exps)]
[new
(for/fold ([acc (car rexps)])
([e (in-list (cdr rexps))])
(make-E (If e acc (make-E (Const #f)))))])
(cps new k))]
[(E: (Or exps))
(let* ([rexps (reverse exps)]
[new
(for/fold ([acc (car rexps)])
([e (in-list (cdr rexps))])
(define new (make-new-cont-var))
(make-E (Let (list (make-Define new e))
(make-E (If (make-E (Var new))
(make-E (Var new))
acc)))))])
(cps new k))]
[else exp]))
(define (cps-tree)
(match tree
[(E: (Letr bindings body))
(let* ([new-exps (mapLR (lambda (b) (cps b init-cont)) bindings)]
[real-defs (cons init-cont-def (filter Define? new-exps))]
[new-tree (make-E (Letr (cons init-cont-def new-exps) body))])
(make-components! real-defs)
(set-tree! new-tree))]))