2 % (c) The AQUA Project, Glasgow University, 1994-1996
4 \section[PrelArr]{Module @PrelArr@}
6 Array implementation, @PrelArr@ exports the basic array
10 {-# OPTIONS -fno-implicit-prelude #-}
14 import {-# SOURCE #-} PrelErr ( error )
16 import PrelList (foldl)
27 {-# SPECIALISE array :: (Int,Int) -> [(Int,b)] -> Array Int b #-}
28 array :: (Ix a) => (a,a) -> [(a,b)] -> Array a b
30 {-# SPECIALISE (!) :: Array Int b -> Int -> b #-}
31 (!) :: (Ix a) => Array a b -> a -> b
33 {-# SPECIALISE bounds :: Array Int b -> (Int,Int) #-}
34 bounds :: (Ix a) => Array a b -> (a,a)
36 {-# SPECIALISE (//) :: Array Int b -> [(Int,b)] -> Array Int b #-}
37 (//) :: (Ix a) => Array a b -> [(a,b)] -> Array a b
39 {-# SPECIALISE accum :: (b -> c -> b) -> Array Int b -> [(Int,c)] -> Array Int b #-}
40 accum :: (Ix a) => (b -> c -> b) -> Array a b -> [(a,c)] -> Array a b
42 {-# SPECIALISE accumArray :: (b -> c -> b) -> b -> (Int,Int) -> [(Int,c)] -> Array Int b #-}
43 accumArray :: (Ix a) => (b -> c -> b) -> b -> (a,a) -> [(a,c)] -> Array a b
47 %*********************************************************
49 \subsection{The @Array@ types}
51 %*********************************************************
56 data Ix ix => Array ix elt = Array ix ix (Array# elt)
57 data Ix ix => ByteArray ix = ByteArray ix ix ByteArray#
58 data Ix ix => MutableArray s ix elt = MutableArray ix ix (MutableArray# s elt)
59 data Ix ix => MutableByteArray s ix = MutableByteArray ix ix (MutableByteArray# s)
61 instance CCallable (MutableByteArray s ix)
62 instance CCallable (ByteArray ix)
64 data MutableVar s a = MutableVar (MutVar# s a)
66 instance Eq (MutableVar s a) where
67 MutableVar v1# == MutableVar v2#
70 -- just pointer equality on arrays:
71 instance Eq (MutableArray s ix elt) where
72 MutableArray _ _ arr1# == MutableArray _ _ arr2#
73 = sameMutableArray# arr1# arr2#
75 instance Eq (MutableByteArray s ix) where
76 MutableByteArray _ _ arr1# == MutableByteArray _ _ arr2#
77 = sameMutableByteArray# arr1# arr2#
80 %*********************************************************
82 \subsection{Operations on mutable variables}
84 %*********************************************************
87 newVar :: a -> ST s (MutableVar s a)
88 readVar :: MutableVar s a -> ST s a
89 writeVar :: MutableVar s a -> a -> ST s ()
91 newVar init = ST $ \ s# ->
92 case (newMutVar# init s#) of { (# s2#, var# #) ->
93 (# s2#, MutableVar var# #) }
95 readVar (MutableVar var#) = ST $ \ s# -> readMutVar# var# s#
97 writeVar (MutableVar var#) val = ST $ \ s# ->
98 case writeMutVar# var# val s# of { s2# ->
102 %*********************************************************
104 \subsection{Operations on immutable arrays}
106 %*********************************************************
108 "array", "!" and "bounds" are basic; the rest can be defined in terms of them
111 bounds (Array l u _) = (l,u)
114 = let n# = case (index (l,u) i) of { I# x -> x } -- index fails if out of range
116 case (indexArray# arr# n#) of
121 = case rangeSize ixs of { I# n ->
123 case newArray# n arrEleBottom s1 of { (# s2, marr #) ->
124 foldr (fill ixs marr) (done ixs marr) ivs s2
127 fill :: Ix ix => (ix,ix) -> MutableArray# s elt
128 -> (ix,elt) -> STRep s a -> STRep s a
130 fill ixs marr (i,v) next = \s1 -> case index ixs i of { I# n ->
131 case writeArray# marr n v s1 of { s2 ->
134 done :: Ix ix => (ix,ix) -> MutableArray# s elt
135 -> STRep s (Array ix elt)
137 done (l,u) marr = \s1 ->
138 case unsafeFreezeArray# marr s1 of { (# s2, arr #) ->
139 (# s2, Array l u arr #) }
142 arrEleBottom = error "(Array.!): undefined array element"
145 -----------------------------------------------------------------------
146 -- These also go better with magic: (//), accum, accumArray
147 -- *** NB *** We INLINE them all so that their foldr's get to the call site
152 -- copy the old array:
153 arr <- thawArray old_array
154 -- now write the new elements into the new array:
159 fill_it_in :: Ix ix => MutableArray s ix elt -> [(ix, elt)] -> ST s ()
160 {-# INLINE fill_it_in #-}
161 fill_it_in arr lst = foldr (fill_one_in arr) (return ()) lst
162 -- **** STRICT **** (but that's OK...)
164 fill_one_in arr (i, v) rst = writeArray arr i v >> rst
166 zap_with_f :: Ix ix => (elt -> elt2 -> elt) -> MutableArray s ix elt -> [(ix,elt2)] -> ST s ()
167 -- zap_with_f: reads an elem out first, then uses "f" on that and the new value
168 {-# INLINE zap_with_f #-}
171 = foldr (zap_one f arr) (return ()) lst
173 zap_one f arr (i, new_v) rst = do
174 old_v <- readArray arr i
175 writeArray arr i (f old_v new_v)
179 accum f old_array ivs
181 -- copy the old array:
182 arr <- thawArray old_array
183 -- now zap the elements in question with "f":
188 {-# INLINE accumArray #-}
189 accumArray f zero ixs ivs
191 arr <- newArray ixs zero
198 %*********************************************************
200 \subsection{Operations on mutable arrays}
202 %*********************************************************
204 Idle ADR question: What's the tradeoff here between flattening these
205 datatypes into @MutableArray ix ix (MutableArray# s elt)@ and using
206 it as is? As I see it, the former uses slightly less heap and
207 provides faster access to the individual parts of the bounds while the
208 code used has the benefit of providing a ready-made @(lo, hi)@ pair as
209 required by many array-related functions. Which wins? Is the
210 difference significant (probably not).
212 Idle AJG answer: When I looked at the outputted code (though it was 2
213 years ago) it seems like you often needed the tuple, and we build
214 it frequently. Now we've got the overloading specialiser things
215 might be different, though.
218 newArray :: Ix ix => (ix,ix) -> elt -> ST s (MutableArray s ix elt)
219 newCharArray, newIntArray, newWordArray, newAddrArray, newFloatArray, newDoubleArray
220 :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
222 {-# SPECIALIZE newArray :: IPr -> elt -> ST s (MutableArray s Int elt),
223 (IPr,IPr) -> elt -> ST s (MutableArray s IPr elt)
225 {-# SPECIALIZE newCharArray :: IPr -> ST s (MutableByteArray s Int) #-}
226 {-# SPECIALIZE newIntArray :: IPr -> ST s (MutableByteArray s Int) #-}
227 {-# SPECIALIZE newWordArray :: IPr -> ST s (MutableByteArray s Int) #-}
228 {-# SPECIALIZE newAddrArray :: IPr -> ST s (MutableByteArray s Int) #-}
229 {-# SPECIALIZE newFloatArray :: IPr -> ST s (MutableByteArray s Int) #-}
230 {-# SPECIALIZE newDoubleArray :: IPr -> ST s (MutableByteArray s Int) #-}
232 newArray (l,u) init = ST $ \ s# ->
233 case rangeSize (l,u) of { I# n# ->
234 case (newArray# n# init s#) of { (# s2#, arr# #) ->
235 (# s2#, MutableArray l u arr# #) }}
237 newCharArray (l,u) = ST $ \ s# ->
238 case rangeSize (l,u) of { I# n# ->
239 case (newCharArray# n# s#) of { (# s2#, barr# #) ->
240 (# s2#, MutableByteArray l u barr# #) }}
242 newIntArray (l,u) = ST $ \ s# ->
243 case rangeSize (l,u) of { I# n# ->
244 case (newIntArray# n# s#) of { (# s2#, barr# #) ->
245 (# s2#, MutableByteArray l u barr# #) }}
247 newWordArray (l,u) = ST $ \ s# ->
248 case rangeSize (l,u) of { I# n# ->
249 case (newWordArray# n# s#) of { (# s2#, barr# #) ->
250 (# s2#, MutableByteArray l u barr# #) }}
252 newAddrArray (l,u) = ST $ \ s# ->
253 case rangeSize (l,u) of { I# n# ->
254 case (newAddrArray# n# s#) of { (# s2#, barr# #) ->
255 (# s2#, MutableByteArray l u barr# #) }}
257 newFloatArray (l,u) = ST $ \ s# ->
258 case rangeSize (l,u) of { I# n# ->
259 case (newFloatArray# n# s#) of { (# s2#, barr# #) ->
260 (# s2#, MutableByteArray l u barr# #) }}
262 newDoubleArray (l,u) = ST $ \ s# ->
263 case rangeSize (l,u) of { I# n# ->
264 case (newDoubleArray# n# s#) of { (# s2#, barr# #) ->
265 (# s2#, MutableByteArray l u barr# #) }}
267 boundsOfArray :: Ix ix => MutableArray s ix elt -> (ix, ix)
269 {-# SPECIALIZE boundsOfArray :: MutableArray s Int elt -> IPr #-}
271 boundsOfArray (MutableArray l u _) = (l,u)
273 readArray :: Ix ix => MutableArray s ix elt -> ix -> ST s elt
275 readCharArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Char
276 readIntArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Int
277 readWordArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Word
278 readAddrArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Addr
279 readFloatArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Float
280 readDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Double
282 {-# SPECIALIZE readArray :: MutableArray s Int elt -> Int -> ST s elt,
283 MutableArray s IPr elt -> IPr -> ST s elt
285 {-# SPECIALIZE readCharArray :: MutableByteArray s Int -> Int -> ST s Char #-}
286 {-# SPECIALIZE readIntArray :: MutableByteArray s Int -> Int -> ST s Int #-}
287 {-# SPECIALIZE readAddrArray :: MutableByteArray s Int -> Int -> ST s Addr #-}
288 --NO:{-# SPECIALIZE readFloatArray :: MutableByteArray s Int -> Int -> ST s Float #-}
289 {-# SPECIALIZE readDoubleArray :: MutableByteArray s Int -> Int -> ST s Double #-}
291 readArray (MutableArray l u arr#) n = ST $ \ s# ->
292 case (index (l,u) n) of { I# n# ->
293 case readArray# arr# n# s# of { (# s2#, r #) ->
296 readCharArray (MutableByteArray l u barr#) n = ST $ \ s# ->
297 case (index (l,u) n) of { I# n# ->
298 case readCharArray# barr# n# s# of { (# s2#, r# #) ->
301 readIntArray (MutableByteArray l u barr#) n = ST $ \ s# ->
302 case (index (l,u) n) of { I# n# ->
303 case readIntArray# barr# n# s# of { (# s2#, r# #) ->
306 readWordArray (MutableByteArray l u barr#) n = ST $ \ s# ->
307 case (index (l,u) n) of { I# n# ->
308 case readWordArray# barr# n# s# of { (# s2#, r# #) ->
311 readAddrArray (MutableByteArray l u barr#) n = ST $ \ s# ->
312 case (index (l,u) n) of { I# n# ->
313 case readAddrArray# barr# n# s# of { (# s2#, r# #) ->
316 readFloatArray (MutableByteArray l u barr#) n = ST $ \ s# ->
317 case (index (l,u) n) of { I# n# ->
318 case readFloatArray# barr# n# s# of { (# s2#, r# #) ->
321 readDoubleArray (MutableByteArray l u barr#) n = ST $ \ s# ->
322 case (index (l,u) n) of { I# n# ->
323 case readDoubleArray# barr# n# s# of { (# s2#, r# #) ->
326 --Indexing of ordinary @Arrays@ is standard Haskell and isn't defined here.
327 indexCharArray :: Ix ix => ByteArray ix -> ix -> Char
328 indexIntArray :: Ix ix => ByteArray ix -> ix -> Int
329 indexWordArray :: Ix ix => ByteArray ix -> ix -> Word
330 indexAddrArray :: Ix ix => ByteArray ix -> ix -> Addr
331 indexFloatArray :: Ix ix => ByteArray ix -> ix -> Float
332 indexDoubleArray :: Ix ix => ByteArray ix -> ix -> Double
334 {-# SPECIALIZE indexCharArray :: ByteArray Int -> Int -> Char #-}
335 {-# SPECIALIZE indexIntArray :: ByteArray Int -> Int -> Int #-}
336 {-# SPECIALIZE indexAddrArray :: ByteArray Int -> Int -> Addr #-}
337 --NO:{-# SPECIALIZE indexFloatArray :: ByteArray Int -> Int -> Float #-}
338 {-# SPECIALIZE indexDoubleArray :: ByteArray Int -> Int -> Double #-}
340 indexCharArray (ByteArray l u barr#) n
341 = case (index (l,u) n) of { I# n# ->
342 case indexCharArray# barr# n# of { r# ->
345 indexIntArray (ByteArray l u barr#) n
346 = case (index (l,u) n) of { I# n# ->
347 case indexIntArray# barr# n# of { r# ->
350 indexWordArray (ByteArray l u barr#) n
351 = case (index (l,u) n) of { I# n# ->
352 case indexWordArray# barr# n# of { r# ->
355 indexAddrArray (ByteArray l u barr#) n
356 = case (index (l,u) n) of { I# n# ->
357 case indexAddrArray# barr# n# of { r# ->
360 indexFloatArray (ByteArray l u barr#) n
361 = case (index (l,u) n) of { I# n# ->
362 case indexFloatArray# barr# n# of { r# ->
365 indexDoubleArray (ByteArray l u barr#) n
366 = case (index (l,u) n) of { I# n# ->
367 case indexDoubleArray# barr# n# of { r# ->
370 writeArray :: Ix ix => MutableArray s ix elt -> ix -> elt -> ST s ()
371 writeCharArray :: Ix ix => MutableByteArray s ix -> ix -> Char -> ST s ()
372 writeIntArray :: Ix ix => MutableByteArray s ix -> ix -> Int -> ST s ()
373 writeWordArray :: Ix ix => MutableByteArray s ix -> ix -> Word -> ST s ()
374 writeAddrArray :: Ix ix => MutableByteArray s ix -> ix -> Addr -> ST s ()
375 writeFloatArray :: Ix ix => MutableByteArray s ix -> ix -> Float -> ST s ()
376 writeDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> Double -> ST s ()
378 {-# SPECIALIZE writeArray :: MutableArray s Int elt -> Int -> elt -> ST s (),
379 MutableArray s IPr elt -> IPr -> elt -> ST s ()
381 {-# SPECIALIZE writeCharArray :: MutableByteArray s Int -> Int -> Char -> ST s () #-}
382 {-# SPECIALIZE writeIntArray :: MutableByteArray s Int -> Int -> Int -> ST s () #-}
383 {-# SPECIALIZE writeAddrArray :: MutableByteArray s Int -> Int -> Addr -> ST s () #-}
384 --NO:{-# SPECIALIZE writeFloatArray :: MutableByteArray s Int -> Int -> Float -> ST s () #-}
385 {-# SPECIALIZE writeDoubleArray :: MutableByteArray s Int -> Int -> Double -> ST s () #-}
387 writeArray (MutableArray l u arr#) n ele = ST $ \ s# ->
388 case index (l,u) n of { I# n# ->
389 case writeArray# arr# n# ele s# of { s2# ->
392 writeCharArray (MutableByteArray l u barr#) n (C# ele) = ST $ \ s# ->
393 case index (l,u) n of { I# n# ->
394 case writeCharArray# barr# n# ele s# of { s2# ->
397 writeIntArray (MutableByteArray l u barr#) n (I# ele) = ST $ \ s# ->
398 case index (l,u) n of { I# n# ->
399 case writeIntArray# barr# n# ele s# of { s2# ->
402 writeWordArray (MutableByteArray l u barr#) n (W# ele) = ST $ \ s# ->
403 case index (l,u) n of { I# n# ->
404 case writeWordArray# barr# n# ele s# of { s2# ->
407 writeAddrArray (MutableByteArray l u barr#) n (A# ele) = ST $ \ s# ->
408 case index (l,u) n of { I# n# ->
409 case writeAddrArray# barr# n# ele s# of { s2# ->
412 writeFloatArray (MutableByteArray l u barr#) n (F# ele) = ST $ \ s# ->
413 case index (l,u) n of { I# n# ->
414 case writeFloatArray# barr# n# ele s# of { s2# ->
417 writeDoubleArray (MutableByteArray l u barr#) n (D# ele) = ST $ \ s# ->
418 case index (l,u) n of { I# n# ->
419 case writeDoubleArray# barr# n# ele s# of { s2# ->
424 %*********************************************************
426 \subsection{Moving between mutable and immutable}
428 %*********************************************************
431 freezeArray :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
432 freezeCharArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
433 freezeIntArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
434 freezeWordArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
435 freezeAddrArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
437 {-# SPECIALISE freezeArray :: MutableArray s Int elt -> ST s (Array Int elt),
438 MutableArray s IPr elt -> ST s (Array IPr elt)
440 {-# SPECIALISE freezeCharArray :: MutableByteArray s Int -> ST s (ByteArray Int) #-}
442 freezeArray (MutableArray l u arr#) = ST $ \ s# ->
443 case rangeSize (l,u) of { I# n# ->
444 case freeze arr# n# s# of { (# s2#, frozen# #) ->
445 (# s2#, Array l u frozen# #) }}
447 freeze :: MutableArray# s ele -- the thing
448 -> Int# -- size of thing to be frozen
449 -> State# s -- the Universe and everything
450 -> (# State# s, Array# ele #)
452 = case newArray# n# init s# of { (# s2#, newarr1# #) ->
453 case copy 0# n# m_arr# newarr1# s2# of { (# s3#, newarr2# #) ->
454 unsafeFreezeArray# newarr2# s3#
457 init = error "freezeArray: element not copied"
460 -> MutableArray# s ele
461 -> MutableArray# s ele
463 -> (# State# s, MutableArray# s ele #)
465 copy cur# end# from# to# st#
469 = case readArray# from# cur# st# of { (# s1#, ele #) ->
470 case writeArray# to# cur# ele s1# of { s2# ->
471 copy (cur# +# 1#) end# from# to# s2#
474 freezeCharArray (MutableByteArray l u arr#) = ST $ \ s# ->
475 case rangeSize (l,u) of { I# n# ->
476 case freeze arr# n# s# of { (# s2#, frozen# #) ->
477 (# s2#, ByteArray l u frozen# #) }}
479 freeze :: MutableByteArray# s -- the thing
480 -> Int# -- size of thing to be frozen
481 -> State# s -- the Universe and everything
482 -> (# State# s, ByteArray# #)
485 = case (newCharArray# n# s1#) of { (# s2#, newarr1# #) ->
486 case copy 0# n# arr1# newarr1# s2# of { (# s3#, newarr2# #) ->
487 unsafeFreezeByteArray# newarr2# s3#
491 -> MutableByteArray# s -> MutableByteArray# s
493 -> (# State# s, MutableByteArray# s #)
495 copy cur# end# from# to# st#
499 = case (readCharArray# from# cur# st#) of { (# s2#, ele #) ->
500 case (writeCharArray# to# cur# ele s2#) of { s3# ->
501 copy (cur# +# 1#) end# from# to# s3#
504 freezeIntArray (MutableByteArray l u arr#) = ST $ \ s# ->
505 case rangeSize (l,u) of { I# n# ->
506 case freeze arr# n# s# of { (# s2#, frozen# #) ->
507 (# s2#, ByteArray l u frozen# #) }}
509 freeze :: MutableByteArray# s -- the thing
510 -> Int# -- size of thing to be frozen
511 -> State# s -- the Universe and everything
512 -> (# State# s, ByteArray# #)
515 = case (newIntArray# n# s#) of { (# s2#, newarr1# #) ->
516 case copy 0# n# m_arr# newarr1# s2# of { (# s3#, newarr2# #) ->
517 unsafeFreezeByteArray# newarr2# s3#
521 -> MutableByteArray# s -> MutableByteArray# s
523 -> (# State# s, MutableByteArray# s #)
525 copy cur# end# from# to# s1#
529 = case (readIntArray# from# cur# s1#) of { (# s2#, ele #) ->
530 case (writeIntArray# to# cur# ele s2#) of { s3# ->
531 copy (cur# +# 1#) end# from# to# s3#
534 freezeWordArray (MutableByteArray l u arr#) = ST $ \ s# ->
535 case rangeSize (l,u) of { I# n# ->
536 case freeze arr# n# s# of { (# s2#, frozen# #) ->
537 (# s2#, ByteArray l u frozen# #) }}
539 freeze :: MutableByteArray# s -- the thing
540 -> Int# -- size of thing to be frozen
541 -> State# s -- the Universe and everything
542 -> (# State# s, ByteArray# #)
545 = case (newWordArray# n# s1#) of { (# s2#, newarr1# #) ->
546 case copy 0# n# m_arr# newarr1# s2# of { (# s3#, newarr2# #) ->
547 unsafeFreezeByteArray# newarr2# s3#
551 -> MutableByteArray# s -> MutableByteArray# s
553 -> (# State# s, MutableByteArray# s #)
555 copy cur# end# from# to# st#
556 | cur# ==# end# = (# st#, to# #)
558 case (readWordArray# from# cur# st#) of { (# s2#, ele #) ->
559 case (writeWordArray# to# cur# ele s2#) of { s3# ->
560 copy (cur# +# 1#) end# from# to# s3#
563 freezeAddrArray (MutableByteArray l u arr#) = ST $ \ s# ->
564 case rangeSize (l,u) of { I# n# ->
565 case freeze arr# n# s# of { (# s2#, frozen# #) ->
566 (# s2#, ByteArray l u frozen# #) }}
568 freeze :: MutableByteArray# s -- the thing
569 -> Int# -- size of thing to be frozen
570 -> State# s -- the Universe and everything
571 -> (# State# s, ByteArray# #)
574 = case (newAddrArray# n# s1#) of { (# s2#, newarr1# #) ->
575 case copy 0# n# m_arr# newarr1# s2# of { (# s3#, newarr2# #) ->
576 unsafeFreezeByteArray# newarr2# s3#
580 -> MutableByteArray# s -> MutableByteArray# s
582 -> (# State# s, MutableByteArray# s #)
584 copy cur# end# from# to# st#
588 = case (readAddrArray# from# cur# st#) of { (# st1#, ele #) ->
589 case (writeAddrArray# to# cur# ele st1#) of { st2# ->
590 copy (cur# +# 1#) end# from# to# st2#
593 unsafeFreezeArray :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
594 unsafeFreezeByteArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
596 {-# SPECIALIZE unsafeFreezeByteArray :: MutableByteArray s Int -> ST s (ByteArray Int)
599 unsafeFreezeArray (MutableArray l u arr#) = ST $ \ s# ->
600 case unsafeFreezeArray# arr# s# of { (# s2#, frozen# #) ->
601 (# s2#, Array l u frozen# #) }
603 unsafeFreezeByteArray (MutableByteArray l u arr#) = ST $ \ s# ->
604 case unsafeFreezeByteArray# arr# s# of { (# s2#, frozen# #) ->
605 (# s2#, ByteArray l u frozen# #) }
608 --This takes a immutable array, and copies it into a mutable array, in a
611 {-# SPECIALISE thawArray :: Array Int elt -> ST s (MutableArray s Int elt),
612 Array IPr elt -> ST s (MutableArray s IPr elt)
615 thawArray :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
616 thawArray (Array l u arr#) = ST $ \ s# ->
617 case rangeSize (l,u) of { I# n# ->
618 case thaw arr# n# s# of { (# s2#, thawed# #) ->
619 (# s2#, MutableArray l u thawed# #)}}
621 thaw :: Array# ele -- the thing
622 -> Int# -- size of thing to be thawed
623 -> State# s -- the Universe and everything
624 -> (# State# s, MutableArray# s ele #)
627 = case newArray# n# init s# of { (# s2#, newarr1# #) ->
628 copy 0# n# arr1# newarr1# s2# }
630 init = error "thawArray: element not copied"
634 -> MutableArray# s ele
636 -> (# State# s, MutableArray# s ele #)
638 copy cur# end# from# to# st#
642 = case indexArray# from# cur# of { (# ele #) ->
643 case writeArray# to# cur# ele st# of { s1# ->
644 copy (cur# +# 1#) end# from# to# s1#
647 -- this is a quicker version of the above, just flipping the type
648 -- (& representation) of an immutable array. And placing a
649 -- proof obligation on the programmer.
650 unsafeThawArray :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
651 unsafeThawArray (Array l u arr#) = ST $ \ s# ->
652 case unsafeThawArray# arr# s# of
653 (# s2#, marr# #) -> (# s2#, MutableArray l u marr# #)