[Haskell-cafe] One-shot? (was: Global variables and stuff)

Adrian Hey ahey at iee.org
Sat Nov 13 07:51:48 EST 2004

On Saturday 13 Nov 2004 10:39 am, Keean Schupke wrote:
> Actually, I Think I'm wrong - I think its not even safe if you cannot
> export the '<-' def. If any functions which use it are exported you are
> in the same situation. I cannot say the kind of code in the example I
> gave is good, can you? Infact the availability of these top level IO
> actions seems to completely change the feel of the language...

I've looked at your example, and the behaviour you describe is exactly
what would be expected and what it is intended. That's the whole point
of things having identity. The reason they have identity is because
they are mutable and all users of a particular TWI are indeed mutating
the same thing, just as all users of stdout are writing to the same

The point is that if the shared TWI is something like an IORef this
is (of course) extremely dangerous because anybody can write anything
they like to it at any time. But that is not how this should be used.
The module exporting one or more TWIs typically will not be exporting
raw IORefs. It will be exporting a well designed stateful api which
access IORefs etc via closures. It's the responsibility of the author
of the exporting module to organise the code so that it delevers (on
whatever promisies it's making) to all clients, and clients should not
rely on anything that isn't being promised.

So it seems to me that the only thing that's wrong here is your
expectations (I.E. that a module should assume it has exclusive access
to whatever state the TWI's it imports mutate). This is not so.
If it wants it's own private TWI (a mutable queue say) it should not
be importing another modules queue (not that any good design should
be exporting such a thing anyway), it should be importing a newQueue
constructor and making it's own queue (either at the top level or
via normal IO monadic operations)..

 myQueue <- newQueue

But there's no magic here. All IO actions have potentially unknown
state dependencies and mutating effects, that's why they're in the
IO monad. All the top level <- extension does is enable the user to
extend the initial world state (as seen by main) with user defined
state, but it doesn't fundamentally change nature or hazards of
programming via the IO monad, for better or worse.

Adrian Hey

More information about the Haskell-Cafe mailing list