【问题标题】:Monoid homomorphism and isomorphismMonoid 同态和同构
【发布时间】:2019-09-11 11:19:17
【问题描述】:

我正在阅读“Scala 编程”一书(红皮书)。

在关于 Monoid 的章节中,我了解了 Monoid 同态是什么,例如:String Monoid M with concatenation and length function f 保留了幺半群结构,因此是同态的。

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

引用这本书(凭记忆,如果我错了,请纠正我:

当这发生在两个方向上时,它被命名为 Monoid isomorphisim,这意味着对于 monoids M, N,函数 f, gf andThen gg andThen fidentity 函数。例如String Monoid 和 List[Char] Monoid 连接是同构的。

但是我看不到实际的例子,我只能将f 视为length 函数,但是g 会发生什么?

注意:我看过这个问题:What are isomorphism and homomorphisms

【问题讨论】:

  • 啊哈!,不是选择的答案,但这个stackoverflow.com/a/55993551/1612432,在 Monoid 同构 中是我的答案。所以,在这种情况下,fg 将是 toVectortoList,对吧?
  • 哦,那是我的,谢谢! :) 是的,没错。
  • @slouc 我在那儿给你投了赞成票 ;-)
  • 谢谢 :) 很高兴看到您的回答对人们有所帮助。干杯!

标签: scala functional-programming monoids isomorphism homomorphism


【解决方案1】:

要查看StringList[Char] 之间的同构,我们有toList: String -> List[Char]mkString: List[Char] -> String

length 是从字符串幺半群到加法的自然数幺半群的同态。

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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-18
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      相关资源
      最近更新 更多