[Haskell-cafe] STArray newListArray

Vojtěch Knyttl knyttv at gmail.com
Sun Feb 21 15:26:31 EST 2010

Ok, the problem was the Monad, which I still don't get completely, because I continue like this:

data Field = W|B|H|D deriving (Eq,Show)

pas :: ST s (STArray s (Int, Int) Field) -> ST s (STArray s (Int, Int) Field)
pas b = do writeArray b (1,1) W

And still getting:
    Couldn't match expected type `STArray s (Int, Int) Field'
           against inferred type `Field'


On Feb 21, 2010, at 5:33, Ben Millwood wrote:

> 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
> usable.
>> 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