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