unsafeRead causing stack overflow

haskell, stack-overflow

Solution

`unsafeRead` uses 0-based indices, so you have to either adjust the index for reading,

elem <- unsafeRead arr (i-1)

or, better, allocate the array with a least index of 0

arr <- newArray (0,upperbound) maxBound

As is, the `unsafeRead` reads from the wrong place in the array.

Unrelated: I suspect ideone uses a 32-bit GHC, so better don't run the code with `upperbound > 100000` there.

Problem

Here's some code that does bounds checks at ideone. It runs successfully with no out of bounds error. In this code, on line 34 I have changed on `readArray` to `unsafeRead`, and it still compiles, but crashes with a stack overflow. Is this a GHC bug or have I done something wrong?

Original source