幺半群同态和同构

5

我正在阅读《Scala编程》(红皮书)。

在关于单子的章节中,我理解什么是单子同态,例如:字符串单子 M 通过连接和长度函数 f 保留了单子结构,因此它们是同态的。

M.op(f(x), f(y)) == M.op(f(x) + f(y))
// "Lorem".length + "ipsum".length == ("Lorem" + "ipsum").length

引用书中的话(从记忆中,如有错误请指正):
当这种情况在两个方向上发生时,它被命名为单子同构,这意味着对于单子M,N和函数f、g,f andThen g和g andThen f是identity函数。例如,具有连接的字符串Monoid和List[Char] Monoid是同构的。
但我看不到实际的例子来证明这一点,我只能想到f是length函数,但g会发生什么?
注意:我看过这个问题:What are isomorphism and homomorphisms

3
你看过这个吗?https://dev59.com/0lMI5IYBdhLWcg3wir6J - slouc
1
啊哈!不是被选中的答案,而是这个 https://dev59.com/0lMI5IYBdhLWcg3wir6J#55993551,在 Monoid同构 中对我有用。所以,在这种情况下,fg将会是toVectortoList,对吗? - Alejandro Alcalde
1
哦,那个是我的,谢谢! :) 是的,没错。 - slouc
1
@slouc 我在那里给你点了赞;-) - Alejandro Alcalde
1
谢谢 :) 很高兴看到你的回答能帮助到人们。干杯! - slouc
2个回答

5
为了看到StringList [Char]之间的同态,我们有toList:String -> List [Char]mkString:List [Char] -> Stringlength是从String幺半群到自然数加法幺半群的同态映射。
String幺半群的一些内同态映射示例包括toUpperCasetoLowerCase
对于列表,我们有许多同态映射,其中许多只是fold的不同版本。

2

这是siyopao的答案,用ScalaCheck程序表达

object IsomorphismSpecification extends Properties("f and g") {
  val f: String => List[Char] = _.toList
  val g: List[Char] => String = _.mkString

  property("isomorphism") = forAll { (a: String, b: List[Char]) =>
    (f andThen g)(a) == a && (g andThen f)(b) == b
  }
}

输出的是
+ f and g.isomorphism: OK, passed 100 tests.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接