úÎkącņ      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 $%&'()*+,-./01234567  $%&'()*+,-./01234567MPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com> Safe-Infered The   8 of a functor f. Formally A 8 v is a cofree 8 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 9 and  :, leaving you with only a ;. 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,    <+ 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 9.  foo ^.  == 9 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   8.  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 8/ transformer, but this instance is convenient.  ABCDEFGHIJKLMNOPQ@RSTUV      ABCDEFGHIJKLMNOPQ@RSTUVnon-portable (fundeps, MPTCs) experimentalEdward Kmett <ekmett@gmail.com> Safe-InferedMonads provide substitution (W) and renormalization ( ):  m X f =   . W f mA free Yp is one that does no work during the normalization step beyond simply grafting the two monadic values together. [] is not a free Y (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 Y Tree where  Z = Tip  Tip a X f = f a  Bin l r X f = Bin (l X f) (r X f) This Y is the free Y 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 Y 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 (X). See  * for a more formal definition of the free Y  for a ;.  Add a layer. [\]^_`abcde [\]^_`abcdeMPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com> Safe-InferedThe "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. fghijklmnopqrstuvwxyz{|}  fghijklmnopqrstuvwxyz{|}MPTCs, fundeps provisionalEdward Kmett <ekmett@gmail.com> Safe-InferedThe  Y for a ; f. Formally A Y n is a free Y 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 Y, leaving only the ;. i.e.  U (M,Z, ) = 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 Y5 homomorphisms (natural transformations that respect Z and  ). *Morphisms in the category of functors are ;* 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  (X)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.   <q 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 ; for f.  is the left inverse of ~ and     . ~ =    .  =   Tear down a  Y 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:;<=>?@>?A>?BCDECFGCHICJKLMNOPQRSTUVWXYZ[\]^_`abcdeCDfCDgCDhCDijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽCDK‘’“”•–—˜™š›œžŸ ĄĒĢĪĨĶ§Ļ free-3.1.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 MonadFreewrapFreeTrunFreeTFreeFPureliftFretractiter$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.2Control.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