为什么这个函数的类型(a - > a) - >一个? [英] Why is the type of this function (a -> a) -> a?

查看:217
本文介绍了为什么这个函数的类型(a - > a) - >一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个函数的类型(a - > a) - > a?

  Prelude>让y f = f(y f)
前奏> :t y
y ::(t - > t) - > t

不应该是无限/递归类型吗?
我会试着把它认为应该是的类型,但我不能这样做。

  y ::(t  - > t) - > ?WTFIsGoingOnOnTheRHS? 

我没有得到f(y f)如何解析为一个值。以下对我更有意义:

  Prelude>让y f x = f(y f)x 
前奏> :(a - > b) - > a - > b) - > a - > b

但它仍然令人无法容易地混淆。怎么回事?

(a - > b) - >对于某些 a b c code>我们还不知道;毕竟,它需要一个函数 f ,并将其应用于参数,因此它必须是一个函数。



由于 yf = fx (同样,对于一些 x ),我们知道返回类型 y 必须是 f 本身的返回类型。因此,我们可以细化一下 y 的类型:它必须是(a - > b) - > b 对于某些 a b 我们还不知道。



为了弄清楚 a 是什么,我们只需要看看传给的值的类型, ˚F。它是 y f ,这是我们试图找出现在类型的表达式。我们说 y 的类型是(a - > b) - > b (对于某些 a b 等),所以我们可以说这个 yf 的应用程序必须是 b 本身。



<因此, f 的参数类型是 b 。把它们全部放在一起,我们得到(b - > b) - > b - 当然,它与(a - > a) - >相同。一个



以下是一个更直观但不太准确的事物视图:我们说 yf = f (yf),我们可以扩展到等价的 yf = f(f(yf)) yf = f (f(f(yf))),依此类推。因此,我们知道我们总是可以在整个事情中应用另一个 f ,并且因为所讨论的整体是应用 f 到一个参数, f 必须有 a - >一个;并且由于我们只是得出结论,整个事情是将 f 应用于参数的结果,所以返回类型 y 必须是 f 本身 - 再次一起作为(a - > a) - > a


Why is the type of this function (a -> a) -> a?

Prelude> let y f = f (y f)
Prelude> :t y
y :: (t -> t) -> t

Shouldn't it be an infinite/recursive type? I was going to try and put into words what I think it's type should be, but I just can't do it for some reason.

y :: (t -> t) -> ?WTFIsGoingOnOnTheRHS?

I don't get how f (y f) resolves to a value. The following makes a little more sense to me:

Prelude> let y f x = f (y f) x
Prelude> :t y
y :: ((a -> b) -> a -> b) -> a -> b

But it's still ridiculously confusing. What's going on?

解决方案

Well, y has to be of type (a -> b) -> c, for some a, b and c we don't know yet; after all, it takes a function, f, and applies it to an argument, so it must be a function taking a function.

Since y f = f x (again, for some x), we know that the return type of y must be the return type of f itself. So, we can refine the type of y a bit: it must be (a -> b) -> b for some a and b we don't know yet.

To figure out what a is, we just have to look at the type of the value passed to f. It's y f, which is the expression we're trying to figure out the type of right now. We're saying that the type of y is (a -> b) -> b (for some a, b, etc.), so we can say that this application of y f must be of type b itself.

So, the type of the argument to f is b. Put it all back together, and we get (b -> b) -> b — which is, of course, the same thing as (a -> a) -> a.

Here's a more intuitive, but less precise view of things: we're saying that y f = f (y f), which we can expand to the equivalent y f = f (f (y f)), y f = f (f (f (y f))), and so on. So, we know that we can always apply another f around the whole thing, and since the "whole thing" in question is the result of applying f to an argument, f has to have the type a -> a; and since we just concluded that the whole thing is the result of applying f to an argument, the return type of y must be that of f itself — coming together, again, as (a -> a) -> a.

这篇关于为什么这个函数的类型(a - &gt; a) - &gt;一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆