[英]Polymorphic type of functions as parameter in haskell?
Im trying to define the polymorphic type of the following function: 我试图定义以下函数的多态类型:
flip f x y = f y x
My thought was the following: 我的想法是:
1st parameter of flip
, f
takes two arguments so (t1 -> t2 -> t3)
flip
第一个参数f
接受两个参数,因此(t1 -> t2 -> t3)
2nd parameter of flip
, x
is of type t1
because of the parameter t1
inside f
function. flip
第二个参数,由于f
函数中的参数t1
, x
为t1
类型。
3rd parameter of flip
, y
which is of type t3
because of the parameter t3
inside f
function. flip
第三个参数y
,由于f
函数中的参数t3
,其类型为t3
。
I don't know the polymorphic type of the overall return. 我不知道整体回报的多态类型。
But when I checked the type in the ghci, I get: 但是,当我在ghci中检查类型时,我得到:
flip :: (t2 -> t1 -> t) -> t1 -> t2 -> t
Can someone please help go through this example was to whats happening here? 有人可以帮忙看看这个例子是怎么回事吗?
Thanks 谢谢
Your second assumption is wrong : 您的第二个假设是错误的 :
2nd parameter of flip, x is of type t1 because of the parameter t1 inside f function.flip的第二个参数,由于f函数中的参数t1,x的类型为t1。
Let us first analyze the function: 让我们首先分析一下功能:
flip f x y = f y x
We see that flip
has three arguments in the head. 我们看到该
flip
在头部具有三个参数。 So we first make the type: 因此,我们首先进行输入:
flip :: a -> (b -> (c -> d))
We will of course now aim to fill in the types. 当然,我们现在的目标是填写类型。 With:
带有:
f :: a
x :: b
y :: c
flip f x y :: d
We see on the right hand side: 我们在右侧看到:
(f y) x
So that means that f
is a function that takes as input y
. 因此,这意味着
f
是一个将y
作为输入的函数。 So that means that a
is the same type as c -> e
(or shorter a ~ c -> e
). 因此,这意味着
a
与c -> e
(或更短a ~ c -> e
)。
So now: 所以现在:
flip :: (c -> e) -> (b -> (c -> d))
f :: (c -> e)
x :: b
y :: c
Furthermore we see that: 此外,我们看到:
(f x) y
So the result of (fx)
is another function, with as input y
. 因此
(fx)
的结果是另一个函数,输入y
。 So that means that e ~ b -> f
. 因此,这意味着
e ~ b -> f
。 Thus: 从而:
flip :: (c -> (b -> f)) -> (b -> (c -> d))
f :: c -> (b -> f)
x :: b
y :: c
Finally we see that (fy) x
is the result of flip fxy
. 最终,我们看到
(fy) x
是flip fxy
的结果。 So that means that the type of the result of (fy) x
is the same type as d
. 因此,这意味着
(fy) x
的结果类型与d
相同。 So that means that f ~ d
. 因此,这意味着
f ~ d
。 Which thus means that: 因此,这意味着:
flip :: (c -> (b -> d)) -> (b -> (c -> d))
Or if we drop some redundant brackets: 或者,如果我们删除一些多余的括号:
flip :: (c -> b -> d) -> b -> c -> d
This is just a matter of solving a system of equations. 这只是解决方程组的问题。 First, assign unknown types:
首先,分配未知类型:
f : a1
x : a2
y : a3
Next, f
is applied to y
. 接下来,将
f
应用于y
。 So, f
must be a function type with argument of the same type as y, that is 因此,
f
必须是带有与y相同类型的参数的函数类型,即
f : a1 = a3 -> a4
f y : a4
Similarily, fy
is applied to x
, so 同样,
fy
应用于x
,所以
f y : a4 = a2 -> a5
f y x : a5
Substituting this back, we get 换回去,我们得到
f : a3 -> a2 -> a5
x : a2
y : a3
We can rename these types 我们可以重命名这些类型
t2 = a3
t1 = a2
t = a5
and get 并得到
f : t2 -> t1 -> t
x : t1
y : t2
The function body is fyx
, which has type t = a5
. 函数主体为
fyx
,类型为t = a5
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.