来自Real World Haskell的MonadState实例无法编译

时间:2013-06-27 18:50:49

标签: haskell monad-transformers

http://book.realworldhaskell.org/read/monad-transformers.html复制的这个MonadState实例给出了GHC 7.4.2的错误

instance (MonadState s m) => MonadState s (MaybeT m) where
  get = lift get
  put k = lift (put k)

给出

    Illegal instance declaration for `MonadState s (MaybeT m)'
  (All instance types must be of the form (T a1 ... an)
   where a1 ... an are *distinct type variables*,
   and each type variable appears at most once in the instance head.
   Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `MonadState s (MaybeT m)'

如果我添加XFlexibleInstances,我会被告知要添加XUndecidableInstances - 我认为我不应该在这里需要这些扩展。如何编译这个实例?

1 个答案:

答案 0 :(得分:5)

当您查看http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/src/Control-Monad-State-Class.html#MonadState时,您会发现它也用于“官方”实施,所以我猜它是必需的。评论说它与覆盖条件有关,这在这些stackoverflow问题中有解释:

在这种情况下,变量s不在右侧,并且功能依赖性从右向左,因此您的实例无效。 (没有UndecidableInstances)