[Haskell-cafe] Re: Stronger STM primitives needed? Or am I just doing it wrong?

apfelmus apfelmus at quantentunnel.de
Thu Apr 24 12:43:07 EDT 2008

Chris Smith wrote:
> apfelmus wrote:
>> For 1), it's enough to have a primitive
>>    scheduleWriteTVar :: TVar a -> a -> STM ()
>> that ensures to write the TVar at the very end of the  atomically block..
> Unfortunately, though, this breaks the very thing that makes STM 
> attractive: namely, composability.  Now in order to work with a TVar, I 
> need to know whether anything that came before me might have modified it, 
> and if so take the current value as a parameter instead of reading it 
> like normal.
> Or am I misunderstanding something?

You're correct, that's what I meant. But it's nothing more and nothing 
less than the purely functional way of dealing with "mutable" state, 
isn't it?

And you need a parameter anyway, namely the  TVar a  itself. I mean, 
when it's in scope like in

      a <- readTVar v
      writeTVar v (a+1)
      readTVar v

you don't need a parameter. But if the do-block is broken up into 
functions, you need a parameter

   foo v = do
      a <- readTVar v
      writeTVar v (a+1)
      bar v
   bar v = readTVar v

and you may as well supply its value instead of the reference  v .


More information about the Haskell-Cafe mailing list