MonadState实例在《Real World Haskell》中无法编译

4

这个MonadState实例是从http://book.realworldhaskell.org/read/monad-transformers.html复制的,但在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个回答

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

在这种情况下,变量s不存在于右侧,并且函数依赖关系从右向左进行,因此您的实例无效。(没有不可判定实例)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接