Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Created November 28, 2019 15:00
Show Gist options
  • Save sjoerdvisscher/12504aced97f653de6e37d66a6d0c6c9 to your computer and use it in GitHub Desktop.
Save sjoerdvisscher/12504aced97f653de6e37d66a6d0c6c9 to your computer and use it in GitHub Desktop.
Cofree traversable functor
{-# LANGUAGE GADTs #-}
import Data.Functor.HCofree
import Data.Vec.Lazy
import Data.Fin
import Data.Type.Nat
data Cotra f a where
Cotra :: SNat n -> f (Fin n) -> Vec n a -> Cotra f a
to :: Functor f => Cotra f a -> HCofree Traversable f a
to (Cotra _ fn v) = HCofree (\vb -> fmap (vb !) fn) v
cotraUnit :: Traversable f => f a -> Cotra f a
cotraUnit = cotraUnit -- see paper
from :: HCofree Traversable f a -> Cotra f a
from hf = case cotraUnit hf of
Cotra n hfn v -> Cotra n (counit hfn) v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment