You can always do something like this with MVars and message passing.  Keep one thread which is State X IO () and loops at:

nextEvent <- takeMVar eventMVar

And then all your call backs will look something like:

putMVar eventMVar


---------- Původní zpráva ----------
Od: Savanni D'Gerinel <>
Datum: 16. 12. 2012
Předmět: Re: [Gtk2hs-users] Thread a pure state through gtk callbacks

I'm with wagnerdm on this. I've not found a way to do this, either.
I'm really weak with monad transformers, though, so a master of the
language might come back with a solution.

However, I did do an application in which I set up a data type to
represent the application state, I set of pure functions for all of the
state manipulation (pure except for loading and saving the data), and
then I wrapped all of that inside of the GUI callbacks. But, my
application could basically be run from the repl by applying the
semantic functions to the state and saving the result.


On 12/16/2012 06:40 AM, koral wrote:
> Hello,
> I'm trying to use the State monad in gtk callbacks, but the more I dig, the more I get convinced this is not possible, and I would like to know whether I'm doing something wrong.
> Gtk expects callbacks to be 'IO ()' functions, while I would like them to be 'StateT X IO ()', with X a global state that lives through the whole program life.
> When registering a callback, I have to wrap it with runStateT or use MonadControl tricks to thread the current state to the callback while keeping the 'IO ()' signature:
>> currentX <- get
>> on object signal $ void $ runStateT callback currentX
> or
>> liftBaseWith $ \runInIO -> on object signal $ void . runInIO $ callback
> However, this won't actually have the correct behavior:
> * 'callback' will always read X as it was at register time, and not at trigger time;
> * all modifications to this X instance are lost when leaving 'callback'.
> Basically, it's as if 'callback' was always provided a hard copy of X at register time, on which it works internally without actually changing the real global state.
> I think I understand the why -- either way, the callback ends up being 'IO ()' which cannot possibly read/write any pure state. Now, I'm wondering whether it is possible at all, and would appreciate your advice.
> If not, are IORef the only way to get a stateful behavior ?
> --
> koral
> ------------------------------------------------------------------------------
> LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
> Remotely access PCs and mobile devices and provide instant support
> Improve your efficiency, and focus on delivering more value-add services
> Discover what IT Professionals Know. Rescue delivers
> _______________________________________________
> Gtk2hs-users mailing list