'Data.Vector.Mutable field can not be written/set properly? : Haskell
I have struggled for days to compose a data structure that has a field of mutable value of Data.Vector.Mutable
I confirmed Data.Vector.Mutable itself behaves as I expected; however once it's included into a structure, somehow stops working against my expectation.
Below is a demo-code that simply has newVal, getVal and setVal targeting the mutable field value of the structure.
newIO is the constructor of the data structure typed as newIO :: a -> A a.
module Main where
import Control.Monad.Primitive (PrimMonad (PrimState))
import qualified Data.Vector.Mutable as M
------------------------------------
data A a = A
{ ioVal :: IO (M.MVector (PrimState IO) a)
}
newIO :: a -> A a
newIO = \a -> A (newVal a)
------------------------------
newVal :: a -> IO (M.MVector (PrimState IO) a)
newVal = \a -> do
val <- M.new 1
M.write val 0 a
return val
getVal :: A a -> IO a
getVal = \aA -> do
val <- ioVal aA
M.read val 0
setVal :: a -> A a -> IO ()
setVal = \a -> \aA -> do
val <- ioVal aA
M.write val 0 a
------------------------------
main :: IO ()
main = do
let ioA = newIO (5 :: Int)
(getVal ioA) >>= print -- 5
setVal 10 ioA
(getVal ioA) >>= print -- 5 ?? expected 10
So, here, to confirm the basic behavior of set/get of the structure, I try to create, read, (re)write, and (re)read the mutable value of the field; however, it does not work as expected as the set should perform.
What's wrong with the code? Please advise.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
