What is monoid homomorphism exactly?

前端 未结 4 818
鱼传尺愫
鱼传尺愫 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:18

    trait Monoid[T] {
    def op(a: T, b: T): T
    def zero: T
    }
    
    val strMonoid = new Monoid[String] {
      def op(a: String, b: String): String = a ++ b
      def zero: String = ""
    }
    
    val lcMonoid = new Monoid[List[Char]] {
      def op(a: List[Char], b: List[Char]): List[Char] = a ::: b
      def zero = List.empty[Char]
    }
    

    homomorphism via function f

    f{M.op(x,y)} = N.op(f(x),g(y))
    
    for example, using toList available on String
    
    //in REPL
    scala> strMonoid.op("abc","def").toList == lcMonoid.op("abc".toList,"def".toList)
    res4: Boolean = true
    

    isomorphism via functions f and g

    given bi-directional homomorphism between monoids M and N,

    f{M.op(x,y)} = N.op(f(x),f(y))
    g{N.op(x,y)} = M.op(g(x),g(y))
    

    And if both (f andThen g) and (g andThen f) are identify functions, then monoids M and N are isomorphic via f and g

    g{f{M.op(x,y)}} = g{N.op(f(x),f(y))} = M.op(g(f(x)),g(f(y))) = M.op(x,y)
    

    for example, using toList available on String and toString available on List[Char] (where toList andThen toString and toString andThen toList are identity functions)

    scala> ( strMonoid.op("abc","def").toList ).toString == ( lcMonoid.op("abc".toList,"def".toList) ).toString
    res7: Boolean = true 
    

提交回复
热议问题