【问题标题】:Continuation monad "interface"延续单子“接口”
【发布时间】:2011-02-09 05:15:16
【问题描述】:

状态单子“接口”

class MonadState s m where
    get :: m s
    put :: s -> m ()

(+ return and bind) 允许使用 State monad 构造任何可能的计算,而无需使用 State 构造函数。比如State $ \s -> (s+1, s-1)可以写成

 do s <- get
    put (s-1)
    return (s+1)

同样,我永远不必使用Reader 构造函数,因为我可以使用askreturn(&gt;&gt;=) 创建该计算。确切地说:Reader f == ask &gt;&gt;= return . f

对于 continuation 是否同样如此 - 是否可以使用 callCCMonadCont 中的唯一函数)编写 Cont r a 的所有实例,返回并绑定,并且从不输入类似 Cont (\c -&gt; ...) 的内容?

【问题讨论】:

    标签: haskell monads continuations state-monad


    【解决方案1】:

    我不这么认为。查看类型:

    Cont :: ((a -> r) -> r) -> Cont r a
    callCC :: ((a -> Cont r b) -> Cont r a) -> Cont r a
    

    如果您只有callCC,则在任何地方都不能使用r 作为类型——它可以是任何类型。所以我不知道你如何翻译使用它作为类型的东西,例如:

    Cont (const 42) :: Cont Int a
    

    如果我只有callCC,我无法限制r

    无论如何,这是我的预感。不是很严谨,但看起来很有说服力。

    【讨论】:

      猜你喜欢
      • 2010-10-14
      • 2014-12-07
      • 1970-01-01
      • 2014-10-21
      • 2011-05-30
      • 2014-08-10
      • 2015-09-30
      • 2017-02-24
      • 1970-01-01
      相关资源
      最近更新 更多