resumable evaluates an expression while establishing named resumption handlers that can be invoked with resume-to.
Syntax
(resumable ⟨expr⟩
((⟨resumption-ctor⟩ ⟨pattern⟩*) ⟨body-element⟩* ⟨expr⟩)
...)
Semantics
- If
⟨expr⟩finishes without invoking a matching resumption,resumablereturns the value of⟨expr⟩. - Each branch head
(⟨resumption-ctor⟩ ⟨pattern⟩*)(optionally wrapped in an alias pattern(= ⟨var⟩ ...)) must match a constructor for a resumption type defined withdefine-resumption(bare variable or_branch heads are not permitted). - Multiple branches may match the same resumption constructor with different field subpatterns; patterns on constructor fields are tested in source order before unwinding.
- If
resume-tois called with a resumption value that does not match any branch of the innerresumable, the value is forwarded to the next enclosingresumablehandler for that resumption type. If no enclosingresumablematches, a runtime error is signaled. - Once a branch matches, control unwinds to the
resumableexpression and evaluates the selected branch body (an implicitprogn). - The final expression of each branch body is in tail position when the
resumableexpression is in tail position (including forrectail calls). ⟨expr⟩and all branch bodies must unify to the same single-value result type.
Examples
(coalton-toplevel
(define-exception ParseIntError
(BadDigit Char))
(define-resumption SkipDigit)
(define-resumption (SubstituteDigit Integer))
(declare parse-digit (Char -> (Optional Integer)))
(define (parse-digit c)
(resumable
(match c
(#\0 (Some 0))
(#\1 (Some 1))
(_ (throw (BadDigit c))))
((SkipDigit) None)
((SubstituteDigit d) (Some d))))
(declare parse-binary-default-zero (Char -> (Optional Integer)))
(define (parse-binary-default-zero c)
(handle (parse-digit c)
((BadDigit _) (resume-to (SubstituteDigit 0))))))
Other notes
resumablecompiles to Common Lispcl:restart-bind: each distinct resumption type establishes a restart that tests branch field patterns before unwinding, chains to any outer restart of the same name on mismatch, and unwinds to execute the matching branch body upon selection.- See
define-resumption,resume-to, andhandle.