[project @ 2005-01-19 22:33:32 by ralf]
[haskell-directory.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 #ifndef __NHC__
76 import Data.Typeable
77 #endif
78
79 #ifndef __NHC__
80 #include "Typeable.h"
81 INSTANCE_TYPEABLE2(Array,arrayTc,"Array")
82 #endif
83
84 #ifdef __GLASGOW_HASKELL__
85
86 -- This instance preserves data abstraction at the cost of inefficiency.
87 -- We omit reflection services for the sake of data abstraction.
88
89 instance (Typeable a, Data b, Ix a) => Data (Array a b)
90  where
91   gfoldl f z a = z (listArray (bounds a)) `f` (elems a)
92   toConstr _   = error "toConstr"
93   gunfold _ _  = error "gunfold"
94   dataTypeOf _ = mkNorepType "Data.Array.Array"
95
96 #endif
97
98 {- $intro
99 Haskell provides indexable /arrays/, which may be thought of as functions
100 whose domains are isomorphic to contiguous subsets of the integers.
101 Functions restricted in this way can be implemented efficiently;
102 in particular, a programmer may reasonably expect rapid access to
103 the components.  To ensure the possibility of such an implementation,
104 arrays are treated as data, not as general functions.
105
106 Since most array functions involve the class 'Ix', this module is exported
107 from "Data.Array" so that modules need not import both "Data.Array" and
108 "Data.Ix".
109 -}