srfi-42.scm 35 KB

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