source: project/wiki/eggref/4/continuations @ 30140

Last change on this file since 30140 was 30140, checked in by juergen, 7 years ago

treaps, skiplists,random-access-lists,lazy-lists,continuations: tests updated

File size: 7.5 KB
Line 
1[[tags: egg]]
2[[toc:]]
3
4== continuations
5
6This library contains two modules, continuations and continuations-used.
7
8The former provides some syntactic sugar to Marc Feeley's continuation
9interface. In this interface, continuations are a datatype separate from
10procedures. Hence it provides a continuation? predicate. I've stripped
11the prefix from the procedures continuation-capture and
12continuation-graft and renamed continuation-return throw. This latter
13procedure is accompanied in this module by a macro named catch, so that
14the usual catch-throw-semantics of other languages is available.  But
15note, that in this pattern, the continuation is a Scheme object, and
16like every Scheme object it has indefinite extent, hence can be
17exported, saved in other data-structures etc. So this pattern is much
18more powerful than the corresponding pattern in other languages.
19
20Some other procedures are provided as well, in particular
21continuation->procedure, which does what the name says, and
22continuations, which provides offline documentation to the module. Like
23in modules written in the design-by-contract style, the call
24(continuations) lists the exported symbols, and (continuations sym)
25provides documentation of the exported symbol sym.
26
27Moreover, another interface to continuations is provided, recommended by
28Matt Might, albeit with different names: continuation and the infamous
29goto. The former is sort of a constructor, the latter, although
30dangerous, is sometimes useful, e.g. in backtracking ....
31 
32The second module, continuations-used, provides some applications of the
33continuations module.
34
35=== The routines of the continuations module
36
37==== continuations
38
39<procedure>(continuations [sym])</procedure>
40
41documentation procedure
42
43==== continuation
44
45<procedure>(continuation)</procedure>
46
47captures and returns the current continuation. Typically used as follows
48
49<enscript highlight=scheme>
50(let ((cc (continuation)))
51  (if (continuation? cc)
52    ... (throw cc val) ...
53    ... do something with val ...))
54</enscript>
55
56Note, that the let is invoked twice, first after the call to
57continuation, then with the object val, which was thrown to cc.
58
59==== continuation?
60
61<procedure>(continuation? xpr)</procedure>
62
63type predicate
64
65==== continuation->procedure
66
67<procedure>(continuation->procedure cont)</procedure>
68
69transforms a continuation into a procedure
70
71==== capture
72
73<procedure>(capture proc)</procedure>
74
75The same as call/cc but with a different datatype:
76Captures the current continuation as a continuation datatype (contrary
77to a procedure datatype in call/cc) and calls proc with that
78continuation as its only argument.
79
80==== graft
81
82<procedure>(graft cont thunk)</procedure>
83
84tail-calls thunk with the implicit continuation cont.
85
86==== throw
87
88<procedure>(throw cont . vals)</procedure>
89
90throws the values vals to the continuation cont.
91
92==== catch
93
94<macro>(catch cont xpr . xprs)</macro>
95
96The same as let/cc of miscmacros but with a different datatype:
97Binds the cont variable to the current continuation as a continuation
98and executes the body xpr .  xprs in this context. Typically used as
99follows
100 
101<enscript highlight=scheme>
102(catch k
103  ...
104  (if ...
105    (throw k val)
106    ...))
107</enscript>
108
109==== goto
110
111<procedure>(goto cc)</procedure>
112
113The infamous goto, but with a continuation as argument instead of a label.
114
115=== The routines of the continuations-used module
116
117==== continuations-used
118
119<procedure>(continuations-used [sym])</procedure>
120
121the usual documentation procedure
122
123==== make-amb
124
125<procedure>(make-amb)</procedure>
126
127produces an ambiguous choice object, which accepts three messages,
128'choose, 'fail and 'assert.
129
130==== make-threads
131
132<procedure>(make-threads)</procedure>
133
134produces a threads object, which accepts five messages,
135'halt, 'quit, 'spawn, 'yield and 'start, implementing cooperative
136threads.
137
138==== iterate
139
140<macro>(iterate var iterator xpr . xprs)</macro>
141
142iterates var over iterater and applies the body, xpr . xprs, to each
143item. iterator should be a curried procedure of a container and a yield
144routine, the latter supplying one value at each pass.
145
146=== Examples
147
148==== amb
149
150<enscript highlight=scheme>
151(require-library continuations)
152(import continuations continuations-used)
153
154(define amb (make-amb))
155
156(define (pythagoras . choices)
157  (let ((a (apply (amb 'choose) choices))
158        (b (apply (amb 'choose) choices))
159        (c (apply (amb 'choose) choices)))
160    ((amb 'assert) (= (* c c) (+ (* a a) (* b b))))
161    ((amb 'assert) (< b a))
162    (list a b c)))
163
164(pythagoras 1 2 3 4 5 6 7) ; -> (4 3 5)
165</enscript>
166
167==== cooperative threads
168
169<enscript highlight=scheme>
170(require-library continuations)
171(import continuations continuations-used)
172
173(define threads (make-threads))
174
175(define make-thunk
176        (let ((counter 10))
177                (lambda (name)
178                        (rec (loop)
179                                (if (< counter 0)
180                                        ((threads 'quit)))
181                                (print (cons name counter))
182                                (set! counter (- counter 1))
183                                ((threads 'yield))
184                                (loop)))))
185
186((threads 'spawn) (make-thunk 'a))
187((threads 'spawn) (make-thunk 'aa))
188((threads 'spawn) (make-thunk 'aaa))
189((threads 'start))
190
191; prints (a . 10) (aa . 9) (aaa . 8) (a . 7) (aa . 6) (aaa . 5)
192;        (a . 4) (aa .  3) (aaa . 2) (a . 1) (aa . 0)
193; in sequence
194</enscript>
195
196==== iterators
197
198<enscript highlight=scheme>
199(require-library continuations)
200(import continuations continuations-used)
201
202;; define an iterator for tree, i.e. a function of yield, which returns
203;; one tree-item at each pass
204(define (tree-iterator tree)
205  (lambda (yield)
206    (let walk ((tree tree))
207      (if (pair? tree)
208        (begin (walk (car tree))
209               (walk (cdr tree)))
210        (yield tree)))))
211
212(iterate var (tree-iterator '(3 . ((4 . 5) . 6))) (print var))
213; prints 3 4 5 6 in sequence
214</enscript>
215
216== Requirements
217
218none
219
220== Last update
221
222Nov 27, 2013
223
224== Author
225
226[[/users/juergen-lorenz|Juergen Lorenz]]
227
228== License
229
230 Copyright (c) 2013, Juergen Lorenz
231 All rights reserved.
232
233 Redistribution and use in source and binary forms, with or without
234 modification, are permitted provided that the following conditions are
235 met:
236 
237 Redistributions of source code must retain the above copyright
238 notice, this list of conditions and the following disclaimer.
239 
240 Redistributions in binary form must reproduce the above copyright
241 notice, this list of conditions and the following disclaimer in the
242 documentation and/or other materials provided with the distribution.
243 Neither the name of the author nor the names of its contributors may be
244 used to endorse or promote products derived from this software without
245 specific prior written permission.
246   
247 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
248 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
249 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
250 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
251 HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
252 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
253 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
254 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
255 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
256 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
257 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258
259== Version History
260
261; 1.2.4 : tests updated
262; 1.2.2 : tests updated
263; 1.2.1 : bug in setup file corrected
264; 1.2 : some tests rewritten and repackeged as an extra module continuations-used
265; 1.1.2 : test cases for iterators and cooperative threads added
266; 1.1.1 : bug fix in documentation procedure
267; 1.1 : added continuation and goto with the amb example
268; 1.0 : initial import
Note: See TracBrowser for help on using the repository browser.