haskell - ST 引用透明吗?

标签 haskell

为什么 ST 被设计为禁止使用以下代码 as shown in the wikibook ?这篇文章似乎表明,如果允许的话,ST 效果会泄漏到另一个 ST 中,但我不太明白为什么。

我似乎无法运行特定的ST (STRef s a)ST 效果是否包含且引用透明,但此类使用仍被视为无效?

直觉告诉我,IOST 之间的语义差异与存在一个 IO 和许多独立的 相关。 ST,但我不确定。

> runST (newSTRef True)

<interactive>:5:8: error:
    • Couldn't match type ‘a’ with ‘STRef s Bool’
        because type variable ‘s’ would escape its scope
      This (rigid, skolem) type variable is bound by
        a type expected by the context:
          forall s. ST s a
        at <interactive>:5:1-21
      Expected type: ST s a
        Actual type: ST s (STRef s Bool)
    • In the first argument of ‘runST’, namely ‘(newSTRef True)’
      In the expression: runST (newSTRef True)
      In an equation for ‘it’: it = runST (newSTRef True)
    • Relevant bindings include it :: a (bound at <interactive>:5:1)

> :t newSTRef
newSTRef :: a -> ST s (STRef s a)

> :t runST
runST :: (forall s. ST s a) -> a

最佳答案

更多的是对问题标题的回答,而不是问题本身,但仍然:

是的,ST 是引用透明的。

很长一段时间以来,这只是猜测和相信,直到今年我们才得到了适当的证明:

状态一元封装的逻辑关系:在存在 runST 的情况下证明上下文等价
阿明·蒂马尼、莱奥·斯特凡内斯科、莫滕·克罗格-杰斯佩森、拉斯·伯克达尔
有条件接受 POPL 2018
http://iris-project.org/ Preprint PDF

关于haskell - ST 引用透明吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47381464/

相关文章:

haskell - 在 Eq 实例实现中仅覆盖少数情况的数据

Haskell:先进先出单子(monad)

haskell - 简而言之,'type family' 与 'data family' ?

haskell - OCaml 中的对应部分

haskell - GHCI monadic 绑定(bind)是否严格?

haskell - 是否有某种方法可以使用没有下划线标识符的镜头来构造记录值?

haskell - 如何确定 Haskell 中类型的大小?

haskell - 有没有办法像 withCString 这样链接函数?

haskell - 如何不确定地将值放入状态中?

haskell - 为什么函数组合需要括号?