Debian Codegraph @

lib/srfi/42.scm

raw
1 ;;
2 ;; SRFI-42 - eager comprehension
3 ;;
4 ;; This is a port of Sebastian Egner's reference implementation to Gauche.
5 ;; Ported by Alex Shinn.
6 ;;
7
8 ; <PLAINTEXT>
9 ; Eager Comprehensions in [outer..inner|expr]-Convention
10 ; ======================================================
11 ;
12 ; [email protected], Eindhoven, The Netherlands, 26-Dec-2007
13 ; Scheme R5RS (incl. macros), SRFI-23 (error).
14 ;
15 ; Loading the implementation into Scheme48 0.57:
16 ; ,open srfi-23
17 ; ,load ec.scm
18 ;
19 ; Loading the implementation into PLT/DrScheme 317:
20 ; ; File > Open ... "ec.scm", click Execute
21 ;
22 ; Loading the implementation into SCM 5d7:
23 ; (require 'macro) (require 'record)
24 ; (load "ec.scm")
25 ;
26 ; Implementation comments:
27 ; * All local (not exported) identifiers are named ec-<something>.
28 ; * This implementation focuses on portability, performance,
29 ; readability, and simplicity roughly in this order. Design
30 ; decisions related to performance are taken for Scheme48.
31 ; * Alternative implementations, Comments and Warnings are
32 ; mentioned after the definition with a heading.
33
34 (define-module srfi.42
35 (use util.match)
36 (use gauche.generator)
37 (export-all))
38 (select-module srfi.42)
39
40 (autoload gauche.uvector uvector->list)
41 (autoload data.range range? range-length range-ref)
42
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 ;;
45 ;; Gauche treats :foo forms as keywords, which are not valid as
46 ;; identifiers or macros so we can't port this directly. However, all
47 ;; of the (:keyword ...) forms must be within one of the enclosing *-ec
48 ;; forms, so we can hack it by making all of those forms replace the
49 ;; keywords with appropriately renamed identifiers.
50
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 ;;
53 ;; The hygienic syntax version. Only replaces top-level qualifiers, so
54 ;; nested (:keyword ...) forms fail. Recursively replacing them is
55 ;; possible but much trickier syntax so I haven't gotten around to it
56 ;; yet. Using this version, the SRFI-42 examples.scm tests fail first
57 ;; on the following form:
58 ;
59 ; (my-check
60 ; (list-ec (:parallel (:range i 1 10) (:list x '(a b c))) (list i x))
61 ; => '((1 a) (2 b) (3 c)) )
62
63 ; (define-syntax %replace-keywords
64 ; (syntax-rules ()
65 ; ((_ syn (collect ...))
66 ; (syn collect ...))
67 ; ((_ syn (collect ...) x y ...)
68 ; (%replace-one-keyword x syn (collect ...) y ...))))
69
70 ; (define-syntax %replace-one-keyword
71 ; (syntax-rules ()
72 ; ((_ (: x ...) syn (c ...) y ...)
73 ; (%replace-keywords syn (c ... (srfi-42- x ...)) y ...))
74 ; ((_ (:list x ...) syn (c ...) y ...)
75 ; (%replace-keywords syn (c ... (srfi-42-list x ...)) y ...))
76 ; ((_ (:string x ...) syn (c ...) y ...)
77 ; (%replace-keywords syn (c ... (srfi-42-string x ...)) y ...))
78 ; ((_ (:vector x ...) syn (c ...) y ...)
79 ; (%replace-keywords syn (c ... (srfi-42-vector x ...)) y ...))
80 ; ((_ (:integers x ...) syn (c ...) y ...)
81 ; (%replace-keywords syn (c ... (srfi-42-integers x ...)) y ...))
82 ; ((_ (:range x ...) syn (c ...) y ...)
83 ; (%replace-keywords syn (c ... (srfi-42-range x ...)) y ...))
84 ; ((_ (:real-range x ...) syn (c ...) y ...)
85 ; (%replace-keywords syn (c ... (srfi-42-real-range x ...)) y ...))
86 ; ((_ (:char-range x ...) syn (c ...) y ...)
87 ; (%replace-keywords syn (c ... (srfi-42-char-range x ...)) y ...))
88 ; ((_ (:port x ...) syn (c ...) y ...)
89 ; (%replace-keywords syn (c ... (srfi-42-port x ...)) y ...))
90 ; ((_ (:dispatched x ...) syn (c ...) y ...)
91 ; (%replace-keywords syn (c ... (srfi-42-dispatched x ...)) y ...))
92 ; ((_ (:do x ...) syn (c ...) y ...)
93 ; (%replace-keywords syn (c ... (srfi-42-do x ...)) y ...))
94 ; ((_ (:let x ...) syn (c ...) y ...)
95 ; (%replace-keywords syn (c ... (srfi-42-let x ...)) y ...))
96 ; ((_ (:parallel x ...) syn (c ...) y ...)
97 ; (%replace-keywords syn (c ... (srfi-42-parallel x ...)) y ...))
98 ; ((_ (:while x ...) syn (c ...) y ...)
99 ; (%replace-keywords syn (c ... (srfi-42-while x ...)) y ...))
100 ; ((_ (:until x ...) syn (c ...) y ...)
101 ; (%replace-keywords syn (c ... (srfi-42-until x ...)) y ...))
102 ; ((_ x syn (c ...) y ...) (%replace-keywords syn (c ... x) y ...))))
103
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
105 ;;
106 ;; The low-level macro version. Non-hygienic, so works so long as you
107 ;; don't try to redefine the meaning of the :qualifiers (which you can't
108 ;; do in Gauche anyway) or try to use syntax which expands into a
109 ;; (:qualifier ...) form inside of a *-ec (which would be ugly and poor
110 ;; style).
111
112 ;; use the same signature as the hygienic version for compatibility, we
113 ;; ignore the middle "collector" arg
114 (define-macro (%replace-keywords syn _ . args)
115 (define (rename symbol) ; bandage for hygiene
116 ((with-module gauche.internal make-identifier)
117 symbol (current-module) '()))
118 (define (rewrite x)
119 (if (pair? x)
120 (if (keyword? (car x))
121 (cons
122 (case (car x)
123 ((:) (rename 'srfi-42-))
124 ((:list) (rename 'srfi-42-list))
125 ((:string) (rename 'srfi-42-string))
126 ((:vector) (rename 'srfi-42-vector))
127 ((:uvector) (rename 'srfi-42-uvector))
128 ((:integers) (rename 'srfi-42-integers))
129 ((:range) (rename 'srfi-42-range))
130 ((:real-range) (rename 'srfi-42-real-range))
131 ((:char-range) (rename 'srfi-42-char-range))
132 ((:port) (rename 'srfi-42-port))
133 ((:generator) (rename 'srfi-42-generator))
134 ((:collection) (rename 'srfi-42-collection))
135 ((:dispatched) (rename 'srfi-42-dispatched))
136 ((:do) (rename 'srfi-42-do))
137 ((:let) (rename 'srfi-42-let))
138 ((:parallel) (rename 'srfi-42-parallel))
139 ((:while) (rename 'srfi-42-while))
140 ((:until) (rename 'srfi-42-until))
141 (else (car x)))
142 (rewrite (cdr x)))
143 (cons (rewrite (car x)) (rewrite (cdr x))))
144 x))
145 `(,syn ,@(map rewrite args)))
146
147
148 ; ==========================================================================
149 ; The fundamental comprehension do-ec
150 ; ==========================================================================
151 ;
152 ; All eager comprehensions are reduced into do-ec and
153 ; all generators are reduced to :do.
154 ;
155 ; We use the following short names for syntactic variables
156 ; q - qualifier
157 ; cc - current continuation, thing to call at the end;
158 ; the CPS is (m (cc ...) arg ...) -> (cc ... expr ...)
159 ; cmd - an expression being evaluated for its side-effects
160 ; expr - an expression
161 ; gen - a generator of an eager comprehension
162 ; ob - outer binding
163 ; oc - outer command
164 ; lb - loop binding
165 ; ne1? - not-end1? (before the payload)
166 ; ib - inner binding
167 ; ic - inner command
168 ; ne2? - not-end2? (after the payload)
169 ; ls - loop step
170 ; etc - more arguments of mixed type
171
172
173 ; (do-ec q ... cmd)
174 ; handles nested, if/not/and/or, begin, :let, and calls generator
175 ; macros in CPS to transform them into fully decorated :do.
176 ; The code generation for a :do is delegated to do-ec:do.
177
178 ; (define-syntax do-ec
179 ; (syntax-rules ()
180 ; ((_ expr ...)
181 ; (%do-ec (%replace-keywords expr) ...))))
182
183 (define-syntax do-ec
184 (syntax-rules ()
185 ((do-ec expr ...)
186 (%replace-keywords %do-ec () expr ...))))
187
188 (define-syntax %do-ec
189 ;;(syntax-rules (nested if not and or begin :do let)
190 (syntax-rules (nested if not and or begin srfi-42-do let)
191
192 ; explicit nesting -> implicit nesting
193 ((do-ec (nested q ...) etc ...)
194 (do-ec q ... etc ...) )
195
196 ; implicit nesting -> fold do-ec
197 ((do-ec q1 q2 etc1 etc ...)
198 (do-ec q1 (do-ec q2 etc1 etc ...)) )
199
200 ; no qualifiers at all -> evaluate cmd once
201 ((do-ec cmd)
202 (begin cmd (if #f #f)) )
203
204 ; now (do-ec q cmd) remains
205
206 ; filter -> make conditional
207 ((do-ec (if test) cmd)
208 (if test (do-ec cmd)) )
209 ((do-ec (not test) cmd)
210 (if (not test) (do-ec cmd)) )
211 ((do-ec (and test ...) cmd)
212 (if (and test ...) (do-ec cmd)) )
213 ((do-ec (or test ...) cmd)
214 (if (or test ...) (do-ec cmd)) )
215
216 ; begin -> make a sequence
217 ((do-ec (begin etc ...) cmd)
218 (begin etc ... (do-ec cmd)) )
219
220 ; fully decorated :do-generator -> delegate to do-ec:do
221 ((do-ec (srfi-42-do olet lbs ne1? ilet ne2? lss) cmd)
222 (do-ec:do cmd (srfi-42-do olet lbs ne1? ilet ne2? lss)) )
223
224 ; anything else -> call generator-macro in CPS; reentry at (*)
225
226 ((do-ec (g arg1 arg ...) cmd)
227 (g (do-ec:do cmd) arg1 arg ...) )))
228
229
230 ; (do-ec:do cmd (:do olet lbs ne1? ilet ne2? lss)
231 ; generates code for a single fully decorated :do-generator
232 ; with cmd as payload, taking care of special cases.
233
234 (define-syntax do-ec:do
235 ;;(syntax-rules (:do let)
236 (syntax-rules (srfi-42-do let)
237
238 ; reentry point (*) -> generate code
239 ((do-ec:do cmd
240 (srfi-42-do (let obs oc ...)
241 lbs
242 ne1?
243 (let ibs ic ...)
244 ne2?
245 (ls ...) ))
246 (ec-simplify
247 (let obs
248 oc ...
249 (let loop lbs
250 (ec-simplify
251 (if ne1?
252 (ec-simplify
253 (let ibs
254 ic ...
255 cmd
256 (ec-simplify
257 (if ne2?
258 (loop ls ...) )))))))))) ))
259
260
261 ; (ec-simplify <expression>)
262 ; generates potentially more efficient code for <expression>.
263 ; The macro handles if, (begin <command>*), and (let () <command>*)
264 ; and takes care of special cases.
265
266 (define-syntax ec-simplify
267 (syntax-rules (if not let begin)
268
269 ; one- and two-sided if
270
271 ; literal <test>
272 ((ec-simplify (if #t consequent))
273 consequent )
274 ((ec-simplify (if #f consequent))
275 (if #f #f) )
276 ((ec-simplify (if #t consequent alternate))
277 consequent )
278 ((ec-simplify (if #f consequent alternate))
279 alternate )
280
281 ; (not (not <test>))
282 ((ec-simplify (if (not (not test)) consequent))
283 (ec-simplify (if test consequent)) )
284 ((ec-simplify (if (not (not test)) consequent alternate))
285 (ec-simplify (if test consequent alternate)) )
286
287 ; (let () <command>*)
288
289 ; empty <binding spec>*
290 ((ec-simplify (let () command ...))
291 (ec-simplify (begin command ...)) )
292
293 ; begin
294
295 ; flatten use helper (ec-simplify 1 done to-do)
296 ((ec-simplify (begin command ...))
297 (ec-simplify 1 () (command ...)) )
298 ((ec-simplify 1 done ((begin to-do1 ...) to-do2 ...))
299 (ec-simplify 1 done (to-do1 ... to-do2 ...)) )
300 ((ec-simplify 1 (done ...) (to-do1 to-do ...))
301 (ec-simplify 1 (done ... to-do1) (to-do ...)) )
302
303 ; exit helper
304 ((ec-simplify 1 () ())
305 (if #f #f) )
306 ((ec-simplify 1 (command) ())
307 command )
308 ((ec-simplify 1 (command1 command ...) ())
309 (begin command1 command ...) )
310
311 ; anything else
312
313 ((ec-simplify expression)
314 expression )))
315
316
317 ; ==========================================================================
318 ; The special generators :do, :let, :parallel, :while, and :until
319 ; ==========================================================================
320
321 (define-syntax srfi-42-do
322 (er-macro-transformer
323 (^[f r c]
324 (match f
325 ;; full decorated -> continue with cc, reentry at (*)
326 [(_ (cc ...) olet lbs ne1? ilet ne2? lss)
327 `(,@cc (,(r'srfi-42-do) ,olet ,lbs ,ne1? ,ilet ,ne2? ,lss))]
328 ;; short form -> fill in default values
329 [(_ cc lbs ne1? lss)
330 (quasirename r
331 `(srfi-42-do ,cc (let ()) ,lbs ,ne1? (let ()) #t ,lss))]))))
332
333 (define-syntax srfi-42-let
334 (er-macro-transformer
335 (^[f r c]
336 (define (index.? x) (c (r'index) x))
337 (match f
338 [(_ cc var ((? index.?) i) expression)
339 (quasirename r
340 `(srfi-42-do ,cc (let ((,var ,expression) (,i 0)))
341 () #t (let ()) #f ()))]
342 [(_ cc var expression)
343 (quasirename r
344 `(srfi-42-do ,cc (let ((,var ,expression)))
345 () #t (let ()) #f ()) )]))))
346
347 (define-syntax srfi-42-parallel
348 ;;(syntax-rules (:do)
349 (syntax-rules ()
350 ((_ cc)
351 cc )
352 ((_ cc (g arg1 arg ...) gen ...)
353 (g (srfi-42-parallel-1 cc (gen ...)) arg1 arg ...) )))
354
355 ; (:parallel-1 cc (to-do ...) result [ next ] )
356 ; iterates over to-do by converting the first generator into
357 ; the :do-generator next and merging next into result.
358
359 (define-syntax srfi-42-parallel-1 ; used as
360 ;;(syntax-rules (:do let)
361 (syntax-rules (srfi-42-do let)
362
363 ; process next element of to-do, reentry at (**)
364 ((_ cc ((g arg1 arg ...) gen ...) result)
365 (g (srfi-42-parallel-1 cc (gen ...) result) arg1 arg ...) )
366
367 ; reentry point (**) -> merge next into result
368 ((_
369 cc
370 gens
371 (srfi-42-do (let (ob1 ...) oc1 ...)
372 (lb1 ...)
373 ne1?1
374 (let (ib1 ...) ic1 ...)
375 ne2?1
376 (ls1 ...) )
377 (srfi-42-do (let (ob2 ...) oc2 ...)
378 (lb2 ...)
379 ne1?2
380 (let (ib2 ...) ic2 ...)
381 ne2?2
382 (ls2 ...) ))
383 (srfi-42-parallel-1
384 cc
385 gens
386 (srfi-42-do (let (ob1 ... ob2 ...) oc1 ... oc2 ...)
387 (lb1 ... lb2 ...)
388 (and ne1?1 ne1?2)
389 (let (ib1 ... ib2 ...) ic1 ... ic2 ...)
390 (and ne2?1 ne2?2)
391 (ls1 ... ls2 ...) )))
392
393 ; no more gens -> continue with cc, reentry at (*)
394 ((_ (cc ...) () result)
395 (cc ... result) )))
396
397 ; (:while-1 cc test (:do ...))
398 ; modifies the fully decorated :do-generator such that it
399 ; runs while test is a true value.
400 ; The original implementation just replaced ne1? by
401 ; (and ne1? test) as follows:
402 ;
403 ; (define-syntax :while-1
404 ; (syntax-rules (:do)
405 ; ((:while-1 cc test (:do olet lbs ne1? ilet ne2? lss))
406 ; (:do cc olet lbs (and ne1? test) ilet ne2? lss) )))
407 ;
408 ; Bug #1:
409 ; Unfortunately, this code is wrong because ne1? may depend
410 ; in the inner bindings introduced in ilet, but ne1? is evaluated
411 ; outside of the inner bindings. (Refer to the specification of
412 ; :do to see the structure.)
413 ; The problem manifests itself (as [email protected]
414 ; observed, 25-Apr-2005) when the :list-generator is modified:
415 ;
416 ; (do-ec (:while (:list x '(1 2)) (= x 1)) (display x)).
417 ;
418 ; In order to generate proper code, we introduce temporary
419 ; variables saving the values of the inner bindings. The inner
420 ; bindings are executed in a new ne1?, which also evaluates ne1?
421 ; outside the scope of the inner bindings, then the inner commands
422 ; are executed (possibly changing the variables), and then the
423 ; values of the inner bindings are saved and (and ne1? test) is
424 ; returned. In the new ilet, the inner variables are bound and
425 ; initialized and their values are restored. So we construct:
426 ;
427 ; (let (ob .. (ib-tmp #f) ...)
428 ; oc ...
429 ; (let loop (lb ...)
430 ; (if (let (ne1?-value ne1?)
431 ; (let ((ib-var ib-rhs) ...)
432 ; ic ...
433 ; (set! ib-tmp ib-var) ...)
434 ; (and ne1?-value test))
435 ; (let ((ib-var ib-tmp) ...)
436 ; /payload/
437 ; (if ne2?
438 ; (loop ls ...) )))))
439 ;
440 ; Bug #2:
441 ; Unfortunately, the above expansion is still incorrect (as Jens-Axel
442 ; Soegaard pointed out, 4-Jun-2007) because ib-rhs are evaluated even
443 ; if ne1?-value is #f, indicating that the loop has ended.
444 ; The problem manifests itself in the following example:
445 ;
446 ; (do-ec (:while (:list x '(1)) #t) (display x))
447 ;
448 ; Which iterates :list beyond exhausting the list '(1).
449 ;
450 ; For the fix, we follow Jens-Axel's approach of guarding the evaluation
451 ; of ib-rhs with a check on ne1?-value.
452
453 (define-syntax srfi-42-while
454 (syntax-rules ()
455 ((_ cc (g arg1 arg ...) test)
456 (g (srfi-42-while-1 cc test) arg1 arg ...) )))
457
458 (define-syntax srfi-42-while-1
459 (syntax-rules (srfi-42-do let)
460 ((srfi-42-while-1 cc test (srfi-42-do olet lbs ne1? ilet ne2? lss))
461 (srfi-42-while-2 cc test () () () (srfi-42-do olet lbs ne1? ilet ne2? lss)))))
462
463 (define-syntax srfi-42-while-2
464 (syntax-rules (srfi-42-do let)
465 ((srfi-42-while-2 cc
466 test
467 (ib-let ...)
468 (ib-save ...)
469 (ib-restore ...)
470 (srfi-42-do olet
471 lbs
472 ne1?
473 (let ((ib-var ib-rhs) ib ...) ic ...)
474 ne2?
475 lss))
476 (srfi-42-while-2 cc
477 test
478 (ib-let ... (ib-tmp #f))
479 (ib-save ... (ib-var ib-rhs))
480 (ib-restore ... (ib-var ib-tmp))
481 (srfi-42-do olet
482 lbs
483 ne1?
484 (let (ib ...) ic ... (set! ib-tmp ib-var))
485 ne2?
486 lss)))
487 ((srfi-42-while-2 cc
488 test
489 (ib-let ...)
490 (ib-save ...)
491 (ib-restore ...)
492 (srfi-42-do (let (ob ...) oc ...) lbs ne1?
493 (let () ic ...) ne2? lss))
494 (srfi-42-do cc
495 (let (ob ... ib-let ...) oc ...)
496 lbs
497 (let ((ne1?-value ne1?))
498 (and ne1?-value
499 (let (ib-save ...)
500 ic ...
501 test)))
502 (let (ib-restore ...))
503 ne2?
504 lss))))
505
506 (define-syntax srfi-42-until
507 (syntax-rules ()
508 ((_ cc (g arg1 arg ...) test)
509 (g (srfi-42-until-1 cc test) arg1 arg ...) )))
510
511 (define-syntax srfi-42-until-1
512 ;;(syntax-rules (:do)
513 (syntax-rules (srfi-42-do)
514 ((_ cc test (srfi-42-do olet lbs ne1? ilet ne2? lss))
515 (srfi-42-do cc olet lbs ne1? ilet (and ne2? (not test)) lss) )))
516
517
518 ; ==========================================================================
519 ; The typed generators :list :string :vector etc.
520 ; ==========================================================================
521
522 (define-syntax srfi-42-list
523 (syntax-rules (index)
524 ((_ cc var (index i) arg ...)
525 (srfi-42-parallel cc (srfi-42-list var arg ...) (srfi-42-integers i)) )
526 ((_ cc var arg1 arg2 arg ...)
527 (srfi-42-list cc var (append arg1 arg2 arg ...)) )
528 ((_ cc var arg)
529 (srfi-42-do cc
530 (let ())
531 ((t arg))
532 (not (null? t))
533 (let ((var (car t))))
534 #t
535 ((cdr t)) ))
536 ((_ x ...) #t)))
537
538
539 (define-syntax srfi-42-string
540 (syntax-rules (index)
541 ((_ cc var (index i) arg)
542 (srfi-42-do cc
543 (let ((str arg) (len 0))
544 (set! len (string-length str)))
545 ((i 0))
546 (< i len)
547 (let ((var (string-ref str i))))
548 #t
549 ((+ i 1)) ))
550 ((_ cc var (index i) arg1 arg2 arg ...)
551 (srfi-42-string cc var (index i) (string-append arg1 arg2 arg ...)) )
552 ((_ cc var arg1 arg ...)
553 (srfi-42-string cc var (index i) arg1 arg ...) )))
554
555 ; Alternative: An implementation in the style of :vector can also
556 ; be used for :string. However, it is less interesting as the
557 ; overhead of string-append is much less than for 'vector-append'.
558
559 (define-syntax srfi-42-vector
560 (syntax-rules ()
561 [(_ . args) (srfi-42-*vector vector-length vector-ref . args)]))
562
563 (define-syntax srfi-42-uvector
564 (syntax-rules ()
565 [(_ . args) (srfi-42-*vector uvector-length uvector-ref . args)]))
566
567 (define-syntax srfi-42-*vector
568 (syntax-rules (index)
569 ((_ *len *ref cc var arg)
570 (srfi-42-*vector *len *ref cc var (index i) arg) )
571 ((_ *len *ref cc var (index i) arg)
572 (srfi-42-do cc
573 (let ((vec arg) (len 0))
574 (set! len (*len vec)))
575 ((i 0))
576 (< i len)
577 (let ((var (*ref vec i))))
578 #t
579 ((+ i 1)) ))
580
581 ((_ *len *ref cc var (index i) arg1 arg2 arg ...)
582 (srfi-42-parallel cc
583 (srfi-42-*vector *len *ref cc var arg1 arg2 arg ...)
584 (srfi-42-integers i)) )
585 ((_ *len *ref cc var arg1 arg2 arg ...)
586 (srfi-42-do cc
587 (let ((vec #f)
588 (len 0)
589 (vecs (ec-:vector-filter *len (list arg1 arg2 arg ...))) ))
590 ((k 0))
591 (if (< k len)
592 #t
593 (if (null? vecs)
594 #f
595 (begin (set! vec (car vecs))
596 (set! vecs (cdr vecs))
597 (set! len (*len vec))
598 (set! k 0)
599 #t )))
600 (let ((var (*ref vec k))))
601 #t
602 ((+ k 1)) ))))
603
604 (define (ec-:vector-filter *len vecs)
605 (if (null? vecs)
606 '()
607 (if (zero? (*len (car vecs)))
608 (ec-:vector-filter *len (cdr vecs))
609 (cons (car vecs) (ec-:vector-filter *len (cdr vecs))) )))
610
611 ; Alternative: A simpler implementation for :vector uses vector->list
612 ; append and :list in the multi-argument case. Please refer to the
613 ; 'design.scm' for more details.
614
615
616 (define-syntax srfi-42-integers
617 (syntax-rules (index)
618 ((_ cc var (index i))
619 (srfi-42-do cc ((var 0) (i 0)) #t ((+ var 1) (+ i 1))) )
620 ((_ cc var)
621 (srfi-42-do cc ((var 0)) #t ((+ var 1))) )))
622
623 ;; Extended to support srfi-196 (data.range) <range> object.
624
625 (define-syntax srfi-42-range
626 (syntax-rules (index)
627
628 ; handle index variable and add optional args
629 ((_ cc var (index i) arg1 arg ...)
630 (srfi-42-parallel cc (srfi-42-range var arg1 arg ...) (srfi-42-integers i)) )
631 ((_ cc var arg1)
632 ;; Avoid (range? arg1), for we don't want to load data.range until
633 ;; absolutely necessary.
634 (if (exact-integer? arg1)
635 (srfi-42-range cc var 0 arg1 1)
636 (srfi-42-*vector range-length range-ref cc var arg1)) )
637 ((_ cc var arg1 arg2)
638 (srfi-42-range cc var arg1 arg2 1) )
639
640 ; special cases (partially evaluated by hand from general case)
641
642 ((_ cc var 0 arg2 1)
643 (srfi-42-do cc
644 (let ((b arg2))
645 (if (not (and (integer? b) (exact? b)))
646 (error
647 "arguments of :range are not exact integer "
648 "(use :real-range?)" 0 b 1 )))
649 ((var 0))
650 (< var b)
651 (let ())
652 #t
653 ((+ var 1)) ))
654
655 ((_ cc var 0 arg2 -1)
656 (srfi-42-do cc
657 (let ((b arg2))
658 (if (not (and (integer? b) (exact? b)))
659 (error
660 "arguments of :range are not exact integer "
661 "(use :real-range?)" 0 b 1 )))
662 ((var 0))
663 (> var b)
664 (let ())
665 #t
666 ((- var 1)) ))
667
668 ((_ cc var arg1 arg2 1)
669 (srfi-42-do cc
670 (let ((a arg1) (b arg2))
671 (if (not (and (integer? a) (exact? a)
672 (integer? b) (exact? b) ))
673 (error
674 "arguments of :range are not exact integer "
675 "(use :real-range?)" a b 1 )) )
676 ((var a))
677 (< var b)
678 (let ())
679 #t
680 ((+ var 1)) ))
681
682 ((_ cc var arg1 arg2 -1)
683 (srfi-42-do cc
684 (let ((a arg1) (b arg2) (s -1) (stop 0))
685 (if (not (and (integer? a) (exact? a)
686 (integer? b) (exact? b) ))
687 (error
688 "arguments of :range are not exact integer "
689 "(use :real-range?)" a b -1 )) )
690 ((var a))
691 (> var b)
692 (let ())
693 #t
694 ((- var 1)) ))
695
696 ; the general case
697
698 ((_ cc var arg1 arg2 arg3)
699 (srfi-42-do cc
700 (let ((a arg1) (b arg2) (s arg3) (stop 0))
701 (if (not (and (integer? a) (exact? a)
702 (integer? b) (exact? b)
703 (integer? s) (exact? s) ))
704 (error
705 "arguments of :range are not exact integer "
706 "(use :real-range?)" a b s ))
707 (if (zero? s)
708 (error "step size must not be zero in :range") )
709 (set! stop (+ a (* (max 0 (ceiling (/ (- b a) s))) s))))
710 ((var a))
711 (not (= var stop))
712 (let ())
713 #t
714 ((+ var s)) ))))
715
716 ; Comment: The macro :range inserts some code to make sure the values
717 ; are exact integers. This overhead has proven very helpful for
718 ; saving users from themselves.
719
720
721 (define-syntax srfi-42-real-range
722 (syntax-rules (index)
723
724 ; add optional args and index variable
725 ((_ cc var arg1)
726 (srfi-42-real-range cc var (index i) 0 arg1 1) )
727 ((_ cc var (index i) arg1)
728 (srfi-42-real-range cc var (index i) 0 arg1 1) )
729 ((_ cc var arg1 arg2)
730 (srfi-42-real-range cc var (index i) arg1 arg2 1) )
731 ((_ cc var (index i) arg1 arg2)
732 (srfi-42-real-range cc var (index i) arg1 arg2 1) )
733 ((_ cc var arg1 arg2 arg3)
734 (srfi-42-real-range cc var (index i) arg1 arg2 arg3) )
735
736 ; the fully qualified case
737 ((_ cc var (index i) arg1 arg2 arg3)
738 (srfi-42-do cc
739 (let ((a arg1) (b arg2) (s arg3) (istop 0))
740 (if (not (and (real? a) (real? b) (real? s)))
741 (error "arguments of :real-range are not real" a b s) )
742 (if (and (exact? a) (or (not (exact? b)) (not (exact? s))))
743 (set! a (exact->inexact a)) )
744 (set! istop (/ (- b a) s)) )
745 ((i 0))
746 (< i istop)
747 (let ((var (+ a (* s i)))))
748 #t
749 ((+ i 1)) ))))
750
751 ; Comment: The macro :real-range adapts the exactness of the start
752 ; value in case any of the other values is inexact. This is a
753 ; precaution to avoid (list-ec (: x 0 3.0) x) => '(0 1.0 2.0).
754
755
756 (define-syntax srfi-42-char-range
757 (syntax-rules (index)
758 ((_ cc var (index i) arg1 arg2)
759 (srfi-42-parallel cc (srfi-42-char-range var arg1 arg2) (srfi-42-integers i)) )
760 ((_ cc var arg1 arg2)
761 (srfi-42-do cc
762 (let ((imax (char->integer arg2))))
763 ((i (char->integer arg1)))
764 (<= i imax)
765 (let ((var (integer->char i))))
766 #t
767 ((+ i 1)) ))))
768
769 ; Warning: There is no R5RS-way to implement the :char-range generator
770 ; because the integers obtained by char->integer are not necessarily
771 ; consecutive. We simply assume this anyhow for illustration.
772
773
774 (define-syntax srfi-42-port
775 (syntax-rules (index)
776 [(_ cc var (index i) arg1 arg ...)
777 (srfi-42-parallel cc (srfi-42-port var arg1 arg ...) (srfi-42-integers i))]
778 [(_ cc var arg)
779 (srfi-42-port cc var arg read)]
780 [(_ cc var arg1 arg2)
781 (srfi-42-do cc
782 (let ((port arg1) (read-proc arg2)))
783 ((var (read-proc port)))
784 (not (eof-object? var))
785 (let ())
786 #t
787 ((read-proc port)) )]))
788
789 ;; Gauche extension
790 (define-syntax srfi-42-generator
791 (syntax-rules (index)
792 [(_ cc var (index i) expr)
793 (srfi-42-parallel cc (srfi-42-generator var expr) (srfi-42-integers i))]
794 [(_ cc var expr)
795 (srfi-42-do cc
796 (let ([gen expr]))
797 ([var (gen)])
798 (not (eof-object? var))
799 (let ())
800 #t
801 [(gen)])]))
802
803 ;; Gauche extension
804 (define-syntax srfi-42-collection
805 (syntax-rules (index)
806 [(_ cc var (index i) expr)
807 (srfi-42-parallel cc (srfi-42-collection var expr) (srfi-42-integers i))]
808 [(_ cc var expr)
809 (srfi-42-do cc
810 (let ([gen (x->generator expr)]))
811 ([var (gen)])
812 (not (eof-object? var))
813 (let ())
814 #t
815 [(gen)])]))
816
817 ; ==========================================================================
818 ; The typed generator :dispatched and utilities for constructing dispatchers
819 ; ==========================================================================
820
821 (define-syntax srfi-42-dispatched
822 (syntax-rules (index)
823 ((_ cc var (index i) dispatch arg1 arg ...)
824 (srfi-42-parallel cc
825 (srfi-42-integers i)
826 (srfi-42-dispatched var dispatch arg1 arg ...) ))
827 ((_ cc var dispatch arg1 arg ...)
828 (srfi-42-do cc
829 (let ((d dispatch)
830 (args (list arg1 arg ...))
831 (g #f)
832 (empty (list #f)) )
833 (set! g (d args))
834 (if (not (procedure? g))
835 (error "unrecognized arguments in dispatching"
836 args
837 (d '()) )))
838 ((var (g empty)))
839 (not (eq? var empty))
840 (let ())
841 #t
842 ((g empty)) ))))
843
844 ; Comment: The unique object empty is created as a newly allocated
845 ; non-empty list. It is compared using eq? which distinguishes
846 ; the object from any other object, according to R5RS 6.1.
847
848
849 (define-syntax srfi-42-generator-proc
850 ;;(syntax-rules (:do let)
851 (syntax-rules (srfi-42-do let)
852
853 ; call g with a variable, reentry at (**)
854 ((_ (g arg ...))
855 (g (srfi-42-generator-proc var) var arg ...) )
856
857 ; reentry point (**) -> make the code from a single :do
858 ((_
859 var
860 (srfi-42-do (let obs oc ...)
861 ((lv li) ...)
862 ne1?
863 (let ((i v) ...) ic ...)
864 ne2?
865 (ls ...)) )
866 (ec-simplify
867 (let obs
868 oc ...
869 (let ((lv li) ... (ne2 #t))
870 (ec-simplify
871 (let ((i #f) ...) ; v not yet valid
872 (lambda (empty)
873 (if (and ne1? ne2)
874 (ec-simplify
875 (begin
876 (set! i v) ...
877 ic ...
878 (let ((value var))
879 (ec-simplify
880 (if ne2?
881 (ec-simplify
882 (begin (set! lv ls) ...) )
883 (set! ne2 #f) ))
884 value )))
885 empty ))))))))
886
887 ; silence warnings of some macro expanders
888 ((_ var)
889 (error "illegal macro call") )))
890
891
892 (define (dispatch-union d1 d2)
893 (lambda (args)
894 (let ((g1 (d1 args)) (g2 (d2 args)))
895 (if g1
896 (if g2
897 (if (null? args)
898 (append (if (list? g1) g1 (list g1))
899 (if (list? g2) g2 (list g2)) )
900 (error "dispatching conflict" args (d1 '()) (d2 '())) )
901 g1 )
902 (if g2 g2 #f) ))))
903
904
905 ; ==========================================================================
906 ; The dispatching generator :
907 ; ==========================================================================
908
909 (define (make-initial-:-dispatch)
910 (lambda (args)
911 (case (length args)
912 [(0) 'SRFI42]
913 [(1) (let ([a1 (car args)])
914 (cond
915 [(list? a1)
916 (srfi-42-generator-proc (srfi-42-list a1))]
917 [(string? a1)
918 (srfi-42-generator-proc (srfi-42-string a1))]
919 [(vector? a1)
920 (srfi-42-generator-proc (srfi-42-vector a1))]
921 [(uvector? a1)
922 (srfi-42-generator-proc (srfi-42-uvector a1))]
923 [(and (integer? a1) (exact? a1))
924 (srfi-42-generator-proc (srfi-42-range a1))]
925 [(real? a1)
926 (srfi-42-generator-proc (srfi-42-real-range a1))]
927 [(input-port? a1)
928 (srfi-42-generator-proc (srfi-42-port a1))]
929 [(is-a? a1 <collection>)
930 (srfi-42-generator-proc (srfi-42-collection a1))]
931 [(applicable? a1)
932 (srfi-42-generator-proc (srfi-42-generator a1))]
933 ;; This would trigger autoloading data.range, so we don't
934 ;; support it for now.
935 ;; [(range? a1)
936 ;; (srfi-42-generator-proc (srfi-42-range a1))]
937 [else #f]))]
938 [(2) (let ([a1 (car args)] [a2 (cadr args)])
939 (cond
940 [(and (list? a1) (list? a2))
941 (srfi-42-generator-proc (srfi-42-list a1 a2)) ]
942 [(and (string? a1) (string? a1))
943 (srfi-42-generator-proc (srfi-42-string a1 a2)) ]
944 [(and (vector? a1) (vector? a2))
945 (srfi-42-generator-proc (srfi-42-vector a1 a2)) ]
946 [(and (uvector? a1) (uvector? a2))
947 (srfi-42-generator-proc (srfi-42-uvector a1 a2)) ]
948 [(and (integer? a1) (exact? a1) (integer? a2) (exact? a2))
949 (srfi-42-generator-proc (srfi-42-range a1 a2)) ]
950 [(and (real? a1) (real? a2))
951 (srfi-42-generator-proc (srfi-42-real-range a1 a2)) ]
952 [(and (char? a1) (char? a2))
953 (srfi-42-generator-proc (srfi-42-char-range a1 a2)) ]
954 [(and (input-port? a1) (procedure? a2))
955 (srfi-42-generator-proc (srfi-42-port a1 a2)) ]
956 [else #f]))]
957 [(3) (let ([a1 (car args)] [a2 (cadr args)] [a3 (caddr args)])
958 (cond
959 [(and (list? a1) (list? a2) (list? a3))
960 (srfi-42-generator-proc (srfi-42-list a1 a2 a3)) ]
961 [(and (string? a1) (string? a1) (string? a3))
962 (srfi-42-generator-proc (srfi-42-string a1 a2 a3)) ]
963 [(and (vector? a1) (vector? a2) (vector? a3))
964 (srfi-42-generator-proc (srfi-42-vector a1 a2 a3)) ]
965 [(and (uvector? a1) (uvector? a2) (uvector? a3))
966 (srfi-42-generator-proc (srfi-42-uvector a1 a2 a3)) ]
967 [(and (integer? a1) (exact? a1)
968 (integer? a2) (exact? a2)
969 (integer? a3) (exact? a3))
970 (srfi-42-generator-proc (srfi-42-range a1 a2 a3)) ]
971 [(and (real? a1) (real? a2) (real? a3))
972 (srfi-42-generator-proc (srfi-42-real-range a1 a2 a3)) ]
973 [else #f]))]
974 [else (cond
975 [(every list? args)
976 (srfi-42-generator-proc (srfi-42-list (apply append args))) ]
977 [(every string? args)
978 (srfi-42-generator-proc (srfi-42-string (apply string-append args)))]
979 [(every vector? args)
980 (srfi-42-generator-proc (srfi-42-vector (apply vector-append args)))]
981 [(every uvector? args)
982 (srfi-42-generator-proc (srfi-42-list (apply append (map uvector->list args))))]
983 [else #f])])))
984
985 (define srfi-42--dispatch
986 (make-initial-:-dispatch) )
987
988 (define (srfi-42--dispatch-ref)
989 srfi-42--dispatch )
990
991 (define (srfi-42--dispatch-set! dispatch)
992 (if (not (procedure? dispatch))
993 (error "not a procedure" dispatch) )
994 (set! srfi-42--dispatch dispatch) )
995
996 (define-syntax srfi-42-
997 (syntax-rules (index)
998 ((_ cc var (index i) arg1 arg ...)
999 (srfi-42-dispatched cc var (index i) srfi-42--dispatch arg1 arg ...) )
1000 ((_ cc var arg1 arg ...)
1001 (srfi-42-dispatched cc var srfi-42--dispatch arg1 arg ...) )))
1002
1003
1004 ; ==========================================================================
1005 ; The utility comprehensions fold-ec, fold3-ec
1006 ; ==========================================================================
1007
1008 (define-syntax fold3-ec
1009 (syntax-rules (nested)
1010 ((fold3-ec x0 (nested q1 ...) q etc1 etc2 etc3 etc ...)
1011 (fold3-ec x0 (nested q1 ... q) etc1 etc2 etc3 etc ...) )
1012 ((fold3-ec x0 q1 q2 etc1 etc2 etc3 etc ...)
1013 (fold3-ec x0 (nested q1 q2) etc1 etc2 etc3 etc ...) )
1014 ((fold3-ec x0 expression f1 f2)
1015 (fold3-ec x0 (nested) expression f1 f2) )
1016
1017 ((fold3-ec x0 qualifier expression f1 f2)
1018 (let ((result #f) (empty #t))
1019 (do-ec qualifier
1020 (let ((value expression)) ; don't duplicate
1021 (if empty
1022 (begin (set! result (f1 value))
1023 (set! empty #f) )
1024 (set! result (f2 value result)) )))
1025 (if empty x0 result) ))))
1026
1027
1028 (define-syntax fold-ec
1029 (syntax-rules (nested)
1030 ((fold-ec x0 (nested q1 ...) q etc1 etc2 etc ...)
1031 (fold-ec x0 (nested q1 ... q) etc1 etc2 etc ...) )
1032 ((fold-ec x0 q1 q2 etc1 etc2 etc ...)
1033 (fold-ec x0 (nested q1 q2) etc1 etc2 etc ...) )
1034 ((fold-ec x0 expression f2)
1035 (fold-ec x0 (nested) expression f2) )
1036
1037 ((fold-ec x0 qualifier expression f2)
1038 (let ((result x0))
1039 (do-ec qualifier (set! result (f2 expression result)))
1040 result ))))
1041
1042
1043 ; ==========================================================================
1044 ; The comprehensions list-ec string-ec vector-ec etc.
1045 ; ==========================================================================
1046
1047 (define-syntax list-ec
1048 (syntax-rules ()
1049 ((list-ec etc1 etc ...)
1050 (reverse (fold-ec '() etc1 etc ... cons)) )))
1051
1052 ; Alternative: Reverse can safely be replaced by reverse! if you have it.
1053 ;
1054 ; Alternative: It is possible to construct the result in the correct order
1055 ; using set-cdr! to add at the tail. This removes the overhead of copying
1056 ; at the end, at the cost of more book-keeping.
1057
1058
1059 (define-syntax append-ec
1060 (syntax-rules ()
1061 ((append-ec etc1 etc ...)
1062 (apply append (list-ec etc1 etc ...)) )))
1063
1064 (define-syntax string-ec
1065 (syntax-rules ()
1066 ((string-ec etc1 etc ...)
1067 (list->string (list-ec etc1 etc ...)) )))
1068
1069 ; Alternative: For very long strings, the intermediate list may be a
1070 ; problem. A more space-aware implementation collect the characters
1071 ; in an intermediate list and when this list becomes too large it is
1072 ; converted into an intermediate string. At the end, the intermediate
1073 ; strings are concatenated with string-append.
1074
1075
1076 (define-syntax string-append-ec
1077 (syntax-rules ()
1078 ((string-append-ec etc1 etc ...)
1079 (apply string-append (list-ec etc1 etc ...)) )))
1080
1081 (define-syntax vector-ec
1082 (syntax-rules ()
1083 ((vector-ec etc1 etc ...)
1084 (list->vector (list-ec etc1 etc ...)) )))
1085
1086 ; Comment: A similar approach as for string-ec can be used for vector-ec.
1087 ; However, the space overhead for the intermediate list is much lower
1088 ; than for string-ec and as there is no vector-append, the intermediate
1089 ; vectors must be copied explicitly.
1090
1091 (define-syntax vector-of-length-ec
1092 (syntax-rules (nested)
1093 ((vector-of-length-ec k (nested q1 ...) q etc1 etc ...)
1094 (vector-of-length-ec k (nested q1 ... q) etc1 etc ...) )
1095 ((vector-of-length-ec k q1 q2 etc1 etc ...)
1096 (vector-of-length-ec k (nested q1 q2) etc1 etc ...) )
1097 ((vector-of-length-ec k expression)
1098 (vector-of-length-ec k (nested) expression) )
1099
1100 ((vector-of-length-ec k qualifier expression)
1101 (let ((len k))
1102 (let ((vec (make-vector len))
1103 (i 0) )
1104 (do-ec qualifier
1105 (if (< i len)
1106 (begin (vector-set! vec i expression)
1107 (set! i (+ i 1)) )
1108 (error "vector is too short for the comprehension") ))
1109 (if (= i len)
1110 vec
1111 (error "vector is too long for the comprehension") ))))))
1112
1113
1114 (define-syntax sum-ec
1115 (syntax-rules ()
1116 ((sum-ec etc1 etc ...)
1117 (fold-ec (+) etc1 etc ... +) )))
1118
1119 (define-syntax product-ec
1120 (syntax-rules ()
1121 ((product-ec etc1 etc ...)
1122 (fold-ec (*) etc1 etc ... *) )))
1123
1124 (define-syntax min-ec
1125 (syntax-rules ()
1126 ((min-ec etc1 etc ...)
1127 (fold3-ec (min) etc1 etc ... min min) )))
1128
1129 (define-syntax max-ec
1130 (syntax-rules ()
1131 ((max-ec etc1 etc ...)
1132 (fold3-ec (max) etc1 etc ... max max) )))
1133
1134 (define-syntax last-ec
1135 (syntax-rules (nested)
1136 ((last-ec default (nested q1 ...) q etc1 etc ...)
1137 (last-ec default (nested q1 ... q) etc1 etc ...) )
1138 ((last-ec default q1 q2 etc1 etc ...)
1139 (last-ec default (nested q1 q2) etc1 etc ...) )
1140 ((last-ec default expression)
1141 (last-ec default (nested) expression) )
1142
1143 ((last-ec default qualifier expression)
1144 (let ((result default))
1145 (do-ec qualifier (set! result expression))
1146 result ))))
1147
1148
1149 ; ==========================================================================
1150 ; The fundamental early-stopping comprehension first-ec
1151 ; ==========================================================================
1152
1153 (define-syntax first-ec
1154 (syntax-rules ()
1155 ((first-ec expr ...)
1156 (%replace-keywords %first-ec () expr ...))))
1157
1158 (define-syntax %first-ec
1159 (syntax-rules (nested)
1160 ((%first-ec default (nested q1 ...) q etc1 etc ...)
1161 (%first-ec default (nested q1 ... q) etc1 etc ...) )
1162 ((%first-ec default q1 q2 etc1 etc ...)
1163 (%first-ec default (nested q1 q2) etc1 etc ...) )
1164 ((%first-ec default expression)
1165 (%first-ec default (nested) expression) )
1166
1167 ((%first-ec default qualifier expression)
1168 (let ((result default) (stop #f))
1169 (ec-guarded-do-ec
1170 stop
1171 (nested qualifier)
1172 (begin (set! result expression)
1173 (set! stop #t) ))
1174 result ))))
1175
1176 ; (ec-guarded-do-ec stop (nested q ...) cmd)
1177 ; constructs (do-ec q ... cmd) where the generators gen in q ... are
1178 ; replaced by (:until gen stop).
1179
1180 (define-syntax ec-guarded-do-ec
1181 (syntax-rules ()
1182 ((ec-guarded-do-ec expr ...)
1183 (%replace-keywords %ec-guarded-do-ec () expr ...))))
1184
1185 (define-syntax %ec-guarded-do-ec
1186 (syntax-rules (nested if not and or begin)
1187
1188 ((ec-guarded-do-ec stop (nested (nested q1 ...) q2 ...) cmd)
1189 (ec-guarded-do-ec stop (nested q1 ... q2 ...) cmd) )
1190
1191 ((ec-guarded-do-ec stop (nested (if test) q ...) cmd)
1192 (if test (ec-guarded-do-ec stop (nested q ...) cmd)) )
1193 ((ec-guarded-do-ec stop (nested (not test) q ...) cmd)
1194 (if (not test) (ec-guarded-do-ec stop (nested q ...) cmd)) )
1195 ((ec-guarded-do-ec stop (nested (and test ...) q ...) cmd)
1196 (if (and test ...) (ec-guarded-do-ec stop (nested q ...) cmd)) )
1197 ((ec-guarded-do-ec stop (nested (or test ...) q ...) cmd)
1198 (if (or test ...) (ec-guarded-do-ec stop (nested q ...) cmd)) )
1199
1200 ((ec-guarded-do-ec stop (nested (begin etc ...) q ...) cmd)
1201 (begin etc ... (ec-guarded-do-ec stop (nested q ...) cmd)) )
1202
1203 ((ec-guarded-do-ec stop (nested gen q ...) cmd)
1204 (do-ec
1205 (srfi-42-until gen stop)
1206 (ec-guarded-do-ec stop (nested q ...) cmd) ))
1207
1208 ((ec-guarded-do-ec stop (nested) cmd)
1209 (do-ec cmd) )))
1210
1211 ; Alternative: Instead of modifying the generator with :until, it is
1212 ; possible to use call-with-current-continuation:
1213 ;
1214 ; (define-syntax first-ec
1215 ; ...same as above...
1216 ; ((first-ec default qualifier expression)
1217 ; (call-with-current-continuation
1218 ; (lambda (cc)
1219 ; (do-ec qualifier (cc expression))
1220 ; default ))) ))
1221 ;
1222 ; This is much simpler but not necessarily as efficient.
1223
1224
1225 ; ==========================================================================
1226 ; The early-stopping comprehensions any?-ec every?-ec
1227 ; ==========================================================================
1228
1229 (define-syntax any?-ec
1230 (syntax-rules (nested)
1231 ((any?-ec (nested q1 ...) q etc1 etc ...)
1232 (any?-ec (nested q1 ... q) etc1 etc ...) )
1233 ((any?-ec q1 q2 etc1 etc ...)
1234 (any?-ec (nested q1 q2) etc1 etc ...) )
1235 ((any?-ec expression)
1236 (any?-ec (nested) expression) )
1237
1238 ((any?-ec qualifier expression)
1239 (first-ec #f qualifier (if expression) #t) )))
1240
1241 (define-syntax every?-ec
1242 (syntax-rules (nested)
1243 ((every?-ec (nested q1 ...) q etc1 etc ...)
1244 (every?-ec (nested q1 ... q) etc1 etc ...) )
1245 ((every?-ec q1 q2 etc1 etc ...)
1246 (every?-ec (nested q1 q2) etc1 etc ...) )
1247 ((every?-ec expression)
1248 (every?-ec (nested) expression) )
1249
1250 ((every?-ec qualifier expression)
1251 (first-ec #t qualifier (if (not expression)) #f) )))