[英]Understanding how Haskell functions are applied
I am doing Nicta course exercises and there I ran into an example that I don't understand. 我正在做Nicta课程练习,在那里遇到了一个我不理解的例子。 I have two functions and their types are as follows:
我有两个函数,它们的类型如下:
filtering :: Applicative f => (a -> f Bool) -> List a -> f (List a)
(>) :: Ord a => a -> a -> Bool
Then I apply filtering
to (>)
and check the type is GHCi. 然后,将
filtering
应用于(>)
并检查类型是否为GHCi。 the resulting type is : 结果类型为:
filtering (>) :: Ord a => List a -> a -> List a
I don't understand how this result came about. 我不明白这个结果是如何产生的。
To understand what the expression filtering (>)
means, you should know which instance of Applicative is used here. 要了解表达式
filtering (>)
含义,您应该知道此处使用了哪个Applicative实例。
Actually, the instance Applicative ((->) a)
is used here, which specializes the function filtering to the following type (notice that we use (b ->)
instead of ((->) a)
below, which is the same) 实际上,这里使用了实例
Applicative ((->) a)
,它专门将函数过滤指定为以下类型(注意,我们使用(b ->)
代替下面的((->) a)
,这是相同的)
filtering :: (a -> (b -> Bool)) -> List a -> (b -> (List a))
And when applied (>)
, to unify (a -> (b -> Bool))
and (a -> (a -> Bool))
, we know that b must equal to a, so filtering
is specialized to 当应用
(>)
来统一(a -> (b -> Bool))
和(a -> (a -> Bool))
,我们知道b必须等于a,因此filtering
专门用于
filtering :: (a -> (a -> Bool)) -> List a -> (a -> (List a))
And so we get the type of filtering (>)
directly 所以我们直接得到
filtering (>)
类型filtering (>)
filtering (>) :: (Ord a) => List a -> (a -> (List a))
which is just same as what GHCi is given. 这与给出的GHCi相同。
The compiler tries unify two types: a -> (a -> Bool)
with b -> f Bool
. 编译器尝试统一两种类型:
a -> (a -> Bool)
与b -> f Bool
。 It sees that lhs and rhs are functions. 它看到lhs和rhs是函数。 So, it tries unify
a = b
and a -> Bool = f Bool
. 因此,它尝试统一
a = b
和a -> Bool = f Bool
。 The a = b
is unified. a = b
是统一的。 The a -> Bool
equivalent with (->) a Bool
. a -> Bool
与(->) a Bool
等效。 So, from (->) a Bool = f Bool
it gets that f = (->) a
. 因此,从
(->) a Bool = f Bool
得到f = (->) a
。 And if we apply the substitute f = (->) a
to type expression List a -> f (List a)
we'll get: List a -> (->) a (List a)
which equivalent with List a -> a -> List a
如果我们使用替代
f = (->) a
来键入表达式List a -> f (List a)
我们将得到: List a -> (->) a (List a)
与List a -> a -> List a
等价List a -> a -> List a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.