Monoid和应用程序如何连接? [英] How are monoid and applicative connected?

查看:53
本文介绍了Monoid和应用程序如何连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 haskellbook 中有关适用性的内容,并试图理解它.

I am reading in the haskellbook about applicative and trying to understand it.

在书中,作者提到:

因此,对于Applicative,我们对结构和功能有一个Monoid 申请我们的价值观!

So, with Applicative, we have a Monoid for our structure and function application for our values!

monoid如何连接到应用程序?

How is monoid connected to applicative?

推荐答案

备注:我还没有这本书,还有IIRC,至少有一位作者活跃于SO上,应该能够回答这个问题问题.话虽如此,一个monoid(或更确切地说是一个半群)背后的想法是,您有一种方法可以从那个monoid 1 :s >

Remark: I don't own the book (yet), and IIRC, at least one of the authors is active on SO and should be able to answer this question. That being said, the idea behind a monoid (or rather a semigroup) is that you have a way to create another object from two objects in that monoid1:

mappend :: Monoid m => m -> m -> m

那么Applicative的单面体如何?好吧,正如您的报价所说,就其结构而言,它是一个单面体.也就是说,我们以f something开头,以f anotherthing继续,然后我们得到了,您猜到它是f resulthing:

So how is Applicative a monoid? Well, it's a monoid in terms of its structure, as your quote says. That is, we start with an f something, continue with f anotherthing, and we get, you've guessed it a f resulthing:

amappend :: f (a -> b) -> f a -> f b

在继续之前(很短很短的时间),让我们忘记f具有种类* -> *.我们最终会得到什么?

Before we continue, for a short, a very short time, let's forget that f has kind * -> *. What do we end up with?

amappend :: f          -> f   -> f

这是单项结构"部分.这就是Haskell中ApplicativeFunctor之间的区别,因为使用Functor我们没有该属性:

That's the "monodial structure" part. And that's the difference between Applicative and Functor in Haskell, since with Functor we don't have that property:

fmap     ::   (a -> b) -> f a -> f b
--          ^
--       no f here

这也是我们尝试仅将(+)或其他功能与fmap一起使用时遇到麻烦的原因:在单个fmap之后,我们将陷入困境,除非我们可以以某种方式应用我们的新功能在这种新结构中.这将我们带到您的问题的第二部分:

That's also the reason we get into trouble if we try to use (+) or other functions with fmap only: after a single fmap we're stuck, unless we can somehow apply our new function in that new structure. Which brings us to the second part of your question:

因此,有了Applicative,我们就可以将函数应用程序用于我们的值!

So, with Applicative, we have [...] function application for our values!

功能应用程序是($).如果我们看一下<*>,我们可以立即看到它们是相似的:

Function application is ($). And if we have a look at <*>, we can immediately see that they are similar:

($)   ::   (a -> b) ->   a ->   b
(<*>) :: f (a -> b) -> f a -> f b

如果我们忘记了(<*>)中的f,我们只会得到($).因此,(<*>)只是在我们的结构上下文中的函数应用程序:

If we forget the f in (<*>), we just end up with ($). So (<*>) is just function application in the context of our structure:

increase  :: Int -> Int
increase x = x + 1

five :: Int
five = 5

increaseA :: Applicative f => f (Int -> Int)
increaseA = pure increase

fiveA :: Applicative f => f Int
fiveA = pure 5

normalIncrease      = increase   $  five
applicativeIncrease = increaseA <*> fiveA

我猜这就是作者对功能应用程序"的含义.我们突然可以接受那些隐藏在我们结构中的功能,并将其应用于我们结构中的其他值.而且由于单调性质,我们停留在该结构中.

And that's, I guessed, what the author meant with "function application". We suddenly can take those functions that are hidden away in our structure and apply them on other values in our structure. And due to the monodial nature, we stay in that structure.

话虽这么说,但我个人绝不将其称为单项式,因为<*>不能对相同类型的两个参数进行运算,并且一个应用程序缺少空元素.

That being said, I personally would never call that monodial, since <*> does not operate on two arguments of the same type, and an applicative is missing the empty element.

1 对于一个真正的半群/monoid,该操作应该是关联的,但这在这里并不重要

1 For a real semigroup/monoid that operation should be associative, but that's not important here

这篇关于Monoid和应用程序如何连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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