1 -----------------------------------------------------------------------------
3 -- Module : Data.Array.ST
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable
11 -- Mutable boxed and unboxed arrays in the 'Control.Monad.ST.ST' monad.
13 -----------------------------------------------------------------------------
15 module Data.Array.ST (
18 STArray, -- instance of: Eq, MArray
22 STUArray, -- instance of: Eq, MArray
24 castSTUArray, -- :: STUArray s i a -> ST s (STUArray s i b)
26 -- * Overloaded mutable array interface
27 module Data.Array.MArray,
32 import Data.Array.MArray
33 import Data.Array.Base ( STUArray, castSTUArray, UArray, unsafeFreezeSTUArray )
34 import Control.Monad.ST ( ST, runST )
37 import Hugs.ST ( STArray )
40 #ifdef __GLASGOW_HASKELL__
41 import GHC.Arr ( STArray, Array )
44 -- | A safe way to create and work with a mutable array before returning an
45 -- immutable array for later perusal. This function avoids copying
46 -- the array before returning it - it uses 'unsafeFreeze' internally, but
47 -- this wrapper is a safe interface to that function.
50 => (forall s . ST s (STArray s i e))
52 runSTArray st = runST (st >>= unsafeFreeze)
54 -- | A safe way to create and work with an unboxed mutable array before
55 -- returning an immutable array for later perusal. This function
56 -- avoids copying the array before returning it - it uses
57 -- 'unsafeFreeze' internally, but this wrapper is a safe interface to
61 => (forall s . ST s (STUArray s i e))
63 runSTUArray st = runST (st >>= unsafeFreezeSTUArray)
66 -- INTERESTING... this is the type we'd like to give to runSTUArray:
68 -- runSTUArray :: (Ix i, IArray UArray e,
69 -- forall s. MArray (STUArray s) e (ST s))
70 -- => (forall s . ST s (STUArray s i e))
73 -- Note the quantified constraint. We dodged the problem by using
74 -- unsafeFreezeSTUArray directly in the defn of runSTUArray above, but
75 -- this essentially constrains us to a single unsafeFreeze for all STUArrays
76 -- (in theory we might have a different one for certain element types).