如何理解Control.Arrow的'&&&'的Haskell类型签名操作者 [英] How to make sense of the Haskell type signature for Control.Arrow's '&&&' operator

查看:173
本文介绍了如何理解Control.Arrow的'&&&'的Haskell类型签名操作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图围绕Haskell的 Control.Arrow &&& 的作品,但恐惧我快要失去我的方式了。



具体来说,我很困惑(作为初学者)如何理解它的行为来自类型签名

 (&&&& amp; amp;):: abc  - > a b c' - > ab(c,c')

in

  import Control.Arrow 
(negate&&&(+5))< $> [1,2,3]

甚至只是

 (negate&&(+5))5 


$例如,b $ b

,第一个参数是missing b c ,而第二个参数是缺少 c',结果在我看来像(c,c'),而不是 ab(c,c')



有人可以通过&&& 在其类型的上下文中工作?

解决方案

我一直认为&&& 作为拆分和应用操作。你有一个箭头值,并且你将会应用两个函数(对不起,箭头,但它可以和函数一起工作,并且使得解释更容易),并且保留两个结果,从而将流分开。

b
$ b

死简单的例子:

 λ> (succ&& pred)42 
(43,41)

那里的类​​型,我们已经得到了

  succ&&& pred :: Arrow a,Enum b => ab(b,b)

一个更复杂的例子,它不全是b:

  show&&& (== 42)::箭头a,显示b,公式b,数字b => ab(String,Bool)

所以用纯英文:&& & 接受两个函数,并将它们组合成一个函数,它接受输入,将两个函数应用到它并返回结果对。



<但它是在箭头上定义的,而不是函数。然而,它的工作原理完全相同:它需要两个箭头,并将它们组合成一个箭头来接受输入,同时应用两个箭头并返回结果对。

  arrowOne :: Arrow a => a b c 
arrowTwo :: Arrow a => a b c'
arrowOne&&& arrowTwo :: Arrow a => ab(c,c')

附录:似乎混淆了你的部分是否 a 类型仍然出现在类型签名中。这里的经验法则是,它和你在函数类型中看到 - > 时的工作方式一样:只要它没有被应用,它就会显示。



我记得阅读一些箭头文献,其中写有箭头 b〜> c (注意波浪线不是破折号),而不是 a b c ,以使与函数并行更为明显。


I'm trying to wrap my head around how Haskell's Control.Arrow's &&& works, but fear I'm on the verge of losing my way.

Specifically, I'm confused (as a beginner) by how to makes sense of its behavior from the type signature

(&&&) :: a b c -> a b c' -> a b (c, c')

in

import Control.Arrow
(negate &&& (+5)) <$> [1,2,3]

or even just

(negate &&& (+5)) 5

for example, the first argument is "missing" b and c, while the second is missing just c', and the result looks to me like (c, c'), not a b (c, c').

Can someone walk me through how &&& works, in the context of its type?

解决方案

I always think of &&& as a split and apply operation. You've got an Arrow value, and you're going to apply two functions (sorry, arrows, but it works with functions and makes explanations easier) to it, and keep both results, thus splitting the stream.

Dead simple example:

λ> (succ &&& pred) 42
(43,41)

Walking the type there, we've got

succ &&& pred :: Arrow a, Enum b => a b (b,b)

A more complex example where it's not all b:

show &&& (== 42) :: Arrow a, Show b, Eq b, Num b => a b (String,Bool)

So in plain english: &&& takes two functions, and combines them into a single function that takes it input, applies both functions to it, and returns the result pair.

But it's defined on arrows, not functions. Yet it works exactly the same: it takes two arrows, and combines them into a single arrow that takes its input, applies both arrows to it, and returns the result pair.

arrowOne :: Arrow a => a b c
arrowTwo :: Arrow a => a b c'
arrowOne &&& arrowTwo :: Arrow a => a b (c,c')

Addendum: part of what seems to confuse you is whether or not the a type still appears in the type signatures. The rule of thumb here is that it works the same as for when you see the -> in function types: it shows as long as it's not applied.

I recall reading some Arrow literature that wrote arrows as b ~> c (note the tilde not dash) instead of a b c, to make the parallel with functions more apparent.

这篇关于如何理解Control.Arrow的'&amp;&amp;&amp;'的Haskell类型签名操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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