Infer the type of this expression:
proc (f,g,p,x)
if (p (f x))
then (g 1 x)
else add1((f x))
This is what we know (we will denote the type of a given expression as t:<exp>):
So the result is a proc-type with four type parameters and a return type.
To determine the final result, we must determine the type of each of the four parameters, and the type of the
proc's body. So we add the following five statements to our list of items to solve:
We discover types by recursing through the AST.
We can immediately infer that t:1 is an int. So we now know that:
What we know about t:if is:
So we substitute those expressions into the equation:
p must return a bool.
add1 takes an int as a parameter, thus f must return an int.
add1 returns an int, thus g must return an int.
f returns an int, thus p must take an int.
t:true-exp = t:false-exp = int, thus t:body = t:if = int.
We can now make the following substitutions (shown in red):
Since there are no other inferences that we can make in this expression, we conclude that it is polymorphic in x, and the final answer is:
t:proc = ( ( t:x -> int ) * ( int * t:x -> int ) * ( int -> bool ) * t:x -> int )