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
|
;;; Continuation-passing style (CPS) intermediate language (IL)
;; Copyright (C) 2013, 2014, 2015 Free Software Foundation, Inc.
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;; Commentary:
;;;
;;; The fundamental lambda calculus reductions, like beta and eta
;;; reduction and so on. Pretty lame currently.
;;;
;;; Code:
(define-module (language cps simplify)
#:use-module (ice-9 match)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-11)
#:use-module (srfi srfi-26)
#:use-module (language cps)
#:use-module (language cps dfg)
#:use-module (language cps renumber)
#:export (simplify))
(define (compute-eta-reductions fun)
(let ((table (make-hash-table)))
(define (visit-cont cont)
(match cont
(($ $cont sym ($ $kargs names syms body))
(visit-term body sym syms))
(($ $cont sym ($ $kfun src meta self tail clause))
(when clause (visit-cont clause)))
(($ $cont sym ($ $kclause arity body alternate))
(visit-cont body)
(when alternate (visit-cont alternate)))
(($ $cont sym _) #f)))
(define (visit-term term term-k term-args)
(match term
(($ $letk conts body)
(for-each visit-cont conts)
(visit-term body term-k term-args))
(($ $continue k src ($ $values args))
(when (and (equal? term-args args) (not (eq? k term-k)))
(hashq-set! table term-k k)))
(($ $continue k src (and fun ($ $fun)))
(visit-fun fun))
(($ $continue k src ($ $rec names syms funs))
(for-each visit-fun funs))
(($ $continue k src _)
#f)))
(define (visit-fun fun)
(match fun
(($ $fun body)
(visit-cont body))))
(visit-cont fun)
table))
(define (eta-reduce fun)
(let ((table (compute-eta-reductions fun))
(dfg (compute-dfg fun)))
(define (reduce* k scope values?)
(match (hashq-ref table k)
(#f k)
(k*
(if (and (continuation-bound-in? k* scope dfg)
(or values?
(match (lookup-cont k* dfg)
(($ $kargs) #t)
(_ #f))))
(reduce* k* scope values?)
k))))
(define (reduce k scope)
(reduce* k scope #f))
(define (reduce-values k scope)
(reduce* k scope #t))
(define (reduce-const k src scope const)
(let lp ((k k) (seen '()) (const const))
(match (lookup-cont k dfg)
(($ $kargs (_) (arg) term)
(match (find-call term)
(($ $continue k* src* ($ $values (arg*)))
(and (eqv? arg arg*)
(not (memq k* seen))
(lp k* (cons k seen) const)))
(($ $continue k* src* ($ $primcall 'not (arg*)))
(and (eqv? arg arg*)
(not (memq k* seen))
(lp k* (cons k seen) (not const))))
(($ $continue k* src* ($ $branch kt ($ $values (arg*))))
(and (eqv? arg arg*)
(let ((k* (if const kt k*)))
(and (continuation-bound-in? k* scope dfg)
(build-cps-term
($continue k* src ($values ())))))))
(_
(and (continuation-bound-in? k scope dfg)
(build-cps-term
($continue k src ($const const)))))))
(_ #f))))
(define (visit-cont cont scope)
(rewrite-cps-cont cont
(($ $cont sym ($ $kargs names syms body))
(sym ($kargs names syms ,(visit-term body sym))))
(($ $cont sym ($ $kfun src meta self tail clause))
(sym ($kfun src meta self ,tail
,(and clause (visit-cont clause sym)))))
(($ $cont sym ($ $kclause arity body alternate))
(sym ($kclause ,arity ,(visit-cont body sym)
,(and alternate (visit-cont alternate sym)))))
(($ $cont sym ($ $kreceive ($ $arity req () rest () #f) kargs))
(sym ($kreceive req rest (reduce kargs scope))))))
(define (visit-term term scope)
(rewrite-cps-term term
(($ $letk conts body)
($letk ,(map (cut visit-cont <> scope) conts)
,(visit-term body scope)))
(($ $continue k src ($ $values args))
($continue (reduce-values k scope) src ($values args)))
(($ $continue k src (and fun ($ $fun)))
($continue (reduce k scope) src ,(visit-fun fun)))
(($ $continue k src ($ $rec names syms funs))
($continue k src ($rec names syms (map visit-fun funs))))
(($ $continue k src ($ $const const))
,(let ((k (reduce k scope)))
(or (reduce-const k src scope const)
(build-cps-term ($continue k src ($const const))))))
(($ $continue k src exp)
($continue (reduce k scope) src ,exp))))
(define (visit-fun fun)
(rewrite-cps-exp fun
(($ $fun body)
($fun ,(visit-cont body #f)))))
(visit-cont fun #f)))
(define (compute-beta-reductions fun)
;; A continuation's body can be inlined in place of a $values
;; expression if the continuation is a $kargs. It should only be
;; inlined if it is used only once, and not recursively.
(let ((var-table (make-hash-table))
(k-table (make-hash-table))
(dfg (compute-dfg fun)))
(define (visit-cont cont)
(match cont
(($ $cont sym ($ $kargs names syms body))
(visit-term body))
(($ $cont sym ($ $kfun src meta self tail clause))
(when clause (visit-cont clause)))
(($ $cont sym ($ $kclause arity body alternate))
(visit-cont body)
(when alternate (visit-cont alternate)))
(($ $cont sym (or ($ $ktail) ($ $kreceive)))
#f)))
(define (visit-term term)
(match term
(($ $letk conts body)
(for-each visit-cont conts)
(visit-term body))
(($ $continue k src ($ $values args))
(match (lookup-cont k dfg)
(($ $kargs names syms body)
(match (lookup-predecessors k dfg)
((_)
;; There is only one use, and it is this use. We assume
;; it's not recursive, as there would to be some other
;; use for control flow to reach this loop. Store the k
;; -> body mapping in the table. Also store the
;; substitutions for the variables bound by the inlined
;; continuation.
(for-each (cut hashq-set! var-table <> <>) syms args)
(hashq-set! k-table k body))
(_ #f)))
(_ #f)))
(($ $continue k src (and fun ($ $fun)))
(visit-fun fun))
(($ $continue k src ($ $rec names syms funs))
(for-each visit-fun funs))
(($ $continue k src _)
#f)))
(define (visit-fun fun)
(match fun
(($ $fun body)
(visit-cont body))))
(visit-cont fun)
(values var-table k-table)))
(define (beta-reduce fun)
(let-values (((var-table k-table) (compute-beta-reductions fun)))
(define (subst var)
(cond ((hashq-ref var-table var) => subst)
(else var)))
(define (must-visit-cont cont)
(or (visit-cont cont)
(error "continuation must not be inlined" cont)))
(define (visit-cont cont)
(match cont
(($ $cont sym cont)
(and (not (hashq-ref k-table sym))
(rewrite-cps-cont cont
(($ $kargs names syms body)
(sym ($kargs names syms ,(visit-term body))))
(($ $kfun src meta self tail clause)
(sym ($kfun src meta self ,tail
,(and clause (must-visit-cont clause)))))
(($ $kclause arity body alternate)
(sym ($kclause ,arity ,(must-visit-cont body)
,(and alternate (must-visit-cont alternate)))))
(($ $kreceive)
(sym ,cont)))))))
(define (visit-term term)
(match term
(($ $letk conts body)
(match (filter-map visit-cont conts)
(() (visit-term body))
(conts (build-cps-term
($letk ,conts ,(visit-term body))))))
(($ $continue k src exp)
(cond
((hashq-ref k-table k) => visit-term)
(else
(build-cps-term ($continue k src ,(visit-exp exp))))))))
(define (visit-exp exp)
(match exp
((or ($ $const) ($ $prim)) exp)
(($ $fun) (visit-fun exp))
(($ $rec names syms funs)
(build-cps-exp ($rec names (map subst syms) (map visit-fun funs))))
(($ $call proc args)
(let ((args (map subst args)))
(build-cps-exp ($call (subst proc) args))))
(($ $callk k proc args)
(let ((args (map subst args)))
(build-cps-exp ($callk k (subst proc) args))))
(($ $primcall name args)
(let ((args (map subst args)))
(build-cps-exp ($primcall name args))))
(($ $values args)
(let ((args (map subst args)))
(build-cps-exp ($values args))))
(($ $branch kt exp)
(build-cps-exp ($branch kt ,(visit-exp exp))))
(($ $prompt escape? tag handler)
(build-cps-exp ($prompt escape? (subst tag) handler)))))
(define (visit-fun fun)
(rewrite-cps-exp fun
(($ $fun body)
($fun ,(must-visit-cont body)))))
(must-visit-cont fun)))
;; Rewrite the scope tree to reflect the dominator tree. Precondition:
;; the fun has been renumbered, its min-label is 0, and its labels are
;; packed.
(define (redominate fun)
(let* ((dfg (compute-dfg fun))
(idoms (compute-idoms dfg 0 (dfg-label-count dfg)))
(doms (compute-dom-edges idoms 0)))
(define (visit-fun-cont cont)
(rewrite-cps-cont cont
(($ $cont label ($ $kfun src meta self tail clause))
(label ($kfun src meta self ,tail
,(and clause (visit-fun-cont clause)))))
(($ $cont label ($ $kclause arity ($ $cont kbody body) alternate))
(label ($kclause ,arity ,(visit-cont kbody body)
,(and alternate (visit-fun-cont alternate)))))))
(define (visit-cont label cont)
(rewrite-cps-cont cont
(($ $kargs names vars body)
(label ($kargs names vars ,(visit-term body label))))
(_ (label ,cont))))
(define (visit-fun fun)
(rewrite-cps-exp fun
(($ $fun body)
($fun ,(visit-fun-cont body)))))
(define (visit-exp k src exp)
(rewrite-cps-term exp
(($ $fun body)
($continue k src ,(visit-fun exp)))
(($ $rec names syms funs)
($continue k src ($rec names syms (map visit-fun funs))))
(_
($continue k src ,exp))))
(define (visit-term term label)
(define (visit-dom-conts label)
(let ((cont (lookup-cont label dfg)))
(match cont
(($ $ktail) '())
(($ $kargs) (list (visit-cont label cont)))
(else
(cons (visit-cont label cont)
(visit-dom-conts* (vector-ref doms label)))))))
(define (visit-dom-conts* labels)
(match labels
(() '())
((label . labels)
(append (visit-dom-conts label)
(visit-dom-conts* labels)))))
(rewrite-cps-term term
(($ $letk conts body)
,(visit-term body label))
(($ $continue k src exp)
,(let ((conts (visit-dom-conts* (vector-ref doms label))))
(if (null? conts)
(visit-exp k src exp)
(build-cps-term
($letk ,conts ,(visit-exp k src exp))))))))
(visit-fun-cont fun)))
(define (simplify fun)
;; Renumbering prunes continuations that are made unreachable by
;; eta/beta reductions.
(redominate (renumber (eta-reduce (beta-reduce fun)))))
|