Skip to content

Commit 957718f

Browse files
authored
Merge pull request #1 from rightfold/master
Name laws and add Unit and Dual instances
2 parents d67cf02 + 699f9f2 commit 957718f

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

src/Data/Group.purs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,35 @@
1515
module Data.Group where
1616

1717
import Data.Monoid (class Monoid)
18+
import Data.Monoid.Dual (Dual(..))
1819
import Data.Ring (class Ring, negate)
1920
import Data.Monoid.Additive (Additive(..))
21+
import Data.Unit (Unit, unit)
2022

2123
-- | A `Group` is a `Monoid` with inverses. Instances
2224
-- | must satisfy the following law in addition to the monoid laws:
2325
-- |
24-
-- | ```text
25-
-- | forall x. ginverse x <> x = mempty = x <> ginverse x
26-
-- | ```
26+
-- | - Inverse: `forall x. ginverse x <> x = mempty = x <> ginverse x`
2727
class Monoid g <= Group g where
2828
ginverse :: g -> g
2929

30-
-- | A `CommutativeGroup` is a `Group` with a commutative monoid operation.
31-
-- | Instances must satisfy the following law in addition to the group laws:
30+
-- | A `CommutativeGroup` is a `Group` with a commutative semigroup operation.
31+
-- | Also known as an Abelian group. Instances must satisfy the following law
32+
-- | in addition to the group laws:
3233
-- |
33-
-- | ```text
34-
-- | forall x, y. x <> y = y <> x
35-
-- | ```
34+
-- | - Commutativity: `forall x, y. x <> y = y <> x`
3635
class Group g <= CommutativeGroup g
3736

37+
instance unitIsGroup :: Group Unit where
38+
ginverse _ = unit
39+
40+
instance unitIsCommutativeGroup :: CommutativeGroup Unit
41+
42+
instance dualGroupsAreGroups :: (Group g) => Group (Dual g) where
43+
ginverse (Dual x) = Dual (ginverse x)
44+
45+
instance dualCommutativeGroupsAreCommutativeGroups :: (CommutativeGroup g) => CommutativeGroup (Dual g)
46+
3847
instance ringsAreAdditiveGroups :: (Ring r) => Group (Additive r) where
3948
ginverse (Additive x) = Additive (negate x)
4049

0 commit comments

Comments
 (0)