Haskell GTK和Monad变形金刚

时间:2013-07-19 01:08:45

标签: haskell gtk monads monad-transformers gtk2hs

我正在制作并使用UI进行应用,我需要运行StateTReaderTIO的代码,我正在使用Graphics.UI.Gtk用户界面和我有以下问题:

我需要在单击某个按钮时调用具有签名的函数

tryAttack :: Int -> ReaderT Builder (StateT BattleState IO) Bool

但在GTK中,clickedOn函数需要IO ()个参数:

onClicked :: ButtonClass b => b -> IO () -> IO (ConnectId b)

如何让clickedOn函数“傻瓜”地认为tryAttackIO ()

attackButton builder leftTR i = do
    moveButton <- builderGetObject builder castToButton ("move" ++ show i)
    onClicked moveButton $ do
        ok <- tryAttack i
        if ok
            then hideAndShow builder castToTable "fight" "option"
            else return ()

tryAttack :: Int -> ReaderT Builder (StateT BattleState IO) Bool
tryAttack i = return True

显然tryAttack代码是暂时的。

1 个答案:

答案 0 :(得分:2)

你需要打开阅读器monad,然后声明monad以获得bool结果:

(ok,newBattleState) <- runStateT (runReaderT (tryAttack i) builder) battleState