The state monad "interface"
class MonadState s m where
get :: m s
put :: s -> m ()
(+ return and bind) allows to construc开发者_运维知识库t any possible computation with State monad without using State
constructor. For example, State $ \s -> (s+1, s-1)
can be written as
do s <- get
put (s-1)
return (s+1)
Similarily, I never have to use Reader
constructor, because I can create that computation using ask
, return
and (>>=)
. Precisely: Reader f == ask >>= return . f
.
Is it the same true for continuations - is it possible to write all instances of Cont r a
using callCC
(the only function in MonadCont
), return and bind, and never type something like Cont (\c -> ...)
?
I don't think so. Looking at the types:
Cont :: ((a -> r) -> r) -> Cont r a
callCC :: ((a -> Cont r b) -> Cont r a) -> Cont r a
If you only have callCC
, there is no use of r
as a type anywhere - it could be of any kind. So I don't know how you could translate something that uses it as a type, eg:
Cont (const 42) :: Cont Int a
I have no way of constraining r
if I only have callCC
.
Anyway, that's my hunch. Not terribly rigorous, but it seems convincing.
精彩评论