(var) ---------------- x : A |- x : A (abs) ------------------ |- \x.x : A -> A

- Does a term always has a type?
- If a term has a type, is this type unique?
- If the type is not unique, is there a sort of "most natuaral type" that one can choose?
- We have seen how to use the system so to prove that a given term has a given type (type checking). Is it possible also to use the system to infer a type for a term (type inference)?
- Given a type T, does it always exists a term which has type T?
- What is the relation between types and reduction? Namely, is the type of a term preserved under reduction?

(var) -------------------------- x : B -> B |- x : B -> B (abs) -------------------------------- |- \x.x : (B -> B) -> (B -> B)In a sense, however, the previous type (

**Theorem** If a lambda term has a type, then
it has a principal type, unique modulo renaming.

We don't give a formal proof of this theorem, but we show
how to construct the principal type. Let us start with
the example of the term `\x.x`. Intuitively,
any proof of a type statement for `\x.x` must have the
following form:

(var) ---------------- A = B x : A |- x : B (abs) ---------------- C = A -> B |- \x.x : CThe equation

the principal type ofIf, on the contray, the generic proof cannot be built, or the equations are not solvable, thenMisA theta.

`[True]`, i.e.`\x\y.x`, has principal type`A -> B -> A`.`[False]`, i.e.`\x\y.y`, has principal type`A -> B -> B`.`[1]`, i.e.`\x\y.x y`, has principal type`(A -> B) -> A -> B`.

- To find a proof for
`|- M : A`, where`A`is a given type. This process is called*Type Checking*. - To derive a (principal)
type for
`M`, by constructing a proof and imposing the conditions as explained above. This process is called*Type Inference*.

(remember that- val f = fn x => x;

(whereval f = fn : 'a -> 'a

Note that
`x x` represents
the application of a generic function
`x` to itself.
There are functions
for which it makes sense to
be applied to themselves (for instance
the identity function), but this is not
the case for all functions.

From a semantic point of view, note that if
`A -> B` is to be interpreted
as the set of functions from a set `A` to a set
`B`, then there is no non-trivial set `A`
which can be equal to the set `A -> B`, for cardinality reasons.
In fact, if `A` has cardinality n, and `B`
has cardinality m,
then `A -> B` has cardinality m^{n}.
In domain theory, where it is desirable that such equations have
a solution, `A -> B` is assumed to represent not all functions
from `A` to
`B`, but only a particular class of them.

- Any type variable
`A` `A -> B`, for any distinct type variables`A`and`B``A -> A -> B`, for any distinct type variables`A`and`B`

the inhabited types are exactly the formulas which are valid in the intuitionistic propositional logicwhere

**Definition**
Given a set of variables Var, and a set of function symbols Fun
(possibly including constant symbols) the
first-order terms are defined by the following grammar:

Term ::= Var | Fun(Term,...,Term)

In the case of type expressions, we have only one binary function symbol (represented in infix notation): the arrow ->.

**Definition**
A substitution theta is any mapping
theta : Var -> Term.

A substitution theta
will be denoted by listing explicitly
the result of its application to every variable (usually
we are interested only in finite substitutions, i.e. substitutions
which affect only a finite number of variables). More precisely, a substitution theta such that
theta(x_{1}) = t_{1},..., theta(x_{n}) = t_{n},
will be denoted by [t_{1}/x_{1},..., t_{n}/x_{n}].

The application of a substitution theta to a term t, denoted by t theta, is the term obtained from t by replacing symultaneously each variable x by theta(x).

The composition of two substitutions sigma and theta is the substitution sigma theta s.t. for every variable x, (sigma theta)(x) = (x sigma)theta.

**Definition**
Given a set of equations on terms
E = {t_{1} = u_{1}, ... , t_{n} = u_{n}},
a substitution theta is a *unifier*
(solution) for E
iff for each i we have that
t_{i}theta is identical to
u_{i}theta.
A substitution theta is the
*most general unifier* of E
if it is a unifier for E
and, for any other unifier sigma,
there exists sigma`'` s.t.
sigma = theta sigma`'` (i.e.
`sigma` can be obtained by
instantiating theta).

**Example**
Consider the set of equations

We have thatE = {A = B->C , B->C = C->B}

- theta =
`[B->B/A , B/C]`is a unifier of E. - theta
`'`=`[B->C/A , B/C]`is**not**a unifier of E (the substitution must be applied simultaneously to all the equations). - sigma =
`[(D->D)->(D->D)/A , D->D/B , D->D/C]`is also a unifier of E. - theta is more general than
sigma in fact,
`sigma = theta [D->D/B]`.

There are various algoriths to find the most general unifier for a set of first order equations; for instance, the algorithm of Martelli-Montanari (see [Apt_Pellegrini, Section 2]. Note that in this reference they use a reversed notation for subsitution: x/t insteand of t/x).

**Proposition**
The algorithm of Martelli-Montanari always terminates
and it gives a most general unifier
if the set of equations is solvable, failure otherwise.

**Corollary** Given a set of first order equations E,
it is decidable whether E is solvable or not, and, if it is
solvable, then it has a most general unifier (which is
unique modulo renaming).

- ML uses:
- First order unification for type inference at compile time (transparent to the user)
- Pattern matching (a sort of one-way unification) for evaluating function calls, at run time

- Prolog uses first order unification for evaluating goals, at run time. For efficiency reasons the occur-check is usually not implemented.
- Lambda Prolog uses:
- First order unification for type checking at compile time (transparent to the user)
- Higher Order Unification (unification of Higher-Order
terms modulo alpha, beta and eta conversion) for evaluating goals,
at run time.

HO unification is a conservative extension of FO unification, in the sense that if we give to lambda Prolog a FO equation, then we get back the FO most general unifier. Those who are interested can find the algorithm for HO unification in [G. Huet, A Unification Algorithm for Typed lambda-Calculus,*Theoretical Computer Science*1:27-57, 1973.]

------------------- E = B x:B, x:D |- x : E ------------------- C = D -> E x:B |- \x.x : C ------------------- A = B -> C |- \xx.x : Awhich would make you conclude that the type of the

This error comes from the fact that there are two assumptions with
the same variable` x `(`x:B, x:D`) in one of the assertion.
This should never happen, because if you get to that point it means that
you don't know anymore which variable (in the assumption) represents the
variable with the same name in the body.

In general, you should use different names for all the bound variables, and this problem will never arise.

Another solution would be to modify the abstraction rule so to introduce a sort of rule of scoping. Maybe we will see this technique later in the course.