Module

Control.Monad.Cont

Package
purescript-transformers
Repository
purescript/purescript-transformers

This module defines the Continuation monad.

#Cont Source

type Cont r = ContT r Identity

The Cont monad is a synonym for the ContT monad transformer applied to the Identity monad.

#cont Source

cont :: forall a r. ((a -> r) -> r) -> Cont r a

Creates a computation in the Cont monad.

#runCont Source

runCont :: forall r a. ContT r Identity a -> (a -> r) -> r

Runs a computation in the Cont monad.

#mapCont Source

mapCont :: forall r a. (r -> r) -> Cont r a -> Cont r a

Transform the result of a continuation-passing function.

#withCont Source

withCont :: forall a b r. ((b -> r) -> (a -> r)) -> Cont r a -> Cont r b

Transform the continuation passed into the continuation-passing function.

Re-exports from Control.Monad.Cont.Class

#MonadCont Source

class MonadCont :: (Type -> Type) -> Constraintclass (Monad m) <= MonadCont m  where

The MonadCont type class represents those monads which support the callCC, or call-with-current-continuation operation.

This action makes the current continuation available to the caller.

For example:

-- setTimeout :: Number -> Effect Unit -> Effect Unit

delay :: Number -> ContT Unit Effect Unit
delay n = callCC \cont ->
  lift $ setTimeout n (runContT (cont unit) (\_ -> pure unit))

An implementation is provided for ContT, and for other monad transformers defined in this library.

Members

  • callCC :: forall a. ((forall b. a -> m b) -> m a) -> m a

Re-exports from Control.Monad.Cont.Trans

#ContT Source

newtype ContT :: forall k. k -> (k -> Type) -> Type -> Typenewtype ContT r m a

The CPS monad transformer.

This monad transformer extends the base monad with the operation callCC.

Constructors

  • ContT ((a -> m r) -> m r)

Instances

#MonadTrans Source

class MonadTrans :: ((Type -> Type) -> Type -> Type) -> Constraintclass MonadTrans t  where

The MonadTrans type class represents monad transformers.

A monad transformer is a type constructor of kind (* -> *) -> * -> *, which takes a Monad as its first argument, and returns another Monad.

This allows us to add additional effects to an existing monad. By iterating this process, we create monad transformer stacks, which contain all of the effects required for a particular computation.

The laws state that lift is a Monad morphism.

Laws:

  • lift (pure a) = pure a
  • lift (do { x <- m ; y }) = do { x <- lift m ; lift y }

Members

#withContT Source

withContT :: forall r m a b. ((b -> m r) -> (a -> m r)) -> ContT r m a -> ContT r m b

Modify the continuation in a ContT monad action

#runContT Source

runContT :: forall r m a. ContT r m a -> (a -> m r) -> m r

Run a computation in the ContT monad, by providing a continuation.

#mapContT Source

mapContT :: forall r m a. (m r -> m r) -> ContT r m a -> ContT r m a

Modify the underlying action in a ContT monad action.