{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
module Control.Recursion
(
Base
, Recursive (..)
, Corecursive (..)
, Fix (..)
, Mu (..)
, Nu (..)
, ListF (..)
, hylo
, prepro
, postpro
, mutu
, zygo
, para
, apo
, elgot
, coelgot
, micro
, meta
, meta'
, dicata
, mhisto
, mcata
, cataM
, anaM
, hyloM
, lambek
, colambek
) where
import Control.Arrow ((&&&))
import Control.Composition ((.*), (.**))
import Control.Monad ((<=<))
import Numeric.Natural (Natural)
type family Base t :: * -> *
class (Functor (Base t)) => Recursive t where
project :: t -> Base t t
cata :: (Base t a -> a) -> t -> a
cata f = c where c = f . fmap c . project
class (Functor (Base t)) => Corecursive t where
embed :: Base t t -> t
ana :: (a -> Base t a) -> a -> t
ana g = a where a = embed . fmap a . g
data ListF a b = Cons a b
| Nil
deriving (Functor)
newtype Fix f = Fix { unFix :: f (Fix f) }
data Nu f = forall a. Nu (a -> f a) a
newtype Mu f = Mu (forall a. (f a -> a) -> a)
type instance Base (Fix f) = f
type instance Base (Mu f) = f
type instance Base (Nu f) = f
type instance Base Natural = Maybe
type instance Base [a] = ListF a
instance Recursive Natural where
project 0 = Nothing
project n = Just (n-1)
instance Corecursive Natural where
embed Nothing = 0
embed (Just n) = n+1
instance Functor f => Recursive (Nu f) where
project (Nu f a) = Nu f <$> f a
instance Functor f => Corecursive (Nu f) where
embed = colambek
ana = Nu
instance Functor f => Recursive (Mu f) where
project = lambek
cata f (Mu g) = g f
instance Functor f => Corecursive (Mu f) where
embed m = Mu (\f -> f (fmap (cata f) m))
instance Recursive [a] where
project [] = Nil
project (x:xs) = Cons x xs
instance Corecursive [a] where
embed Nil = []
embed (Cons x xs) = x : xs
hylo :: Functor f => (f b -> b) -> (a -> f a) -> a -> b
hylo f g = h where h = f . fmap h . g
cataM :: (Recursive t, Traversable (Base t), Monad m) => (Base t a -> m a) -> t -> m a
cataM f = c where c = f <=< (traverse c . project)
anaM :: (Corecursive t, Traversable (Base t), Monad m) => (a -> m (Base t a)) -> a -> m t
anaM f = a where a = (fmap embed . traverse a) <=< f
hyloM :: (Traversable f, Monad m) => (f b -> m b) -> (a -> m (f a)) -> a -> m b
hyloM f g = h where h = f <=< traverse h <=< g
lambek :: (Recursive t, Corecursive t) => (t -> Base t t)
lambek = cata (fmap embed)
colambek :: (Recursive t, Corecursive t) => (Base t t -> t)
colambek = ana (fmap project)
prepro :: (Recursive t, Corecursive t) => (Base t t -> Base t t) -> (Base t a -> a) -> t -> a
prepro e f = c
where c = f . fmap (c . cata (embed . e)) . project
postpro :: (Recursive t, Corecursive t) => (Base t t -> Base t t) -> (a -> Base t a) -> a -> t
postpro e g = a'
where a' = embed . fmap (ana (e . project) . a') . g
mutu :: (Recursive t) => (Base t (a, a) -> a) -> (Base t (a, a) -> a) -> t -> a
mutu f g = snd . cata (f &&& g)
dicata :: (Recursive t) => (Base t (a, t) -> a) -> (Base t (a, t) -> t) -> t -> a
dicata = fst .** (cata .* (&&&))
zygo :: (Recursive t) => (Base t b -> b) -> (Base t (b, a) -> a) -> t -> a
zygo f g = snd . cata (((,) . f . fmap fst) <*> g)
para :: (Recursive t, Corecursive t) => (Base t (t, a) -> a) -> t -> a
para f = snd . cata (((,) . embed . fmap fst) <*> f)
meta :: (Corecursive t', Recursive t) => (a -> Base t' a) -> (b -> a) -> (Base t b -> b) -> t -> t'
meta f e g = ana f . e . cata g
meta' :: (Functor g) => (f a -> a) -> (forall c. g c -> f c) -> (b -> g b) -> b -> a
meta' h e k = g
where g = h . e . fmap g . k
mcata :: (forall y. ((y -> c) -> f y -> c)) -> Fix f -> c
mcata psi = mc where mc = psi mc . unFix
mhisto :: (forall y. ((y -> c) -> (y -> f y) -> f y -> c)) -> Fix f -> c
mhisto psi = mh where mh = psi mh unFix . unFix
elgot :: Functor f => (f a -> a) -> (b -> Either a (f b)) -> b -> a
elgot phi psi = h where h = either id (phi . fmap h) . psi
micro :: (Corecursive a) => (b -> Either a (Base a b)) -> b -> a
micro = elgot embed
coelgot :: Functor f => ((a, f b) -> b) -> (a -> f a) -> a -> b
coelgot phi psi = h where h = phi . ((,) <*> (fmap h . psi))
apo :: (Corecursive t) => (a -> Base t (Either t a)) -> a -> t
apo g = a where a = embed . fmap (either id a) . g