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

Val*_*Dia 3 haskell functional-programming scala scalaz comonad

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

Dan*_*ner 5

作为参考,以下是它们是什么的快速回顾:

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

data RepStore f a = RepStore (Key f) (f    a)
data    Store s a =    Store s       (s -> a)
Run Code Online (Sandbox Code Playgroud)

请注意,特别是

instance Representable (s -> a) where
    type Key (s -> a) = s
    -- ...
Run Code Online (Sandbox Code Playgroud)

所以我们直接拥有,Store s并且RepStore (s ->)几乎完全可以互换。在另一个方向,范畴论告诉我们所有的Representable函子都与函数同构(以它们Key为域),因此RepStore f和Store (Key f ->)是同构的。

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