You are looking at historical revision 29406 of this page. It may differ significantly from its current revision.
This module provides some syntactic sugar to Marc Feeley's continuation interface. In this interface, continuations are a datatype separate from procedures. Hence it provides a continuation? predicate. I've stripped the prefix from the procedures continuation-capture and continuation-graft and renamed continuation-return throw. This latter procedure is accompanied in this module by a macro named catch, so that the usual catch-throw-semantics of other languages is available. But note, that in this pattern, the continuation is a Scheme object, and like every Scheme object it has indefinite extent, hence can be exported, saved in other data-structures etc. So this pattern is much more powerful than the corresponding pattern in other languages.
Some other procedures are provided as well, in particular continuation->procedure, which does what the name says, and continuations, which provides offline documentation to the module. Like in modules written in the design-by-contract style, the call (continuations) lists the exported symbols, and (continuations sym) provides documentation of the exported symbol sym.
Moreover, another interface to continuations is provided, recommended by Matt Might, albeit with different names: continuation and the infamous goto. The former is sort of a constructor, the latter, although dangerous, is sometimes useful, e.g. in backtracking ....
continuations[procedure] (continuations [sym])
captures and returns the current continuation. Typically used as follows
(let ((cc (continuation))) (if (continuation? cc) ... (throw cc val) ... ... do something with val ...))
Note, that the let is invoked twice, first after the call to continuation, then with the object val, which was thrown to cc.
continuation?[procedure] (continuation? xpr)
continuation->procedure[procedure] (continuation->procedure cont)
transforms a continuation into a procedure
capture[procedure] (capture proc)
The same as call/cc but with a different datatype: Captures the current continuation as a continuation datatype (contrary to a procedure datatype in call/cc) and calls proc with that continuation as its only argument.
graft[procedure] (graft cont thunk)
tail-calls thunk with the implicit continuation cont.
throw[procedure] (throw cont . vals)
throws the values vals to the continuation cont.
catch[syntax] (catch cont xpr . xprs)
The same as let/cc of miscmacros but with a different datatype: Binds the cont variable to the current continuation as a continuation and executes the body xpr . xprs in this context. Typically used as follows
(catch k ... (if ... (throw k val) ...))
goto[procedure] (goto cc)
The infamous goto, but with a continuation as argument instead of a label.
;; multiple operators sharing common state (define-values (amb-fail amb-choose amb-assert) (let ((amb-stack '())) (values (lambda () (if (pair? amb-stack) (let ((back-track-point (car amb-stack))) (set! amb-stack (cdr amb-stack)) (goto back-track-point)) (error 'amb-fail "amb-stack exhausted"))) (lambda choices (let ((cc (continuation))) (cond ((null? choices) (amb-fail)) ((pair? choices) (let ((choice (car choices))) (set! choices (cdr choices)) (set! amb-stack (cons cc amb-stack)) choice))))) (lambda (xpr) (if (not xpr) (amb-fail) #t)) ))) (define (pythagoras . choices) (let ((a (apply amb-choose choices)) (b (apply amb-choose choices)) (c (apply amb-choose choices))) (amb-assert (= (* c c) (+ (* a a) (* b b)))) (amb-assert (< b a)) (list a b c))) (pythagoras 1 2 3 4 5 6 7) ; -> (4 3 5)
(define-syntax iterate (syntax-rules () ((_ var iterator xpr . xprs) (let ((it iterator) (it-cont #f)) (let loop () (let ((cc (continuation))) (if (continuation? cc) ;; first let-pass (if (continuation? it-cont) (throw it-cont (void)) (it (lambda (val) (catch next-cc (throw cc (cons next-cc val)))))) ;; second let-pass (cc is now pair) (let ((next-cont (car cc)) (next-val (cdr cc))) (set! it-cont next-cont) (let ((var next-val)) xpr . xprs) (loop))))))))) ;; define an iterator for tree, i.e. a function of yield, which returns ;; one tree-item at each pass (define (tree-iterator tree) (lambda (yield) (let walk ((tree tree)) (if (pair? tree) (begin (walk (car tree)) (walk (cdr tree))) (yield tree))))) (iterate var (tree-iterator '(3 . ((4 . 5) . 6))) (print var)) ; prints 3 4 5 6 in sequence
;; multiple operators sharing common state (define-values (threads-halt threads-spawn threads-yield threads-quit threads-start) (let ((threads-queue '())) (values #f (lambda (thunk) (let ((cc (continuation))) (if (continuation? cc) (set! threads-queue (append threads-queue (list cc))) (begin (thunk) (threads-quit))))) (lambda () (let ((cc (continuation))) (if (and (continuation? cc) (pair? threads-queue)) (let ((next-thread (car threads-queue))) (set! threads-queue (append (cdr threads-queue) (list cc))) (throw next-thread 'resume))))) (lambda () (if (pair? threads-queue) (let ((next-thread (car threads-queue))) (set! threads-queue (cdr threads-queue)) (throw next-thread 'resume)) (threads-halt))) (lambda () (let ((cc (continuation))) (when cc;(continuation? cc) (set! threads-halt (lambda () (throw cc #f))) (if (not (null? threads-queue)) (let ((next-thread (car threads-queue))) (set! threads-queue (cdr threads-queue)) (throw next-thread 'resume)))))) ))) (define counter 10) (define (make-thunk name) (rec (loop) (if (< counter 0) (threads-quit)) (print "in thread " name " with counter = " counter) (set! counter (- counter 1)) (threads-yield) (loop))) (threads-spawn (make-thunk 'a)) (threads-spawn (make-thunk 'aa)) (threads-spawn (make-thunk 'aaa)) (threads-start) ;-> prints ;in thread a with counter = 10 ;in thread aa with counter = 9 ;in thread aaa with counter = 8 ;in thread a with counter = 7 ;in thread aa with counter = 6 ;in thread aaa with counter = 5 ;in thread a with counter = 4 ;in thread aa with counter = 3 ;in thread aaa with counter = 2 ;in thread a with counter = 1 ;in thread a with counter = 0
Jul 23, 2013
Copyright (c) 2013, Juergen Lorenz All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- bug fix in documentation procedure
- added continuation and goto with the amb example
- initial import