Store Comonad 和 Representable Store Comonad 在函数式编程中有什么区别?

What is the difference between the Store Comonad and a Representable Store Comonad in functional programming?

Representable Store Comonad 和 Store Comonad 提供了类似的功能...我们应该在什么时候使用一个而不是另一个,有什么好处?

作为参考,这里快速回顾一下它们是什么:

class {- ... => -} Representable f where
    type Key f
    -- ...

data RepStore f a = RepStore (Key f) (f    a)
data    Store s a =    Store s       (s -> a)

特别注意

instance Representable (s -> a) where
    type Key (s -> a) = s
    -- ...

所以我们直接知道 Store sRepStore (s ->) 几乎完全可以互换。另一方面,范畴论告诉我们所有 Representable 函子都与函数同构(以它们的 Key 作为定义域),因此 RepStore fStore (Key f ->) 是同构的。

总而言之:在大多数情况下,您选择哪个并不重要。如果您打算只在函数上使用它,您不妨使用 Store 并受益于它的语法轻巧;如果你想使用一些不完全是函数的可表示函子(比如,记忆函数或类似的东西),那么 RepStore 是一个合适的概括。