[Haskell-beginners] Type of a Group.

Robert Goss goss.robert at gmail.com
Sat Feb 9 14:05:12 CET 2013


Dear all,

Looking to learn a little haskell I tried to program up a little group 
theory but feel I am stuck on what the types should be. At first it 
seemed obvious (and the algebra package does this) that the type of a 
group should be given by:

class Group g where
   mul :: g -> g -> g
   inv :: g -> g
   unit :: g

My problem is this seems to assume that the type of group you are in is 
encoded by the type system.

I first ran into problems with this when I wanted to define a cyclic 
group. The only way I could define the type was either to define each 
cyclic group separately so have C2, C3, C4, ... or parametrise it over 
the class Nat. So a cyclic group would have the type Cyclic (Succ(Succ( 
... Succ(Zero)) ... )) which would consistently define all cyclic groups 
but is hardly any better. For example a computation mod a large prime p 
is not going to be pleasant.

I came up with a partial solution by realising that a group is defined 
as a set X and some operations on it to get

class Group g x where
   mul :: g -> x -> x -> x
   inv :: g-> x -> x
   unit :: g -> x

Making it easy to define cyclic groups and all my old groups carry over. 
But now to evaluate an expression I need to hang onto the group i am in 
and pass it around. As i am newish to haskell I wanted to know if I have 
missed a simpler more obvious way of doing things?

All the best,

Robert Goss




More information about the Beginners mailing list