How to define the type profile for this function?

匆匆过客 提交于 2019-12-24 00:01:35

问题


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

回答1:


(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


来源:https://stackoverflow.com/questions/39210584/how-to-define-the-type-profile-for-this-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!