1 -- *** all of PreludeArray except the actual data decls
23 import IInt -- instances
26 import List ( (++), zipWith, foldr )
27 import Prel ( (&&), (.) )
28 import PS ( _PackedString, _unpackPS )
30 import TyArray ( Array(..), Assoc(..) )
33 -- Hey! This isn't wimp Haskell-report code! This is
34 -- the Business End of Arrays...
40 -----------------------------------------------------------
41 instance (Eq a, Eq b) => Eq (Assoc a b) where
42 (a1 := b1) == (a2 := b2) = a1 == a2 && b1 == b2
43 a /= b = if a == b then False else True
45 instance (Ord a, Ord b) => Ord (Assoc a b) where
46 a < b = case _tagCmp a b of { _LT -> True; _EQ -> False; _GT -> False }
47 a <= b = case _tagCmp a b of { _LT -> True; _EQ -> True; _GT -> False }
48 a >= b = case _tagCmp a b of { _LT -> False; _EQ -> True; _GT -> True }
49 a > b = case _tagCmp a b of { _LT -> False; _EQ -> False; _GT -> True }
50 max a b = case _tagCmp a b of { _LT -> b; _EQ -> a; _GT -> a }
51 min a b = case _tagCmp a b of { _LT -> a; _EQ -> a; _GT -> b }
52 _tagCmp (a1 := b1) (a2 := b2)
53 = case (_tagCmp a1 a2) of { _LT -> _LT; _GT -> _GT; _EQ -> _tagCmp b1 b2 }
55 instance (Ix a, Ix b) => Ix (Assoc a b) where
56 range (l1 := l2, u1 := u2)
57 = [ (i1 := i2) | i1 <- range (l1, u1), i2 <- range (l2, u2) ]
59 index (l1 := l2, u1 := u2) (i1 := i2)
60 = index (l1, u1) i1 * (index (l2, u2) u2 + 1){-rangeSize (l2, u2)-} + index (l2, u2) i2
62 inRange (l1 := l2, u1 := u2) (i1 := i2)
63 = inRange (l1, u1) i1 && inRange (l2, u2) i2
65 instance (Text a, Text b) => Text (Assoc a b) where
66 -- magic fixity wired in: infix 1 :=
69 (\ r -> [ (x := y, s2) | (x, s0) <- readsPrec 2 r,
71 (y, s2) <- readsPrec 2 s1 ])
74 (showsPrec 2 a . showString " := " . showsPrec 2 b)
78 -----------------------------------------------------------
82 {-# GENERATE_SPECS array a{~,Int,IPr} b{} #-}
83 array :: (Ix a) => (a,a) -> [Assoc a b] -> Array a b
85 {-# GENERATE_SPECS (!) a{~,Int,IPr} b{} #-}
86 (!) :: (Ix a) => Array a b -> a -> b
88 bounds :: Array a b -> (a,a)
90 {-# GENERATE_SPECS listArray a{~,Int,IPr} b{} #-}
91 listArray :: (Ix a) => (a,a) -> [b] -> Array a b
93 {-# GENERATE_SPECS indices a{~,Int,IPr} b{} #-}
94 indices :: (Ix a) => Array a b -> [a]
96 {-# GENERATE_SPECS elems a{~,Int,IPr} b{} #-}
97 elems :: (Ix a) => Array a b -> [b]
99 {-# GENERATE_SPECS assocs a{~,Int,IPr} b{} #-}
100 assocs :: (Ix a) => Array a b -> [Assoc a b]
102 {-# GENERATE_SPECS accumArray a{~,Int,IPr} b{} c{} #-}
103 accumArray :: (Ix a) => (b -> c -> b) -> b -> (a,a) -> [Assoc a c] -> Array a b
105 {-# GENERATE_SPECS (//) a{~,Int,IPr} b{} #-}
106 (//) :: (Ix a) => Array a b -> [Assoc a b] -> Array a b
108 {-# GENERATE_SPECS accum a{~,Int,IPr} b{} c{} #-}
109 accum :: (Ix a) => (b -> c -> b) -> Array a b -> [Assoc a c] -> Array a b
111 {-# GENERATE_SPECS amap a{~,Int,IPr} b{} c{} #-}
112 amap :: (Ix a) => (b -> c) -> Array a b -> Array a c
114 ixmap :: (Ix a, Ix b) => (a,a) -> (a -> b) -> Array b c -> Array a c
117 {- "array", "!" and "bounds" are basic;
118 the rest can be defined in terms of them
121 bounds (_Array b _) = b
123 array ixs@(ix_start, ix_end) ivs
125 newArray ixs arrEleBottom `thenStrictlyST` \ arr# ->
126 fill_it_in arr# ivs `seqStrictlyST`
130 arrEleBottom = error "(!){PreludeArray}: undefined array element"
132 (_Array bounds arr#) ! i
133 = let n# = case (index bounds i) of { I# x -> x } -- index fails if out of range
135 case (indexArray# arr# n#) of
139 = foldr fill_one_in (returnStrictlyST ()) lst s
140 where -- **** STRICT **** (but that's OK...)
141 fill_one_in (i := v) rst s
142 = (writeArray arr i v `seqStrictlyST` rst) s
144 {- the rest ------------------------------------------------- -}
146 listArray b vs = array b (zipWith (:=) (range b) vs)
148 indices a = range (bounds a)
150 elems a = [a!i | i <- indices a]
152 assocs a = [i := a!i | i <- indices a]
154 #ifdef USE_REPORT_PRELUDE
155 a // us = array (bounds a)
156 ([i := a!i | i <- indices a \\ [i | i:=_ <- us]]
159 accum f = foldl (\a (i := v) -> a // [i := f (a!i) v])
161 accumArray f z b = accum f (array b [i := z | i <- range b])
163 #else /* ! USE_REPORT_PRELUDE */
167 -- copy the old array:
168 newArray (bounds old_array) bottom `thenStrictlyST` \ arr# ->
169 fill_it_in arr# (assocs old_array) `seqStrictlyST`
170 -- now write the new elements into the new array:
171 fill_it_in arr# ivs `seqStrictlyST`
175 bottom = error "(//){PreludeArray}: error in copying old array\n"
177 -- zap_with_f: reads an elem out first, then uses "f" on that and the new value
179 zap_with_f f arr lst s
180 = foldr zap_one (returnStrictlyST ()) lst s
182 zap_one (i := new_v) rst s
183 = (readArray arr i `thenStrictlyST` \ old_v ->
184 writeArray arr i (f old_v new_v) `seqStrictlyST`
189 -- copy the old array:
190 newArray (bounds arr) bottom `thenST` \ arr# ->
191 fill_it_in arr# (assocs arr) `seqST`
193 -- now zap the elements in question with "f":
194 zap_with_f f arr# ivs `seqST`
198 bottom = error "accum{PreludeArray}: error in copying old array\n"
200 accumArray f zero ixs ivs
202 newArray ixs zero `thenST` \ arr# ->
203 zap_with_f f arr# ivs `seqST`
206 #endif /* ! USE_REPORT_PRELUDE */
208 amap f a = array b [i := f (a!i) | i <- range b]
211 ixmap b f a = array b [i := a ! f i | i <- range b]
213 instance (Ix a, Eq b) => Eq (Array a b) where
214 a == a' = assocs a == assocs a'
215 a /= a' = assocs a /= assocs a'
217 instance (Ix a, Ord b) => Ord (Array a b) where
218 a < b = case _tagCmp a b of { _LT -> True; _EQ -> False; _GT -> False }
219 a <= b = case _tagCmp a b of { _LT -> True; _EQ -> True; _GT -> False }
220 a >= b = case _tagCmp a b of { _LT -> False; _EQ -> True; _GT -> True }
221 a > b = case _tagCmp a b of { _LT -> False; _EQ -> False; _GT -> True }
223 max a b = case _tagCmp a b of { _LT -> b; _EQ -> a; _GT -> a }
224 min a b = case _tagCmp a b of { _LT -> a; _EQ -> a; _GT -> b }
226 _tagCmp a b = _tagCmp (assocs a) (assocs b)
228 instance (Ix a, Text a, Text b) => Text (Array a b) where
229 showsPrec p a = showParen (p > 9) (
230 showString "array " .
231 shows (bounds a) . showChar ' ' .
234 readsPrec p = readParen (p > 9)
235 (\r -> [(array b as, u) | ("array",s) <- lex r,
239 [(listArray b xs, u) | ("listArray",s) <- lex r,
243 {- **** OMITTED **** (ToDo)
244 instance (Ix a, Binary a, Binary b) => Binary (Array a b) where
245 showBin a = showBin (bounds a) . showBin (elems a)
247 readBin bin = (listArray b vs, bin'')
248 where (b,bin') = readBin bin
249 (vs,bin'') = readBin bin'
253 #if defined(__UNBOXED_INSTANCES__)
255 -- {-# GENERATE_SPECS array a{~,Int#,Int,IPr} b{Int#,Double#} #-}
256 -- {-# GENERATE_SPECS (!) a{~,Int#,Int,IPr} b{Int#,Double#} #-}
257 -- {-# GENERATE_SPECS bounds a{~,Int#} b{Int#,Double#} #-}
258 -- {-# GENERATE_SPECS listArray a{~,Int#,Int,IPr} b{Int#,Double#} #-}
259 -- {-# GENERATE_SPECS indices a{~,Int#,Int,IPr} b{Int#,Double#} #-}
260 -- {-# GENERATE_SPECS elems a{~,Int#,Int,IPr} b{Int#,Double#} #-}
261 -- {-# GENERATE_SPECS assocs a{~,Int#,Int,IPr} b{Int#,Double#} #-}
262 -- {-# GENERATE_SPECS accumArray a{~,Int#,Int,IPr} b{Int#,Double#} c{Int#,Double#} #-}
263 -- {-# GENERATE_SPECS (//) a{~,Int#,Int,IPr} b{Int#,Double#} #-}
264 -- {-# GENERATE_SPECS accum a{~,Int#,Int,IPr} b{Int#,Double#} c{Int#,Double#} #-}
265 -- {-# GENERATE_SPECS amap a{~,Int#,Int,IPr} b{Int#,Double#} c{Int#,Double#} #-}
266 -- {-# GENERATE_SPECS ixmap a{~,Int#,Int} b{~,Int#,Int} c{Int#,Double#} #-}
268 -- {-# GENERATE_SPECS instance a{Int#} b{Int#,Double#} :: Eq (Array a b) #-}
269 -- {-# GENERATE_SPECS instance a{Int#} b{Int#,Double#} :: Ord (Array a b) #-}
270 -- {-# GENERATE_SPECS instance a{Int#} b{Int#,Double#} :: Text (Array a b) #-}
273 -- {-# GENERATE_SPECS instance a{Int} b{} :: Eq (Array a b) #-}
274 This raises the question of ambiguous specialised instances:
275 Which instance would be chosen for Array Int Int# ?
276 Array Int b or Array a Int# ?
278 -- {-# GENERATE_SPECS instance a{Int#} b{Int#,Double#} :: Eq (Assoc a b) #-}
279 -- {-# GENERATE_SPECS instance a{Int#} b{Int#,Double#} :: Ord (Assoc a b) #-}
280 -- {-# GENERATE_SPECS instance a{Int#} b{Int#,Double#} :: Ix (Assoc a b) #-}
281 -- {-# GENERATE_SPECS instance a{Int#} b{Int#,Double#} :: Text (Assoc a b) #-}