HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulefree-5.2Haskell2010

Control.Monad.Trans.Free

The free monad transformer

  • 3 types
  • 1 class
  • 17 values
  • Packagefree-5.2
  • Exports21
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceFree.hs

The base functor

1 declaration
datadata FreeF (f :: Type -> Type) a b
#

The base functor for a free monad.

Constructors

Instances23Generic1, Bifoldable, Bifunctor, Bitraversable, Eq2, Ord2, …

The free monad transformer

1 declaration
newtypenewtype FreeT (f :: Type -> Type) (m :: Type -> Type) a
#

The "free monad transformer" for a functor f

Constructors

Instances29MonadError, MonadReader, MonadState, MonadWriter, MonadBase, MonadFree, …

The free monad

3 declarations
valuefree :: FreeF f a (Free f a) -> Free f a
#

Pushes a layer into a free monad value.

valuerunFree :: Free f a -> FreeF f a (Free f a)
#

Evaluates the first layer out of a free monad value.

Operations

12 declarations
valueliftF :: (Functor f, MonadFree f m) => f a -> m a
#

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

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

Tear down a free monad transformer using iteration.

valuefoldFreeT
  1. :: (MonadTrans t, Monad (t m), Monad m)
  2. => forall (n :: Type -> Type) x. Monad n => f x -> t n x
  3. -> FreeT f m a
  4. -> t m a
#

The very definition of a free monad transformer is that given a natural transformation you get a monad transformer homomorphism.

valuecutoff
  1. :: (Functor f, Monad m)
  2. => Integer
  3. -> FreeT f m a
  4. -> FreeT 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

3 declarations

Free Monads With Class

1 declaration
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, …