【问题标题】:What is the difference between the Store Comonad and a Representable Store Comonad in functional programming?Store Comonad 和 Representable Store Comonad 在函数式编程中有什么区别?
【发布时间】:2019-06-19 11:44:15
【问题描述】:

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

【问题讨论】:

  • 值得注意的是,在 Cats 中,例如 Store 形式上只是一个专门的 RepresentableStore(其中可表示的函子是 I => _)。我不知道 Haskell 情况的细节,但我猜 adjunctioncomonad 不这样做的唯一原因是历史原因。
  • 感谢@TravisBrown!顺便说一句,Cats 中有一个讨论 PR 介绍 IndexedStore,是否应该在 RepresentableStore 之上构建它,您可能会有一些有用的意见...
  • 感谢指点,没看到PR!

标签: scala haskell functional-programming scalaz comonad


【解决方案1】:

作为参考,以下是它们的简要回顾:

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 是一个适当的概括。

【讨论】:

    猜你喜欢
    • 2018-01-12
    • 2015-02-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2014-07-23
    • 2015-04-05
    • 2019-06-20
    • 2012-01-15
    相关资源
    最近更新 更多