Commutative monoid from 'algebra' package on Hackage

China☆狼群 提交于 2019-12-07 03:30:40

问题


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.


回答1:


(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.




回答2:


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


来源:https://stackoverflow.com/questions/12289746/commutative-monoid-from-algebra-package-on-hackage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!