Delimited continuations
tkersey opened this issue · 0 comments
tkersey commented
/// Captures the continuation up to the nearest enclosing reset
and passes it
/// to the given function.
public func shift<R, I, J, O, A>(_ f : @escaping ((A) -> IxCont<I, I, O>) -> IxCont<R, J, J>) -> IxCont<R, O, A> {
return IxCont { k in run(f { pure(k($0)) }) }
}
/// Delimits the continuation of any shift
inside.
public func reset<R, O, A>(_ a : IxCont<A, O, O>) -> IxCont<R, R, A> {
return pure(run(a))
}
/// callCC
(call-with-current-continuation) calls its argument function,
/// passing it the current continuation. It provides an escape continuation
/// mechanism for use with the indexed continuation monad.
///
/// Escape continuations one allow to abort the current computation and return a
/// value immediately, much like do-catch
blocks in Swift.
public func callCC<R, O, A, B>(_ f : @escaping ((A) -> IxCont<O, O, B>) -> IxCont<R, O, A>) -> IxCont<R, O, A> {
return IxCont({ k in (f { x in IxCont { _ in k(x) } }).run(k) })
}