问题
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,
f
andx
, 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 thata
can be any type, as long as it appears only once in the signature) and the signature look like(a -> b) -> c -> d
- result of
twice
is same as the result of its first argument - that makesd = b
and the signature(a -> b) -> c -> b
f
takes second argument oftwice
as its argument - this makesc = a
and the signature look like this:(a -> b) -> a -> b
twice
is 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