263f9707c669dafb9cf62060e7dfb8650db68d65
[ghc-base.git] / Data / Array.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Data.Array 
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  provisional
10 -- Portability :  portable
11 --
12 -- Basic non-strict arrays.
13 --
14 -- /Note:/ The "Data.Array.IArray" module provides more general interface
15 -- to immutable arrays: it defines operations with the same names as
16 -- those defined below, but with more general types, and also defines
17 -- 'Array' instances of the relevant classes.  To use that more general
18 -- interface, import "Data.Array.IArray" but not "Data.Array".
19 -----------------------------------------------------------------------------
20
21 module  Data.Array 
22
23     ( 
24     -- * Immutable non-strict arrays
25     -- $intro
26       module Data.Ix            -- export all of Ix 
27     , Array                     -- Array type is abstract
28
29     -- * Array construction
30     , array         -- :: (Ix a) => (a,a) -> [(a,b)] -> Array a b
31     , listArray     -- :: (Ix a) => (a,a) -> [b] -> Array a b
32     , accumArray    -- :: (Ix a) => (b -> c -> b) -> b -> (a,a) -> [(a,c)] -> Array a b
33     -- * Accessing arrays
34     , (!)           -- :: (Ix a) => Array a b -> a -> b
35     , bounds        -- :: (Ix a) => Array a b -> (a,a)
36     , indices       -- :: (Ix a) => Array a b -> [a]
37     , elems         -- :: (Ix a) => Array a b -> [b]
38     , assocs        -- :: (Ix a) => Array a b -> [(a,b)]
39     -- * Incremental array updates
40     , (//)          -- :: (Ix a) => Array a b -> [(a,b)] -> Array a b
41     , accum         -- :: (Ix a) => (b -> c -> b) -> Array a b -> [(a,c)] -> Array a b
42     -- * Derived arrays
43     , ixmap         -- :: (Ix a, Ix b) => (a,a) -> (a -> b) -> Array b c -> Array a b
44
45     -- Array instances:
46     --
47     --   Ix a => Functor (Array a)
48     --   (Ix a, Eq b)  => Eq   (Array a b)
49     --   (Ix a, Ord b) => Ord  (Array a b)
50     --   (Ix a, Show a, Show b) => Show (Array a b)
51     --   (Ix a, Read a, Read b) => Read (Array a b)
52     -- 
53
54     -- Implementation checked wrt. Haskell 98 lib report, 1/99.
55
56     ) where
57
58 import Data.Ix
59
60 #ifdef __GLASGOW_HASKELL__
61 import GHC.Arr                  -- Most of the hard work is done here
62 import Data.Generics.Basics     -- To provide a Data instance
63 import Data.Generics.Instances  -- To provide a Data instance
64 import GHC.Err ( error )        -- Needed for Data instance
65 #endif
66
67 #ifdef __HUGS__
68 import Hugs.Array
69 #endif
70
71 #ifdef __NHC__
72 import Array            -- Haskell'98 arrays
73 #endif
74
75 import Data.Typeable
76 #include "Typeable.h"
77 INSTANCE_TYPEABLE2(Array,arrayTc,"Array")
78
79 #ifdef __GLASGOW_HASKELL__
80
81 -- This instance preserves data abstraction at the cost of inefficiency.
82 -- We omit reflection services for the sake of data abstraction.
83
84 instance (Typeable a, Data b, Ix a) => Data (Array a b)
85  where
86   gfoldl f z a = z (listArray (bounds a)) `f` (elems a)
87   toConstr _   = error "toConstr"
88   gunfold _ _  = error "gunfold"
89   dataTypeOf _ = mkNorepType "Data.Array.Array"
90
91 #endif
92
93 {- $intro
94 Haskell provides indexable /arrays/, which may be thought of as functions
95 whose domains are isomorphic to contiguous subsets of the integers.
96 Functions restricted in this way can be implemented efficiently;
97 in particular, a programmer may reasonably expect rapid access to
98 the components.  To ensure the possibility of such an implementation,
99 arrays are treated as data, not as general functions.
100
101 Since most array functions involve the class 'Ix', this module is exported
102 from "Data.Array" so that modules need not import both "Data.Array" and
103 "Data.Ix".
104 -}