HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulefree-5.2Haskell2010

Control.Monad.Trans.Free.Church

Church-encoded free monad transformer.

Contents
  1. The free monad transformer
  2. The free monad
  3. Operations
  4. Operations of free monad
  5. Free Monads With Class
Synopsis
  • Packagefree-5.2
  • Exports22
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceChurch.hs

The free monad transformer

newtypenewtype FT (f :: Type -> Type) (m :: Type -> Type) a

The "free monad transformer" for a functor f

Constructors

  • FT
    • runFT :: forall r. (a -> m r) -> (forall x. (x -> m r) -> f x -> m r) -> m r
Instances24MonadError, MonadReader, MonadState, MonadWriter, MonadFree, MonadTrans, …

The free monad

valuefree :: (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.

valuerunF :: 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

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

Tear down a free monad transformer using iteration.

valuetransFT :: (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

valuecutoff :: (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):

Property
cutoff 0     _        == return Nothing
Property
cutoff (n+1) . return == return . Just
Property
cutoff (n+1) . lift   ==   lift . liftM Just
Property
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

valueimprove
  1. :: Functor f
  2. => forall (m :: Type -> Type). MonadFree f m => m a
  3. -> 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

valuetoF :: Free f a -> F f a

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

Free Monads With Class

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

Methods

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

    Add a layer.

    wrap (fmap f x) ≡ wrap (fmap return x) >>= f
    
Instances18MonadFree, …