我正在尝试创建代数类型类的“层次结构”,如下所示:
class Semigroup a where
(.*) :: a -> a -> a
foldr1 (.*) = foldl1 (.*) -- GHCi error: "`foldr1' is not a (visible) method of class `Semigroup'"
class (Semigroup a) => Monoid a where
identity :: a
(.*) identity = id :: a -> a -- GHCi error: "`.*' is not a (visible) method of class `Monoid'"
class (Monoid a) => Group a where
inverse :: a -> a
因此,群是幺半群,而幺半群是半群。但是,我收到类无法看到其父类的功能的错误。
这些错误困扰着我,因为我假设通过编写(例如)class (Semigroup a) => Monoid a
类Monoid a
将能够看到 function (.*)
。此外,foldr1
Prelude 中的类型没有限制,所以我认为这foldr1
在这种情况下会起作用。