runSTArray for multiple arrays

Bertram Felgenhauer bertram.felgenhauer at
Thu Mar 13 14:02:03 EDT 2008

Henning Thielemann wrote:
> Currently, runSTArray can handle only one array:
> runSTArray :: Ix i => (forall s . ST s (STArray s i e)) -> Array i e
> Would it be possible to access multiple arrays successively by a function 
> like
> runSTArraySplit :: Ix i =>
>    (forall s . ST s (STArray s i e, a)) ->
>    (Array i e, ST t a)
> Hm, this would not work, because type variables 's' in 'a' had to be 
> converted to 't' as well. Are there other ideas to tackle this problem?

I tried making a type class for this, namely

    class EvalST st pure | st -> pure where
        freezeST :: st s -> ST s pure

together with

    evalST :: EvalST st pure => (forall s. ST s (st s)) -> pure
    evalST f = runST (f >>= freezeST)

I'll attach the complete module. Feel free to use, modify, etc.

Unfortunately, the extra 's' type argument to the 'st' constructor
makes this a bit cumbersome to use. In particular I needed wrapper
types for arrays and pairs.

On the positive side, the approach allows dealing with pure
values, boxed and unboxed arrays, and STRefs (not implemented) in a
uniform way.


    *EvalST> evalST (do a <- newArray (0,0) 0; return (STPair (STPure 1) (STArray' a))) :: (Int, Array Int Int)
    (1,array (0,0) [(0,0)])

Is there any way to make

    evalST (do a <- newArray (0,0) 0; return (STPure 1, a)) :: (Int, Array Int Int)

work? That's the prettiest interface I can imagine right now.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: EvalST.hs
Type: text/x-haskell
Size: 1451 bytes
Desc: not available
Url :

More information about the Libraries mailing list