I'm playing around with CPS and Control.Monad.Cont
and wonder what we gain by noticing the monadic structure. For code like this:
sumOfSquares'cps :: Cont r Int -> Cont r Int -> Cont r Int
sumOfSquares'cps x y = x >>= \x' ->
y >>= \y' ->
return (x'*x' + y'*y')
Can easily be rewritten as
type Cont' r a = (a -> r) -> r
sos'cps :: Cont' r Int -> Cont' r Int -> Cont' r Int
sos'cps x y = \k -> x $ \x' ->
y $ \y' ->
k (x'*x' + y'*y')
Don't get me wrong, but I can't see the sensation here apart from being able to use do
notation and a newtype
. I don't think that callCC
is dependent on the monad instance either.
I'm lacking imagination to come up with an example. What do we actually get for declaring Cont r
a monad?
callCC
is dependent on the monad instance either". Strictly speaking, nothing ever is. For example, in theMaybe
monadreturn = Just
and(=<<) = maybe Nothing
. TheMonad
class abstracts over preexisting functionality to make the things Michael Snoyman mentions in his answer possible. – BayernCont
, because there is no real difference in syntax, I guess. – SyndactylCont
, you might want to check out The Mother of all Monads. – Shaker