无法推断(半组(可选a))是由于实例声明的超类引起的 [英] Could not deduce (Semigroup (Optional a)) arising from the superclasses of an instance declaration

查看:87
本文介绍了无法推断(半组(可选a))是由于实例声明的超类引起的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"Haskell编程:从第一原理"中的以下代码无法编译:

The following code from "Haskell Programming: From first principles" fails to compile:

module Learn where
import Data.Semigroup
import Data.Monoid

-- Exercise: Optional Monoid
data Optional a = Nada
                | Only a
                deriving (Eq, Show)

instance Monoid a => Monoid (Optional a) where
 mempty = Nada
 mappend Nada Nada = Nada
 mappend (Only a) Nada = Only $ mappend a mempty
 mappend Nada (Only a) = Only $ mappend mempty a
 mappend (Only a) (Only b) = Only $ mappend a b

它给出了以下错误:

intermission.hs:11:10: error:
    • Could not deduce (Semigroup (Optional a))
        arising from the superclasses of an instance declaration
      from the context: Monoid a
        bound by the instance declaration at intermission.hs:11:10-40
    • In the instance declaration for ‘Monoid (Optional a)’
   |
11 | instance Monoid a => Monoid (Optional a) where
   |   

为了阻止ghc抱怨,我必须创建Optional a的半组实例并定义<>".这对我来说不太有意义,并且想知道我是否忽略了某些事情.

In order to stop ghc from complaining, I had to create a semigroup instance of Optional a and define "<>". This doesn't quite make sense to me and was wondering if there was something I was overlooking.

推荐答案

"注意:自base-4.11.0.0起,半群是Monoid的超类."

其中的超类列表发展缓慢.当提出了新的有用类时,将更新旧类的API以反映它们之间的关系.不幸的是,这会破坏旧代码. Base 4.11.1.0于2018年4月发布,对Monoid进行了重大更改.

The list of superclasses in has been evolving slowly. As new useful classes are proposed, the API for older classes is updated to reflect their relationships. This has the unfortunate effect of breaking older code. Base 4.11.1.0 was released in April of 2018 with this breaking change to Monoid.

这篇关于无法推断(半组(可选a))是由于实例声明的超类引起的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆