Control.Applicative.Trans.Free

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.Trans.Free

Contents

- Free Applicative
- Free Alternative

Description

`Applicative` functor transformers for free

Synopsis

- newtype ApT (f :: Type -> Type) (g :: Type -> Type) a = ApT {
  - getApT :: g (ApF f g a)
  }
- data ApF (f :: Type -> Type) (g :: Type -> Type) a where
  - Pure :: forall a (f :: Type -> Type) (g :: Type -> Type). a -> ApF f g a
  - Ap :: forall (f :: Type -> Type) a1 (g :: Type -> Type) a. f a1 -> ApT f g (a1 -> a) -> ApF f g a
- liftApT :: forall (g :: Type -> Type) f a. Applicative g => f a -> ApT f g a
- liftApO :: forall g a (f :: Type -> Type). Functor g => g a -> ApT f g a
- runApT :: (Applicative h, Functor g) => (forall a. f a -> h a) -> (forall a. g (h a) -> h a) -> ApT f g b -> h b
- runApF :: (Applicative h, Functor g) => (forall a. f a -> h a) -> (forall a. g (h a) -> h a) -> ApF f g b -> h b
- runApT_ :: (Functor g, Monoid m) => (forall a. f a -> m) -> (g m -> m) -> ApT f g b -> m
- hoistApT :: forall (g :: Type -> Type) f f' b. Functor g => (forall a. f a -> f' a) -> ApT f g b -> ApT f' g b
- hoistApF :: forall (g :: Type -> Type) f f' b. Functor g => (forall a. f a -> f' a) -> ApF f g b -> ApF f' g b
- transApT :: forall g g' (f :: Type -> Type) b. Functor g => (forall a. g a -> g' a) -> ApT f g b -> ApT f g' b
- transApF :: forall g g' (f :: Type -> Type) b. Functor g => (forall a. g a -> g' a) -> ApF f g b -> ApF f g' b
- joinApT :: forall m (f :: Type -> Type) a. Monad m => ApT f m a -> m (Ap f a)
- type Ap (f :: Type -> Type) = ApT f Identity
- runAp :: Applicative g => (forall x. f x -> g x) -> Ap f a -> g a
- runAp_ :: Monoid m => (forall x. f x -> m) -> Ap f a -> m
- retractAp :: Applicative f => Ap f a -> f a
- type Alt (f :: Type -> Type) = ApT f []
- runAlt :: forall g (t :: Type -> Type) f a. (Alternative g, Foldable t) => (forall x. f x -> g x) -> ApT f t a -> g a

# Documentation

Compared to the free monad transformers, 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.

newtype ApT (f :: Type -> Type) (g :: Type -> Type) a #

The free `Applicative` transformer for a `Functor` `f` over `Applicative` `g`.

Constructors

ApT |
Fields getApT :: g (ApF f g a)

#### Instances

Instances details

Alternative g => Alternative (ApT f g) # |
Instance details Defined in Control.Applicative.Trans.Free Methods empty :: ApT f g a # (<|>) :: ApT f g a -> ApT f g a -> ApT f g a # some :: ApT f g a -> ApT f g [a] # many :: ApT f g a -> ApT f g [a] #
Applicative g => Applicative (ApT f g) # |
Instance details Defined in Control.Applicative.Trans.Free Methods pure :: a -> ApT f g a # (<*>) :: ApT f g (a -> b) -> ApT f g a -> ApT f g b # liftA2 :: (a -> b -> c) -> ApT f g a -> ApT f g b -> ApT f g c # (*>) :: ApT f g a -> ApT f g b -> ApT f g b # (<*) :: ApT f g a -> ApT f g b -> ApT f g a #
Functor g => Functor (ApT f g) # |
Instance details Defined in Control.Applicative.Trans.Free Methods fmap :: (a -> b) -> ApT f g a -> ApT f g b # (<$) :: a -> ApT f g b -> ApT f g a #
Applicative g => Apply (ApT f g) # |
Instance details Defined in Control.Applicative.Trans.Free Methods (<.>) :: ApT f g (a -> b) -> ApT f g a -> ApT f g b # (.>) :: ApT f g a -> ApT f g b -> ApT f g b # (<.) :: ApT f g a -> ApT f g b -> ApT f g a # liftF2 :: (a -> b -> c) -> ApT f g a -> ApT f g b -> ApT f g c #

data ApF (f :: Type -> Type) (g :: Type -> Type) a where #

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

Constructors

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

#### Instances

Instances details

Applicative g => Applicative (ApF f g) # |
Instance details Defined in Control.Applicative.Trans.Free Methods pure :: a -> ApF f g a # (<*>) :: ApF f g (a -> b) -> ApF f g a -> ApF f g b # liftA2 :: (a -> b -> c) -> ApF f g a -> ApF f g b -> ApF f g c # (*>) :: ApF f g a -> ApF f g b -> ApF f g b # (<*) :: ApF f g a -> ApF f g b -> ApF f g a #
Functor g => Functor (ApF f g) # |
Instance details Defined in Control.Applicative.Trans.Free Methods fmap :: (a -> b) -> ApF f g a -> ApF f g b # (<$) :: a -> ApF f g b -> ApF f g a #
Applicative g => Apply (ApF f g) # |
Instance details Defined in Control.Applicative.Trans.Free Methods (<.>) :: ApF f g (a -> b) -> ApF f g a -> ApF f g b # (.>) :: ApF f g a -> ApF f g b -> ApF f g b # (<.) :: ApF f g a -> ApF f g b -> ApF f g a # liftF2 :: (a -> b -> c) -> ApF f g a -> ApF f g b -> ApF f g c #

liftApT :: forall (g :: Type -> Type) f a. Applicative g => f a -> ApT f g a #

A version of `lift` that can be used with no constraint for `f`.

liftApO :: forall g a (f :: Type -> Type). Functor g => g a -> ApT f g a #

Lift an action of the "outer" `Functor` `g a` to ``ApT` f g a`.

runApT :: (Applicative h, Functor g) => (forall a. f a -> h a) -> (forall a. g (h a) -> h a) -> ApT f g b -> h b #

Given natural transformations `f ~> h` and `g . h ~> h` this gives a natural transformation `ApT f g ~> h`.

runApF :: (Applicative h, Functor g) => (forall a. f a -> h a) -> (forall a. g (h a) -> h a) -> ApF f g b -> h b #

Given natural transformations `f ~> h` and `g . h ~> h` this gives a natural transformation `ApF f g ~> h`.

runApT_ :: (Functor g, Monoid m) => (forall a. f a -> m) -> (g m -> m) -> ApT f g b -> m #

Perform a monoidal analysis over ``ApT` f g b` value.

Examples:

    height :: (Functor g, Foldable g) => ApT f g a -> Int
    height = getSum . runApT_ (_ -> Sum 1) maximum

    size :: (Functor g, Foldable g) => ApT f g a -> Int
    size = getSum . runApT_ (_ -> Sum 1) fold

hoistApT :: forall (g :: Type -> Type) f f' b. Functor g => (forall a. f a -> f' a) -> ApT f g b -> ApT f' g b #

Given a natural transformation from `f` to `f'` this gives a monoidal natural transformation from `ApT f g` to `ApT f' g`.

hoistApF :: forall (g :: Type -> Type) f f' b. Functor g => (forall a. f a -> f' a) -> ApF f g b -> ApF f' g b #

Given a natural transformation from `f` to `f'` this gives a monoidal natural transformation from `ApF f g` to `ApF f' g`.

transApT :: forall g g' (f :: Type -> Type) b. Functor g => (forall a. g a -> g' a) -> ApT f g b -> ApT f g' b #

Given a natural transformation from `g` to `g'` this gives a monoidal natural transformation from `ApT f g` to `ApT f g'`.

transApF :: forall g g' (f :: Type -> Type) b. Functor g => (forall a. g a -> g' a) -> ApF f g b -> ApF f g' b #

Given a natural transformation from `g` to `g'` this gives a monoidal natural transformation from `ApF f g` to `ApF f g'`.

joinApT :: forall m (f :: Type -> Type) a. Monad m => ApT f m a -> m (Ap f a) #

Pull out and join `m` layers of ``ApT` f m a`.

# Free Applicative

type Ap (f :: Type -> Type) = ApT f Identity #

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

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 x. f x -> m) -> Ap f a -> m #

Perform a monoidal analysis over free applicative value.

Example:

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

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

# Free Alternative

type Alt (f :: Type -> Type) = ApT f [] #

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

runAlt :: forall g (t :: Type -> Type) f a. (Alternative g, Foldable t) => (forall x. f x -> g x) -> ApT f t a -> g a #

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