Skip to content

Instantly share code, notes, and snippets.

@lemastero
Forked from sjoerdvisscher/Cotra.hs
Created November 28, 2019 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemastero/955c02235bd9c166bc60703b0f4396d8 to your computer and use it in GitHub Desktop.
Save lemastero/955c02235bd9c166bc60703b0f4396d8 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