Re: [Haskell-beginners] How to manage typeclass hierarchies and instances?

-- in Semigroup.hs
class Semigroup a where (|.|) :: a -> a -> a
instance Semigroup Integer where (|.|) = (+)
-- In Monoid.hs
class (Semigroup a) => Monoid a where identity :: a
instance Monoid Integer where identity = 0
-- In Group.hs
class (Monoid a) => Group a where inverse :: a -> a
instance Group Integer where (|.|) = (+) identity = 0 inverse = (-)
In Group.hs I'm trying to create an (additive) group instance for Integer values but GHC complains that (|.|) and identity are not "visible" typeclass methods.
You only get one instance per type, so the Semigroup/Monoid instances for Integer are "set in stone." When you "import Semigroup" and "import Monoid", those instances come into scope. So in Group.hs, '|.|' and 'identity' are already defined for Integer. All you need is,
instance Group Integer where inverse = negate
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? Thanks, Stu

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 =)

On Sat, Feb 21, 2015 at 6:44 PM, Michael Orlitzky
I don't think you can do that
You can't, at present. (Note: Typeclasses are not OOP classes; `class (Num a, Group a) => Additive a` does not declare superclasses, but prerequisites.) -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On 22 February 2015 at 00:04, Brandon Allbery
You can't, at present. (Note: Typeclasses are not OOP classes; `class (Num a, Group a) => Additive a` does not declare superclasses, but prerequisites.)
In logic we would say (Num a AND Group a) is a necessary condition for ( Additive a) => (Num a AND Group a) to hold. -- This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman http://www.dit.ie/grangegorman

OPPS! My previous email should have be stated as a question rather than a
statement!
class (Num a, Group a) => Additive a
Or maybe given ( Additive a) => (Num a AND Group a).
Then (Num a AND Group a) is a necessary condition for (Additive a) to
hold.
On 22 February 2015 at 07:53, PATRICK BROWNE
On 22 February 2015 at 00:04, Brandon Allbery
wrote: You can't, at present. (Note: Typeclasses are not OOP classes; `class (Num a, Group a) => Additive a` does not declare superclasses, but prerequisites.)
In logic we would say (Num a AND Group a) is a necessary condition for ( Additive a) => (Num a AND Group a) to hold.
-- This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman http://www.dit.ie/grangegorman
participants (4)
-
Brandon Allbery
-
Michael Orlitzky
-
PATRICK BROWNE
-
Stuart Hungerford