简体   繁体   English

有人可以解释((。)$(。))(==)1(1+)0的含义

[英]Can someone explain the meaning of ((.)$(.)) (==) 1 (1+) 0

On haskell.org I came across this point free style function, dubbed "the owl". haskell.org上,我遇到了这个自由风格的功能,被称为“猫头鹰”。

((.)$(.))

Its type signature is (a -> b -> c) -> a -> (a1 -> b) -> a1 -> c . 它的类型签名是(a -> b -> c) -> a -> (a1 -> b) -> a1 -> c

It's equivalent to fabcd = ab (cd) and apparently, ((.)$(.)) (==) 1 (1+) 0 returns True . 它相当于fabcd = ab (cd) ,显然, ((.)$(.)) (==) 1 (1+) 0返回True

So my questions are: 所以我的问题是:

  1. What does the a1 in the type signature mean? 类型签名中的a1是什么意思? Is it related to a ? 它与a有关吗?
  2. Is (==) some kind of function equality operator? (==)某种函数相等运算符? Because 0 (==) 0 throws an error in GHCi. 因为0 (==) 0在GHCi中引发错误。
  3. What does 1 (1+) 0 mean in this context? 1 (1+) 0在这种情况下意味着什么? I don't see how this is even a valid expression. 我不知道这甚至是一个有效的表达方式。
  4. Why does the expression return True ? 为什么表达式返回True
  1. The a1 is "just another type variable". a1是“另一种类型变量”。 It could mean anything, including a , but doesn't necessarily mean anything. 它可能意味着什么,包括a ,但并不一定意味着什么。 Most likely it is different from a . 最有可能的是它与a不同。

  2. (==) is the "forced prefix" form of == the regular equality operator form the Eq type class. (==)(==)强制前缀形式的==正则相等运算符形成Eq类型类。 Normally you'd write a == b , but that's just syntax sugar for (==) ab , the prefix application of == . 通常你会写a == b ,但是这只是语法糖(==) ab ,前缀应用==

  3. 1 (1+) 0 doesn't mean anything in particular in this context, each of the three subexpressions is an independent argument to "the owl", which ultimately takes four arguments. 在这种情况下, 1 (1+) 0并不意味着什么,三个子表达式中的每一个都是“猫头鹰”的独立参数,最终需要四个参数。

  4. We can walk through the reduction. 我们可以逐步减少。

     ((.)$(.)) (==) 1 (1+) 0 === [ apply ] ((.)(.)) (==) 1 (1+) 0 === [ implicit association ] ((.)(.)(==)) 1 (1+) 0 === [ apply the definition: (fg) x = f (gx) ] ((.) (1 ==)) (1+) 0 === [ implicit association ] ((.) (1 ==) (1+)) 0 === [ apply the definition: (fg) x = f (gx) ] 1 == (1+0) === [addition] 1 == 1 === [equality] True 

As this page mentions, the owl is equivalent to a function f 本页所述,猫头鹰相当于一个函数f

f a b c d = a b (c d)

which is to say it applies its first argument, a function of two arguments, to its second argument and the result of applying its third argument to its fourth. 也就是说它将第一个参数(两个参数的函数)应用于其第二个参数以及将其第三个参数应用于其第四个参数的结果。 For the example given ((.)$(.)) (==) 1 (1+) 0 that means you first apply (+1) to 0 , then combine the 1 and the (1+0) using (==) which is what happened in our reduction. 对于给出的示例((.)$(.)) (==) 1 (1+) 0表示您首先将(+1)应用于0 ,然后使用(==)1(1+0) (==)这是我们减少的事情。

More broadly, you might think of it as a function which modifies a binary operation a to take a slight variation on its second argument. 更广泛地说,您可能会将其视为一个修改二元运算a的函数,以便对其第二个参数稍作修改。

First, let's write _B fgx = f (gx) = (f . g) x . 首先,让我们写_B fgx = f (gx) = (f . g) x

Since f $ x = fx , we have (.)$(.) = _B $ _B = _B _B . 由于f $ x = fx ,我们有(.)$(.) = _B $ _B = _B _B Its type is derived mechanically, as 它的类型是机械推导出来的

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

1. (.) ::  (b1 -> c1) -> ((a1 -> b1) -> (a1 -> c1))

2. (.) (.) :: {b ~ b1 -> c1, c ~ (a1 -> b1) -> (a1 -> c1)} (a -> b) -> (a -> c)

           :: (a -> b1 -> c1) -> a -> (a1 -> b1) -> (a1 -> c1)
           :: (a -> b  -> c ) -> a -> (a1 -> b ) ->  a1 -> c  

a and a1 are two distinct type variables, just like b and b1 . aa1是两个不同的类型变量,就像bb1 But since there's no b or c in the final type, we can rename b1 and c1 back to just b and c , to simplify. 但由于最终类型中没有bc ,我们可以将b1c1重命名为bc ,以简化。 But not a1 . 但不是a1

We can read this type, actually: it get f :: a -> b -> c a binary function; 实际上我们可以读取这种类型:它得到f :: a -> b -> c二进制函数; x :: a an argument value, g :: a1 -> b a unary function, and another value y :: a1 , and combines them in the only possible way so that the types fit: x :: a参数值, g :: a1 -> b一元函数,另一个值y :: a1 ,并以唯一可能的方式组合它们以使类型适合:

    f x       :: b -> c
    g y       :: b
    f x (g y) ::      c

The rest is already answered. 其余的已经回答了。 Reductions are usually easier to follow in combinatory equations, like _B _B fxgy = _B (fx) gy = fx (gy) , just by two applications of _B 's definition (we can always add as many arguments as we need there). 在组合方程中通常更容易遵循减少,例如_B _B fxgy = _B (fx) gy = fx (gy) ,只需通过_B定义的两个应用程序(我们总是可以在那里添加尽可能多的参数)。

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

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