因此,考虑到 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)
,法律是适用于 counit
或 counit
的“ map ”到 cojoin
的结果等价于恒等函数。
关于Scala 组合;共产法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247004/