gpt4 book ai didi

variables - GHC 拒绝 ST monad 代码无法统一类型​​变量?

转载 作者:行者123 更新时间:2023-12-04 09:42:31 24 4
gpt4 key购买 nike

我写了以下函数:

(.>=.) :: Num a => STRef s a -> a -> Bool
r .>=. x = runST $ do
v <- readSTRef r
return $ v >= x

但是当我尝试编译时出现以下错误:
Could not deduce (s ~ s1)
from the context (Num a)
bound by the type signature for
.>=. :: Num a => STRef s a -> a -> Bool
at test.hs:(27,1)-(29,16)
`s' is a rigid type variable bound by
the type signature for .>=. :: Num a => STRef s a -> a -> Bool
at test.hs:27:1
`s1' is a rigid type variable bound by
a type expected by the context: ST s1 Bool at test.hs:27:12
Expected type: STRef s1 a
Actual type: STRef s a
In the first argument of `readSTRef', namely `r'
In a stmt of a 'do' expression: v <- readSTRef r

任何人都可以帮忙吗?

最佳答案

这完全符合预期。一个 STRef仅在 runST 的一次运行中有效.你试着把一个外部的STRef进入新的 runST .那是无效的。这将允许纯代码中的任意副作用。

所以,你尝试的东西是不可能实现的。按设计!

关于variables - GHC 拒绝 ST monad 代码无法统一类型​​变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8007076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com