Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Suspension

A computation can stop short of its result: an error ends it, a generator hands over a value and waits to be asked for the next, a task waits for a timer. snail-scheme has one mechanism for all three, of three functions in (snail core base):

(make-prompt)                        ; (Prompt S R)
(with-prompt prompt body handler)    ; A
(suspend prompt value)               ; R

(with-prompt prompt body handler) calls body, a function of no arguments, and answers its value. If the body, or anything it calls, however deep, calls (suspend prompt value), the body stops there, and the call answers (handler value resume) instead. resume is the rest of the body: (resume answer) goes on from the suspend, whose value is answer, to the body’s end, and answers the body’s value.

A Prompt S R is what ties the two together: a suspension carries an S out and is resumed with an R. Nothing between the suspend and its with-prompt takes part: no function on the way says in its type that it may be suspended, takes anything for it, or is written differently.

Every example is part of a program under tests/programs/, which make test runs, and the output shown is that program’s.

Stopping

Exceptions

A handler need not call resume. One that never does is an exception handler (tests/programs/prompts.sld):

;; A prompt whose answer is Never: a suspension to it never comes back.
(define failing (→ (Prompt String Never)) (make-prompt))

(define (fail (why String)) (→ A) (∀ (A))
  (absurd (suspend failing why)))

(define (digit (c Char)) (→ I64)
  (if (char-numeric? c)
      (- (char->integer c) (char->integer #\0))
      (fail (string-append "not a digit: " (make-string 1 c)))))

(define (number (text String)) (→ I64)
  (fold-left (lambda (value c) (+ (* value 10) (digit c))) 0 (string->list text)))

;; `text` as a number, or `default`, after saying why not.
(define (parse-or (out OutputPort) (default I64) (text String)) (→ I64)
  (with-prompt failing
               (lambda () (number text))
               (lambda (why resume)
                 (write-line out why)
                 default)))

  (write-line out (show (parse-or out 0 "123")))
  (write-line out (show (parse-or out 0 "12x")))
123
not a digit: x
0

The handler’s value is the value of the whole with-prompt, here default. Nothing after the fail runs, in digit, in number’s fold, or anywhere between them and the with-prompt. A procedure held across an abandoned suspension is never called; there is no finally clause.

failing answers Never, the type with no values, so nothing could be passed to its resume, and (absurd never), of type (∀ (A) (Fn Never → A)), gives the suspension whatever type the code around it needs. Since nothing resumes such a suspension, it costs less: it saves nothing of the computation it leaves, and only unwinds (tests/programs/prompt-exception.sld). The compiler reports every source error this way (compile-error in (snail shell compiler read)).

The handler runs outside

A handler runs outside its with-prompt, so a suspension it makes goes to the next with-prompt of that prompt, further out. A handler that fails again is caught there:

  ;; A handler that fails again reaches the `with-prompt` outside its own.
  (report out
          (attempt (lambda ()
                     (match (attempt (lambda () (total out (list -7))))
                       ((Ok value) value)
                       ((Err why) (fail (string-append "again, " why)))))))
failed: again, negative: -7

Resuming

Generators

(resume answer) runs the rest of the body, with answer as the suspension’s value, to the body’s end, and answers the body’s value, so the handler may build on it:

(define yielding (→ (Prompt I64 Unit)) (make-prompt))

(define (yield (value I64)) (→ Unit)
  (suspend yielding value))

;; Yields the numbers from `low` to `high`.
(define (range (low I64) (high I64)) (→ Unit)
  (when (<= low high)
    (yield low)
    (range (+ low 1) high)))

;; Every value `body` yields, in order.
(define (collect (body (Fn → Unit))) (→ (List I64))
  (with-prompt yielding
               (lambda ()
                 (body)
                 (Nil))
               (lambda (value resume) (Cons value (resume (unit))))))

  (show-all out (collect (lambda () (range 1 5))))
1 2 3 4 5

Each yield stops range and hands its value to the handler, which conses it onto whatever the rest of the body produces. The body ends with (Nil), the answer when nothing more is yielded. A handler may also choose, value by value, whether to resume:

;; The first value above `limit` that `body` yields, or -1.
(define (first-above (limit I64) (body (Fn → Unit))) (→ I64)
  (with-prompt yielding
               (lambda ()
                 (body)
                 -1)
               (lambda (value resume) (if (> value limit) value (resume (unit))))))

4
-1

The with-prompt is around the rest again

Resumed, the body runs under its with-prompt again: its next suspension reaches the same handler, with a new resume for what is left then. collect above depends on it: each (resume (unit)) runs to the next yield, whose handler conses again.

Keeping resume

resume is an ordinary function. A handler may return it inside a value, and whoever holds that value may call it later, after the with-prompt has returned:

;; A generator stopped at a value, holding the rest of its body.
(enum Step
  (Done)
  (Yielded
   ((value I64)
    (next (Fn Unit → Step)))))

;; Runs `body` to its first yield, and hands back the rest rather than
;; resuming it.
(define (start (body (Fn → Unit))) (→ Step)
  (with-prompt yielding
               (lambda ()
                 (body)
                 (Done))
               (lambda (value resume) (Yielded value resume))))

;; Takes turns between two generators until both are done.
(define (interleave (this Step) (other Step)) (→ (List I64))
  (match this
    ((Done)
     (match other
       ((Done) (Nil))
       (rest (interleave rest (Done)))))
    ((Yielded value next) (Cons value (interleave other (next (unit)))))))

1 10 2 11 3 12 13 14

start runs a generator to its first yield, and the handler returns the value with resume instead of calling it. interleave calls each next long after start returned. Another task may call it just as well (tests/programs/prompt-handoff.sld).

Resuming once

The frames resume holds run at most once. The checker does not track this: to it, resume is an Fn like any other. Instead the continuation records that it has run, and a second call panics (tests/programs/prompt-resume-twice.sld):

(import
 (snail core base)
 (snail core io))

;; `resume` runs the rest of the body at most once. It is an ordinary
;; function to the checker, so a handler may call it twice, as this one
;; does, and the second call panics.
(define choosing (→ (Prompt Unit Bool)) (make-prompt))

(define (both (out OutputPort)) (→ (List Bool))
  (with-prompt choosing
               (lambda () (list (suspend choosing (unit))))
               (lambda (value resume)
                 (let ((first (resume #t)))
                   (write-line out "resumed once")
                   (append first (resume #f))))))

(define (main (out OutputPort)) (→ Unit)
  (both out)
  (write-line out "not reached")
  (unit))
resumed once
snail: resume called twice

A handler that never calls resume leaves its frames to the collector; nothing in them runs. Multi-shot continuations are out of scope: they would need every frame to be reachable twice, a cost paid by code that never captures one.

Loops in constant stack

A handler that calls resume in tail position runs in constant stack (tests/programs/prompt-tail-loop.sld):

(define ticking (→ (Prompt I64 I64)) (make-prompt))

(define (count-down (n I64) (total I64)) (→ I64)
  (if (= n 0) total (count-down (- n 1) (+ total (suspend ticking n)))))

(define (main (out OutputPort)) (→ Unit)
  (display out
           (with-prompt ticking
                        (lambda () (count-down 100000 0))
                        (lambda (n resume) (resume (* n 2)))))
  (newline out)
  (unit))

10000100000

A hundred thousand suspensions grow neither the loop’s stack nor the handler’s.

Prompts

Which with-prompt catches

A suspension goes to the nearest with-prompt of its own prompt that is running around it, past those of other prompts. A with-prompt it passes is kept with the rest of the body and is around it again when the body resumes, so the two nest as calls do (tests/programs/prompt-nested.sld):

(define outer (→ (Prompt String Unit)) (make-prompt))

(define inner (→ (Prompt I64 I64)) (make-prompt))

;; Doubles what its body asks `inner` for, and tells `outer` about it
;; first: that suspension passes this `with-prompt` on its way out.
(define (doubling (body (Fn → I64))) (→ I64)
  (with-prompt inner
               body
               (lambda (n resume)
                 (suspend outer (string-append "doubling " (show n)))
                 (resume (* n 2)))))

;; Every line the body tells `outer`, then its value.
(define (told (out OutputPort) (body (Fn → I64))) (→ I64)
  (with-prompt outer
               body
               (lambda (line resume)
                 (write-line out line)
                 (resume (unit)))))

  (display out
           (told out
                 (lambda ()
                   (doubling (lambda ()
                               (suspend outer "asking")
                               (+ (suspend inner 1) (suspend inner 20)))))))
  (newline out)
asking
doubling 1
doubling 20
42

Of two with-prompts of one prompt, the inner one catches.

A prompt is a value

A prompt made at the top level is one prompt for the whole program, which any code that can name it may suspend to: failing and yielding above, and the prompt every task waits on (tasks). A prompt made inside a function is that call’s own (tests/programs/prompt-local.sld):

;; Every value `body` hands the function it is given, in order.
(define (collect (body (Fn (Fn A → Unit) → Unit))) (→ (List A)) (∀ (A))
  (let ((prompt (ann (make-prompt) (Prompt A Unit))))
    (with-prompt prompt
                 (lambda ()
                   (body (lambda (value) (suspend prompt value)))
                   (Nil))
                 (lambda (value resume) (Cons value (resume (unit)))))))

;; The collection inside sees only its own values, and the one outside
;; the rest, though both prompts carry an I64.
(define (nested) (→ (List I64))
  (collect (lambda (emit)
             (emit 1)
             (for-each emit (collect (lambda (inner) (inner 10) (emit 2) (inner 20))))
             (emit 3))))

("a" "b" "c")
(1 2 10 20 3)

The collection inside sees only its own values, though both prompts carry an I64: nothing but the function a prompt was handed to can suspend to it. find-first in the same file makes a prompt of its own for an early exit that no caller’s handler can intercept.

No with-prompt running

A suspension needs a with-prompt of its prompt running around it. With none, nothing could catch it, so suspend panics where it is called (tests/programs/prompt-unhandled.sld):

made
snail: suspend: no with-prompt of this prompt is running

What a suspension passes through

A suspension unwinds through the calls between it and its with-prompt, and resuming runs what they had left. Anything written in snail-scheme can be left and resumed. Three places are the backend’s own:

  • A function an array or string is generated by (array-generate, string-generate, and what is built on them, such as array-map) is called by a loop that cannot go on from the middle. A suspension nothing resumes passes through it; one that may be resumed panics there (tests/programs/prompt-generate.sld).
  • A cache and a top-level value keep a result. A call that suspended kept none: the next call with that key runs the function again, and the next read of the value runs its initializer again (tests/programs/prompt-kept.sld).
  • The host. A suspension cannot leave through a function the host called to reach a with-prompt running beneath it; that panics.

How the compiler knows

No type says which calls may suspend, so the compiler finds out. After specialization, when every function is concrete, it computes what each definition’s call may suspend to, and what a call of a function value of each type may, and tests for a suspension only after the calls that may have one (the compiler tour). A prompt held by a top-level value is known by name, so a with-prompt of it is known to catch its suspensions, and code that only stops, as the compiler’s passes do, pays for no frames. Code that never suspends is compiled as if suspension did not exist.

Suspension is this translation, ordinary calls and explicit frames, and not stack switching, so any WasmGC engine runs it and both hosts agree. call/cc, dynamic-wind and multi-shot continuations are not language features (notable omissions).