当我尝试编译这个时:
module Main where
import qualified Data.Vector.Unboxed.Mutable as MV
import Control.Monad.ST
myRead mv = runST $ MV.read mv 0
我收到以下错误消息:
Could not deduce (t ~ U.MVector s a)
from the context (U.Unbox a)
bound by the inferred type of myRead :: U.Unbox a => t -> a
at src/Main.hs:53:1-32
`t' is a rigid type variable bound by
the inferred type of myRead :: U.Unbox a => t -> a
at src/Main.hs:53:1
Expected type: U.MVector (PrimState (ST s)) a
Actual type: t
In the first argument of `MV.read', namely `mv'
In the second argument of `($)', namely `MV.read mv 0'
In the expression: runST $ MV.read mv 0
我可以使用 runST 读取可变向量 pure 吗?如果是这样,怎么办?我认为它需要 myRead
的类型签名,但我尝试过的所有操作都只会导致越来越多难以理解的错误消息。
编辑:突出显示一些上下文,我刚刚在下面添加了评论:这里的上下文是我有一个函数,它接受可变向量,使用可变向量作为临时暂存器进行一些计算空间,然后需要返回一个浮点值。因为我不关心可变向量的更改,所以我想知道是否有一种方法可以忽略它的“状态更改”并简单地返回其中的一个值。
最佳答案
applicative 的答案告诉您如何编译代码。但代码将无法使用:runST
的要点是命令式计算无法逃脱它,因为那里存在存在绑定(bind)的类型变量。
现在,您在某处创建的任何可变数组都将具有 fixed 的 MVector s a
类型,而您的 myRead
需要一个提供以下值的值: 任何 的向量。
似乎之前有一个问题让你想要拥有那个(不可能的)功能。
关于Haskell:runST 出现 "Could not deduce"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11801811/