使用具有haskeline的mtl MonadReader具有类型错误

时间:2016-06-18 00:48:49

标签: haskell monad-transformers haskeline

我希望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

1 个答案:

答案 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相同。