Hello,
while experimenting with arrays in Hugs98 (November 1999 and February 2000
versions), I discovered that
Array> listArray ((0,0),(-1,1)) [] :: Array (Int,Int) Int
array ((0,0),(-1,1)) []
Array> listArray ((0,0),(1,-1)) [] :: Array (Int,Int) Int
array
Program error: index: Index out of range
In GHC and NHC98 the examples work without a runtime error. I finally found
that Hugs's behaviour is caused by the definition of rangeSize in
Prelude.hs:
rangeSize r@(l,u)
| l > u = 0
| otherwise = index r u + 1
This implementation has a bug, as one can read in the Haskell '98 Library
Report (Section 5.2, module Ix):
rangeSize :: Ix a => (a,a) -> Int
rangeSize b@(l,h) | null (range b) = 0
| otherwise = index b h + 1
-- NB: replacing "null (range b)" by "l > h" fails if
-- the bounds are tuples. For example,
-- (2,1) > (1,2),
-- but
-- range ((2,1),(1,2)) = []
Modifying rangeSize in Prelude.hs (with the patch appended below) fixes the
problem.
I don't know if I looked at the right place in the GHC/StgHugs CVS
repository [1], but I think that StgHugs uses the same (wrong) definition of
rangeSize.
Cheers,
Armin
[1] http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/ghc/lib/
hugs/PrelPrim.hs?rev=1.4&content-type=text/x-cvsweb-markup
--- /usr/share/hugs98/lib/Prelude.hs Sun Apr 9 14:08:53 2000
+++ Prelude.hs Fri Sep 1 00:37:13 2000
@@ -322,8 +322,8 @@
rangeSize :: (a,a) -> Int
rangeSize r@(l,u)
- | l > u = 0
- | otherwise = index r u + 1
+ | null (range r) = 0
+ | otherwise = index r u + 1
class Enum a where
succ, pred :: a -> a