We start as usual with a set of type constants or nullary type constructors and an innite set of type variables as shown below.
2 Type-Constructors = fint;
bool;:::
g (2.4) , 2Type-Variables = ft;t
0;t
1;t
2;:::
g (2.5) Using these, we dene the type expressions of the language: 2Types ::=j
j
1 !2(2.6)
2Type-Schemes ::=j 8
:
1 (2.7)The function type constructor (!), associates to the right. Also note that Types are not quantied, while the Type-Schemes are universally quantied at the outermost. A type scheme
8
1:
82:
8n:
is also written as 812n:
.2.3.2 Some Notations and Denitions
First, we dene some general set notation to help us later.
Notation 2.1
IfA
andB
are sets, then,1. powerset(
A
) is dened to be the set of all subsets ofA
. 2.A
+B
is the disjoint union ofA
andB
.3.
A
nB
is the set dierenceA
,B
.4.
A
B
is the cartesian product ofA
andB
.5.
A
!B
is a set of functions fromA
toB
. The precise interpretation of (!) depends on the characteristics of the setsA
andB
. For example, ifA
andB
are simple syntactic sets, thenA
!B
merely denotes the set of all total functions fromA
toB
; ifA
andB
are scott-domains, thenA
!B
denotes the set of continuous functions fromA
toB
, and so on.6.
A
,,n!B
is the set of nite maps7 fromA
toB
.Notation 2.2
Iff
andg
are functions (maps) then, 1. Dom(f
) is the Domain off
.2. Ran(
f
) is the Range off
.3.
f
2A
!B
means thatf
is a function fromA
toB
as determined by the interpretation of the setA
!B
.4.
f
2A
,,n!B
means thatf
can be written as fa
1 7!b
1;:::;a
i 7!b
i;:::;a
n7!b
ng wherea
i2A
andb
i2B
.5. fgrepresents the empty map.
6.
f
g
(readf composed with g
) is the composite map dened by (f
g
)(a
) =f
(g
(a
)).7.
f
+g
(readf modied by g
) is the map with domain Dom(f
) [Dom(g
) dened by (f
+g
)(a
) = ifa
2Dom(g
) theng
(a
) elsef
(a
).8. If
f
andg
have disjoint domains thenf
jg
(readf simultaneously composed with
g
) is the map with domain Dom(f
) Dom(g
) formed by the concatenation of the two maps.This serves in place of either
f
org
and emphasizes the disjointness of their respective domains.9.
f
nA is the mapf
with its domain restricted to Dom(f
)nA
. 10.f
#A is the mapf
with its domain restricted toA
.We can relate the identiers of the language to the types assigned to them in a type envi-ronment, which is a nite map from identiers to type-schemes.
TE 2Type-Environments = Identiers,,n!Type-Schemes (2
:
8) Next, we dene some notation and operations on type expressions which would be useful later.Denition 2.3
Given a type scheme =81n:
, 1. 1;:::;
n arebound
in .2. A type variable
isfree
in if it is not bound and it occurs in .3. A type variable
isfree
in a type environment TE, if it is free in some type scheme from its range Ran(TE).In short, the quantied type variables are bound and the others are free in a type scheme, just as one might expect. Using the above denition, we can dene the following functions for computing free and bound variables of types, type schemes and type environments:
Denition 2.4
The function FV 2Types!powerset(Type-Variables) computes the free type variables of a type expression as follows:1. FV(
) is the set of all the type variables in type , all of which are free.2. By extension, FV(
) and FV(TE) denote the set of free type variables in and TE respectively as dened above in denition 2.3.Denition 2.5
The function BV 2 Type-Schemes ! powerset(Type-Variables) gives the set of bound variables of the type scheme .If FV(
) =then is called aground type
ormonotype
. Similarly , if FV() = and FV(TE) =, then and TE are said to beclosed
, respectively. When it is clear by context that we are talking about only free type variables of an object, we may also use the symbol tyvars() instead of FV().Now we dene the important concept of substituting free and bound type variables in type and type schemes with other types.
Denition 2.6
ASubstitution
S 2 Type-Variables ! Types is a map from type variables to types. Moreover,1. By extension, a substitution S is applied to a type
yielding 0, written as 0 =S
, by applying it to all the (free) type variables FV(). The type 0 is called asubstitution instance
or sometimes simply aninstance
of type.2. Similarly, for a type scheme
= 81n:
, a substitution 0 =S
is applied only to its free type variables FV() renaming all its bound type variables. More formally, we dene 0=81n:
0 where 1n are new type variables, 0= (S
0jfi 7!ig) andS
0=S
#tyvars(). As before,0 is called asubstitution instance
of .3. A substitution to a type environment TE is applied to the free variables of each type scheme
in its range.Using notation 2.2, a nite substitution is also written as
S
= f1 7! 1;:::;
n 7! ng. We may also abbreviate it asS
=fi 7! ig. Composition of Substitutions (S
2S
1) meansS
2(S
1) where the parentheses can be dropped by assuming that substitutions associate to the right.Note that substitutions are dened for free type variables only. Substitutions for the bound variables in a type scheme are done dierently and are called Instantiations8.
Denition 2.7
Given a type scheme =81n:
with BV() =f1;:::;
ng,1. A type
0 is said to be ageneric instance
(also called an Instantiation) of , written as 0, if there exists a nite substitutionS
=fi 7!ig with Dom(S
) BV() such that 0=S
.2. A type scheme
0=81m:
0is said to be ageneric instance
of, written as 0, when any of the following equivalent conditions hold,For all types
1, 01 implies 1.0 and no j is free in .
0 and FV()FV(0).
8We dier slightly from the terminology used in [20] where these are called Instantiations and Generic
3. Similarly, by extension, instantiation of a type environment TE yields a
generic instance
TE0, written as TE TE0, obtained by pointwise instantiation of the type-schemes in its range.
Note here that Instantiations act only on bound variables of type schemes since the domain of the substitution S used in showing
is limited to BV(). Thus substitution instances are created via substitutions and generic instances are created via instantiations.We may sometimes write
to mean where could also be just a type without any bound variables. In fact, it can be easily seen from the above denitions that for types and 0, 0implies =0.Finally, we admit quantication of the free type variables in a type and converting it to a type scheme as an inverse operation of instantiation.
Denition 2.8 Generalization
orClosing
a type under a type environment TE is dened as follows:This is the operation that introduces polymorphism in the system. Note that we are not allowed to quantify over free variables of a type that may already exist freely in the type environment. This is an important point that makes this system sound.
2.3.3 Operational Semantics of the Mini-Language
Now we are ready to present the operational semantics of the mini-language. Both dynamic and static semantics are presented as a set of inference rules [52] operating on the expressions of the mini-language. The static semantic rules constitute the basic Hindley/Milner type inference rules, from which valid assertions about the type of the expressions can be inferred.