[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / interpreter / library / Ix.hs
1 #ifdef HEAD
2 module Ix ( Ix(range, index, inRange), rangeSize ) where
3 import PreludeBuiltin
4 #endif /* HEAD */
5 #ifdef BODY
6
7 class  (Show a, Ord a) => Ix a  where
8     range               :: (a,a) -> [a]
9     index               :: (a,a) -> a -> Int
10     inRange             :: (a,a) -> a -> Bool
11
12 rangeSize :: Ix a => (a,a) -> Int
13 rangeSize b@(l,h) | l > h     = 0
14                   | otherwise = index b h + 1 
15  
16 #if STD_PRELUDE
17 #else
18 instance  Ix Bool  where
19     range (c,c')        =  [c..c']
20     index b@(c,c') ci
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'
24                            where i = fromEnum ci
25 #endif
26
27 instance  Ix Char  where
28     range (c,c')        =  [c..c']
29     index b@(c,c') ci
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'
33                            where i = fromEnum ci
34
35 instance  Ix Int  where
36     range (m,n)         =  [m..n]
37     index b@(m,n) i
38         | inRange b i   =  i - m
39         | otherwise     =  error "Ix.index.Int: Index out of range."
40     inRange (m,n) i     =  m <= i && i <= n
41
42 #ifdef PROVIDE_INTEGER
43 instance  Ix Integer  where
44     range (m,n)         =  [m..n]
45     index b@(m,n) i
46 #if STD_PRELUDE
47         | inRange b i   =  fromInteger (i - m)
48 #else
49                            /* fromInteger may not have an Integer arg :-) */
50         | inRange b i   =  toInt (i - m)
51 #endif
52         | otherwise     =  error "Ix.index.Integer: Index out of range."
53     inRange (m,n) i     =  m <= i && i <= n
54 #endif
55
56 #if STD_PRELUDE
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
61 #else
62 -- #error "Missing Ix instances"
63 #endif
64
65 #endif /* BODY */