[Haskell] How to use STM in GUI applications?

Chris Kuklewicz haskell at list.mightyreason.com
Tue Dec 5 05:31:00 EST 2006


Lemmih wrote:
> On 12/4/06, Thorsten Seitz <thorsten.seitz at web.de> wrote:
>> Hello,
>>
>> I'm very intrigued by the concepts of STM but I'm having difficulties
>> to see
>> how a TVar based model can be used by a GUI application (e.g. Gtk2hs):
>>
>> For example, I'd like to update the GUI (e.g. changing widget
>> sensitivity)
>> upon changes in the model.
>> But changing the sensitivity of a widget is an IO operation which
>> cannot be
>> initiated atomically by a model change which is an STM operation.
>> Therefore
>> the GUI change cannot be executed atomically together with the model
>> change.
>> This means for example that I can still press a button although the
>> model has
>> changed such that the button action is not allowed anymore. All I can
>> do is
>> check the condition of the model when the button is pressed and then
>> ignore
>> the button press which is not very satisfying for the user.
>>
>> Is it simply not appropriate to use TVar based models in a GUI
>> application?
>> Maybe STM should be more used like a service for MVar based GUI models?
> 
> How about making a list of IO actions to be executed when the
> transaction commits?
> 

That onCommit list and its execution can be done by using the slightly
modified/enhanced/wrapped AdvSTM monad.

A complex example that also can queue actions on retry is at
http://haskell.org/haskellwiki/?title=New_monads/MonadAdvSTM#Helper_Thread_Code

But for just onCommit the simpler code at
http://haskell.org/haskellwiki/New_monads/MonadAdvSTM#Just_onCommit
should work.

Cheers,
  Chris


More information about the Haskell mailing list