简体   繁体   English

Haskell:为什么((。)。(。))fg等于f。 GX?

[英]Haskell: Why is ((.).(.)) f g equal to f . g x?

Could you please explain the meaning of the expression ((.).(.))? 你能解释一下表达式的含义((。)。(。))吗? As far as I know (.) has the type (b -> c) -> (a -> b) -> a -> c. 据我所知(。)有类型(b - > c) - >(a - > b) - > a - > c。

(.) . (.) (.) . (.) is the composition of the composition operator with itself. (.) . (.)是组合运算符本身的组合。

If we look at 如果我们看一下

((.) . (.)) f g x

we can evaluate that a few steps, first we parenthesise, 我们可以评估几个步骤,首先我们括起来,

((((.) . (.)) f) g) x

then we apply, using (foo . bar) arg = foo (bar arg) : 然后我们申请,使用(foo . bar) arg = foo (bar arg)

~> (((.) ((.) f)) g) x
~> (((.) f) . g) x
~> ((.) f) (g x)
~> f . g x

More principled, 更有原则的,

(.) :: (b -> c) -> (a -> b) -> (a -> c)

So, using (.) as the first argument of (.) , we must unify 因此,使用(.)作为第一个参数(.)我们必须统一

b -> c

with

(v -> w) -> (u -> v) -> (u -> w)

That yields 产量

b = v -> w
c = (u -> v) -> (u -> w)

and

(.) (.) = ((.) .) :: (a -> v -> w) -> a -> (u -> v) -> (u -> w)

Now, to apply that to (.) , we must unify the type 现在,要将其应用于(.) ,我们必须统一类型

a -> v -> w

with the type of (.) , after renaming 重命名后的(.)类型

(s -> t) -> (r -> s) -> (r -> t)

which yields 产量

a = s -> t
v = r -> s
w = r -> t

and thus 因此

(.) . (.) :: (s -> t) -> (u -> r -> s) -> (u -> r -> t)

and from the type we can (almost) read that (.) . (.) 从类型我们可以(几乎)读取(.) . (.) (.) . (.) applies a function (of one argument) to the result of a function of two arguments. (.) . (.)将函数(一个参数)应用于两个参数的函数的结果。

You've got an answer already, here's a slightly different take on it. 你已经有了答案,这里有一个不同的看法。

In combinatory logic (.) is B -combinator : Babc = a(bc) . 组合逻辑 (.)B- Babc = a(bc)Babc = a(bc) When writing combinator expressions it is customary to assume that every identifier consists of one letter only, and omit white-space in application, to make the expressions more readable. 在编写组合子表达式时,习惯上假设每个标识符仅由一个字母组成,并在应用程序中省略空格,以使表达式更具可读性。 Of course the usual currying applies: abcde is (((ab)c)d)e and vice versa. 当然通常的currying适用: abcde(((ab)c)d)e ,反之亦然。

(.) is B , so ((.) . (.)) == (.) (.) (.) == BBB . (.)B ,所以((.) . (.)) == (.) (.) (.) == BBB So, 所以,

BBBfgxy = B(Bf)gxy = (Bf)(gx)y = Bf(gx)y = (f . g x) y    
 abc        a  bc                 a b  c                  

We can throw away both y s at the end (this is known as eta-reduction : Gy=Hy --> G=H , if y does not appear inside H 1 ). 我们可以扔掉两个y s(这被称为eta-reductionGy=Hy - > G=H ,如果y不出现在H 1中 )。 But also, another way to present this, is 但是,提出这个问题的另一种方式是

BBBfgxy = B(Bf)gxy = ((f .) . g) x y = f (g x y)     -- (.) f == (f .)
-- compare with:       (f .) g x = f (g x)

((f .) . g) xy might be easier to type in than ((.).(.)) fgxy , but YMMV. ((f .) . g) xy可能比((.).(.)) fgxy更容易输入,但是YMMV。


1 For example, with S combinator , defined as Sfgx = fx(gx) , without regard for that rule we could write 1例如,使用S组合子 ,定义为Sfgx = fx(gx) ,不考虑该规则我们可以写

Sfgx = fx(gx) = B(fx)gx = (f x . g) x
Sfg = B(fx)g = (f x . g)   --- WRONG, what is "x"?

which is nonsense. 这是胡说八道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM