The Monad
typeclass allows treating explicitly the sequencing of actions inside a given context.
The Monad
typeclass extends the Applicative
typeclass.
The Monad
typeclass adds a single method to the Applicative
typeclass, providing the ability of applying a function,
which produces a result in a context, to a value already in that context.
interface Monad extends Applicative
{
/**
* @param F<A> $a
* @param callable(A): F<B> $f
* @return F<B>
*/
public function bind($a, callable $f);
}
Its simplified signature is
bind :: (F<A>, A -> F<B>) -> F<B>
The ExtraMonad
class provides on additional method which could be used with Monad
instances, which allow removing
additional layers of the wrapping datatype
final class ExtraMonad
{
/**
* @param F<F<A>> $ffa
* @return F<A>
*/
public function join($ffa)
}
Its simplified signature is
join :: F<F<A>> -> F<A>
A Monad
instance should satisfy three more laws in addition to the ones specified by Functor
, Apply
and
Applicative
$monad->bind($monad->pure($a), $f) == $f($a)
$monad->bind($a, fn($x) => $monad->pure($x)) == $a
$monad->bind($a, fn($x) => $monad->bind($f($x), $g)) == $monad->bind($monad->bind($a, $g), $g)
EitherMonad
IdentityMonad
IOMonad
LinkedListMonad
MaybeMonad
PairMonad
ReaderMonad
StateMonad