test :: VM.MVector s Int -> Int
test x = runST $ do
a <- return x
VM.read a 0 -- Type error
我想弄清楚如何不把一個ST monad中的所有東西都放到一個函數中。如果我試圖修改x
或從中返回值,編譯器會抱怨可變向量的狀態部分不匹配。如何修改或讀取作爲函數參數傳遞的可變向量?
在Haskell中可能對傳遞的可變向量進行操作嗎?還是必須在對它們做任何事情之前將它們凍結到不可變對象中?
編輯:
這是實際的錯誤。
Couldn't match type `s1' with `s'
`s1' is a rigid type variable bound by
a type expected by the context: ST s1 Int at rjb.hs:17:12
`s' is a rigid type variable bound by
the type signature for test :: VM.MVector s Int -> Int
at rjb.hs:16:11
Expected type: VM.MVector
(Control.Monad.Primitive.PrimState (ST s1)) Int
Actual type: VM.MVector s Int
Relevant bindings include
a :: VM.MVector s Int (bound at rjb.hs:18:5)
x :: VM.MVector s Int (bound at rjb.hs:17:8)
test :: VM.MVector s Int -> Int (bound at rjb.hs:17:3)
In the first argument of `VM.read', namely `a'
In a stmt of a 'do' block: VM.read a 0
編輯:以下通過類型檢查。
test :: VM.MVector (Control.Monad.Primitive.PrimState IO) Int -> IO (Int)
test x = VM.read x 0
我在猜測我也可以改變x
矢量。所以...
請問您可以添加實際的錯誤嗎? – Carsten
'a < - return x'是多餘的。這只是給你'x'。 – melpomene
@Carsten增加了錯誤。 –