module Data.Foldable.Functor.Extensions
( dicata
, dendro
, micro
, cataM
) where
import Control.Arrow ((&&&))
import Control.Monad ((<=<))
import Data.Composition ((.*))
import Data.Functor.Foldable (Base, Corecursive, Recursive, cata,
elgot, embed, project)
class (Functor f, Functor g) => SubHom f g a b where
homo :: (f a -> a) -> (g b -> b) -> (g b -> b)
dendro :: (SubHom (Base t1) (Base t2) a b, Recursive t2) => (Base t1 a -> a) -> (Base t2 b -> b) -> t2 -> b
dendro = cata .* homo
dicata :: (Recursive a) => (Base a (b, a) -> b) -> (Base a (b, a) -> a) -> a -> b
dicata f g = fst . cata (f &&& g)
micro :: (Corecursive a) => (b -> Either a (Base a b)) -> b -> a
micro = elgot embed
cataM :: (Recursive t, Traversable (Base t), Monad m) => (Base t a -> m a) -> (t -> m a)
cataM phi = g where g = phi <=< (mapM g . project)