[GHC] #2158: deriving (Ix) and listArray give unexpected results

GHC trac at galois.com
Sat Mar 15 00:18:35 EDT 2008


#2158: deriving (Ix) and listArray give unexpected results
------------------------+---------------------------------------------------
    Reporter:  guest    |       Owner:                
        Type:  bug      |      Status:  new           
    Priority:  normal   |   Component:  libraries/base
     Version:  6.6      |    Severity:  normal        
    Keywords:           |    Testcase:                
Architecture:  Unknown  |          Os:  Unknown       
------------------------+---------------------------------------------------
 When `arrayList` is used together with a derived instance of `Ix`, the
 results are not the same as what the Haskell 98 report suggests.

 {{{
 import Data.Array
 import Data.Ix

 data Pos = Pos Integer Integer
   deriving (Show, Eq, Ord, Ix)

 {-
 -- copied from the H98 report, with (,) replaced by Pos
 instance Ix Pos where
   range (Pos l l', Pos u u')
     = [ Pos i i' | i <- range (l, u), i' <- range (l', u') ]
   index (Pos l l', Pos u u') (Pos i i')
     = index (l, u) i * rangeSize (l', u') + index (l', u') i'
   inRange (Pos l l', Pos u u') (Pos i i')
     = inRange (l, u) i && inRange (l', u') i'
 -}

 contents = concat $
   [ "ABCD"
   , "wxyz"
   , "1234"
   ]

 -- example definition of listArray from the H98 report
 listArray98 b xs = array b (range b `zip` xs)

 array1 = listArray   (Pos 0 0, Pos 2 3) contents
 array2 = listArray98 (Pos 0 0, Pos 2 3) contents


 main = if array1 == array2
   then putStrLn "arrays are equal"
   else putStrLn "arrays are NOT equal"
 }}}

 In this example, `array1` and `array2` should be equal, but they are not.
 The `listArray` function interprets its argument list in a different order
 from `listArray98`.

 The underlying problem seems to be that the integer indices produced by
 the derived `Ix` do not agree with the order implied by `range`, but
 `listArray` assumes that they do. Replacing either the `Ix` instance or
 the `listArray` definition with the H98 version gives correct results.

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/2158>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the Glasgow-haskell-bugs mailing list