The type checker is in fact a type inference machine that synthesizes missing type information by type unification. It may be (and often is) used as a type-checking automaton when types are (partially) present.
Each expression must specify its own typeCheck(TypeChecker t) method that encodes a formal typing rule of the form:
is a boolean condition on typing context | (3.4) |
is read thus: ``the expression
has type
under typing context
if the expression
has type boolean under typing context
and if the expressions
and
have
same type
under typing context
.
In its simplest form, a typing context
is logical
variable substitution: i.e., a mapping from type parameters to
types. Since type parameters are also types, we do have to
compose substitutions. In the formal presentation of an expression's
typing rule, the context keeps the type binding under which the typiung
derivation has progressed up to applying the rule in which it occurs.
Hence, we shall treat typing contexts as (composable) substitutions
mapping type parameter variables to types. The notation
denotes the context defined from
by: