
[英]Pass variables to code <input type=“image” src=“imageSrc;” >
[英]GHC rejects ST monad code as unable to unify type variables?
我写了以下函数:
(.>=.) :: 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
一次运行中runST
。 并尝试把外部STRef
进入一个新的运行runST
。 那是无效的。 这将允许纯代码中的任意副作用。
所以,你尝试的是不可能实现的。 按设计!
你需要留在ST
环境中:
(.>=.) :: Ord a => STRef s a -> a -> ST s Bool
r .>=. x = do
v <- readSTRef r
return $ v >= x
(正如哈马尔指出,使用>=
你所需要的Ord
类型类,其Num
不提供。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.