I've read about monoid homomorphism from Monoid Morphisms, Products, and Coproducts and could not understand 100%.
The author says (emphasis original):
The
length
function maps fromString
toInt
while preserving the monoid structure. Such a function, that maps from one monoid to another in such a preserving way, is called a monoid homomorphism. In general, for monoidsM
andN
, a homomorphismf: M => N
, and all valuesx:M
,y:M
, the following equations hold:f(x |+| y) == (f(x) |+| f(y)) f(mzero[M]) == mzero[N]
Does he mean that, since the datatypes String
and Int
are monoids, and the function length
maps String => Int
preserving the monoid structure (Int
is a monoid), it is called monoid homomorphism, right?
Here are some examples of a monoid: set of integers with operation = addition and identity = zero. set of integers with operation = multiplication and identity = one. set of lists with operation = appending and identity = empty list. set of strings with operation = concatenation and identity = empty string.
There are two main types: group homomorphisms and ring homomorphisms. (Other examples include vector space homomorphisms, which are generally called linear maps, as well as homomorphisms of modules and homomorphisms of algebras.)
A monoid is a set that is closed under an associative binary operation and has an identity element such that for all , . Note that unlike a group, its elements need not have inverses. It can also be thought of as a semigroup with an identity element. A monoid must contain at least one element.
In abstract algebra, a branch of mathematics, a monoid is a set equipped with an associative binary operation and an identity element. For example, the nonnegative integers with addition form a monoid, the identity element being 0.
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
.Datatype cannot be a monoid on its own. For a monoid, you need a data type T
and two more things:
|+|
, that takes two elements of type T
and produces an element of type T
T
, let's call it i
, such that for every element t
of type T
the following holds: t |+| i = i |+| t = t
Here are some examples of a monoid:
Monoid homomorphism
String concatenation monoid can be transformed into integer addition monoid by applying .length
to all its elements. Both those sets form a monoid. By the way, remember that we can't just say "set of integers forms a monoid"; we have to pick an associative operation and a corresponding identity element. If we take e.g. division as operation, we break the first rule (instead of producing an element of type integer, we might produce an element of type float/double).
Method length
allows us to go from a monoid (string concatenation) to another monoid (integer addition). If such operation also preserves monoid structure, it is considered to be a monoid homomorphism.
Preserving the structure means:
length(t1 |+| t2) = length(t1) |+| length(t2) and length(i) = i'
where t1
and t2
represent elements of the "source" monoid, i
is the identity of the "source" monoid, and i'
is the identity of the "destination" monoid. You can try it out yourself and see that length
indeed is a structure-preserving operation on a string concatenation monoid, while e.g. indexOf("a")
isn't.
Monoid isomorphism
As demonstrated, length
maps all strings to their corresponding integers and forms a monoid with addition as operation and zero as identity. But we can't go back - for every string, we can figure out its length, but given a length we can't reconstruct the "original" string. If we could, then the operation of "going forward" combined with the operation of "going back" would form a monoid isomorphism.
Isomorphism means being able to go back and forth without any loss of information. For example, as stated earlier, list forms a monoid under appending as operation and empty list as identity element. We could go from "list under appending" monoid to "vector under appending" monoid and back without any loss of information, which means that operations .toVector
and .toList
together form an isomorphism. Another example of an isomorphism, which Runar mentioned in his text, is String
⟷ List[Char]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With