This is an example of a monoid in Haskell:
> import Data.Monoid
> Sum 5 <> Sum 6 <> Sum 10
Sum {getSum = 21}
> mconcat [Sum 5, Sum 6, Sum 10]
Sum {getSum = 21}
> getSum $ mconcat $ map Sum [5, 6, 10]
21
> getProduct $ mconcat $ map Product [5, 6, 10]
300
This is an example of a monoid in Clojure:
(defn plus-monoid
([]
0)
([a b]
(+ a b)))
(plus-monoid)
(plus-monoid 3 4)
(reduce plus-monoid [2 3 4])
This is an example of a ring in Haskell:
module Rings where
newtype Matrix r = M [[r]] deriving (Eq,Show)
instance Num r => Num (Matrix r) where
M [[a,b],[c,d]] + M [[a',b'],[c',d']] = M [[a+a',b+b'],[c+c',d+d']]
negate (M [[a,b],[c,d]]) = M [[-a,-b],[-c,-d]]
M [[a,b],[c,d]] * M [[e,f],[g,h]] = M [[a*e+b*g, a*f+b*h] ,[c*e+d*g, c*f+d*h]]
fromInteger n = M [[fromInteger n, 0],[0, fromInteger n]]
> M [[1,2],[3,4]] - M [[1,0],[0,1]]
M [[0,2],[3,3]]
> M [[2,0],[0,3]] * M [[1,2],[3,4]]
M [[2,4],[9,12]]
This is an example of a ring in Clojure based on this:
(defprotocol ring
(plus [x y])
(mult [x y])
(neg [x])
(zero [])
(one []) )
It seems to be - (to borrow Java parlance) that the difference between a ring and a monoid is that the ring has an "additional method on the interface to implement". (Perhaps I'm mistaken). Now to me this would have an impact on associativity - but I haven't got my head around the full implications of this.
My question is: What are the implications of the differences between a monoid and a ring?