srfi-71.scm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. ; Reference implementation of SRFI-71 (generic part)
  2. ; Copyright (c) 2005 Sebastian Egner.
  3. ; Permission is hereby granted, free of charge, to any person
  4. ; obtaining a copy of this software and associated documentation files
  5. ; (the ``Software''), to deal in the Software without restriction,
  6. ; including without limitation the rights to use, copy, modify, merge,
  7. ; publish, distribute, sublicense, and/or sell copies of the Software,
  8. ; and to permit persons to whom the Software is furnished to do so,
  9. ; subject to the following conditions:
  10. ;
  11. ; The above copyright notice and this permission notice shall be
  12. ; included in all copies or substantial portions of the Software.
  13. ;
  14. ; THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  15. ; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. ; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. ; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. ; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. ; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. ; SOFTWARE.
  22. ; Sebastian.Egner@philips.com, 20-May-2005, PLT 208
  23. ; In order to avoid conflicts with the existing let etc.
  24. ; the macros defined here are called srfi-let etc.,
  25. ; and they are defined in terms of r5rs-let etc.
  26. ; It is up to the actual implementation to save let/*/rec
  27. ; in r5rs-let/*/rec first and redefine let/*/rec
  28. ; by srfi-let/*/rec then.
  29. ;
  30. ; There is also a srfi-letrec* being defined (in view of R6RS.)
  31. ;
  32. ; Macros used internally are named i:<something>.
  33. ;
  34. ; Abbreviations for macro arguments:
  35. ; bs - <binding spec>
  36. ; b - component of a binding spec (values, <variable>, or <expression>)
  37. ; v - <variable>
  38. ; vr - <variable> for rest list
  39. ; x - <expression>
  40. ; t - newly introduced temporary variable
  41. ; vx - (<variable> <expression>)
  42. ; rec - flag if letrec is produced (and not let)
  43. ; cwv - call-with-value skeleton of the form (x formals)
  44. ; (call-with-values (lambda () x) (lambda formals /payload/))
  45. ; where /payload/ is of the form (let (vx ...) body1 body ...).
  46. ;
  47. ; Remark (*):
  48. ; We bind the variables of a letrec to i:undefined since there is
  49. ; no portable (R5RS) way of binding a variable to a values that
  50. ; raises an error when read uninitialized.
  51. (define i:undefined 'undefined)
  52. (define-syntax srfi-letrec* ; -> srfi-letrec
  53. (syntax-rules ()
  54. ((srfi-letrec* () body1 body ...)
  55. (srfi-letrec () body1 body ...))
  56. ((srfi-letrec* (bs) body1 body ...)
  57. (srfi-letrec (bs) body1 body ...))
  58. ((srfi-letrec* (bs1 bs2 bs ...) body1 body ...)
  59. (srfi-letrec (bs1) (srfi-letrec* (bs2 bs ...) body1 body ...)))))
  60. (define-syntax srfi-letrec ; -> i:let
  61. (syntax-rules ()
  62. ((srfi-letrec ((b1 b2 b ...) ...) body1 body ...)
  63. (i:let "bs" #t () () (body1 body ...) ((b1 b2 b ...) ...)))))
  64. (define-syntax srfi-let* ; -> srfi-let
  65. (syntax-rules ()
  66. ((srfi-let* () body1 body ...)
  67. (srfi-let () body1 body ...))
  68. ((srfi-let* (bs) body1 body ...)
  69. (srfi-let (bs) body1 body ...))
  70. ((srfi-let* (bs1 bs2 bs ...) body1 body ...)
  71. (srfi-let (bs1) (srfi-let* (bs2 bs ...) body1 body ...)))))
  72. (define-syntax srfi-let ; -> i:let or i:named-let
  73. (syntax-rules ()
  74. ((srfi-let ((b1 b2 b ...) ...) body1 body ...)
  75. (i:let "bs" #f () () (body1 body ...) ((b1 b2 b ...) ...)))
  76. ((srfi-let tag ((b1 b2 b ...) ...) body1 body ...)
  77. (i:named-let tag () (body1 body ...) ((b1 b2 b ...) ...)))))
  78. (define-syntax i:let
  79. (syntax-rules (values)
  80. ; (i:let "bs" rec (cwv ...) (vx ...) body (bs ...))
  81. ; processes the binding specs bs ... by adding call-with-values
  82. ; skeletons to cwv ... and bindings to vx ..., and afterwards
  83. ; wrapping the skeletons around the payload (let (vx ...) . body).
  84. ; no more bs to process -> wrap call-with-values skeletons
  85. ((i:let "bs" rec (cwv ...) vxs body ())
  86. (i:let "wrap" rec vxs body cwv ...))
  87. ; recognize form1 without variable -> dummy binding for side-effects
  88. ((i:let "bs" rec cwvs (vx ...) body (((values) x) bs ...))
  89. (i:let "bs" rec cwvs (vx ... (dummy (begin x #f))) body (bs ...)))
  90. ; recognize form1 with single variable -> just extend vx ...
  91. ((i:let "bs" rec cwvs (vx ...) body (((values v) x) bs ...))
  92. (i:let "bs" rec cwvs (vx ... (v x)) body (bs ...)))
  93. ; recognize form1 without rest arg -> generate cwv
  94. ((i:let "bs" rec cwvs vxs body (((values v ...) x) bs ...))
  95. (i:let "form1" rec cwvs vxs body (bs ...) (x ()) (values v ...)))
  96. ; recognize form1 with rest arg -> generate cwv
  97. ((i:let "bs" rec cwvs vxs body (((values . vs) x) bs ...))
  98. (i:let "form1+" rec cwvs vxs body (bs ...) (x ()) (values . vs)))
  99. ; recognize form2 with single variable -> just extend vx ...
  100. ((i:let "bs" rec cwvs (vx ...) body ((v x) bs ...))
  101. (i:let "bs" rec cwvs (vx ... (v x)) body (bs ...)))
  102. ; recognize form2 with >=2 variables -> transform to form1
  103. ((i:let "bs" rec cwvs vxs body ((b1 b2 b3 b ...) bs ...))
  104. (i:let "form2" rec cwvs vxs body (bs ...) (b1 b2) (b3 b ...)))
  105. ; (i:let "form1" rec cwvs vxs body bss (x (t ...)) (values v1 v2 v ...))
  106. ; processes the variables in v1 v2 v ... adding them to (t ...)
  107. ; and producing a cwv when finished. There is not rest argument.
  108. ((i:let "form1" rec (cwv ...) vxs body bss (x ts) (values))
  109. (i:let "bs" rec (cwv ... (x ts)) vxs body bss))
  110. ((i:let "form1" rec cwvs (vx ...) body bss (x (t ...)) (values v1 v ...))
  111. (i:let "form1" rec cwvs (vx ... (v1 t1)) body bss (x (t ... t1)) (values v ...)))
  112. ; (i:let "form1+" rec cwvs vxs body bss (x (t ...)) (values v ... . vr))
  113. ; processes the variables in v ... . vr adding them to (t ...)
  114. ; and producing a cwv when finished. The rest arg is vr.
  115. ((i:let "form1+" rec cwvs (vx ...) body bss (x (t ...)) (values v1 v2 . vs))
  116. (i:let "form1+" rec cwvs (vx ... (v1 t1)) body bss (x (t ... t1)) (values v2 . vs)))
  117. ((i:let "form1+" rec (cwv ...) (vx ...) body bss (x (t ...)) (values v1 . vr))
  118. (i:let "bs" rec (cwv ... (x (t ... t1 . tr))) (vx ... (v1 t1) (vr tr)) body bss))
  119. ((i:let "form1+" rec (cwv ...) (vx ...) body bss (x ()) (values . vr))
  120. (i:let "bs" rec (cwv ... (x tr)) (vx ... (vr tr)) body bss))
  121. ; (i:let "form2" rec cwvs vxs body bss (v ...) (b ... x))
  122. ; processes the binding items (b ... x) from form2 as in
  123. ; (v ... b ... x) into ((values v ... b ...) x), i.e. form1.
  124. ; Then call "bs" recursively.
  125. ((i:let "form2" rec cwvs vxs body (bs ...) (v ...) (x))
  126. (i:let "bs" rec cwvs vxs body (((values v ...) x) bs ...)))
  127. ((i:let "form2" rec cwvs vxs body bss (v ...) (b1 b2 b ...))
  128. (i:let "form2" rec cwvs vxs body bss (v ... b1) (b2 b ...)))
  129. ; (i:let "wrap" rec ((v x) ...) (body ...) cwv ...)
  130. ; wraps cwv ... around the payload generating the actual code.
  131. ; For letrec this is of course different than for let.
  132. ((i:let "wrap" #f vxs body)
  133. (r5rs-let vxs . body))
  134. ((i:let "wrap" #f vxs body (x formals) cwv ...)
  135. (call-with-values
  136. (lambda () x)
  137. (lambda formals (i:let "wrap" #f vxs body cwv ...))))
  138. ((i:let "wrap" #t vxs body)
  139. (r5rs-letrec vxs . body))
  140. ((i:let "wrap" #t ((v t) ...) body cwv ...)
  141. (r5rs-let ((v i:undefined) ...) ; (*)
  142. (i:let "wraprec" ((v t) ...) body cwv ...)))
  143. ; (i:let "wraprec" ((v t) ...) body cwv ...)
  144. ; generate the inner code for a letrec. The variables v ...
  145. ; are the user-visible variables (bound outside), and t ...
  146. ; are the temporary variables bound by the cwv consumers.
  147. ((i:let "wraprec" ((v t) ...) (body ...))
  148. (begin (set! v t) ... (r5rs-let () body ...)))
  149. ((i:let "wraprec" vxs body (x formals) cwv ...)
  150. (call-with-values
  151. (lambda () x)
  152. (lambda formals (i:let "wraprec" vxs body cwv ...))))
  153. ))
  154. (define-syntax i:named-let
  155. (syntax-rules (values)
  156. ; (i:named-let tag (vx ...) body (bs ...))
  157. ; processes the binding specs bs ... by extracting the variable
  158. ; and expression, adding them to vx and turning the result into
  159. ; an ordinary named let.
  160. ((i:named-let tag vxs body ())
  161. (r5rs-let tag vxs . body))
  162. ((i:named-let tag (vx ...) body (((values v) x) bs ...))
  163. (i:named-let tag (vx ... (v x)) body (bs ...)))
  164. ((i:named-let tag (vx ...) body ((v x) bs ...))
  165. (i:named-let tag (vx ... (v x)) body (bs ...)))))
  166. ; --- standard procedures ---
  167. (define (uncons pair)
  168. (values (car pair) (cdr pair)))
  169. (define (uncons-2 list)
  170. (values (car list) (cadr list) (cddr list)))
  171. (define (uncons-3 list)
  172. (values (car list) (cadr list) (caddr list) (cdddr list)))
  173. (define (uncons-4 list)
  174. (values (car list) (cadr list) (caddr list) (cadddr list) (cddddr list)))
  175. (define (uncons-cons alist)
  176. (values (caar alist) (cdar alist) (cdr alist)))
  177. (define (unlist list)
  178. (apply values list))
  179. (define (unvector vector)
  180. (apply values (vector->list vector)))
  181. ; --- standard macros ---
  182. (define-syntax values->list
  183. (syntax-rules ()
  184. ((values->list x)
  185. (call-with-values (lambda () x) list))))
  186. (define-syntax values->vector
  187. (syntax-rules ()
  188. ((values->vector x)
  189. (call-with-values (lambda () x) vector))))