Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:35:20+00:00 2026-06-09T21:35:20+00:00

Trying to learn to write applications with Gtk2Hs I’m getting difficulties bridging the gap

  • 0

Trying to learn to write applications with Gtk2Hs I’m getting difficulties bridging the gap between the event driven Gtk2HS and the persistent state of my model. So to simplify, lets say that I have this simple application

module Main where

import Graphics.UI.Gtk
import Control.Monad.State

main = do
    initGUI
    window <- windowNew
    button <- buttonNew
    set button [buttonLabel := "Press me"]
    containerAdd window button

    -- Events
    onDestroy window mainQuit
    onClicked button (putStrLn ---PUT MEANINGFUL CODE HERE---)

    widgetShowAll window
    mainGUI

and the state of my application is how many times the button has been pressed. Seeing other posts like this they rely on MVars or IORefs which do not seem satisfactory to me, because in the future maybe I will want to refactor the code so the state lives on its own context.

I think that the solution should use the State monad using a step function like:

State $ \s -> ((),s+1)

but I’m not sure about the implications, how to do that in the above code or even if that monad is the right solution to my problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:35:21+00:00Added an answer on June 9, 2026 at 9:35 pm

    There’s basically two approaches:

    1. Use a pointer of some kind. This is your IORef or MVar approach. You can hide this behind a MonadState-like interface if you like:

      newtype GtkT s m a = GtkT { unGtkT :: ReaderT (IORef s) m a } deriving (Functor, Applicative, Monad, MonadIO)
      runGtkT = runReaderT . unGtkT
      
      instance MonadIO m => MonadState s (GtkT s m) where
          get   = GtkT (ask >>= liftIO . readIORef)
          put s = GtkT (ask >>= liftIO . flip writeIORef s)
      
    2. Pull an “inversion of control” style trick. Write a callback that prints a number, then replaces itself with a new callback that prints a higher number.

    If you try to use State or StateT directly, you’re gonna have a bad time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn how to write plugins using SIMBL. I got my
I'm trying to learn C and I'm currently trying to write a basic stack
I am trying to learn Ruby on Rails and trying to write some of
I'm trying to write a Date class in an attempt to learn C++. I'm
I am trying to learn how to write more dynamic web sites that use
I'm trying to learn to write a WordPress plugin by setting myself a goal
I am trying to learn to write OpenGL apps for the iPhone. How can
I'm trying to learn how to write apps for Windows Phone 7. I would
I am trying to learn how to write firefox extensions. I download an HelloWorld
I am trying to learn how to write a bookmarklet where I can highlight

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.