a1      !"#$%&'()*+,-./0fundeps, MPTCs experimentalEdward Kmett <ekmett@gmail.com>None1Allows you to peel a layer off a cofree comonad. Remove a layer. 12341234MPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com>None)This is a cofree comonad of some functor f, with a comonad w$ threaded through it at each level. <This is the base functor of the cofree comonad transformer. %Extract the head of the base functor &Extract the tails of the base functor !56789:;<=>?@ABCDEFGHIJKLMN  56789:;<=>?@ABCDEFGHIJKLMNMPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com>None The   O of a functor f. Formally A O v is a cofree O for f if every comonad homomorphism  another comonad w to v+ is equivalent to a natural transformation  from w to f. A cofree2 functor is right adjoint to a forgetful functor. NCofree is a functor from the category of functors to the category of comonads N that is right adjoint to the forgetful functor from the category of comonads 1 to the category of functors that forgets how to P and  Q, leaving you with only a R. KIn 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,    S+ forms the a comonad for a non-empty list.    (T b) is a product.    Identity forms an infinite stream.    ((->) b)'j describes a Moore machine with states labeled with values of type a, and transitions on edges of type b. :Use coiteration to generate a cofree comonad from a seed.   f =   (U    f) %Unfold a cofree comonad from a seed. V .   = UDThis is a lens that can be used to read or write from the target of P.  foo ^.  == P fooFor more on lenses see the lens package on hackage 'extracted :: Simple Lens (Cofree g a) aCThis is a lens that can be used to read or write to the tails of a   O.  foo ^.  ==  fooFor more on lenses see the lens package on hackage 6unwrapped :: Simple Lens (Cofree g a) (g (Cofree g a)) Construct a Lens into a   f/ given a list of lenses into the base functor. For more on lenses see the lens package on hackage. !telescoped :: Functor g => [Rep g] -> Simple Lens (Cofree g a) aWThis is not a true O/ transformer, but this instance is convenient. " XYZ[\]^_`abcdefghijkWlmnop    !  XYZ[\]^_`abcdefghijkWlmnopnon-portable (fundeps, MPTCs) experimentalEdward Kmett <ekmett@gmail.com> Safe-InferredMonads provide substitution (q) and renormalization (  ):  m r f =    . q f mA free sp is one that does no work during the normalization step beyond simply grafting the two monadic values together. [] is not a free s (in this sense) because    [[a]] smashes the lists flat. On the other hand, consider:   - data Tree a = Bin (Tree a) (Tree a) | Tip a    instance s Tree where  t = Tip  Tip a r f = f a  Bin l r r f = Bin (l r f) (r r f) This s is the free s of Pair:    data Pair a = Pair a a !And we could make an instance of  for it directly:    instance  Pair Tree where   (Pair l r) = Bin l r #Or we could choose to program with   Pair instead of Tree , and thereby avoid having to define our own s instance. Moreover, the kan-extensions package provides  instances that can  improve the  asymptotic5 complexity of code that constructors free monads by ' effectively reassociating the use of (r). See  * for a more formal definition of the free s  for a R.  Add a layer. uvwxyz{|}~ uvwxyz{|}~MPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com>NoneThe "free monad transformer" for a functor f. #The base functor for a free monad. LFreeT is a functor from the category of functors to the category of monads. This provides the mapping. Lift a monad homomorphism from m to n into a monad homomorphism from  f m to  f n  :: (s m, R f) => (m ~> n) ->  f m ~>  f n#Lift a natural transformation from f to g into a monad homomorphism from  f m to  g n )  %MPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com>NoneThe  s for a R f. Formally A s n is a free s 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 U9 from the category of monads to the category of functors  that just forgets the s, leaving only the R. i.e.  U (M,t,  ) = Mthen  is the left adjoint to U. Being  being left adjoint to U, means that there is an isomorphism between  f -> m in the category of monads and f -> U m in the category of functors. (Morphisms in the category of monads are s5 homomorphisms (natural transformations that respect t and   ). *Morphisms in the category of functors are R* homomorphisms (natural transformations). 6Given this isomorphism, every monad homomorphism from  f to m0 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  f a as many layers of f wrapped around values of type a, where  (r)0 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 k is fairly naive about the encoding. For more efficient free monad implementations that require additional  extensions and thus aren'-t included here, you may want to look at the kan-extensions package. 0A number of common monads arise as free monads,  Given  data Empty a,  Empty is isomorphic to the  monad.   Sq can be used to model a partiality monad where each layer represents running the computation for a while longer.  A version of  that can be used with just a R for f.   is the left inverse of  and      .  =     .  =  ! Tear down a  s using iteration. "#Lift a natural transformation from f to g$ into a natural transformation from FreeT f to FreeT g. EThis is not a true monad transformer. It is only a monad transformer "up to  ". 4This violates the MonadPlus laws, handle with care. 6This violates the Alternative laws, handle with care. # !"  !"  !"! !""non-portable (rank-2 polymorphism) provisionalEdward Kmett <ekmett@gmail.com>None#,The Church-encoded free monad for a functor f. It is asymptotically more efficient to use (r) for # than it is to (r) with .  6http://comonad.com/reader/2011/free-monads-for-less-2/ & A version of  that can be used with just a R for f. '' is the left inverse of  and &   ' .  =   ' . & =  (.Convert to another free monad representation. ),Generate a Church-encoded free monad from a  monad. *iImprove the asymptotic performance of code that builds a free monad with only binds and returns by using # behind the scenes. This is based on the "Free Monads for Less"% series of articles by Edward Kmett:  4http://comonad.com/reader/2011/free-monads-for-less/   6http://comonad.com/reader/2011/free-monads-for-less-2/ and "7Asymptotic Improvement of Computations over Free Monads" by Janis Voightlnder:  (http://www.iai.uni-bonn.de/~jv/mpc08.pdf #$%&'()*#$%&'()*#$%*()&'#$%&'()*GADTs, Rank2Types provisionalEdward Kmett <ekmett@gmail.com>None+ The free  for a R f. .$Given a natural transformation from f to g>, this gives a canonical monoidal natural transformation from + f to g. / A version of lift that can be used with just a R for f. 0$Given a natural transformation from f to g3 this gives a monoidal natural transformation from Ap f to Ap g. +,-./0+,-./0+-,./0 +-,./0 !!"# $%&'  $%()*++,%(-./00$123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTRSURSVWXYWZ[W\]W^_`abcdefghijklmnopqrstuvwxyz{|WX}WX~WXWXWX_W\free-3.2Control.Comonad.Cofree.ClassControl.Comonad.Trans.CofreeControl.Comonad.CofreeControl.Monad.Free.ClassControl.Monad.Trans.FreeControl.Monad.FreeControl.Monad.Free.ChurchControl.Applicative.Free Control.Arrow&&& Control.MonadjoinFree Data.FunctorIdentity ComonadCofreeunwrapCofreeT runCofreeTCofreeF:<headFtailFCofreecoiterunfoldsection extracted unwrapped telescoped MonadFreewrapFreeTrunFreeTFreeFPureliftF hoistFreeT transFreeTretractiter hoistFreeFrunFfromFtoFimproveAprunApliftAphoistAp$fComonadCofreefTracedT$fComonadCofreefStoreT$fComonadCofreefEnvT$fComonadCofreefIdentityT cofreeTTyCon cofreeFTyCon cofreeFConstr cofreeTConstrcofreeFDataTypecofreeTDataType $fDataCofreeT $fDataCofreeF$fTypeable1CofreeT$fTypeable2CofreeF $fOrdCofreeT $fEqCofreeT $fReadCofreeT $fShowCofreeT$fComonadCofreefCofreeT$fComonadTransCofreeT$fTraversableCofreeT$fFoldableCofreeT$fComonadCofreeT$fFunctorCofreeT$fBitraversableCofreeF$fBifoldableCofreeF$fBifunctorCofreeF$fTraversableCofreeF$fFoldableCofreeF$fFunctorCofreeFcomonad-3.0.0.2Control.ComonadComonadextract duplicatebaseGHC.BaseFunctor Data.MaybeMaybeControl.ApplicativeConstControl.Categoryidcomonad-transformers-3.0Control.Comonad.Trans.Classlower$fComonadTransCofree cofreeTyCon cofreeConstrcofreeDataType$fComonadTracedmCofree$fComonadStoresCofree$fComonadEnveCofree $fDataCofree$fTypeableCofree$fTypeable1Cofree$fTraversable1Cofree$fTraversableCofree$fFoldable1Cofree$fFoldableCofree $fOrdCofree $fEqCofree $fReadCofree $fShowCofree$fApplicativeCofree$fComonadApplyCofree $fApplyCofree$fComonadCofree$fExtendCofree$fFunctorCofree$fDistributiveCofree$fComonadCofreefCofreefmap>>=Monadreturn$fMonadFreefErrorT$fMonadFreefListT$fMonadFreefIdentityT$fMonadFreefMaybeT$fMonadFreefRWST$fMonadFreefRWST0$fMonadFreefWriterT$fMonadFreefWriterT0$fMonadFreefStateT$fMonadFreefStateT0$fMonadFreefReaderT transFreeF freeTTyCon freeFTyCon pureConstr freeConstr freeTConstr freeFDataType freeTDataType $fDataFreeT $fDataFreeF$fTypeable1FreeT$fTypeable2FreeF$fTraversableFreeT$fFoldableFreeT$fMonadFreefFreeT$fMonadPlusFreeT$fAlternativeFreeT$fMonadIOFreeT$fMonadTransFreeT $fMonadFreeT$fApplicativeFreeT$fFunctorFreeT $fReadFreeT $fShowFreeT $fOrdFreeT $fEqFreeT$fBitraversableFreeF$fBifoldableFreeF$fBifunctorFreeF$fTraversableFreeF$fFoldableFreeF$fFunctorFreeFtransformers-0.3.0.0Control.Monad.Trans.Classlift$fMonadTransFree$fMonadPlusFree$fAlternativeFree freeTyCon freeDataType $fDataFree$fTypeable1Free$fMonadFreefFree$fMonadContFree$fMonadErroreFree$fMonadStatesFree$fMonadReadereFree$fMonadWritereFree$fTraversable1Free$fTraversableFree$fFoldable1Free$fFoldableFree $fMonadFree $fBindFree$fApplicativeFree $fApplyFree $fFunctorFree $fReadFree $fShowFree $fOrdFree$fEqFree $fMonadContF$fMonadWriterwF$fMonadReadereF$fMonadStatesF $fMonadFreefF $fMonadTransF $fMonadPlusF$fMonadF$fBindF$fAlternativeF$fApplicativeF$fApplyF $fFunctorF ApplicativeapTyCon $fTypeable1Ap$fApplicativeAp $fApplyAp $fFunctorAp