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 fcalls the error handlerfif 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 fupdates the state using the functionf.
An implementation is provided for StateT, and for other monad transformers
defined in this library.
Laws:
do { get ; get } = getdo { put x ; put y } = put ydo { put x ; get } = put x $> xdo { 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 alift (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.
The monad in which each individual rule is run.
The MonadStore class captures monads which implement a stored value,
along with methods to get, update (via an action type, a), or subscribe
to changes in the stored value.
An instance is provided for StoreT, which is the standard way to use
the MonadStore class.
The Monad instance guarantees that there are both Applicative and
Bind instances for Either.
The MonadEffect class captures those monads which support native effects.
Instances are provided for Effect itself, and the standard monad
transformers.
liftEffect can be used in any appropriate monad transformer stack to lift an
action of type Effect a into the monad.
An extension of the MonadAsk class that introduces a function local f x
that allows the value of the local context to be modified for the duration
of the execution of action x.
An implementation is provided for ReaderT, and for other monad
transformers defined in this library.
Laws in addition to the MonadAsk law:
local f ask = f <$> asklocal _ (pure a) = pure alocal f (do { a <- x ; y }) = do { a <- local f x ; local f y }
An extension of the MonadTell class that introduces some operations on
the accumulator:
listenmodifies the result to include the changes to the accumulator.passapplies the returned function to the accumulator.
An implementation is provided for WriterT, and for other monad
transformers defined in this library.
Laws in addition to the MonadTell law:
do { tell x ; tell y } = tell (x <> y)listen (pure a) = pure (Tuple a mempty)listen (writer a x) = tell x $> Tuple a x
This instance is provided for compatibility. Aff is always stack-safe
within a given fiber. This instance will just result in unnecessary
bind overhead.
This instance is provided for compatibility, but is otherwise
unnecessary. You can use monadic recursion with Run, deferring the
MonadRec constraint till it is interpretted.
Monads which allow their actions to be run in a base monad.
MonadUnlift captures the opposite notion of MonadBase - while
MonadBase allows any base monad b to be lifted into a transformed monad
m, MonadUnlift allows m to be run in b, as long as the outer
context is in m.
Note that the laws given below require that a monad have no "monadic
state", which essentially limits instances to ReaderT and IdentityT
stacks.
Instances should satisfy the following laws, which state that
unlift is a transformer of monads for any given u returned by
askUnlift:
unlift u <<< pure = pure
unlift u (f =<< m) = unlift u <<< f =<< unlift u m
A collection of loop operators for use in monads