应用程序是monad X要共享的内容 [英] Applicative is to monad what X is to comonad

查看:161
本文介绍了应用程序是monad X要共享的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我们可以用X来解释这个公式吗?



解决方案

经过一番思考,我认为这实际上是一个落后的问题。有人可能会认为 ComonadApply Comonad 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注入值是真正的区别。



Comonad
作为分类对偶的定义由 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

*能够定义 apply > extract 是一个符号,表示 class Extend w => Comonad w 可能更可行,但可以将 Monad 分成 class(Applicative f,Bind f)=> ; Monad f ,因此 Comonad (Extend w,Extract w)=> Comonad w ,所以它或多或少会分裂发型。

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 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

As you can see, ComonadApply is merely (Apply w, Comonad w) => w. However, Applicative's ability to inject values into the functor with pure is the real difference.

The definition of a 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

So if we look at the step from 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

Note that we cannot define 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

So 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.

Note that 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    

* Being able to define 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.

这篇关于应用程序是monad X要共享的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆