2 module Ix ( Ix(range, index, inRange), rangeSize ) where
7 class (Show a, Ord a) => Ix a where
9 index :: (a,a) -> a -> Int
10 inRange :: (a,a) -> a -> Bool
12 rangeSize :: Ix a => (a,a) -> Int
13 rangeSize b@(l,h) | l > h = 0
14 | otherwise = index b h + 1
18 instance Ix Bool where
19 range (c,c') = [c..c']
21 | inRange b ci = fromEnum ci - fromEnum c
22 | otherwise = error "Ix.index.Bool: Index out of range."
23 inRange (c,c') ci = fromEnum c <= i && i <= fromEnum c'
27 instance Ix Char where
28 range (c,c') = [c..c']
30 | inRange b ci = fromEnum ci - fromEnum c
31 | otherwise = error "Ix.index.Char: Index out of range."
32 inRange (c,c') ci = fromEnum c <= i && i <= fromEnum c'
39 | otherwise = error "Ix.index.Int: Index out of range."
40 inRange (m,n) i = m <= i && i <= n
42 #ifdef PROVIDE_INTEGER
43 instance Ix Integer where
47 | inRange b i = fromInteger (i - m)
49 /* fromInteger may not have an Integer arg :-) */
50 | inRange b i = toInt (i - m)
52 | otherwise = error "Ix.index.Integer: Index out of range."
53 inRange (m,n) i = m <= i && i <= n
57 instance (Ix a,Ix b) => Ix (a, b) -- as derived, for all tuples
58 instance Ix Bool -- as derived
59 instance Ix Ordering -- as derived
60 instance Ix () -- as derived
62 -- #error "Missing Ix instances"