
I'm puzzling out how to get a Bool from am IO Bool. I know I'm not supposed to, but I don't see any way around my predicament. The basic setup is: I have an edit box, and a panel. If you click the LMB on the panel when the edit box is checked, this means you want to move a graphical object around the panel. If it is unchecked, then clicking the LMB means you want to add a graphical object. The relevant bits I've managed to put together so far are: mainFrame = do -- main application frame streams <- varCreate [] ... cbEdit <- checkBox p1 [text := "Edit Mode", on command ::= onCbEdit textlog] -- p1 is the panel, ignore textlog let isEditing = get cbEdit checked -- returns type IO Bool windowOnMouse p False {- no motion events -} (onMouse p streams isEditing) ... where onMouse w streams isEditChecked mouse = case mouse of MouseLeftDown pt mods -> if isEditChecked then findStream w streams pt else addStream w streams pt other -> skipCurrentEvent -- unprocessed event: send up the window chain where -- define findStream and addStream The problem is that isEditChecked is of type IO Bool, not Bool. I presume that I should actually be taking a different (non-imperative) approach, and I'm wondering if anyone could suggest what that approach should be? Many apologies for being a clueless n00b. ___________________________________________________________ Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com

Take a look at unsafePerformIO, it is of type IO a -> a. Its not
particularly "safe" (the name gives a clue), but it does what you
want.
On 9/20/05, Mark Carter
I'm puzzling out how to get a Bool from am IO Bool. I know I'm not supposed to, but I don't see any way around my predicament.
The basic setup is: I have an edit box, and a panel. If you click the LMB on the panel when the edit box is checked, this means you want to move a graphical object around the panel. If it is unchecked, then clicking the LMB means you want to add a graphical object.
The relevant bits I've managed to put together so far are:
mainFrame = do -- main application frame streams <- varCreate [] ... cbEdit <- checkBox p1 [text := "Edit Mode", on command ::= onCbEdit textlog] -- p1 is the panel, ignore textlog let isEditing = get cbEdit checked -- returns type IO Bool windowOnMouse p False {- no motion events -} (onMouse p streams isEditing) ... where onMouse w streams isEditChecked mouse = case mouse of MouseLeftDown pt mods -> if isEditChecked then findStream w streams pt else addStream w streams pt other -> skipCurrentEvent -- unprocessed event: send up the window chain
where -- define findStream and addStream
The problem is that isEditChecked is of type IO Bool, not Bool. I presume that I should actually be taking a different (non-imperative) approach, and I'm wondering if anyone could suggest what that approach should be? Many apologies for being a clueless n00b.
___________________________________________________________ Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Tue, Sep 20, 2005 at 04:30:25PM +0100, Neil Mitchell wrote:
Take a look at unsafePerformIO, it is of type IO a -> a. Its not particularly "safe" (the name gives a clue), but it does what you want.
I dont think you would ever need to do unsafePerformIO unless you are writing some lib calls or some such thing
onMouse w streams isEditChecked mouse = case mouse of MouseLeftDown pt mods -> if isEditChecked then findStream w streams pt else addStream w streams pt other -> skipCurrentEvent --
In your case the approach should be some thing along these lines onMouse w streams isEditChecked mouse = do ischecked <- isEditChecked case mouse of if ischecked then findStream w streams pt ... I am assuming that findStream w streams pt is of type IO a for some a. otherwise you might have to use something like return $ findStream w streams pt Also the function onMouse will return some IO something. Remember there is no real reason to use unsafePerformIO unless you are writing some new IO library call. ppk

Mark Carter wrote:
I'm puzzling out how to get a Bool from am IO Bool. I know I'm not supposed to, but I don't see any way around my predicament.
The basic setup is: I have an edit box, and a panel. If you click the LMB on the panel when the edit box is checked, this means you want to move a graphical object around the panel. If it is unchecked, then clicking the LMB means you want to add a graphical object.
The relevant bits I've managed to put together so far are:
mainFrame = do -- main application frame streams <- varCreate [] ... cbEdit <- checkBox p1 [text := "Edit Mode", on command ::= onCbEdit textlog] -- p1 is the panel, ignore textlog let isEditing = get cbEdit checked -- returns type IO Bool windowOnMouse p False {- no motion events -} (onMouse p streams isEditing) ... where onMouse w streams isEditChecked mouse = case mouse of MouseLeftDown pt mods -> if isEditChecked then findStream w streams pt else addStream w streams pt other -> skipCurrentEvent -- unprocessed event: send up the window chain
where -- define findStream and addStream
The problem is that isEditChecked is of type IO Bool, not Bool. I presume that I should actually be taking a different (non-imperative) approach, and I'm wondering if anyone could suggest what that approach should be? Many apologies for being a clueless n00b.
Well, your onMouse function is acutally in the IO monad, so you can just use the do notation. You can also get rid of the case, like so: onMouse w streams isEditChecked (MouseLeftDown pt mods) = do ec <- isEditChecked if ec then ... else ... onMouse _ _ _ _ = skipCurrentEvent
participants (4)
-
Mark Carter
-
Neil Mitchell
-
Piyush P Kurur
-
robert dockins