Control.Applicative.Free.Final

Contents

Description

Final encoding of free Applicative functors.

Synopsis

Documentation

Compared to the free monad, they are less expressive. However, they are also more flexible to inspect and interpret, as the number of ways in which the values can be nested is more limited.

newtype Ap f a Source #

The free Applicative for a Functor f.

Constructors

 Ap Fields_runAp :: forall g. Applicative g => (forall x. f x -> g x) -> g a
Instances
 Functor (Ap f) Source # Instance detailsDefined in Control.Applicative.Free.Final Methodsfmap :: (a -> b) -> Ap f a -> Ap f b #(<\$) :: a -> Ap f b -> Ap f a # Source # Instance detailsDefined in Control.Applicative.Free.Final Methodspure :: a -> Ap f a #(<*>) :: Ap f (a -> b) -> Ap f a -> Ap f b #liftA2 :: (a -> b -> c) -> Ap f a -> Ap f b -> Ap f c #(*>) :: Ap f a -> Ap f b -> Ap f b #(<*) :: Ap f a -> Ap f b -> Ap f a # Apply (Ap f) Source # Instance detailsDefined in Control.Applicative.Free.Final Methods(<.>) :: Ap f (a -> b) -> Ap f a -> Ap f b #(.>) :: Ap f a -> Ap f b -> Ap f b #(<.) :: Ap f a -> Ap f b -> Ap f a #liftF2 :: (a -> b -> c) -> Ap f a -> Ap f b -> Ap f c #

runAp :: Applicative g => (forall x. f x -> g x) -> Ap f a -> g a Source #

Given a natural transformation from f to g, this gives a canonical monoidal natural transformation from Ap f to g.

runAp t == retractApp . hoistApp t

runAp_ :: Monoid m => (forall a. f a -> m) -> Ap f b -> m Source #

Perform a monoidal analysis over free applicative value.

Example:

count :: Ap f a -> Int
count = getSum . runAp_ (\_ -> Sum 1)


liftAp :: f a -> Ap f a Source #

A version of lift that can be used with just a Functor for f.

hoistAp :: (forall a. f a -> g a) -> Ap f b -> Ap g b Source #

Given a natural transformation from f to g this gives a monoidal natural transformation from Ap f to Ap g.

retractAp :: Applicative f => Ap f a -> f a Source #

Interprets the free applicative functor over f using the semantics for pure and <*> given by the Applicative instance for f.

retractApp == runAp id