*** empty log message ***
[bpt/guile.git] / srfi / srfi-11.scm
CommitLineData
69dab98b
RB
1;;;; srfi-11.scm --- SRFI-11 procedures for Guile
2
3;;; Copyright (C) 2000 Free Software Foundation, Inc.
4;;;
5;;; This program is free software; you can redistribute it and/or
6;;; modify it under the terms of the GNU General Public License as
7;;; published by the Free Software Foundation; either version 2, or
8;;; (at your option) any later version.
9;;;
10;;; This program 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;;; General Public License for more details.
14;;;
15;;; You should have received a copy of the GNU General Public License
16;;; along with this software; see the file COPYING. If not, write to
17;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18;;; Boston, MA 02111-1307 USA
19
20(define-module (srfi srfi-11)
21 :use-module (ice-9 syncase))
22
23;;;;;;;;;;;;;;
24;; let-values
25;;
26;; Current approach is to translate
27;;
109c463f 28;; (let-values (((x y . z) (foo a b))
69dab98b
RB
29;; ((p q) (bar c)))
30;; (baz x y z p q))
31;;
32;; into
33;;
34;; (call-with-values (lambda () (foo a b))
109c463f 35;; (lambda (<tmp-x> <tmp-y> . <tmp-z>)
69dab98b
RB
36;; (call-with-values (lambda () (bar c))
37;; (lambda (<tmp-p> <tmp-q>)
38;; (let ((x <tmp-x>)
39;; (y <tmp-y>)
40;; (z <tmp-z>)
41;; (p <tmp-p>)
42;; (q <tmp-q>))
43;; (baz x y z p q))))))
44
45;; I originally wrote this as a define-macro, but then I found out
46;; that guile's gensym/gentemp was broken, so I tried rewriting it as
47;; a syntax-rules statement.
48;;
49;; Since syntax-rules didn't seem powerful enough to implement
50;; let-values in one definition without exposing illegal syntax (or
51;; perhaps my brain's just not powerful enough :>). I tried writing
52;; it using a private helper, but that didn't work because the
53;; let-values expands outside the scope of this module. I wonder why
54;; syntax-rules wasn't designed to allow "private" patterns or
55;; similar...
56;;
57;; So in the end, I dumped the syntax-rules implementation, reproduced
58;; here for posterity, and went with the define-macro one below --
59;; gensym/gentemp's got to be fixed anyhow...
60;
61; (define-syntax let-values-helper
62; (syntax-rules ()
63; ;; Take the vars from one let binding (i.e. the (x y z) from ((x y
64; ;; z) (values 1 2 3)) and turn it in to the corresponding (lambda
65; ;; (<tmp-x> <tmp-y> <tmp-z>) ...) from above, keeping track of the
66; ;; temps you create so you can use them later...
67; ;;
68; ;; I really don't fully understand why the (var-1 var-1) trick
69; ;; works below, but basically, when all those (x x) bindings show
70; ;; up in the final "let", syntax-rules forces a renaming.
71
72; ((_ "consumer" () lambda-tmps final-let-bindings lv-bindings
73; body ...)
74; (lambda lambda-tmps
75; (let-values-helper "cwv" lv-bindings final-let-bindings body ...)))
76
77; ((_ "consumer" (var-1 var-2 ...) (lambda-tmp ...) final-let-bindings lv-bindings
78; body ...)
79; (let-values-helper "consumer"
80; (var-2 ...)
81; (lambda-tmp ... var-1)
82; ((var-1 var-1) . final-let-bindings)
83; lv-bindings
84; body ...))
85
86; ((_ "cwv" () final-let-bindings body ...)
87; (let final-let-bindings
88; body ...))
89
90; ((_ "cwv" ((vars-1 binding-1) other-bindings ...) final-let-bindings
91; body ...)
92; (call-with-values (lambda () binding-1)
93; (let-values-helper "consumer"
94; vars-1
95; ()
96; final-let-bindings
97; (other-bindings ...)
98; body ...)))))
99;
100; (define-syntax let-values
101; (syntax-rules ()
102; ((let-values () body ...)
103; (begin body ...))
104; ((let-values (binding ...) body ...)
105; (let-values-helper "cwv" (binding ...) () body ...))))
106;
107;
108; (define-syntax let-values
109; (letrec-syntax ((build-consumer
110; ;; Take the vars from one let binding (i.e. the (x
111; ;; y z) from ((x y z) (values 1 2 3)) and turn it
112; ;; in to the corresponding (lambda (<tmp-x> <tmp-y>
113; ;; <tmp-z>) ...) from above.
114; (syntax-rules ()
115; ((_ () new-tmps tmp-vars () body ...)
116; (lambda new-tmps
117; body ...))
118; ((_ () new-tmps tmp-vars vars body ...)
119; (lambda new-tmps
120; (lv-builder vars tmp-vars body ...)))
121; ((_ (var-1 var-2 ...) new-tmps tmp-vars vars body ...)
122; (build-consumer (var-2 ...)
123; (tmp-1 . new-tmps)
124; ((var-1 tmp-1) . tmp-vars)
125; bindings
126; body ...))))
127; (lv-builder
128; (syntax-rules ()
129; ((_ () tmp-vars body ...)
130; (let tmp-vars
131; body ...))
132; ((_ ((vars-1 binding-1) (vars-2 binding-2) ...)
133; tmp-vars
134; body ...)
135; (call-with-values (lambda () binding-1)
136; (build-consumer vars-1
137; ()
138; tmp-vars
139; ((vars-2 binding-2) ...)
140; body ...))))))
141;
142; (syntax-rules ()
143; ((_ () body ...)
144; (begin body ...))
145; ((_ ((vars binding) ...) body ...)
146; (lv-builder ((vars binding) ...) () body ...)))))
147
148;; FIXME: This is currently somewhat unsafe (b/c gentemp/gensym is
149;; broken -- right now (as of 1.4.1, it doesn't generate unique
150;; symbols)
151(define-macro (let-values vars . body)
109c463f
RB
152
153 (define (map-1-dot proc elts)
154 ;; map over one optionally dotted (a b c . d) list, producing an
155 ;; optionally dotted result.
156 (cond
157 ((null? elts) '())
158 ((pair? elts) (cons (proc (car elts)) (map-1-dot proc (cdr elts))))
159 (else (proc elts))))
160
161 (define (undot-list lst)
162 ;; produce a non-dotted list from a possibly dotted list.
163 (cond
164 ((null? lst) '())
165 ((pair? lst) (cons (car lst) (undot-list (cdr lst))))
166 (else (list lst))))
167
168 (define (let-values-helper vars body prev-let-vars)
69dab98b 169 (let* ((var-binding (car vars))
109c463f
RB
170 (new-tmps (map-1-dot (lambda (sym) (gentemp))
171 (car var-binding)))
172 (let-vars (map (lambda (sym tmp) (list sym tmp))
173 (undot-list (car var-binding))
174 (undot-list new-tmps))))
175
69dab98b
RB
176 (if (null? (cdr vars))
177 `(call-with-values (lambda () ,(cadr var-binding))
109c463f
RB
178 (lambda ,new-tmps
179 (let ,(apply append let-vars prev-let-vars)
69dab98b
RB
180 ,@body)))
181 `(call-with-values (lambda () ,(cadr var-binding))
109c463f
RB
182 (lambda ,new-tmps
183 ,(let-values-helper (cdr vars) body
184 (cons let-vars prev-let-vars)))))))
69dab98b
RB
185
186 (if (null? vars)
187 `(begin ,@body)
188 (let-values-helper vars body '())))
189
190;;;;;;;;;;;;;;
191;; let*-values
192;;
193;; Current approach is to translate
194;;
195;; (let*-values (((x y z) (foo a b))
196;; ((p q) (bar c)))
197;; (baz x y z p q))
198;;
199;; into
200;;
201;; (call-with-values (lambda () (foo a b))
202;; (lambda (x y z)
203;; (call-with-values (lambda (bar c))
204;; (lambda (p q)
205;; (baz x y z p q)))))
206
207(define-syntax let*-values
208 (syntax-rules ()
209 ((let*-values () body ...)
210 (begin body ...))
211 ((let*-values ((vars-1 binding-1) (vars-2 binding-2) ...) body ...)
212 (call-with-values (lambda () binding-1)
213 (lambda vars-1
214 (let*-values ((vars-2 binding-2) ...)
215 body ...))))))
216
217; Alternate define-macro implementation...
218;
219; (define-macro (let*-values vars . body)
220; (define (let-values-helper vars body)
221; (let ((var-binding (car vars)))
222; (if (null? (cdr vars))
223; `(call-with-values (lambda () ,(cadr var-binding))
224; (lambda ,(car var-binding)
225; ,@body))
226; `(call-with-values (lambda () ,(cadr var-binding))
227; (lambda ,(car var-binding)
228; ,(let-values-helper (cdr vars) body))))))
229
230; (if (null? vars)
231; `(begin ,@body)
232; (let-values-helper vars body)))
233
234(export-syntax let-values
235 let*-values)