The Data.Array.* hierarchy is unsafe (or, Segfaulting for fun and profit)

Stefan O'Rear stefanor at
Wed Dec 6 08:39:08 EST 2006

On Wed, Dec 06, 2006 at 07:50:30AM +0000, Simon Peyton-Jones wrote:
> I rather agree with Iavor here.  If a program makes no use of unsafeX
> functions, and has no foreign calls, and passes the typechecker, then
> it should not crash.
> However, I don't see how to achieve this for array indexing, without
> adding another test to every array access.

If we allow inRange to return anything at all if the result of index is
out-of-bounds, then the standard Ix instances lose one test for each
access; e.g. the Int instance can return constant True for inRange
because any invalid index would give an invalid result.

Obviously, this makes inRange into a rather low level operation that
should be renamed and wrapped (with a function that always gives the
right answer).

More information about the Libraries mailing list