Skip to content

Proposal : ContT : minimal usage example of shift/reset #85

@sjshuck

Description

@sjshuck

[original issue 81 by @ocramz]

Since delimited continuations are far from intuitive, I would like to add at least one usage example to their haddocks, something along these lines :

-- | demonstration of non-local control flow with a single shift/reset pair
--
-- λ> flip runState [] $ evalContT t1
-- (2,[2,0,1])
t1 :: ContT Int (State [Int]) Int
t1 = resetT $ do
  let
    x = 1 -- input
    cons w = lift $ modify (w :)
  r <- shiftT $ \k -> do
    cons x -- initial state uses the input
    let y = succ x -- compute a function of the input
    z <- lift $ k y -- delegate to the continuation k
    cons z -- mutate state with the return value of k
    pure y
  cons 0
  pure r

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions