Skip to content

Latest commit

 

History

History
68 lines (48 loc) · 1.37 KB

Applicative.md

File metadata and controls

68 lines (48 loc) · 1.37 KB

Applicative

A data structure admits an Applicative instance if it allows lifting values into the data structure.

Parent

The Applicative typeclass extends the Apply typeclass.

API

The Applicative typeclass adds a single method which allows wrapping values in the data structure.

interface Applicative extends Apply
{
    /**
     * @param A $a
     * @return F<A>
     */
    public function pure($a);
}

Its simplified signature is

pure :: A -> F<A>

Laws

The Applicative typeclass needs to satisfy four laws in addition to the Apply and Functor ones.

Identity

$applicative->apply($applicative->pure(fn($x) => $x), $y) == $y

Composition

$applicative->apply($applicative->apply($applicative->apply($applicative->pure(fn ($f, $g) => fn ($x) => $f($g($x))), $a), $b), $c) == $applicative->apply($a, $applicative->apply($b, $c))

Homomorphism

$applicative->apply($applicative->pure($f), $applicative->pure($x)) == $applicative->pure($f($x))

Interchange

$applicative->apply($f, $applicative->pure($x)) == $applicative->apply(fn (callable $g) => $g($x), $f)

Implemented instances

  • EitherApplicative
  • ValidationApplicative
  • IdentityApplicative
  • IOApplicative
  • LinkedListApplicative
  • MaybeApplicative
  • PairApplicative
  • ReaderApplicative
  • StateApplicative