【发布时间】:2012-07-06 07:56:12
【问题描述】:
由于下面的do块:
do
x <- foo
y <- bar
return x + y
脱糖为以下形式:
foo >>= (\x -> bar >>= (\y -> return x + y))
\x -> ... 和 y -> ... 实际上不是在这里延续吗?
我想知道是否有办法捕获bind 定义中的延续,但我无法正确选择类型。即:
data Pause a = Pause a | Stop
instance Monad Pause where
return x = Stop
m >>= k = Pause k -- this doesn't work of course
现在我试着混淆类型:
data Pause a = Pause a (a -> Pause ???) | Stop
------- k ------
但这也不起作用。有没有办法捕捉到这些隐含的延续?
顺便说一句,我知道Cont monad,我只是在试验和尝试一些东西。
【问题讨论】:
-
我不确定,但也许您需要更加仔细地思考“捕获延续”的含义。看看你对
>>=的定义:它抛弃了第一个参数......那么第一个参数在那里做什么?
标签: haskell types continuations coroutine continuation-passing