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?