1. continuations
    1. The escape-procedure interface
      1. escape-procedure
      2. escape-procedure?
    2. The continuation interface
      1. continuation
      2. current
      3. continuation?
      4. continuation->procedure
      5. capture
      6. graft
      7. throw
      8. catch
      9. goto
      10. call
      11. continuations
    3. The module continuations-used
      1. continuations-used
      2. make-amb
      3. make-threads
      4. iterate
    4. Examples
      1. escape procedures
      2. amb
      3. cooperative threads
      4. iterators
  2. Requirements
  3. Last update
  4. Author
  5. License
  6. Version History

continuations

This library contains two modules, continuations and continuations-used.

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.

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.

And then there is the infamous goto, which albeit dangerous is sometimes useful, e.g. in backtracking ....

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.

The second module, continuations-used, provides some applications of the continuations module.

The escape-procedure interface

escape-procedure

[procedure] (escape-procedure)

captures and returns the current continuation as an escape procedure. Typically used as follows

(let ((cc (escape-procedure)))
  (cond
    ((escape-procedure? cc)
     ;; normal body
     ;; possibly calling (cc val) ...
    ((ok? cc)
     ;; exceptional case
     ;; do something with cc ...))

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).

escape-procedure?

[procedure] (escape-procedure? xpr)

type predicate, defined simultaneously with escape-procedure

The continuation interface

continuation

[procedure] (continuation)

deprecated, use current instead.

current

[procedure] (current)

captures and returns the current continuation. Typically used as follows

(let ((cc (current)))
  (if (continuation? cc)
    ... (throw cc val) ...
    ... do something with val ...))

Note, that the let is invoked twice, first after the call to current, then with the object val, which was thrown to cc.

continuation?

[procedure] (continuation? xpr)

type predicate

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 val ....)

throws the values val .... to the continuation cont.

catch

[syntax] (catch cont xpr ....)

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

 
(catch k
  ...
  (if ...
    (throw k val)
    ...))

goto

[procedure] (goto cc)

The infamous goto, but with a continuation as argument instead of a label.

call

[procedure] (call receiver)

The same as call/cc, but implemented via capture.

continuations

[procedure] (continuations sym ..)

documentation procedure

The module continuations-used

continuations-used

[procedure] (continuations-used sym ..)

the usual documentation procedure

make-amb

[procedure] (make-amb)

produces an ambiguous choice object, which accepts three messages, 'choose, 'fail and 'assert.

make-threads

[procedure] (make-threads)

produces a threads object, which accepts five messages, 'halt, 'quit, 'spawn, 'yield and 'start, implementing cooperative threads.

iterate

[syntax] (iterate var iterator xpr . xprs)

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.

Examples

escape procedures

(use continuations)

(define (product . nums)
  (let ((cc (current-continuation)))
    (cond
      ((escape-procedure? cc) ; continuation cc just created
       (print "NORMAL BODY")
       (cond 
         ((null? nums) 1)
         ((zero? (car nums))
          (cc 0))
         (else
           (* (car nums) (apply product (cdr nums))))))
      ((number? cc) ; cc has been thrown a number
       (print "EXCEPTIONAL CASE")
       cc)
      )))

amb

(require-library continuations)
(import continuations continuations-used)

(define amb (make-amb))

(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)

cooperative threads

(require-library continuations)
(import continuations continuations-used)

(define threads (make-threads))

(define make-thunk
	(let ((counter 10))
		(lambda (name)
			(rec (loop)
				(if (< counter 0)
					((threads 'quit)))
				(print (cons name 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 (a . 10) (aa . 9) (aaa . 8) (a . 7) (aa . 6) (aaa . 5)
;        (a . 4) (aa .  3) (aaa . 2) (a . 1) (aa . 0)
; in sequence

iterators

(require-library continuations)
(import continuations continuations-used)

;; 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

Requirements

none

Last update

Nov 09, 2016

Author

Juergen Lorenz

License

Copyright (c) 2013-2016, 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.

Version History

1.4.1
current-continuation renamed escape-procedure
1.4
escape procedure interface added
1.3
continuation renamed current, call added
1.2.4
tests updated
1.2.2
tests updated
1.2.1
bug in setup file corrected
1.2
some tests rewritten and repackeged as an extra module continuations-used
1.1.2
test cases for iterators and cooperative threads added
1.1.1
bug fix in documentation procedure
1.1
added continuation and goto with the amb example
1.0
initial import