f package:free

The Church-encoded free monad for a functor f. It is asymptotically more efficient to use (>>=) for F than it is to (>>=) with Free. https://ekmett.github.io/reader/2011/free-monads-for-less-2/
The "free monad" for a functor f.
Monads for free Free monads are useful for many tree-like structures and domain specific languages. If f is a Functor then the free Monad on f is the type of trees whose nodes are labeled with the constructors of f. The word "free" is used in the sense of "unrestricted" rather than "zero-cost": Free f makes no constraining assumptions beyond those given by f and the definition of Monad. As used here it is a standard term from the mathematical theory of adjoint functors. Cofree comonads are dual to free monads. They provide convenient ways to talk about branching streams and rose-trees, and can be used to annotate syntax trees. The cofree comonad can be seen as a stream parameterized by a Functor that controls its branching factor. More information on free monads, including examples, can be found in the following blog posts: https://ekmett.github.io/reader/2008/monads-for-free/ https://ekmett.github.io/reader/2011/free-monads-for-less/
The very definition of a free monad is that given a natural transformation you get a monad homomorphism.
Given an applicative homomorphism, you get a monad homomorphism.
The very definition of a free monad is that given a natural transformation you get a monad homomorphism.
Convert to another free monad representation.
The very definition of a free monad transformer is that given a natural transformation you get a monad transformer homomorphism.
Pushes a layer into a free monad value.
Wrap a Church-encoding of a "free monad" as the free monad for a functor.
Convert to another free monad representation.
Convert to a FreeT free monad representation.
Tear down a Free Monad using iteration.
Like fold with monadic result.
Left distributive Alternative functors for free, based on a design by Stijn van Drongelen.
Final encoding of free Alternative functors.
Applicative functors for free
A faster free applicative. Based on Dave Menendez's work.
Final encoding of free Applicative functors.
Applicative functor transformers for free
Monads for free
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 Identity monad.
  • Free Maybe can be used to model a partiality monad where each layer represents running the computation for a while longer.
A free monad given an applicative