I\'ve read about monoid homomorphism from Monoid Morphisms, Products, and Coproducts and could not understand 100%.
The author says (emphasis original):
Does he mean, the datatype String and Int are monoid.
No, neither String
nor Int
are monoids. A monoid is a 3-tuple (S, ⊕, e) where ⊕ is a binary operator ⊕ : S×S → S, such that for all elements a, b, c∈S it holds that (a⊕b)⊕c=a⊕(b⊕c), and e∈S is an "identity element" such that a⊕e=e⊕a=a. String
and Int
are types, so basically sets of values, but not 3-tuples.
The article says:
Let's take the
String
concatenation andInt
addition as example monoids that have a relationship.
So the author clearly also mentions the binary operators ((++)
in case of String
, and (+)
in case of Int
). The identities (empty string in case of String
and 0
in case of Int
) are left implicit; leaving the identities as an exercise for the reader is common in informal English discourse.
Now given that we have two monoid structures (M, ⊕, em) and (N, ⊗, en), a function f : M → N (like length
) is then called a monoid homomorphism [wiki] given it holds that f(m1⊕m2)=f(m1)⊗f(m2) for all elements m1, m2∈M and that mapping also preserves the identity element: f(em)=en.
For example length :: String -> Int
is a monoid homomorphism, since we can consider the monoids (String
, (++)
, ""
) and (Int
, (+)
, 0
). It holds that:
length (s1 ++ s2) == length s1 + length s2
(for all String
s s1
and s2
); andlength "" == 0
.