1 ;;; srfi-11.scm --- let-values and let*-values
3 ;; Copyright (C) 2000, 2001, 2002 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 2.1 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ;; This module exports two syntax forms: let-values and let*-values.
25 ;; (let-values (((x y . z) (foo a b))
29 ;; This binds `x' and `y' to the first to values returned by `foo',
30 ;; `z' to the rest of the values from `foo', and `p' and `q' to the
31 ;; values returned by `bar'. All of these are available to `baz'.
33 ;; let*-values : let-values :: let* : let
35 ;; This module is fully documented in the Guile Reference Manual.
39 (define-module (srfi srfi-11)
40 :use-module (ice-9 syncase)
41 :export-syntax (let-values let*-values))
43 (cond-expand-provide (current-module) '(srfi-11))
48 ;; Current approach is to translate
50 ;; (let-values (((x y . z) (foo a b))
56 ;; (call-with-values (lambda () (foo a b))
57 ;; (lambda (<tmp-x> <tmp-y> . <tmp-z>)
58 ;; (call-with-values (lambda () (bar c))
59 ;; (lambda (<tmp-p> <tmp-q>)
65 ;; (baz x y z p q))))))
67 ;; I originally wrote this as a define-macro, but then I found out
68 ;; that guile's gensym/gentemp was broken, so I tried rewriting it as
69 ;; a syntax-rules statement.
71 ;; Since syntax-rules didn't seem powerful enough to implement
72 ;; let-values in one definition without exposing illegal syntax (or
73 ;; perhaps my brain's just not powerful enough :>). I tried writing
74 ;; it using a private helper, but that didn't work because the
75 ;; let-values expands outside the scope of this module. I wonder why
76 ;; syntax-rules wasn't designed to allow "private" patterns or
79 ;; So in the end, I dumped the syntax-rules implementation, reproduced
80 ;; here for posterity, and went with the define-macro one below --
81 ;; gensym/gentemp's got to be fixed anyhow...
83 ; (define-syntax let-values-helper
85 ; ;; Take the vars from one let binding (i.e. the (x y z) from ((x y
86 ; ;; z) (values 1 2 3)) and turn it in to the corresponding (lambda
87 ; ;; (<tmp-x> <tmp-y> <tmp-z>) ...) from above, keeping track of the
88 ; ;; temps you create so you can use them later...
90 ; ;; I really don't fully understand why the (var-1 var-1) trick
91 ; ;; works below, but basically, when all those (x x) bindings show
92 ; ;; up in the final "let", syntax-rules forces a renaming.
94 ; ((_ "consumer" () lambda-tmps final-let-bindings lv-bindings
97 ; (let-values-helper "cwv" lv-bindings final-let-bindings body ...)))
99 ; ((_ "consumer" (var-1 var-2 ...) (lambda-tmp ...) final-let-bindings lv-bindings
101 ; (let-values-helper "consumer"
103 ; (lambda-tmp ... var-1)
104 ; ((var-1 var-1) . final-let-bindings)
108 ; ((_ "cwv" () final-let-bindings body ...)
109 ; (let final-let-bindings
112 ; ((_ "cwv" ((vars-1 binding-1) other-bindings ...) final-let-bindings
114 ; (call-with-values (lambda () binding-1)
115 ; (let-values-helper "consumer"
119 ; (other-bindings ...)
122 ; (define-syntax let-values
124 ; ((let-values () body ...)
126 ; ((let-values (binding ...) body ...)
127 ; (let-values-helper "cwv" (binding ...) () body ...))))
130 ; (define-syntax let-values
131 ; (letrec-syntax ((build-consumer
132 ; ;; Take the vars from one let binding (i.e. the (x
133 ; ;; y z) from ((x y z) (values 1 2 3)) and turn it
134 ; ;; in to the corresponding (lambda (<tmp-x> <tmp-y>
135 ; ;; <tmp-z>) ...) from above.
137 ; ((_ () new-tmps tmp-vars () body ...)
140 ; ((_ () new-tmps tmp-vars vars body ...)
142 ; (lv-builder vars tmp-vars body ...)))
143 ; ((_ (var-1 var-2 ...) new-tmps tmp-vars vars body ...)
144 ; (build-consumer (var-2 ...)
146 ; ((var-1 tmp-1) . tmp-vars)
151 ; ((_ () tmp-vars body ...)
154 ; ((_ ((vars-1 binding-1) (vars-2 binding-2) ...)
157 ; (call-with-values (lambda () binding-1)
158 ; (build-consumer vars-1
161 ; ((vars-2 binding-2) ...)
167 ; ((_ ((vars binding) ...) body ...)
168 ; (lv-builder ((vars binding) ...) () body ...)))))
170 ;; FIXME: This is currently somewhat unsafe (b/c gentemp/gensym is
171 ;; broken -- right now (as of 1.4.1, it doesn't generate unique
173 (define-macro (let-values vars . body)
175 (define (map-1-dot proc elts)
176 ;; map over one optionally dotted (a b c . d) list, producing an
177 ;; optionally dotted result.
180 ((pair? elts) (cons (proc (car elts)) (map-1-dot proc (cdr elts))))
183 (define (undot-list lst)
184 ;; produce a non-dotted list from a possibly dotted list.
187 ((pair? lst) (cons (car lst) (undot-list (cdr lst))))
190 (define (let-values-helper vars body prev-let-vars)
191 (let* ((var-binding (car vars))
192 (new-tmps (map-1-dot (lambda (sym) (gensym))
194 (let-vars (map (lambda (sym tmp) (list sym tmp))
195 (undot-list (car var-binding))
196 (undot-list new-tmps))))
198 (if (null? (cdr vars))
199 `(call-with-values (lambda () ,(cadr var-binding))
201 (let ,(apply append let-vars prev-let-vars)
203 `(call-with-values (lambda () ,(cadr var-binding))
205 ,(let-values-helper (cdr vars) body
206 (cons let-vars prev-let-vars)))))))
210 (let-values-helper vars body '())))
215 ;; Current approach is to translate
217 ;; (let*-values (((x y z) (foo a b))
223 ;; (call-with-values (lambda () (foo a b))
225 ;; (call-with-values (lambda (bar c))
227 ;; (baz x y z p q)))))
229 (define-syntax let*-values
231 ((let*-values () body ...)
233 ((let*-values ((vars-1 binding-1) (vars-2 binding-2) ...) body ...)
234 (call-with-values (lambda () binding-1)
236 (let*-values ((vars-2 binding-2) ...)
239 ; Alternate define-macro implementation...
241 ; (define-macro (let*-values vars . body)
242 ; (define (let-values-helper vars body)
243 ; (let ((var-binding (car vars)))
244 ; (if (null? (cdr vars))
245 ; `(call-with-values (lambda () ,(cadr var-binding))
246 ; (lambda ,(car var-binding)
248 ; `(call-with-values (lambda () ,(cadr var-binding))
249 ; (lambda ,(car var-binding)
250 ; ,(let-values-helper (cdr vars) body))))))
254 ; (let-values-helper vars body)))
256 ;;; srfi-11.scm ends here