Scala 组合;共产法?

标签 scala functional-programming category-theory comonad

因此,考虑到 comonad 的这种编码(见下文),它上面的 comonad 定律是否正确?出于某种原因,我不认为他们是从看着他们的,我知道从那里走错路只会导致糟糕的道路,所以我很感激轻推,提示,帮助,回答你有什么。

      /**
       * note: I think these are right
       * Comonad Laws
       * 
       * (i)   counit(cojoin(m))      == m
       * 
       * (ii)  >>(counit(m))(cojoin)  == m
       * 
       * (iii) cojoin(cojoin(m))      == >>(cojoin(m))(cojoin)
       * 
       */

        trait Comonad[M[_]] {

           // map
           def >>[A,B](a: M[A])(f: A => B): B

          // extract | coeta 
          def counit[A](a:M[A]): A

          // cobind | =<< | extend
          def coflatMap[A,B](ma:M[A])(f: M[A] => B): M[B]

          // coflatten | comu
         def cojoin[A](a: M[A]): M[M[A]]
        }

最佳答案

你快到了。两者,(i)(iii)是正确的,但是 (ii)是错的。您可以发现错误,因为 (ii)输入不正确:对于 >>(counit(m)参数 counit(m)有类型 A而不是 M[A] .

您的操作的正确法律是:

   * (i)   counit(cojoin(m))      == m
   * 
   * (ii)  >>(cojoin(m))(counit)  == m
   * 
   * (iii) cojoin(cojoin(m))      == >>(cojoin(m))(cojoin)

因此,对于 (i)(ii) ,法律是适用于 counitcounit 的“ map ”到 cojoin 的结果等价于恒等函数。

关于Scala 组合;共产法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247004/

相关文章:

types - 什么是 "vocabulary types",存在多少?

haskell - 仿函数定律是否证明了结构的完全保存?

scala - Scala 的 Option 以何种方式折叠 catamorphism?

scala - 我可以在 Scala 中有一个负值作为常量表达式吗?

python - 在单次迭代期间应用多个独立过滤器

scala - 如何在 Scala 中定义和使用自定义注解

functional-programming - 方案函数的位置独立参数

f# - 使用延续将二元递归转化为尾递归

scala - 如何编写绑定(bind)集合类型和元素类型的通用 Scala 增强方法?

scala - 在 Scala 中抛出异常的代价