[Haskell-cafe] Array bug?

Andrew Coppin andrewcoppin at btinternet.com
Sat Nov 1 08:53:14 EDT 2008


Consider the following GHCi session:

GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude> :m Data.Array.IO
Prelude Data.Array.IO> t <- newArray ((0,0),(5,4)) 0 :: IO (IOUArray 
(Int,Int) Int)
Loading package array-0.1.0.0 ... linking ... done.
Prelude Data.Array.IO> getBounds t
((0,0),(5,4))
Prelude Data.Array.IO> writeArray t (10,10) 5
*** Exception: Error in array index
Prelude Data.Array.IO> writeArray t (7,3) 5
*** Exception: Error in array index
Prelude Data.Array.IO> writeArray t (3,7) 5
Prelude Data.Array.IO>

So the array has 0 <= x <= 5 and 0 <= y <= 4, and writing to (10,10) or 
(7,3) throws an exception. However, writing to (3,7) triggers no 
exception - despite being clearly out of range. Judging by the behaviour 
of the large, complex program I was debugging when I stumbled upon this, 
the coordinates "wrap round" to the next column. (!!)

Obviously, writing to non-existent coordinates and not getting an 
exception is a Very Bad Thing. I was counting on writeArray (and 
readArray) to detect out-of-range coordinates so I could fix my code. 
But as you can see, it doesn't actually work as advertised. You have *no 
idea* how long I spent trying to track this bug down! >_<

Is this a known bug? Is it likely to be fixed any time soon? (I'm 
guessing the bug is as simple is converting indicies to integers and 
then checking the integers are in-range, rather than the underlying 
index type.)




More information about the Haskell-Cafe mailing list