<div dir="ltr"><div>You should use "runSTArray" or "runSTUArray" instead of "runST" to convert your STArray to an immutable array:  <a href="https://hackage.haskell.org/package/array-0.5.2.0/docs/Data-Array-ST.html#v:runSTUArray" target="_blank">https://hackage.haskell.<wbr>org/package/array-0.5.2.0/<wbr>docs/Data-Array-ST.html#v:<wbr>runSTUArray</a></div><div><br></div><div>Or another option is to use "stToIO"  to convert the "STArray" to an "IOArray." <a href="https://hackage.haskell.org/package/base-4.10.1.0/docs/Control-Monad-ST-Lazy.html#v:stToIO">https://hackage.haskell.org/package/base-4.10.1.0/docs/Control-Monad-ST-Lazy.html#v:stToIO</a>  but if you want to build an IOArray, it is better to just start with an IOArray rather than converting an STArray to an IOArray.</div><div><br></div>The design of ST arrays is to allow you to construct them quickly and then make them immutable once you are done constructing it.<div><br></div><div>An immutable array must have the whole array copied after every single update, but ST arrays allow you to make many updates without copying, then when you have completed constructing the ST array, you must freeze it to an immutable array using the "runSTUArray" function. Freezing happens without copying the array, after that it is immutable and may not be made into an STArray again unless you unfreeze it using "thaw", which creates a copy of it:  <a href="https://hackage.haskell.org/package/array-0.5.2.0/docs/Data-Array-MArray.html#v:thaw">https://hackage.haskell.org/package/array-0.5.2.0/docs/Data-Array-MArray.html#v:thaw</a><div><br></div><div>Once you have constructed your immutable array, you can access it arbitrarily using the immutable operator (!).</div><div><br></div><div>If you want to make multiple updates at multiple times, you must use an IOArray or IOUArray. The ST monad is designed for you to construct pure, referentially transparent, immutable values in an isolated and strictly evaluated "environment" that lets you perform strict updates during construction. Once evaluation of the "ST" monad is complete, the returned value becomes pure, immutable, and referentially transparent. The for-all'd "s" parameter of the "runST" function ensures you do not mix separate "environments," and this is the reason you got your type error. Using "runSTArray" or "runSTUArray" does not have this restriction.</div><div><br></div><div>I am not sure of the reason for this design decision, but I know it has something to do with the compiler guaranteeing that pure immutable referentially transparent types are constructed without effecting each other, preventing race conditions. There is discussion of this on the Haskell wiki:  <a href="https://wiki.haskell.org/Monad/ST#An_explanation_in_Haskell-Cafe">https://wiki.haskell.org/Monad/ST#An_explanation_in_Haskell-Cafe</a><br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 29, 2017 at 11:36 PM, Jean-Marc Alliot <span dir="ltr"><<a href="mailto:jm@alliot.org" target="_blank">jm@alliot.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
This is my first post to this list so I apologize in advance if I don't use it properly, or if my question is too simple or inapropriate.<br>
<br>
I come from the Caml world and I am quite new to Haskell (but not to functional programming). I am currently trying to get the hang of Haskell arrays. I have gone through regular arrays, IO Arrays and I am now working with ST Arrays.<br>
<br>
This is the problem I am currently stuck with. I write the following code:<br>
<br>
arr = newArray (-1, 1) 0 :: ST s (STArray s Int Int)<br>
get :: Int -> Int<br>
get i = runST (arr >>= (\b -> readArray b i))<br>
<br>
Here everything is perfectly OK.<br>
<br>
Now I want a more general version that could deal with any array like arr. So I write:<br>
<br>
get2 :: ST s (STArray s Int Int) -> Int -> Int<br>
get2 tab i = runST (tab >>= (\b -> readArray b i))<br>
<br>
And the compiler is clearly very upset by my code:<br>
<br>
Couldn't match type ‘s’ with ‘s1’<br>
      ‘s’ is a rigid type variable bound by<br>
        the type signature for:<br>
          get2 :: forall s. ST s (STArray s Int Int) -> Int -> Int<br>
        at testst.hs:17:9<br>
      ‘s1’ is a rigid type variable bound by<br>
        a type expected by the context:<br>
          forall s1. ST s1 Int<br>
        at testst.hs:18:14<br>
      Expected type: ST s1 Int<br>
        Actual type: ST s Int<br>
I am pretty sure that the compiler is right and I am wrong, but I don't get why... Anyone could help?<br>
<br>
Thanks<br>
<br>
<br>
______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/haskell-caf<wbr>e</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br></div>