如何定义此功能的类型配置文件? [英] How to define the type profile for this function?

查看:138
本文介绍了如何定义此功能的类型配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 两次f x = f(f x);我必须定义这个函数的类型配置文件。 

结果应该如下,但我不明白为什么。

 ('a  - >'a) - > 'a  - > 'a 


解决方案 > a) - > a - >一个是正确的答案。让我们把它分成几部分来找出原因。


  1. 你的函数有两个参数, f x ,因此签名将包含三个部分 - 比如 a - > c - > d

  2. 这些参数中的第一个是一元函数 - 它使得 a =(a - > b) a 可以是任何类型,只要它在签名中只出现一次)并且签名看起来像(a - > ; b) - > c - > d

  3. 结果两次与第一个参数的结果相同 - 这使得 d = b 和签名(a - > b) - > c - > b

  4. f 第二个参数为两次作为它的参数 - 这使得 c = a 并且签名看起来像这样:(a - > b) - > a - > b

  5. 两次应用于其自己的输出,这意味着 a = b - 这使得最终签名(a - > a) - > a - > a


I have to define the type profile of this function:

twice f x = f (f x);

The result should be the following, but I don't really get why.

('a -> 'a) -> 'a -> 'a

解决方案

(a -> a) -> a -> a is the right answer. Let's split it to pieces to find out why.

  1. your function takes two arguments, f and x, so the signature will have three parts - say, a -> c -> d
  2. first of these arguments is an unary function - that makes a = (a -> b) (remember that a can be any type, as long as it appears only once in the signature) and the signature look like (a -> b) -> c -> d
  3. result of twice is same as the result of its first argument - that makes d = b and the signature (a -> b) -> c -> b
  4. f takes second argument of twice as its argument - this makes c = a and the signature look like this: (a -> b) -> a -> b
  5. twice is applied to its own output, which means that a = b - this makes the final signature (a -> a) -> a -> a

这篇关于如何定义此功能的类型配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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