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 (MutableByteArray# s)
64 instance CCallable (ByteArray ix)
65 instance CCallable ByteArray#
67 data MutableVar s a = MutableVar (MutVar# s a)
69 instance Eq (MutableVar s a) where
70 MutableVar v1# == MutableVar v2#
73 -- just pointer equality on arrays:
74 instance Eq (MutableArray s ix elt) where
75 MutableArray _ arr1# == MutableArray _ arr2#
76 = sameMutableArray# arr1# arr2#
78 instance Eq (MutableByteArray s ix) where
79 MutableByteArray _ arr1# == MutableByteArray _ arr2#
80 = sameMutableByteArray# arr1# arr2#
83 %*********************************************************
85 \subsection{Operations on mutable variables}
87 %*********************************************************
90 newVar :: a -> ST s (MutableVar s a)
91 readVar :: MutableVar s a -> ST s a
92 writeVar :: MutableVar s a -> a -> ST s ()
94 newVar init = ST $ \ s# ->
95 case (newMutVar# init s#) of { (# s2#, var# #) ->
96 (# s2#, MutableVar var# #) }
98 readVar (MutableVar var#) = ST $ \ s# -> readMutVar# var# s#
100 writeVar (MutableVar var#) val = ST $ \ s# ->
101 case writeMutVar# var# val s# of { s2# ->
105 %*********************************************************
107 \subsection{Operations on immutable arrays}
109 %*********************************************************
111 "array", "!" and "bounds" are basic; the rest can be defined in terms of them
114 bounds (Array b _) = b
116 (Array bounds arr#) ! i
117 = let n# = case (index bounds i) of { I# x -> x } -- index fails if out of range
119 case (indexArray# arr# n#) of
124 = case rangeSize ixs of { I# n ->
126 case newArray# n arrEleBottom s1 of { (# s2, marr #) ->
127 foldr (fill ixs marr) (done ixs marr) ivs s2
130 fill :: Ix ix => (ix,ix) -> MutableArray# s elt
131 -> (ix,elt) -> STRep s a -> STRep s a
133 fill ixs marr (i,v) next = \s1 -> case index ixs i of { I# n ->
134 case writeArray# marr n v s1 of { s2 ->
137 done :: Ix ix => (ix,ix) -> MutableArray# s elt
138 -> STRep s (Array ix elt)
140 done ixs marr = \s1 -> case unsafeFreezeArray# marr s1 of { (# s2, arr #) ->
141 (# s2, Array ixs arr #) }
144 arrEleBottom = error "(Array.!): undefined array element"
147 -----------------------------------------------------------------------
148 -- these also go better with magic: (//), accum, accumArray
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 ()
161 = foldr fill_one_in (return ()) lst
162 where -- **** STRICT **** (but that's OK...)
163 fill_one_in (i, v) rst
164 = 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
170 = foldr zap_one (return ()) lst
172 zap_one (i, new_v) rst = do
173 old_v <- readArray arr i
174 writeArray arr i (f old_v new_v)
177 accum f old_array ivs
179 -- copy the old array:
180 arr <- thawArray old_array
181 -- now zap the elements in question with "f":
186 accumArray f zero ixs ivs
188 arr# <- newArray ixs zero
189 zap_with_f f arr# ivs
195 %*********************************************************
197 \subsection{Operations on mutable arrays}
199 %*********************************************************
201 Idle ADR question: What's the tradeoff here between flattening these
202 datatypes into @MutableArray ix ix (MutableArray# s elt)@ and using
203 it as is? As I see it, the former uses slightly less heap and
204 provides faster access to the individual parts of the bounds while the
205 code used has the benefit of providing a ready-made @(lo, hi)@ pair as
206 required by many array-related functions. Which wins? Is the
207 difference significant (probably not).
209 Idle AJG answer: When I looked at the outputted code (though it was 2
210 years ago) it seems like you often needed the tuple, and we build
211 it frequently. Now we've got the overloading specialiser things
212 might be different, though.
215 newArray :: Ix ix => (ix,ix) -> elt -> ST s (MutableArray s ix elt)
216 newCharArray, newIntArray, newWordArray, newAddrArray, newFloatArray, newDoubleArray
217 :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
219 {-# SPECIALIZE newArray :: IPr -> elt -> ST s (MutableArray s Int elt),
220 (IPr,IPr) -> elt -> ST s (MutableArray s IPr elt)
222 {-# SPECIALIZE newCharArray :: IPr -> ST s (MutableByteArray s Int) #-}
223 {-# SPECIALIZE newIntArray :: IPr -> ST s (MutableByteArray s Int) #-}
224 {-# SPECIALIZE newWordArray :: IPr -> ST s (MutableByteArray s Int) #-}
225 {-# SPECIALIZE newAddrArray :: IPr -> ST s (MutableByteArray s Int) #-}
226 {-# SPECIALIZE newFloatArray :: IPr -> ST s (MutableByteArray s Int) #-}
227 {-# SPECIALIZE newDoubleArray :: IPr -> ST s (MutableByteArray s Int) #-}
229 newArray ixs init = ST $ \ s# ->
230 case rangeSize ixs of { I# n# ->
231 case (newArray# n# init s#) of { (# s2#, arr# #) ->
232 (# s2#, MutableArray ixs arr# #) }}
234 newCharArray ixs = ST $ \ s# ->
235 case rangeSize ixs of { I# n# ->
236 case (newCharArray# n# s#) of { (# s2#, barr# #) ->
237 (# s2#, MutableByteArray ixs barr# #) }}
239 newIntArray ixs = ST $ \ s# ->
240 case rangeSize ixs of { I# n# ->
241 case (newIntArray# n# s#) of { (# s2#, barr# #) ->
242 (# s2#, MutableByteArray ixs barr# #) }}
244 newWordArray ixs = ST $ \ s# ->
245 case rangeSize ixs of { I# n# ->
246 case (newWordArray# n# s#) of { (# s2#, barr# #) ->
247 (# s2#, MutableByteArray ixs barr# #) }}
249 newAddrArray ixs = ST $ \ s# ->
250 case rangeSize ixs of { I# n# ->
251 case (newAddrArray# n# s#) of { (# s2#, barr# #) ->
252 (# s2#, MutableByteArray ixs barr# #) }}
254 newFloatArray ixs = ST $ \ s# ->
255 case rangeSize ixs of { I# n# ->
256 case (newFloatArray# n# s#) of { (# s2#, barr# #) ->
257 (# s2#, MutableByteArray ixs barr# #) }}
259 newDoubleArray ixs = ST $ \ s# ->
260 case rangeSize ixs of { I# n# ->
261 case (newDoubleArray# n# s#) of { (# s2#, barr# #) ->
262 (# s2#, MutableByteArray ixs barr# #) }}
264 boundsOfArray :: Ix ix => MutableArray s ix elt -> (ix, ix)
266 {-# SPECIALIZE boundsOfArray :: MutableArray s Int elt -> IPr #-}
268 boundsOfArray (MutableArray ixs _) = ixs
270 readArray :: Ix ix => MutableArray s ix elt -> ix -> ST s elt
272 readCharArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Char
273 readIntArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Int
274 readWordArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Word
275 readAddrArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Addr
276 readFloatArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Float
277 readDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Double
279 {-# SPECIALIZE readArray :: MutableArray s Int elt -> Int -> ST s elt,
280 MutableArray s IPr elt -> IPr -> ST s elt
282 {-# SPECIALIZE readCharArray :: MutableByteArray s Int -> Int -> ST s Char #-}
283 {-# SPECIALIZE readIntArray :: MutableByteArray s Int -> Int -> ST s Int #-}
284 {-# SPECIALIZE readAddrArray :: MutableByteArray s Int -> Int -> ST s Addr #-}
285 --NO:{-# SPECIALIZE readFloatArray :: MutableByteArray s Int -> Int -> ST s Float #-}
286 {-# SPECIALIZE readDoubleArray :: MutableByteArray s Int -> Int -> ST s Double #-}
288 readArray (MutableArray ixs arr#) n = ST $ \ s# ->
289 case (index ixs n) of { I# n# ->
290 case readArray# arr# n# s# of { (# s2#, r #) ->
293 readCharArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
294 case (index ixs n) of { I# n# ->
295 case readCharArray# barr# n# s# of { (# s2#, r# #) ->
298 readIntArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
299 case (index ixs n) of { I# n# ->
300 case readIntArray# barr# n# s# of { (# s2#, r# #) ->
303 readWordArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
304 case (index ixs n) of { I# n# ->
305 case readWordArray# barr# n# s# of { (# s2#, r# #) ->
308 readAddrArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
309 case (index ixs n) of { I# n# ->
310 case readAddrArray# barr# n# s# of { (# s2#, r# #) ->
313 readFloatArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
314 case (index ixs n) of { I# n# ->
315 case readFloatArray# barr# n# s# of { (# s2#, r# #) ->
318 readDoubleArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
319 case (index ixs n) of { I# n# ->
320 case readDoubleArray# barr# n# s# of { (# s2#, r# #) ->
323 --Indexing of ordinary @Arrays@ is standard Haskell and isn't defined here.
324 indexCharArray :: Ix ix => ByteArray ix -> ix -> Char
325 indexIntArray :: Ix ix => ByteArray ix -> ix -> Int
326 indexWordArray :: Ix ix => ByteArray ix -> ix -> Word
327 indexAddrArray :: Ix ix => ByteArray ix -> ix -> Addr
328 indexFloatArray :: Ix ix => ByteArray ix -> ix -> Float
329 indexDoubleArray :: Ix ix => ByteArray ix -> ix -> Double
331 {-# SPECIALIZE indexCharArray :: ByteArray Int -> Int -> Char #-}
332 {-# SPECIALIZE indexIntArray :: ByteArray Int -> Int -> Int #-}
333 {-# SPECIALIZE indexAddrArray :: ByteArray Int -> Int -> Addr #-}
334 --NO:{-# SPECIALIZE indexFloatArray :: ByteArray Int -> Int -> Float #-}
335 {-# SPECIALIZE indexDoubleArray :: ByteArray Int -> Int -> Double #-}
337 indexCharArray (ByteArray ixs barr#) n
338 = case (index ixs n) of { I# n# ->
339 case indexCharArray# barr# n# of { r# ->
342 indexIntArray (ByteArray ixs barr#) n
343 = case (index ixs n) of { I# n# ->
344 case indexIntArray# barr# n# of { r# ->
347 indexWordArray (ByteArray ixs barr#) n
348 = case (index ixs n) of { I# n# ->
349 case indexWordArray# barr# n# of { r# ->
352 indexAddrArray (ByteArray ixs barr#) n
353 = case (index ixs n) of { I# n# ->
354 case indexAddrArray# barr# n# of { r# ->
357 indexFloatArray (ByteArray ixs barr#) n
358 = case (index ixs n) of { I# n# ->
359 case indexFloatArray# barr# n# of { r# ->
362 indexDoubleArray (ByteArray ixs barr#) n
363 = case (index ixs n) of { I# n# ->
364 case indexDoubleArray# barr# n# of { r# ->
367 writeArray :: Ix ix => MutableArray s ix elt -> ix -> elt -> ST s ()
368 writeCharArray :: Ix ix => MutableByteArray s ix -> ix -> Char -> ST s ()
369 writeIntArray :: Ix ix => MutableByteArray s ix -> ix -> Int -> ST s ()
370 writeWordArray :: Ix ix => MutableByteArray s ix -> ix -> Word -> ST s ()
371 writeAddrArray :: Ix ix => MutableByteArray s ix -> ix -> Addr -> ST s ()
372 writeFloatArray :: Ix ix => MutableByteArray s ix -> ix -> Float -> ST s ()
373 writeDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> Double -> ST s ()
375 {-# SPECIALIZE writeArray :: MutableArray s Int elt -> Int -> elt -> ST s (),
376 MutableArray s IPr elt -> IPr -> elt -> ST s ()
378 {-# SPECIALIZE writeCharArray :: MutableByteArray s Int -> Int -> Char -> ST s () #-}
379 {-# SPECIALIZE writeIntArray :: MutableByteArray s Int -> Int -> Int -> ST s () #-}
380 {-# SPECIALIZE writeAddrArray :: MutableByteArray s Int -> Int -> Addr -> ST s () #-}
381 --NO:{-# SPECIALIZE writeFloatArray :: MutableByteArray s Int -> Int -> Float -> ST s () #-}
382 {-# SPECIALIZE writeDoubleArray :: MutableByteArray s Int -> Int -> Double -> ST s () #-}
384 writeArray (MutableArray ixs arr#) n ele = ST $ \ s# ->
385 case index ixs n of { I# n# ->
386 case writeArray# arr# n# ele s# of { s2# ->
389 writeCharArray (MutableByteArray ixs barr#) n (C# ele) = ST $ \ s# ->
390 case (index ixs n) of { I# n# ->
391 case writeCharArray# barr# n# ele s# of { s2# ->
394 writeIntArray (MutableByteArray ixs barr#) n (I# ele) = ST $ \ s# ->
395 case (index ixs n) of { I# n# ->
396 case writeIntArray# barr# n# ele s# of { s2# ->
399 writeWordArray (MutableByteArray ixs barr#) n (W# ele) = ST $ \ s# ->
400 case (index ixs n) of { I# n# ->
401 case writeWordArray# barr# n# ele s# of { s2# ->
404 writeAddrArray (MutableByteArray ixs barr#) n (A# ele) = ST $ \ s# ->
405 case (index ixs n) of { I# n# ->
406 case writeAddrArray# barr# n# ele s# of { s2# ->
409 writeFloatArray (MutableByteArray ixs barr#) n (F# ele) = ST $ \ s# ->
410 case (index ixs n) of { I# n# ->
411 case writeFloatArray# barr# n# ele s# of { s2# ->
414 writeDoubleArray (MutableByteArray ixs barr#) n (D# ele) = ST $ \ s# ->
415 case (index ixs n) of { I# n# ->
416 case writeDoubleArray# barr# n# ele s# of { s2# ->
421 %*********************************************************
423 \subsection{Moving between mutable and immutable}
425 %*********************************************************
428 freezeArray :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
429 freezeCharArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
430 freezeIntArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
431 freezeWordArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
432 freezeAddrArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
434 {-# SPECIALISE freezeArray :: MutableArray s Int elt -> ST s (Array Int elt),
435 MutableArray s IPr elt -> ST s (Array IPr elt)
437 {-# SPECIALISE freezeCharArray :: MutableByteArray s Int -> ST s (ByteArray Int) #-}
439 freezeArray (MutableArray ixs arr#) = ST $ \ s# ->
440 case rangeSize ixs of { I# n# ->
441 case freeze arr# n# s# of { (# s2#, frozen# #) ->
442 (# s2#, Array ixs frozen# #) }}
444 freeze :: MutableArray# s ele -- the thing
445 -> Int# -- size of thing to be frozen
446 -> State# s -- the Universe and everything
447 -> (# State# s, Array# ele #)
449 = case newArray# n# init s# of { (# s2#, newarr1# #) ->
450 case copy 0# n# m_arr# newarr1# s2# of { (# s3#, newarr2# #) ->
451 unsafeFreezeArray# newarr2# s3#
454 init = error "freezeArray: element not copied"
457 -> MutableArray# s ele
458 -> MutableArray# s ele
460 -> (# State# s, MutableArray# s ele #)
462 copy cur# end# from# to# st#
466 = case readArray# from# cur# st# of { (# s1#, ele #) ->
467 case writeArray# to# cur# ele s1# of { s2# ->
468 copy (cur# +# 1#) end# from# to# s2#
471 freezeCharArray (MutableByteArray ixs arr#) = ST $ \ s# ->
472 case rangeSize ixs of { I# n# ->
473 case freeze arr# n# s# of { (# s2#, frozen# #) ->
474 (# s2#, ByteArray ixs frozen# #) }}
476 freeze :: MutableByteArray# s -- the thing
477 -> Int# -- size of thing to be frozen
478 -> State# s -- the Universe and everything
479 -> (# State# s, ByteArray# #)
482 = case (newCharArray# n# s1#) of { (# s2#, newarr1# #) ->
483 case copy 0# n# arr1# newarr1# s2# of { (# s3#, newarr2# #) ->
484 unsafeFreezeByteArray# newarr2# s3#
488 -> MutableByteArray# s -> MutableByteArray# s
490 -> (# State# s, MutableByteArray# s #)
492 copy cur# end# from# to# st#
496 = case (readCharArray# from# cur# st#) of { (# s2#, ele #) ->
497 case (writeCharArray# to# cur# ele s2#) of { s3# ->
498 copy (cur# +# 1#) end# from# to# s3#
501 freezeIntArray (MutableByteArray ixs arr#) = ST $ \ s# ->
502 case rangeSize ixs of { I# n# ->
503 case freeze arr# n# s# of { (# s2#, frozen# #) ->
504 (# s2#, ByteArray ixs frozen# #) }}
506 freeze :: MutableByteArray# s -- the thing
507 -> Int# -- size of thing to be frozen
508 -> State# s -- the Universe and everything
509 -> (# State# s, ByteArray# #)
512 = case (newIntArray# n# s#) of { (# s2#, newarr1# #) ->
513 case copy 0# n# m_arr# newarr1# s2# of { (# s3#, newarr2# #) ->
514 unsafeFreezeByteArray# newarr2# s3#
518 -> MutableByteArray# s -> MutableByteArray# s
520 -> (# State# s, MutableByteArray# s #)
522 copy cur# end# from# to# s1#
526 = case (readIntArray# from# cur# s1#) of { (# s2#, ele #) ->
527 case (writeIntArray# to# cur# ele s2#) of { s3# ->
528 copy (cur# +# 1#) end# from# to# s3#
531 freezeWordArray (MutableByteArray ixs arr#) = ST $ \ s# ->
532 case rangeSize ixs of { I# n# ->
533 case freeze arr# n# s# of { (# s2#, frozen# #) ->
534 (# s2#, ByteArray ixs frozen# #) }}
536 freeze :: MutableByteArray# s -- the thing
537 -> Int# -- size of thing to be frozen
538 -> State# s -- the Universe and everything
539 -> (# State# s, ByteArray# #)
542 = case (newWordArray# n# s1#) of { (# s2#, newarr1# #) ->
543 case copy 0# n# m_arr# newarr1# s2# of { (# s3#, newarr2# #) ->
544 unsafeFreezeByteArray# newarr2# s3#
548 -> MutableByteArray# s -> MutableByteArray# s
550 -> (# State# s, MutableByteArray# s #)
552 copy cur# end# from# to# st#
553 | cur# ==# end# = (# st#, to# #)
555 case (readWordArray# from# cur# st#) of { (# s2#, ele #) ->
556 case (writeWordArray# to# cur# ele s2#) of { s3# ->
557 copy (cur# +# 1#) end# from# to# s3#
560 freezeAddrArray (MutableByteArray ixs arr#) = ST $ \ s# ->
561 case rangeSize ixs of { I# n# ->
562 case freeze arr# n# s# of { (# s2#, frozen# #) ->
563 (# s2#, ByteArray ixs frozen# #) }}
565 freeze :: MutableByteArray# s -- the thing
566 -> Int# -- size of thing to be frozen
567 -> State# s -- the Universe and everything
568 -> (# State# s, ByteArray# #)
571 = case (newAddrArray# n# s1#) of { (# s2#, newarr1# #) ->
572 case copy 0# n# m_arr# newarr1# s2# of { (# s3#, newarr2# #) ->
573 unsafeFreezeByteArray# newarr2# s3#
577 -> MutableByteArray# s -> MutableByteArray# s
579 -> (# State# s, MutableByteArray# s #)
581 copy cur# end# from# to# st#
585 = case (readAddrArray# from# cur# st#) of { (# st1#, ele #) ->
586 case (writeAddrArray# to# cur# ele st1#) of { st2# ->
587 copy (cur# +# 1#) end# from# to# st2#
590 unsafeFreezeArray :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
591 unsafeFreezeByteArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
593 {-# SPECIALIZE unsafeFreezeByteArray :: MutableByteArray s Int -> ST s (ByteArray Int)
596 unsafeFreezeArray (MutableArray ixs arr#) = ST $ \ s# ->
597 case unsafeFreezeArray# arr# s# of { (# s2#, frozen# #) ->
598 (# s2#, Array ixs frozen# #) }
600 unsafeFreezeByteArray (MutableByteArray ixs arr#) = ST $ \ s# ->
601 case unsafeFreezeByteArray# arr# s# of { (# s2#, frozen# #) ->
602 (# s2#, ByteArray ixs frozen# #) }
605 --This takes a immutable array, and copies it into a mutable array, in a
608 {-# SPECIALISE thawArray :: Array Int elt -> ST s (MutableArray s Int elt),
609 Array IPr elt -> ST s (MutableArray s IPr elt)
612 thawArray :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
613 thawArray (Array ixs arr#) = ST $ \ s# ->
614 case rangeSize ixs of { I# n# ->
615 case thaw arr# n# s# of { (# s2#, thawed# #) ->
616 (# s2#, MutableArray ixs thawed# #)}}
618 thaw :: Array# ele -- the thing
619 -> Int# -- size of thing to be thawed
620 -> State# s -- the Universe and everything
621 -> (# State# s, MutableArray# s ele #)
624 = case newArray# n# init s# of { (# s2#, newarr1# #) ->
625 copy 0# n# arr1# newarr1# s2# }
627 init = error "thawArray: element not copied"
631 -> MutableArray# s ele
633 -> (# State# s, MutableArray# s ele #)
635 copy cur# end# from# to# st#
639 = case indexArray# from# cur# of { (# ele #) ->
640 case writeArray# to# cur# ele st# of { s1# ->
641 copy (cur# +# 1#) end# from# to# s1#
644 -- this is a quicker version of the above, just flipping the type
645 -- (& representation) of an immutable array. And placing a
646 -- proof obligation on the programmer.
647 unsafeThawArray :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
648 unsafeThawArray (Array ixs arr#) = ST $ \ s# ->
649 case unsafeThawArray# arr# s# of
650 (# s2#, marr# #) -> (# s2#, MutableArray ixs marr# #)