free-5.2: Monads for free

Copyright | (C) 2008-2014 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | provisional
Portability | non-portable (rank-2 polymorphism, MTPCs)
Safe Haskell | Safe
Language | Haskell2010

Control.Monad.Trans.Free.Church

Contents

- The free monad transformer
- The free monad
- Operations
- Operations of free monad
- Free Monads With Class

Description

Church-encoded free monad transformer.

Synopsis

- newtype FT (f :: Type -> Type) (m :: Type -> Type) a = FT {
  - runFT :: forall r. (a -> m r) -> (forall x. (x -> m r) -> f x -> m r) -> m r
  }
- type F (f :: Type -> Type) = FT f Identity
- free :: (forall r. (a -> r) -> (f r -> r) -> r) -> F f a
- runF :: Functor f => F f a -> forall r. (a -> r) -> (f r -> r) -> r
- improveT :: forall (f :: Type -> Type) (m :: Type -> Type) a. (Functor f, Monad m) => (forall (t :: (Type -> Type) -> Type -> Type). MonadFree f (t m) => t m a) -> FreeT f m a
- toFT :: forall (m :: Type -> Type) (f :: Type -> Type) a. Monad m => FreeT f m a -> FT f m a
- fromFT :: forall (m :: Type -> Type) (f :: Type -> Type) a. (Monad m, Functor f) => FT f m a -> FreeT f m a
- iterT :: (Functor f, Monad m) => (f (m a) -> m a) -> FT f m a -> m a
- iterTM :: forall f (m :: Type -> Type) t a. (Functor f, Monad m, MonadTrans t, Monad (t m)) => (f (t m a) -> t m a) -> FT f m a -> t m a
- hoistFT :: forall m n (f :: Type -> Type) b. (Monad m, Monad n) => (forall a. m a -> n a) -> FT f m b -> FT f n b
- transFT :: forall f g (m :: Type -> Type) b. (forall a. f a -> g a) -> FT f m b -> FT g m b
- joinFT :: forall m (f :: Type -> Type) a. (Monad m, Traversable f) => FT f m a -> m (F f a)
- cutoff :: forall (f :: Type -> Type) (m :: Type -> Type) a. (Functor f, Monad m) => Integer -> FT f m a -> FT f m (Maybe a)
- improve :: forall (f :: Type -> Type) a. Functor f => (forall (m :: Type -> Type). MonadFree f m => m a) -> Free f a
- fromF :: forall (f :: Type -> Type) m a. (Functor f, MonadFree f m) => F f a -> m a
- toF :: forall (f :: Type -> Type) a. Free f a -> F f a
- retract :: Monad f => F f a -> f a
- retractT :: forall t (m :: Type -> Type) a. (MonadTrans t, Monad (t m), Monad m) => FT (t m) m a -> t m a
- iter :: Functor f => (f a -> a) -> F f a -> a
- iterM :: (Functor f, Monad m) => (f (m a) -> m a) -> F f a -> m a
- class Monad m => MonadFree (f :: Type -> Type) (m :: Type -> Type) | m -> f where
  - wrap :: f (m a) -> m a
- liftF :: (Functor f, MonadFree f m) => f a -> m a

# The free monad transformer

newtype FT (f :: Type -> Type) (m :: Type -> Type) a #

The "free monad transformer" for a functor `f`

Constructors

FT |

Fields

- runFT :: forall r. (a -> m r) -> (forall x. (x -> m r) -> f x -> m r) -> m r

#### Instances

Instances details

MonadFree f (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods wrap :: f (FT f m a) -> FT f m a #
(Functor f, MonadError e m) => MonadError e (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods throwError :: e -> FT f m a # catchError :: FT f m a -> (e -> FT f m a) -> FT f m a #
MonadReader r m => MonadReader r (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods ask :: FT f m r # local :: (r -> r) -> FT f m a -> FT f m a # reader :: (r -> a) -> FT f m a #
MonadState s m => MonadState s (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods get :: FT f m s # put :: s -> FT f m () # state :: (s -> (a, s)) -> FT f m a #
(Functor f, MonadWriter w m) => MonadWriter w (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods writer :: (a, w) -> FT f m a # tell :: w -> FT f m () # listen :: FT f m a -> FT f m (a, w) # pass :: FT f m (a, w -> w) -> FT f m a #
MonadTrans (FT f) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods lift :: Monad m => m a -> FT f m a #
MonadFail m => MonadFail (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods fail :: String -> FT f m a #
MonadIO m => MonadIO (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods liftIO :: IO a -> FT f m a #
(Foldable f, Foldable m, Monad m) => Foldable (FT f m) # |

Instance details

Defined in Control.Monad.Trans.Free.Church

Methods

fold :: Monoid m0 => FT f m m0 -> m0 #

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

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

foldr :: (a -> b -> b) -> b -> FT f m a -> b #

foldr' :: (a -> b -> b) -> b -> FT f m a -> b #

foldl :: (b -> a -> b) -> b -> FT f m a -> b #

foldl' :: (b -> a -> b) -> b -> FT f m a -> b #

foldr1 :: (a -> a -> a) -> FT f m a -> a #

foldl1 :: (a -> a -> a) -> FT f m a -> a #

toList :: FT f m a -> [a] #

null :: FT f m a -> Bool #

length :: FT f m a -> Int #

elem :: Eq a => a -> FT f m a -> Bool #

maximum :: Ord a => FT f m a -> a #

minimum :: Ord a => FT f m a -> a #

sum :: Num a => FT f m a -> a #

product :: Num a => FT f m a -> a #

(Functor f, Monad m, Eq1 f, Eq1 m) => Eq1 (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods liftEq :: (a -> b -> Bool) -> FT f m a -> FT f m b -> Bool #
(Functor f, Monad m, Ord1 f, Ord1 m) => Ord1 (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods liftCompare :: (a -> b -> Ordering) -> FT f m a -> FT f m b -> Ordering #
(Monad m, Traversable m, Traversable f) => Traversable (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods traverse :: Applicative f0 => (a -> f0 b) -> FT f m a -> f0 (FT f m b) # sequenceA :: Applicative f0 => FT f m (f0 a) -> f0 (FT f m a) # mapM :: Monad m0 => (a -> m0 b) -> FT f m a -> m0 (FT f m b) # sequence :: Monad m0 => FT f m (m0 a) -> m0 (FT f m a) #
Alternative m => Alternative (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods empty :: FT f m a # (<|>) :: FT f m a -> FT f m a -> FT f m a # some :: FT f m a -> FT f m [a] # many :: FT f m a -> FT f m [a] #
Applicative (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods pure :: a -> FT f m a # (<*>) :: FT f m (a -> b) -> FT f m a -> FT f m b # liftA2 :: (a -> b -> c) -> FT f m a -> FT f m b -> FT f m c # (*>) :: FT f m a -> FT f m b -> FT f m b # (<*) :: FT f m a -> FT f m b -> FT f m a #
Functor (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods fmap :: (a -> b) -> FT f m a -> FT f m b # (<$) :: a -> FT f m b -> FT f m a #
Monad (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods (>>=) :: FT f m a -> (a -> FT f m b) -> FT f m b # (>>) :: FT f m a -> FT f m b -> FT f m b # return :: a -> FT f m a #
MonadPlus m => MonadPlus (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods mzero :: FT f m a # mplus :: FT f m a -> FT f m a -> FT f m a #
(Functor f, MonadCatch m) => MonadCatch (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods catch :: (HasCallStack, Exception e) => FT f m a -> (e -> FT f m a) -> FT f m a #
MonadThrow m => MonadThrow (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods throwM :: (HasCallStack, Exception e) => e -> FT f m a #
MonadCont m => MonadCont (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods callCC :: ((a -> FT f m b) -> FT f m a) -> FT f m a #
Apply (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods (<.>) :: FT f m (a -> b) -> FT f m a -> FT f m b # (.>) :: FT f m a -> FT f m b -> FT f m b # (<.) :: FT f m a -> FT f m b -> FT f m a # liftF2 :: (a -> b -> c) -> FT f m a -> FT f m b -> FT f m c #
Bind (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods (>>-) :: FT f m a -> (a -> FT f m b) -> FT f m b # join :: FT f m (FT f m a) -> FT f m a #
(Functor f, Monad m, Eq1 f, Eq1 m, Eq a) => Eq (FT f m a) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods (==) :: FT f m a -> FT f m a -> Bool # (/=) :: FT f m a -> FT f m a -> Bool #
(Functor f, Monad m, Ord1 f, Ord1 m, Ord a) => Ord (FT f m a) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods compare :: FT f m a -> FT f m a -> Ordering # (<) :: FT f m a -> FT f m a -> Bool # (<=) :: FT f m a -> FT f m a -> Bool # (>) :: FT f m a -> FT f m a -> Bool # (>=) :: FT f m a -> FT f m a -> Bool # max :: FT f m a -> FT f m a -> FT f m a # min :: FT f m a -> FT f m a -> FT f m a #

# The free monad

type F (f :: Type -> Type) = FT f Identity #

The "free monad" for a functor `f`.

free :: (forall r. (a -> r) -> (f r -> r) -> r) -> F f a #

Wrap a Church-encoding of a "free monad" as the free monad for a functor.

runF :: Functor f => F f a -> forall r. (a -> r) -> (f r -> r) -> r #

Unwrap the `Free` monad to obtain it's Church-encoded representation.

# Operations

improveT :: forall (f :: Type -> Type) (m :: Type -> Type) a. (Functor f, Monad m) => (forall (t :: (Type -> Type) -> Type -> Type). MonadFree f (t m) => t m a) -> FreeT f m a #

Improve the asymptotic performance of code that builds a free monad transformer with only binds and returns by using `FT` behind the scenes.

Similar to `improve`.

toFT :: forall (m :: Type -> Type) (f :: Type -> Type) a. Monad m => FreeT f m a -> FT f m a #

Generate a Church-encoded free monad transformer from a `FreeT` monad transformer.

fromFT :: forall (m :: Type -> Type) (f :: Type -> Type) a. (Monad m, Functor f) => FT f m a -> FreeT f m a #

Convert to a `FreeT` free monad representation.

iterT :: (Functor f, Monad m) => (f (m a) -> m a) -> FT f m a -> m a #

Tear down a free monad transformer using iteration.

iterTM :: forall f (m :: Type -> Type) t a. (Functor f, Monad m, MonadTrans t, Monad (t m)) => (f (t m a) -> t m a) -> FT f m a -> t m a #

Tear down a free monad transformer using iteration over a transformer.

hoistFT :: forall m n (f :: Type -> Type) b. (Monad m, Monad n) => (forall a. m a -> n a) -> FT f m b -> FT f n b #

Lift a monad homomorphism from `m` to `n` into a monad homomorphism from ``FT` f m` to ``FT` f n`

    hoistFT :: (Monad m, Monad n, Functor f) => (m ~> n) -> FT f m ~> FT f n

transFT :: forall f g (m :: Type -> Type) b. (forall a. f a -> g a) -> FT f m b -> FT g m b #

Lift a natural transformation from `f` to `g` into a monad homomorphism from ``FT` f m` to ``FT` g n`

joinFT :: forall m (f :: Type -> Type) a. (Monad m, Traversable f) => FT f m a -> m (F f a) #

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

cutoff :: forall (f :: Type -> Type) (m :: Type -> Type) a. (Functor f, Monad m) => Integer -> FT f m a -> FT f m (Maybe a) #

Cuts off a tree of computations at a given depth. If the depth is 0 or less, no computation nor monadic effects will take place.

Some examples (n ≥ 0):

    cutoff 0     _        == return Nothing

    cutoff (n+1) . return == return . Just

    cutoff (n+1) . lift   ==   lift . liftM Just

    cutoff (n+1) . wrap   ==  wrap . fmap (cutoff n)

Calling 'retract . cutoff n' is always terminating, provided each of the steps in the iteration is terminating.

# Operations of free monad

improve :: forall (f :: Type -> Type) a. Functor f => (forall (m :: Type -> Type). MonadFree f m => m a) -> Free f a #

Improve the asymptotic performance of code that builds a free monad with only binds and returns by using `F` behind the scenes.

This is based on the "Free Monads for Less" series of articles by Edward Kmett:

https://ekmett.github.io/reader/2011/free-monads-for-less/ https://ekmett.github.io/reader/2011/free-monads-for-less-2/

and "Asymptotic Improvement of Computations over Free Monads" by Janis Voightländer:

http://www.iai.uni-bonn.de/~jv/mpc08.pdf

fromF :: forall (f :: Type -> Type) m a. (Functor f, MonadFree f m) => F f a -> m a #

Convert to another free monad representation.

toF :: forall (f :: Type -> Type) a. Free f a -> F f a #

Generate a Church-encoded free monad from a `Free` monad.

retract :: Monad f => F f a -> f a #

`retract` is the left inverse of `liftF`

    retract . liftF = id

retractT :: forall t (m :: Type -> Type) a. (MonadTrans t, Monad (t m), Monad m) => FT (t m) m a -> t m a #

Tear down a free monad transformer using iteration over a transformer.

iter :: Functor f => (f a -> a) -> F f a -> a #

Tear down an `F` `Monad` using iteration.

iterM :: (Functor f, Monad m) => (f (m a) -> m a) -> F f a -> m a #

Like `iter` for monadic values.

# Free Monads With Class

class Monad m => MonadFree (f :: Type -> Type) (m :: Type -> Type) | m -> f where #

Monads provide substitution (`fmap`) and renormalization (`join`):

    m >>= f = join (fmap f m)

A free `Monad` is one that does no work during the normalization step beyond simply grafting the two monadic values together.

`[]` is not a free `Monad` (in this sense) because ``join` [[a]]` smashes the lists flat.

On the other hand, consider:

    data Tree a = Bin (Tree a) (Tree a) | Tip a

    instance Monad Tree where
      return = Tip
      Tip a >>= f = f a
      Bin l r >>= f = Bin (l >>= f) (r >>= f)

This `Monad` is the free `Monad` of Pair:

    data Pair a = Pair a a

And we could make an instance of `MonadFree` for it directly:

    instance MonadFree Pair Tree where
       wrap (Pair l r) = Bin l r

Or we could choose to program with ``Free` Pair` instead of `Tree` and thereby avoid having to define our own `Monad` instance.

Moreover, Control.Monad.Free.Church provides a `MonadFree` instance that can improve the asymptotic complexity of code that constructs free monads by effectively reassociating the use of (`>>=`). You may also want to take a look at the `kan-extensions` package (http://hackage.haskell.org/package/kan-extensions).

See `Free` for a more formal definition of the free `Monad` for a `Functor`.

Minimal complete definition

Nothing

Methods

wrap :: f (m a) -> m a #

Add a layer.

    wrap (fmap f x) ≡ wrap (fmap return x) >>= f

default wrap :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type) a. (m ~ t n, MonadTrans t, MonadFree f n, Functor f) => f (m a) -> m a #

#### Instances

Instances details

Monad m => MonadFree Identity (IterT m) # |
Instance details Defined in Control.Monad.Trans.Iter Methods wrap :: Identity (IterT m a) -> IterT m a #
Functor f => MonadFree f (Free f) # |
Instance details Defined in Control.Monad.Free Methods wrap :: f (Free f a) -> Free f a #
Applicative f => MonadFree f (Free f) # |
Instance details Defined in Control.Monad.Free.Ap Methods wrap :: f (Free f a) -> Free f a #
Functor f => MonadFree f (F f) # |
Instance details Defined in Control.Monad.Free.Church Methods wrap :: f (F f a) -> F f a #
(Functor f, MonadFree f m) => MonadFree f (MaybeT m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (MaybeT m a) -> MaybeT m a #
(Functor f, Monad m) => MonadFree f (FreeT f m) # |
Instance details Defined in Control.Monad.Trans.Free Methods wrap :: f (FreeT f m a) -> FreeT f m a #
(Applicative f, Monad m) => MonadFree f (FreeT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Ap Methods wrap :: f (FreeT f m a) -> FreeT f m a #
MonadFree f (FT f m) # |
Instance details Defined in Control.Monad.Trans.Free.Church Methods wrap :: f (FT f m a) -> FT f m a #
(Functor f, MonadFree f m) => MonadFree f (ExceptT e m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (ExceptT e m a) -> ExceptT e m a #
(Functor f, MonadFree f m) => MonadFree f (IdentityT m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (IdentityT m a) -> IdentityT m a #
(Functor f, MonadFree f m) => MonadFree f (ReaderT e m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (ReaderT e m a) -> ReaderT e m a #
(Functor f, MonadFree f m) => MonadFree f (StateT s m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (StateT s m a) -> StateT s m a #
(Functor f, MonadFree f m) => MonadFree f (StateT s m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (StateT s m a) -> StateT s m a #
(Functor f, MonadFree f m, Monoid w) => MonadFree f (WriterT w m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (WriterT w m a) -> WriterT w m a #
(Functor f, MonadFree f m, Monoid w) => MonadFree f (WriterT w m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (WriterT w m a) -> WriterT w m a #
(Functor f, MonadFree f m) => MonadFree f (ContT r m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (ContT r m a) -> ContT r m a #
(Functor f, MonadFree f m, Monoid w) => MonadFree f (RWST r w s m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (RWST r w s m a) -> RWST r w s m a #
(Functor f, MonadFree f m, Monoid w) => MonadFree f (RWST r w s m) # |
Instance details Defined in Control.Monad.Free.Class Methods wrap :: f (RWST r w s m a) -> RWST r w s m a #

liftF :: (Functor f, MonadFree f m) => f a -> m a #

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