在 Haskell 中创建可变的 Data.Vector

chr*_*ris 4 haskell vector mutable

我希望使用 Data.Vector.Generic.Mutable.new 创建一个可变向量。我找到了通过解冻纯向量来创建可变向量的示例,但这不是我想要做的。

这是许多失败的尝试之一:

import Control.Monad.Primitive
import qualified Data.Vector.Generic.Mutable as GM

main = do
  v <- (GM.new 10) :: (GM.MVector v a) => IO (v RealWorld a)
  GM.write v 0 (3::Int)
  x <- GM.read v 0
  putStrLn $ show x
Run Code Online (Sandbox Code Playgroud)

给我错误

No instance for (GM.MVector v0 Int)
  arising from an expression type signature
Possible fix: add an instance declaration for (GM.MVector v0 Int)
Run Code Online (Sandbox Code Playgroud)

我尝试了基于 Haskell Vector 教程的变体,但没有成功。

我也欢迎关于构建向量的更清洁方法的建议。对现实世界的提及对我来说似乎很难看。

cdk*_*cdk 5

中的约束GM.MVector v a是不明确的v。换句话说,根据您提供给 GHC 的类型信息,它仍然无法弄清楚GM.MVector您希望它使用哪个特定实例。对于可变向量的Int使用Data.Vector.Unboxed.Mutable

import qualified Data.Vector.Unboxed.Mutable as M

main = do
    v <- M.new 10
    M.write v 0 (3 :: Int)
    x <- M.read v 0
    print x
Run Code Online (Sandbox Code Playgroud)