Commutative monoid from 'algebra' package on Hackage
Asked Answered
N

2

6

The documentation for algebra/2.1.1.2/doc/html shows a colossal number of type classes.

How do I declare that a structure in question must be equipped with a commutative associative operation and a unit/identity element, but without anything else (inverses, distributivity etc)?

I'm thinking of

reduce :: Monoid m => (a -> m) -> [a] -> m

but instances of Data.Monoid are not supposed to be commutative and I want users of my function to see that they need commutativity for the function to work by looking at the type.

Numb answered 5/9, 2012 at 21:3 Comment(0)
I
8

(Abelian m, Monoidal m)

It might seem that Monoidal is much more than you want, but it is all based on Natural being a Semiring.

Inhambane answered 5/9, 2012 at 21:29 Comment(0)
S
1

It looks like that package provides a Commutative class, so correct me if I'm wrong, but it looks like it's just a matter of specifying a second typeclass:

reduce :: (Monoid m, Commutative m) => (a -> m) -> [a] -> m
Serum answered 5/9, 2012 at 21:26 Comment(1)
Commutative talks about the action of the Multiplicative class, not Monoid. You could use (Commutative m, Unital m) to get a multiplicative commutative monoid that works with (*) and one, or (Abelian m, Monoidal m) to get a commutative monoid that works with (+) and zero.Headrest

© 2022 - 2024 — McMap. All rights reserved.