2 % (c) The AQUA Project, Glasgow University, 1994
4 \section[PreludeGlaST]{Basic ``state transformer'' monad, mutable arrays and variables}
6 See state-interface.verb, from which this is taken directly.
9 #include "../../includes/platform.h"
10 #include "../../includes/GhcConstants.h"
15 _MutableByteArray(..),
16 ST(..), -- it's a known GHC infelicity that synonyms must
17 MutableVar(..), -- be listed separately.
19 --!! because this interface is now the "everything state-transformer"ish
20 --!! interface, here is all the PreludePrimIO stuff
22 -- PrimIO(..): no, the compiler already knows about it
32 unsafeInterleavePrimIO,
35 -- all the Stdio stuff (this is how you get to it)
37 fclose, fdopen, fflush, fopen, fread, freopen,
40 -- backward compatibility -- don't use!
46 --!! end of PreludePrimIO
48 _ByteArray(..), Array(..) -- reexport *unabstractly*
51 import PreludePrimIO (
60 unsafeInterleavePrimIO,
73 import List ( map, null, foldr, (++) )
74 import PS ( _PackedString, _unpackPS )
75 import TyArray ( Array(..), _ByteArray(..) )
78 infixr 9 `thenST`, `thenStrictlyST`, `seqST`, `seqStrictlyST`
83 %************************************************************************
85 \subsection[PreludeGlaST-ST-monad]{The state-transformer proper}
87 %************************************************************************
90 --BUILT-IN: type _ST s a -- State transformer
92 type ST s a = _ST s a -- so you don't need -fglasgow-exts
94 {-# INLINE returnST #-}
95 {-# INLINE returnStrictlyST #-}
96 {-# INLINE thenStrictlyST #-}
97 {-# INLINE seqStrictlyST #-}
99 returnST, returnStrictlyST :: a -> _ST s a
100 returnST a s = (a, s)
102 thenST, thenStrictlyST :: _ST s a -> (a -> _ST s b) -> _ST s b
103 thenST m k s = let (r,new_s) = m s
107 fixST :: (a -> _ST s a) -> _ST s a
108 fixST k s = let ans = k r s
113 -- BUILT-IN: _runST (see Builtin.hs)
115 unsafeInterleaveST :: _ST s a -> _ST s a -- ToDo: put in state-interface.tex
117 unsafeInterleaveST m s
123 seqST, seqStrictlyST :: _ST s a -> _ST s b -> _ST s b
124 seqST m1 m2 = m1 `thenST` (\ _ -> m2)
126 returnStrictlyST a s@(S# _) = (a, s)
128 thenStrictlyST m k s -- @(S# _) Omitted SLPJ [May95] no need to evaluate the state
129 = case (m s) of { (r, new_s) ->
132 seqStrictlyST m k s -- @(S# _) Omitted SLPJ [May95] no need to evaluate the state
133 = case (m s) of { (_, new_s) ->
136 listST :: [_ST s a] -> _ST s [a]
138 listST [] = returnST []
139 listST (m:ms) = m `thenST` \ x ->
140 listST ms `thenST` \ xs ->
143 mapST :: (a -> _ST s b) -> [a] -> _ST s [b]
144 mapST f ms = listST (map f ms)
146 mapAndUnzipST :: (a -> _ST s (b,c)) -> [a] -> _ST s ([b],[c])
147 mapAndUnzipST f [] = returnST ([], [])
148 mapAndUnzipST f (m:ms)
149 = f m `thenST` \ ( r1, r2) ->
150 mapAndUnzipST f ms `thenST` \ (rs1, rs2) ->
151 returnST (r1:rs1, r2:rs2)
154 forkST :: ST s () -> ST s ()
156 #ifndef __CONCURRENT_HASKELL__
162 (_, new_s) = action s
164 new_s `_fork_` ((), s)
166 _fork_ x y = case (fork# x) of { 0# -> parError#; _ -> y }
168 #endif {- __CONCURRENT_HASKELL__ -}
170 forkPrimIO :: PrimIO () -> PrimIO ()
174 %************************************************************************
176 \subsection[PreludeGlaST-arrays]{Mutable arrays}
178 %************************************************************************
180 Idle ADR question: What's the tradeoff here between flattening these
181 datatypes into @_MutableArray ix ix (MutableArray# s elt)@ and using
182 it as is? As I see it, the former uses slightly less heap and
183 provides faster access to the individual parts of the bounds while the
184 code used has the benefit of providing a ready-made @(lo, hi)@ pair as
185 required by many array-related functions. Which wins? Is the
186 difference significant (probably not).
188 Idle AJG answer: When I looked at the outputted code (though it was 2
189 years ago) it seems like you often needed the tuple, and we build
190 it frequently. Now we've got the overloading specialiser things
191 might be different, though.
194 data _MutableArray s ix elt = _MutableArray (ix,ix) (MutableArray# s elt)
195 data _MutableByteArray s ix = _MutableByteArray (ix,ix) (MutableByteArray# s)
197 instance _CCallable (_MutableByteArray s ix)
201 newArray :: Ix ix => (ix,ix) -> elt -> _ST s (_MutableArray s ix elt)
202 newCharArray, newIntArray, newAddrArray, newFloatArray, newDoubleArray
203 :: Ix ix => (ix,ix) -> _ST s (_MutableByteArray s ix)
205 {-# SPECIALIZE newArray :: IPr -> elt -> _ST s (_MutableArray s Int elt),
206 (IPr,IPr) -> elt -> _ST s (_MutableArray s IPr elt)
208 {-# SPECIALIZE newCharArray :: IPr -> _ST s (_MutableByteArray s Int) #-}
209 {-# SPECIALIZE newIntArray :: IPr -> _ST s (_MutableByteArray s Int) #-}
210 {-# SPECIALIZE newAddrArray :: IPr -> _ST s (_MutableByteArray s Int) #-}
211 --NO:{-# SPECIALIZE newFloatArray :: IPr -> _ST s (_MutableByteArray s Int) #-}
212 {-# SPECIALIZE newDoubleArray :: IPr -> _ST s (_MutableByteArray s Int) #-}
214 newArray ixs@(ix_start, ix_end) init (S# s#)
215 = let n# = case (if null (range ixs)
217 else (index ixs ix_end) + 1) of { I# x -> x }
218 -- size is one bigger than index of last elem
220 case (newArray# n# init s#) of { StateAndMutableArray# s2# arr# ->
221 (_MutableArray ixs arr#, S# s2#)}
223 newCharArray ixs@(ix_start, ix_end) (S# s#)
224 = let n# = case (if null (range ixs)
226 else ((index ixs ix_end) + 1)) of { I# x -> x }
228 case (newCharArray# n# s#) of { StateAndMutableByteArray# s2# barr# ->
229 (_MutableByteArray ixs barr#, S# s2#)}
231 newIntArray ixs@(ix_start, ix_end) (S# s#)
232 = let n# = case (if null (range ixs)
234 else ((index ixs ix_end) + 1)) of { I# x -> x }
236 case (newIntArray# n# s#) of { StateAndMutableByteArray# s2# barr# ->
237 (_MutableByteArray ixs barr#, S# s2#)}
239 newAddrArray ixs@(ix_start, ix_end) (S# s#)
240 = let n# = case (if null (range ixs)
242 else ((index ixs ix_end) + 1)) of { I# x -> x }
244 case (newAddrArray# n# s#) of { StateAndMutableByteArray# s2# barr# ->
245 (_MutableByteArray ixs barr#, S# s2#)}
247 newFloatArray ixs@(ix_start, ix_end) (S# s#)
248 = let n# = case (if null (range ixs)
250 else ((index ixs ix_end) + 1)) of { I# x -> x }
252 case (newFloatArray# n# s#) of { StateAndMutableByteArray# s2# barr# ->
253 (_MutableByteArray ixs barr#, S# s2#)}
255 newDoubleArray ixs@(ix_start, ix_end) (S# s#)
256 = let n# = case (if null (range ixs)
258 else ((index ixs ix_end) + 1)) of { I# x -> x }
260 -- trace ("newDoubleArray:"++(show (I# n#))) (
261 case (newDoubleArray# n# s#) of { StateAndMutableByteArray# s2# barr# ->
262 (_MutableByteArray ixs barr#, S# s2#)}
267 boundsOfArray :: Ix ix => _MutableArray s ix elt -> (ix, ix)
268 boundsOfByteArray :: Ix ix => _MutableByteArray s ix -> (ix, ix)
270 {-# SPECIALIZE boundsOfArray :: _MutableArray s Int elt -> IPr #-}
271 {-# SPECIALIZE boundsOfByteArray :: _MutableByteArray s Int -> IPr #-}
273 boundsOfArray (_MutableArray ixs _) = ixs
274 boundsOfByteArray (_MutableByteArray ixs _) = ixs
278 readArray :: Ix ix => _MutableArray s ix elt -> ix -> _ST s elt
280 readCharArray :: Ix ix => _MutableByteArray s ix -> ix -> _ST s Char
281 readIntArray :: Ix ix => _MutableByteArray s ix -> ix -> _ST s Int
282 readAddrArray :: Ix ix => _MutableByteArray s ix -> ix -> _ST s _Addr
283 --NO:readFloatArray :: Ix ix => _MutableByteArray s ix -> ix -> _ST s Float
284 readDoubleArray :: Ix ix => _MutableByteArray s ix -> ix -> _ST s Double
286 {-# SPECIALIZE readArray :: _MutableArray s Int elt -> Int -> _ST s elt,
287 _MutableArray s IPr elt -> IPr -> _ST s elt
289 {-# SPECIALIZE readCharArray :: _MutableByteArray s Int -> Int -> _ST s Char #-}
290 {-# SPECIALIZE readIntArray :: _MutableByteArray s Int -> Int -> _ST s Int #-}
291 {-# SPECIALIZE readAddrArray :: _MutableByteArray s Int -> Int -> _ST s _Addr #-}
292 --NO:{-# SPECIALIZE readFloatArray :: _MutableByteArray s Int -> Int -> _ST s Float #-}
293 {-# SPECIALIZE readDoubleArray :: _MutableByteArray s Int -> Int -> _ST s Double #-}
295 readArray (_MutableArray ixs arr#) n (S# s#)
296 = case (index ixs n) of { I# n# ->
297 case readArray# arr# n# s# of { StateAndPtr# s2# r ->
300 readCharArray (_MutableByteArray ixs barr#) n (S# s#)
301 = case (index ixs n) of { I# n# ->
302 case readCharArray# barr# n# s# of { StateAndChar# s2# r# ->
305 readIntArray (_MutableByteArray ixs barr#) n (S# s#)
306 = case (index ixs n) of { I# n# ->
307 case readIntArray# barr# n# s# of { StateAndInt# s2# r# ->
310 readAddrArray (_MutableByteArray ixs barr#) n (S# s#)
311 = case (index ixs n) of { I# n# ->
312 case readAddrArray# barr# n# s# of { StateAndAddr# s2# r# ->
315 readFloatArray (_MutableByteArray ixs barr#) n (S# s#)
316 = case (index ixs n) of { I# n# ->
317 case readFloatArray# barr# n# s# of { StateAndFloat# s2# r# ->
320 readDoubleArray (_MutableByteArray ixs barr#) n (S# s#)
321 = case (index ixs n) of { I# n# ->
322 -- trace ("readDoubleArray:"++(show (I# n#))) (
323 case readDoubleArray# barr# n# s# of { StateAndDouble# s2# r# ->
327 Indexing of ordinary @Arrays@ is standard Haskell and isn't defined here.
329 indexCharArray :: Ix ix => _ByteArray ix -> ix -> Char
330 indexIntArray :: Ix ix => _ByteArray ix -> ix -> Int
331 indexAddrArray :: Ix ix => _ByteArray ix -> ix -> _Addr
332 indexFloatArray :: Ix ix => _ByteArray ix -> ix -> Float
333 indexDoubleArray :: Ix ix => _ByteArray ix -> ix -> Double
335 {-# SPECIALIZE indexCharArray :: _ByteArray Int -> Int -> Char #-}
336 {-# SPECIALIZE indexIntArray :: _ByteArray Int -> Int -> Int #-}
337 {-# SPECIALIZE indexAddrArray :: _ByteArray Int -> Int -> _Addr #-}
338 --NO:{-# SPECIALIZE indexFloatArray :: _ByteArray Int -> Int -> Float #-}
339 {-# SPECIALIZE indexDoubleArray :: _ByteArray Int -> Int -> Double #-}
341 indexCharArray (_ByteArray ixs barr#) n
342 = case (index ixs n) of { I# n# ->
343 case indexCharArray# barr# n# of { r# ->
346 indexIntArray (_ByteArray ixs barr#) n
347 = case (index ixs n) of { I# n# ->
348 case indexIntArray# barr# n# of { r# ->
351 indexAddrArray (_ByteArray ixs barr#) n
352 = case (index ixs n) of { I# n# ->
353 case indexAddrArray# barr# n# of { r# ->
356 indexFloatArray (_ByteArray ixs barr#) n
357 = case (index ixs n) of { I# n# ->
358 case indexFloatArray# barr# n# of { r# ->
361 indexDoubleArray (_ByteArray ixs barr#) n
362 = case (index ixs n) of { I# n# ->
363 -- trace ("indexDoubleArray:"++(show (I# n#))) (
364 case indexDoubleArray# barr# n# of { r# ->
368 Indexing off @_Addrs@ is similar, and therefore given here.
370 indexCharOffAddr :: _Addr -> Int -> Char
371 indexIntOffAddr :: _Addr -> Int -> Int
372 indexAddrOffAddr :: _Addr -> Int -> _Addr
373 indexFloatOffAddr :: _Addr -> Int -> Float
374 indexDoubleOffAddr :: _Addr -> Int -> Double
376 indexCharOffAddr (A# addr#) n
377 = case n of { I# n# ->
378 case indexCharOffAddr# addr# n# of { r# ->
381 indexIntOffAddr (A# addr#) n
382 = case n of { I# n# ->
383 case indexIntOffAddr# addr# n# of { r# ->
386 indexAddrOffAddr (A# addr#) n
387 = case n of { I# n# ->
388 case indexAddrOffAddr# addr# n# of { r# ->
391 indexFloatOffAddr (A# addr#) n
392 = case n of { I# n# ->
393 case indexFloatOffAddr# addr# n# of { r# ->
396 indexDoubleOffAddr (A# addr#) n
397 = case n of { I# n# ->
398 case indexDoubleOffAddr# addr# n# of { r# ->
403 writeArray :: Ix ix => _MutableArray s ix elt -> ix -> elt -> _ST s ()
404 writeCharArray :: Ix ix => _MutableByteArray s ix -> ix -> Char -> _ST s ()
405 writeIntArray :: Ix ix => _MutableByteArray s ix -> ix -> Int -> _ST s ()
406 writeAddrArray :: Ix ix => _MutableByteArray s ix -> ix -> _Addr -> _ST s ()
407 writeFloatArray :: Ix ix => _MutableByteArray s ix -> ix -> Float -> _ST s ()
408 writeDoubleArray :: Ix ix => _MutableByteArray s ix -> ix -> Double -> _ST s ()
410 {-# SPECIALIZE writeArray :: _MutableArray s Int elt -> Int -> elt -> _ST s (),
411 _MutableArray s IPr elt -> IPr -> elt -> _ST s ()
413 {-# SPECIALIZE writeCharArray :: _MutableByteArray s Int -> Int -> Char -> _ST s () #-}
414 {-# SPECIALIZE writeIntArray :: _MutableByteArray s Int -> Int -> Int -> _ST s () #-}
415 {-# SPECIALIZE writeAddrArray :: _MutableByteArray s Int -> Int -> _Addr -> _ST s () #-}
416 --NO:{-# SPECIALIZE writeFloatArray :: _MutableByteArray s Int -> Int -> Float -> _ST s () #-}
417 {-# SPECIALIZE writeDoubleArray :: _MutableByteArray s Int -> Int -> Double -> _ST s () #-}
419 writeArray (_MutableArray ixs arr#) n ele (S# s#)
420 = case index ixs n of { I# n# ->
421 case writeArray# arr# n# ele s# of { s2# ->
424 writeCharArray (_MutableByteArray ixs barr#) n (C# ele) (S# s#)
425 = case (index ixs n) of { I# n# ->
426 case writeCharArray# barr# n# ele s# of { s2# ->
429 writeIntArray (_MutableByteArray ixs barr#) n (I# ele) (S# s#)
430 = case (index ixs n) of { I# n# ->
431 case writeIntArray# barr# n# ele s# of { s2# ->
434 writeAddrArray (_MutableByteArray ixs barr#) n (A# ele) (S# s#)
435 = case (index ixs n) of { I# n# ->
436 case writeAddrArray# barr# n# ele s# of { s2# ->
439 writeFloatArray (_MutableByteArray ixs barr#) n (F# ele) (S# s#)
440 = case (index ixs n) of { I# n# ->
441 case writeFloatArray# barr# n# ele s# of { s2# ->
444 writeDoubleArray (_MutableByteArray ixs barr#) n (D# ele) (S# s#)
445 = case (index ixs n) of { I# n# ->
446 -- trace ("writeDoubleArray:"++(show (I# n#))) (
447 case writeDoubleArray# barr# n# ele s# of { s2# ->
452 freezeArray :: Ix ix => _MutableArray s ix elt -> _ST s (Array ix elt)
453 freezeCharArray :: Ix ix => _MutableByteArray s ix -> _ST s (_ByteArray ix)
454 freezeIntArray :: Ix ix => _MutableByteArray s ix -> _ST s (_ByteArray ix)
455 freezeAddrArray :: Ix ix => _MutableByteArray s ix -> _ST s (_ByteArray ix)
456 freezeFloatArray :: Ix ix => _MutableByteArray s ix -> _ST s (_ByteArray ix)
457 freezeDoubleArray :: Ix ix => _MutableByteArray s ix -> _ST s (_ByteArray ix)
459 {-# SPECIALISE freezeArray :: _MutableArray s Int elt -> _ST s (Array Int elt),
460 _MutableArray s IPr elt -> _ST s (Array IPr elt)
462 {-# SPECIALISE freezeCharArray :: _MutableByteArray s Int -> _ST s (_ByteArray Int) #-}
464 freezeArray (_MutableArray ixs@(ix_start, ix_end) arr#) (S# s#)
465 = let n# = case (if null (range ixs)
467 else (index ixs ix_end) + 1) of { I# x -> x }
469 case freeze arr# n# s# of { StateAndArray# s2# frozen# ->
470 (_Array ixs frozen#, S# s2#)}
472 freeze :: MutableArray# s ele -- the thing
473 -> Int# -- size of thing to be frozen
474 -> State# s -- the Universe and everything
475 -> StateAndArray# s ele
478 = case newArray# n# init s# of { StateAndMutableArray# s2# newarr1# ->
479 case copy 0# n# arr# newarr1# s2# of { StateAndMutableArray# s3# newarr2# ->
480 unsafeFreezeArray# newarr2# s3#
483 init = error "freezeArr: element not copied"
486 -> MutableArray# s ele -> MutableArray# s ele
488 -> StateAndMutableArray# s ele
490 copy cur# end# from# to# s#
492 = StateAndMutableArray# s# to#
494 = case readArray# from# cur# s# of { StateAndPtr# s1# ele ->
495 case writeArray# to# cur# ele s1# of { s2# ->
496 copy (cur# +# 1#) end# from# to# s2#
499 freezeCharArray (_MutableByteArray ixs@(ix_start, ix_end) arr#) (S# s#)
500 = let n# = case (if null (range ixs)
502 else ((index ixs ix_end) + 1)) of { I# x -> x }
504 case freeze arr# n# s# of { StateAndByteArray# s2# frozen# ->
505 (_ByteArray ixs frozen#, S# s2#) }
507 freeze :: MutableByteArray# s -- the thing
508 -> Int# -- size of thing to be frozen
509 -> State# s -- the Universe and everything
510 -> StateAndByteArray# s
513 = case (newCharArray# n# s#) of { StateAndMutableByteArray# s2# newarr1# ->
514 case copy 0# n# arr# newarr1# s2# of { StateAndMutableByteArray# s3# newarr2# ->
515 unsafeFreezeByteArray# newarr2# s3#
519 -> MutableByteArray# s -> MutableByteArray# s
521 -> StateAndMutableByteArray# s
523 copy cur# end# from# to# s#
525 = StateAndMutableByteArray# s# to#
527 = case (readCharArray# from# cur# s#) of { StateAndChar# s1# ele ->
528 case (writeCharArray# to# cur# ele s1#) of { s2# ->
529 copy (cur# +# 1#) end# from# to# s2#
532 freezeIntArray (_MutableByteArray ixs@(ix_start, ix_end) arr#) (S# s#)
533 = let n# = case (if null (range ixs)
535 else ((index ixs ix_end) + 1)) of { I# x -> x }
537 case freeze arr# n# s# of { StateAndByteArray# s2# frozen# ->
538 (_ByteArray ixs frozen#, S# s2#) }
540 freeze :: MutableByteArray# s -- the thing
541 -> Int# -- size of thing to be frozen
542 -> State# s -- the Universe and everything
543 -> StateAndByteArray# s
546 = case (newIntArray# n# s#) of { StateAndMutableByteArray# s2# newarr1# ->
547 case copy 0# n# arr# newarr1# s2# of { StateAndMutableByteArray# s3# newarr2# ->
548 unsafeFreezeByteArray# newarr2# s3#
552 -> MutableByteArray# s -> MutableByteArray# s
554 -> StateAndMutableByteArray# s
556 copy cur# end# from# to# s#
558 = StateAndMutableByteArray# s# to#
560 = case (readIntArray# from# cur# s#) of { StateAndInt# s1# ele ->
561 case (writeIntArray# to# cur# ele s1#) of { s2# ->
562 copy (cur# +# 1#) end# from# to# s2#
565 freezeAddrArray (_MutableByteArray ixs@(ix_start, ix_end) arr#) (S# s#)
566 = let n# = case (if null (range ixs)
568 else ((index ixs ix_end) + 1)) of { I# x -> x }
570 case freeze arr# n# s# of { StateAndByteArray# s2# frozen# ->
571 (_ByteArray ixs frozen#, S# s2#) }
573 freeze :: MutableByteArray# s -- the thing
574 -> Int# -- size of thing to be frozen
575 -> State# s -- the Universe and everything
576 -> StateAndByteArray# s
579 = case (newAddrArray# n# s#) of { StateAndMutableByteArray# s2# newarr1# ->
580 case copy 0# n# arr# newarr1# s2# of { StateAndMutableByteArray# s3# newarr2# ->
581 unsafeFreezeByteArray# newarr2# s3#
585 -> MutableByteArray# s -> MutableByteArray# s
587 -> StateAndMutableByteArray# s
589 copy cur# end# from# to# s#
591 = StateAndMutableByteArray# s# to#
593 = case (readAddrArray# from# cur# s#) of { StateAndAddr# s1# ele ->
594 case (writeAddrArray# to# cur# ele s1#) of { s2# ->
595 copy (cur# +# 1#) end# from# to# s2#
598 freezeFloatArray (_MutableByteArray ixs@(ix_start, ix_end) arr#) (S# s#)
599 = let n# = case (if null (range ixs)
601 else ((index ixs ix_end) + 1)) of { I# x -> x }
603 case freeze arr# n# s# of { StateAndByteArray# s2# frozen# ->
604 (_ByteArray ixs frozen#, S# s2#) }
606 freeze :: MutableByteArray# s -- the thing
607 -> Int# -- size of thing to be frozen
608 -> State# s -- the Universe and everything
609 -> StateAndByteArray# s
612 = case (newFloatArray# n# s#) of { StateAndMutableByteArray# s2# newarr1# ->
613 case copy 0# n# arr# newarr1# s2# of { StateAndMutableByteArray# s3# newarr2# ->
614 unsafeFreezeByteArray# newarr2# s3#
618 -> MutableByteArray# s -> MutableByteArray# s
620 -> StateAndMutableByteArray# s
622 copy cur# end# from# to# s#
624 = StateAndMutableByteArray# s# to#
626 = case (readFloatArray# from# cur# s#) of { StateAndFloat# s1# ele ->
627 case (writeFloatArray# to# cur# ele s1#) of { s2# ->
628 copy (cur# +# 1#) end# from# to# s2#
631 freezeDoubleArray (_MutableByteArray ixs@(ix_start, ix_end) arr#) (S# s#)
632 = let n# = case (if null (range ixs)
634 else ((index ixs ix_end) + 1)) of { I# x -> x }
636 case freeze arr# n# s# of { StateAndByteArray# s2# frozen# ->
637 (_ByteArray ixs frozen#, S# s2#) }
639 freeze :: MutableByteArray# s -- the thing
640 -> Int# -- size of thing to be frozen
641 -> State# s -- the Universe and everything
642 -> StateAndByteArray# s
645 = case (newDoubleArray# n# s#) of { StateAndMutableByteArray# s2# newarr1# ->
646 case copy 0# n# arr# newarr1# s2# of { StateAndMutableByteArray# s3# newarr2# ->
647 unsafeFreezeByteArray# newarr2# s3#
651 -> MutableByteArray# s -> MutableByteArray# s
653 -> StateAndMutableByteArray# s
655 copy cur# end# from# to# s#
657 = StateAndMutableByteArray# s# to#
659 = case (readDoubleArray# from# cur# s#) of { StateAndDouble# s1# ele ->
660 case (writeDoubleArray# to# cur# ele s1#) of { s2# ->
661 copy (cur# +# 1#) end# from# to# s2#
666 unsafeFreezeArray :: Ix ix => _MutableArray s ix elt -> _ST s (Array ix elt)
667 unsafeFreezeByteArray :: Ix ix => _MutableByteArray s ix -> _ST s (_ByteArray ix)
669 {-# SPECIALIZE unsafeFreezeByteArray :: _MutableByteArray s Int -> _ST s (_ByteArray Int)
672 unsafeFreezeArray (_MutableArray ixs arr#) (S# s#)
673 = case unsafeFreezeArray# arr# s# of { StateAndArray# s2# frozen# ->
674 (_Array ixs frozen#, S# s2#) }
676 unsafeFreezeByteArray (_MutableByteArray ixs arr#) (S# s#)
677 = case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
678 (_ByteArray ixs frozen#, S# s2#) }
682 sameMutableArray :: _MutableArray s ix elt -> _MutableArray s ix elt -> Bool
683 sameMutableByteArray :: _MutableByteArray s ix -> _MutableByteArray s ix -> Bool
685 sameMutableArray (_MutableArray _ arr1#) (_MutableArray _ arr2#)
686 = sameMutableArray# arr1# arr2#
688 sameMutableByteArray (_MutableByteArray _ arr1#) (_MutableByteArray _ arr2#)
689 = sameMutableByteArray# arr1# arr2#
692 %************************************************************************
694 \subsection[PreludeGlaST-variables]{Variables}
696 %************************************************************************
699 type MutableVar s a = _MutableArray s Int a
703 newVar :: a -> _ST s (MutableVar s a)
704 readVar :: MutableVar s a -> _ST s a
705 writeVar :: MutableVar s a -> a -> _ST s ()
706 sameVar :: MutableVar s a -> MutableVar s a -> Bool
708 {- MUCH GRATUITOUS INEFFICIENCY: WDP 95/09:
710 newVar init s = newArray (0,0) init s
711 readVar v s = readArray v 0 s
712 writeVar v val s = writeArray v 0 val s
713 sameVar v1 v2 = sameMutableArray v1 v2
717 = case (newArray# 1# init s#) of { StateAndMutableArray# s2# arr# ->
718 (_MutableArray vAR_IXS arr#, S# s2#) }
720 vAR_IXS = error "Shouldn't access `bounds' of a MutableVar\n"
722 readVar (_MutableArray _ var#) (S# s#)
723 = case readArray# var# 0# s# of { StateAndPtr# s2# r ->
726 writeVar (_MutableArray _ var#) val (S# s#)
727 = case writeArray# var# 0# val s# of { s2# ->
730 sameVar (_MutableArray _ var1#) (_MutableArray _ var2#)
731 = sameMutableArray# var1# var2#