define-resumption defines a single-constructor resumption type used with resumable and resume-to.
Syntax
;; Nullary resumption
(define-resumption ⟨name⟩
⟨docstring⟩?)
;; Resumption carrying payload fields
(define-resumption (⟨name⟩ ⟨field-type⟩*)
⟨docstring⟩?)
Semantics
define-resumptionis a top-level definition form valid insidecoalton-toplevel.- It defines both a resumption type
⟨name⟩and its single constructor⟨name⟩in the current package:- When written
⟨name⟩or(⟨name⟩),⟨name⟩is a nullary constructor value of type⟨name⟩. - When written
(⟨name⟩ t1 ... tn)with $n \ge 1$ fields,⟨name⟩is a constructor function of type(t1 * ... * tn -> ⟨name⟩).
- When written
- Resumption types do not accept type variables,
repr, orderiveattributes. - Every resumption type automatically receives an instance of the marker class
Resumption(required byresume-to) andRuntimeRepr. Manual instances ofResumptioncannot be written, and a resumption type cannot be redefined as a non-resumption type. - Resumptions represent structured recovery protocols established by
resumableand invoked viaresume-to, typically from inside ahandlebranch before the stack unwinds.
Examples
(coalton-toplevel
(define-exception MissingKey
(KeyNotFound String))
;; Nullary resumption and payload-carrying resumption
(define-resumption SkipEntry
"Skip the missing entry entirely.")
(define-resumption (UseFallback Integer)
"Supply a fallback integer for the missing entry.")
(declare lookup-with-resumptions ((Optional Integer) -> (Optional Integer)))
(define (lookup-with-resumptions entry)
(resumable
(match entry
((Some v) (Some v))
((None) (throw (KeyNotFound "x"))))
((SkipEntry) None)
((UseFallback alt) (Some alt))))
(declare lookup-defaulting ((Optional Integer) -> (Optional Integer)))
(define (lookup-defaulting entry)
(handle (lookup-with-resumptions entry)
((KeyNotFound _) (resume-to (UseFallback 0))))))