Haskell:runST 出现 "Could not deduce"错误

标签 haskell

当我尝试编译这个时:

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)的类型变量。

现在,您在某处创建的任何可变数组都将具有 fixedMVector s a 类型,而您的 myRead 需要一个提供以下值的值: 任何 的向量。

似乎之前有一个问题让你想要拥有那个(不可能的)功能。

关于Haskell:runST 出现 "Could not deduce"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11801811/

相关文章:

haskell - Monads(Haskell)的主要目的

Haskell 无法构造无限类型 : a0 = [a0]

haskell - 如何使用免费的 monad 实现 Reader?

haskell - 什么时候泛型函数不是泛型的?

haskell - Haskell 可以评估列表中的随机索引而不是垃圾收集吗?

haskell - 安装不在 stackage 上但在 hackage 上的包

haskell - 从 lambda 项到组合项的转换

haskell - 在haskell中将管道组合成一个循环或循环

parsing - 无法理解解析器组合器中的 'pMany' 和 'pMany1'

haskell - 在 Sqlite3 中输入的 Scotty 参数