Synopsis

newtype FT f m a Source

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

Constructors

 FT FieldsrunFT :: forall r. (a -> m r) -> (f (m r) -> m r) -> m r

Instances

type F f = FT f IdentitySource

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

free :: Functor f => (forall r. (a -> r) -> (f r -> r) -> r) -> F f aSource

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

# Operations

toFT :: (Monad m, Functor f) => FreeT f m a -> FT f m aSource

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

fromFT :: (Monad m, Functor f) => FT f m a -> FreeT f m aSource

Convert to a `FreeT` free monad representation.

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

Tear down a free monad transformer using iteration.

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

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 :: (Monad m, Functor g) => (forall a. f a -> g a) -> FT f m b -> FT g m bSource

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

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

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:

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

fromF :: (Functor f, MonadFree f m) => F f a -> m aSource

Convert to another free monad representation.

toF :: Functor f => Free f a -> F f aSource

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

retract :: (Functor f, Monad f) => F f a -> f aSource

`retract` is the left inverse of `liftF`

``` `retract` . `liftF` = `id`
```

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

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

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

Like `iter` for monadic values.

class Monad m => MonadFree f m | m -> f whereSource

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

Methods

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

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