• Aucun résultat trouvé

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 = f

t;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

:

8

2

:

8

n

:

is also written as 8

1

2

n

:

.

2.3.2 Some Notations and Denitions

First, we dene some general set notation to help us later.

Notation 2.1

If

A

and

B

are sets, then,

1. powerset(

A

) is dened to be the set of all subsets of

A

. 2.

A

+

B

is the disjoint union of

A

and

B

.

3.

A

n

B

is the set dierence

A

,

B

.

4.

A

B

is the cartesian product of

A

and

B

.

5.

A

!

B

is a set of functions from

A

to

B

. The precise interpretation of (!) depends on the characteristics of the sets

A

and

B

. For example, if

A

and

B

are simple syntactic sets, then

A

!

B

merely denotes the set of all total functions from

A

to

B

; if

A

and

B

are scott-domains, then

A

!

B

denotes the set of continuous functions from

A

to

B

, and so on.

6.

A

,,n!

B

is the set of nite maps7 from

A

to

B

.

Notation 2.2

If

f

and

g

are functions (maps) then, 1. Dom(

f

) is the Domain of

f

.

2. Ran(

f

) is the Range of

f

.

3.

f

2

A

!

B

means that

f

is a function from

A

to

B

as determined by the interpretation of the set

A

!

B

.

4.

f

2

A

,,n!

B

means that

f

can be written as f

a

1 7!

b

1

;:::;a

i 7!

b

i

;:::;a

n7!

b

ng where

a

i2

A

and

b

i2

B

.

5. fgrepresents the empty map.

6.

f

g

(read

f composed with g

) is the composite map dened by (

f

g

)(

a

) =

f

(

g

(

a

)).

7.

f

+

g

(read

f modied by g

) is the map with domain Dom(

f

) [Dom(

g

) dened by (

f

+

g

)(

a

) = if

a

2Dom(

g

) then

g

(

a

) else

f

(

a

).

8. If

f

and

g

have disjoint domains then

f

j

g

(read

f 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

or

g

and emphasizes the disjointness of their respective domains.

9.

f

nA is the map

f

with its domain restricted to Dom(

f

)n

A

. 10.

f

#A is the map

f

with its domain restricted to

A

.

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

=8

1

n

:

, 1.

1

;:::;

n are

bound

in

.

2. A type variable

is

free

in

if it is not bound and it occurs in

.

3. A type variable

is

free

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 a

ground type

or

monotype

. Similarly , if FV(

) =

and FV(TE) =

, then

and TE are said to be

closed

, 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

A

Substitution

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 a

substitution instance

or sometimes simply an

instance

of type

.

2. Similarly, for a type scheme

= 8

1

n

:

, a substitution

0 =

S

is applied only to its free type variables FV(

) renaming all its bound type variables. More formally, we dene

0=8

1

n

:

0 where

1

n are new type variables,

0= (

S

0jf

i 7!

ig)

and

S

0=

S

#tyvars(). As before,

0 is called a

substitution 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

= f

1 7!

1

;:::;

n 7!

ng. We may also abbreviate it as

S

=f

i 7!

ig. Composition of Substitutions (

S

2

S

1)

means

S

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

=8

1

n

:

with BV(

) =f

1

;:::;

ng,

1. A type

0 is said to be a

generic instance

(also called an Instantiation) of

, written as

0, if there exists a nite substitution

S

=f

i 7!

ig with Dom(

S

) BV(

) such that

0=

S

.

2. A type scheme

0=8

1

m

:

0is said to be a

generic instance

of

, written as

0, when any of the following equivalent conditions hold,

For all types

1,

0

1 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

or

Closing

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.