ec.scm 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. ; <PLAINTEXT>
  2. ; Eager Comprehensions in [outer..inner|expr]-Convention
  3. ; ======================================================
  4. ;
  5. ; sebastian.egner@philips.com, 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. ; 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. ; Bug #1:
  227. ; Unfortunately, this code is wrong because ne1? may depend
  228. ; in the inner bindings introduced in ilet, but ne1? is evaluated
  229. ; outside of the inner bindings. (Refer to the specification of
  230. ; :do to see the structure.)
  231. ; The problem manifests itself (as sunnan@handgranat.org
  232. ; observed, 25-Apr-2005) when the :list-generator is modified:
  233. ;
  234. ; (do-ec (:while (:list x '(1 2)) (= x 1)) (display x)).
  235. ;
  236. ; In order to generate proper code, we introduce temporary
  237. ; variables saving the values of the inner bindings. The inner
  238. ; bindings are executed in a new ne1?, which also evaluates ne1?
  239. ; outside the scope of the inner bindings, then the inner commands
  240. ; are executed (possibly changing the variables), and then the
  241. ; values of the inner bindings are saved and (and ne1? test) is
  242. ; returned. In the new ilet, the inner variables are bound and
  243. ; initialized and their values are restored. So we construct:
  244. ;
  245. ; (let (ob .. (ib-tmp #f) ...)
  246. ; oc ...
  247. ; (let loop (lb ...)
  248. ; (if (let (ne1?-value ne1?)
  249. ; (let ((ib-var ib-rhs) ...)
  250. ; ic ...
  251. ; (set! ib-tmp ib-var) ...)
  252. ; (and ne1?-value test))
  253. ; (let ((ib-var ib-tmp) ...)
  254. ; /payload/
  255. ; (if ne2?
  256. ; (loop ls ...) )))))
  257. ;
  258. ; Bug #2:
  259. ; Unfortunately, the above expansion is still incorrect (as Jens-Axel
  260. ; Soegaard pointed out, 4-Jun-2007) because ib-rhs are evaluated even
  261. ; if ne1?-value is #f, indicating that the loop has ended.
  262. ; The problem manifests itself in the following example:
  263. ;
  264. ; (do-ec (:while (:list x '(1)) #t) (display x))
  265. ;
  266. ; Which iterates :list beyond exhausting the list '(1).
  267. ;
  268. ; For the fix, we follow Jens-Axel's approach of guarding the evaluation
  269. ; of ib-rhs with a check on ne1?-value.
  270. (define-syntax :while-1
  271. (syntax-rules (:do let)
  272. ((:while-1 cc test (:do olet lbs ne1? ilet ne2? lss))
  273. (:while-2 cc test () () () (:do olet lbs ne1? ilet ne2? lss)))))
  274. (define-syntax :while-2
  275. (syntax-rules (:do let)
  276. ((:while-2 cc
  277. test
  278. (ib-let ...)
  279. (ib-save ...)
  280. (ib-restore ...)
  281. (:do olet
  282. lbs
  283. ne1?
  284. (let ((ib-var ib-rhs) ib ...) ic ...)
  285. ne2?
  286. lss))
  287. (:while-2 cc
  288. test
  289. (ib-let ... (ib-tmp #f))
  290. (ib-save ... (ib-var ib-rhs))
  291. (ib-restore ... (ib-var ib-tmp))
  292. (:do olet
  293. lbs
  294. ne1?
  295. (let (ib ...) ic ... (set! ib-tmp ib-var))
  296. ne2?
  297. lss)))
  298. ((:while-2 cc
  299. test
  300. (ib-let ...)
  301. (ib-save ...)
  302. (ib-restore ...)
  303. (:do (let (ob ...) oc ...) lbs ne1? (let () ic ...) ne2? lss))
  304. (:do cc
  305. (let (ob ... ib-let ...) oc ...)
  306. lbs
  307. (let ((ne1?-value ne1?))
  308. (and ne1?-value
  309. (let (ib-save ...)
  310. ic ...
  311. test)))
  312. (let (ib-restore ...))
  313. ne2?
  314. lss))))
  315. (define-syntax :until
  316. (syntax-rules ()
  317. ((:until cc (g arg1 arg ...) test)
  318. (g (:until-1 cc test) arg1 arg ...) )))
  319. (define-syntax :until-1
  320. (syntax-rules (:do)
  321. ((:until-1 cc test (:do olet lbs ne1? ilet ne2? lss))
  322. (:do cc olet lbs ne1? ilet (and ne2? (not test)) lss) )))
  323. ; ==========================================================================
  324. ; The typed generators :list :string :vector etc.
  325. ; ==========================================================================
  326. (define-syntax :list
  327. (syntax-rules (index)
  328. ((:list cc var (index i) arg ...)
  329. (:parallel cc (:list var arg ...) (:integers i)) )
  330. ((:list cc var arg1 arg2 arg ...)
  331. (:list cc var (append arg1 arg2 arg ...)) )
  332. ((:list cc var arg)
  333. (:do cc
  334. (let ())
  335. ((t arg))
  336. (not (null? t))
  337. (let ((var (car t))))
  338. #t
  339. ((cdr t)) ))))
  340. (define-syntax :string
  341. (syntax-rules (index)
  342. ((:string cc var (index i) arg)
  343. (:do cc
  344. (let ((str arg) (len 0))
  345. (set! len (string-length str)))
  346. ((i 0))
  347. (< i len)
  348. (let ((var (string-ref str i))))
  349. #t
  350. ((+ i 1)) ))
  351. ((:string cc var (index i) arg1 arg2 arg ...)
  352. (:string cc var (index i) (string-append arg1 arg2 arg ...)) )
  353. ((:string cc var arg1 arg ...)
  354. (:string cc var (index i) arg1 arg ...) )))
  355. ; Alternative: An implementation in the style of :vector can also
  356. ; be used for :string. However, it is less interesting as the
  357. ; overhead of string-append is much less than for 'vector-append'.
  358. (define-syntax :vector
  359. (syntax-rules (index)
  360. ((:vector cc var arg)
  361. (:vector cc var (index i) arg) )
  362. ((:vector cc var (index i) arg)
  363. (:do cc
  364. (let ((vec arg) (len 0))
  365. (set! len (vector-length vec)))
  366. ((i 0))
  367. (< i len)
  368. (let ((var (vector-ref vec i))))
  369. #t
  370. ((+ i 1)) ))
  371. ((:vector cc var (index i) arg1 arg2 arg ...)
  372. (:parallel cc (:vector cc var arg1 arg2 arg ...) (:integers i)) )
  373. ((:vector cc var arg1 arg2 arg ...)
  374. (:do cc
  375. (let ((vec #f)
  376. (len 0)
  377. (vecs (ec-:vector-filter (list arg1 arg2 arg ...))) ))
  378. ((k 0))
  379. (if (< k len)
  380. #t
  381. (if (null? vecs)
  382. #f
  383. (begin (set! vec (car vecs))
  384. (set! vecs (cdr vecs))
  385. (set! len (vector-length vec))
  386. (set! k 0)
  387. #t )))
  388. (let ((var (vector-ref vec k))))
  389. #t
  390. ((+ k 1)) ))))
  391. (define (ec-:vector-filter vecs)
  392. (if (null? vecs)
  393. '()
  394. (if (zero? (vector-length (car vecs)))
  395. (ec-:vector-filter (cdr vecs))
  396. (cons (car vecs) (ec-:vector-filter (cdr vecs))) )))
  397. ; Alternative: A simpler implementation for :vector uses vector->list
  398. ; append and :list in the multi-argument case. Please refer to the
  399. ; 'design.scm' for more details.
  400. (define-syntax :integers
  401. (syntax-rules (index)
  402. ((:integers cc var (index i))
  403. (:do cc ((var 0) (i 0)) #t ((+ var 1) (+ i 1))) )
  404. ((:integers cc var)
  405. (:do cc ((var 0)) #t ((+ var 1))) )))
  406. (define-syntax :range
  407. (syntax-rules (index)
  408. ; handle index variable and add optional args
  409. ((:range cc var (index i) arg1 arg ...)
  410. (:parallel cc (:range var arg1 arg ...) (:integers i)) )
  411. ((:range cc var arg1)
  412. (:range cc var 0 arg1 1) )
  413. ((:range cc var arg1 arg2)
  414. (:range cc var arg1 arg2 1) )
  415. ; special cases (partially evaluated by hand from general case)
  416. ((:range cc var 0 arg2 1)
  417. (:do cc
  418. (let ((b arg2))
  419. (if (not (and (integer? b) (exact? b)))
  420. (error
  421. "arguments of :range are not exact integer "
  422. "(use :real-range?)" 0 b 1 )))
  423. ((var 0))
  424. (< var b)
  425. (let ())
  426. #t
  427. ((+ var 1)) ))
  428. ((:range cc var 0 arg2 -1)
  429. (:do cc
  430. (let ((b arg2))
  431. (if (not (and (integer? b) (exact? b)))
  432. (error
  433. "arguments of :range are not exact integer "
  434. "(use :real-range?)" 0 b 1 )))
  435. ((var 0))
  436. (> var b)
  437. (let ())
  438. #t
  439. ((- var 1)) ))
  440. ((:range cc var arg1 arg2 1)
  441. (:do cc
  442. (let ((a arg1) (b arg2))
  443. (if (not (and (integer? a) (exact? a)
  444. (integer? b) (exact? b) ))
  445. (error
  446. "arguments of :range are not exact integer "
  447. "(use :real-range?)" a b 1 )) )
  448. ((var a))
  449. (< var b)
  450. (let ())
  451. #t
  452. ((+ var 1)) ))
  453. ((:range cc var arg1 arg2 -1)
  454. (:do cc
  455. (let ((a arg1) (b arg2) (s -1) (stop 0))
  456. (if (not (and (integer? a) (exact? a)
  457. (integer? b) (exact? b) ))
  458. (error
  459. "arguments of :range are not exact integer "
  460. "(use :real-range?)" a b -1 )) )
  461. ((var a))
  462. (> var b)
  463. (let ())
  464. #t
  465. ((- var 1)) ))
  466. ; the general case
  467. ((:range cc var arg1 arg2 arg3)
  468. (:do cc
  469. (let ((a arg1) (b arg2) (s arg3) (stop 0))
  470. (if (not (and (integer? a) (exact? a)
  471. (integer? b) (exact? b)
  472. (integer? s) (exact? s) ))
  473. (error
  474. "arguments of :range are not exact integer "
  475. "(use :real-range?)" a b s ))
  476. (if (zero? s)
  477. (error "step size must not be zero in :range") )
  478. (set! stop (+ a (* (max 0 (ceiling (/ (- b a) s))) s))) )
  479. ((var a))
  480. (not (= var stop))
  481. (let ())
  482. #t
  483. ((+ var s)) ))))
  484. ; Comment: The macro :range inserts some code to make sure the values
  485. ; are exact integers. This overhead has proven very helpful for
  486. ; saving users from themselves.
  487. (define-syntax :real-range
  488. (syntax-rules (index)
  489. ; add optional args and index variable
  490. ((:real-range cc var arg1)
  491. (:real-range cc var (index i) 0 arg1 1) )
  492. ((:real-range cc var (index i) arg1)
  493. (:real-range cc var (index i) 0 arg1 1) )
  494. ((:real-range cc var arg1 arg2)
  495. (:real-range cc var (index i) arg1 arg2 1) )
  496. ((:real-range cc var (index i) arg1 arg2)
  497. (:real-range cc var (index i) arg1 arg2 1) )
  498. ((:real-range cc var arg1 arg2 arg3)
  499. (:real-range cc var (index i) arg1 arg2 arg3) )
  500. ; the fully qualified case
  501. ((:real-range cc var (index i) arg1 arg2 arg3)
  502. (:do cc
  503. (let ((a arg1) (b arg2) (s arg3) (istop 0))
  504. (if (not (and (real? a) (real? b) (real? s)))
  505. (error "arguments of :real-range are not real" a b s) )
  506. (if (and (exact? a) (or (not (exact? b)) (not (exact? s))))
  507. (set! a (exact->inexact a)) )
  508. (set! istop (/ (- b a) s)) )
  509. ((i 0))
  510. (< i istop)
  511. (let ((var (+ a (* s i)))))
  512. #t
  513. ((+ i 1)) ))))
  514. ; Comment: The macro :real-range adapts the exactness of the start
  515. ; value in case any of the other values is inexact. This is a
  516. ; precaution to avoid (list-ec (: x 0 3.0) x) => '(0 1.0 2.0).
  517. (define-syntax :char-range
  518. (syntax-rules (index)
  519. ((:char-range cc var (index i) arg1 arg2)
  520. (:parallel cc (:char-range var arg1 arg2) (:integers i)) )
  521. ((:char-range cc var arg1 arg2)
  522. (:do cc
  523. (let ((imax (char->integer arg2))))
  524. ((i (char->integer arg1)))
  525. (<= i imax)
  526. (let ((var (integer->char i))))
  527. #t
  528. ((+ i 1)) ))))
  529. ; Warning: There is no R5RS-way to implement the :char-range generator
  530. ; because the integers obtained by char->integer are not necessarily
  531. ; consecutive. We simply assume this anyhow for illustration.
  532. (define-syntax :port
  533. (syntax-rules (index)
  534. ((:port cc var (index i) arg1 arg ...)
  535. (:parallel cc (:port var arg1 arg ...) (:integers i)) )
  536. ((:port cc var arg)
  537. (:port cc var arg read) )
  538. ((:port cc var arg1 arg2)
  539. (:do cc
  540. (let ((port arg1) (read-proc arg2)))
  541. ((var (read-proc port)))
  542. (not (eof-object? var))
  543. (let ())
  544. #t
  545. ((read-proc port)) ))))
  546. ; ==========================================================================
  547. ; The typed generator :dispatched and utilities for constructing dispatchers
  548. ; ==========================================================================
  549. (define-syntax :dispatched
  550. (syntax-rules (index)
  551. ((:dispatched cc var (index i) dispatch arg1 arg ...)
  552. (:parallel cc
  553. (:integers i)
  554. (:dispatched var dispatch arg1 arg ...) ))
  555. ((:dispatched cc var dispatch arg1 arg ...)
  556. (:do cc
  557. (let ((d dispatch)
  558. (args (list arg1 arg ...))
  559. (g #f)
  560. (empty (list #f)) )
  561. (set! g (d args))
  562. (if (not (procedure? g))
  563. (error "unrecognized arguments in dispatching"
  564. args
  565. (d '()) )))
  566. ((var (g empty)))
  567. (not (eq? var empty))
  568. (let ())
  569. #t
  570. ((g empty)) ))))
  571. ; Comment: The unique object empty is created as a newly allocated
  572. ; non-empty list. It is compared using eq? which distinguishes
  573. ; the object from any other object, according to R5RS 6.1.
  574. (define-syntax :generator-proc
  575. (syntax-rules (:do let)
  576. ; call g with a variable, reentry at (**)
  577. ((:generator-proc (g arg ...))
  578. (g (:generator-proc var) var arg ...) )
  579. ; reentry point (**) -> make the code from a single :do
  580. ((:generator-proc
  581. var
  582. (:do (let obs oc ...)
  583. ((lv li) ...)
  584. ne1?
  585. (let ((i v) ...) ic ...)
  586. ne2?
  587. (ls ...)) )
  588. (ec-simplify
  589. (let obs
  590. oc ...
  591. (let ((lv li) ... (ne2 #t))
  592. (ec-simplify
  593. (let ((i #f) ...) ; v not yet valid
  594. (lambda (empty)
  595. (if (and ne1? ne2)
  596. (ec-simplify
  597. (begin
  598. (set! i v) ...
  599. ic ...
  600. (let ((value var))
  601. (ec-simplify
  602. (if ne2?
  603. (ec-simplify
  604. (begin (set! lv ls) ...) )
  605. (set! ne2 #f) ))
  606. value )))
  607. empty ))))))))
  608. ; silence warnings of some macro expanders
  609. ((:generator-proc var)
  610. (error "illegal macro call") )))
  611. (define (dispatch-union d1 d2)
  612. (lambda (args)
  613. (let ((g1 (d1 args)) (g2 (d2 args)))
  614. (if g1
  615. (if g2
  616. (if (null? args)
  617. (append (if (list? g1) g1 (list g1))
  618. (if (list? g2) g2 (list g2)) )
  619. (error "dispatching conflict" args (d1 '()) (d2 '())) )
  620. g1 )
  621. (if g2 g2 #f) ))))
  622. ; ==========================================================================
  623. ; The dispatching generator :
  624. ; ==========================================================================
  625. (define (make-initial-:-dispatch)
  626. (lambda (args)
  627. (case (length args)
  628. ((0) 'SRFI42)
  629. ((1) (let ((a1 (car args)))
  630. (cond
  631. ((list? a1)
  632. (:generator-proc (:list a1)) )
  633. ((string? a1)
  634. (:generator-proc (:string a1)) )
  635. ((vector? a1)
  636. (:generator-proc (:vector a1)) )
  637. ((and (integer? a1) (exact? a1))
  638. (:generator-proc (:range a1)) )
  639. ((real? a1)
  640. (:generator-proc (:real-range a1)) )
  641. ((input-port? a1)
  642. (:generator-proc (:port a1)) )
  643. (else
  644. #f ))))
  645. ((2) (let ((a1 (car args)) (a2 (cadr args)))
  646. (cond
  647. ((and (list? a1) (list? a2))
  648. (:generator-proc (:list a1 a2)) )
  649. ((and (string? a1) (string? a1))
  650. (:generator-proc (:string a1 a2)) )
  651. ((and (vector? a1) (vector? a2))
  652. (:generator-proc (:vector a1 a2)) )
  653. ((and (integer? a1) (exact? a1) (integer? a2) (exact? a2))
  654. (:generator-proc (:range a1 a2)) )
  655. ((and (real? a1) (real? a2))
  656. (:generator-proc (:real-range a1 a2)) )
  657. ((and (char? a1) (char? a2))
  658. (:generator-proc (:char-range a1 a2)) )
  659. ((and (input-port? a1) (procedure? a2))
  660. (:generator-proc (:port a1 a2)) )
  661. (else
  662. #f ))))
  663. ((3) (let ((a1 (car args)) (a2 (cadr args)) (a3 (caddr args)))
  664. (cond
  665. ((and (list? a1) (list? a2) (list? a3))
  666. (:generator-proc (:list a1 a2 a3)) )
  667. ((and (string? a1) (string? a1) (string? a3))
  668. (:generator-proc (:string a1 a2 a3)) )
  669. ((and (vector? a1) (vector? a2) (vector? a3))
  670. (:generator-proc (:vector a1 a2 a3)) )
  671. ((and (integer? a1) (exact? a1)
  672. (integer? a2) (exact? a2)
  673. (integer? a3) (exact? a3))
  674. (:generator-proc (:range a1 a2 a3)) )
  675. ((and (real? a1) (real? a2) (real? a3))
  676. (:generator-proc (:real-range a1 a2 a3)) )
  677. (else
  678. #f ))))
  679. (else
  680. (letrec ((every?
  681. (lambda (pred args)
  682. (if (null? args)
  683. #t
  684. (and (pred (car args))
  685. (every? pred (cdr args)) )))))
  686. (cond
  687. ((every? list? args)
  688. (:generator-proc (:list (apply append args))) )
  689. ((every? string? args)
  690. (:generator-proc (:string (apply string-append args))) )
  691. ((every? vector? args)
  692. (:generator-proc (:list (apply append (map vector->list args)))) )
  693. (else
  694. #f )))))))
  695. (define :-dispatch
  696. (make-initial-:-dispatch) )
  697. (define (:-dispatch-ref)
  698. :-dispatch )
  699. (define (:-dispatch-set! dispatch)
  700. (if (not (procedure? dispatch))
  701. (error "not a procedure" dispatch) )
  702. (set! :-dispatch dispatch) )
  703. (define-syntax :
  704. (syntax-rules (index)
  705. ((: cc var (index i) arg1 arg ...)
  706. (:dispatched cc var (index i) :-dispatch arg1 arg ...) )
  707. ((: cc var arg1 arg ...)
  708. (:dispatched cc var :-dispatch arg1 arg ...) )))
  709. ; ==========================================================================
  710. ; The utility comprehensions fold-ec, fold3-ec
  711. ; ==========================================================================
  712. (define-syntax fold3-ec
  713. (syntax-rules (nested)
  714. ((fold3-ec x0 (nested q1 ...) q etc1 etc2 etc3 etc ...)
  715. (fold3-ec x0 (nested q1 ... q) etc1 etc2 etc3 etc ...) )
  716. ((fold3-ec x0 q1 q2 etc1 etc2 etc3 etc ...)
  717. (fold3-ec x0 (nested q1 q2) etc1 etc2 etc3 etc ...) )
  718. ((fold3-ec x0 expression f1 f2)
  719. (fold3-ec x0 (nested) expression f1 f2) )
  720. ((fold3-ec x0 qualifier expression f1 f2)
  721. (let ((result #f) (empty #t))
  722. (do-ec qualifier
  723. (let ((value expression)) ; don't duplicate
  724. (if empty
  725. (begin (set! result (f1 value))
  726. (set! empty #f) )
  727. (set! result (f2 value result)) )))
  728. (if empty x0 result) ))))
  729. (define-syntax fold-ec
  730. (syntax-rules (nested)
  731. ((fold-ec x0 (nested q1 ...) q etc1 etc2 etc ...)
  732. (fold-ec x0 (nested q1 ... q) etc1 etc2 etc ...) )
  733. ((fold-ec x0 q1 q2 etc1 etc2 etc ...)
  734. (fold-ec x0 (nested q1 q2) etc1 etc2 etc ...) )
  735. ((fold-ec x0 expression f2)
  736. (fold-ec x0 (nested) expression f2) )
  737. ((fold-ec x0 qualifier expression f2)
  738. (let ((result x0))
  739. (do-ec qualifier (set! result (f2 expression result)))
  740. result ))))
  741. ; ==========================================================================
  742. ; The comprehensions list-ec string-ec vector-ec etc.
  743. ; ==========================================================================
  744. (define-syntax list-ec
  745. (syntax-rules ()
  746. ((list-ec etc1 etc ...)
  747. (reverse (fold-ec '() etc1 etc ... cons)) )))
  748. ; Alternative: Reverse can safely be replaced by reverse! if you have it.
  749. ;
  750. ; Alternative: It is possible to construct the result in the correct order
  751. ; using set-cdr! to add at the tail. This removes the overhead of copying
  752. ; at the end, at the cost of more book-keeping.
  753. (define-syntax append-ec
  754. (syntax-rules ()
  755. ((append-ec etc1 etc ...)
  756. (apply append (list-ec etc1 etc ...)) )))
  757. (define-syntax string-ec
  758. (syntax-rules ()
  759. ((string-ec etc1 etc ...)
  760. (list->string (list-ec etc1 etc ...)) )))
  761. ; Alternative: For very long strings, the intermediate list may be a
  762. ; problem. A more space-aware implementation collect the characters
  763. ; in an intermediate list and when this list becomes too large it is
  764. ; converted into an intermediate string. At the end, the intermediate
  765. ; strings are concatenated with string-append.
  766. (define-syntax string-append-ec
  767. (syntax-rules ()
  768. ((string-append-ec etc1 etc ...)
  769. (apply string-append (list-ec etc1 etc ...)) )))
  770. (define-syntax vector-ec
  771. (syntax-rules ()
  772. ((vector-ec etc1 etc ...)
  773. (list->vector (list-ec etc1 etc ...)) )))
  774. ; Comment: A similar approach as for string-ec can be used for vector-ec.
  775. ; However, the space overhead for the intermediate list is much lower
  776. ; than for string-ec and as there is no vector-append, the intermediate
  777. ; vectors must be copied explicitly.
  778. (define-syntax vector-of-length-ec
  779. (syntax-rules (nested)
  780. ((vector-of-length-ec k (nested q1 ...) q etc1 etc ...)
  781. (vector-of-length-ec k (nested q1 ... q) etc1 etc ...) )
  782. ((vector-of-length-ec k q1 q2 etc1 etc ...)
  783. (vector-of-length-ec k (nested q1 q2) etc1 etc ...) )
  784. ((vector-of-length-ec k expression)
  785. (vector-of-length-ec k (nested) expression) )
  786. ((vector-of-length-ec k qualifier expression)
  787. (let ((len k))
  788. (let ((vec (make-vector len))
  789. (i 0) )
  790. (do-ec qualifier
  791. (if (< i len)
  792. (begin (vector-set! vec i expression)
  793. (set! i (+ i 1)) )
  794. (error "vector is too short for the comprehension") ))
  795. (if (= i len)
  796. vec
  797. (error "vector is too long for the comprehension") ))))))
  798. (define-syntax sum-ec
  799. (syntax-rules ()
  800. ((sum-ec etc1 etc ...)
  801. (fold-ec (+) etc1 etc ... +) )))
  802. (define-syntax product-ec
  803. (syntax-rules ()
  804. ((product-ec etc1 etc ...)
  805. (fold-ec (*) etc1 etc ... *) )))
  806. (define-syntax min-ec
  807. (syntax-rules ()
  808. ((min-ec etc1 etc ...)
  809. (fold3-ec (min) etc1 etc ... min min) )))
  810. (define-syntax max-ec
  811. (syntax-rules ()
  812. ((max-ec etc1 etc ...)
  813. (fold3-ec (max) etc1 etc ... max max) )))
  814. (define-syntax last-ec
  815. (syntax-rules (nested)
  816. ((last-ec default (nested q1 ...) q etc1 etc ...)
  817. (last-ec default (nested q1 ... q) etc1 etc ...) )
  818. ((last-ec default q1 q2 etc1 etc ...)
  819. (last-ec default (nested q1 q2) etc1 etc ...) )
  820. ((last-ec default expression)
  821. (last-ec default (nested) expression) )
  822. ((last-ec default qualifier expression)
  823. (let ((result default))
  824. (do-ec qualifier (set! result expression))
  825. result ))))
  826. ; ==========================================================================
  827. ; The fundamental early-stopping comprehension first-ec
  828. ; ==========================================================================
  829. (define-syntax first-ec
  830. (syntax-rules (nested)
  831. ((first-ec default (nested q1 ...) q etc1 etc ...)
  832. (first-ec default (nested q1 ... q) etc1 etc ...) )
  833. ((first-ec default q1 q2 etc1 etc ...)
  834. (first-ec default (nested q1 q2) etc1 etc ...) )
  835. ((first-ec default expression)
  836. (first-ec default (nested) expression) )
  837. ((first-ec default qualifier expression)
  838. (let ((result default) (stop #f))
  839. (ec-guarded-do-ec
  840. stop
  841. (nested qualifier)
  842. (begin (set! result expression)
  843. (set! stop #t) ))
  844. result ))))
  845. ; (ec-guarded-do-ec stop (nested q ...) cmd)
  846. ; constructs (do-ec q ... cmd) where the generators gen in q ... are
  847. ; replaced by (:until gen stop).
  848. (define-syntax ec-guarded-do-ec
  849. (syntax-rules (nested if not and or begin)
  850. ((ec-guarded-do-ec stop (nested (nested q1 ...) q2 ...) cmd)
  851. (ec-guarded-do-ec stop (nested q1 ... q2 ...) cmd) )
  852. ((ec-guarded-do-ec stop (nested (if test) q ...) cmd)
  853. (if test (ec-guarded-do-ec stop (nested q ...) cmd)) )
  854. ((ec-guarded-do-ec stop (nested (not test) q ...) cmd)
  855. (if (not test) (ec-guarded-do-ec stop (nested q ...) cmd)) )
  856. ((ec-guarded-do-ec stop (nested (and test ...) q ...) cmd)
  857. (if (and test ...) (ec-guarded-do-ec stop (nested q ...) cmd)) )
  858. ((ec-guarded-do-ec stop (nested (or test ...) q ...) cmd)
  859. (if (or test ...) (ec-guarded-do-ec stop (nested q ...) cmd)) )
  860. ((ec-guarded-do-ec stop (nested (begin etc ...) q ...) cmd)
  861. (begin etc ... (ec-guarded-do-ec stop (nested q ...) cmd)) )
  862. ((ec-guarded-do-ec stop (nested gen q ...) cmd)
  863. (do-ec
  864. (:until gen stop)
  865. (ec-guarded-do-ec stop (nested q ...) cmd) ))
  866. ((ec-guarded-do-ec stop (nested) cmd)
  867. (do-ec cmd) )))
  868. ; Alternative: Instead of modifying the generator with :until, it is
  869. ; possible to use call-with-current-continuation:
  870. ;
  871. ; (define-synatx first-ec
  872. ; ...same as above...
  873. ; ((first-ec default qualifier expression)
  874. ; (call-with-current-continuation
  875. ; (lambda (cc)
  876. ; (do-ec qualifier (cc expression))
  877. ; default ))) ))
  878. ;
  879. ; This is much simpler but not necessarily as efficient.
  880. ; ==========================================================================
  881. ; The early-stopping comprehensions any?-ec every?-ec
  882. ; ==========================================================================
  883. (define-syntax any?-ec
  884. (syntax-rules (nested)
  885. ((any?-ec (nested q1 ...) q etc1 etc ...)
  886. (any?-ec (nested q1 ... q) etc1 etc ...) )
  887. ((any?-ec q1 q2 etc1 etc ...)
  888. (any?-ec (nested q1 q2) etc1 etc ...) )
  889. ((any?-ec expression)
  890. (any?-ec (nested) expression) )
  891. ((any?-ec qualifier expression)
  892. (first-ec #f qualifier (if expression) #t) )))
  893. (define-syntax every?-ec
  894. (syntax-rules (nested)
  895. ((every?-ec (nested q1 ...) q etc1 etc ...)
  896. (every?-ec (nested q1 ... q) etc1 etc ...) )
  897. ((every?-ec q1 q2 etc1 etc ...)
  898. (every?-ec (nested q1 q2) etc1 etc ...) )
  899. ((every?-ec expression)
  900. (every?-ec (nested) expression) )
  901. ((every?-ec qualifier expression)
  902. (first-ec #t qualifier (if (not expression)) #f) )))