scala - 到底什么是幺半群同态?

标签 scala haskell functional-programming category-theory monoids

我从 Monoid Morphisms, Products, and Coproducts 读到了幺半群同态并且无法100%理解。

作者说(强调原创):

The length function maps from String to Int 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 monoids M and N, a homomorphism f: M => N, and all values x:M, y:M, the following equations hold:

f(x |+| y) == (f(x) |+| f(y))

f(mzero[M]) == mzero[N]

他的意思是这样吗,因为数据类型 StringInt 是幺半群,并且函数 length 映射 String => Int 保留幺半群结构(Int 是一个幺半群),这就是所谓的幺半群同态,对吗?

最佳答案

Does he mean, the datatype String and Int are monoid.

,都没有String也不Int是幺半群。幺半群是一个三元组(S, ⊕, e),其中⊕是二元运算符⊕:S×S → S,这样对于所有元素 a, b, c∈S 则有 (a⊕b)⊕c=a⊕(b⊕c),且 e∈S 是“单位元”使得a⊕e=e⊕a=aStringInt是类型,所以基本上是值的集合,但不是三元组。

文章说:

Let's take the String concatenation and Int addition as example monoids that have a relationship.

因此,作者显然还提到了二元运算符(在 (++) 的情况下为 String ,在 (+) 的情况下为 Int )。身份(在 String 的情况下为空字符串,在 0 的情况下 Int )保持隐式;在非正式英语对话中,将身份留给读者作为练习是很常见的。

现在假设我们有两个幺半群结构(M, ⊕, em)(N, ⊗, en),函数 f : M → N (如 length )被称为 monoid homomorphism [wiki]鉴于它成立 f(m1⊕m2)=f(m1)⊗f(m2 ) 对于所有元素 m1, m2∈M 并且该映射还保留单位元素:< em>f(em)=en.

例如length :: String -> Int是幺半群同态,因为我们可以考虑幺半群 ( String , (++) , "" )( Int , (+) , 0 )。认为:

  1. length (s1 ++ s2) == length s1 + length s2 (对于所有 String s s1s2 );和
  2. length "" == 0 .

关于scala - 到底什么是幺半群同态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55993254/

相关文章:

python - cmp 根据评估方法返回两个不同的结果

java - 通过Java触发的Spark程序中自定义log4j进行日志记录

performance - 阅读 GHC 核心

haskell - 如何使用没有 SomeException 作为参数类型的处理程序

haskell - Haskell 中叶子的数量比节点的数量大 1

functional-programming - 如何在Mac上安装标准版本的工作版本?

bash - 如何在 Unix 中获取给定数字的因子?

scala - 解析器组合器未终止 - 如何记录正在发生的事情?

list - 如何在Scala中通过相同元素合并元组

scala - 使用进纸器确保 Gatling 中的 CSV 记录