[Haskell] threading mutable state through callbacks
jules at jellybean.co.uk
Tue Oct 12 17:11:32 EDT 2004
On 12 Oct 2004, at 18:59, Marcin 'Qrczak' Kowalczyk wrote:
> Jules Bean <jules at jellybean.co.uk> writes:
>> I think what people are trying to suggest is an 'initialization
>> phase' in the IO monad, which takes place "before" the pure
>> functions are defined.
> If it was done before, what could you use to specify initial value of
> such a variable? Only literals? Constructors? Named constants? Results
> of arithmetic operations? Results of arbitrary functions?
Yes, that's what I was trying to get at. But you said it much better...
> The well-definedness of a global IORef relies on the fact that
> creation of an IORef doesn't have visible side effects, so it doesn't
> matter when it happens, as long as it happens once between the
> variable is used. An arbitrary IO computation doesn't have this
There are other hacks you can think of, though, if IORefs are your only
goal. For example, something like
getIORef :: Int -> a -> IORef a
getIORef :: String -> a -> IORef a
(importantly, it's IORef, not IO IORef.) But this isn't great for
programmer convenience, since there's nothing to help him discipline
the use of particular variables.
All we really need is a 'unique value monad' to give us unique values
to be keys for IORefs (or whatever else), and allow this monad to be
accessible at the top level. Conceptually this monad is commutative, so
ordering of the actions doesn't matter.
More information about the Haskell