Mutable vectors in Haskell have three element-level mutators:
read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a
write :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m ()
swap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m ()
Now I can use these fine --
import Data.Vector
import Data.Vector.Mutable
import Control.Monad.ST
import Control.Monad.Primitive
incrAt :: Vector Double -> Int -> Vector Double
incrAt vec i = runST $ do
mvec <- thaw vec
oldval <- read mvec i
write mvec i (oldval + 1)
freeze mvec
But what is going on here? What is a PrimMonad
? And is PrimState
a constructor?
I understand there is some binding going on here, on a PrimMonad
class monad. thaw
returns m (MVector (PrimState m) a)
, where m
is a PrimMonad
... but the monad contains itself? Why is m
inside the context of another m
?
I see that everything is basically binding on this PrimState
or PrimMonad
, but I don't see how this has to do with mutable/storable vectors. Is there something special about those typeclasses that allow them to store state?
Thank you for your time!