Search results
The Monad
type class combines the operations of the Bind
and
Applicative
type classes. Therefore, Monad
instances represent type
constructors which support sequential composition, and also lifting of
functions of arbitrary arity.
Instances must satisfy the following laws in addition to the
Applicative
and Bind
laws:
- Left Identity:
pure x >>= f = f x
- Right Identity:
x >>= pure = x
This type class captures those monads which support tail recursion in constant stack space.
The tailRecM
function takes a step function, and applies that step
function recursively until a pure value of type b
is found.
Instances are provided for standard monad transformers.
For example:
loopWriter :: Int -> WriterT (Additive Int) Effect Unit
loopWriter n = tailRecM go n
where
go 0 = do
traceM "Done!"
pure (Done unit)
go i = do
tell $ Additive i
pure (Loop (i - 1))
A class for random generator implementations.
Instances should provide implementations for the generation functions that return choices with uniform probability.
See also Gen
in purescript-quickcheck
, which implements this
type class.
The MonadAsk
type class represents those monads which support a global
context that can be provided via the ask
function.
An implementation is provided for ReaderT
, and for other monad
transformers defined in this library.
Law:
do { ask ; ask } = ask
Type class for monads that support fixpoints.
mfix f
runs f
once with the eventual result of f
as input. Make sure
not to apply the supplied function until the computation returned; else
a dynamic error will be thrown.
A "type class alias" for the constraints required by most FRP primitives.
The MonadPlus
type class has no members of its own; it just specifies
that the type has both Monad
and Alternative
instances.
Types which have MonadPlus
instances should also satisfy the following
law:
- Distributivity:
(x <|> y) >>= f == (x >>= f) <|> (y >>= f)
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.
The MonadTell w
type class represents those monads which support a
monoidal accumulator of type w
, where tell
appends a value to the
accumulator.
An implementation is provided for WriterT
, and for other monad
transformers defined in this library.
Law:
do { tell x ; tell y } = tell (x <> y)
Based on http://hackage.haskell.org/package/free/docs/Control-Monad-Free-Class.html
Represents Monads which can be forked asynchronously.
Laws:
-- Unjoined suspension is a no-op
suspend a1 *> suspend a2 = suspend a2
-- Suspend/join is identity
suspend >=> join = id
-- Fork/join is identity
fork >=> join = id
-- Join is idempotent
join t *> join t = join t
Represents Monads which can be killed after being forked.
Laws:
-- Killed suspension is an exception
suspend a >>= \f -> kill e f *> join f = throwError e
-- Suspend/kill is unit
suspend a >>= kill e = pure unit
The Monad
instance guarantees that there are both Applicative
and
Bind
instances for Maybe
. This also enables the do
syntactic sugar:
do
x' <- x
y' <- y
pure (f x' y')
Which is equivalent to:
x >>= (\x' -> y >>= (\y' -> pure (f x' y')))
Which is equivalent to:
case x of
Nothing -> Nothing
Just x' -> case y of
Nothing -> Nothing
Just y' -> Just (f x' y')
The MonadError
type class represents those monads which support catching
errors.
catchError x f
calls the error handlerf
if an error is thrown during the evaluation ofx
.
An implementation is provided for ExceptT
, and for other monad transformers
defined in this library.
Laws:
- Catch:
catchError (throwError e) f = f e
- Pure:
catchError (pure a) f = pure a
The MonadState s
type class represents those monads which support a single piece of mutable
state of type s
.
state f
updates the state using the functionf
.
An implementation is provided for StateT
, and for other monad transformers
defined in this library.
Laws:
do { get ; get } = get
do { put x ; put y } = put y
do { put x ; get } = put x $> x
do { s <- get ; put s } = pure unit
The MonadThrow
type class represents those monads which support errors via
throwError
, where throwError e
halts, yielding the error e
.
An implementation is provided for ExceptT
, and for other monad transformers
defined in this library.
Laws:
- Left zero:
throwError e >>= f = throwError e
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 }
This class is for Monads whose side effect includes delayed computation. So far, it's Effect and Hooks.