Skip to content

Latest commit

 

History

History
86 lines (63 loc) · 1.54 KB

Monad.md

File metadata and controls

86 lines (63 loc) · 1.54 KB

Monad

The Monad typeclass allows treating explicitly the sequencing of actions inside a given context.

Parent

The Monad typeclass extends the Applicative typeclass.

API

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>

Extra

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>

Laws

A Monad instance should satisfy three more laws in addition to the ones specified by Functor, Apply and Applicative

Left identity

$monad->bind($monad->pure($a), $f) == $f($a)

Right identity

$monad->bind($a, fn($x) => $monad->pure($x)) == $a

Associativity

$monad->bind($a, fn($x) => $monad->bind($f($x), $g)) == $monad->bind($monad->bind($a, $g), $g)

Implemented instances

  • EitherMonad
  • IdentityMonad
  • IOMonad
  • LinkedListMonad
  • MaybeMonad
  • PairMonad
  • ReaderMonad
  • StateMonad