[Haskell] How to use STArray?

Benjamin Franksen benjamin.franksen at bessy.de
Tue Aug 30 06:33:58 EDT 2005


On Tuesday 30 August 2005 06:32, oleg at pobox.com wrote:
> Benjamin Franksen wrote:
> > On Thursday 25 August 2005 19:58, Udo Stenzel wrote:
> > > [...] you'll need a type signature somewhere to help ghc resolve
> > > the overloading of newArray and readArray, which is surprisingly
> > > tricky due to the "s" that must not escape.  This works:
> > >
> > > compute :: Int -> Int
> > > compute n = runST ( do
> > >     arr <- newArray (-1, 1) n :: ST s (STArray s Int Int)
> > >     readArray arr 1
> > >   )
> >
> > I am fighting with a similar problem. I want to use STUArray but
> > without committing to a fixed element type.
>
> That problem has been addressed in a message
> 	http://www.haskell.org/pipermail/haskell-cafe/2004-July/006400.html

Ups, I have missed this one. Next time I'll do a list search first.

> which discussed several solutions. Given below is one of the
> solutions adjusted to fit the question of the original poster. His
> code is almost unchanged.
>
> It would havebeen nice if the GHC library supported the second
> solution, a class Unpackable. Currently there are instances of
> 	MArray (STUArray s) e (ST s)
> and
> 	IArray UArray e
> for exactly the same set of types `e'. Alas, that condition is not
> stated formally, so we cannot infer that MArray (STUArray s) e (ST s)
> holds whenever IArray UArray e does.

Any chance that the standard libraries will be changed along these 
lines?

> > [snip complete solution]

I almost suspected that I have to introduce some existentially 
quantified data type, but had no idea where and how.

This would make a useful wiki page, BTW.

Thanks a lot for the help.

Ben


More information about the Haskell mailing list