robrix / freer-cofreer

freer monads and cofreer comonads.
BSD 3-Clause "New" or "Revised" License
21 stars 1 forks source link

Document the relationship with (Co)Yoneda #15

Open robrix opened 6 years ago

robrix commented 6 years ago

Then encodes the continuation passed to >>= in the same way as Coyoneda encodes the function passed to fmap; you can in fact arrive at a definition of Freer using Coyoneda:

data Freer f a where
  Return :: a -> Freer f a
  Then :: f b -> (b -> Freer f a) -> Freer f a

is isomorphic to

data Coyoneda f a where
  Coyoneda :: (b -> a) -> f b -> Coyoneda f a

data Freer f a where
  Return :: a -> Freer f a
  Then :: Coyoneda f (Freer f a) -> Freer f a

and indeed to

newtype Freer f a = Freer { runFreer :: Either a (Coyoneda f (Freer f a)) }

This isn’t really documented but it’s pretty significant.