jc     fundeps, MPTCs experimentalEdward Kmett <ekmett@gmail.com> Safe-Infered1Allows you to peel a layer off a cofree comonad. Remove a layer.   MPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com> Safe-Infered)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 !"#$%&'()*+,-./01234  !"#$%&'()*+,-./01234MPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com> Safe-Infered The   5 of a functor f. Formally A 5 v is a cofree 5 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 6 and  7, leaving you with only a 8. 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,    9+ forms the a comonad for a non-empty list.    (: 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 =   (;  f) %Unfold a cofree comonad from a seed. < .   = ;DThis is a lens that can be used to read or write from the target of 6.  foo ^.  == 6 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   5.  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) a=This is not a true 5/ transformer, but this instance is convenient.  >?@ABCDEFGHIJKLMN=OPQRS      >?@ABCDEFGHIJKLMN=OPQRSnon-portable (fundeps, MPTCs) experimentalEdward Kmett <ekmett@gmail.com> Safe-InferedMonads provide substitution (T) and renormalization ( ):  m U f =   . T f mA free Vp is one that does no work during the normalization step beyond simply grafting the two monadic values together. [] is not a free V (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 V Tree where  W = Tip  Tip a U f = f a  Bin l r U f = Bin (l U f) (r U f) This V is the free V 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 V 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 (U). See  * for a more formal definition of the free V  for a 8.  Add a layer. XYZ[\]^_`ab XYZ[\]^_`abMPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com> Safe-InferedThe "free monad transformer" for a functor f. LFreeT is a functor from the category of functors to the category of monads. This provides the mapping. cdefghijklmnopqrstuvwxyzcdefghijklmnopqrstuvwxyzMPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com> Safe-InferedThe  V for a 8 f. Formally A V n is a free V 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 V, leaving only the 8. i.e.  U (M,W, ) = 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 V5 homomorphisms (natural transformations that respect W and  ). *Morphisms in the category of functors are 8* 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  (U)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.   9q 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 8 for f.  is the left inverse of { and     . { = |   .  = |  Tear down a  V using iteration. }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. }~}~ !  "!#$%&'()*+,-./0123456789:;<=>?=>@=>ABCDBEFBGHBIJKLMNOPQRSTUVWXYZ[\]^_`abcdBCeBCfBCgBChijklmnopqrstuvwxyz{|}~BCJfree-3.1Control.Comonad.Cofree.ClassControl.Comonad.Trans.CofreeControl.Comonad.CofreeControl.Monad.Free.ClassControl.Monad.Trans.FreeControl.Monad.Free Control.Arrow&&& Control.MonadjoinFree Data.FunctorIdentity ComonadCofreeunwrapCofreeT runCofreeTCofreeF:<headFtailFCofreecoiterunfoldsection extracted unwrapped telescoped MonadFreewrapFreeTrunFreeTliftFPureretractiter$fComonadCofreefTracedT$fComonadCofreefStoreT$fComonadCofreefEnvT$fComonadCofreefIdentityT $fDataCofreeT $fDataCofreeF$fTypeable1CofreeT$fTypeable2CofreeF $fOrdCofreeT $fEqCofreeT $fReadCofreeT $fShowCofreeT$fComonadCofreefCofreeT$fComonadTransCofreeT$fTraversableCofreeT$fFoldableCofreeT$fComonadCofreeT$fFunctorCofreeT$fBitraversableCofreeF$fBifoldableCofreeF$fBifunctorCofreeF$fTraversableCofreeF$fFoldableCofreeF$fFunctorCofreeFcomonad-3.0.0.1Control.ComonadComonadextract duplicatebaseGHC.BaseFunctor Data.MaybeMaybeControl.ApplicativeConstControl.Categoryidcomonad-transformers-3.0Control.Comonad.Trans.Classlower$fComonadTransCofree$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 $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 $fDataFree$fTypeable1Free$fMonadFreefFree$fMonadContFree$fMonadErroreFree$fMonadStatesFree$fMonadReadereFree$fMonadWritereFree$fTraversable1Free$fTraversableFree$fFoldable1Free$fFoldableFree $fMonadFree $fBindFree$fApplicativeFree $fApplyFree $fFunctorFree $fReadFree $fShowFree $fOrdFree$fEqFree