{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module Data.Functor.Foldable.Monadic
  ( -- * Folding
    cataM
  , preproM
  , paraM
  , zygoM
  , histoM, histoM'
  , dynaM, dynaM', dynaM''

    -- * Unfolding
  , anaM
  , postproM
  , apoM
  , cozygoM
  , futuM, futuM'
  , codynaM, codynaM', codynaM''

    -- * Refolding
  , hyloM, metaM
  , hyloM', metaM'
  , chronoM, cochronoM
  , chronoM' -- cochronoM'

    -- * Generalized Folding
  , gcataM, gcataM'

    -- * Others
  , mutuM, comutuM
  , mutuM', comutuM'
  , cascadeM, iterateM
  ) where

import           Control.Comonad              (Comonad (..))
import           Control.Comonad.Cofree       (Cofree (..))
import qualified Control.Comonad.Trans.Cofree as CF (CofreeF (..))
import           Control.Monad                ((<=<), liftM, liftM2)
import           Control.Monad.Free           (Free (..))
import qualified Control.Monad.Trans.Free     as FR (FreeF (..))
import           Data.Functor.Foldable        (Recursive (..), Corecursive (..), Base)

-- | catamorphism
cataM :: (Monad m, Traversable (Base t), Recursive t)
      => (Base t a -> m a) -- ^ algebra
      -> t -> m a
cataM :: forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> t -> m a
cataM Base t a -> m a
phi = t -> m a
h
  where h :: t -> m a
h = Base t a -> m a
phi (Base t a -> m a) -> (t -> m (Base t a)) -> t -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (t -> m a) -> Base t t -> m (Base t a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base t a -> m (Base t b)
mapM t -> m a
h (Base t t -> m (Base t a)) -> (t -> Base t t) -> t -> m (Base t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Base t t
forall t. Recursive t => t -> Base t t
project

-- | anamorphism
anaM :: (Monad m, Traversable (Base t), Corecursive t)
     => (a -> m (Base t a)) -- ^ coalgebra
     -> a -> m t
anaM :: forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> a -> m t
anaM a -> m (Base t a)
psi = a -> m t
h
  where h :: a -> m t
h = t -> m t
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (t -> m t) -> (Base t t -> t) -> Base t t -> m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base t t -> t
forall t. Corecursive t => Base t t -> t
embed (Base t t -> m t) -> (a -> m (Base t t)) -> a -> m t
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (a -> m t) -> Base t a -> m (Base t t)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base t a -> m (Base t b)
mapM a -> m t
h (Base t a -> m (Base t t))
-> (a -> m (Base t a)) -> a -> m (Base t t)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m (Base t a)
psi

-- | paramorphism
paraM :: (Monad m, Traversable (Base t), Recursive t)
      => (Base t (t, a) -> m a) -- ^ algebra
      -> t -> m a
paraM :: forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t (t, a) -> m a) -> t -> m a
paraM Base t (t, a) -> m a
phi = t -> m a
h
  where h :: t -> m a
h = Base t (t, a) -> m a
phi (Base t (t, a) -> m a) -> (t -> m (Base t (t, a))) -> t -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (t -> m (t, a)) -> Base t t -> m (Base t (t, a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base t a -> m (Base t b)
mapM ((t -> a -> (t, a)) -> m t -> m a -> m (t, a)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (,) (m t -> m a -> m (t, a)) -> (t -> m t) -> t -> m a -> m (t, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t -> m t
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (t -> m a -> m (t, a)) -> (t -> m a) -> t -> m (t, a)
forall a b. (t -> a -> b) -> (t -> a) -> t -> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t -> m a
h) (Base t t -> m (Base t (t, a)))
-> (t -> Base t t) -> t -> m (Base t (t, a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Base t t
forall t. Recursive t => t -> Base t t
project

-- | apomorphism
apoM :: (Monad m, Traversable (Base t), Corecursive t)
     => (a -> m (Base t (Either t a))) -- ^ coalgebra
     -> a -> m t
apoM :: forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t (Either t a))) -> a -> m t
apoM a -> m (Base t (Either t a))
psi = a -> m t
h
  where h :: a -> m t
h = t -> m t
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (t -> m t) -> (Base t t -> t) -> Base t t -> m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base t t -> t
forall t. Corecursive t => Base t t -> t
embed (Base t t -> m t) -> (a -> m (Base t t)) -> a -> m t
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Either t a -> m t) -> Base t (Either t a) -> m (Base t t)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base t a -> m (Base t b)
mapM ((t -> m t) -> (a -> m t) -> Either t a -> m t
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either t -> m t
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a -> m t
h) (Base t (Either t a) -> m (Base t t))
-> (a -> m (Base t (Either t a))) -> a -> m (Base t t)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m (Base t (Either t a))
psi

-- | histomorphism on anamorphism variant
histoM :: (Monad m, Traversable (Base t), Recursive t)
       => (Base t (Cofree (Base t) a) -> m a) -- ^ algebra
       -> t -> m a
histoM :: forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t (Cofree (Base t) a) -> m a) -> t -> m a
histoM Base t (Cofree (Base t) a) -> m a
phi = t -> m a
h
  where h :: t -> m a
h = Base t (Cofree (Base t) a) -> m a
phi (Base t (Cofree (Base t) a) -> m a)
-> (t -> m (Base t (Cofree (Base t) a))) -> t -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (t -> m (Cofree (Base t) a))
-> Base t t -> m (Base t (Cofree (Base t) a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base t a -> m (Base t b)
mapM t -> m (Cofree (Base t) a)
f (Base t t -> m (Base t (Cofree (Base t) a)))
-> (t -> Base t t) -> t -> m (Base t (Cofree (Base t) a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Base t t
forall t. Recursive t => t -> Base t t
project
        f :: t -> m (Cofree (Base t) a)
f = (t -> m (Base (Cofree (Base t) a) t)) -> t -> m (Cofree (Base t) a)
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> a -> m t
anaM ((a -> Base t t -> CofreeF (Base t) a t)
-> m a -> m (Base t t) -> m (CofreeF (Base t) a t)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a -> Base t t -> CofreeF (Base t) a t
forall (f :: * -> *) a b. a -> f b -> CofreeF f a b
(CF.:<) (m a -> m (Base t t) -> m (CofreeF (Base t) a t))
-> (t -> m a) -> t -> m (Base t t) -> m (CofreeF (Base t) a t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t -> m a
h (t -> m (Base t t) -> m (CofreeF (Base t) a t))
-> (t -> m (Base t t)) -> t -> m (CofreeF (Base t) a t)
forall a b. (t -> a -> b) -> (t -> a) -> t -> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Base t t -> m (Base t t)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Base t t -> m (Base t t)) -> (t -> Base t t) -> t -> m (Base t t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Base t t
forall t. Recursive t => t -> Base t t
project)

-- | histomorphism on catamorphism variant
histoM' :: (Monad m, Traversable (Base t), Recursive t)
        => (Base t (Cofree (Base t) a) -> m a) -- ^ algebra
        -> t -> m a
histoM' :: forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t (Cofree (Base t) a) -> m a) -> t -> m a
histoM' Base t (Cofree (Base t) a) -> m a
phi = a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (Cofree (Base t) a -> a) -> Cofree (Base t) a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cofree (Base t) a -> a
forall a. Cofree (Base t) a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract (Cofree (Base t) a -> m a)
-> (t -> m (Cofree (Base t) a)) -> t -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Base t (Cofree (Base t) a) -> m (Cofree (Base t) a))
-> t -> m (Cofree (Base t) a)
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> t -> m a
cataM Base t (Cofree (Base t) a) -> m (Cofree (Base t) a)
f
  where f :: Base t (Cofree (Base t) a) -> m (Cofree (Base t) a)
f = (a -> Base t (Cofree (Base t) a) -> Cofree (Base t) a)
-> m a -> m (Base t (Cofree (Base t) a)) -> m (Cofree (Base t) a)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a -> Base t (Cofree (Base t) a) -> Cofree (Base t) a
forall (f :: * -> *) a. a -> f (Cofree f a) -> Cofree f a
(:<) (m a -> m (Base t (Cofree (Base t) a)) -> m (Cofree (Base t) a))
-> (Base t (Cofree (Base t) a) -> m a)
-> Base t (Cofree (Base t) a)
-> m (Base t (Cofree (Base t) a))
-> m (Cofree (Base t) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Base t (Cofree (Base t) a) -> m a
phi (Base t (Cofree (Base t) a)
 -> m (Base t (Cofree (Base t) a)) -> m (Cofree (Base t) a))
-> (Base t (Cofree (Base t) a) -> m (Base t (Cofree (Base t) a)))
-> Base t (Cofree (Base t) a)
-> m (Cofree (Base t) a)
forall a b.
(Base t (Cofree (Base t) a) -> a -> b)
-> (Base t (Cofree (Base t) a) -> a)
-> Base t (Cofree (Base t) a)
-> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Base t (Cofree (Base t) a) -> m (Base t (Cofree (Base t) a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return

-- | futumorphism on catamorphism variant
futuM :: (Monad m, Traversable (Base t), Corecursive t)
      => (a -> m (Base t (Free (Base t) a))) -- ^ coalgebra
      -> a -> m t
futuM :: forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t (Free (Base t) a))) -> a -> m t
futuM a -> m (Base t (Free (Base t) a))
psi = a -> m t
h
  where h :: a -> m t
h = t -> m t
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (t -> m t) -> (Base t t -> t) -> Base t t -> m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base t t -> t
forall t. Corecursive t => Base t t -> t
embed (Base t t -> m t) -> (a -> m (Base t t)) -> a -> m t
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Free (Base t) a -> m t)
-> Base t (Free (Base t) a) -> m (Base t t)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base t a -> m (Base t b)
mapM Free (Base t) a -> m t
f (Base t (Free (Base t) a) -> m (Base t t))
-> (a -> m (Base t (Free (Base t) a))) -> a -> m (Base t t)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m (Base t (Free (Base t) a))
psi
        f :: Free (Base t) a -> m t
f = (Base (Free (Base t) a) t -> m t) -> Free (Base t) a -> m t
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> t -> m a
cataM ((Base (Free (Base t) a) t -> m t) -> Free (Base t) a -> m t)
-> (Base (Free (Base t) a) t -> m t) -> Free (Base t) a -> m t
forall a b. (a -> b) -> a -> b
$ \case
          FR.Pure  a
a -> a -> m t
h a
a
          FR.Free Base t t
fb -> t -> m t
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Base t t -> t
forall t. Corecursive t => Base t t -> t
embed Base t t
fb)

-- | futumorphism on anamorphism variant
futuM' :: (Monad m, Traversable (Base t), Corecursive t)
       => (a -> m (Base t (Free (Base t) a))) -- ^ coalgebra
       -> a -> m t
futuM' :: forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t (Free (Base t) a))) -> a -> m t
futuM' a -> m (Base t (Free (Base t) a))
psi = (Free (Base t) a -> m (Base t (Free (Base t) a)))
-> Free (Base t) a -> m t
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> a -> m t
anaM Free (Base t) a -> m (Base t (Free (Base t) a))
f (Free (Base t) a -> m t) -> (a -> Free (Base t) a) -> a -> m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Free (Base t) a
forall (f :: * -> *) a. a -> Free f a
Pure
  where f :: Free (Base t) a -> m (Base t (Free (Base t) a))
f (Pure  a
a) = a -> m (Base t (Free (Base t) a))
psi a
a
        f (Free Base t (Free (Base t) a)
fb) = Base t (Free (Base t) a) -> m (Base t (Free (Base t) a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Base t (Free (Base t) a)
fb

-- | zygomorphism
zygoM :: (Monad m, Traversable (Base t), Recursive t)
      => (Base t a -> m a)      -- ^ algebra for fst
      -> (Base t (a, b) -> m b) -- ^ algebra for snd from product
      -> t -> m b
zygoM :: forall (m :: * -> *) t a b.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> (Base t (a, b) -> m b) -> t -> m b
zygoM Base t a -> m a
f Base t (a, b) -> m b
phi = b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> ((a, b) -> b) -> (a, b) -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, b) -> b
forall a b. (a, b) -> b
snd ((a, b) -> m b) -> (t -> m (a, b)) -> t -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Base t (a, b) -> m (a, b)) -> t -> m (a, b)
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> t -> m a
cataM Base t (a, b) -> m (a, b)
g
  where g :: Base t (a, b) -> m (a, b)
g = (a -> b -> (a, b)) -> m a -> m b -> m (a, b)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (,) (m a -> m b -> m (a, b))
-> (Base t (a, b) -> m a) -> Base t (a, b) -> m b -> m (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Base t a -> m a
f (Base t a -> m a)
-> (Base t (a, b) -> m (Base t a)) -> Base t (a, b) -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Base t a -> m (Base t a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Base t a -> m (Base t a))
-> (Base t (a, b) -> Base t a) -> Base t (a, b) -> m (Base t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, b) -> a) -> Base t (a, b) -> Base t a
forall a b. (a -> b) -> Base t a -> Base t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, b) -> a
forall a b. (a, b) -> a
fst) (Base t (a, b) -> m b -> m (a, b))
-> (Base t (a, b) -> m b) -> Base t (a, b) -> m (a, b)
forall a b.
(Base t (a, b) -> a -> b)
-> (Base t (a, b) -> a) -> Base t (a, b) -> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Base t (a, b) -> m b
phi

-- | cozygomorphism
cozygoM :: (Monad m, Traversable (Base t), Corecursive t)
        => (a -> m (Base t a))            -- ^ coalgebra for fst
        -> (b -> m (Base t (Either a b))) -- ^ coalgebra for snd to coproduct
        -> b -> m t
cozygoM :: forall (m :: * -> *) t a b.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> (b -> m (Base t (Either a b))) -> b -> m t
cozygoM a -> m (Base t a)
f b -> m (Base t (Either a b))
psi = (Either a b -> m (Base t (Either a b))) -> Either a b -> m t
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> a -> m t
anaM Either a b -> m (Base t (Either a b))
g (Either a b -> m t) -> (b -> Either a b) -> b -> m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Either a b
forall a b. b -> Either a b
Right
  where g :: Either a b -> m (Base t (Either a b))
g = (a -> m (Base t (Either a b)))
-> (b -> m (Base t (Either a b)))
-> Either a b
-> m (Base t (Either a b))
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Base t (Either a b) -> m (Base t (Either a b))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Base t (Either a b) -> m (Base t (Either a b)))
-> (Base t a -> Base t (Either a b))
-> Base t a
-> m (Base t (Either a b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Either a b) -> Base t a -> Base t (Either a b)
forall a b. (a -> b) -> Base t a -> Base t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Either a b
forall a b. a -> Either a b
Left (Base t a -> m (Base t (Either a b)))
-> (a -> m (Base t a)) -> a -> m (Base t (Either a b))
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m (Base t a)
f) b -> m (Base t (Either a b))
psi

-- | hylomorphism on recursive variant
hyloM :: (Monad m, Traversable t)
      => (t b -> m b)   -- ^ algebra
      -> (a -> m (t a)) -- ^ coalgebra
      -> a -> m b
hyloM :: forall (m :: * -> *) (t :: * -> *) b a.
(Monad m, Traversable t) =>
(t b -> m b) -> (a -> m (t a)) -> a -> m b
hyloM t b -> m b
phi a -> m (t a)
psi = a -> m b
h
  where h :: a -> m b
h = t b -> m b
phi (t b -> m b) -> (a -> m (t b)) -> a -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (a -> m b) -> t a -> m (t b)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> t a -> m (t b)
mapM a -> m b
h (t a -> m (t b)) -> (a -> m (t a)) -> a -> m (t b)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m (t a)
psi

-- | hylomorphism on combination variant of ana to cata
hyloM' :: forall m t a b. (Monad m, Traversable (Base t), Recursive t, Corecursive t)
       => (Base t b -> m b)   -- ^ algebra
       -> (a -> m (Base t a)) -- ^ coalgebra
       -> a -> m b
hyloM' :: forall (m :: * -> *) t a b.
(Monad m, Traversable (Base t), Recursive t, Corecursive t) =>
(Base t b -> m b) -> (a -> m (Base t a)) -> a -> m b
hyloM' Base t b -> m b
phi a -> m (Base t a)
psi = ((Base t b -> m b) -> t -> m b
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> t -> m a
cataM Base t b -> m b
phi :: t -> m b) (t -> m b) -> (a -> m t) -> a -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ((a -> m (Base t a)) -> a -> m t
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> a -> m t
anaM a -> m (Base t a)
psi :: a -> m t)

-- | metamorphism on recursive variant
metaM :: (Monad m, Traversable (Base t), Recursive s, Corecursive t, Base s ~ Base t)
      => (Base t t -> m t)   -- ^ algebra
      -> (s -> m (Base s s)) -- ^ coalgebra
      -> s -> m t
metaM :: forall (m :: * -> *) t s.
(Monad m, Traversable (Base t), Recursive s, Corecursive t,
 Base s ~ Base t) =>
(Base t t -> m t) -> (s -> m (Base s s)) -> s -> m t
metaM Base t t -> m t
_phi s -> m (Base s s)
_psi = s -> m t
h
  where h :: s -> m t
h = t -> m t
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (t -> m t) -> (Base t t -> t) -> Base t t -> m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base t t -> t
forall t. Corecursive t => Base t t -> t
embed (Base t t -> m t) -> (s -> m (Base t t)) -> s -> m t
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (s -> m t) -> Base t s -> m (Base t t)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base t a -> m (Base t b)
mapM s -> m t
h (Base t s -> m (Base t t)) -> (s -> Base t s) -> s -> m (Base t t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Base t s
s -> Base s s
forall t. Recursive t => t -> Base t t
project

-- | metamorphism on combination variant of cata to ana
metaM' :: (Monad m, Corecursive c, Traversable (Base c), Traversable (Base t), Recursive t)
       => (Base t a -> m a)   -- ^ algebra
       -> (a -> m (Base c a)) -- ^ coalgebra
       -> t -> m c
metaM' :: forall (m :: * -> *) c t a.
(Monad m, Corecursive c, Traversable (Base c),
 Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> (a -> m (Base c a)) -> t -> m c
metaM' Base t a -> m a
phi a -> m (Base c a)
psi = (a -> m (Base c a)) -> a -> m c
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> a -> m t
anaM a -> m (Base c a)
psi (a -> m c) -> (t -> m a) -> t -> m c
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Base t a -> m a) -> t -> m a
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> t -> m a
cataM Base t a -> m a
phi

-- | chronomorphism on recursive variant over hylomorphism
chronoM' :: (Monad m, Traversable t)
         => (t (Cofree t b) -> m b) -- ^ algebra
         -> (a -> m (t (Free t a))) -- ^ coalgebra
         -> a -> m b
chronoM' :: forall (m :: * -> *) (t :: * -> *) b a.
(Monad m, Traversable t) =>
(t (Cofree t b) -> m b) -> (a -> m (t (Free t a))) -> a -> m b
chronoM' t (Cofree t b) -> m b
phi a -> m (t (Free t a))
psi = b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (Cofree t b -> b) -> Cofree t b -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cofree t b -> b
forall a. Cofree t a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract (Cofree t b -> m b) -> (a -> m (Cofree t b)) -> a -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (t (Cofree t b) -> m (Cofree t b))
-> (Free t a -> m (t (Free t a))) -> Free t a -> m (Cofree t b)
forall (m :: * -> *) (t :: * -> *) b a.
(Monad m, Traversable t) =>
(t b -> m b) -> (a -> m (t a)) -> a -> m b
hyloM t (Cofree t b) -> m (Cofree t b)
f Free t a -> m (t (Free t a))
g (Free t a -> m (Cofree t b))
-> (a -> Free t a) -> a -> m (Cofree t b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Free t a
forall (f :: * -> *) a. a -> Free f a
Pure
  where f :: t (Cofree t b) -> m (Cofree t b)
f = (b -> t (Cofree t b) -> Cofree t b)
-> m b -> m (t (Cofree t b)) -> m (Cofree t b)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 b -> t (Cofree t b) -> Cofree t b
forall (f :: * -> *) a. a -> f (Cofree f a) -> Cofree f a
(:<) (m b -> m (t (Cofree t b)) -> m (Cofree t b))
-> (t (Cofree t b) -> m b)
-> t (Cofree t b)
-> m (t (Cofree t b))
-> m (Cofree t b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t (Cofree t b) -> m b
phi (t (Cofree t b) -> m (t (Cofree t b)) -> m (Cofree t b))
-> (t (Cofree t b) -> m (t (Cofree t b)))
-> t (Cofree t b)
-> m (Cofree t b)
forall a b.
(t (Cofree t b) -> a -> b)
-> (t (Cofree t b) -> a) -> t (Cofree t b) -> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t (Cofree t b) -> m (t (Cofree t b))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
        g :: Free t a -> m (t (Free t a))
g (Pure  a
a) = a -> m (t (Free t a))
psi a
a
        g (Free t (Free t a)
fb) = t (Free t a) -> m (t (Free t a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return t (Free t a)
fb

-- | chronomorphism on combination variant of futu to hist
chronoM :: forall m t a b. (Monad m, Traversable (Base t), Recursive t, Corecursive t)
        => (Base t (Cofree (Base t) b) -> m b) -- ^ algebra
        -> (a -> m (Base t (Free (Base t) a))) -- ^ coalgebra
        -> a -> m b
chronoM :: forall (m :: * -> *) t a b.
(Monad m, Traversable (Base t), Recursive t, Corecursive t) =>
(Base t (Cofree (Base t) b) -> m b)
-> (a -> m (Base t (Free (Base t) a))) -> a -> m b
chronoM Base t (Cofree (Base t) b) -> m b
phi a -> m (Base t (Free (Base t) a))
psi = ((Base t (Cofree (Base t) b) -> m b) -> t -> m b
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t (Cofree (Base t) a) -> m a) -> t -> m a
histoM Base t (Cofree (Base t) b) -> m b
phi :: t -> m b) (t -> m b) -> (a -> m t) -> a -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ((a -> m (Base t (Free (Base t) a))) -> a -> m t
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t (Free (Base t) a))) -> a -> m t
futuM a -> m (Base t (Free (Base t) a))
psi :: a -> m t)

-- | cochronomorphism on combination variant of histo to futu
cochronoM :: (Monad m, Corecursive c, Traversable (Base c), Traversable (Base t), Recursive t)
          => (Base t (Cofree (Base t) a) -> m a) -- ^ algebra
          -> (a -> m (Base c (Free (Base c) a))) -- ^ coalgebra
          -> t -> m c
cochronoM :: forall (m :: * -> *) c t a.
(Monad m, Corecursive c, Traversable (Base c),
 Traversable (Base t), Recursive t) =>
(Base t (Cofree (Base t) a) -> m a)
-> (a -> m (Base c (Free (Base c) a))) -> t -> m c
cochronoM Base t (Cofree (Base t) a) -> m a
phi a -> m (Base c (Free (Base c) a))
psi = (a -> m (Base c (Free (Base c) a))) -> a -> m c
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t (Free (Base t) a))) -> a -> m t
futuM a -> m (Base c (Free (Base c) a))
psi (a -> m c) -> (t -> m a) -> t -> m c
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Base t (Cofree (Base t) a) -> m a) -> t -> m a
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t (Cofree (Base t) a) -> m a) -> t -> m a
histoM Base t (Cofree (Base t) a) -> m a
phi

-- | dynamorphism on recursive variant over chronomorphism
dynaM :: (Monad m, Traversable (Base t), Recursive t, Corecursive t)
      => (Base t (Cofree (Base t) b) -> m b) -- ^ algebra
      -> (a -> m (Base t a))                 -- ^ coalgebra
      -> a -> m b
dynaM :: forall (m :: * -> *) t b a.
(Monad m, Traversable (Base t), Recursive t, Corecursive t) =>
(Base t (Cofree (Base t) b) -> m b)
-> (a -> m (Base t a)) -> a -> m b
dynaM Base t (Cofree (Base t) b) -> m b
phi a -> m (Base t a)
psi = (Base t (Cofree (Base t) b) -> m b)
-> (a -> m (Base t (Free (Base t) a))) -> a -> m b
forall (m :: * -> *) (t :: * -> *) b a.
(Monad m, Traversable t) =>
(t (Cofree t b) -> m b) -> (a -> m (t (Free t a))) -> a -> m b
chronoM' Base t (Cofree (Base t) b) -> m b
phi (Base t (Free (Base t) a) -> m (Base t (Free (Base t) a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Base t (Free (Base t) a) -> m (Base t (Free (Base t) a)))
-> (Base t a -> Base t (Free (Base t) a))
-> Base t a
-> m (Base t (Free (Base t) a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Free (Base t) a) -> Base t a -> Base t (Free (Base t) a)
forall a b. (a -> b) -> Base t a -> Base t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Free (Base t) a
forall (f :: * -> *) a. a -> Free f a
Pure (Base t a -> m (Base t (Free (Base t) a)))
-> (a -> m (Base t a)) -> a -> m (Base t (Free (Base t) a))
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m (Base t a)
psi)

-- | dynamorphism on combination variant of ana to histo
dynaM' :: forall m t a c. (Monad m, Traversable (Base t), Recursive t, Corecursive t)
       => (Base t (Cofree (Base t) c) -> m c) -- ^ algebra
       -> (a -> m (Base t a))                 -- ^ coalgebra
       -> a -> m c
dynaM' :: forall (m :: * -> *) t a c.
(Monad m, Traversable (Base t), Recursive t, Corecursive t) =>
(Base t (Cofree (Base t) c) -> m c)
-> (a -> m (Base t a)) -> a -> m c
dynaM' Base t (Cofree (Base t) c) -> m c
phi a -> m (Base t a)
psi = ((Base t (Cofree (Base t) c) -> m c) -> t -> m c
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t (Cofree (Base t) a) -> m a) -> t -> m a
histoM Base t (Cofree (Base t) c) -> m c
phi :: t -> m c) (t -> m c) -> (a -> m t) -> a -> m c
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ((a -> m (Base t a)) -> a -> m t
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> a -> m t
anaM a -> m (Base t a)
psi :: a -> m t)

-- | dynamorphism on recursive variant over hylomorphism
dynaM'' :: (Monad m, Traversable t)
        => (t (Cofree t c) -> m c) -- ^ algebra
        -> (a -> m (t a))          -- ^ coalgebra
        -> a -> m c
dynaM'' :: forall (m :: * -> *) (t :: * -> *) c a.
(Monad m, Traversable t) =>
(t (Cofree t c) -> m c) -> (a -> m (t a)) -> a -> m c
dynaM'' t (Cofree t c) -> m c
phi a -> m (t a)
psi = c -> m c
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> m c) -> (Cofree t c -> c) -> Cofree t c -> m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cofree t c -> c
forall a. Cofree t a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract (Cofree t c -> m c) -> (a -> m (Cofree t c)) -> a -> m c
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (t (Cofree t c) -> m (Cofree t c))
-> (a -> m (t a)) -> a -> m (Cofree t c)
forall (m :: * -> *) (t :: * -> *) b a.
(Monad m, Traversable t) =>
(t b -> m b) -> (a -> m (t a)) -> a -> m b
hyloM t (Cofree t c) -> m (Cofree t c)
f a -> m (t a)
psi
  where f :: t (Cofree t c) -> m (Cofree t c)
f = (c -> t (Cofree t c) -> Cofree t c)
-> m c -> m (t (Cofree t c)) -> m (Cofree t c)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 c -> t (Cofree t c) -> Cofree t c
forall (f :: * -> *) a. a -> f (Cofree f a) -> Cofree f a
(:<) (m c -> m (t (Cofree t c)) -> m (Cofree t c))
-> (t (Cofree t c) -> m c)
-> t (Cofree t c)
-> m (t (Cofree t c))
-> m (Cofree t c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t (Cofree t c) -> m c
phi (t (Cofree t c) -> m (t (Cofree t c)) -> m (Cofree t c))
-> (t (Cofree t c) -> m (t (Cofree t c)))
-> t (Cofree t c)
-> m (Cofree t c)
forall a b.
(t (Cofree t c) -> a -> b)
-> (t (Cofree t c) -> a) -> t (Cofree t c) -> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t (Cofree t c) -> m (t (Cofree t c))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return

-- | codynamorphism on recursive variant over chronomorphism
codynaM :: (Monad m, Traversable t)
        => (t b -> m b)            -- ^ algebra
        -> (a -> m (t (Free t a))) -- ^ coalgebra
        -> a -> m b
codynaM :: forall (m :: * -> *) (t :: * -> *) b a.
(Monad m, Traversable t) =>
(t b -> m b) -> (a -> m (t (Free t a))) -> a -> m b
codynaM t b -> m b
phi a -> m (t (Free t a))
psi = (t (Cofree t b) -> m b) -> (a -> m (t (Free t a))) -> a -> m b
forall (m :: * -> *) (t :: * -> *) b a.
(Monad m, Traversable t) =>
(t (Cofree t b) -> m b) -> (a -> m (t (Free t a))) -> a -> m b
chronoM' (t b -> m b
phi (t b -> m b) -> (t (Cofree t b) -> t b) -> t (Cofree t b) -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Cofree t b -> b) -> t (Cofree t b) -> t b
forall a b. (a -> b) -> t a -> t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Cofree t b -> b
forall a. Cofree t a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract) a -> m (t (Free t a))
psi

-- | codynamorphism on combination variant of histo to ana
codynaM' :: (Monad m, Corecursive c, Traversable (Base c), Traversable (Base t), Recursive t)
         => (Base t (Cofree (Base t) a) -> m a) -- ^ algebra
         -> (a -> m (Base c a))                 -- ^ coalgebra
         -> t -> m c
codynaM' :: forall (m :: * -> *) c t a.
(Monad m, Corecursive c, Traversable (Base c),
 Traversable (Base t), Recursive t) =>
(Base t (Cofree (Base t) a) -> m a)
-> (a -> m (Base c a)) -> t -> m c
codynaM' Base t (Cofree (Base t) a) -> m a
phi a -> m (Base c a)
psi = (a -> m (Base c a)) -> a -> m c
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> a -> m t
anaM a -> m (Base c a)
psi (a -> m c) -> (t -> m a) -> t -> m c
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Base t (Cofree (Base t) a) -> m a) -> t -> m a
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t (Cofree (Base t) a) -> m a) -> t -> m a
histoM Base t (Cofree (Base t) a) -> m a
phi

-- | codynamorphism on recursive variant over hylomorphism
codynaM'' :: (Monad m, Traversable t)
          => (t b -> m b)            -- ^ algebra
          -> (a -> m (t (Free t a))) -- ^ coalgebra
          -> a -> m b
codynaM'' :: forall (m :: * -> *) (t :: * -> *) b a.
(Monad m, Traversable t) =>
(t b -> m b) -> (a -> m (t (Free t a))) -> a -> m b
codynaM'' t b -> m b
phi a -> m (t (Free t a))
psi = (t b -> m b) -> (Free t a -> m (t (Free t a))) -> Free t a -> m b
forall (m :: * -> *) (t :: * -> *) b a.
(Monad m, Traversable t) =>
(t b -> m b) -> (a -> m (t a)) -> a -> m b
hyloM t b -> m b
phi Free t a -> m (t (Free t a))
g (Free t a -> m b) -> (a -> Free t a) -> a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Free t a
forall (f :: * -> *) a. a -> Free f a
Pure
  where g :: Free t a -> m (t (Free t a))
g (Pure  a
a) = a -> m (t (Free t a))
psi a
a
        g (Free t (Free t a)
fb) = t (Free t a) -> m (t (Free t a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return t (Free t a)
fb

-- | mutumorphism on mutual recursive
mutuM :: (Monad m, Traversable (Base t), Recursive t)
      => (Base t (a, b) -> m b) -- ^ algebra
      -> (Base t (a, b) -> m a) -- ^ algebra
      -> t -> m b
mutuM :: forall (m :: * -> *) t a b.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t (a, b) -> m b) -> (Base t (a, b) -> m a) -> t -> m b
mutuM Base t (a, b) -> m b
q Base t (a, b) -> m a
p = (Base t (a, b) -> m b) -> (Base t (a, b) -> m a) -> t -> m b
forall (m :: * -> *) t a b.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t (a, b) -> m b) -> (Base t (a, b) -> m a) -> t -> m b
v Base t (a, b) -> m b
q Base t (a, b) -> m a
p
  where u :: (Base a (a1, a2) -> m a1) -> (Base a (a1, a2) -> m a2) -> a -> m a1
u Base a (a1, a2) -> m a1
f Base a (a1, a2) -> m a2
g = Base a (a1, a2) -> m a1
f (Base a (a1, a2) -> m a1)
-> (a -> m (Base a (a1, a2))) -> a -> m a1
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (a -> m (a1, a2)) -> Base a a -> m (Base a (a1, a2))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base a a -> m (Base a b)
mapM ((a1 -> a2 -> (a1, a2)) -> m a1 -> m a2 -> m (a1, a2)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (,) (m a1 -> m a2 -> m (a1, a2))
-> (a -> m a1) -> a -> m a2 -> m (a1, a2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Base a (a1, a2) -> m a1) -> (Base a (a1, a2) -> m a2) -> a -> m a1
u Base a (a1, a2) -> m a1
f Base a (a1, a2) -> m a2
g (a -> m a2 -> m (a1, a2)) -> (a -> m a2) -> a -> m (a1, a2)
forall a b. (a -> a -> b) -> (a -> a) -> a -> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Base a (a1, a2) -> m a2) -> (Base a (a1, a2) -> m a1) -> a -> m a2
v Base a (a1, a2) -> m a2
g Base a (a1, a2) -> m a1
f) (Base a a -> m (Base a (a1, a2)))
-> (a -> Base a a) -> a -> m (Base a (a1, a2))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Base a a
forall t. Recursive t => t -> Base t t
project
        v :: (Base a (a1, a2) -> m a2) -> (Base a (a1, a2) -> m a1) -> a -> m a2
v Base a (a1, a2) -> m a2
g Base a (a1, a2) -> m a1
f = Base a (a1, a2) -> m a2
g (Base a (a1, a2) -> m a2)
-> (a -> m (Base a (a1, a2))) -> a -> m a2
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (a -> m (a1, a2)) -> Base a a -> m (Base a (a1, a2))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base a a -> m (Base a b)
mapM ((a1 -> a2 -> (a1, a2)) -> m a1 -> m a2 -> m (a1, a2)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (,) (m a1 -> m a2 -> m (a1, a2))
-> (a -> m a1) -> a -> m a2 -> m (a1, a2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Base a (a1, a2) -> m a1) -> (Base a (a1, a2) -> m a2) -> a -> m a1
u Base a (a1, a2) -> m a1
f Base a (a1, a2) -> m a2
g (a -> m a2 -> m (a1, a2)) -> (a -> m a2) -> a -> m (a1, a2)
forall a b. (a -> a -> b) -> (a -> a) -> a -> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Base a (a1, a2) -> m a2) -> (Base a (a1, a2) -> m a1) -> a -> m a2
v Base a (a1, a2) -> m a2
g Base a (a1, a2) -> m a1
f) (Base a a -> m (Base a (a1, a2)))
-> (a -> Base a a) -> a -> m (Base a (a1, a2))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Base a a
forall t. Recursive t => t -> Base t t
project

-- | mutumorphism on recursive variant over catamorphism
mutuM' :: (Monad m, Traversable (Base t), Recursive t)
       => (a -> b)          -- ^ project
       -> (Base t a -> m a) -- ^ algebra
       -> t -> m b
mutuM' :: forall (m :: * -> *) t a b.
(Monad m, Traversable (Base t), Recursive t) =>
(a -> b) -> (Base t a -> m a) -> t -> m b
mutuM' a -> b
f Base t a -> m a
phi = b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (a -> b) -> a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f (a -> m b) -> (t -> m a) -> t -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Base t a -> m a) -> t -> m a
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> t -> m a
cataM Base t a -> m a
phi

-- | comutumorphism on comutual recursive
comutuM :: (Monad m, Traversable (Base t), Corecursive t)
        => (b -> m (Base t (Either a b))) -- ^ coalgebra
        -> (a -> m (Base t (Either a b))) -- ^ coalgebra
        -> b -> m t
comutuM :: forall (m :: * -> *) t b a.
(Monad m, Traversable (Base t), Corecursive t) =>
(b -> m (Base t (Either a b)))
-> (a -> m (Base t (Either a b))) -> b -> m t
comutuM b -> m (Base t (Either a b))
q a -> m (Base t (Either a b))
p = (b -> m (Base t (Either a b)))
-> (a -> m (Base t (Either a b))) -> b -> m t
forall {b} {m :: * -> *} {a} {b}.
(Corecursive b, Traversable (Base b), Monad m) =>
(b -> m (Base b (Either a b)))
-> (a -> m (Base b (Either a b))) -> b -> m b
v b -> m (Base t (Either a b))
q a -> m (Base t (Either a b))
p
  where u :: (a -> m (Base b (Either a b)))
-> (b -> m (Base b (Either a b))) -> a -> m b
u a -> m (Base b (Either a b))
f b -> m (Base b (Either a b))
g = (Base b b -> b) -> m (Base b b) -> m b
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Base b b -> b
forall t. Corecursive t => Base t t -> t
embed (m (Base b b) -> m b)
-> (Base b (Either a b) -> m (Base b b))
-> Base b (Either a b)
-> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either a b -> m b) -> Base b (Either a b) -> m (Base b b)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base b a -> m (Base b b)
mapM ((a -> m b) -> (b -> m b) -> Either a b -> m b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ((a -> m (Base b (Either a b)))
-> (b -> m (Base b (Either a b))) -> a -> m b
u a -> m (Base b (Either a b))
f b -> m (Base b (Either a b))
g) ((b -> m (Base b (Either a b)))
-> (a -> m (Base b (Either a b))) -> b -> m b
v b -> m (Base b (Either a b))
g a -> m (Base b (Either a b))
f)) (Base b (Either a b) -> m b)
-> (a -> m (Base b (Either a b))) -> a -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m (Base b (Either a b))
f
        v :: (b -> m (Base b (Either a b)))
-> (a -> m (Base b (Either a b))) -> b -> m b
v b -> m (Base b (Either a b))
g a -> m (Base b (Either a b))
f = (Base b b -> b) -> m (Base b b) -> m b
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Base b b -> b
forall t. Corecursive t => Base t t -> t
embed (m (Base b b) -> m b)
-> (Base b (Either a b) -> m (Base b b))
-> Base b (Either a b)
-> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either a b -> m b) -> Base b (Either a b) -> m (Base b b)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base b a -> m (Base b b)
mapM ((a -> m b) -> (b -> m b) -> Either a b -> m b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ((a -> m (Base b (Either a b)))
-> (b -> m (Base b (Either a b))) -> a -> m b
u a -> m (Base b (Either a b))
f b -> m (Base b (Either a b))
g) ((b -> m (Base b (Either a b)))
-> (a -> m (Base b (Either a b))) -> b -> m b
v b -> m (Base b (Either a b))
g a -> m (Base b (Either a b))
f)) (Base b (Either a b) -> m b)
-> (b -> m (Base b (Either a b))) -> b -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< b -> m (Base b (Either a b))
g

-- | comutumorphism on recursive variant over anamorphism
comutuM' :: (Monad m, Traversable (Base t), Corecursive t)
         => (b -> a)            -- ^ embed
         -> (a -> m (Base t a)) -- ^ coalgebra
         -> b -> m t
comutuM' :: forall (m :: * -> *) t b a.
(Monad m, Traversable (Base t), Corecursive t) =>
(b -> a) -> (a -> m (Base t a)) -> b -> m t
comutuM' b -> a
f a -> m (Base t a)
psi = (a -> m (Base t a)) -> a -> m t
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> a -> m t
anaM a -> m (Base t a)
psi (a -> m t) -> (b -> a) -> b -> m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
f

-- | prepromorphism
preproM :: (Monad m, Traversable (Base t), Recursive t, Corecursive t)
        => (Base t t -> m (Base t t)) -- ^ monadic natural transformation
        -> (Base t a -> m a)          -- ^ algebra
        -> t -> m a
preproM :: forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t, Corecursive t) =>
(Base t t -> m (Base t t)) -> (Base t a -> m a) -> t -> m a
preproM Base t t -> m (Base t t)
h Base t a -> m a
phi = t -> m a
u
  where u :: t -> m a
u = Base t a -> m a
phi (Base t a -> m a) -> (t -> m (Base t a)) -> t -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (t -> m a) -> Base t t -> m (Base t a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base t a -> m (Base t b)
mapM t -> m a
f (Base t t -> m (Base t a)) -> (t -> Base t t) -> t -> m (Base t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Base t t
forall t. Recursive t => t -> Base t t
project
        f :: t -> m a
f = t -> m a
u (t -> m a) -> (t -> m t) -> t -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Base t t -> m t) -> t -> m t
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> t -> m a
cataM (t -> m t
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (t -> m t) -> (Base t t -> t) -> Base t t -> m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base t t -> t
forall t. Corecursive t => Base t t -> t
embed (Base t t -> m t) -> (Base t t -> m (Base t t)) -> Base t t -> m t
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Base t t -> m (Base t t)
h)

-- | postpromorphism
postproM :: (Monad m, Traversable (Base t), Recursive t, Corecursive t)
         => (Base t t -> m (Base t t)) -- ^ monadic natural transformation
         -> (a -> m (Base t a))        -- ^ coalgebra
         -> a -> m t
postproM :: forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t, Corecursive t) =>
(Base t t -> m (Base t t)) -> (a -> m (Base t a)) -> a -> m t
postproM Base t t -> m (Base t t)
h a -> m (Base t a)
psi = a -> m t
u
  where u :: a -> m t
u = t -> m t
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (t -> m t) -> (Base t t -> t) -> Base t t -> m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base t t -> t
forall t. Corecursive t => Base t t -> t
embed (Base t t -> m t) -> (a -> m (Base t t)) -> a -> m t
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (a -> m t) -> Base t a -> m (Base t t)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base t a -> m (Base t b)
mapM a -> m t
f (Base t a -> m (Base t t))
-> (a -> m (Base t a)) -> a -> m (Base t t)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m (Base t a)
psi
        f :: a -> m t
f = (t -> m (Base t t)) -> t -> m t
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Corecursive t) =>
(a -> m (Base t a)) -> a -> m t
anaM (Base t t -> m (Base t t)
h (Base t t -> m (Base t t)) -> (t -> Base t t) -> t -> m (Base t t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Base t t
forall t. Recursive t => t -> Base t t
project) (t -> m t) -> (a -> m t) -> a -> m t
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a -> m t
u

-- | cascade (a.k.a supermap)
cascadeM :: (Monad m, Corecursive (f a), Traversable (Base (f a)), Traversable f, Recursive (f a))
         => (a -> m a) -- ^ pre-operator
         -> f a -> m (f a)
cascadeM :: forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Corecursive (f a), Traversable (Base (f a)),
 Traversable f, Recursive (f a)) =>
(a -> m a) -> f a -> m (f a)
cascadeM a -> m a
f = f a -> m (f a)
u
  where u :: f a -> m (f a)
u = f a -> m (f a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (f a -> m (f a))
-> (Base (f a) (f a) -> f a) -> Base (f a) (f a) -> m (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base (f a) (f a) -> f a
forall t. Corecursive t => Base t t -> t
embed (Base (f a) (f a) -> m (f a))
-> (f a -> m (Base (f a) (f a))) -> f a -> m (f a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (f a -> m (f a)) -> Base (f a) (f a) -> m (Base (f a) (f a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base (f a) a -> m (Base (f a) b)
mapM f a -> m (f a)
u (Base (f a) (f a) -> m (Base (f a) (f a)))
-> (f a -> m (Base (f a) (f a))) -> f a -> m (Base (f a) (f a))
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (f a -> m (f a)) -> Base (f a) (f a) -> m (Base (f a) (f a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base (f a) a -> m (Base (f a) b)
mapM ((a -> m a) -> f a -> m (f a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> f a -> m (f b)
mapM a -> m a
f) (Base (f a) (f a) -> m (Base (f a) (f a)))
-> (f a -> Base (f a) (f a)) -> f a -> m (Base (f a) (f a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> Base (f a) (f a)
forall t. Recursive t => t -> Base t t
project

-- | iterate
iterateM :: (Monad m, Corecursive (f a), Traversable (Base (f a)), Traversable f, Recursive (f a))
         => (a -> m a) -- ^ post-operator
         -> f a -> m (f a)
iterateM :: forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Corecursive (f a), Traversable (Base (f a)),
 Traversable f, Recursive (f a)) =>
(a -> m a) -> f a -> m (f a)
iterateM a -> m a
f = f a -> m (f a)
u
  where u :: f a -> m (f a)
u = f a -> m (f a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (f a -> m (f a))
-> (Base (f a) (f a) -> f a) -> Base (f a) (f a) -> m (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base (f a) (f a) -> f a
forall t. Corecursive t => Base t t -> t
embed (Base (f a) (f a) -> m (f a))
-> (f a -> m (Base (f a) (f a))) -> f a -> m (f a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (f a -> m (f a)) -> Base (f a) (f a) -> m (Base (f a) (f a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base (f a) a -> m (Base (f a) b)
mapM ((a -> m a) -> f a -> m (f a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> f a -> m (f b)
mapM a -> m a
f) (Base (f a) (f a) -> m (Base (f a) (f a)))
-> (f a -> m (Base (f a) (f a))) -> f a -> m (Base (f a) (f a))
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (f a -> m (f a)) -> Base (f a) (f a) -> m (Base (f a) (f a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base (f a) a -> m (Base (f a) b)
mapM f a -> m (f a)
u (Base (f a) (f a) -> m (Base (f a) (f a)))
-> (f a -> Base (f a) (f a)) -> f a -> m (Base (f a) (f a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> Base (f a) (f a)
forall t. Recursive t => t -> Base t t
project


-- | generalized catamorphism
gcataM :: (Monad m, Comonad w, Traversable w, Traversable (Base t), Recursive t, b ~ w a)
       => (Base t (w b) -> m (w (Base t b))) -- ^ Distributive (Base t) w b
       -> (Base t (w a) -> m a)              -- ^ algebra
       -> t -> m a
gcataM :: forall (m :: * -> *) (w :: * -> *) t b a.
(Monad m, Comonad w, Traversable w, Traversable (Base t),
 Recursive t, b ~ w a) =>
(Base t (w b) -> m (w (Base t b)))
-> (Base t (w a) -> m a) -> t -> m a
gcataM Base t (w b) -> m (w (Base t b))
k Base t (w a) -> m a
g = (w a -> a) -> m (w a) -> m a
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM w a -> a
forall a. w a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract (m (w a) -> m a) -> (t -> m (w a)) -> t -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Base t (w a) -> m (w a)) -> t -> m (w a)
forall (m :: * -> *) t a.
(Monad m, Traversable (Base t), Recursive t) =>
(Base t a -> m a) -> t -> m a
cataM Base t (w a) -> m (w a)
phi
  where phi :: Base t (w a) -> m (w a)
phi = (Base t (w a) -> m a) -> w (Base t (w a)) -> m (w a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> w a -> m (w b)
mapM Base t (w a) -> m a
g (w (Base t (w a)) -> m (w a))
-> (Base t (w a) -> m (w (Base t (w a))))
-> Base t (w a)
-> m (w a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Base t (w b) -> m (w (Base t b))
Base t (w (w a)) -> m (w (Base t (w a)))
k (Base t (w (w a)) -> m (w (Base t (w a))))
-> (Base t (w a) -> m (Base t (w (w a))))
-> Base t (w a)
-> m (w (Base t (w a)))
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Base t (w (w a)) -> m (Base t (w (w a)))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Base t (w (w a)) -> m (Base t (w (w a))))
-> (Base t (w a) -> Base t (w (w a)))
-> Base t (w a)
-> m (Base t (w (w a)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (w a -> w (w a)) -> Base t (w a) -> Base t (w (w a))
forall a b. (a -> b) -> Base t a -> Base t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap w a -> w (w a)
forall a. w a -> w (w a)
forall (w :: * -> *) a. Comonad w => w a -> w (w a)
duplicate

-- | generalized catamorphism variant
gcataM' :: (Monad m, Comonad w, Traversable w, Traversable (Base t), Recursive t, b ~ w a)
        => (Base t (w b) -> m (w (Base t b))) -- ^ Distributive (Base t) w b
        -> (Base t (w a) -> m a)              -- ^ algebra
        -> t -> m a
gcataM' :: forall (m :: * -> *) (w :: * -> *) t b a.
(Monad m, Comonad w, Traversable w, Traversable (Base t),
 Recursive t, b ~ w a) =>
(Base t (w b) -> m (w (Base t b)))
-> (Base t (w a) -> m a) -> t -> m a
gcataM' Base t (w b) -> m (w (Base t b))
k Base t (w a) -> m a
g = Base t (w a) -> m a
g (Base t (w a) -> m a) -> (t -> m (Base t (w a))) -> t -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Base t (w a) -> m (Base t (w a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Base t (w a) -> m (Base t (w a)))
-> (w (Base t (w a)) -> Base t (w a))
-> w (Base t (w a))
-> m (Base t (w a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w (Base t (w a)) -> Base t (w a)
forall a. w a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract (w (Base t (w a)) -> m (Base t (w a)))
-> (t -> m (w (Base t (w a)))) -> t -> m (Base t (w a))
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< t -> m (w (Base t (w a)))
c
  where c :: t -> m (w (Base t (w a)))
c = Base t (w b) -> m (w (Base t b))
Base t (w (w a)) -> m (w (Base t (w a)))
k (Base t (w (w a)) -> m (w (Base t (w a))))
-> (t -> m (Base t (w (w a)))) -> t -> m (w (Base t (w a)))
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (t -> m (w (w a))) -> Base t t -> m (Base t (w (w a)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Base t a -> m (Base t b)
mapM t -> m (w (w a))
u (Base t t -> m (Base t (w (w a))))
-> (t -> Base t t) -> t -> m (Base t (w (w a)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Base t t
forall t. Recursive t => t -> Base t t
project
        u :: t -> m (w (w a))
u = w (w a) -> m (w (w a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (w (w a) -> m (w (w a))) -> (w a -> w (w a)) -> w a -> m (w (w a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w a -> w (w a)
forall a. w a -> w (w a)
forall (w :: * -> *) a. Comonad w => w a -> w (w a)
duplicate (w a -> m (w (w a))) -> (t -> m (w a)) -> t -> m (w (w a))
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Base t (w a) -> m a) -> w (Base t (w a)) -> m (w a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> w a -> m (w b)
mapM Base t (w a) -> m a
g (w (Base t (w a)) -> m (w a))
-> (t -> m (w (Base t (w a)))) -> t -> m (w a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< t -> m (w (Base t (w a)))
c