# STArray and Lazy ST

Josef Svenningsson josefs@cs.chalmers.se
Thu, 8 May 2003 17:20:01 +0200 (MET DST)

Hi,

I've spent some time running after a little bug in my code that seems to
be caused by the following fact:
If I want to use Data.Array.ST I can only use it with the strict ST monad.
I would really like to use it with the lazy ST monad. Is there any way to
do it?

Further more, I got some really weird error messages. My file looked
something like this:

\begin{pseudocode}
import Data.Array.ST

... further down I have the following function....

foo :: Ix v => STArray s v Bool -> v -> ST s Bool
foo a b = readArray a b
\end{pseudocode}

Now I got the following error message:

formats/Graph.hs:91:
Could not deduce (MArray (STArray s) Bool (ST s))
from the context (Ix v)
Probable fix:
Add (MArray (STArray s) Bool (ST s))
to the type signature(s) for contains'
Or add an instance declaration for (MArray (STArray s) Bool (ST s))
arising from use of readArray' at formats/Graph.hs:91
In the definition of foo': readArray a b

The thing is, there is already an instance MArray (STArray s) e (ST s)! So
what is the compiler complaining about? The problem is, it's not the right
ST type! In the error message the name ST means two different things which
is REALLY confusing and it took me a while to figure it out. One ST comes
from Control.Monad.ST.Lazy and the other from Data.Array.ST.

Is there any way to improve the situation? I'm not claiming these issues
are bugs, but they certainly made my life a lot harder for a while.

Cheers,

/Josef

`