[Haskell-cafe] save/restore of STRef state

Andrew Coppin andrewcoppin at btinternet.com
Sat Feb 27 12:57:49 EST 2010


Job Vranish wrote:
>
>
> On Sat, Feb 27, 2010 at 11:53 AM, Andrew Coppin 
> <andrewcoppin at btinternet.com <mailto:andrewcoppin at btinternet.com>> wrote:
>
>
>
>     Why an IORef? Why not an STRef? Then you won't need unsafeIOToST.
>     (And since the type system forces a ContextRef to exist in only
>     one state thread, worrying about thread isolation with
>     atomicModifyIORef seems unecessary.)
>
>
> I use the IORefs because I wanted to use mkWeakIORef (maybe mkWeak 
> would work just as well?) and atomicModifyIORef. The thread isolation 
> is needed because of the the finalizers that clean out the map when 
> the references get GC'd.  

Hmm, I see. (I can't actually figure out what the map does, so I can't 
really comment further about that. You'd think there's a way around this 
though...)

>     Using a state monad with a mutable structure as the state looks
>     highly dubious. (The whole point of a state monad is, after all,
>     to avoid needing to mutate stuff.) I can see 2 calls to "get", but
>     none to "put". I would suggest you either use a reader monad with
>     mutable state, or a state monad with immutable state. One or the
>     other. (Personally, I'd go for the latter.)
>
>
> Yeah, I'll switch to Reader, but the state needs to be mutable so that 
> the finalizers can get to it.

Ah, I see. That at least makes sense...



More information about the Haskell-Cafe mailing list