[Haskell-cafe] STArray newListArray

Ben Millwood haskell at benmachine.co.uk
Sun Feb 21 11:33:56 EST 2010

2010/2/21 Vojtěch Knyttl <knyttv at gmail.com>:
> Hello,
> I am trying to create STArray with newListArray like this:
> la x y = newListArray ((1,1),(x,y)) [(x'+y') | x' <- [1..x], y' <- [1..y]]
> – but it does not work:
> No instance for (MArray a Field m)

Notice that newListArray has a monadic return type:

newListArray :: (MArray a e m, Ix i) => (i, i) -> [e] -> m (a i e)

with MArray a e m requiring Monad m.
So newListArray returns your STArray in a monad. That pretty much has
to be the ST monad, but that's not in scope, so you need to import
Control.Monad.ST before the MArray (STArray s) e (ST s) instance is

> I tried to define the type like this, but it would not work either:
> la :: Int -> Int -> STArray (Int,Int) Field

The kind for STArray here is slightly off. STArray needs three type
parameters, not two.
This is linked to the fact that ST is not a monad, but (ST s) is - the
extra parameter is essential to ST, to ensure the mutability doesn't
"leak out".
You probably want:

la :: Integer -> Integer -> ST s (STArray s (Integer, Integer) Field)

More information about the Haskell-Cafe mailing list