在Scala中的Comonad示例

21

什么是Comonad?如果可能,请使用Scala语法进行描述。 我发现了scalaz库的实现,但不清楚它在哪里有用。


2
参见:Haskell 中的 Comonad 类型类是什么。Scalaz 通常尽可能地使事情像 Haskell 一样,因此这些博客文章可能会有所帮助。 - Dan Burton
另请参阅:/r/haskell 上最近的 Reddit 帖子 一种共单子符号表示法 - Dan Burton
3个回答

12

嗯,单子让你向它们中添加值,并基于从非单子到单子的计算来更改它们。余单子允许您从其中提取值,并基于从余单子到非余单子的计算来更改它们。

自然的直觉是它们通常会出现在您拥有CM[A]并想要提取A的地方。

请参见这篇非常有趣的帖子,它稍微随意地涉及了一些余单子,但至少对我来说,使它们非常清晰明了。


自然的直觉是,它们通常会出现在您拥有 CM[A] 并希望提取 A 的位置。这就是 Copointed/Copure。加上 extract(W[A] => (W[A] => B) => W[B]) 或 cojoin(W[A] => W[W[A]]),就可以得到 Comonad - missingfaktor
@missingfaktor 我没有定义它们 - 我假设Stas已经看过定义了。我是说像这样的情况通常会发现共函子。 - Daniel C. Sobral

7
以下是从这篇博客文章中的代码的逐字翻译。
case class U[X](left: Stream[X], center: X, right: Stream[X]) {
  def shiftRight = this match {
    case U(a, b, c #:: cs) => U(b #:: a, c, cs)
  }

  def shiftLeft = this match {
    case U(a #:: as, b, c) => U(as, a, b #:: c)
  }
}

// Not necessary, as Comonad also has fmap.
/*
implicit object uFunctor extends Functor[U] {
  def fmap[A, B](x: U[A], f: A => B): U[B] = U(x.left.map(f), f(x.center), x.right.map(f))
}
*/

implicit object uComonad extends Comonad[U] {
  def copure[A](u: U[A]): A = u.center
  def cojoin[A](a: U[A]): U[U[A]] = U(Stream.iterate(a)(_.shiftLeft).tail, a, Stream.iterate(a)(_.shiftRight).tail)
  def fmap[A, B](x: U[A], f: A => B): U[B] = U(x.left.map(f), x.center |> f, x.right.map(f))
}

def rule(u: U[Boolean]) = u match {
  case U(a #:: _, b, c #:: _) => !(a && b && !c || (a == b))
}

def shift[A](i: Int, u: U[A]) = {
  Stream.iterate(u)(x => if (i < 0) x.shiftLeft else x.shiftRight).apply(i.abs)
}

def half[A](u: U[A]) = u match {
  case U(_, b, c) => Stream(b) ++ c
}

def toList[A](i: Int, j: Int, u: U[A]) = half(shift(i, u)).take(j - i)

val u = U(Stream continually false, true, Stream continually false)

val s = Stream.iterate(u)(_ =>> rule)

val s0 = s.map(r => toList(-20, 20, r).map(x => if(x) '#' else ' '))

val s1 = s.map(r => toList(-20, 20, r).map(x => if(x) '#' else ' ').mkString("|")).take(20).force.mkString("\n")

println(s1)

输出:

 | | | | | | | | | | | | | | | | | | | |#| | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#|#| | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#| |#| | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#|#|#|#| | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#| | | |#| | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#|#| | |#|#| | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#| |#| |#| |#| | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#|#|#|#|#|#|#|#| | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#| | | | | | | |#| | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#|#| | | | | | |#|#| | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#| |#| | | | | |#| |#| | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#|#|#|#| | | | |#|#|#|#| | | | | | | |
 | | | | | | | | | | | | | | | | | | | |#| | | |#| | | |#| | | |#| | | | | | |
 | | | | | | | | | | | | | | | | | | | |#|#| | |#|#| | |#|#| | |#|#| | | | | |
 | | | | | | | | | | | | | | | | | | | |#| |#| |#| |#| |#| |#| |#| |#| | | | |
 | | | | | | | | | | | | | | | | | | | |#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#| | | |
 | | | | | | | | | | | | | | | | | | | |#| | | | | | | | | | | | | | | |#| | |
 | | | | | | | | | | | | | | | | | | | |#|#| | | | | | | | | | | | | | |#|#| |
 | | | | | | | | | | | | | | | | | | | |#| |#| | | | | | | | | | | | | |#| |#|
 | | | | | | | | | | | | | | | | | | | |#|#|#|#| | | | | | | | | | | | |#|#|#|#

缺少Comonad的导入。=>>|>也未定义。 - EnverOsmanov
1
@EnverOsmanov,这篇文章是我还没有意识到在代码片段中包含导入的重要性的时候写的。无论如何,自那以后Scalaz已经发生了巨大变化,而且软件包已经进行了多次重组。如果您能根据新的Scalaz(或Cats,如果您喜欢)更新此帖子,我将不胜感激。 - missingfaktor

1

scalaz库提供了一个ComonadStore,它扩展了Comonad的属性。它的定义如下:

trait ComonadStore[F[_], S] extends Comonad[F] { self =>

  def pos[A](w: F[A]): S
  def peek[A](s: S, w: F[A]): A

  def peeks[A](s: S => S, w: F[A]): A =
    peek(s(pos(w)), w)

  def seek[A](s: S, w: F[A]): F[A] =
    peek(s, cojoin(w))

  def seeks[A](s: S => S, w: F[A]): F[A] =
    peeks(s, cojoin(w))

  def experiment[G[_], A](s: S => G[S], w: F[A])(implicit FG: Functor[G]): G[A] =
    FG.map(s(pos(w)))(peek(_, w))

}

一个类似于 (S => A, S)Store(仓库)拥有 Comonad 实例。您可以查看这个 问题,更具体地解释了它是什么。

您还有 CoreaderCowriter Comonads,它们是 ReaderWriter Monads 的对偶。这里有一篇优秀的 博客 文章,在 Scala 中讨论了这个问题。


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