[Haskell-cafe] STUArray

Frederik Eaton frederik at a5.repetae.net
Fri Mar 10 17:01:55 EST 2006

Hi all,

I'm trying to figure out how to use STUArray. Is it possible to let it
be polymorphic?

Here is an excerpt from my program:

    class (IArray UArray k, Ord k, Fractional k) => Elt k

    class (Bounded a, Enum a, Ix a, Eq a, Show a) => IxB a 

    -- |like Array but uses IxB instead of Ix 
    newtype Vector k i = Vector (UArray.UArray i k) deriving (Eq) 

    margin :: (IxB a, IxB b, Elt k, (MArray (STUArray s) k (ST s))) => Vector k a -> (a -> b) -> Vector k b
    margin (Vector v) f = Vector (runSTUArray (do
        ax <- newArray (minBound, maxBound) 0
        mapM_ (\ (i,x) -> updateArray ax (f i) (+x)) (assocs v)
        return ax
      )) where
        updateArray ax i f = do
          e <- readArray ax i
          writeArray ax i (f e)

When I try to run it, I get the following error. I think I understand
what the error means, but not how to fix the problem:

        No instance for (MArray (STUArray s) k (ST s))
          arising from use of `updateArray' at Vector2.hs:166:22-32
        Probable fix:
          add (MArray (STUArray s) k (ST s)) to the expected type of an expression
          or add an instance declaration for (MArray (STUArray s) k (ST s))
        In a lambda abstraction: \ (i, x) -> updateArray ax (f i) ((+ x))
        In the first argument of `mapM_', namely
            `(\ (i, x) -> updateArray ax (f i) ((+ x)))'
        In a 'do' expression:
            mapM_ (\ (i, x) -> updateArray ax (f i) ((+ x))) (assocs v)

Thanks in advance,



More information about the Haskell-Cafe mailing list