[英]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: 所以我的问题是:
a1
in the type signature mean? a1
是什么意思? Is it related to a
? a
有关吗? (==)
some kind of function equality operator? (==)
某种函数相等运算符? Because 0 (==) 0
throws an error in GHCi. 0 (==) 0
在GHCi中引发错误。 1 (1+) 0
mean in this context? 1 (1+) 0
在这种情况下意味着什么? I don't see how this is even a valid expression. True
? True
? 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
不同。
(==)
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
,前缀应用==
。
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
并不意味着什么,三个子表达式中的每一个都是“猫头鹰”的独立参数,最终需要四个参数。
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
. a
和a1
是两个不同的类型变量,就像b
和b1
。 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. 但由于最终类型中没有
b
或c
,我们可以将b1
和c1
重命名为b
和c
,以简化。 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.