我希望InputT (ReaderT Int IO) a
使用MonadReader Int
。
我在下面编写代码,在InputT
上创建实例MonadReader{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
module HaskelineMonadReader where
import Control.Monad.Reader
import System.Console.Haskeline
instance MonadReader r m => MonadReader r (InputT m) where
ask = lift ask
local = mapInputT . local
main :: IO ()
main = putStrLn "HI"
但我得到了这种类型错误。
HaskelineMonadReader.hs:11:13:
Couldn't match type ‘m0 a0 -> m0 a0’ with ‘forall b. m b -> m b’
Expected type: (m0 a0 -> m0 a0) -> InputT m a -> InputT m a
Actual type: (forall b. m b -> m b) -> InputT m a -> InputT m a
Relevant bindings include
local :: (r -> r) -> InputT m a -> InputT m a
(bound at HaskelineMonadReader.hs:11:5)
In the first argument of ‘(.)’, namely ‘mapInputT’
In the expression: mapInputT . local
如何解决此错误。
完整源代码为here
答案 0 :(得分:1)
编译:
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
module HaskelineMonadReader where
import Control.Monad.Reader
import System.Console.Haskeline
instance MonadReader r m => MonadReader r (InputT m) where
ask = lift ask
local f = mapInputT (local f)
main :: IO ()
main = putStrLn "HI"
<强>更新强>
以下是错误消息:
Couldn't match type ‘m0 a0 -> m0 a0’
with ‘forall b. m b -> m b’
Expected type: (m0 a0 -> m0 a0) -> InputT m a -> InputT m a
Actual type: (forall b. m b -> m b) -> InputT m a -> InputT m a
因此,预期类型更为通用,因为m0
不必须与m
相同。在实际类型中,m
中的m b
必须与m
中的m a
相同。