module Control.Morphism.Cata where
import Control.Comonad
import Control.Category.Hask
import Control.Functor
import Control.Functor.Pointed
import Control.Functor.Algebra
import Control.Functor.HigherOrder
import Control.Functor.Extras
import Control.Functor.Fix
import Control.Monad.Identity
cata :: Functor f => Algebra f a -> FixF f -> a
cata f = f . fmap (cata f) . outF
g_cata :: (Functor f, Comonad w) => Dist f w -> GAlgebra f w a -> FixF f -> a
g_cata k g = extract . c where c = liftW g . k . fmap (duplicate . c) . outF
distCata :: Functor f => Dist f Identity
distCata = Identity . fmap runIdentity
bicata :: Bifunctor f Hask Hask Hask => Algebra (f b) a -> Fix f b -> a
bicata f = f . bimap id (bicata f) . outB
g_bicata :: (Bifunctor f Hask Hask Hask, Comonad w) => Dist (f b) w -> GAlgebra (f b) w a -> Fix f b -> a
g_bicata k g = extract . c where c = liftW g . k . bimap id (duplicate . c) . outB
hcata :: HFunctor f => HAlgebra f a -> FixH f :~> a
hcata f = f . hfmap (hcata f) . outH