Skip to content

Instantly share code, notes, and snippets.

@tkersey
Forked from vvviiimmm/yonedahaskell.hs
Created October 27, 2023 22:04
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 tkersey/00822646ca5af92204526e545a24107b to your computer and use it in GitHub Desktop.
Save tkersey/00822646ca5af92204526e545a24107b to your computer and use it in GitHub Desktop.
-- Yoneda --------------------------------------------------------------
newtype Yoneda f a = Yoneda { runYoneda :: forall b. ((a -> b) -> f b) }
instance Functor (Yoneda f) where
fmap f y = Yoneda (\ab -> runYoneda y (ab . f))
toYoneda :: Functor f => f a -> Yoneda f a
toYoneda fa = Yoneda (\f -> fmap f fa)
fromYoneda :: Yoneda f a -> f a
fromYoneda y = runYoneda y id
-- Coyoneda -----------------------------------------
data CoYoneda f a = forall b . CoYoneda (b -> a) (f b)
instance Functor (CoYoneda f) where
fmap f (CoYoneda mp fb) = CoYoneda (f . mp) fb
toCoYoneda :: f a -> CoYoneda f a
toCoYoneda fa = CoYoneda id fa
fromCoYoneda :: Functor f => CoYoneda f a -> f a
fromCoYoneda (CoYoneda mp fb) = fmap mp fb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment