The key is that a
and b
have type Int
, and the conversion to a String
with show
is only done after a
and b
are summed.
If you need further convincing, ask yourself if you have any problem with
runCont ex2 id
or
runCont ex2 (\a -> a * a)
If you need even more convincing, note that the type definition and monad instance are
data Cont r a = Cont { runCont :: (a -> r) -> r }
instance Monad (Cont r) where
return a = Cont ($a)
so you can squint a bit and convince yourself that your example is equivalent to
ex = do
a <- return 1
b <- return 10
return (a + b)
which is just
ex = return (1 + 10)