:N      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~                                  ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i jklmnopqrstuvwxyz{|}~      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM(C) 2012 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalGADTs, Rank2TypesSafe&'1QV$ A version of lift that can be used with any f.$Given a natural transformation from f to g>, this gives a canonical monoidal natural transformation from  f to g.$Given a natural transformation from f to g3 this gives a monoidal natural transformation from Alt f to Alt g.  3(C) 2012 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalGADTs, Rank2TypesSafeQV,; The free N for any f. A version of lift that can be used with f.$Given a natural transformation from f to g>, this gives a canonical monoidal natural transformation from  f to g.$Given a natural transformation from f to g3 this gives a monoidal natural transformation from Alt f to Alt g.(C) 2012-2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalGADTs, Rank2TypesSafe&'1QV9X The free O for a P f."$Given a natural transformation from f to g>, this gives a canonical monoidal natural transformation from  f to g."runAp t == retractApp . hoistApp t#8Perform a monoidal analysis over free applicative value.Example: =count :: Ap f a -> Int count = getSum . runAp_ (\_ -> Sum 1) $ A version of lift that can be used with just a P for f.%Tear down a free O using iteration.&$Given a natural transformation from f to g3 this gives a monoidal natural transformation from Ap f to Ap g.'JInterprets the free applicative functor over f using the semantics for Q and R) given by the Applicative instance for f.retractApp == runAp id  !"#$%&'  !"#$%&' !Safe&'1QVRU ,The faster free O./The free applicative is composed of a sequence of effects, and a pure function to apply that sequence to. The fast free applicative separates these from each other, so that the sequence may be built up independently, and so that SJ can run in constant time by having immediate access to the pure function.2>Interprets the sequence of effects using the semantics for Q and R' given by the Applicative instance for f.3$Given a natural transformation from f to g* this gives a natural transformation from ASeq f to ASeq g.4=Traverse a sequence with resepect to its interpretation type f.5 It may not be obvious, but this essentially acts like ++, traversing the first sequence and creating a new one by appending the second sequence. The difference is that this also has to modify the return functions and that the return type depends on the input types.See the source of 9 as an example usage.6$Given a natural transformation from f to g>, this gives a canonical monoidal natural transformation from , f to g."runAp t == retractApp . hoistApp t78Perform a monoidal analysis over free applicative value.Example: =count :: Ap f a -> Int count = getSum . runAp_ (\_ -> Sum 1) 8 A version of lift that can be used with just a P for f.9$Given a natural transformation from f to g3 this gives a monoidal natural transformation from Ap f to Ap g.:JInterprets the free applicative functor over f using the semantics for Q and R) given by the Applicative instance for f.retractApp == runAp id,-./0123456789:/012345,-.8:679,-./01(C) 2012-2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalGADTs, Rank2TypesSafeQV_> The free O for a P f.A$Given a natural transformation from f to g>, this gives a canonical monoidal natural transformation from > f to g."runAp t == retractApp . hoistApp tB8Perform a monoidal analysis over free applicative value.Example: =count :: Ap f a -> Int count = getSum . runAp_ (\_ -> Sum 1) C A version of lift that can be used with just a P for f.D$Given a natural transformation from f to g3 this gives a monoidal natural transformation from Ap f to Ap g.EJInterprets the free applicative functor over f using the semantics for Q and R) given by the Applicative instance for f.retractApp == runAp id>?@ABCDE>?@ABCDE>?@(C) 2012-2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalGADTs, Rank2TypesSafe&'1QV}I The free N for a P f.J The free O for a P f.K The free O transformer for a P f over O g.N The free O for a P f.Q A version of lift) that can be used with no constraint for f.RLift an action of the "outer" P g a to K f g a.SGiven natural transformations f ~> h and  g . h ~> h& this gives a natural transformation  ApF f g ~> h.TGiven natural transformations f ~> h and  g . h ~> h& this gives a natural transformation  ApT f g ~> h.U!Perform a monoidal analysis over K f g b value. Examples:  height :: (P g, T g) => K f g a -> U height = getSum . runApT_ (_ -> Sum 1) V  size :: (P g, T g) => K f g a -> U size = getSum . runApT_ (_ -> Sum 1) W V$Given a natural transformation from f to f'3 this gives a monoidal natural transformation from ApF f g to ApF f' g.W$Given a natural transformation from f to f'3 this gives a monoidal natural transformation from ApT f g to ApT f' g.X$Given a natural transformation from g to g'3 this gives a monoidal natural transformation from ApF f g to ApF f g'.Y$Given a natural transformation from g to g'3 this gives a monoidal natural transformation from ApT f g to ApT f g'.ZPull out and join m layers of K f m a.[$Given a natural transformation from f to g>, this gives a canonical monoidal natural transformation from J f to g."runAp t == retractApp . hoistApp t\8Perform a monoidal analysis over free applicative value.Example:  count :: J f a -> U count = getSum . runAp_ (\_ -> Sum 1) ]JInterprets the free applicative functor over f using the semantics for Q and R) given by the Applicative instance for f.retractApp == runAp id^$Given a natural transformation from f to g>, this gives a canonical monoidal natural transformation from I f to g.IJKLMNOPQRSTUVWXYZ[\]^KLMNOPQRTSUWVYXZJ[\]I^KLMNOP(C) 2008-2011 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> experimentalfundeps, MPTCsSafe;=>?Af0Allows you to peel a layer off a cofree comonad.gRemove a layer.fgfgfg(C) 2008-2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalMPTCs, fundepsSafe 1;=>?QV o The cofree X of a functor f.p)This is a cofree comonad of some functor f, with a comonad w# threaded through it at each level.s;This is the base functor of the cofree comonad transformer.u$Extract the head of the base functorv%Extract the tails of the base functorw1Wrap another layer around a cofree comonad value.cofree is a right inverse of x. runCofree . cofree == id x2Unpeel the first layer off a cofree comonad value. runCofree is a right inverse of w. cofree . runCofree == id y#Lift a natural transformation from f to g" into a comonad homomorphism from p f w to p g wz Unfold a CofreeT= comonad transformer from a coalgebra and an initial comonad.fgopqrstuvwxyzpqrowxstfguvyzpqrstt5 (C) 2008-2015 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> experimentalnon-portable (fundeps, MPTCs)Safe 7;=>?AFTMonads provide substitution (S) and renormalization (Y): m Z f = Y (S f m)A free [o is one that does no work during the normalization step beyond simply grafting the two monadic values together.[] is not a free [ (in this sense) because Y [[a]] smashes the lists flat.On the other hand, consider: ,data Tree a = Bin (Tree a) (Tree a) | Tip a   instance [ Tree where \ = Tip Tip a Z f = f a Bin l r Z f = Bin (l Z f) (r Z f) This [ is the free [ 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 [ instance. Moreover, Control.Monad.Free.Church provides a  instance that can improve the  asymptotic[ complexity of code that constructs free monads by effectively reassociating the use of (Z+). You may also want to take a look at the kan-extensions package ( 1http://hackage.haskell.org/package/kan-extensions).See * for a more formal definition of the free [ for a P. Add a layer. -wrap (fmap f x) "a wrap (fmap return x) >>= f =A version of lift that can be used with just a Functor for f.;A version of wrap for monad transformers over a free monad.Note:- that this is the default implementation for  for MonadFree f (t m). (C) 2008-2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalMPTCs, fundepsSafe]rPick a name for an operation. For normal constructors it lowers first letter. For infix ones it omits the first :.^#Check if parameter is used in type._Analyze constructor argument.`TApply transformation to the return value independently of how many parameters does e have. E.g.  mapRet Just (x y z -> x + y * z) goes to x y z -> Just (x + y * z)aUnification of two types. next with  a -> next gives Maybe a return type  a -> next with  b -> next gives  Either a b return typebyUnifying a list of types (possibly refining expressions). Name is used when the return type is supposed to be arbitrary.c<Provide free monadic actions for a single value constructor.dGet construstor name(s).e4Provide free monadic actions for a type declaration.f)Generate monadic actions for a data type.g@Generate monadic action for a single constructor of a data type.$( ''T)L provides free monadic actions for the constructors of the given data type T.Like , but does not provide type signatures. This can be used to attach Haddock comments to individual arguments for each generated function. data LangF x = Output String x makeFree_ 'LangF -- | Output a string. output :: MonadFree LangF m => String -- ^ String to output. -> m () -- ^ No result. 6 must be called *before* the explicit type signatures.$( 'Con)6 provides free monadic action for a data constructor Conx. Note that you can attach Haddock comment to the generated function by placing it before the top-level invocation of : *-- | Output a string. makeFreeCon 'Output Like n, but does not provide a type signature. This can be used to attach Haddock comments to individual arguments. data LangF x = Output String x makeFreeCon_ 'Output -- | Output a string. output :: MonadFree LangF m => String -- ^ String to output. -> m () -- ^ No result. 5 must be called *before* the explicit type signature.eInclude type signature?DInclude only mentioned constructor names. Use all constructors when Nothing.Data type declaration.fInclude type signature?DInclude only mentioned constructor names. Use all constructors when Nothing. Type name.Generated declarations.gInclude type signature?Constructor name.Generated declarations.hijSafeG5klmnopqrstuvwxyz{|}~ (C) 2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalMPTCs, fundepsSafe 1;=>?QVPlain iterative computations.7The monad supporting iteration based over a base monad m.  ~ FreeT  4Builds an iterative computation from one first step.runIter . iter == id3Executes the first step of an iterative computationiter . runIter == id*Adds an extra layer to a free monad value.'In particular, for the iterative monad X, this makes the computation require one more step, without changing its final result.runIter (delay ma) == Right ma is the left inverse of   .  =   Tear down a Free [ using iteration.Like  with monadic result.Lift a monad homomorphism from m to n into a Monad homomorphism from  m to  n.GLifts a plain, non-terminating computation into a richer environment.  is a [ homomorphism.#A computation that never terminates1Repeatedly run a computation until it produces a K value. This can be useful when paired with a monad that has side effects.For example, we may have genId :: IO (Maybe Id)z that uses a random number generator to allocate ids, but fails if it finds a collision. We can repeatedly run this with  ( genId) :: IO Id Cuts off an iterative computation after a given number of steps. If the number of steps is 0 or less, no computation nor monadic effects will take place.IThe step where the final value is produced also counts towards the limit.Some examples (n "e 0):  0 _ "a \   (n+1)  \ "a \    (n+1)   "a      (n+1)   "a  .  n  n  "a   (\ )  n Calling    nT is always terminating, provided each of the steps in the iteration is terminating.`Interleaves the steps of a finite list of iterative computations, and collects their results.VThe resulting computation has as many steps as the longest computation in the list.VInterleaves the steps of a finite list of computations, and discards their results.VThe resulting computation has as many steps as the longest computation in the list.Equivalent to void  . Capretta's race" combinator. Satisfies left catch. Safe 01;=>?QV$The "free monad" for an applicative f.0The "free monad transformer" for an applicative f"The base functor for a free monad.4Evaluates the first layer out of a free monad value.'Pushes a layer into a free monad value.'Given an applicative homomorphism from f (m a) to m a6, tear down a free monad transformer using iteration.'Given an applicative homomorphism from  f (t m a) to t m aI, tear down a free monad transformer using iteration over a transformer.Lift a monad homomorphism from m to n into a monad homomorphism from  f m to  f n  :: ([ m, P f) => (m ~> n) ->  f m ~>  f n&Lift an applicative homomorphism from f to g into a monad homomorphism from  f m to  g mPull out and join m layers of  f m a. is the left inverse of   .  =  'Given an applicative homomorphism from f to , tear down a  [ using iteration.Like  for monadic values.CCuts off a tree of computations at a given depth. If the depth is 0> or less, no computation nor monadic effects will take place.Some examples (n "e 0):  0 _ "a \   (n+1)  \ "a \    (n+1)   "a      (n+1)   "a   S ( n) Calling    nT is always terminating, provided each of the steps in the iteration is terminating.partialIterT n phi m interprets first n layers of m using phi$. This is sort of the opposite for .Some examples (n "e 0):  0 _ m "a m  (n+1) phi  \ "a \  (n+1) phi   "a   (n+1) phi   "a Y .  . phi intersperseT f m inserts a layer f between every two layers in m.  f  \ "a \  f   "a   f   "a   S ( (  ( f)  )) <Tear down a free monad transformer using Monad instance for t m.intercalateT f m inserts a layer f between every two layers in m and then retracts the result.  f "a  .  f  (C) 2008-2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalMPTCs, fundepsSafe 01;=>?QV0$The "free monad" for a functor f.%+The "free monad transformer" for a functor f("The base functor for a free monad.+4Evaluates the first layer out of a free monad value.,'Pushes a layer into a free monad value.-3Tear down a free monad transformer using iteration..FTear down a free monad transformer using iteration over a transformer./Lift a monad homomorphism from m to n into a monad homomorphism from % f m to % f n / :: ([ m, P f) => (m ~> n) -> % f m ~> % f n0The very definition of a free monad transformer is that given a natural transformation you get a monad transformer homomorphism.1#Lift a natural transformation from f to g into a monad homomorphism from % f m to % g m2Pull out and join m layers of % f m a.33 is the left inverse of  3 .  =  4 Tear down a $ [ using iteration.5Like 4 for monadic values.6CCuts off a tree of computations at a given depth. If the depth is 0> or less, no computation nor monadic effects will take place.Some examples (n "e 0): 6 0 _ "a \  6 (n+1)  \ "a \   6 (n+1)   "a     6 (n+1)   "a   S (6 n) Calling 3  6 nT is always terminating, provided each of the steps in the iteration is terminating.7partialIterT n phi m interprets first n layers of m using phi$. This is sort of the opposite for 6.Some examples (n "e 0): 7 0 _ m "a m 7 (n+1) phi  \ "a \ 7 (n+1) phi   "a  7 (n+1) phi   "a Y .  . phi 8intersperseT f m inserts a layer f between every two layers in m. 8 f  \ "a \ 8 f   "a  8 f   "a   S (. (  ( f)  )) 9<Tear down a free monad transformer using Monad instance for t m.:intercalateT f m inserts a layer f between every two layers in m and then retracts the result. : f "a 9 . 8 f $%&'()*+,-./0123456789:()*%&'$,+-./012678:9345%&'()*(C) 2008-2014 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisional)non-portable (rank-2 polymorphism, MTPCs)Safe;=>?QVU8jThe "free monad" for a functor f.k+The "free monad transformer" for a functor fn8Generate a Church-encoded free monad transformer from a % monad transformer.o Convert to a % free monad representation.p Unwrap the $4 monad to obtain it's Church-encoded representation.qIWrap a Church-encoding of a "free monad" as the free monad for a functor.r3Tear down a free monad transformer using iteration.sFTear down a free monad transformer using iteration over a transformer.tLift a monad homomorphism from m to n into a monad homomorphism from k f m to k f n t :: ([ m, [ n, P f) => (m ~> n) -> k f m ~> k f nu#Lift a natural transformation from f to g into a monad homomorphism from k f m to k g nvPull out and join m layers of % f m a.wCuts off a tree of computations at a given depth. If the depth is 0 or less, no computation nor monadic effects will take place.Some examples (n "e 0):'cutoff 0 _ == return Nothing&cutoff (n+1) . return == return . Just,cutoff (n+1) . lift == lift . liftM Just0cutoff (n+1) . wrap == wrap . fmap (cutoff n)pCalling 'retract . cutoff n' is always terminating, provided each of the steps in the iteration is terminating.xx is the left inverse of  x .  =  yFTear down a free monad transformer using iteration over a transformer.z Tear down an j [ using iteration.{Like z for monadic values.|-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 j behind the scenes.OThis 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/Tand "Asymptotic Improvement of Computations over Free Monads" by Janis Voightlnder: (http://www.iai.uni-bonn.de/~jv/mpc08.pdfvImprove the asymptotic performance of code that builds a free monad transformer with only binds and returns by using k behind the scenes. Similar to ~.jklmnopqrstuvwxyz{|}~klmjqpnorstuvw~|}xyz{klmSafe 1;=>?QVo-!A free monad given an applicative is the left inverse of  and   .  =   .  =  'Given an applicative homomorphism from f to Identity, tear down a  [ using iteration.Like  for applicative values.Like  for monadic values.&Lift an applicative homomorphism from f to g into a monad homomorphism from  f to  g.@Given an applicative homomorphism, you get a monad homomorphism. Convert a  monad from Control.Monad.Free.Ap to a  monad from Control.Monad.Trans.Free.Ap. WARNING: This assumes that  is an applicative homomorphism.Cuts off a tree of computations at a given depth. If the depth is 0 or less, no computation nor monadic effects will take place.Some examples (n "e 0):'cutoff 0 _ == return Nothing&cutoff (n+1) . return == return . Just,cutoff (n+1) . lift == lift . liftM Just0cutoff (n+1) . wrap == wrap . fmap (cutoff n)pCalling 'retract . cutoff n' is always terminating, provided each of the steps in the iteration is terminating. Unfold a free monad from a seed.-Unfold a free monad from a seed, monadically.This is Prism' (Free f a) a in disguisepreview _Pure (Pure 3)Just 3 review _Pure 3 :: Free Maybe IntPure 3This is  Prism' (Free f a) (f (Free f a)) in disguise,preview _Free (review _Free (Just (Pure 3)))Just (Just (Pure 3))review _Free (Just (Pure 3))Free (Just (Pure 3))LThis is not a true monad transformer. It is only a monad transformer "up to ".3This violates the MonadPlus laws, handle with care.5This violates the Alternative laws, handle with care.(C) 2008-2015 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalMPTCs, fundepsSafe 01;=>?QVIThe  [ for a P f.FormallyA [ n is a free [ for f# if every monad homomorphism from n to another monad m1 is equivalent to a natural transformation from f to m. Why Free?AEvery "free" functor is left adjoint to some "forgetful" functor.!If we define a forgetful functor UP from the category of monads to the category of functors that just forgets the [, leaving only the P. i.e. U (M,\,) = 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 [5 homomorphisms (natural transformations that respect \ and ).*Morphisms in the category of functors are P) 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 (Z)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 V is fairly naive about the encoding. For more efficient free monad implementation see Control.Monad.Free.Church, in particular note the 6 combinator. You may also want to take a look at the kan-extensions package ( 1http://hackage.haskell.org/package/kan-extensions)./A number of common monads arise as free monads,Given  data Empty a,  Empty is isomorphic to the  monad. p can be used to model a partiality monad where each layer represents running the computation for a while longer. is the left inverse of  and   .  =   .  =   Tear down a  [ using iteration.Like  for applicative values.Like  for monadic values.#Lift a natural transformation from f to g$ into a natural transformation from FreeT f to FreeT g.hThe very definition of a free monad is that given a natural transformation you get a monad homomorphism. Convert a  monad from Control.Monad.Free to a % monad from Control.Monad.Trans.Free.Cuts off a tree of computations at a given depth. If the depth is 0 or less, no computation nor monadic effects will take place.Some examples (n "e 0):'cutoff 0 _ == return Nothing&cutoff (n+1) . return == return . Just,cutoff (n+1) . lift == lift . liftM Just0cutoff (n+1) . wrap == wrap . fmap (cutoff n)pCalling 'retract . cutoff n' is always terminating, provided each of the steps in the iteration is terminating. Unfold a free monad from a seed.-Unfold a free monad from a seed, monadically.This is Prism' (Free f a) a in disguisepreview _Pure (Pure 3)Just 3 review _Pure 3 :: Free Maybe IntPure 3This is  Prism' (Free f a) (f (Free f a)) in disguise,preview _Free (review _Free (Just (Pure 3)))Just (Just (Pure 3))review _Free (Just (Pure 3))Free (Just (Pure 3))LThis is not a true monad transformer. It is only a monad transformer "up to ".3This violates the MonadPlus laws, handle with care.5This violates the Alternative laws, handle with care.(C) 2011-2015 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisional"non-portable (rank-2 polymorphism)Safe ;=>?QV” ,The Church-encoded free monad for a functor f.It is asymptotically more efficient to use (Z) for  than it is to (Z) with . 6http://comonad.com/reader/2011/free-monads-for-less-2/ Tear down a  [ using iteration.Like iter for monadic values. is the left inverse of  and   .  =   .  =  #Lift a natural transformation from f to g$ into a natural transformation from F f to F g.hThe very definition of a free monad is that given a natural transformation you get a monad homomorphism.-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.OThis is based on the "Free Monads for Less" series of articles by Edward Kmett: 4http://comonad.com/reader/2011/free-monads-for-less/Free monads for less  Part 1 6http://comonad.com/reader/2011/free-monads-for-less-2/Free monads for less  Part 2and  (http://www.iai.uni-bonn.de/~jv/mpc08.pdf9"Asymptotic Improvement of Computations over Free Monads" by Janis Voightlnder.Cuts off a tree of computations at a given depth. If the depth is 0 or less, no computation nor monadic effects will take place.Some examples (n "e 0):'cutoff 0 _ == return Nothing&cutoff (n+1) . return == return . Just*cutoff (n+1) . lift == lift . liftM Just/cutoff (n+1) . wrap == wrap . fmap (cutoff n)Calling  .  nT is always terminating, provided each of the steps in the iteration is terminating.3This violates the MonadPlus laws, handle with care.5This violates the Alternative laws, handle with care.(C) 2008-2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalMPTCs, fundepsSafe1;=>? The coiterative comonad 6This is the coiterative comonad generated by a comonad/Prepends a result to a coiterative computation. runCoiter . uncurry coiter == id9Extracts the first result from a coiterative computation. uncurry coiter . runCoiter == id Unfold a CoiterTJ comonad transformer from a cokleisli arrow and an initial comonadic seed. fg         fg   (C) 2008-2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> provisionalMPTCs, fundepsSafe 01;=>?QVn $The $ X of a functor f.FormallyA X v is a cofree X for f5 if every comonad homomorphism from another comonad w to v1 is equivalent to a natural transformation from w to f.A cofree1 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  and , leaving you with only a P.eIn 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)'i describes a Moore machine with states labeled with values of type a, and transitions on edges of type b.Furthermore, if the functor f7 forms a monoid (for example, by being an instance of N), the resulting X is also a [. See  5http://www.cs.appstate.edu/~johannp/jfp06-revised.pdf/Monadic Augment and Generalised Shortcut Fusion5 by Neil Ghani et al., Section 4.3 for more details.In particular, if  f a "a [a]%, the resulting data structure is a  'https://en.wikipedia.org/wiki/Rose_tree Rose tree'. For a practical application, check  ghttps://web.archive.org/web/20161208002902/http://www.cs.le.ac.uk/people/ak155/Papers/CALCO-07/GK07.pdf'Higher Dimensional Trees, Algebraically by Neil Ghani et al.&9Use coiteration to generate a cofree comonad from a seed. & f = ( (  f)'!Like coiter for comonadic values.($Unfold a cofree comonad from a seed.)1Unfold a cofree comonad from a seed, monadically.+  . + = ,DThis is a lens that can be used to read or write from the target of .Using (^.) from the lens package: foo ^. , ==  fooFor more on lenses see the lens package on hackage , :: Lens' ($ g a) a-CThis is a lens that can be used to read or write to the tails of a $ X.Using (^.) from the lens package: foo ^. - == g fooFor more on lenses see the lens package on hackage - :: Lens' ($ g a) (g ($ g a)). Construct an Lens into a $ gd given a list of lenses into the base functor. When the input list is empty, this is equivalent to ,J. When the input list is non-empty, this composes the input lenses with - to walk through the $ g before using ,* to get the element at the final location.For more on lenses see the lens package on hackage. telescoped :: [Lens' (g ($ g a)) ($ g a)] -> Lens' ($ g a) a telescoped :: [Traversal' (g ($ g a)) ($ g a)] -> Traversal' ($ g a) a telescoped :: [Getter (g ($ g a)) ($ g a)] -> Getter ($ g a) a telescoped :: [Fold (g ($ g a)) ($ g a)] -> Fold ($ g a) a telescoped :: [Setter' (g ($ g a)) ($ g a)] -> Setter' ($ g a) a/ Construct an Lens into a $ gU given a list of lenses into the base functor. The only difference between this and . is that . focuses on a single value, but this focuses on the entire remaining subtree. When the input list is empty, this is equivalent to J. When the input list is non-empty, this composes the input lenses with - to walk through the $ g.For more on lenses see the lens package on hackage. telescoped :: [Lens' (g ($ g a)) ($ g a)] -> Lens' ($ g a) ($ g a) telescoped :: [Traversal' (g ($ g a)) ($ g a)] -> Traversal' ($ g a) ($ g a) telescoped :: [Getter (g ($ g a)) ($ g a)] -> Getter ($ g a) ($ g a) telescoped :: [Fold (g ($ g a)) ($ g a)] -> Fold ($ g a) ($ g a) telescoped :: [Setter' (g ($ g a)) ($ g a)] -> Setter' ($ g a) ($ g a)0A  Traversal'# that gives access to all non-leaf a elements of a $ g! a, where non-leaf is defined as x from  (x :< xs) where null xs is False.6Because this doesn't give access to all values in the $ g%, it cannot be used to change types. 4shoots :: Traversable g => Traversal' (Cofree g a) aEN.B. On GHC < 7.9, this is slightly less flexible, as it has to use null (toList xs) instead.1A  Traversal' that gives access to all leaf a elements of a $ g a, where leaf is defined as x from  (x :< xs) where null xs is True.6Because this doesn't give access to all values in the $ g%, it cannot be used to change types. 4shoots :: Traversable g => Traversal' (Cofree g a) aEN.B. On GHC < 7.9, this is slightly less flexible, as it has to use null (toList xs) instead.GThis is not a true X. transformer, but this instance is convenient.fg$%&'()*+,-./01$%fg+&'()*,-./01$%%5 !"#$%&'()*+,-."#$%&'(*), ! /012345678  9:;<=>?@/0134678  A/0134678 BBCD! EFGHIJKLMN/04#OPQRSTUVWXYZ[\]^_``abcdefghijklmnopqrstuvwxyz{|}~         !                 !                 !"#$%&'()*+,-./!0123456789:;<=>?@ABCDEFGHIJKLMNOPQR!0123456789:;<=>?@ABCDEFGHIJKLMNOPQRS  TUVWXYZ[\]^_`abcdefghiijkl4mnopqrstuvwxyz{|}~_ck45                   free-5-6UoWGErQ04KGIlv1KLBUqPControl.Alternative.FreeControl.Alternative.Free.FinalControl.Applicative.FreeControl.Applicative.Free.FastControl.Applicative.Free.FinalControl.Applicative.Trans.FreeControl.Comonad.Cofree.ClassControl.Comonad.Trans.CofreeControl.Monad.Free.ClassControl.Monad.Free.THControl.Monad.Trans.IterControl.Monad.Trans.Free.ApControl.Monad.Trans.FreeControl.Monad.Trans.Free.ChurchControl.Monad.Free.ApControl.Monad.FreeControl.Monad.Free.ChurchControl.Comonad.Trans.CoiterControl.Comonad.CofreeFreeData.Functor.Classes.Compat Control.Monadjoinimprove Data.FunctorIdentity Control.Arrow&&&Alt alternativesAltFApPureliftAltrunAlthoistAlt $fMonoidAlt$fSemigroupAlt$fAlternativeAlt$fAltAlt $fApplyAlt$fApplicativeAlt$fApplicativeAltF $fFunctorAlt $fFunctorAltF_runAltrunAprunAp_liftApiterAphoistAp retractAp $fComonadAp$fApplicativeAp $fApplyAp $fFunctorApunApASeqANilACons reduceASeq hoistASeq traverseASeq rebaseASeq_runApApTgetApTApFliftApTliftApOrunApFrunApTrunApT_hoistApFhoistApTtransApFtransApTjoinApT$fAlternativeApT $fApplyApT $fApplyApF$fApplicativeApT$fApplicativeApF $fFunctorApT $fFunctorApF ComonadCofreeunwrap$fComonadCofreefTracedT$fComonadCofreefStoreT$fComonadCofreefEnvT$fComonadCofreefIdentityT$fComonadCofreeConst(,)$fComonadCofree[]Tree$fComonadCofreeMaybeNonEmptyCofreeCofreeT runCofreeTCofreeF:<headFtailFcofree runCofree transCofreeTcoiterT $fDataCofreeF$fBitraversableCofreeF$fBifoldableCofreeF$fBifunctorCofreeF$fTraversableCofreeF$fFoldableCofreeF$fFunctorCofreeF $fDataCofreeT$fMonadZipCofreeT$fMonadTransCofreeT$fApplicativeCofreeT$fMonadCofreeT $fOrdCofreeT $fEqCofreeT $fReadCofreeT $fShowCofreeT$fComonadHoistCofreeT$fComonadEnveCofreeT$fComonadCofreefCofreeT$fComonadTransCofreeT$fTraversableCofreeT$fFoldableCofreeT$fComonadCofreeT$fFunctorCofreeT $fEqCofreeF $fOrdCofreeF $fShowCofreeF $fReadCofreeF MonadFreewrapliftFwrapT$fMonadFreefExceptT$fMonadFreefErrorT$fMonadFreefListT$fMonadFreefIdentityT$fMonadFreefMaybeT$fMonadFreefRWST$fMonadFreefRWST0$fMonadFreefWriterT$fMonadFreefWriterT0$fMonadFreefContT$fMonadFreefStateT$fMonadFreefStateT0$fMonadFreefReaderTmakeFree makeFree_ makeFreeCon makeFreeCon_ $fShowArgIterIterTrunIterTiterrunIterdelayretractfoldfoldM hoistIterTliftIternever untilJustcutoff interleave interleave_ $fDataIterT$fSemigroupIterT $fMonoidIterT$fMonadCatchIterT$fMonadThrowIterT$fMonadFreeIdentityIterT$fMonadContIterT$fMonadIOIterT$fMonadErroreIterT$fMonadStatesIterT$fMonadWriterwIterT$fMonadReadereIterT$fTraversable1IterT$fTraversableIterT$fFoldable1IterT$fFoldableIterT$fMonadTransIterT$fMonadPlusIterT$fAlternativeIterT$fMonadFixIterT $fBindIterT $fApplyIterT$fMonadFailIterT $fMonadIterT$fApplicativeIterT$fFunctorIterT $fReadIterT $fRead1IterT $fShowIterT $fShow1IterT $fOrdIterT $fOrd1IterT $fEqIterT $fEq1IterTFreeTrunFreeTFreeFrunFreefreeiterTiterTM hoistFreeT transFreeT joinFreeTiterM partialIterT intersperseTretractT intercalateT$fBitraversableFreeF$fBifoldableFreeF$fBifunctorFreeF$fTraversableFreeF$fFoldableFreeF$fFunctorFreeF $fOrd1FreeF $fOrd2FreeF $fEq1FreeF $fEq2FreeF $fRead1FreeF $fRead2FreeF $fShow1FreeF $fShow2FreeF$fTraversableFreeT$fFoldableFreeT$fMonadCatchFreeT$fMonadThrowFreeT$fMonadFreefFreeT$fMonadPlusFreeT$fAlternativeFreeT$fMonadContFreeT$fMonadErroreFreeT$fMonadStatesFreeT$fMonadWriterwFreeT$fMonadReaderrFreeT$fMonadIOFreeT$fMonadTransFreeT$fMonadFailFreeT $fMonadFreeT $fBindFreeT $fApplyFreeT$fApplicativeFreeT$fFunctorFreeT $fReadFreeT $fRead1FreeT $fShowFreeT $fShow1FreeT $fOrd1FreeT $fOrdFreeT $fEq1FreeT $fEqFreeT $fEqFreeF $fOrdFreeF $fShowFreeF $fReadFreeF foldFreeT$fMonadBasebFreeTFFTrunFTtoFTfromFTrunFhoistFTtransFTjoinFTfromFtoFimproveT$fMonadCatchFT$fMonadThrowFT$fMonadStatesFT$fMonadWriterwFT$fMonadReaderrFT $fMonadContFT$fMonadErroreFT $fMonadIOFT$fTraversableFT $fFoldableFT $fMonadPlusFT$fAlternativeFT$fMonadTransFT$fMonadFreefFT $fMonadFT$fBindFT$fApplicativeFT $fApplyFT $fFunctorFT$fOrdFT$fEqFT$fOrd1FT$fEq1FTiterA hoistFreefoldFreetoFreeTunfoldunfoldM_Pure_Free$fMonadFreefFree$fMonadContFree$fMonadErroreFree$fMonadStatesFree$fMonadReadereFree$fMonadWritereFree$fTraversable1Free$fTraversableFree$fFoldable1Free$fFoldableFree$fMonadTransFree$fMonadPlusFree$fAlternativeFree$fMonadFixFree $fMonadFree $fBindFree$fApplicativeFree $fApplyFree $fFunctorFree $fReadFree $fRead1Free $fShowFree $fShow1Free $fOrdFree $fOrd1Free$fEqFree $fEq1Free $fDataFreehoistFfoldF $fMonadContF$fMonadWriterwF$fMonadReadereF$fMonadStatesF $fMonadFreefF $fMonadTransF $fMonadPlusF$fTraversable1F $fFoldable1F$fTraversableF $fFoldableF $fMonadFixF$fMonadF$fBindF$fAlternativeF$fApplicativeF$fApplyF $fFunctorFCoiterCoiterT runCoiterTcoiter runCoiter $fDataCoiterT $fOrdCoiterT $fEqCoiterT $fReadCoiterT $fShowCoiterT$fComonadStoresCoiterT$fComonadTracedmCoiterT$fComonadHoistCoiterT$fComonadEnveCoiterT$fComonadCofreeIdentityCoiterT$fComonadTransCoiterT$fTraversableCoiterT$fFoldableCoiterT$fComonadCoiterT$fFunctorCoiterT$fRead1CoiterT$fShow1CoiterT $fOrd1CoiterT $fEq1CoiterTcoiterW hoistCofreesection_extract_unwrap telescoped telescoped_shootsleaves$fComonadTracedmCofree$fComonadStoresCofree$fComonadEnveCofree$fComonadHoistCofree$fTraversable1Cofree$fTraversableCofree$fFoldable1Cofree$fFoldableCofree $fOrd1Cofree $fOrdCofree $fEq1Cofree $fEqCofree $fReadCofree $fRead1Cofree $fShowCofree $fShow1Cofree$fApplicativeCofree$fComonadApplyCofree $fApplyCofree$fMonadZipCofree $fMonadCofree$fComonadTransCofree$fComonadCofree$fExtendCofree$fFunctorCofree$fDistributiveCofree$fComonadCofreefCofree $fDataCofreebaseGHC.Base Alternative ApplicativeFunctorpure<*>fmap Data.FoldableFoldableghc-prim GHC.TypesIntmaximum$comonad-5.0.2-9aHGfiTnx05C06L3T42NQuControl.ComonadComonad>>=MonadreturnmkOpNameusesTVmkArgmapRetunifyT unifyCapturedliftConconstructorNamesliftDecgenFree genFreeConArgCapturedParammappendData.Functor.Classes showsBinary1 showsUnary1 showsUnary readsBinary1 readsUnary1 readsUnaryshowsBinaryWithshowsUnaryWithreadBinaryWithreadsBinaryWith readUnaryWithreadsUnaryWithreadData readsData showsPrec2liftReadListPrec2DefaultliftReadList2Default readPrec2 readsPrec2compare2eq2 showsPrec1liftReadListPrecDefaultliftReadListDefault readPrec1 readsPrec1compare1eq1Eq1liftEqOrd1 liftCompareRead1 liftReadsPrec liftReadList liftReadPrecliftReadListPrecShow1 liftShowsPrec liftShowListEq2liftEq2Ord2 liftCompare2Read2liftReadsPrec2 liftReadList2 liftReadPrec2liftReadListPrec2Show2liftShowsPrec2 liftShowList2Data.Functor.Identitytransformers-0.5.2.0Control.Monad.Trans.ClassliftidJustNothing.liftMGHC.Listiterate!!<$Maybeextract duplicateData.Functor.ConstConstControl.CategoryControl.Comonad.Trans.Classlower