ekmett / kan-extensions

Kan extensions, Kan lifts, the Yoneda lemma, and (co)monads generated by a functor
Other
78 stars 33 forks source link

Instances for right/left Kan extensions for functors along themselves #32

Open Icelandjack opened 7 years ago

Icelandjack commented 7 years ago

These instances exist already as Codensity and Density but they could be included for right/left Kan extensions of functors along themselves.

instance f₁ ~ f₂ => Applicative (Ran f₁ f₂) where
  pure x = Ran (\k -> k x)
  Ran f <*> Ran g = Ran (\bfr -> f (\ab -> g (bfr . ab)))

instance f₁ ~ f₂ => Monad (Ran f₁ f₂) where
  return = pure
  m >>= k = Ran (\c -> runRan m (\a -> runRan (k a) c))
-- etc.

and

instance f₁ ~ f₂ => Comonad (Lan f₁ f₂) where
  duplicate (Lan f ws) = Lan (Lan f) ws
  extract (Lan f a) = f a
-- etc.

Annoyingly the same can be done for Yoneda / Coyoneda

instance (Identity ~ id, Applicative f) => Applicative (Ran id f) where
  pure a = Ran (\f -> pure (runIdentity (f a)))

-- ... 
instance Identity ~ id => Foldable (Codensity id) where
  foldMap f = foldMap f . lowerYoneda . ranToYoneda . codensityToRan

which overlaps and looks icky, is this a horrible idea?

Icelandjack commented 7 years ago

Similar example?

instance p ~ q => Category (Rift p q) where
  id = Rift id
  Rift f . Rift g = Rift (g . f)

Context

a ~ b => Comonad (Context a b)
a ~ b => ComonadStore a (Context a b)