srfi-42.scm 33 KB

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