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

Monads for free

  • 1 type
  • 1 class
  • 13 values
  • Packagefree-5.2
  • Exports15
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceFree.hs
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, …
datadata Free (f :: Type -> Type) a
#

The Free Monad for a Functor f.

Formally

A Monad n is a free Monad for f if every monad homomorphism from n to another monad m is equivalent to a natural transformation from f to m.

Why Free?

Every "free" functor is left adjoint to some "forgetful" functor.

If we define a forgetful functor U from the category of monads to the category of functors that just forgets the Monad, leaving only the Functor. i.e.

U (M,return,join) = M

then Free is the left adjoint to U.

Free being left adjoint to U means that there is an isomorphism between

Free f -> m in the category of monads and f -> U m in the category of functors.

Morphisms in the category of monads are Monad homomorphisms (natural transformations that respect return and join).

Morphisms in the category of functors are Functor homomorphisms (natural transformations).

Given this isomorphism, every monad homomorphism from Free f to m is equivalent to a natural transformation from f to m

Showing that this isomorphism holds is left as an exercise.

In practice, you can just view a Free f a as many layers of f wrapped around values of type a, where (>>=) performs substitution and grafts new layers of f in for each of the free variables.

This can be very useful for modeling domain specific languages, trees, or other constructs.

This instance of MonadFree is fairly naive about the encoding. For more efficient free monad implementation see Control.Monad.Free.Church, in particular note the improve combinator. You may also want to take a look at the kan-extensions package (http://hackage.haskell.org/package/kan-extensions).

A number of common monads arise as free monads,

  • Given data Empty a, Free Empty is isomorphic to the Data.Functor.Identity monad.

  • Free Maybe can be used to model a partiality monad where each layer represents running the computation for a while longer.

Constructors

Instances35MonadTrans, Generic1, MonadError, MonadReader, MonadState, MonadWriter, …
valueliftF :: (Functor f, MonadFree f m) => f a -> m a
#

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

valuehoistFree :: Functor g => (forall a. f a -> g a) -> Free f b -> Free g b
#

Lift a natural transformation from f to g into a natural transformation from Free f to Free g.

valuefoldFree :: Monad m => (forall x. f x -> m x) -> Free f a -> m a
#

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

valuecutoff :: Functor f => Integer -> Free f a -> Free f (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.

value_Pure
  1. :: (Choice p, Applicative m)
  2. => p a (m a)
  3. -> p (Free f a) (m (Free f a))
#

This is Prism' (Free f a) a in disguise

Example1 expression
preview _Pure (Pure 3)Just 3
Example1 expression
review _Pure 3 :: Free Maybe IntPure 3
value_Free
  1. :: (Choice p, Applicative m)
  2. => p (f (Free f a)) (m (g (Free g a)))
  3. -> p (Free f a) (m (Free g a))
#

This is Prism (Free f a) (Free g a) (f (Free f a)) (g (Free g a)) in disguise

Example1 expression
preview _Free (review _Free (Just (Pure 3)))Just (Just (Pure 3))
Example1 expression
review _Free (Just (Pure 3))Free (Just (Pure 3))