F-operator
TOC »
Documentation
The static delimited continuation operators shift and reset.
Shift/Reset - Dynamically scoped shift/reset
(import shift-reset)
A "stuck on control" situation, a 'shift' without an enclosing 'reset', is an error.
reset
- (reset EXPRESSION ...)syntax
Evaluate the body EXPRESSION ... with a delimited continuation. The body will contain one or more instances of (shift ...).
shift
- (shift PC-TAG EXPRESSION)syntax
Within the scope of EXPRESSION PC-TAG is bound to the reified partial continuation delimited by the enclosing (reset ...). Provide a value to the partial continuation using the form (PC-TAG <something>).
reset-values
- (reset-values EXPRESSION ...)syntax
Multiple value return version of (reset ...). The body will contain one or more instances of (shift-values ...).
shift-values
- (shift-values PC-TAG EXPRESSION)syntax
Multiple value return version of (shift ...). Provide a value to the partial continuation using the form (PC-TAG <something> ...).
BShift/BReset - Statically scoped shift/reset
(import bshift-breset)
Invalid delimited continuations, what RC-TAG below represents, and stuck on control will generate an error
breset
- (breset RC-TAG EXPRESSION ...)syntax
Evaluate the body EXPRESSION ... with a delimited continuation named RC-TAG. The body will contain one or more instances of (bshift RC-TAG ...).
bshift
- (bshift RC-TAG PC-TAG EXPRESSION)syntax
Within the scope of EXPRESSION PC-TAG is bound to the reified partial continuation delimited by the enclosing (breset RC-TAG ...). Provide a value to the partial continuation using the form (PC-TAG <something>).
breset-values
- (breset-values RC-TAG EXPRESSION ...)syntax
Multiple value return version of (breset ...). The body will contain one or more instances of (bshift-values RC-TAG ...).
bshift-values
- (bshift-values RC-TAG PC-TAG EXPRESSION)syntax
Multiple value return version of (bshift ...). Provide a value to the partial continuation using the form (PC-TAG <something> ...).
Range
(import range)
range
- (range RC-TAG FROM VALUE STEP TO?)syntax
The value of the delimited continuation RC-TAG ranges over the set of values specified by the state generation procedure suite. For use with (breset ...)
- FROM
- Zero argument procedure, returning the initial state
- VALUE
- Single argument procedure, of the state, returning the value of the state
- STEP
- Single argument procedure, of the state, returning the next state
- TO?
- Single argument procedure, of the state, returning {{#t) when the range is complete
range
- (range RC-TAG FROM [STEP] TO)syntax
The value of the delimited continuation RC-TAG ranges over the number interval [FROM TO], by STEP. The increment is 1 when missing. For use with (breset ...).
Reflect/Reify - Monads
(import reflect-reify)
The Monad example from Filinski, POPL '94
define-unit
- (define-unit KIND BODY ...)syntax
Expands to (define (KIND-unit obj) BODY ...).
define-bind
- (define-bind KIND BODY ...)syntax
Expands to (define (KIND-bind monad func) BODY ...).
reflect
- (reflect KIND MONAD)syntax
Extract value from MONAD. Plays the role of Haskell '<-'.
reflect-values
- (reflect-values KIND MONAD)syntax
Extract value from MONAD. Plays the role of Haskell '<-'.
reify
- (reify KIND EXPRESSION)syntax
Return result of EXPRESSION as a monad.
reify-values
- (reify-values KIND EXPRESSION)syntax
Return result of EXPRESSION as a monad.
GShift/GReset - Generalized shift/reset
(import gshift-greset)
The generalized shift and reset operator family from How to remove a dynamic prompt: static and dynamic delimited continuation operators are equally expressible
greset
- (greset HR E)syntax
Reset parameterized by the H Reset procedure HR.
gshift
- (gshift HS F E)syntax
Shift parameterized by the H Shift procedure HS.
hr-stop
- hr-stop Vprocedure
H Reset Stop.
hs-stop
- hs-stop Vprocedure
H Shift Stop
hr-prop
- hr-prop Vprocedure
H Reset Propagate.
hs-prop
- hs-prop Vprocedure
H Shift Propagate.
h-compose
- h-compose F Xprocedure
Returns the composition of F and X as an h-datatype.
h-value
- h-value Vprocedure
Returns the value of V as an h-datatype.
h-datatype?
- h-datatype? OBJECTprocedure
Is OBJECT an h-datatype?
h-cases
- (h-cases E ((F X) ON-h-EXPR) (V ON-V-EXPR))syntax
Deconstructs the h-datatype E, binding F & X for an evaluation of the ON-h-EXPR and V for an evaluation of the ON-V-EXPR.
Delimited Control
(import delimited-control)
Generalized shift/reset implementations of some control operators.
Within the scope of EXPRESSION PP is bound to the reified partial continuation delimited by the enclosing (prompt/prompt0/reset/reset0 ...). Provide a value to the partial continuation using the form (PP <something>).
prompt
- (prompt EXPR)syntax
prompt0
- (prompt0 EXPR)syntax
reset
- (reset EXPR)syntax
reset0
- (reset0 EXPR)syntax
control
- (control PP EXPR)syntax
control-abort
- (control-abort V)syntax
control0
- (control0 PP EXPR)syntax
shift
- (shift PP EXPR)syntax
shift0
- (shift0 PP EXPR)syntax
Examples
(import shift-reset srfi-41 srfi-45) (define (iteration-procedure->stream iteration-procedure) (reset (iteration-procedure (lambda (element) (shift continue-iteration (stream-cons element (lazy (continue-iteration (void))))))) stream-null)) (define (my-list->stream list) (iteration-procedure->stream (lambda (receiver) (for-each receiver list))))
Notes
- The reflect-reify module (Monads) is an example of the use of shift/reset. Not a general purpose implementation for use in Scheme monadic programming.
- The distinction between the single-valued return & multi-valued return versions of the control operators, ex: shift & shift-values, is probably confusing.
Requirements
Bugs and Limitations
- Single Thread Only
- Not a direct implementation of partial continuations. Simulated using full continuations. As such may be prey to the issues elaborated by "call/cc implements shift? A good question".
- The distinction between the single-valued return & multi-valued return versions of the control operators, ex: shift & shift-values. Unlike the CHICKEN 4 egg, the current single-valued syntax cannot handle multi-value returns, due to the type signature.
Author
Version history
- 4.1.0
- Added delimited-control module.
- 4.0.0
- CHICKEN 5 release.
- 3.0.0
- Remove %reset, %shift, %reset-values, %shift-values, %breset, %bshift, %breset-values, %bshift-values, %range, %reflect, & %reify.
- 2.0.4
- Has argc-checks.
- 2.0.3
- Use er-macro-transformer.
- 2.0.2
- Has lambda-info.
- 2.0.1
- Added miscmacros as a dependency [Ivan Raikov].
- 2.0.0
- Initial Chicken 4 release.
License
Copyright (C) 2009-2022 Kon Lovett. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Does not supercede any restrictions found in the source code.