srfi-71.scm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ; Reference implementation of SRFI-71 (generic part)
  2. ; Sebastian.Egner@philips.com, 20-May-2005, PLT 208
  3. ;
  4. ; In order to avoid conflicts with the existing let etc.
  5. ; the macros defined here are called srfi-let etc.,
  6. ; and they are defined in terms of r5rs-let etc.
  7. ; It is up to the actual implementation to save let/*/rec
  8. ; in r5rs-let/*/rec first and redefine let/*/rec
  9. ; by srfi-let/*/rec then.
  10. ;
  11. ; There is also a srfi-letrec* being defined (in view of R6RS.)
  12. ;
  13. ; Macros used internally are named i:<something>.
  14. ;
  15. ; Abbreviations for macro arguments:
  16. ; bs - <binding spec>
  17. ; b - component of a binding spec (values, <variable>, or <expression>)
  18. ; v - <variable>
  19. ; vr - <variable> for rest list
  20. ; x - <expression>
  21. ; t - newly introduced temporary variable
  22. ; vx - (<variable> <expression>)
  23. ; rec - flag if letrec is produced (and not let)
  24. ; cwv - call-with-value skeleton of the form (x formals)
  25. ; (call-with-values (lambda () x) (lambda formals /payload/))
  26. ; where /payload/ is of the form (let (vx ...) body1 body ...).
  27. ;
  28. ; Remark (*):
  29. ; We bind the variables of a letrec to i:undefined since there is
  30. ; no portable (R5RS) way of binding a variable to a values that
  31. ; raises an error when read uninitialized.
  32. (define i:undefined 'undefined)
  33. (define-syntax srfi-letrec* ; -> srfi-letrec
  34. (syntax-rules ()
  35. ((srfi-letrec* () body1 body ...)
  36. (srfi-letrec () body1 body ...))
  37. ((srfi-letrec* (bs) body1 body ...)
  38. (srfi-letrec (bs) body1 body ...))
  39. ((srfi-letrec* (bs1 bs2 bs ...) body1 body ...)
  40. (srfi-letrec (bs1) (srfi-letrec* (bs2 bs ...) body1 body ...)))))
  41. (define-syntax srfi-letrec ; -> i:let
  42. (syntax-rules ()
  43. ((srfi-letrec ((b1 b2 b ...) ...) body1 body ...)
  44. (i:let "bs" #t () () (body1 body ...) ((b1 b2 b ...) ...)))))
  45. (define-syntax srfi-let* ; -> srfi-let
  46. (syntax-rules ()
  47. ((srfi-let* () body1 body ...)
  48. (srfi-let () body1 body ...))
  49. ((srfi-let* (bs) body1 body ...)
  50. (srfi-let (bs) body1 body ...))
  51. ((srfi-let* (bs1 bs2 bs ...) body1 body ...)
  52. (srfi-let (bs1) (srfi-let* (bs2 bs ...) body1 body ...)))))
  53. (define-syntax srfi-let ; -> i:let or i:named-let
  54. (syntax-rules ()
  55. ((srfi-let ((b1 b2 b ...) ...) body1 body ...)
  56. (i:let "bs" #f () () (body1 body ...) ((b1 b2 b ...) ...)))
  57. ((srfi-let tag ((b1 b2 b ...) ...) body1 body ...)
  58. (i:named-let tag () (body1 body ...) ((b1 b2 b ...) ...)))))
  59. (define-syntax i:let
  60. (syntax-rules (values)
  61. ; (i:let "bs" rec (cwv ...) (vx ...) body (bs ...))
  62. ; processes the binding specs bs ... by adding call-with-values
  63. ; skeletons to cwv ... and bindings to vx ..., and afterwards
  64. ; wrapping the skeletons around the payload (let (vx ...) . body).
  65. ; no more bs to process -> wrap call-with-values skeletons
  66. ((i:let "bs" rec (cwv ...) vxs body ())
  67. (i:let "wrap" rec vxs body cwv ...))
  68. ; recognize form1 without variable -> dummy binding for side-effects
  69. ((i:let "bs" rec cwvs (vx ...) body (((values) x) bs ...))
  70. (i:let "bs" rec cwvs (vx ... (dummy (begin x #f))) body (bs ...)))
  71. ; recognize form1 with single variable -> just extend vx ...
  72. ((i:let "bs" rec cwvs (vx ...) body (((values v) x) bs ...))
  73. (i:let "bs" rec cwvs (vx ... (v x)) body (bs ...)))
  74. ; recognize form1 without rest arg -> generate cwv
  75. ((i:let "bs" rec cwvs vxs body (((values v ...) x) bs ...))
  76. (i:let "form1" rec cwvs vxs body (bs ...) (x ()) (values v ...)))
  77. ; recognize form1 with rest arg -> generate cwv
  78. ((i:let "bs" rec cwvs vxs body (((values . vs) x) bs ...))
  79. (i:let "form1+" rec cwvs vxs body (bs ...) (x ()) (values . vs)))
  80. ; recognize form2 with single variable -> just extend vx ...
  81. ((i:let "bs" rec cwvs (vx ...) body ((v x) bs ...))
  82. (i:let "bs" rec cwvs (vx ... (v x)) body (bs ...)))
  83. ; recognize form2 with >=2 variables -> transform to form1
  84. ((i:let "bs" rec cwvs vxs body ((b1 b2 b3 b ...) bs ...))
  85. (i:let "form2" rec cwvs vxs body (bs ...) (b1 b2) (b3 b ...)))
  86. ; (i:let "form1" rec cwvs vxs body bss (x (t ...)) (values v1 v2 v ...))
  87. ; processes the variables in v1 v2 v ... adding them to (t ...)
  88. ; and producing a cwv when finished. There is not rest argument.
  89. ((i:let "form1" rec (cwv ...) vxs body bss (x ts) (values))
  90. (i:let "bs" rec (cwv ... (x ts)) vxs body bss))
  91. ((i:let "form1" rec cwvs (vx ...) body bss (x (t ...)) (values v1 v ...))
  92. (i:let "form1" rec cwvs (vx ... (v1 t1)) body bss (x (t ... t1)) (values v ...)))
  93. ; (i:let "form1+" rec cwvs vxs body bss (x (t ...)) (values v ... . vr))
  94. ; processes the variables in v ... . vr adding them to (t ...)
  95. ; and producing a cwv when finished. The rest arg is vr.
  96. ((i:let "form1+" rec cwvs (vx ...) body bss (x (t ...)) (values v1 v2 . vs))
  97. (i:let "form1+" rec cwvs (vx ... (v1 t1)) body bss (x (t ... t1)) (values v2 . vs)))
  98. ((i:let "form1+" rec (cwv ...) (vx ...) body bss (x (t ...)) (values v1 . vr))
  99. (i:let "bs" rec (cwv ... (x (t ... t1 . tr))) (vx ... (v1 t1) (vr tr)) body bss))
  100. ((i:let "form1+" rec (cwv ...) (vx ...) body bss (x ()) (values . vr))
  101. (i:let "bs" rec (cwv ... (x tr)) (vx ... (vr tr)) body bss))
  102. ; (i:let "form2" rec cwvs vxs body bss (v ...) (b ... x))
  103. ; processes the binding items (b ... x) from form2 as in
  104. ; (v ... b ... x) into ((values v ... b ...) x), i.e. form1.
  105. ; Then call "bs" recursively.
  106. ((i:let "form2" rec cwvs vxs body (bs ...) (v ...) (x))
  107. (i:let "bs" rec cwvs vxs body (((values v ...) x) bs ...)))
  108. ((i:let "form2" rec cwvs vxs body bss (v ...) (b1 b2 b ...))
  109. (i:let "form2" rec cwvs vxs body bss (v ... b1) (b2 b ...)))
  110. ; (i:let "wrap" rec ((v x) ...) (body ...) cwv ...)
  111. ; wraps cwv ... around the payload generating the actual code.
  112. ; For letrec this is of course different than for let.
  113. ((i:let "wrap" #f vxs body)
  114. (r5rs-let vxs . body))
  115. ((i:let "wrap" #f vxs body (x formals) cwv ...)
  116. (call-with-values
  117. (lambda () x)
  118. (lambda formals (i:let "wrap" #f vxs body cwv ...))))
  119. ((i:let "wrap" #t vxs body)
  120. (r5rs-letrec vxs . body))
  121. ((i:let "wrap" #t ((v t) ...) body cwv ...)
  122. (r5rs-let ((v i:undefined) ...) ; (*)
  123. (i:let "wraprec" ((v t) ...) body cwv ...)))
  124. ; (i:let "wraprec" ((v t) ...) body cwv ...)
  125. ; generate the inner code for a letrec. The variables v ...
  126. ; are the user-visible variables (bound outside), and t ...
  127. ; are the temporary variables bound by the cwv consumers.
  128. ((i:let "wraprec" ((v t) ...) (body ...))
  129. (begin (set! v t) ... (r5rs-let () body ...)))
  130. ((i:let "wraprec" vxs body (x formals) cwv ...)
  131. (call-with-values
  132. (lambda () x)
  133. (lambda formals (i:let "wraprec" vxs body cwv ...))))
  134. ))
  135. (define-syntax i:named-let
  136. (syntax-rules (values)
  137. ; (i:named-let tag (vx ...) body (bs ...))
  138. ; processes the binding specs bs ... by extracting the variable
  139. ; and expression, adding them to vx and turning the result into
  140. ; an ordinary named let.
  141. ((i:named-let tag vxs body ())
  142. (r5rs-let tag vxs . body))
  143. ((i:named-let tag (vx ...) body (((values v) x) bs ...))
  144. (i:named-let tag (vx ... (v x)) body (bs ...)))
  145. ((i:named-let tag (vx ...) body ((v x) bs ...))
  146. (i:named-let tag (vx ... (v x)) body (bs ...)))))
  147. ; --- standard procedures ---
  148. (define (uncons pair)
  149. (values (car pair) (cdr pair)))
  150. (define (uncons-2 list)
  151. (values (car list) (cadr list) (cddr list)))
  152. (define (uncons-3 list)
  153. (values (car list) (cadr list) (caddr list) (cdddr list)))
  154. (define (uncons-4 list)
  155. (values (car list) (cadr list) (caddr list) (cadddr list) (cddddr list)))
  156. (define (uncons-cons alist)
  157. (values (caar alist) (cdar alist) (cdr alist)))
  158. (define (unlist list)
  159. (apply values list))
  160. (define (unvector vector)
  161. (apply values (vector->list vector)))
  162. ; --- standard macros ---
  163. (define-syntax values->list
  164. (syntax-rules ()
  165. ((values->list x)
  166. (call-with-values (lambda () x) list))))
  167. (define-syntax values->vector
  168. (syntax-rules ()
  169. ((values->vector x)
  170. (call-with-values (lambda () x) vector))))