source: project/release/4/continuations/trunk/continuations.scm @ 33737

Last change on this file since 33737 was 33737, checked in by juergen, 23 months ago

continuations 1.4.1 with current-continuation renamed escape-procedure

File size: 9.8 KB
Line 
1; Author: Juergen Lorenz
2; ju (at) jugilo (dot) de
3;
4; Copyright (c) 2013-2016, Juergen Lorenz
5; All rights reserved.
6;
7; Redistribution and use in source and binary forms, with or without
8; modification, are permitted provided that the following conditions are
9; met:
10;
11; Redistributions of source code must retain the above copyright
12; notice, this list of conditions and the following disclaimer.
13;
14; Redistributions in binary form must reproduce the above copyright
15; notice, this list of conditions and the following disclaimer in the
16; documentation and/or other materials provided with the distribution.
17;
18; Neither the name of the author nor the names of its contributors may be
19; used to endorse or promote products derived from this software without
20; specific prior written permission.
21;
22; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23; IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24; TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25; PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26; HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33;
34; Last update: Oct 16, 2016
35;
36;;; simplifying Marc Feeley's continuation interface with some additions,
37;;; i.a. by  Matt Might
38
39(require-extension lolevel)
40
41(module continuations
42  (continuations catch capture graft throw continuation? continuation
43                 current call goto continuation->procedure
44                 escape-procedure escape-procedure?)
45
46  (import scheme
47          (rename (only chicken
48                        continuation-capture
49                        continuation-graft
50                        continuation-return
51                        continuation?
52                        define-values
53                        case-lambda
54                        print
55                        gensym)
56                  (continuation-capture capture)
57                  (continuation-graft graft)
58                  (continuation-return throw))
59          (only lolevel extend-procedure procedure-data
60                extended-procedure?))
61
62(define-syntax catch
63  (syntax-rules ()
64    ((_ cont xpr . xprs)
65     (capture (lambda (cont) xpr . xprs)))))
66
67(define (continuation->procedure cont)
68  (lambda vals (apply throw cont vals)))
69
70(define (call receiver)
71  (capture (lambda (cc)
72             (receiver (continuation->procedure cc)))))
73
74(define (current)
75  (capture (lambda (cc) (throw cc cc))))
76
77(define (continuation) ; deprecated
78  (capture (lambda (cc) (throw cc cc))))
79
80(define (goto cc) (throw cc cc))
81
82;;; Matt Wright's functional interface
83(define-values (escape-procedure escape-procedure?)
84  (let ((type (gensym 'escape-procedure)))
85    (values
86      (lambda ()
87        (call-with-current-continuation
88          (lambda (cc)
89                   (cc (extend-procedure cc type)))))
90      (lambda (xpr)
91        (and (extended-procedure? xpr)
92             (eq? (procedure-data xpr) type)))
93      )))
94
95(define continuations
96  (let ((alist '(
97          (escape-procedure
98            procedure:
99            (escape-procedure)
100            "captures the current continuation as an escape procedure")
101          (escape-procedure?
102            procedure:
103            (escape-procedure? xpr)
104            "checks if xpr is an escape procedure")
105          (catch
106            macro:
107            (catch cc xpr ....)
108            "catches cc as a continuation"
109            "and evaluates xpr .... in this context")
110          (capture
111            procedure:
112            (capture receiver)
113             "alias for continuation-capture")
114          (continuation?
115            procedure:
116            (continuation xpr)
117            "tests if xpr is a continuation")
118          (continuation->procedure
119            procedure:
120            (continuation->procedure cc)
121            "transforms a continuation into a procedure")
122          (call
123            procedure:
124            (call receiver)
125            "the same as call/cc, but implemented via capture")
126          (graft
127            procedure:
128            (graft cc thunk)
129            "alias for continuation-graft")
130          (throw
131            procedure:
132            (throw cc val ...)
133            "alias for continuation-return")
134          (current
135            procedure:
136            (current)
137            "captures the current continuation as a continuation")
138          (goto
139            procedure:
140            (goto cc)
141            "jumps to the continuation cc")
142          (continuations
143            procedure:
144            (continuations sym ..)
145            "documentation procedure")
146          )))
147    (case-lambda
148      (() (map car alist))
149      ((sym)
150       (let ((result (assq sym alist)))
151         (if result
152           (for-each print result)
153           (begin
154             (print "choose one of "
155                    (map car alist)))))))))
156
157) ; end continuations
158
159(module continuations-used
160  (continuations-used make-amb make-threads iterate)
161(import scheme continuations
162        (only chicken when unless error print case-lambda)
163        (only extras format))
164
165;;; iterators
166(define-syntax iterate
167  (syntax-rules ()
168    ((_ var iterator xpr . xprs)
169     (let ((it iterator) (it-cont #f))
170       (let loop ()
171         (let ((cc (continuation)))
172           (if (continuation? cc)
173             ;; first let-pass
174             (if (continuation? it-cont)
175               (throw it-cont (void))
176               (it (lambda (val)
177                   (catch next-cc
178                     (throw cc (cons next-cc val))))))
179             ;; second let-pass (cc is now pair)
180             (let ((next-cont (car cc))
181                   (next-val (cdr cc)))
182               (set! it-cont next-cont)
183               (let ((var next-val)) xpr . xprs)
184               (loop)))))))))
185
186;; amb packaged as a message-passing object
187;; ----------------------------------------
188(define (make-amb)
189  (let ((stack '()))
190    (define (amb-fail)
191      (if (null? stack)
192        (error 'amb-fail "amb-stack exhausted")
193        (begin
194          (let ((choice (car stack)))
195            (set! stack (cdr stack))
196            (goto choice)))))
197    (define (amb-choose . choices)
198      (let ((cc (continuation)))
199        (cond
200          ((null? choices)
201           (amb-fail))
202          ((pair? choices)
203           (let ((choice (car choices)))
204             (set! choices (cdr choices))
205             (set! stack (cons cc stack))
206             choice)))))
207    (define (amb-assert xpr)
208      (if xpr
209        #t
210        (amb-fail)))
211    (lambda (sym)
212      (case sym
213        ((fail) amb-fail)
214        ((choose) amb-choose)
215        ((assert) amb-assert)
216        (else
217          (error 'make-amb
218                 (format #f "message not in ~s: ~s"
219                         '(fail choose assert)
220                         sym)))))))
221
222;; cooperative threads packaged as a message-passing object
223;; -------------------------------------------------------
224(define (make-threads)
225  ;; a queue implemented by two lists giving
226  ;; amortized constant time access to its items
227  (let ((in '()) (out '()))
228    (define (enq! var)
229      (set! in (cons var in)))
230    (define (deq!)
231      (when (null? out)
232        (set! out (reverse in))
233        (set! in '()))
234      (let ((var (car out)))
235        (set! out (cdr out))
236        var))
237    (define threads-halt #f)
238    (define (threads-spawn thunk)
239      (let ((cc (continuation)))
240        (if (continuation? cc)
241          (enq! cc)
242          (begin (thunk) (threads-quit)))))
243    (define (threads-yield)
244      (let ((cc (continuation)))
245        (if (and (continuation? cc)
246                 (not (and (null? in) (null? out))))
247          (let ((next-thread (deq!)))
248            (enq! cc)
249            (throw next-thread 'resume)))))
250    (define (threads-quit)
251      (if (and (null? in) (null? out))
252        (threads-halt)
253        (throw (deq!) 'resume)))
254    (define (threads-start)
255      (let ((cc (continuation)))
256        (when cc;(continuation? cc)
257          (set! threads-halt (lambda () (throw cc #f)))
258          (unless (and (null? in) (null? out))
259            (throw (deq!) 'resume)))))
260    (lambda (sym)
261      (case sym
262        ((halt) threads-halt)
263        ((spawn) threads-spawn)
264        ((yield) threads-yield)
265        ((quit) threads-quit)
266        ((start) threads-start)
267        (else
268          (error 'make-threads
269                 (format #f "message not in ~s: ~s"
270                         '(spawn yield quit start halt)
271                         sym)))))))
272
273(define continuations-used
274  (let ((alist '(
275          (iterate
276            macro:
277            (iterate var iterator xpr ....)
278            "iterator should accept a container argument"
279            "and return a generator, i.e. a function"
280            "which can yield any item of the container."
281            "interate inspects them in turn")
282          (make-amb
283            procedure:
284            (make-amb)
285            "ambiguous choice"
286            "result accepts symbols choose fail and assert")
287          (make-threads
288            procedure:
289            (make-threads)
290            "cooperative threads"
291            "result accepts symbols halt, spawn, yield, quit and start")
292          (continuations-used
293            procedure:
294            (continuations sym ..)
295            "documentation procedure")
296          )))
297    (case-lambda
298      (() (map car alist))
299      ((sym)
300       (let ((result (assq sym alist)))
301         (if result
302           (for-each print result)
303           (begin
304             (print "choose one of "
305                    (map car alist)))))))))
306
307) ; module continuations-used
Note: See TracBrowser for help on using the repository browser.