free-5.1.10: 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

Description

Applicative functors for free

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.

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

data Ap f a where Source #

The free Applicative for a Functor f .

Constructors

Pure :: a -> Ap f a
Ap :: f a -> Ap f (a -> b) -> Ap f b

Instances

Instances details
Functor ( Ap f) Source #
Instance details

Defined in Control.Applicative.Free

Methods

fmap :: (a -> b) -> Ap f a -> Ap f b Source #

(<$) :: a -> Ap f b -> Ap f a Source #

Applicative ( Ap f) Source #
Instance details

Defined in Control.Applicative.Free

Methods

pure :: a -> Ap f a Source #

(<*>) :: Ap f (a -> b) -> Ap f a -> Ap f b Source #

liftA2 :: (a -> b -> c) -> Ap f a -> Ap f b -> Ap f c Source #

(*>) :: Ap f a -> Ap f b -> Ap f b Source #

(<*) :: Ap f a -> Ap f b -> Ap f a Source #

Comonad f => Comonad ( Ap f) Source #
Instance details

Defined in Control.Applicative.Free

Apply ( Ap f) Source #
Instance details

Defined in Control.Applicative.Free

Methods

(<.>) :: Ap f (a -> b) -> Ap f a -> Ap f b Source #

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

(<.) :: Ap f a -> Ap f b -> Ap f a Source #

liftF2 :: (a -> b -> c) -> Ap f a -> Ap f b -> Ap f c Source #

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 .

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

Tear down a free Applicative using iteration.

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

Examples