Update base for latest Safe Haskell.
[packages/base.git] / Data / Ix.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : Data.Ix
7 -- Copyright : (c) The University of Glasgow 2001
8 -- License : BSD-style (see the file libraries/base/LICENSE)
9 --
10 -- Maintainer : libraries@haskell.org
11 -- Stability : stable
12 -- Portability : portable
13 --
14 -- The 'Ix' class is used to map a contiguous subrange of values in
15 -- type onto integers. It is used primarily for array indexing
16 -- (see the array package).
17 --
18 -----------------------------------------------------------------------------
19
20 module Data.Ix
21 (
22 -- * The 'Ix' class
23 Ix
24 ( range -- :: (Ix a) => (a,a) -> [a]
25 , index -- :: (Ix a) => (a,a) -> a -> Int
26 , inRange -- :: (Ix a) => (a,a) -> a -> Bool
27 , rangeSize -- :: (Ix a) => (a,a) -> Int
28 )
29 -- Ix instances:
30 --
31 -- Ix Char
32 -- Ix Int
33 -- Ix Integer
34 -- Ix Bool
35 -- Ix Ordering
36 -- Ix ()
37 -- (Ix a, Ix b) => Ix (a, b)
38 -- ...
39
40 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
41
42 -- * Deriving Instances of 'Ix'
43 -- | Derived instance declarations for the class 'Ix' are only possible
44 -- for enumerations (i.e. datatypes having only nullary constructors)
45 -- and single-constructor datatypes, including arbitrarily large tuples,
46 -- whose constituent types are instances of 'Ix'.
47 --
48 -- * For an enumeration, the nullary constructors are assumed to be
49 -- numbered left-to-right with the indices being 0 to n-1 inclusive. This
50 -- is the same numbering defined by the 'Enum' class. For example, given
51 -- the datatype:
52 --
53 -- > data Colour = Red | Orange | Yellow | Green | Blue | Indigo | Violet
54 --
55 -- we would have:
56 --
57 -- > range (Yellow,Blue) == [Yellow,Green,Blue]
58 -- > index (Yellow,Blue) Green == 1
59 -- > inRange (Yellow,Blue) Red == False
60 --
61 -- * For single-constructor datatypes, the derived instance declarations
62 -- are as shown for tuples in Figure 1
63 -- <http://www.haskell.org/onlinelibrary/ix.html#prelude-index>.
64
65 ) where
66
67 -- import Prelude
68
69 #ifdef __GLASGOW_HASKELL__
70 import GHC.Arr
71 #endif
72
73 #ifdef __HUGS__
74 import Hugs.Prelude( Ix(..) )
75 #endif
76
77 #ifdef __NHC__
78 import Ix (Ix(..))
79 #endif
80