[Haskell-cafe] Bug in writeArray?

Bulat Ziganshin bulat.ziganshin at gmail.com
Wed Sep 23 11:36:41 EDT 2009


Hello Grzegorz,

Wednesday, September 23, 2009, 7:19:59 PM, you wrote:

> This seems like a bug in the implementation of writeArray: when passed
>   let (l,u) = ((0,10),(20,20))

writeArray computes raw index (from 0 to total number of array
elements) and check that this index is correct. with multi-dimensional
arrays this approach may lead to wrong results, as you mentioned. it's
known problem that isn't fixed for a long time probably due to
efficiency cautions. the error is here:

data Ix i => Array i e
                 = Array !i         -- the lower bound, l
                         !i         -- the upper bound, u
                         !Int       -- a cache of (rangeSize (l,u))
                                    -- used to make sure an index is
                                    -- really in range
                         (Array# e) -- The actual elements

(!) :: Ix i => Array i e -> i -> e
arr@(Array l u n _) ! i = unsafeAt arr $ safeIndex (l,u) n i

safeIndex :: Ix i => (i, i) -> Int -> i -> Int
safeIndex (l,u) n i = let i' = unsafeIndex (l,u) i
                      in if (0 <= i') && (i' < n)
                         then i'
                         else error "Error in array index"


obviously, safeIndex should use inRange call instead


-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list