As a toy project, I would like to understand how to model math groups in Haskell in general.
To get us started, we start by observing that a, which will be defined Group, is simply a Monoidwith inversion.
class (Monoid m) => Group m where
minvert :: m -> m
Then we first restrict ourselves to cyclic groups and begin by defining a cyclic group of order 12.
data Cyclic12 = Cyclic12 Int deriving (Show, Eq)
Finally, we instantiate both classes for Cyclic12.
instance Monoid Cyclic12 where
mempty = Cyclic12 0
mappend (Cyclic12 x) (Cyclic12 y) = Cyclic12 ((x + y) `mod` 12)
instance Group Cyclic12 where
minvert (Cyclic12 x) = Cyclic12 ((0 - x) `mod` 12)
How can I separate the previous definition from a specific value of 12 to allow a more general definition of various cyclic groups?
Ideally, I would like to write type definitions
instance Monoid (Cyclic k) where
mempty = Cyclic k 0
mappend (Cyclic k x) (Cyclic k y) = Cyclic k ((x + y) `mod` k)
instance Group (Cyclic k) where
minvert (Cyclic k x) = Cyclic k ((0 - x) `mod` k)
But with data definition, for example
data Cyclic = Cyclic Int Int deriving (Show, Eq)
, k " ". , , . .