f1edf00907db063b5efd359ff88b4c1d8f6d1891
[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 module Data.Ix
20 (
21 -- * The 'Ix' class
22 Ix
23 ( range -- :: (Ix a) => (a,a) -> [a]
24 , index -- :: (Ix a) => (a,a) -> a -> Int
25 , inRange -- :: (Ix a) => (a,a) -> a -> Bool
26 , rangeSize -- :: (Ix a) => (a,a) -> Int
27 )
28 -- Ix instances:
29 --
30 -- Ix Char
31 -- Ix Int
32 -- Ix Integer
33 -- Ix Bool
34 -- Ix Ordering
35 -- Ix ()
36 -- (Ix a, Ix b) => Ix (a, b)
37 -- ...
38
39 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
40
41 -- * Deriving Instances of 'Ix'
42 -- | Derived instance declarations for the class 'Ix' are only possible
43 -- for enumerations (i.e. datatypes having only nullary constructors)
44 -- and single-constructor datatypes, including arbitrarily large tuples,
45 -- whose constituent types are instances of 'Ix'.
46 --
47 -- * For an enumeration, the nullary constructors are assumed to be
48 -- numbered left-to-right with the indices being 0 to n-1 inclusive. This
49 -- is the same numbering defined by the 'Enum' class. For example, given
50 -- the datatype:
51 --
52 -- > data Colour = Red | Orange | Yellow | Green | Blue | Indigo | Violet
53 --
54 -- we would have:
55 --
56 -- > range (Yellow,Blue) == [Yellow,Green,Blue]
57 -- > index (Yellow,Blue) Green == 1
58 -- > inRange (Yellow,Blue) Red == False
59 --
60 -- * For single-constructor datatypes, the derived instance declarations
61 -- are as shown for tuples in Figure 1
62 -- <http://www.haskell.org/onlinelibrary/ix.html#prelude-index>.
63
64 ) where
65
66 -- import Prelude
67
68 #ifdef __GLASGOW_HASKELL__
69 import GHC.Arr
70 #endif
71
72 #ifdef __HUGS__
73 import Hugs.Prelude( Ix(..) )
74 #endif
75
76 #ifdef __NHC__
77 import Ix (Ix(..))
78 #endif
79