[Haskell-beginners] How to manage typeclass hierarchies and instances?
Michael Orlitzky
michael at orlitzky.com
Sat Feb 21 23:44:21 UTC 2015
On 02/21/2015 04:15 PM, Stuart Hungerford wrote:
>
> Thanks for that insight. Ignoring the instance issue for a minute,
> suppose I wanted to define a typeclass Additive where I'd like to set
> default implementations for the methods:
>
> class (Num a, Group a) => Additive a where
> (|.|) = (+)
>
> -- etc...
>
> In this situation GHC says (|.|) is not a visible method. If I add a
> type declaration for (|.|) am I effectively "shadowing" the method
> from Semigroup with a new method rather than supplying a default
> implementation?
>
I don't think you can do that, but I wouldn't swear to it. One problem
would be that you could declare a Semigroup instance of Integer
independently of the Additive instance, and GHC would have to choose
which operation to use. In this case Additive is more specific than
Semigroup, but my instinct would be to prefer the Semigroup "override."
I think some of these things are addresses in,
https://ghc.haskell.org/trac/ghc/wiki/IntrinsicSuperclasses
but I've never made it through the whole document =)
More information about the Beginners
mailing list