-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/

-- | Monads for free
--   
--   Free monads are useful for many tree-like structures and domain
--   specific languages.
--   
--   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.
--   
--   Cofree comonads provide convenient ways to talk about branching
--   streams and rose-trees, and can be used to annotate syntax trees.
--   
--   A Comonad v is a cofree Comonad for f
--   if every Comonad homomorphism another comonad w to
--   v is equivalent to a natural transformation from w
--   to f.
@package free
@version 4.2

-- | Monads for free.
module Control.Monad.Free.Class

-- | Monads provide substitution (fmap) and renormalization
--   (join):
--   
--   
--   m >>= f = join (fmap f m)
--   
--   
--   A free Monad is one that does no work during the normalization
--   step beyond simply grafting the two monadic values together.
--   
--   [] is not a free Monad (in this sense) because
--   join [[a]] smashes the lists flat.
--   
--   On the other hand, consider:
--   
--   
--   data Tree a = Bin (Tree a) (Tree a) | Tip a
--   
--   
--   
--   instance Monad Tree where
--     return = Tip
--     Tip a >>= f = f a
--     Bin l r >>= f = Bin (l >>= f) (r >>= f)
--   
--   
--   This Monad is the free Monad of Pair:
--   
--   
--   data Pair a = Pair a a
--   
--   
--   And we could make an instance of MonadFree for it directly:
--   
--   
--   instance MonadFree Pair Tree where
--      wrap (Pair l r) = Bin l r
--   
--   
--   Or we could choose to program with Free Pair instead
--   of Tree and thereby avoid having to define our own
--   Monad instance.
--   
--   Moreover, Control.Monad.Free.Church provides a MonadFree
--   instance that can improve the asymptotic complexity of code
--   that constructs free monads by effectively reassociating the use of
--   (>>=). You may also want to take a look at the
--   kan-extensions package
--   (http://hackage.haskell.org/package/kan-extensions).
--   
--   See Free for a more formal definition of the free Monad
--   for a Functor.
class Monad m => MonadFree f m | m -> f where wrap = join . lift . wrap . fmap return
wrap :: MonadFree f m => f (m a) -> m a

-- | A version of lift that can be used with just a Functor for f.
liftF :: (Functor f, MonadFree f m) => f a -> m a

-- | A version of wrap for monad transformers over a free monad.
--   
--   Note: that this is the default implementation for wrap
--   for MonadFree f (t m).
wrapT :: (Functor f, MonadFree f m, MonadTrans t, Monad (t m)) => f (t m a) -> t m a
instance (Functor f, MonadFree f m, Error e) => MonadFree f (ErrorT e m)
instance (Functor f, MonadFree f m) => MonadFree f (ListT m)
instance (Functor f, MonadFree f m) => MonadFree f (IdentityT m)
instance (Functor f, MonadFree f m) => MonadFree f (MaybeT m)
instance (Functor f, MonadFree f m, Monoid w) => MonadFree f (RWST r w s m)
instance (Functor f, MonadFree f m, Monoid w) => MonadFree f (RWST r w s m)
instance (Functor f, MonadFree f m, Monoid w) => MonadFree f (WriterT w m)
instance (Functor f, MonadFree f m, Monoid w) => MonadFree f (WriterT w m)
instance (Functor f, MonadFree f m) => MonadFree f (ContT r m)
instance (Functor f, MonadFree f m) => MonadFree f (StateT s m)
instance (Functor f, MonadFree f m) => MonadFree f (StateT s m)
instance (Functor f, MonadFree f m) => MonadFree f (ReaderT e m)

-- | The free monad transformer
module Control.Monad.Trans.Free

-- | The base functor for a free monad.
data FreeF f a b
Pure :: a -> FreeF f a b
Free :: (f b) -> FreeF f a b

-- | The "free monad transformer" for a functor f.
newtype FreeT f m a
FreeT :: m (FreeF f a (FreeT f m a)) -> FreeT f m a
runFreeT :: FreeT f m a -> m (FreeF f a (FreeT f m a))

-- | Monads provide substitution (fmap) and renormalization
--   (join):
--   
--   
--   m >>= f = join (fmap f m)
--   
--   
--   A free Monad is one that does no work during the normalization
--   step beyond simply grafting the two monadic values together.
--   
--   [] is not a free Monad (in this sense) because
--   join [[a]] smashes the lists flat.
--   
--   On the other hand, consider:
--   
--   
--   data Tree a = Bin (Tree a) (Tree a) | Tip a
--   
--   
--   
--   instance Monad Tree where
--     return = Tip
--     Tip a >>= f = f a
--     Bin l r >>= f = Bin (l >>= f) (r >>= f)
--   
--   
--   This Monad is the free Monad of Pair:
--   
--   
--   data Pair a = Pair a a
--   
--   
--   And we could make an instance of MonadFree for it directly:
--   
--   
--   instance MonadFree Pair Tree where
--      wrap (Pair l r) = Bin l r
--   
--   
--   Or we could choose to program with Free Pair instead
--   of Tree and thereby avoid having to define our own
--   Monad instance.
--   
--   Moreover, Control.Monad.Free.Church provides a MonadFree
--   instance that can improve the asymptotic complexity of code
--   that constructs free monads by effectively reassociating the use of
--   (>>=). You may also want to take a look at the
--   kan-extensions package
--   (http://hackage.haskell.org/package/kan-extensions).
--   
--   See Free for a more formal definition of the free Monad
--   for a Functor.
class Monad m => MonadFree f m | m -> f where wrap = join . lift . wrap . fmap return
wrap :: MonadFree f m => f (m a) -> m a

-- | A version of lift that can be used with just a Functor for f.
liftF :: (Functor f, MonadFree f m) => f a -> m a

-- | Tear down a free monad transformer using iteration.
iterT :: (Functor f, Monad m) => (f (m a) -> m a) -> FreeT f m a -> m a

-- | Lift a monad homomorphism from m to n into a monad
--   homomorphism from FreeT f m to FreeT f
--   n
--   
--   
--   hoistFreeT :: (Monad m, Functor f) => (m ~> n) -> FreeT f m ~> FreeT f n
--   
hoistFreeT :: (Monad m, Functor f) => (forall a. m a -> n a) -> FreeT f m b -> FreeT f n b

-- | Lift a natural transformation from f to g into a
--   monad homomorphism from FreeT f m to FreeT
--   g n
transFreeT :: (Monad m, Functor g) => (forall a. f a -> g a) -> FreeT f m b -> FreeT g m b
instance (Eq a, Eq (f b)) => Eq (FreeF f a b)
instance (Ord a, Ord (f b)) => Ord (FreeF f a b)
instance (Show a, Show (f b)) => Show (FreeF f a b)
instance (Read a, Read (f b)) => Read (FreeF f a b)
instance (Typeable1 f, Typeable1 w, Typeable a, Data (w (FreeF f a (FreeT f w a))), Data a) => Data (FreeT f w a)
instance (Typeable1 f, Typeable a, Typeable b, Data a, Data (f b), Data b) => Data (FreeF f a b)
instance (Typeable1 f, Typeable1 w) => Typeable1 (FreeT f w)
instance Typeable1 f => Typeable2 (FreeF f)
instance (Monad m, Traversable m, Traversable f) => Traversable (FreeT f m)
instance (Foldable m, Foldable f) => Foldable (FreeT f m)
instance (Functor f, Monad m) => MonadFree f (FreeT f m)
instance (Functor f, MonadPlus m) => MonadPlus (FreeT f m)
instance (Functor f, MonadPlus m) => Alternative (FreeT f m)
instance (Functor f, MonadIO m) => MonadIO (FreeT f m)
instance MonadTrans (FreeT f)
instance (Functor f, Monad m) => Monad (FreeT f m)
instance (Functor f, Monad m) => Applicative (FreeT f m)
instance (Functor f, Monad m) => Functor (FreeT f m)
instance Read (m (FreeF f a (FreeT f m a))) => Read (FreeT f m a)
instance Show (m (FreeF f a (FreeT f m a))) => Show (FreeT f m a)
instance Ord (m (FreeF f a (FreeT f m a))) => Ord (FreeT f m a)
instance Eq (m (FreeF f a (FreeT f m a))) => Eq (FreeT f m a)
instance Traversable f => Bitraversable (FreeF f)
instance Foldable f => Bifoldable (FreeF f)
instance Functor f => Bifunctor (FreeF f)
instance Traversable f => Traversable (FreeF f a)
instance Foldable f => Foldable (FreeF f a)
instance Functor f => Functor (FreeF f a)

-- | Based on Capretta's Iterative Monad Transformer
--   
--   Unlike Free, this is a true monad transformer.
module Control.Monad.Trans.Iter

-- | Monads provide substitution (fmap) and renormalization
--   (join):
--   
--   
--   m >>= f = join (fmap f m)
--   
--   
--   A free Monad is one that does no work during the normalization
--   step beyond simply grafting the two monadic values together.
--   
--   [] is not a free Monad (in this sense) because
--   join [[a]] smashes the lists flat.
--   
--   On the other hand, consider:
--   
--   
--   data Tree a = Bin (Tree a) (Tree a) | Tip a
--   
--   
--   
--   instance Monad Tree where
--     return = Tip
--     Tip a >>= f = f a
--     Bin l r >>= f = Bin (l >>= f) (r >>= f)
--   
--   
--   This Monad is the free Monad of Pair:
--   
--   
--   data Pair a = Pair a a
--   
--   
--   And we could make an instance of MonadFree for it directly:
--   
--   
--   instance MonadFree Pair Tree where
--      wrap (Pair l r) = Bin l r
--   
--   
--   Or we could choose to program with Free Pair instead
--   of Tree and thereby avoid having to define our own
--   Monad instance.
--   
--   Moreover, Control.Monad.Free.Church provides a MonadFree
--   instance that can improve the asymptotic complexity of code
--   that constructs free monads by effectively reassociating the use of
--   (>>=). You may also want to take a look at the
--   kan-extensions package
--   (http://hackage.haskell.org/package/kan-extensions).
--   
--   See Free for a more formal definition of the free Monad
--   for a Functor.
class Monad m => MonadFree f m | m -> f where wrap = join . lift . wrap . fmap return
wrap :: MonadFree f m => f (m a) -> m a
data IterF a b
Pure :: a -> IterF a b
Iter :: b -> IterF a b

-- | The monad supporting iteration based over a base monad m.
--   
--   
--   IterT ~ FreeT Identity
--   
data IterT m a
IterT :: m (IterF a (IterT m a)) -> IterT m a
runIterT :: IterT m a -> m (IterF a (IterT m a))
delay :: (Monad f, MonadFree f m) => m a -> m a

-- | retract is the left inverse of lift
--   
--   
--   retract . lift = id
--   
retract :: Monad m => IterT m a -> m a

-- | Tear down a Free Monad using iteration.
iter :: Monad m => (m a -> a) -> IterT m a -> a

-- | Lift a monad homomorphism from m to n into a Monad
--   homomorphism from IterT m to IterT n.
hoistIterT :: Monad n => (forall a. m a -> n a) -> IterT m b -> IterT n b
instance Typeable2 IterF
instance (Eq a, Eq b) => Eq (IterF a b)
instance (Ord a, Ord b) => Ord (IterF a b)
instance (Show a, Show b) => Show (IterF a b)
instance (Read a, Read b) => Read (IterF a b)
instance (Typeable1 m, Typeable a, Data (m (IterF a (IterT m a))), Data a) => Data (IterT m a)
instance Typeable1 m => Typeable1 (IterT m)
instance Monad m => MonadFree Identity (IterT m)
instance (Functor m, MonadState s m) => MonadState s (IterT m)
instance (Functor m, MonadReader e m) => MonadReader e (IterT m)
instance (Monad m, Traversable1 m) => Traversable1 (IterT m)
instance (Monad m, Traversable m) => Traversable (IterT m)
instance Foldable1 m => Foldable1 (IterT m)
instance Foldable m => Foldable (IterT m)
instance MonadTrans IterT
instance MonadPlus m => MonadPlus (IterT m)
instance MonadPlus m => Alternative (IterT m)
instance MonadFix m => MonadFix (IterT m)
instance Monad m => Bind (IterT m)
instance Monad m => Apply (IterT m)
instance Monad m => Monad (IterT m)
instance Monad m => Applicative (IterT m)
instance Monad m => Functor (IterT m)
instance Read (m (IterF a (IterT m a))) => Read (IterT m a)
instance Show (m (IterF a (IterT m a))) => Show (IterT m a)
instance Ord (m (IterF a (IterT m a))) => Ord (IterT m a)
instance Eq (m (IterF a (IterT m a))) => Eq (IterT m a)
instance Bitraversable IterF
instance Bifoldable IterF
instance Bifunctor IterF
instance Traversable (IterF a)
instance Foldable (IterF a)
instance Functor (IterF a)

-- | left-distributive MonadPlus for free.
module Control.MonadPlus.Free

-- | Monads provide substitution (fmap) and renormalization
--   (join):
--   
--   
--   m >>= f = join (fmap f m)
--   
--   
--   A free Monad is one that does no work during the normalization
--   step beyond simply grafting the two monadic values together.
--   
--   [] is not a free Monad (in this sense) because
--   join [[a]] smashes the lists flat.
--   
--   On the other hand, consider:
--   
--   
--   data Tree a = Bin (Tree a) (Tree a) | Tip a
--   
--   
--   
--   instance Monad Tree where
--     return = Tip
--     Tip a >>= f = f a
--     Bin l r >>= f = Bin (l >>= f) (r >>= f)
--   
--   
--   This Monad is the free Monad of Pair:
--   
--   
--   data Pair a = Pair a a
--   
--   
--   And we could make an instance of MonadFree for it directly:
--   
--   
--   instance MonadFree Pair Tree where
--      wrap (Pair l r) = Bin l r
--   
--   
--   Or we could choose to program with Free Pair instead
--   of Tree and thereby avoid having to define our own
--   Monad instance.
--   
--   Moreover, Control.Monad.Free.Church provides a MonadFree
--   instance that can improve the asymptotic complexity of code
--   that constructs free monads by effectively reassociating the use of
--   (>>=). You may also want to take a look at the
--   kan-extensions package
--   (http://hackage.haskell.org/package/kan-extensions).
--   
--   See Free for a more formal definition of the free Monad
--   for a Functor.
class Monad m => MonadFree f m | m -> f where wrap = join . lift . wrap . fmap return
wrap :: MonadFree f m => f (m a) -> m a

-- | The Free MonadPlus for a Functor f.
--   
--   Formally
--   
--   A MonadPlus n is a free MonadPlus for
--   f if every monadplus homomorphism from n to another
--   MonadPlus m is equivalent to a natural transformation from
--   f to m.
--   
--   We model this internally as if left-distribution holds.
--   
data Free f a
Pure :: a -> Free f a
Free :: (f (Free f a)) -> Free f a
Plus :: [Free f a] -> Free f a

-- | retract is the left inverse of lift and liftF
--   
--   
--   retract . lift = id
--   retract . liftF = id
--   
retract :: MonadPlus f => Free f a -> f a

-- | A version of lift that can be used with just a Functor for f.
liftF :: (Functor f, MonadFree f m) => f a -> m a

-- | Tear down a Free Monad using iteration.
iter :: Functor f => (f a -> a) -> ([a] -> a) -> Free f a -> a

-- | Like iter for monadic values.
iterM :: (Monad m, Functor f) => (f (m a) -> m a) -> ([m a] -> m a) -> Free f a -> m a

-- | Lift a natural transformation from f to g into a
--   natural transformation from FreeT f to
--   FreeT g.
hoistFree :: Functor g => (forall a. f a -> g a) -> Free f b -> Free g b
instance (Typeable1 f, Typeable a, Data a, Data (f (Free f a))) => Data (Free f a)
instance Typeable1 f => Typeable1 (Free f)
instance Functor f => MonadFree f (Free f)
instance (Functor m, MonadPlus m, MonadCont m) => MonadCont (Free m)
instance (Functor m, MonadPlus m, MonadError e m) => MonadError e (Free m)
instance (Functor m, MonadState s m) => MonadState s (Free m)
instance (Functor m, MonadPlus m, MonadReader e m) => MonadReader e (Free m)
instance (Functor m, MonadPlus m, MonadWriter e m) => MonadWriter e (Free m)
instance Traversable f => Traversable (Free f)
instance Foldable f => Foldable (Free f)
instance MonadTrans Free
instance Functor f => Monoid (Free f a)
instance Functor f => Semigroup (Free f a)
instance Functor f => MonadPlus (Free f)
instance Functor f => Alternative (Free f)
instance Functor f => Monad (Free f)
instance Functor f => Bind (Free f)
instance Functor f => Applicative (Free f)
instance Functor f => Apply (Free f)
instance Functor f => Functor (Free f)
instance (Read (f (Free f a)), Read a) => Read (Free f a)
instance (Show (f (Free f a)), Show a) => Show (Free f a)
instance (Ord (f (Free f a)), Ord a) => Ord (Free f a)
instance (Eq (f (Free f a)), Eq a) => Eq (Free f a)

-- | Monads for free
module Control.Monad.Free

-- | Monads provide substitution (fmap) and renormalization
--   (join):
--   
--   
--   m >>= f = join (fmap f m)
--   
--   
--   A free Monad is one that does no work during the normalization
--   step beyond simply grafting the two monadic values together.
--   
--   [] is not a free Monad (in this sense) because
--   join [[a]] smashes the lists flat.
--   
--   On the other hand, consider:
--   
--   
--   data Tree a = Bin (Tree a) (Tree a) | Tip a
--   
--   
--   
--   instance Monad Tree where
--     return = Tip
--     Tip a >>= f = f a
--     Bin l r >>= f = Bin (l >>= f) (r >>= f)
--   
--   
--   This Monad is the free Monad of Pair:
--   
--   
--   data Pair a = Pair a a
--   
--   
--   And we could make an instance of MonadFree for it directly:
--   
--   
--   instance MonadFree Pair Tree where
--      wrap (Pair l r) = Bin l r
--   
--   
--   Or we could choose to program with Free Pair instead
--   of Tree and thereby avoid having to define our own
--   Monad instance.
--   
--   Moreover, Control.Monad.Free.Church provides a MonadFree
--   instance that can improve the asymptotic complexity of code
--   that constructs free monads by effectively reassociating the use of
--   (>>=). You may also want to take a look at the
--   kan-extensions package
--   (http://hackage.haskell.org/package/kan-extensions).
--   
--   See Free for a more formal definition of the free Monad
--   for a Functor.
class Monad m => MonadFree f m | m -> f where wrap = join . lift . wrap . fmap return
wrap :: MonadFree f m => f (m a) -> m a

-- | 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.
--   
--   Being 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.
--   
data Free f a
Pure :: a -> Free f a
Free :: (f (Free f a)) -> Free f a

-- | retract is the left inverse of lift and liftF
--   
--   
--   retract . lift = id
--   retract . liftF = id
--   
retract :: Monad f => Free f a -> f a

-- | A version of lift that can be used with just a Functor for f.
liftF :: (Functor f, MonadFree f m) => f a -> m a

-- | Tear down a Free Monad using iteration.
iter :: Functor f => (f a -> a) -> Free f a -> a

-- | Like iter for monadic values.
iterM :: (Monad m, Functor f) => (f (m a) -> m a) -> Free f a -> m a

-- | Lift a natural transformation from f to g into a
--   natural transformation from FreeT f to
--   FreeT g.
hoistFree :: Functor g => (forall a. f a -> g a) -> Free f b -> Free g b

-- | This is Prism' (Free f a) a in disguise
--   
--   
--   >>> preview _Pure (Pure 3)
--   Just 3
--   
--   
--   
--   >>> review _Pure 3 :: Free Maybe Int
--   Pure 3
--   
_Pure :: (Choice p, Applicative m) => p a (m a) -> p (Free f a) (m (Free f a))

-- | This 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))
--   
_Free :: (Choice p, Applicative m) => p (f (Free f a)) (m (f (Free f a))) -> p (Free f a) (m (Free f a))
instance (Typeable1 f, Typeable a, Data a, Data (f (Free f a))) => Data (Free f a)
instance Typeable1 f => Typeable1 (Free f)
instance Functor f => MonadFree f (Free f)
instance (Functor m, MonadCont m) => MonadCont (Free m)
instance (Functor m, MonadError e m) => MonadError e (Free m)
instance (Functor m, MonadState s m) => MonadState s (Free m)
instance (Functor m, MonadReader e m) => MonadReader e (Free m)
instance (Functor m, MonadWriter e m) => MonadWriter e (Free m)
instance Traversable1 f => Traversable1 (Free f)
instance Traversable f => Traversable (Free f)
instance Foldable1 f => Foldable1 (Free f)
instance Foldable f => Foldable (Free f)
instance MonadTrans Free
instance (Functor v, MonadPlus v) => MonadPlus (Free v)
instance Alternative v => Alternative (Free v)
instance Functor f => MonadFix (Free f)
instance Functor f => Monad (Free f)
instance Functor f => Bind (Free f)
instance Functor f => Applicative (Free f)
instance Functor f => Apply (Free f)
instance Functor f => Functor (Free f)
instance (Read (f (Free f a)), Read a) => Read (Free f a)
instance (Show (f (Free f a)), Show a) => Show (Free f a)
instance (Ord (f (Free f a)), Ord a) => Ord (Free f a)
instance (Eq (f (Free f a)), Eq a) => Eq (Free f a)

-- | "Free Monads for Less"
--   
--   This is based on the "Free Monads for Less" series of articles:
--   
--   http://comonad.com/reader/2011/free-monads-for-less/
--   http://comonad.com/reader/2011/free-monads-for-less-2/
module Control.Monad.Free.Church

-- | The Church-encoded free monad for a functor f.
--   
--   It is asymptotically more efficient to use (>>=)
--   for F than it is to (>>=) with Free.
--   
--   http://comonad.com/reader/2011/free-monads-for-less-2/
newtype F f a
F :: (forall r. (a -> r) -> (f r -> r) -> r) -> F f a
runF :: F f a -> forall r. (a -> r) -> (f r -> r) -> r

-- | Improve the asymptotic performance of code that builds a free monad
--   with only binds and returns by using F behind the scenes.
--   
--   This is based on the "Free Monads for Less" series of articles by
--   Edward Kmett:
--   
--   http://comonad.com/reader/2011/free-monads-for-less/
--   http://comonad.com/reader/2011/free-monads-for-less-2/
--   
--   and "Asymptotic Improvement of Computations over Free Monads" by Janis
--   Voightländer:
--   
--   http://www.iai.uni-bonn.de/~jv/mpc08.pdf
improve :: Functor f => (forall m. MonadFree f m => m a) -> Free f a

-- | Convert to another free monad representation.
fromF :: MonadFree f m => F f a -> m a

-- | Like iter for monadic values.
iterM :: (Monad m, Functor f) => (f (m a) -> m a) -> F f a -> m a

-- | Generate a Church-encoded free monad from a Free monad.
toF :: Functor f => Free f a -> F f a

-- | retract is the left inverse of lift and liftF
--   
--   
--   retract . lift = id
--   retract . liftF = id
--   
retract :: Monad m => F m a -> m a

-- | Monads provide substitution (fmap) and renormalization
--   (join):
--   
--   
--   m >>= f = join (fmap f m)
--   
--   
--   A free Monad is one that does no work during the normalization
--   step beyond simply grafting the two monadic values together.
--   
--   [] is not a free Monad (in this sense) because
--   join [[a]] smashes the lists flat.
--   
--   On the other hand, consider:
--   
--   
--   data Tree a = Bin (Tree a) (Tree a) | Tip a
--   
--   
--   
--   instance Monad Tree where
--     return = Tip
--     Tip a >>= f = f a
--     Bin l r >>= f = Bin (l >>= f) (r >>= f)
--   
--   
--   This Monad is the free Monad of Pair:
--   
--   
--   data Pair a = Pair a a
--   
--   
--   And we could make an instance of MonadFree for it directly:
--   
--   
--   instance MonadFree Pair Tree where
--      wrap (Pair l r) = Bin l r
--   
--   
--   Or we could choose to program with Free Pair instead
--   of Tree and thereby avoid having to define our own
--   Monad instance.
--   
--   Moreover, Control.Monad.Free.Church provides a MonadFree
--   instance that can improve the asymptotic complexity of code
--   that constructs free monads by effectively reassociating the use of
--   (>>=). You may also want to take a look at the
--   kan-extensions package
--   (http://hackage.haskell.org/package/kan-extensions).
--   
--   See Free for a more formal definition of the free Monad
--   for a Functor.
class Monad m => MonadFree f m | m -> f where wrap = join . lift . wrap . fmap return
wrap :: MonadFree f m => f (m a) -> m a

-- | A version of lift that can be used with just a Functor for f.
liftF :: (Functor f, MonadFree f m) => f a -> m a
instance MonadCont m => MonadCont (F m)
instance MonadWriter w m => MonadWriter w (F m)
instance MonadReader e m => MonadReader e (F m)
instance MonadState s m => MonadState s (F m)
instance Functor f => MonadFree f (F f)
instance MonadTrans F
instance MonadPlus f => MonadPlus (F f)
instance Monad (F f)
instance Bind (F f)
instance Alternative f => Alternative (F f)
instance Applicative (F f)
instance Apply (F f)
instance Functor (F f)

module Control.Comonad.Cofree.Class

-- | Allows you to peel a layer off a cofree comonad.
class (Functor f, Comonad w) => ComonadCofree f w | w -> f
unwrap :: ComonadCofree f w => w a -> f (w a)
instance (ComonadCofree f w, Semigroup m, Monoid m) => ComonadCofree f (TracedT m w)
instance ComonadCofree f w => ComonadCofree f (StoreT s w)
instance ComonadCofree f w => ComonadCofree f (EnvT e w)
instance ComonadCofree f w => ComonadCofree f (IdentityT w)
instance ComonadCofree (Const b) ((,) b)
instance ComonadCofree Maybe NonEmpty

-- | The cofree comonad transformer
module Control.Comonad.Trans.Cofree

-- | This is a cofree comonad of some functor f, with a comonad
--   w threaded through it at each level.
newtype CofreeT f w a
CofreeT :: w (CofreeF f a (CofreeT f w a)) -> CofreeT f w a
runCofreeT :: CofreeT f w a -> w (CofreeF f a (CofreeT f w a))

-- | This is the base functor of the cofree comonad transformer.
data CofreeF f a b
(:<) :: a -> f b -> CofreeF f a b

-- | Allows you to peel a layer off a cofree comonad.
class (Functor f, Comonad w) => ComonadCofree f w | w -> f
unwrap :: ComonadCofree f w => w a -> f (w a)

-- | Extract the head of the base functor
headF :: CofreeF f a b -> a

-- | Extract the tails of the base functor
tailF :: CofreeF f a b -> f b

-- | Unfold a CofreeT comonad transformer from a coalgebra and an
--   initial comonad.
coiterT :: (Functor f, Comonad w) => (w a -> f (w a)) -> w a -> CofreeT f w a
instance (Eq a, Eq (f b)) => Eq (CofreeF f a b)
instance (Ord a, Ord (f b)) => Ord (CofreeF f a b)
instance (Show a, Show (f b)) => Show (CofreeF f a b)
instance (Read a, Read (f b)) => Read (CofreeF f a b)
instance (Typeable1 f, Typeable1 w, Typeable a, Data (w (CofreeF f a (CofreeT f w a))), Data a) => Data (CofreeT f w a)
instance (Typeable1 f, Typeable a, Typeable b, Data a, Data (f b), Data b) => Data (CofreeF f a b)
instance (Typeable1 f, Typeable1 w) => Typeable1 (CofreeT f w)
instance Typeable1 f => Typeable2 (CofreeF f)
instance Ord (w (CofreeF f a (CofreeT f w a))) => Ord (CofreeT f w a)
instance Eq (w (CofreeF f a (CofreeT f w a))) => Eq (CofreeT f w a)
instance Read (w (CofreeF f a (CofreeT f w a))) => Read (CofreeT f w a)
instance Show (w (CofreeF f a (CofreeT f w a))) => Show (CofreeT f w a)
instance (Functor f, Comonad w) => ComonadCofree f (CofreeT f w)
instance Functor f => ComonadTrans (CofreeT f)
instance (Traversable f, Traversable w) => Traversable (CofreeT f w)
instance (Foldable f, Foldable w) => Foldable (CofreeT f w)
instance (Functor f, Comonad w) => Comonad (CofreeT f w)
instance (Functor f, Functor w) => Functor (CofreeT f w)
instance Traversable f => Bitraversable (CofreeF f)
instance Foldable f => Bifoldable (CofreeF f)
instance Functor f => Bifunctor (CofreeF f)
instance Traversable f => Traversable (CofreeF f a)
instance Foldable f => Foldable (CofreeF f a)
instance Functor f => Functor (CofreeF f a)

-- | The iterative comonad generated by a comonad
module Control.Comonad.Trans.Coiter

-- | This is the (co?)iterative comonad generated by a comonad
newtype CoiterT w a
CoiterT :: w (a, CoiterT w a) -> CoiterT w a
runCoiterT :: CoiterT w a -> w (a, CoiterT w a)

-- | Allows you to peel a layer off a cofree comonad.
class (Functor f, Comonad w) => ComonadCofree f w | w -> f
unwrap :: ComonadCofree f w => w a -> f (w a)

-- | Unfold a CoiterT comonad transformer from a cokleisli arrow
--   and an initial comonadic seed.
coiterT :: Comonad w => (w a -> a) -> w a -> CoiterT w a
instance (Typeable1 w, Typeable a, Data (w (a, CoiterT w a)), Data a) => Data (CoiterT w a)
instance Typeable1 w => Typeable1 (CoiterT w)
instance Ord (w (a, CoiterT w a)) => Ord (CoiterT w a)
instance Eq (w (a, CoiterT w a)) => Eq (CoiterT w a)
instance Read (w (a, CoiterT w a)) => Read (CoiterT w a)
instance Show (w (a, CoiterT w a)) => Show (CoiterT w a)
instance Comonad w => ComonadCofree Identity (CoiterT w)
instance ComonadTrans CoiterT
instance Traversable w => Traversable (CoiterT w)
instance Foldable w => Foldable (CoiterT w)
instance Comonad w => Comonad (CoiterT w)
instance Functor w => Functor (CoiterT w)

-- | Cofree comonads
module Control.Comonad.Cofree

-- | The Cofree Comonad of a functor f.
--   
--   Formally
--   
--   A Comonad v is a cofree Comonad for f
--   if every comonad homomorphism 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 a 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.
--   
data Cofree f a
(:<) :: a -> f (Cofree f a) -> Cofree f a

-- | Allows you to peel a layer off a cofree comonad.
class (Functor f, Comonad w) => ComonadCofree f w | w -> f
unwrap :: ComonadCofree f w => w a -> f (w a)

-- | 
--   lower . section = id
--   
section :: Comonad f => f a -> Cofree f a

-- | Use coiteration to generate a cofree comonad from a seed.
--   
--   
--   coiter f = unfold (id &&& f)
--   
coiter :: Functor f => (a -> f a) -> a -> Cofree f a

-- | Unfold a cofree comonad from a seed.
unfold :: Functor f => (b -> (a, f b)) -> b -> Cofree f a

-- | This is a lens that can be used to read or write from the target of
--   extract.
--   
--   Using (^.) from the lens package:
--   
--   
--   foo ^. _extract == extract foo
--   
--   
--   For more on lenses see the lens package on hackage
--   
--   
--   _extract :: Lens' (Cofree g a) a
--   
_extract :: Functor f => (a -> f a) -> Cofree g a -> f (Cofree g a)

-- | This is a lens that can be used to read or write to the tails of a
--   Cofree Comonad.
--   
--   Using (^.) from the lens package:
--   
--   
--   foo ^. _unwrap == unwrap foo
--   
--   
--   For more on lenses see the lens package on hackage
--   
--   
--   _unwrap :: Lens' (Cofree g a) (g (Cofree g a))
--   
_unwrap :: Functor f => (g (Cofree g a) -> f (g (Cofree g a))) -> Cofree g a -> f (Cofree g a)

-- | Construct a Lens into a Cofree f given a list
--   of lenses into the base functor.
--   
--   For more on lenses see the lens package on hackage.
--   
--   
--   telescoped :: Functor g => [Lens' (Cofree g a) (g (Cofree g a))] -> Lens' (Cofree g a) a
--   
telescoped :: (Functor f, Functor g) => [(Cofree g a -> f (Cofree g a)) -> g (Cofree g a) -> f (g (Cofree g a))] -> (a -> f a) -> Cofree g a -> f (Cofree g a)
instance ComonadTraced m w => ComonadTraced m (Cofree w)
instance ComonadStore s w => ComonadStore s (Cofree w)
instance ComonadEnv e w => ComonadEnv e (Cofree w)
instance (Typeable1 f, Data (f (Cofree f a)), Data a) => Data (Cofree f a)
instance (Typeable1 f, Typeable a) => Typeable (Cofree f a)
instance Typeable1 f => Typeable1 (Cofree f)
instance Traversable1 f => Traversable1 (Cofree f)
instance Traversable f => Traversable (Cofree f)
instance Foldable1 f => Foldable1 (Cofree f)
instance Foldable f => Foldable (Cofree f)
instance (Ord (f (Cofree f a)), Ord a) => Ord (Cofree f a)
instance (Eq (f (Cofree f a)), Eq a) => Eq (Cofree f a)
instance (Read (f (Cofree f a)), Read a) => Read (Cofree f a)
instance (Show (f (Cofree f a)), Show a) => Show (Cofree f a)
instance Applicative f => Applicative (Cofree f)
instance ComonadApply f => ComonadApply (Cofree f)
instance Apply f => Apply (Cofree f)
instance Alternative f => Monad (Cofree f)
instance ComonadTrans Cofree
instance Functor f => Comonad (Cofree f)
instance Functor f => Extend (Cofree f)
instance Functor f => Functor (Cofree f)
instance Distributive f => Distributive (Cofree f)
instance Functor f => ComonadCofree f (Cofree f)

-- | Left distributive Alternative functors for free, based on a
--   design by Stijn van Drongelen.
module Control.Alternative.Free

-- | The free Alternative for a Functor f.
data Alt f a
Pure :: a -> Alt f a
Ap :: f a -> Alt f (a -> b) -> Alt f b
Alt :: [Alt f a] -> Alt f a

-- | Given a natural transformation from f to g, this
--   gives a canonical monoidal natural transformation from Alt
--   f to g.
runAlt :: Alternative g => (forall x. f x -> g x) -> Alt f a -> g a

-- | A version of lift that can be used with just a Functor
--   for f.
liftAlt :: f a -> Alt f a

-- | Given a natural transformation from f to g this
--   gives a monoidal natural transformation from Alt f to Alt
--   g.
hoistAlt :: (forall a. f a -> g a) -> Alt f b -> Alt g b
instance Typeable1 f => Typeable1 (Alt f)
instance Monoid (Alt f a)
instance Semigroup (Alt f a)
instance Alternative (Alt f)
instance Applicative (Alt f)
instance Apply (Alt f)
instance Functor (Alt f)

-- | Applicative functors for free
module Control.Applicative.Free

-- | The free Applicative for a Functor f.
data Ap f a
Pure :: a -> Ap f a
Ap :: f a -> Ap f (a -> b) -> Ap f b

-- | Given a natural transformation from f to g, this
--   gives a canonical monoidal natural transformation from Ap
--   f to g.
runAp :: Applicative g => (forall x. f x -> g x) -> Ap f a -> g a

-- | A version of lift that can be used with just a Functor
--   for f.
liftAp :: f a -> Ap f a

-- | Given a natural transformation from f to g this
--   gives a monoidal natural transformation from Ap f to Ap
--   g.
hoistAp :: (forall a. f a -> g a) -> Ap f b -> Ap g b
retractAp :: Applicative f => Ap f a -> f a
instance Typeable1 f => Typeable1 (Ap f)
instance Applicative (Ap f)
instance Apply (Ap f)
instance Functor (Ap f)
