What is monoid homomorphism exactly?

前端 未结 4 817
鱼传尺愫
鱼传尺愫 2021-01-31 13:54

I\'ve read about monoid homomorphism from Monoid Morphisms, Products, and Coproducts and could not understand 100%.

The author says (emphasis original):

4条回答
  •  天涯浪人
    2021-01-31 14:11

    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 and Int 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:

    1. length (s1 ++ s2) == length s1 + length s2 (for all Strings s1 and s2); and
    2. length "" == 0.

提交回复
热议问题