Skip to content

Instantly share code, notes, and snippets.

@i-am-tom
Last active January 8, 2018 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i-am-tom/d081342329d31daef49f6cda31b7fed2 to your computer and use it in GitHub Desktop.
Save i-am-tom/d081342329d31daef49f6cda31b7fed2 to your computer and use it in GitHub Desktop.
Primitive recursion schemes and combinators specialised to Nat.
-- Folding
project :: Nat -> Maybe Nat
cata :: (Maybe a -> a) -> Nat -> a
gcata :: Comonad w => Distributive w => (Maybe (w a) -> a) -> Nat -> a
para :: (Maybe (Nat, a) -> a) -> Nat -> a
gpara :: Comonad w => Distributive w => (Maybe (Nat, w a) -> a) -> Nat -> a
prepro :: (Maybe ~> Maybe) -> (Maybe a -> a) -> Nat -> a
gprepro :: Comonad w => Distributive w => (Maybe ~> Maybe) -> (Maybe (w a) -> a) -> Nat -> a
zygo :: (Maybe b -> b) -> (Maybe (b, a) -> a) -> Nat -> a
gzygo :: Comonad w => Distributive w => (Maybe b -> b) -> (Maybe (b, w a) -> a) -> Nat -> a
histo :: Maybe (Cofree Maybe a -> a) -> Nat -> a
ghisto :: Distributive f => Maybe (Cofree f a -> a) -> Nat -> a
futu :: (a -> Maybe (Free Maybe a)) -> a -> Nat
gfutu :: Traversable f => (a -> Maybe (Free f a)) -> Nat -> a
chrono :: Functor f => (f (Cofree f b) -> b) -> (a -> f (Free f a)) -> a -> b
gchrono :: Distributive f => Distributive w => (f (Cofree w b) -> b) -> (a -> f (Free f a)) -> a -> b
-- Distributive laws
distCata :: Functor f => f (Identity a) -> Identity (f a)
distPara :: Maybe (Nat, a) -> (Nat, Maybe a)
distParaT :: Comonad w => Distributive w => Maybe (Nat, w a) -> (Nat, w (Maybe a))
distZygo :: Functor f => (f b -> b) -> f (b, a) -> b (f, a)
distZygoT :: Functor f => Distributive w => Comonad w => (f b -> b) -> f (b, w a) -> (b, w (f a))
distHisto :: Functor f => f (Cofree f a) -> Cofree f (f a)
distGHisto :: Functor f => Distributive h => f (Cofree h a) -> Cofree h (f a)
distFutu :: Functor f => Free f (f a) -> f (Free f a)
distGFutu :: Distributive f => Functor h => Free h (f a) -> f (Free h a)
-- Unfolding
embed :: Maybe Nat -> Nat
ana :: (a -> Maybe a ) -> a -> Nat
gana :: Traversable m => (a -> Maybe (m a)) -> a -> Nat
apo :: (a -> Maybe (Either Nat a)) -> a -> Nat
gapo :: (b -> Maybe b) -> (a -> Maybe (Either b a)) -> a -> Nat
postpro :: (Maybe ~> Maybe) -> (a -> Maybe a ) -> a -> Nat
gpostpro :: Monad m => Traversable m => (Maybe ~> Maybe) -> (a -> Maybe (m a)) -> a -> Nat
-- Distributive laws
distAna :: Functor f => Identity (f a) -> f (Identity a)
distApo :: Either Nat (Maybe a) -> Maybe (Either Nat a)
distGApo :: Functor f => (b -> f b) -> Either b ( f a) -> f (Either b a)
distGApoT :: Functor f => Traversable m => (b -> f b) -> m (Either b ( f a)) -> f (Either b (m a))
-- Zygohistomorphic prepromorphisms
zygoHistoPrepro :: (Maybe b -> b) -> (Maybe ~> Maybe) -> (Maybe (b, Cofree Maybe a) -> a) -> Nat -> a
----------
-- Refolding
hylo :: Functor f => (f b -> b) -> (a -> f a) -> a -> b
-- OR traversable + distributive f???
ghylo :: Comonad w => Functor f => Monad m => Distributive w => Traversable m => (f (w b) -> b) -> (a -> f (m a)) -> a -> b
-- Elgot
elgot :: Functor f => (f a -> a) -> (b -> Either a (f b)) -> b -> a
coelgot :: Functor f => ((a, f b) -> b) -> (a -> f a) -> a -> b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment