问题
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.
- your function takes two arguments,
fandx, so the signature will have three parts - say,a -> c -> d - first of these arguments is an unary function - that makes
a = (a -> b)(remember thatacan be any type, as long as it appears only once in the signature) and the signature look like(a -> b) -> c -> d - result of
twiceis same as the result of its first argument - that makesd = band the signature(a -> b) -> c -> b ftakes second argument oftwiceas its argument - this makesc = aand the signature look like this:(a -> b) -> a -> btwiceis applied to its own output, which means thata = b- this makes the final signature(a -> a) -> a -> a
来源:https://stackoverflow.com/questions/39210584/how-to-define-the-type-profile-for-this-function