Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Last active August 2, 2023 13:09
Show Gist options
  • Save sjoerdvisscher/dbf700b5881609495f378bbbc2153b67 to your computer and use it in GitHub Desktop.
Save sjoerdvisscher/dbf700b5881609495f378bbbc2153b67 to your computer and use it in GitHub Desktop.
{-# LANGUAGE RankNTypes #-}
import Prelude hiding (id, (.))
import Control.Category
import Data.Functor.Coyoneda
import Data.Bifunctor (first)
newtype Object f g = Object { runObject :: forall a. f a -> g (Object f g, a) }
newtype Obj f g = Obj { runObj :: forall a. f a -> Coyoneda g (Obj f g, a) }
to :: Object f g -> Obj f g
to (Object n) = Obj $ fmap (first to) . liftCoyoneda . n
from :: Functor g => Obj f g -> Object f g
from (Obj n) = Object $ lowerCoyoneda . fmap (first from) . n
instance Category Obj where
id = Obj $ fmap ((,) id) . liftCoyoneda
Obj l . Obj r = Obj $ \fa -> case r fa of
Coyoneda f ga -> case l ga of
Coyoneda g ha -> Coyoneda (comp g f) ha
where
comp :: (s0 -> (Obj g h, s1)) -> (s1 -> (Obj f g, s2)) -> s0 -> (Obj f h, s2)
comp sgh sfg s0 =
let (gh, s1) = sgh s0 in
let (fg, s2) = sfg s1 in
(gh . fg, s2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment