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