我正在尝试编写一个具有FFI到C的程序。有一个方法需要在指针上调用,它不会返回任何内容。我的指针是RWS monad中的状态,但是当我尝试更改状态时,由于lazy-ness而未调用该函数。如何确保调用该函数?
EG。在下面的代码中,如何显示“创建外部monad”?
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Main where
import Control.Monad.RWS.Strict
newtype OuterMonad a = OuterMonad {
unwrapOuterMonad :: RWST () () () IO a
} deriving (Monad, MonadIO)
createOuterMonad :: OuterMonad ()
createOuterMonad = do
liftIO $ putStrLn "creating outer monad"
return ()
runOuterMonad :: OuterMonad a -> IO ()
runOuterMonad _ = putStrLn "RunOuterMonad!!"
main :: IO ()
main = do
putStrLn "starting Program"
runOuterMonad $ do
createOuterMonad
答案 0 :(得分:3)
您应该在某个时候评估OuterMonad
。例如:
runOuterMonad :: OuterMonad a -> IO ()
runOuterMonad m = do
evalRWST (unwrapOuterMonad m) () ()
putStrLn "RunOuterMonad!!"