Control.Applicative.Free.Final

free-5.2: Monads for free

Copyright | (C) 2012-2013 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | provisional
Portability | GADTs, Rank2Types
Safe Haskell | Safe
Language | Haskell2010

Control.Applicative.Free.Final

Contents

- Examples

Description

Final encoding of free `Applicative` functors.

Synopsis

- newtype Ap (f :: Type -> Type) a = Ap {
  - _runAp :: forall (g :: Type -> Type). Applicative g => (forall x. f x -> g x) -> g a
  }
- runAp :: Applicative g => (forall x. f x -> g x) -> Ap f a -> g a
- runAp_ :: Monoid m => (forall a. f a -> m) -> Ap f b -> m
- liftAp :: f a -> Ap f a
- hoistAp :: (forall a. f a -> g a) -> Ap f b -> Ap g b
- retractAp :: Applicative f => Ap f a -> f a

# 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 :: Type -> Type) a #

The free `Applicative` for a `Functor` `f`.

Constructors

Ap |
Fields _runAp :: forall (g :: Type -> Type). Applicative g => (forall x. f x -> g x) -> g a

#### Instances

Instances details

Applicative (Ap f) # |
Instance details Defined in Control.Applicative.Free.Final Methods pure :: 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 #
Functor (Ap f) # |
Instance details Defined in Control.Applicative.Free.Final Methods fmap :: (a -> b) -> Ap f a -> Ap f b # (<$) :: a -> Ap f b -> Ap f a #
Apply (Ap f) # |
Instance details Defined 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 #

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 #

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 #

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 #

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 #

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

    retractApp == runAp id

# Examples

Validation form