fix-letrec.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ;;; transformation of letrec into simpler forms
  2. ;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. (define-module (language tree-il fix-letrec)
  17. #:use-module (system base syntax)
  18. #:use-module (srfi srfi-1)
  19. #:use-module (srfi srfi-11)
  20. #:use-module (language tree-il)
  21. #:use-module (language tree-il primitives)
  22. #:export (fix-letrec!))
  23. ;; For a detailed discussion, see "Fixing Letrec: A Faithful Yet
  24. ;; Efficient Implementation of Scheme's Recursive Binding Construct", by
  25. ;; Oscar Waddell, Dipanwita Sarkar, and R. Kent Dybvig.
  26. (define fix-fold
  27. (make-tree-il-folder unref ref set simple lambda complex))
  28. (define (simple-expression? x bound-vars simple-primitive?)
  29. (record-case x
  30. ((<void>) #t)
  31. ((<const>) #t)
  32. ((<lexical-ref> gensym)
  33. (not (memq gensym bound-vars)))
  34. ((<conditional> test consequent alternate)
  35. (and (simple-expression? test bound-vars simple-primitive?)
  36. (simple-expression? consequent bound-vars simple-primitive?)
  37. (simple-expression? alternate bound-vars simple-primitive?)))
  38. ((<sequence> exps)
  39. (and-map (lambda (x) (simple-expression? x bound-vars simple-primitive?))
  40. exps))
  41. ((<application> proc args)
  42. (and (primitive-ref? proc)
  43. (simple-primitive? (primitive-ref-name proc))
  44. ;; FIXME: check arity?
  45. (and-map (lambda (x)
  46. (simple-expression? x bound-vars simple-primitive?))
  47. args)))
  48. (else #f)))
  49. (define (partition-vars x)
  50. (let-values
  51. (((unref ref set simple lambda* complex)
  52. (fix-fold x
  53. (lambda (x unref ref set simple lambda* complex)
  54. (record-case x
  55. ((<lexical-ref> gensym)
  56. (values (delq gensym unref)
  57. (lset-adjoin eq? ref gensym)
  58. set
  59. simple
  60. lambda*
  61. complex))
  62. ((<lexical-set> gensym)
  63. (values unref
  64. ref
  65. (lset-adjoin eq? set gensym)
  66. simple
  67. lambda*
  68. complex))
  69. ((<letrec> gensyms)
  70. (values (append gensyms unref)
  71. ref
  72. set
  73. simple
  74. lambda*
  75. complex))
  76. ((<let> gensyms)
  77. (values (append gensyms unref)
  78. ref
  79. set
  80. simple
  81. lambda*
  82. complex))
  83. (else
  84. (values unref ref set simple lambda* complex))))
  85. (lambda (x unref ref set simple lambda* complex)
  86. (record-case x
  87. ((<letrec> in-order? (orig-gensyms gensyms) vals)
  88. (let lp ((gensyms orig-gensyms) (vals vals)
  89. (s '()) (l '()) (c '()))
  90. (cond
  91. ((null? gensyms)
  92. ;; Unreferenced complex vars are still
  93. ;; complex for letrec*. We need to update
  94. ;; our algorithm to "Fixing letrec reloaded"
  95. ;; to fix this.
  96. (values (if in-order?
  97. (lset-difference eq? unref c)
  98. unref)
  99. ref
  100. set
  101. (append s simple)
  102. (append l lambda*)
  103. (append c complex)))
  104. ((memq (car gensyms) unref)
  105. ;; See above note about unref and letrec*.
  106. (if (and in-order?
  107. (not (lambda? (car vals)))
  108. (not (simple-expression?
  109. (car vals) orig-gensyms
  110. effect+exception-free-primitive?)))
  111. (lp (cdr gensyms) (cdr vals)
  112. s l (cons (car gensyms) c))
  113. (lp (cdr gensyms) (cdr vals)
  114. s l c)))
  115. ((memq (car gensyms) set)
  116. (lp (cdr gensyms) (cdr vals)
  117. s l (cons (car gensyms) c)))
  118. ((lambda? (car vals))
  119. (lp (cdr gensyms) (cdr vals)
  120. s (cons (car gensyms) l) c))
  121. ((simple-expression?
  122. (car vals) orig-gensyms
  123. (if in-order?
  124. effect+exception-free-primitive?
  125. effect-free-primitive?))
  126. ;; For letrec*, we can't consider e.g. `car' to be
  127. ;; "simple", as it could raise an exception. Hence
  128. ;; effect+exception-free-primitive? above.
  129. (lp (cdr gensyms) (cdr vals)
  130. (cons (car gensyms) s) l c))
  131. (else
  132. (lp (cdr gensyms) (cdr vals)
  133. s l (cons (car gensyms) c))))))
  134. ((<let> (orig-gensyms gensyms) vals)
  135. ;; The point is to compile let-bound lambdas as
  136. ;; efficiently as we do letrec-bound lambdas, so
  137. ;; we use the same algorithm for analyzing the
  138. ;; gensyms. There is no problem recursing into the
  139. ;; bindings after the let, because all variables
  140. ;; have been renamed.
  141. (let lp ((gensyms orig-gensyms) (vals vals)
  142. (s '()) (l '()) (c '()))
  143. (cond
  144. ((null? gensyms)
  145. (values unref
  146. ref
  147. set
  148. (append s simple)
  149. (append l lambda*)
  150. (append c complex)))
  151. ((memq (car gensyms) unref)
  152. (lp (cdr gensyms) (cdr vals)
  153. s l c))
  154. ((memq (car gensyms) set)
  155. (lp (cdr gensyms) (cdr vals)
  156. s l (cons (car gensyms) c)))
  157. ((and (lambda? (car vals))
  158. (not (memq (car gensyms) set)))
  159. (lp (cdr gensyms) (cdr vals)
  160. s (cons (car gensyms) l) c))
  161. ;; There is no difference between simple and
  162. ;; complex, for the purposes of let. Just lump
  163. ;; them all into complex.
  164. (else
  165. (lp (cdr gensyms) (cdr vals)
  166. s l (cons (car gensyms) c))))))
  167. (else
  168. (values unref ref set simple lambda* complex))))
  169. '()
  170. '()
  171. '()
  172. '()
  173. '()
  174. '())))
  175. (values unref simple lambda* complex)))
  176. (define (fix-letrec! x)
  177. (let-values (((unref simple lambda* complex) (partition-vars x)))
  178. (post-order!
  179. (lambda (x)
  180. (record-case x
  181. ;; Sets to unreferenced variables may be replaced by their
  182. ;; expression, called for effect.
  183. ((<lexical-set> gensym exp)
  184. (if (memq gensym unref)
  185. (make-sequence #f (list exp (make-void #f)))
  186. x))
  187. ((<letrec> src in-order? names gensyms vals body)
  188. (let ((binds (map list gensyms names vals)))
  189. ;; The bindings returned by this function need to appear in the same
  190. ;; order that they appear in the letrec.
  191. (define (lookup set)
  192. (let lp ((binds binds))
  193. (cond
  194. ((null? binds) '())
  195. ((memq (caar binds) set)
  196. (cons (car binds) (lp (cdr binds))))
  197. (else (lp (cdr binds))))))
  198. (let ((u (lookup unref))
  199. (s (lookup simple))
  200. (l (lookup lambda*))
  201. (c (lookup complex)))
  202. ;; Bind "simple" bindings, and locations for complex
  203. ;; bindings.
  204. (make-let
  205. src
  206. (append (map cadr s) (map cadr c))
  207. (append (map car s) (map car c))
  208. (append (map caddr s) (map (lambda (x) (make-void #f)) c))
  209. ;; Bind lambdas using the fixpoint operator.
  210. (make-fix
  211. src (map cadr l) (map car l) (map caddr l)
  212. (make-sequence
  213. src
  214. (append
  215. ;; The right-hand-sides of the unreferenced
  216. ;; bindings, for effect.
  217. (map caddr u)
  218. (cond
  219. ((null? c)
  220. ;; No complex bindings, just emit the body.
  221. (list body))
  222. (in-order?
  223. ;; For letrec*, assign complex bindings in order, then the
  224. ;; body.
  225. (append
  226. (map (lambda (c)
  227. (make-lexical-set #f (cadr c) (car c)
  228. (caddr c)))
  229. c)
  230. (list body)))
  231. (else
  232. ;; Otherwise for plain letrec, evaluate the the "complex"
  233. ;; bindings, in a `let' to indicate that order doesn't
  234. ;; matter, and bind to their variables.
  235. (list
  236. (let ((tmps (map (lambda (x) (gensym)) c)))
  237. (make-let
  238. #f (map cadr c) tmps (map caddr c)
  239. (make-sequence
  240. #f
  241. (map (lambda (x tmp)
  242. (make-lexical-set
  243. #f (cadr x) (car x)
  244. (make-lexical-ref #f (cadr x) tmp)))
  245. c tmps))))
  246. body))))))))))
  247. ((<let> src names gensyms vals body)
  248. (let ((binds (map list gensyms names vals)))
  249. (define (lookup set)
  250. (map (lambda (v) (assq v binds))
  251. (lset-intersection eq? gensyms set)))
  252. (let ((u (lookup unref))
  253. (l (lookup lambda*))
  254. (c (lookup complex)))
  255. (make-sequence
  256. src
  257. (append
  258. ;; unreferenced bindings, called for effect.
  259. (map caddr u)
  260. (list
  261. ;; unassigned lambdas use fix.
  262. (make-fix src (map cadr l) (map car l) (map caddr l)
  263. ;; and the "complex" bindings.
  264. (make-let src (map cadr c) (map car c) (map caddr c)
  265. body))))))))
  266. (else x)))
  267. x)))
  268. ;;; Local Variables:
  269. ;;; eval: (put 'record-case 'scheme-indent-function 1)
  270. ;;; End: