summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsof <unknown>1999-01-23 17:43:21 +0000
committersof <unknown>1999-01-23 17:43:21 +0000
commit4d38d0728a1e6e899c23efd7a67060d45bf784cd (patch)
treede7d01fa1f57cf49842404e36280d2c812dd1966
parentfda7f6a2492691092c007a2c684c23b9822fb9ba (diff)
downloadhaskell-4d38d0728a1e6e899c23efd7a67060d45bf784cd.tar.gz
[project @ 1999-01-23 17:43:21 by sof]
Changed the various Ix.range methods, to specifically check whether we're dealing with an empty range or not. As it was, empty array weren't handled properly.
-rw-r--r--ghc/lib/std/Ix.lhs27
-rw-r--r--ghc/lib/std/PrelNum.lhs5
2 files changed, 23 insertions, 9 deletions
diff --git a/ghc/lib/std/Ix.lhs b/ghc/lib/std/Ix.lhs
index db17b458e3..545c266f43 100644
--- a/ghc/lib/std/Ix.lhs
+++ b/ghc/lib/std/Ix.lhs
@@ -56,17 +56,21 @@ class ({-Show a,-} Ord a) => Ix a where
\begin{code}
instance Ix Char where
- range (c,c') = [c..c']
+ range (c,c')
+ | c <= c' = [c..c']
+ | otherwise = []
index b@(c,_) ci
| inRange b ci = fromEnum ci - fromEnum c
| otherwise = indexError ci b "Char"
inRange (m,n) i = m <= i && i <= n
instance Ix Int where
- range (m,n) = [m..n]
+ range (m,n)
+ | m <= n = [m..n]
+ | otherwise = []
index b@(m,_) i
- | inRange b i = i - m
- | otherwise = indexError i b "Int"
+ | inRange b i = i - m
+ | otherwise = indexError i b "Int"
inRange (m,n) i = m <= i && i <= n
-- abstract these errors from the relevant index functions so that
@@ -84,13 +88,17 @@ indexError i rng tp
----------------------------------------------------------------------
instance Ix Bool where -- as derived
- range (l,u) = map toEnum [fromEnum l .. fromEnum u]
+ range (l,u)
+ | l <= u = map toEnum [fromEnum l .. fromEnum u]
+ | otherwise = []
index (l,_) i = fromEnum i - fromEnum l
inRange (l,u) i = fromEnum i >= fromEnum l && fromEnum i <= fromEnum u
----------------------------------------------------------------------
instance Ix Ordering where -- as derived
- range (l,u) = map toEnum [fromEnum l .. fromEnum u]
+ range (l,u)
+ | l <= u = map toEnum [fromEnum l .. fromEnum u]
+ | otherwise = []
index (l,_) i = fromEnum i - fromEnum l
inRange (l,u) i = fromEnum i >= fromEnum l && fromEnum i <= fromEnum u
@@ -183,7 +191,10 @@ in the range for an @Ix@ pair:
{-# SPECIALISE rangeSize :: (Int,Int) -> Int #-}
rangeSize :: (Ix a) => (a,a) -> Int
rangeSize b@(l,h)
- | l > h = 0
- | otherwise = index b h + 1
+ | l > h || isnull (range b) = 0
+ | otherwise = index b h + 1
+ where
+ isnull [] = True
+ isnull _ = False
\end{code}
diff --git a/ghc/lib/std/PrelNum.lhs b/ghc/lib/std/PrelNum.lhs
index 5a835ad119..08081353f3 100644
--- a/ghc/lib/std/PrelNum.lhs
+++ b/ghc/lib/std/PrelNum.lhs
@@ -288,7 +288,10 @@ instance Show Integer where
instance Ix Integer where
- range (m,n) = [m..n]
+ range (m,n)
+ | m <= n = [m..n]
+ | otherwise = []
+
index b@(m,_) i
| inRange b i = fromInteger (i - m)
| otherwise = indexIntegerError i b