fix-letrec.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ;;; transformation of letrec into simpler forms
  2. ;; Copyright (C) 2009, 2010 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 vars are still complex for letrec*.
  93. ;; We need to update our algorithm to "Fixing letrec
  94. ;; reloaded" to fix this.
  95. (values (if in-order?
  96. (lset-difference eq? unref c)
  97. unref)
  98. ref
  99. set
  100. (append s simple)
  101. (append l lambda*)
  102. (append c complex)))
  103. ((memq (car gensyms) unref)
  104. ;; See above note about unref and letrec*.
  105. (if in-order?
  106. (lp (cdr gensyms) (cdr vals)
  107. s l (cons (car gensyms) c))
  108. (lp (cdr gensyms) (cdr vals)
  109. s l c)))
  110. ((memq (car gensyms) set)
  111. (lp (cdr gensyms) (cdr vals)
  112. s l (cons (car gensyms) c)))
  113. ((lambda? (car vals))
  114. (lp (cdr gensyms) (cdr vals)
  115. s (cons (car gensyms) l) c))
  116. ((simple-expression?
  117. (car vals) orig-gensyms
  118. (if in-order?
  119. effect+exception-free-primitive?
  120. effect-free-primitive?))
  121. ;; For letrec*, we can't consider e.g. `car' to be
  122. ;; "simple", as it could raise an exception. Hence
  123. ;; effect+exception-free-primitive? above.
  124. (lp (cdr gensyms) (cdr vals)
  125. (cons (car gensyms) s) l c))
  126. (else
  127. (lp (cdr gensyms) (cdr vals)
  128. s l (cons (car gensyms) c))))))
  129. ((<let> (orig-gensyms gensyms) vals)
  130. ;; The point is to compile let-bound lambdas as
  131. ;; efficiently as we do letrec-bound lambdas, so
  132. ;; we use the same algorithm for analyzing the
  133. ;; gensyms. There is no problem recursing into the
  134. ;; bindings after the let, because all variables
  135. ;; have been renamed.
  136. (let lp ((gensyms orig-gensyms) (vals vals)
  137. (s '()) (l '()) (c '()))
  138. (cond
  139. ((null? gensyms)
  140. (values unref
  141. ref
  142. set
  143. (append s simple)
  144. (append l lambda*)
  145. (append c complex)))
  146. ((memq (car gensyms) unref)
  147. (lp (cdr gensyms) (cdr vals)
  148. s l c))
  149. ((memq (car gensyms) set)
  150. (lp (cdr gensyms) (cdr vals)
  151. s l (cons (car gensyms) c)))
  152. ((and (lambda? (car vals))
  153. (not (memq (car gensyms) set)))
  154. (lp (cdr gensyms) (cdr vals)
  155. s (cons (car gensyms) l) c))
  156. ;; There is no difference between simple and
  157. ;; complex, for the purposes of let. Just lump
  158. ;; them all into complex.
  159. (else
  160. (lp (cdr gensyms) (cdr vals)
  161. s l (cons (car gensyms) c))))))
  162. (else
  163. (values unref ref set simple lambda* complex))))
  164. '()
  165. '()
  166. '()
  167. '()
  168. '()
  169. '())))
  170. (values unref simple lambda* complex)))
  171. (define (fix-letrec! x)
  172. (let-values (((unref simple lambda* complex) (partition-vars x)))
  173. (post-order!
  174. (lambda (x)
  175. (record-case x
  176. ;; Sets to unreferenced variables may be replaced by their
  177. ;; expression, called for effect.
  178. ((<lexical-set> gensym exp)
  179. (if (memq gensym unref)
  180. (make-sequence #f (list exp (make-void #f)))
  181. x))
  182. ((<letrec> src in-order? names gensyms vals body)
  183. (let ((binds (map list gensyms names vals)))
  184. ;; The bindings returned by this function need to appear in the same
  185. ;; order that they appear in the letrec.
  186. (define (lookup set)
  187. (let lp ((binds binds))
  188. (cond
  189. ((null? binds) '())
  190. ((memq (caar binds) set)
  191. (cons (car binds) (lp (cdr binds))))
  192. (else (lp (cdr binds))))))
  193. (let ((u (lookup unref))
  194. (s (lookup simple))
  195. (l (lookup lambda*))
  196. (c (lookup complex)))
  197. ;; Bind "simple" bindings, and locations for complex
  198. ;; bindings.
  199. (make-let
  200. src
  201. (append (map cadr s) (map cadr c))
  202. (append (map car s) (map car c))
  203. (append (map caddr s) (map (lambda (x) (make-void #f)) c))
  204. ;; Bind lambdas using the fixpoint operator.
  205. (make-fix
  206. src (map cadr l) (map car l) (map caddr l)
  207. (make-sequence
  208. src
  209. (append
  210. ;; The right-hand-sides of the unreferenced
  211. ;; bindings, for effect.
  212. (map caddr u)
  213. (cond
  214. ((null? c)
  215. ;; No complex bindings, just emit the body.
  216. (list body))
  217. (in-order?
  218. ;; For letrec*, assign complex bindings in order, then the
  219. ;; body.
  220. (append
  221. (map (lambda (c)
  222. (make-lexical-set #f (cadr c) (car c) (caddr c)))
  223. c)
  224. (list body)))
  225. (else
  226. ;; Otherwise for plain letrec, evaluate the the "complex"
  227. ;; bindings, in a `let' to indicate that order doesn't
  228. ;; matter, and bind to their variables.
  229. (list
  230. (let ((tmps (map (lambda (x) (gensym)) c)))
  231. (make-let
  232. #f (map cadr c) tmps (map caddr c)
  233. (make-sequence
  234. #f
  235. (map (lambda (x tmp)
  236. (make-lexical-set
  237. #f (cadr x) (car x)
  238. (make-lexical-ref #f (cadr x) tmp)))
  239. c tmps))))
  240. body))))))))))
  241. ((<let> src names gensyms vals body)
  242. (let ((binds (map list gensyms names vals)))
  243. (define (lookup set)
  244. (map (lambda (v) (assq v binds))
  245. (lset-intersection eq? gensyms set)))
  246. (let ((u (lookup unref))
  247. (l (lookup lambda*))
  248. (c (lookup complex)))
  249. (make-sequence
  250. src
  251. (append
  252. ;; unreferenced bindings, called for effect.
  253. (map caddr u)
  254. (list
  255. ;; unassigned lambdas use fix.
  256. (make-fix src (map cadr l) (map car l) (map caddr l)
  257. ;; and the "complex" bindings.
  258. (make-let src (map cadr c) (map car c) (map caddr c)
  259. body))))))))
  260. (else x)))
  261. x)))