ekmett / kan-extensions

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

MonadCont instance for Codensity #3

Open fizruk opened 10 years ago

fizruk commented 10 years ago

There exist at least 2 conflicting candidatures:

-- Codensity (Const r) ~ Cont r
instance MonadCont (Codensity (Const r)) where
  callCC f = Codensity (\k -> mapConst $ runCodensity (f (\x -> Codensity (\_ -> mapConst $ k x))) k)
    where
      mapConst :: Const r a -> Const r b
      mapConst = Const . runConst

-- using callCC of m
instance MonadCont m => MonadCont (Codensity m) where
  callCC f = join . lift $ callCC (\k -> return $ f (lift . k . return))