[Haskell-cafe] How to use STArray?

ChrisK chrisk at MIT.EDU
Fri Aug 26 20:27:43 EDT 2005

Alistair Bayley wrote:
>>There are also STArray examples on the wiki at
>>This includes a very high performance use of STUArray example (from
>>Autrijus), and a ST.Lazy example that I wrote that uses STArray.
> Thanks. I saw these, but couldn't quite figure out what I needed to do
> for my program. I might add a note about helping the type-checker with
> the array type overloading.
> BTW, the STArray version of my code is honkin' fast (well, compared to
> the immutable array versions). And memory usage is way down, too.

I made the Lazy example after almost two hours of trial and error with
very very short functions until discovered how to get it correct and add
type annotations.

The difficulty is that ST uses an advanced type system trick to do what
other languages do automatically: provide scope.  In C++ any pair of
braces works.  In Haskell the scope is not well defined since you can
compose many ST monad pieces into a final computation that runST or
runST(U)Array calls.  This is flexible, but the type annotation becomes
unnatural, and the error messages make C++ template error reporting look
clear by comparison.  Mixing ST.Strict and ST.Lazy was the hardest bit
to figure out since there was no Data.Array.ST.Lazy.  Does anyone know
why it was left out?  I'll put a note on the HaskellTwo page about that...

The odd thing is that there is not a "Haskell Array Tutorial" among all
the other documentation.


More information about the Haskell-Cafe mailing list