External Publication
Visit Post

Haskell's missing mutable reference type

Haskell Community [Unofficial] June 14, 2026
Source

michaelpj:

OK, this is supposed to be clear from loggerExampleConcurrently. I’ll see if I can clarify that.

In that example you do the modification inside the fork, so it wasn’t obvious to me whether the modification would be visible if you did it the other way around.

Ah yes, this example needs improving, thanks!

michaelpj:

What’s the use of having thread local state that allows non-scoped modifications?

What’s a “non-scoped modification”? Do you mean writeIORef versus withModifiedIORef :: IORef a -> (a -> a) -> IO a -> IO a?

Yes

michaelpj:

I just don’t like the semantics of the latter. It would allow you to observe whether you’re running in a forked thread. See “the interaction between the semantics of thread creation and of IOScopedRef are good” in the Concurrency section.

I don’t understand how it would do that, can you give an example?

Sure, suppose we have an inherited, thread-local IORef-like type, ITLIORef, and someone gives me a function

foo :: IO a -> IO a

Then I can call it like

ref <- newITLIORef False
foo $
  writeITLIORef ref True
readITLIORef ref

and the result depends on whether foo ran its body in a new thread or not. Now that’s not terrible. There are all sorts of ways we could get similar behaviour, such as myThreadId, but it does suggest to me it’s not a particularly principled primitive, whereas it seems to me that IOScopedRef is principled (although I don’t know in what terms to formulate that claim).

michaelpj:

The problems you observe in fork-fragile-reader-like-operations seem to me to be related to these two things. I’m arguing that the “scoped” part of it is perhaps not the crux!

Right, whereas I think the “scoped” part probably is the crux. That’s the part where I see an opportunity for a new primitive with principled behaviour. I understand the general form of your argument that the “scoped” part isn’t the crux: in principle there’s a use for a souped-up version of the thing I propose, which allows for more behaviours. But In practice is there a use for it? Where are people using that?

Discussion in the ATmosphere

Loading comments...