我正在嘗試爲未來的項目創建monad變換器,但不幸的是,我的Monad typeclasse的實現(>> =)功能不起作用。嘗試實施(>> =)函數以創建自定義monad變換器時出現錯誤
首先,這裏是底層的單子的實現:
newtype Runtime a = R {
unR :: State EInfo a
} deriving (Monad)
這裏,單子typeclasse的實現是由GHC自動完成(使用GeneralizedNewtypeDeriving
語言編譯)。 該單子轉換被定義爲這樣:
newtype RuntimeT m a = RuntimeT {
runRuntimeT :: m (Runtime a)
}
的問題來自我實例化的單子typeclasse的(>> =)功能的方式:
instance (Monad m) => Monad (RuntimeT m) where
return a = RuntimeT $ (return . return) a
x >>= f = runRuntimeT x >>= id >>= f
我看到它的方式,所述第一個>>=
運行在底層m
monad中。因此,runRuntimeT x >>=
返回Runtime a
類型的值(對吧?)。然後,下面的代碼id >>=
應返回類型爲a
的值。該值是傳遞給類型爲f :: (Monad m) => a -> RuntimeT m b
的函數f的值。
問題在於類型問題:f
函數的類型與(>> =)函數所需的類型不匹配。我可以讓這個連貫一致嗎?我可以明白爲什麼這不起作用,但我無法將它變成功能性的東西。
編輯:錯誤消息:
Core.hs:34:4:
Occurs check: cannot construct the infinite type: m = RuntimeT m
When generalising the type(s) for `>>='
In the instance declaration for `Monad (RuntimeT m)'
Failed, modules loaded: none.
謝謝你的幫助,不要猶豫,糾正任何缺陷在我的消息,
查理P.
請向我們顯示您收到的錯誤消息。 – dave4420 2010-04-18 12:58:59