[Haskell-cafe] Re: [Haskell] Top Level <-
judah.jacobson at gmail.com
Tue Aug 26 16:16:48 EDT 2008
On Tue, Aug 26, 2008 at 3:15 AM, Ashley Yakeley <ashley at semantic.org> wrote:
> Judah Jacobson wrote:
>> I've been wondering: is there any benefit to having top-level ACIO'd
>> <- instead of just using runOnce (or perhaps "oneshot") as the
>> primitive for everything?
> I don't think oneshots are very good for open witness declarations (such as
> the open exceptions I mentioned originally), since there are pure functions
> that do useful things with them.
I think you're saying that you want to write "w <- newIOWitness" at
the top level, so that w can then be referenced in a pure function.
Fair enough. But newIOWitness's implementation requires writeIORef
(or an equivalent), which is not ACIO, right? I suppose you could
call unsafeIOToACIO, but if that function is used often it seems to
defeat the purpose of defining an ACIO monad in the first place.
> Not sure about TVars either, which operate in the STM monad. Would you also
> need a oneshotSTM (or a class)?
Interesting point; I think you can work around it, but it does make
the code a little more complicated. For example:
oneshot uniqueVar :: IO (TVar Integer)
uniqueVar =< atomically $ newTVar 0 -- alternately, use newTVarIO
uniqueIntSTM :: IO (STM Integer)
uniqueIntSTM = uniqueVar >>= \v -> return $ do
n <- readTVar v
writeTVar v (n+1)
getUniqueInt :: IO Integer
getUniqueInt = uniqueIntSTM >>= atomically
More information about the Haskell-Cafe