应用程序是monad X要共享的内容 [英] Applicative is to monad what X is to comonad
问题描述
我们可以用X来解释这个公式吗?
经过一番思考,我认为这实际上是一个落后的问题。有人可能会认为 正如您所见, ComonadApply
是 Applicative
是 Monad
,但事实并非如此。但是为了看到这一点,让我们使用PureScript的类型类层次结构:
pre codelass c Functor f其中
fmap ::(a - > b) - > f a - > f b
class Functor f =>应用f,其中
apply :: f(a - > b) - > f a - > f b - (*)
class Apply f =>应用f其中
pure :: a - > f a
class Applicative m => Monad m其中
绑定:: m a - > (a - > m b) - > m b - (>> =)
- join :: m(m a) - > ma
- join = flip bind id
ComonadApply
仅仅是(Apply w,Comonad w)=>瓦特
。但是, Applicative
pure
向functor注入值是真正的区别。
return
的s双解压缩
和绑定
的双重扩展
(或通过重复
作为加入
的双重)的替代定义: class Functor w => Comonad w where
extract :: w a - >
extend ::(w a - > b) - > w a - > w b
- 扩展f = fmap f。复制k
- 复制:: w a - > w(wa)
- duplicate = extend id
所以如果我们看一下步骤从 Applicative
到 Monad
,逻辑步骤将是一个类型类型,其中纯
'dual:
class Apply w =>提取w,其中
extract :: w a - > a
class Extract w => comonad w其中
扩展::(w a - > b) - > w a - > wb
请注意,我们无法定义 extract
in extend
或 duplicate
的条款,我们也不能定义纯粹
/ 返回
根据绑定
或连接
,so这似乎是合乎逻辑的一步。 apply
在这里大多不相关;它可以被定义为 Extract
或 Monad
,只要它们的定律成立:
applyC f = fmap $ extract f - Comonad variant;实际上只需要提取(*)
applyM f = bind f。翻转fmap - Monad变种;我们需要连接或绑定
所以 Extract
(获取值)是 Comonad
应用
Monad
。 申请
或多或少是一个快乐的小事故。 Hask中的类型是否有 Extract
,但不是 Comonad
(或 Extend
但不是 Comonad
,见下文),但我想这些都很少。 请注意, Extract
不存在 - mdash;。但是 2010年报告 Applicative >。此外,任何既是 Extract
和 Applicative
的实例的类型都是 Monad 和一个
Comonad
,因为您可以定义绑定
和扩展依据
摘录
和纯粹
:
bindC :: Extract w => w a - > (a - > w b) - > w b
bindC k f = f $摘录k
extendM ::应用w => (w a - > b) - > w a - > wb
extendM fk =纯$ fk
*能够定义 Can we solve this equation for X ? Applicative is to monad what X is to comonad
After giving it some thought, I think this is actually a backward question. One might think that As you can see, The definition of a So if we look at the step from Note that we cannot define So Note that * Being able to define 这篇关于应用程序是monad X要共享的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! apply >
class Extend w => Comonad w
可能更可行,但可以将 Monad
分成 class(Applicative f,Bind f)=> ; Monad f
,因此 Comonad
到(Extend w,Extract w)=> Comonad w
,所以它或多或少会分裂发型。
ComonadApply
is to Comonad
what Applicative
is to Monad
, but that is not the case. But to see this, let us use PureScript's typeclass hierarchy:class Functor f where
fmap :: (a -> b) -> f a -> f b
class Functor f => Apply f where
apply :: f (a -> b) -> f a -> f b -- (<*>)
class Apply f => Applicative f where
pure :: a -> f a
class Applicative m => Monad m where
bind :: m a -> (a -> m b) -> m b -- (>>=)
-- join :: m (m a) -> m a
-- join = flip bind id
ComonadApply
is merely (Apply w, Comonad w) => w
. However, Applicative
's ability to inject values into the functor with pure
is the real difference.Comonad
as the categorical dual consists of return
's dual extract
and bind
's dual extend
(or the alternative definiton via duplicate
as join
's dual):class Functor w => Comonad w where
extract :: w a -> a
extend :: (w a -> b) -> w a -> w b
-- extend f = fmap f . duplicate k
-- duplicate :: w a -> w (w a)
-- duplicate = extend id
Applicative
to Monad
, the logical step between would be a typeclass with pure
's dual:class Apply w => Extract w where
extract :: w a -> a
class Extract w => Comonad w where
extend :: (w a -> b) -> w a -> w b
extract
in terms of extend
or duplicate
, and neither can we define pure
/return
in terms of bind
or join
, so this seems like the "logical" step. apply
is mostly irrelevant here; it can be defined for either Extract
or Monad
, as long as their laws hold:applyC f = fmap $ extract f -- Comonad variant; needs only Extract actually (*)
applyM f = bind f . flip fmap -- Monad variant; we need join or bind
Extract
(getting values out) is to Comonad
what Applicative
(getting values in) is to Monad
. Apply
is more or less a happy little accident along the way. It would be interesting whether there are types in Hask that have Extract
, but not Comonad
(or Extend
but not Comonad
, see below), but I guess those are rather rare.Extract
doesn't exist—yet. But neither did Applicative
in the 2010 report. Also, any type that is both an instance of Extract
and Applicative
automatically is both a Monad
and a Comonad
, since you can define bind
and extend
in terms of extract
and pure
:bindC :: Extract w => w a -> (a -> w b) -> w b
bindC k f = f $ extract k
extendM :: Applicative w => (w a -> b) -> w a -> w b
extendM f k = pure $ f k
apply
in terms of extract
is a sign that class Extend w => Comonad w
could be more feasible, but one could have split Monad
into class (Applicative f, Bind f) => Monad f
and therefore Comonad
into (Extend w, Extract w) => Comonad w
, so it's more or less splitting hair.