free package:free

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/
Pushes a layer into a free monad value.
Wrap a Church-encoding of a "free monad" as the free monad for a functor.
Left distributive Alternative functors for free, based on a design by Stijn van Drongelen.
Applicative functors for free
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
The free monad transformer
The "free monad" for a functor f.
The "free monad" for an applicative f.
The base functor for a free monad.
The "free monad transformer" for a functor f
The "free monad transformer" for an applicative f
Cofree comonads
The Cofree Comonad of a functor f. Formally A Comonad v is a cofree Comonad for f if every comonad homomorphism from another comonad w to v is equivalent to a natural transformation from w to f. A cofree functor is right adjoint to a forgetful functor. Cofree is a functor from the category of functors to the category of comonads that is right adjoint to the forgetful functor from the category of comonads to the category of functors that forgets how to extract and duplicate, leaving you with only a Functor. In practice, cofree comonads are quite useful for annotating syntax trees, or talking about streams. A number of common comonads arise directly as cofree comonads. For instance,
  • Cofree Maybe forms the comonad for a non-empty list.
  • Cofree (Const b) is a product.
  • Cofree Identity forms an infinite stream.
  • Cofree ((->) b)' describes a Moore machine with states labeled with values of type a, and transitions on edges of type b.
Furthermore, if the functor f forms a monoid (for example, by being an instance of Alternative), the resulting Comonad is also a Monad. See Monadic Augment and Generalised Shortcut Fusion by Neil Ghani et al., Section 4.3 for more details. In particular, if f a ≡ [a], the resulting data structure is a Rose tree. For a practical application, check Higher Dimensional Trees, Algebraically by Neil Ghani et al.
Allows you to peel a layer off a cofree comonad.
The cofree comonad transformer
The cofree Comonad of a functor f.
This is the base functor of the cofree comonad transformer.