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

Contents

- Examples

Description

`Applicative` functors for free

Synopsis

- data Ap (f :: Type -> Type) a where
  - Pure :: forall a (f :: Type -> Type). a -> Ap f a
  - Ap :: forall (f :: Type -> Type) a1 a. f a1 -> Ap f (a1 -> a) -> Ap f 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
- iterAp :: Functor g => (g a -> a) -> Ap g a -> 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.

See Free Applicative Functors, by Paolo Capriotti and Ambrus Kaposi, for some applications.

data Ap (f :: Type -> Type) a where #

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

Constructors

Pure :: forall a (f :: Type -> Type). a -> Ap f a |
Ap :: forall (f :: Type -> Type) a1 a. f a1 -> Ap f (a1 -> a) -> Ap f a |

#### Instances

Instances details

Foldable f => Foldable (Ap f) #

    foldMap f == foldMap f . runAp toList

Instance details

Defined in Control.Applicative.Free

Methods

fold :: Monoid m => Ap f m -> m #

foldMap :: Monoid m => (a -> m) -> Ap f a -> m #

foldMap' :: Monoid m => (a -> m) -> Ap f a -> m #

foldr :: (a -> b -> b) -> b -> Ap f a -> b #

foldr' :: (a -> b -> b) -> b -> Ap f a -> b #

foldl :: (b -> a -> b) -> b -> Ap f a -> b #

foldl' :: (b -> a -> b) -> b -> Ap f a -> b #

foldr1 :: (a -> a -> a) -> Ap f a -> a #

foldl1 :: (a -> a -> a) -> Ap f a -> a #

toList :: Ap f a -> [a] #

null :: Ap f a -> Bool #

length :: Ap f a -> Int #

elem :: Eq a => a -> Ap f a -> Bool #

maximum :: Ord a => Ap f a -> a #

minimum :: Ord a => Ap f a -> a #

sum :: Num a => Ap f a -> a #

product :: Num a => Ap f a -> a #

Foldable1 f => Foldable1 (Ap f) #

    foldMap f == foldMap f . runAp toNonEmpty

Instance details

Defined in Control.Applicative.Free

Methods

fold1 :: Semigroup m => Ap f m -> m #

foldMap1 :: Semigroup m => (a -> m) -> Ap f a -> m #

foldMap1' :: Semigroup m => (a -> m) -> Ap f a -> m #

toNonEmpty :: Ap f a -> NonEmpty a #

maximum :: Ord a => Ap f a -> a #

minimum :: Ord a => Ap f a -> a #

head :: Ap f a -> a #

last :: Ap f a -> a #

foldrMap1 :: (a -> b) -> (a -> b -> b) -> Ap f a -> b #

foldlMap1' :: (a -> b) -> (b -> a -> b) -> Ap f a -> b #

foldlMap1 :: (a -> b) -> (b -> a -> b) -> Ap f a -> b #

foldrMap1' :: (a -> b) -> (a -> b -> b) -> Ap f a -> b #

Eq1 f => Eq1 (Ap f) # |
Instance details Defined in Control.Applicative.Free Methods liftEq :: (a -> b -> Bool) -> Ap f a -> Ap f b -> Bool #
Ord1 f => Ord1 (Ap f) # |
Instance details Defined in Control.Applicative.Free Methods liftCompare :: (a -> b -> Ordering) -> Ap f a -> Ap f b -> Ordering #
Applicative (Ap f) # |
Instance details Defined in Control.Applicative.Free 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 Methods fmap :: (a -> b) -> Ap f a -> Ap f b # (<$) :: a -> Ap f b -> Ap f a #
Comonad f => Comonad (Ap f) # |
Instance details Defined in Control.Applicative.Free Methods extract :: Ap f a -> a # duplicate :: Ap f a -> Ap f (Ap f a) # extend :: (Ap f a -> b) -> Ap f a -> Ap f b #
Apply (Ap f) # |
Instance details Defined in Control.Applicative.Free 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 #
(Eq1 f, Eq a) => Eq (Ap f a) # |
Instance details Defined in Control.Applicative.Free Methods (==) :: Ap f a -> Ap f a -> Bool # (/=) :: Ap f a -> Ap f a -> Bool #
(Ord1 f, Ord a) => Ord (Ap f a) # |
Instance details Defined in Control.Applicative.Free Methods compare :: Ap f a -> Ap f a -> Ordering # (<) :: Ap f a -> Ap f a -> Bool # (<=) :: Ap f a -> Ap f a -> Bool # (>) :: Ap f a -> Ap f a -> Bool # (>=) :: Ap f a -> Ap f a -> Bool # max :: Ap f a -> Ap f a -> Ap f a # min :: Ap f a -> Ap f a -> Ap f a #

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`.

iterAp :: Functor g => (g a -> a) -> Ap g a -> a #

Tear down a free `Applicative` using iteration.

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