
Hello Haskell-Experts! I was trying to express the notion of a (mathematical) group in Haskell, using type classes, as an exercise. If I am not mistaken, a group can be for example the Reals together with the multiplication operation and "1" as the neutral element. Or, the Reals with addition and "0". my first idea was to write something like class Group a where ... _but_, I thought, I need to parameterise, so to speak, not only using the type "a", but also the group operation. How could I do that in Haskell? Is that even possible? Thanks in advance! Christian

Pragmatically you might want two classes - AdditiveGroup and MultiplicativeGroup. There is a lot of work in this area such as the NumericPrelude on Hackage and Jerzy Karczmarczuk's paper "Functional Programming and Mathematical Objects" [link below]. Numeric hierarchies expose problems for Haskell's class system, hence everyone complains about the Num classes but no-one has been able to fix them. http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.32.3328

Hi,
Math newbie here: what's the difference between a group and a monoid?
Patrick
On Wed, Mar 16, 2011 at 4:49 AM, Christian
Hello Haskell-Experts! I was trying to express the notion of a (mathematical) group in Haskell, using type classes, as an exercise. If I am not mistaken, a group can be for example the Reals together with the multiplication operation and "1" as the neutral element. Or, the Reals with addition and "0". my first idea was to write something like
class Group a where ...
_but_, I thought, I need to parameterise, so to speak, not only using the type "a", but also the group operation. How could I do that in Haskell? Is that even possible?
Thanks in advance! Christian
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

On Wednesday 16 March 2011 13:41:25, Patrick LeBoutillier wrote:
Hi,
Math newbie here: what's the difference between a group and a monoid?
In a group, there are inverses. A group is a monoid in which every element has an inverse. So, natural numbers with addition: monoid, no inverses for n /= 0 Integers with addition: group

...I apologize for starting a new thread in this manner, but it is the only way that seems to work... ...can someone provide me with a reference for QuickCheck? Thank you

On Wed, Mar 16, 2011 at 08:49:27AM +0000, Christian wrote:
Hello Haskell-Experts! I was trying to express the notion of a (mathematical) group in Haskell, using type classes, as an exercise. If I am not mistaken, a group can be for example the Reals together with the multiplication operation and "1" as the neutral element. Or, the Reals with addition and "0". my first idea was to write something like
class Group a where ...
_but_, I thought, I need to parameterise, so to speak, not only using the type "a", but also the group operation. How could I do that in Haskell? Is that even possible?
Yes, this is a problem, and it's not really possible directly. In this sort of situation what you often do is make two *newtypes* which are just wrappers around the type in question, and then make a different Group instance for each. As an example, you can look at Data.Monoid, which defines two newtype wrappers called Sum and Product, with additive and multiplicative Monoid instances respectively. (As an aside, the real numbers do *not* actually form a group under multiplication, since 0 has no inverse. However, the reals without 0 do.) -Brent
participants (6)
-
Brent Yorgey
-
Christian
-
Daniel Fischer
-
Patrick LeBoutillier
-
Patrick Lynch
-
Stephen Tetley