2013-08-01 19 views
1

我只是好奇爲什麼這段代碼不能編譯。爲什麼它需要米是MonadReader ConnState ?:將MonadReader實例添加到ReaderT包裝器

newtype Netbeans m a = Netbeans { unNetbeans :: ReaderT ConnState m a } 
          deriving (Monad, Functor, Applicative, MonadIO, MonadTrans) 

instance Monad m => MonadReader ConnState (Netbeans m) where 
    ask = Netbeans $ ask 
    local f x = Netbeans $ mapReaderT (local f) (unNetbeans x) 

該錯誤消息是以下的(線88是與本地函數定義行):

src/Vim/Netbeans.hs:88:40: 
Could not deduce (MonadReader ConnState m) 
    arising from a use of `local' 
from the context (Monad m) 
    bound by the instance declaration at src/Vim/Netbeans.hs:86:10-54 
Possible fix: 
    add (MonadReader ConnState m) to the context of 
    the type signature for 
     local :: (ConnState -> ConnState) -> Netbeans m a -> Netbeans m a 
    or the instance declaration 
    or add an instance declaration for (MonadReader ConnState m) 
In the first argument of `mapReaderT', namely `(local f)' 
In the second argument of `($)', namely 
    `mapReaderT (local f) (unNetbeans x)' 
In the expression: Netbeans $ mapReaderT (local f) (unNetbeans x) 

預先感謝。

回答

2

原因是,您正在將local f應用於ReaderT轉換器中的基本單元m。相反,它應該看起來就像

local f x = Netbeans $ local f (unNetbeans x) 
+0

它看起來像我沒有理由的過於複雜的東西。 –

相關問題