((section 2 "Outdated egg!" (p "This is an egg for CHICKEN 4, the unsupported old release.  You're almost certainly looking for " (int-link "/eggref/5/continuations" "the CHICKEN 5 version of this egg") ", if it exists.") (p "If it does not exist, there may be equivalent functionality provided by another egg; have a look at the " (link "https://wiki.call-cc.org/chicken-projects/egg-index-5.html" "egg index") ". Otherwise, please consider porting this egg to the current version of CHICKEN.") (tags "egg") (toc)) (section 2 "continuations" (p "This library contains two modules, continuations and continuations-used.") (p "The former 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.") (p "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 my other modules, the call (continuations) lists the exported symbols, and (continuations sym) provides documentation of the exported symbol sym.") (p "And then there is the infamous goto, which albeit dangerous is sometimes useful, e.g. in backtracking ....") (pre "") (p "Moreover, another interface to continuations is provided, recommended by Matt Might, and that in two flavors, with continuations, checked by continuation?, and escape procedures, checked by escape-procedure?. This makes possible code written in an idiom similar to the setjmp-longjmp-pair in C.") (p "The second module, continuations-used, provides some applications of the continuations module.") (section 3 "The escape-procedure interface" (section 4 "escape-procedure" (def (sig (procedure "(escape-procedure)" (id escape-procedure))) (p "captures and returns the current continuation as an escape procedure. Typically used as follows") (highlight scheme "(let ((cc (escape-procedure)))\n  (cond\n    ((escape-procedure? cc)\n     ;; normal body\n     ;; possibly calling (cc val) ...\n    ((ok? cc)\n     ;; exceptional case\n     ;; do something with cc ...))") (p "Note, that the let is invoked twice, first after the call to escape-procedure, then with the object val, which was bound to cc by calling (cc val)."))) (section 4 "escape-procedure?" (def (sig (procedure "(escape-procedure? xpr)" (id escape-procedure?))) (p "type predicate, defined simultaneously with escape-procedure")))) (section 3 "The continuation interface" (section 4 "continuation" (def (sig (procedure "(continuation)" (id continuation))) (p "deprecated, use current instead."))) (section 4 "current" (def (sig (procedure "(current)" (id current))) (p "captures and returns the current continuation. Typically used as follows") (highlight scheme "(let ((cc (current)))\n  (if (continuation? cc)\n    ... (throw cc val) ...\n    ... do something with val ...))") (p "Note, that the let is invoked twice, first after the call to current, then with the object val, which was thrown to cc."))) (section 4 "continuation?" (def (sig (procedure "(continuation? xpr)" (id continuation?))) (p "type predicate"))) (section 4 "continuation->procedure" (def (sig (procedure "(continuation->procedure cont)" (id continuation->procedure))) (p "transforms a continuation into a procedure"))) (section 4 "capture" (def (sig (procedure "(capture proc)" (id capture))) (p "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."))) (section 4 "graft" (def (sig (procedure "(graft cont thunk)" (id graft))) (p "tail-calls thunk with the implicit continuation cont."))) (section 4 "throw" (def (sig (procedure "(throw cont val ....)" (id throw))) (p "throws the values val .... to the continuation cont."))) (section 4 "catch" (def (sig (syntax "(catch cont xpr ....)" (id catch))) (p "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 .... in this context. Typically used as follows") (pre " ") (highlight scheme "(catch k\n  ...\n  (if ...\n    (throw k val)\n    ...))"))) (section 4 "goto" (def (sig (procedure "(goto cc)" (id goto))) (p "The infamous goto, but with a continuation as argument instead of a label."))) (section 4 "call" (def (sig (procedure "(call receiver)" (id call))) (p "The same as call/cc, but implemented via capture."))) (section 4 "continuations" (def (sig (procedure "(continuations sym ..)" (id continuations))) (p "documentation procedure")))) (section 3 "The module continuations-used" (section 4 "continuations-used" (def (sig (procedure "(continuations-used sym ..)" (id continuations-used))) (p "the usual documentation procedure"))) (section 4 "make-amb" (def (sig (procedure "(make-amb)" (id make-amb))) (p "produces an ambiguous choice object, which accepts three messages, 'choose, 'fail and 'assert."))) (section 4 "make-threads" (def (sig (procedure "(make-threads)" (id make-threads))) (p "produces a threads object, which accepts five messages, 'halt, 'quit, 'spawn, 'yield and 'start, implementing cooperative threads."))) (section 4 "iterate" (def (sig (syntax "(iterate var iterator xpr . xprs)" (id iterate))) (p "iterates var over iterater and applies the body, xpr . xprs, to each item. iterator should be a curried procedure of a container and a yield routine, the latter supplying one value at each pass.")))) (section 3 "Examples" (section 4 "escape procedures" (highlight scheme "(use continuations)\n\n(define (product . nums)\n  (let ((cc (escape-procedure)))\n    (cond\n      ((escape-procedure? cc) ; continuation cc just created\n       (print \"NORMAL BODY\")\n       (cond \n         ((null? nums) 1)\n         ((zero? (car nums))\n          (cc 0))\n         (else\n           (* (car nums) (apply product (cdr nums))))))\n      ((number? cc) ; cc has been thrown a number\n       (print \"EXCEPTIONAL CASE\")\n       cc)\n      )))\n")) (section 4 "amb" (highlight scheme "(require-library continuations)\n(import continuations continuations-used)\n\n(define amb (make-amb))\n\n(define (pythagoras . choices)\n  (let ((a (apply (amb 'choose) choices))\n        (b (apply (amb 'choose) choices))\n        (c (apply (amb 'choose) choices)))\n    ((amb 'assert) (= (* c c) (+ (* a a) (* b b))))\n    ((amb 'assert) (< b a))\n    (list a b c)))\n\n(pythagoras 1 2 3 4 5 6 7) ; -> (4 3 5)")) (section 4 "cooperative threads" (highlight scheme "(require-library continuations)\n(import continuations continuations-used)\n\n(define threads (make-threads))\n\n(define make-thunk\n\t(let ((counter 10))\n\t\t(lambda (name)\n\t\t\t(rec (loop)\n\t\t\t\t(if (< counter 0)\n\t\t\t\t\t((threads 'quit)))\n\t\t\t\t(print (cons name counter))\n\t\t\t\t(set! counter (- counter 1))\n\t\t\t\t((threads 'yield))\n\t\t\t\t(loop)))))\n\n((threads 'spawn) (make-thunk 'a))\n((threads 'spawn) (make-thunk 'aa))\n((threads 'spawn) (make-thunk 'aaa))\n((threads 'start))\n\n; prints (a . 10) (aa . 9) (aaa . 8) (a . 7) (aa . 6) (aaa . 5)\n;        (a . 4) (aa .  3) (aaa . 2) (a . 1) (aa . 0)\n; in sequence")) (section 4 "iterators" (highlight scheme "(require-library continuations)\n(import continuations continuations-used)\n\n;; define an iterator for tree, i.e. a function of yield, which returns \n;; one tree-item at each pass\n(define (tree-iterator tree)\n  (lambda (yield)\n    (let walk ((tree tree))\n      (if (pair? tree)\n        (begin (walk (car tree))\n               (walk (cdr tree)))\n        (yield tree)))))\n\n(iterate var (tree-iterator '(3 . ((4 . 5) . 6))) (print var))\n; prints 3 4 5 6 in sequence")))) (section 2 "Requirements" (p "none")) (section 2 "Last update" (p "Nov 09, 2016")) (section 2 "Author" (p (int-link "/users/juergen-lorenz" "Juergen Lorenz"))) (section 2 "License" (pre "Copyright (c) 2013-2016, Juergen Lorenz\nAll rights reserved.") (pre "Redistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\nRedistributions of source code must retain the above copyright\nnotice, this list of conditions and the following disclaimer.\n\nRedistributions in binary form must reproduce the above copyright\nnotice, this list of conditions and the following disclaimer in the\ndocumentation and/or other materials provided with the distribution.\nNeither the name of the author nor the names of its contributors may be\nused to endorse or promote products derived from this software without\nspecific prior written permission. \n  \nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS\nIS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\nTO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\nPARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nHOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED\nTO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\nPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\nLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.")) (section 2 "Version History" (dl (dt "1.4.1") (dd "current-continuation renamed escape-procedure") (dt "1.4") (dd "escape procedure interface added") (dt "1.3") (dd "continuation renamed current, call added") (dt "1.2.4") (dd "tests updated") (dt "1.2.2") (dd "tests updated") (dt "1.2.1") (dd "bug in setup file corrected") (dt "1.2") (dd "some tests rewritten and repackeged as an extra module continuations-used") (dt "1.1.2") (dd "test cases for iterators and cooperative threads added") (dt "1.1.1") (dd "bug fix in documentation procedure") (dt "1.1") (dd "added continuation and goto with the amb example") (dt "1.0") (dd "initial import"))))