1. continuations
    1. The escape-procedure interface
      1. escape-procedure
      2. escape-procedure?
      3. call/ep
      4. let/ep
    2. The continuation interface
      1. continuation
      2. continuation?
      3. continuation->procedure
      4. capture
      5. graft
      6. throw
      7. catch
      8. goto
      9. call
      10. continuations
      11. escape procedures
    3. Requirements
    4. Examples
  2. Last update
  3. Author
  4. License
  5. Version History

continuations

This library contains two interfaces to continuations, Marc Feeley's and Matt Might's, both with some syntactic sugar.

In Mark Feeley's 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 ....

Matt Might's interface is proveded 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 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

call/ep

[procedure] (call/ep receiver)

like call/cc, but reifies the captured continuation as a typed escape-procedure

let/ep

[syntax] (let/ep k xpr . xprs)

like let/cc from miscmacros, but reifies the captured continuation k as a typed escape-procedure

The continuation interface

continuation

[procedure] (continuation)

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)

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

escape procedures

</enscript>

Requirements

none

Examples

(import continuations)

(define 1+ #f)

;; ... with catch
(define (init)
  (+ 1 (catch cont
              (set! 1+ (continuation->procedure cont))
              (throw cont 0))))

;; ... with continuation
(define (init-again)
  (+ 1 (let ((cc (continuation)))
         (cond
           ((continuation? cc)
            (set! 1+ (continuation->procedure cc))
            (throw cc 0))
           (else cc)))))

(define (search ok? lst)
  (catch return
         (for-each (lambda (item)
                     (if (ok? item)
                       (throw return item)))
                   lst)
         #f))

(define (search-with-goto ok? lst)
  (let ((start (continuation)))
    (cond
      ((null? lst) #f)
      ((ok? (car lst)) (car lst))
      (else
        (set! lst (cdr lst))
        (goto start)))))

;; nonlocal return: throw and catch in different procedures
(define (treat ok?)
  (lambda (item cont)
    (if (ok? item)
      (throw cont item))))

(define (handled-search handle lst)
  (catch return
         (for-each (lambda (item)
                     (handle item return))
                   lst)
         #f))

(define (product . nums)
  (let ((cc (escape-procedure)))
    (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)
      )))

Last update

Jun 23, 2019

Author

Juergen Lorenz

License

Copyright (c) 2013-2019, 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.1
call/ep and let/ep added
1.0.1
typo in documentation procedure corrected
1.0
ported and simplyfied from chicken-4 version 1.4.1