1 ;;; transformation of letrec into simpler forms
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (language tree-il fix-letrec)
20 #:use-module (system base syntax)
21 #:use-module (srfi srfi-1)
22 #:use-module (srfi srfi-11)
23 #:use-module (language tree-il)
24 #:use-module (language tree-il primitives)
25 #:export (fix-letrec!))
27 ;; For a detailed discussion, see "Fixing Letrec: A Faithful Yet
28 ;; Efficient Implementation of Scheme’s Recursive Binding Construct", by
29 ;; Oscar Waddell, Dipanwita Sarkar, and R. Kent Dybvig.
32 (make-tree-il-folder unref ref set simple lambda complex))
34 (define (simple-expression? x bound-vars)
38 ((<lexical-ref> gensym)
39 (not (memq gensym bound-vars)))
40 ((<conditional> test consequent alternate)
41 (and (simple-expression? test bound-vars)
42 (simple-expression? consequent bound-vars)
43 (simple-expression? alternate bound-vars)))
45 (and-map (lambda (x) (simple-expression? x bound-vars))
47 ((<application> proc args)
48 (and (primitive-ref? proc)
49 (effect-free-primitive? (primitive-ref-name proc))
50 ;; FIXME: check arity?
51 (and-map (lambda (x) (simple-expression? x bound-vars))
55 (define (partition-vars x)
57 (((unref ref set simple lambda* complex)
59 (lambda (x unref ref set simple lambda* complex)
61 ((<lexical-ref> gensym)
62 (values (delq gensym unref)
63 (lset-adjoin eq? ref gensym)
68 ((<lexical-set> gensym)
71 (lset-adjoin eq? set gensym)
76 (values (append gensyms unref)
83 (values (append gensyms unref)
90 (values unref ref set simple lambda* complex))))
91 (lambda (x unref ref set simple lambda* complex)
93 ((<letrec> (orig-gensyms gensyms) vals)
94 (let lp ((gensyms orig-gensyms) (vals vals)
95 (s '()) (l '()) (c '()))
104 ((memq (car gensyms) unref)
105 (lp (cdr gensyms) (cdr vals)
107 ((memq (car gensyms) set)
108 (lp (cdr gensyms) (cdr vals)
109 s l (cons (car gensyms) c)))
110 ((lambda? (car vals))
111 (lp (cdr gensyms) (cdr vals)
112 s (cons (car gensyms) l) c))
113 ((simple-expression? (car vals) orig-gensyms)
114 (lp (cdr gensyms) (cdr vals)
115 (cons (car gensyms) s) l c))
117 (lp (cdr gensyms) (cdr vals)
118 s l (cons (car gensyms) c))))))
119 ((<let> (orig-gensyms gensyms) vals)
120 ;; The point is to compile let-bound lambdas as
121 ;; efficiently as we do letrec-bound lambdas, so
122 ;; we use the same algorithm for analyzing the
123 ;; gensyms. There is no problem recursing into the
124 ;; bindings after the let, because all variables
125 ;; have been renamed.
126 (let lp ((gensyms orig-gensyms) (vals vals)
127 (s '()) (l '()) (c '()))
136 ((memq (car gensyms) unref)
137 (lp (cdr gensyms) (cdr vals)
139 ((memq (car gensyms) set)
140 (lp (cdr gensyms) (cdr vals)
141 s l (cons (car gensyms) c)))
142 ((and (lambda? (car vals))
143 (not (memq (car gensyms) set)))
144 (lp (cdr gensyms) (cdr vals)
145 s (cons (car gensyms) l) c))
146 ;; There is no difference between simple and
147 ;; complex, for the purposes of let. Just lump
148 ;; them all into complex.
150 (lp (cdr gensyms) (cdr vals)
151 s l (cons (car gensyms) c))))))
153 (values unref ref set simple lambda* complex))))
160 (values unref simple lambda* complex)))
162 (define (fix-letrec! x)
163 (let-values (((unref simple lambda* complex) (partition-vars x)))
168 ;; Sets to unreferenced variables may be replaced by their
169 ;; expression, called for effect.
170 ((<lexical-set> gensym exp)
171 (if (memq gensym unref)
172 (make-sequence #f (list exp (make-void #f)))
175 ((<letrec> src names gensyms vals body)
176 (let ((binds (map list gensyms names vals)))
178 (map (lambda (v) (assq v binds))
179 (lset-intersection eq? gensyms set)))
180 (let ((u (lookup unref))
183 (c (lookup complex)))
184 ;; Bind "simple" bindings, and locations for complex
188 (append (map cadr s) (map cadr c))
189 (append (map car s) (map car c))
190 (append (map caddr s) (map (lambda (x) (make-void #f)) c))
191 ;; Bind lambdas using the fixpoint operator.
193 src (map cadr l) (map car l) (map caddr l)
197 ;; The right-hand-sides of the unreferenced
198 ;; bindings, for effect.
201 ;; No complex bindings, just emit the body.
204 ;; Evaluate the the "complex" bindings, in a `let' to
205 ;; indicate that order doesn't matter, and bind to
207 (let ((tmps (map (lambda (x) (gensym)) c)))
209 #f (map cadr c) tmps (map caddr c)
215 (make-lexical-ref #f (cadr x) tmp)))
217 ;; Finally, the body.
220 ((<let> src names gensyms vals body)
221 (let ((binds (map list gensyms names vals)))
223 (map (lambda (v) (assq v binds))
224 (lset-intersection eq? gensyms set)))
225 (let ((u (lookup unref))
227 (c (lookup complex)))
231 ;; unreferenced bindings, called for effect.
234 ;; unassigned lambdas use fix.
235 (make-fix src (map cadr l) (map car l) (map caddr l)
236 ;; and the "complex" bindings.
237 (make-let src (map cadr c) (map car c) (map caddr c)