无法满足超类

时间:2015-07-30 23:52:34

标签: haskell

我有以下代码,我无法弄清楚为什么我收到下面显示的第一个错误。据我所知,MonadState实例应该得到满足。即使我明确添加了" non-deducible"实例约束中字符的实例字符,我仍然得到错误。这是一个错误吗?我该如何解决这个问题?

Windows GHC版本7.8.3

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}

module Test where

import Control.Applicative
import Control.Lens
import Control.Lens.Internal.Zoom
import Control.Monad.State


newtype Engine st m a = Engine {
    unEngine :: StateT st m a
} deriving (Functor, Applicative, Monad, MonadState st, MonadTrans, MonadIO)


type instance Zoomed (Engine st m) = Focusing m


-- Same error message even with: (Monad m, MonadState st' (Engine st' m)) =>
instance (Monad m) => Zoom (Engine st m) (Engine st' m) st st' where
    zoom l = Engine . zoom l . unEngine
Test.hs:22:10:
    Could not deduce (mtl-2.1.3.1:Control.Monad.State.Class.MonadState
                        st' (Engine st' m))
      arising from the superclasses of an instance declaration
    from the context (Monad m)
      bound by the instance declaration at Test.hs:22:10-62
    In the instance declaration for
      Zoom (Engine st m) (Engine st' m) st st'

Test.hs:23:23:
    Could not deduce (Zoomed (StateT st' m) ~ Focusing m)
    from the context (Zoomed (Engine st m) ~ Zoomed (Engine st' m),
                      mtl-2.1.3.1:Control.Monad.State.Class.MonadState st (Engine st m),
                      mtl-2.1.3.1:Control.Monad.State.Class.MonadState
                        st' (Engine st' m),
                      Monad m)
      bound by the instance declaration at Test.hs:22:10-62
    Relevant bindings include
      l :: LensLike' (Zoomed (Engine st m) c) st' st
        (bound at Test.hs:23:10)
      zoom :: LensLike' (Zoomed (Engine st m) c) st' st
              -> Engine st m c -> Engine st' m c
        (bound at Test.hs:23:5)
    In the first argument of `(.)', namely `zoom l'
    In the second argument of `(.)', namely `zoom l . unEngine'
    In the expression: Engine . zoom l . unEngine

Test.hs:23:23:
    Could not deduce (Zoom (StateT st m) (StateT st' m) st st')
      arising from a use of `zoom'
    from the context (Zoomed (Engine st m) ~ Zoomed (Engine st' m),
                      mtl-2.1.3.1:Control.Monad.State.Class.MonadState st (Engine st m),
                      mtl-2.1.3.1:Control.Monad.State.Class.MonadState
                        st' (Engine st' m),
                      Monad m)
      bound by the instance declaration at Test.hs:22:10-62
    In the first argument of `(.)', namely `zoom l'
    In the second argument of `(.)', namely `zoom l . unEngine'
    In the expression: Engine . zoom l . unEngine

Test.hs:23:28:
    Could not deduce (Zoomed (StateT st m) ~ Focusing m)
    from the context (Zoomed (Engine st m) ~ Zoomed (Engine st' m),
                      mtl-2.1.3.1:Control.Monad.State.Class.MonadState st (Engine st m),
                      mtl-2.1.3.1:Control.Monad.State.Class.MonadState
                        st' (Engine st' m),
                      Monad m)
      bound by the instance declaration at Test.hs:22:10-62
    Expected type: LensLike' (Zoomed (StateT st m) c) st' st
      Actual type: LensLike' (Zoomed (Engine st m) c) st' st
    Relevant bindings include
      l :: LensLike' (Zoomed (Engine st m) c) st' st
        (bound at Test.hs:23:10)
      zoom :: LensLike' (Zoomed (Engine st m) c) st' st
              -> Engine st m c -> Engine st' m c
        (bound at Test.hs:23:5)
    In the first argument of `zoom', namely `l'
    In the first argument of `(.)', namely `zoom l'

1 个答案:

答案 0 :(得分:4)

您的代码适用于我,也适用于GHC 7.8.3。但是,请仔细查看错误消息:

Test.hs:22:10:
    Could not deduce (mtl-2.1.3.1:Control.Monad.State.Class.MonadState
                        st' (Engine st' m))

mtl-2.1.3.1等显式包标识符通常不包含在错误中。当它们 时,这意味着GHC已经加载了两个不同版本的包,具有相同名称但不同的类型和类。在这种情况下,一个可能由lens使用,另一个可能由您的模块直接使用。

Cabal沙箱是避免这种情况的一种方法(它们确保一致的软件包版本)。