哈斯克尔的康纳德类型是什么?

哈斯克尔的康纳德类型是什么?就像 Comonad 的 控件。共生包中的共生体一样(也欢迎解释任何其他提供 Comonad 类型类的包)。我隐约听说过 Comonad,但我真正知道的是它提供了 extract :: w a -> a,有点类似于 Monad 的 return :: a -> m a

注意到 Comonad 在“真实”代码中的“真实生活”用法的额外好处。

18413 次浏览

These links may be helpful:

  1. Evaluating cellular automata is comonadic. In particular, "whenever you see large datastructures pieced together from lots of small but similar computations there's a good chance that we're dealing with a comonad".
  2. Sequences, streams, and segments
  3. Comonads in everyday life

This doesn't fully answer my question, but I wanted to put some relevant information in answer format:

"co" (loosely) means "flip the arrows". Here's a rough visual of that.

Consider the monadic operations:

return :: a ~> m a
flip (>>=) :: (a ~> m b) -> (m a ~> m b)

Reverse the squiggly arrows and you get the comonadic operations:

extract :: a <~ w a
extend :: (a <~ w b) -> (w a <~ w b)

(Written with normal arrows)

extract :: w a -> a
extend :: (w a -> b) -> w a -> w b

Notice how in this format, return is an arrow that just so happens to fit in the argument slot for flip (>>=), and the same is true of extract and extend. Monad/comonad laws say that when you put return or extract into that slot, the result is the identity arrow. The laws are the same, "just with the arrows flipped". That's a super handwavey answer but hopefully it provides some insight.