test :: VM.MVector s Int -> Int
test x = runST $ do
a <- return x
VM.read a 0 -- Type error
I am trying to figure out how to not put everything inside a ST monad into a single function. If I tried to modify x or return a value from it, the compiler would complain about the state part of the mutable vector not matching up.
Would operating on passed mutable vectors be possible in Haskell or do I have to freeze them into the immutable counterparts before doing anything on them?
Edit:
Here is the actual error.
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
Edit: The following passes type checking.
test :: VM.MVector (Control.Monad.Primitive.PrimState IO) Int -> IO (Int)
test x = VM.read x 0
I am guessing that I would be able to mutate the x vector as well. So...
a <- return xis redundant. That just gives youxagain. - melpomeneVM.MVector s Int -> Intis necessarily a constant function. - chi