-- 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 <a>Monad</a> <tt>n</tt> is a free <a>Monad</a> for <tt>f</tt> if
--   every <a>Monad</a> homomorphism from <tt>n</tt> to another monad
--   <tt>m</tt> is equivalent to a natural transformation from <tt>f</tt>
--   to <tt>m</tt>.
--   
--   Cofree comonads provide convenient ways to talk about branching
--   streams and rose-trees, and can be used to annotate syntax trees.
--   
--   A <a>Comonad</a> <tt>v</tt> is a cofree <a>Comonad</a> for <tt>f</tt>
--   if every <a>Comonad</a> homomorphism another comonad <tt>w</tt> to
--   <tt>v</tt> is equivalent to a natural transformation from <tt>w</tt>
--   to <tt>f</tt>.
@package free
@version 4.2


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

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

-- | Lift a natural transformation from <tt>f</tt> to <tt>g</tt> into a
--   monad homomorphism from <tt><a>FreeT</a> f m</tt> to <tt><a>FreeT</a>
--   g n</tt>
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 <a>Capretta's Iterative Monad Transformer</a>
--   
--   Unlike <tt>Free</tt>, this is a true monad transformer.
module Control.Monad.Trans.Iter

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

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

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

-- | Lift a monad homomorphism from <tt>m</tt> to <tt>n</tt> into a Monad
--   homomorphism from <tt><a>IterT</a> m</tt> to <tt><a>IterT</a> n</tt>.
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 (<a>fmap</a>) and renormalization
--   (<a>join</a>):
--   
--   <pre>
--   m <a>&gt;&gt;=</a> f = <a>join</a> (<a>fmap</a> f m)
--   </pre>
--   
--   A free <a>Monad</a> is one that does no work during the normalization
--   step beyond simply grafting the two monadic values together.
--   
--   <tt>[]</tt> is not a free <a>Monad</a> (in this sense) because
--   <tt><a>join</a> [[a]]</tt> smashes the lists flat.
--   
--   On the other hand, consider:
--   
--   <pre>
--   data Tree a = Bin (Tree a) (Tree a) | Tip a
--   </pre>
--   
--   <pre>
--   instance <a>Monad</a> Tree where
--     <a>return</a> = Tip
--     Tip a <a>&gt;&gt;=</a> f = f a
--     Bin l r <a>&gt;&gt;=</a> f = Bin (l <a>&gt;&gt;=</a> f) (r <a>&gt;&gt;=</a> f)
--   </pre>
--   
--   This <a>Monad</a> is the free <a>Monad</a> of Pair:
--   
--   <pre>
--   data Pair a = Pair a a
--   </pre>
--   
--   And we could make an instance of <a>MonadFree</a> for it directly:
--   
--   <pre>
--   instance <a>MonadFree</a> Pair Tree where
--      <a>wrap</a> (Pair l r) = Bin l r
--   </pre>
--   
--   Or we could choose to program with <tt><a>Free</a> Pair</tt> instead
--   of <tt>Tree</tt> and thereby avoid having to define our own
--   <a>Monad</a> instance.
--   
--   Moreover, <a>Control.Monad.Free.Church</a> provides a <a>MonadFree</a>
--   instance that can improve the <i>asymptotic</i> complexity of code
--   that constructs free monads by effectively reassociating the use of
--   (<a>&gt;&gt;=</a>). You may also want to take a look at the
--   <tt>kan-extensions</tt> package
--   (<a>http://hackage.haskell.org/package/kan-extensions</a>).
--   
--   See <a>Free</a> for a more formal definition of the free <a>Monad</a>
--   for a <a>Functor</a>.
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 <a>Free</a> <a>MonadPlus</a> for a <a>Functor</a> <tt>f</tt>.
--   
--   <i>Formally</i>
--   
--   A <a>MonadPlus</a> <tt>n</tt> is a free <a>MonadPlus</a> for
--   <tt>f</tt> if every monadplus homomorphism from <tt>n</tt> to another
--   MonadPlus <tt>m</tt> is equivalent to a natural transformation from
--   <tt>f</tt> to <tt>m</tt>.
--   
--   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

-- | <a>retract</a> is the left inverse of <a>lift</a> and <a>liftF</a>
--   
--   <pre>
--   <a>retract</a> . <a>lift</a> = <a>id</a>
--   <a>retract</a> . <a>liftF</a> = <a>id</a>
--   </pre>
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 <a>Free</a> <a>Monad</a> 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 <tt>f</tt> to <tt>g</tt> into a
--   natural transformation from <tt><tt>FreeT</tt> f</tt> to
--   <tt><tt>FreeT</tt> g</tt>.
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 (<a>fmap</a>) and renormalization
--   (<a>join</a>):
--   
--   <pre>
--   m <a>&gt;&gt;=</a> f = <a>join</a> (<a>fmap</a> f m)
--   </pre>
--   
--   A free <a>Monad</a> is one that does no work during the normalization
--   step beyond simply grafting the two monadic values together.
--   
--   <tt>[]</tt> is not a free <a>Monad</a> (in this sense) because
--   <tt><a>join</a> [[a]]</tt> smashes the lists flat.
--   
--   On the other hand, consider:
--   
--   <pre>
--   data Tree a = Bin (Tree a) (Tree a) | Tip a
--   </pre>
--   
--   <pre>
--   instance <a>Monad</a> Tree where
--     <a>return</a> = Tip
--     Tip a <a>&gt;&gt;=</a> f = f a
--     Bin l r <a>&gt;&gt;=</a> f = Bin (l <a>&gt;&gt;=</a> f) (r <a>&gt;&gt;=</a> f)
--   </pre>
--   
--   This <a>Monad</a> is the free <a>Monad</a> of Pair:
--   
--   <pre>
--   data Pair a = Pair a a
--   </pre>
--   
--   And we could make an instance of <a>MonadFree</a> for it directly:
--   
--   <pre>
--   instance <a>MonadFree</a> Pair Tree where
--      <a>wrap</a> (Pair l r) = Bin l r
--   </pre>
--   
--   Or we could choose to program with <tt><a>Free</a> Pair</tt> instead
--   of <tt>Tree</tt> and thereby avoid having to define our own
--   <a>Monad</a> instance.
--   
--   Moreover, <a>Control.Monad.Free.Church</a> provides a <a>MonadFree</a>
--   instance that can improve the <i>asymptotic</i> complexity of code
--   that constructs free monads by effectively reassociating the use of
--   (<a>&gt;&gt;=</a>). You may also want to take a look at the
--   <tt>kan-extensions</tt> package
--   (<a>http://hackage.haskell.org/package/kan-extensions</a>).
--   
--   See <a>Free</a> for a more formal definition of the free <a>Monad</a>
--   for a <a>Functor</a>.
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 <a>Free</a> <a>Monad</a> for a <a>Functor</a> <tt>f</tt>.
--   
--   <i>Formally</i>
--   
--   A <a>Monad</a> <tt>n</tt> is a free <a>Monad</a> for <tt>f</tt> if
--   every monad homomorphism from <tt>n</tt> to another monad <tt>m</tt>
--   is equivalent to a natural transformation from <tt>f</tt> to
--   <tt>m</tt>.
--   
--   <i>Why Free?</i>
--   
--   Every "free" functor is left adjoint to some "forgetful" functor.
--   
--   If we define a forgetful functor <tt>U</tt> from the category of
--   monads to the category of functors that just forgets the <a>Monad</a>,
--   leaving only the <a>Functor</a>. i.e.
--   
--   <pre>
--   U (M,<a>return</a>,<a>join</a>) = M
--   </pre>
--   
--   then <a>Free</a> is the left adjoint to <tt>U</tt>.
--   
--   Being <a>Free</a> being left adjoint to <tt>U</tt> means that there is
--   an isomorphism between
--   
--   <tt><a>Free</a> f -&gt; m</tt> in the category of monads and <tt>f
--   -&gt; U m</tt> in the category of functors.
--   
--   Morphisms in the category of monads are <a>Monad</a> homomorphisms
--   (natural transformations that respect <a>return</a> and <a>join</a>).
--   
--   Morphisms in the category of functors are <a>Functor</a> homomorphisms
--   (natural transformations).
--   
--   Given this isomorphism, every monad homomorphism from <tt><a>Free</a>
--   f</tt> to <tt>m</tt> is equivalent to a natural transformation from
--   <tt>f</tt> to <tt>m</tt>
--   
--   Showing that this isomorphism holds is left as an exercise.
--   
--   In practice, you can just view a <tt><a>Free</a> f a</tt> as many
--   layers of <tt>f</tt> wrapped around values of type <tt>a</tt>, where
--   <tt>(<a>&gt;&gt;=</a>)</tt> performs substitution and grafts new
--   layers of <tt>f</tt> in for each of the free variables.
--   
--   This can be very useful for modeling domain specific languages, trees,
--   or other constructs.
--   
--   This instance of <a>MonadFree</a> is fairly naive about the encoding.
--   For more efficient free monad implementation see
--   <a>Control.Monad.Free.Church</a>, in particular note the
--   <a>improve</a> combinator. You may also want to take a look at the
--   <tt>kan-extensions</tt> package
--   (<a>http://hackage.haskell.org/package/kan-extensions</a>).
--   
--   A number of common monads arise as free monads,
--   
--   <ul>
--   <li>Given <tt>data Empty a</tt>, <tt><a>Free</a> Empty</tt> is
--   isomorphic to the <a>Identity</a> monad.</li>
--   <li><tt><a>Free</a> <a>Maybe</a></tt> can be used to model a
--   partiality monad where each layer represents running the computation
--   for a while longer.</li>
--   </ul>
data Free f a
Pure :: a -> Free f a
Free :: (f (Free f a)) -> Free f a

-- | <a>retract</a> is the left inverse of <a>lift</a> and <a>liftF</a>
--   
--   <pre>
--   <a>retract</a> . <a>lift</a> = <a>id</a>
--   <a>retract</a> . <a>liftF</a> = <a>id</a>
--   </pre>
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 <a>Free</a> <a>Monad</a> 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 <tt>f</tt> to <tt>g</tt> into a
--   natural transformation from <tt><tt>FreeT</tt> f</tt> to
--   <tt><tt>FreeT</tt> g</tt>.
hoistFree :: Functor g => (forall a. f a -> g a) -> Free f b -> Free g b

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

-- | This is <tt>Prism' (Free f a) (f (Free f a))</tt> in disguise
--   
--   <pre>
--   &gt;&gt;&gt; preview _Free (review _Free (Just (Pure 3)))
--   Just (Just (Pure 3))
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; review _Free (Just (Pure 3))
--   Free (Just (Pure 3))
--   </pre>
_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:
--   
--   <a>http://comonad.com/reader/2011/free-monads-for-less/</a>
--   <a>http://comonad.com/reader/2011/free-monads-for-less-2/</a>
module Control.Monad.Free.Church

-- | The Church-encoded free monad for a functor <tt>f</tt>.
--   
--   It is <i>asymptotically</i> more efficient to use (<a>&gt;&gt;=</a>)
--   for <a>F</a> than it is to (<a>&gt;&gt;=</a>) with <a>Free</a>.
--   
--   <a>http://comonad.com/reader/2011/free-monads-for-less-2/</a>
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 <a>F</a> behind the scenes.
--   
--   This is based on the "Free Monads for Less" series of articles by
--   Edward Kmett:
--   
--   <a>http://comonad.com/reader/2011/free-monads-for-less/</a>
--   <a>http://comonad.com/reader/2011/free-monads-for-less-2/</a>
--   
--   and "Asymptotic Improvement of Computations over Free Monads" by Janis
--   Voightländer:
--   
--   <a>http://www.iai.uni-bonn.de/~jv/mpc08.pdf</a>
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 <a>Free</a> monad.
toF :: Functor f => Free f a -> F f a

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

-- | Monads provide substitution (<a>fmap</a>) and renormalization
--   (<a>join</a>):
--   
--   <pre>
--   m <a>&gt;&gt;=</a> f = <a>join</a> (<a>fmap</a> f m)
--   </pre>
--   
--   A free <a>Monad</a> is one that does no work during the normalization
--   step beyond simply grafting the two monadic values together.
--   
--   <tt>[]</tt> is not a free <a>Monad</a> (in this sense) because
--   <tt><a>join</a> [[a]]</tt> smashes the lists flat.
--   
--   On the other hand, consider:
--   
--   <pre>
--   data Tree a = Bin (Tree a) (Tree a) | Tip a
--   </pre>
--   
--   <pre>
--   instance <a>Monad</a> Tree where
--     <a>return</a> = Tip
--     Tip a <a>&gt;&gt;=</a> f = f a
--     Bin l r <a>&gt;&gt;=</a> f = Bin (l <a>&gt;&gt;=</a> f) (r <a>&gt;&gt;=</a> f)
--   </pre>
--   
--   This <a>Monad</a> is the free <a>Monad</a> of Pair:
--   
--   <pre>
--   data Pair a = Pair a a
--   </pre>
--   
--   And we could make an instance of <a>MonadFree</a> for it directly:
--   
--   <pre>
--   instance <a>MonadFree</a> Pair Tree where
--      <a>wrap</a> (Pair l r) = Bin l r
--   </pre>
--   
--   Or we could choose to program with <tt><a>Free</a> Pair</tt> instead
--   of <tt>Tree</tt> and thereby avoid having to define our own
--   <a>Monad</a> instance.
--   
--   Moreover, <a>Control.Monad.Free.Church</a> provides a <a>MonadFree</a>
--   instance that can improve the <i>asymptotic</i> complexity of code
--   that constructs free monads by effectively reassociating the use of
--   (<a>&gt;&gt;=</a>). You may also want to take a look at the
--   <tt>kan-extensions</tt> package
--   (<a>http://hackage.haskell.org/package/kan-extensions</a>).
--   
--   See <a>Free</a> for a more formal definition of the free <a>Monad</a>
--   for a <a>Functor</a>.
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 <tt>f</tt>, with a comonad
--   <tt>w</tt> 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 <tt>CofreeT</tt> 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 <tt>CoiterT</tt> 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 <a>Cofree</a> <a>Comonad</a> of a functor <tt>f</tt>.
--   
--   <i>Formally</i>
--   
--   A <a>Comonad</a> <tt>v</tt> is a cofree <a>Comonad</a> for <tt>f</tt>
--   if every comonad homomorphism another comonad <tt>w</tt> to <tt>v</tt>
--   is equivalent to a natural transformation from <tt>w</tt> to
--   <tt>f</tt>.
--   
--   A <tt>cofree</tt> 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
--   <a>extract</a> and <a>duplicate</a>, leaving you with only a
--   <a>Functor</a>.
--   
--   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,
--   
--   <ul>
--   <li><tt><a>Cofree</a> <a>Maybe</a></tt> forms the a comonad for a
--   non-empty list.</li>
--   <li><tt><a>Cofree</a> (<a>Const</a> b)</tt> is a product.</li>
--   <li><tt><a>Cofree</a> <tt>Identity</tt></tt> forms an infinite
--   stream.</li>
--   <li><tt><a>Cofree</a> ((-&gt;) b)'</tt> describes a Moore machine with
--   states labeled with values of type a, and transitions on edges of type
--   b.</li>
--   </ul>
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)

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

-- | Use coiteration to generate a cofree comonad from a seed.
--   
--   <pre>
--   <a>coiter</a> f = <a>unfold</a> (<a>id</a> <a>&amp;&amp;&amp;</a> f)
--   </pre>
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
--   <a>extract</a>.
--   
--   Using (^.) from the <tt>lens</tt> package:
--   
--   <pre>
--   foo ^. <a>_extract</a> == <a>extract</a> foo
--   </pre>
--   
--   For more on lenses see the <tt>lens</tt> package on hackage
--   
--   <pre>
--   <a>_extract</a> :: Lens' (<a>Cofree</a> g a) a
--   </pre>
_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
--   <a>Cofree</a> <a>Comonad</a>.
--   
--   Using (^.) from the <tt>lens</tt> package:
--   
--   <pre>
--   foo ^. <a>_unwrap</a> == <a>unwrap</a> foo
--   </pre>
--   
--   For more on lenses see the <tt>lens</tt> package on hackage
--   
--   <pre>
--   <a>_unwrap</a> :: Lens' (<a>Cofree</a> g a) (g (<a>Cofree</a> g a))
--   </pre>
_unwrap :: Functor f => (g (Cofree g a) -> f (g (Cofree g a))) -> Cofree g a -> f (Cofree g a)

-- | Construct a <tt>Lens</tt> into a <tt><a>Cofree</a> f</tt> given a list
--   of lenses into the base functor.
--   
--   For more on lenses see the <tt>lens</tt> package on hackage.
--   
--   <pre>
--   telescoped :: <a>Functor</a> g =&gt; [Lens' (<a>Cofree</a> g a) (g (<a>Cofree</a> g a))] -&gt; Lens' (<a>Cofree</a> g a) a
--   </pre>
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 <a>Alternative</a> functors for free, based on a
--   design by Stijn van Drongelen.
module Control.Alternative.Free

-- | The free <a>Alternative</a> for a <a>Functor</a> <tt>f</tt>.
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 <tt>f</tt> to <tt>g</tt>, this
--   gives a canonical monoidal natural transformation from <tt><a>Alt</a>
--   f</tt> to <tt>g</tt>.
runAlt :: Alternative g => (forall x. f x -> g x) -> Alt f a -> g a

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

-- | Given a natural transformation from <tt>f</tt> to <tt>g</tt> this
--   gives a monoidal natural transformation from <tt>Alt f</tt> to <tt>Alt
--   g</tt>.
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)


-- | <a>Applicative</a> functors for free
module Control.Applicative.Free

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

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

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

-- | Given a natural transformation from <tt>f</tt> to <tt>g</tt> this
--   gives a monoidal natural transformation from <tt>Ap f</tt> to <tt>Ap
--   g</tt>.
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)
