• Aucun résultat trouvé

Properties of the Assumptions

Environment Snapshot 3:

3.3 Proving Correctness of the Incremental System

3.3.2 Properties of the Assumptions

Before we go into the details of proving algorithm

W

1 correct, let us rst examine the charac-teristics and signicance of the upper bound type assumptions (M) we collect for each of the

This is because the bindings actually get rearranged in topological order in the complete program system,

generic top-level identiers via algorithm

W

0. By denition, these assumptions record all the generic type instances created from the top-level type-schemes within the call to

W

0. They serve as an exact type specication of the interface between the current top-level denition and other denitions.

Below, we dene these assumption sets in a general setting and then examine their properties in the context of algorithm

W

0, which will help us later in the correctness proofs.

Notation 3.3

Given a typing TE `

e

:

and its associated derivation tree, we write

A

TE`

e

:

, or simply

A

`

e

:

(when TE is clear by context), as another typing representing the same derivation tree that explicitly records all type instances of its free (external) identiers.

A

TE is the set of assumptions constructed inductively as below16.

Case 1: |

e

)

x

| Dene

A

TE=f(

x

7!

)g.

Case 2: |

e

)

x:e

1 | If inductively,

A

TE0 `

e

1 :

1 represents the typing for the body

e

1 where TE0= TE +f

x

7!

2g, then dene

A

TE =

A

TE0nfxg.

Case 3: |

e

)

e

1

e

2 | If inductively,

A

TE1 `

e

1 :

2 !

1 and

A

TE2 `

e

2 :

2, then dene

A

TE=

A

TE1 [

A

TE2 .

Case 4: |

e

) let

x

=

e

1 in

e

2 | If inductively,

A

TE1 `

e

1 :

1 and

A

TE2 0 `

e

2 :

2 where TE0= TE +f

x

7!close(TE

;

1)g, then dene

A

TE =

A

TE1 [

A

TE2 0nfxg.

Informally, the typing

A

TE`

e

:

simply collects the actual type instances assigned to each occurrence of an external identier inside the derivation tree of the typing TE `

e

:

into the assumption set

A

TE.

A

TEis a special multi-set mapping in the sense that several type instance may be recorded for the same identier

x

corresponding to its various occurrences within

e

, and also because it always refers to a particular derivation tree expansion of the typing TE `

e

:

, recording a specic set of type instances used within it.

Note that

A

TE is not a type environment in itself, but it serves as a cumulative record of the use of the free identiers (FId) of the expression

e

during the derivation of the typing TE `

e

:

. Note that the notation respects the scoping rules for free identiers of an expression;

the identiers bound locally via a let or a

-construct are free internally but bound in the whole expression, therefore, the assumptions collected for these identiers are discarded as soon as they become bound.

16Unless it is necessary to specify the assumer explicitly, we treat a set of assumptions as a mapping from the assumee to the assumed value. The identier on LHS of the current top-level binding is implicitly taken to be the assumer.

Now we dene operations on these assumption sets and show some of their properties.

Denition 3.4

Let

A

be a set of assumptions collected for the typing TE `

e

:

as dened above. Then, for a specic occurrence of a free identier

x

within

e

, dene

A

(

x

) =

x if and only if (

x

7!

x) 2

A

and this is the assumption corresponding to the specied occurrence of

x

as recorded in

A

`

e

:

.

It is usually clear by context which occurrence of

x

is being referred to in

A

(

x

)17because it makes sense to use an assumption set

A

only as prescribed by the derivation tree of

A

`

e

:

.

Denition 3.5

Given an assumption set

A

collected for a typing TE `

e

:

and another type environment TE0, we dene TE0

A

if and only if Dom(

A

)Dom(TE0) and8(

x

7!

x)2

A

with TE0(

x

) =

x, we have

x

x. Note that TE

A

by construction.

The above denition provides a meaningful way to compare an assumption set with type environments other than that for which it was originally collected. The following two lemmas follow directly from this denition.

Lemma 3.1

Given an assumption set

A

collected for the typing TE `

e

:

and another type environment TE0 satisfying TE0

A

according to denition 3.5, then TE0 `

e

:

is also a valid typing.

Proof:

By denition,

A

records the actual type instances of all the free identiers used within the derivation tree of the typing TE `

e

:

. So, any type environment capable of generating those external type instances is an acceptable candidate for preserving the derivation tree.

In other words, any type environment TE0 satisfying TE0

A

will be able to regenerate the same derivation tree and hence produce a valid typing. 2 Note that in the above lemma, the original type environment TE may be incomparable to the given environment TE0, but as long as they can instantiate the same assumption set, both are able to generate the same typing. This further corroborates the notion that the assumption sets can be used as a minimal description of a typing with respect to the types of the free identiers used in it.

Lemma 3.2

Given an assumption set

A

collected for a typing TE `

e

:

and another type environment TE0, if TE0

A

then for all substitutions

S

,

S

(TE0)

S

(

A

).

Proof:

It follows trivially from denition 3.5 above and by pointwise extension of lemma 2.2.

2

We may note that the range of an assumption set is a set of types, and in general, may contain several type variables all of which are free. In order to reason about substitutions as applied to assumption sets, we partition these type variables into the following categories.

Notation 3.6

The set of all type variables in Ran(

A

TE) denoted by tyvars(

A

TE) is partitioned into three categories.

1. First, there may be some type variables that occur free in TE as well. These are represented by,

freevars(

A

TE) = tyvars(

A

TE)\tyvars(TE)

2. There may be some type variables that are closed over by the generalization operation (denition 2.8) of the LET rule while typing some let-construct within the expression

e

. By denition of generalization, these have to be disjoint from the free type variables of TE and become bound in that branch of the derivation tree. These type variables are represented by genvars(

A

TE).

3. All other type variables that are neither free in TE nor appear bound in any branch of the derivation tree are simply fresh type variables used in the derivation tree that have not yet been closed over and are denoted by othervars(

A

TE).

By denition, we have,

tyvars(

A

TE) = freevars(

A

TE)[genvars(

A

TE)[othervars(

A

TE)

In order to make the partitioning clear, consider the following simple example in mini-language. The expression

e

in equation 3.8 has a typing 3.9 while we collect the assumptions as shown in equation 3.10. We have superscripted all identiers with their types for clarity.

e

::= let

f

(list);=nil(list)

;y

in nil(list); (3.8)

TE+f

y

7!

g `

e

: (list

) (3.9)

A

= fnil7!(list

)

;y

7!

;

nil7!(list

)g (3.10) Among the type variables of the assumption set

A

,

is a free variable since it occurs free in the supplied type environment as well,

is a generic variable since it is closed over in the

let-binding of

f

(assuming that

is not free in TE as well), and

belongs to neither of the above two categories. Also note that there are two assumptions for nil corresponding to its two occurrences within

e

as expected.

The following lemma forms the backbone of the assumption based reasoning in the subse-quent proofs for correctness of our incremental system.

Lemma 3.3

Let

A

TE `

e

:

be a typing corresponding to TE `

e

:

denedvia notation 3.3.

Then for any substitution

S

that does not involve genvars(

A

) in its domain or range,

S

(

A

TE)`

e

:

S

is also a valid typing corresponding to

S

(TE)`

e

:

S

.

Proof:

Using lemma 2.3 we obtain,

TE `

e

:

=)

S

(TE) `

e

:

S

(3.11)

We only have to show that the assumption set

A

S(TE) corresponding to typing 3.11 is the same as

S

(

A

TE). We will prove this inductively by traversing the derivation tree of typing 3.11 and verifying that the assumption set

A

S(TE) generated at each step (via notation 3.3) satises the condition

A

S(TE)=

S

(

A

TE).

Case1: |

e

)

x

| Typing 3.11 directly generates an assumption set

A

S(TE) =f

x

7!

S

g which is the same as

S

(

A

TE).

Case2: |

e

)

x:e

1 | If inductively,

A

S(TE0) =

S

(

A

TE0) for the antecedent

S

(TE0)`

e

1 :

S

1 where TE0= TE +f

x

7!

2g, then

A

S(TE) =

A

S(TE0)nfxg =

S

(

A

TE0)nfxg=

S

(

A

TE) using notation 3.3.

Case3: |

e

)

e

1

e

2 | If inductively,

A

S1(TE) =

S

(

A

TE1 ) and

A

S2(TE) =

S

(

A

TE2 ) for the antecedents

S

(TE)`

e

1 :

S

(

2!

1) and

S

(TE) `

e

2 :

S

2 respectively, then

A

S(TE)=

A

S1(TE)[

A

S2(TE)=

S

(

A

TE1 )[

S

(

A

TE2 ) =

S

(

A

TE) using notation 3.3.

Case4: |

e

) let

x

=

e

1 in

e

2 | We expand the immediate antecedents of typing 3.11 for this case18.

TE0`

e

1 :

S

0

1 TE0+f

x

7!close(TE0

;S

0

1)g`

e

2 :

S

2

TE0`let

x

=

e

1 in

e

2 :

S

2 (3.12)

18For a proof of how these antecedents were arrived at, see [57], pages 18{20.

where,

TE0 =

S

(TE) =

S

0(TE)

and

S

0 =

S

#tyvars(TE)

=)

S

(close(TE

;

1)) = close(TE0

;S

0

1)

We are given that the substitution

S

does not involve any generic type variables that are closed over in any branch of the derivation tree. In other words,

S

may involve only those type variables that are not allowed to be closed over as far as this typing is concerned. So we must have,

S

(close(TE

;

1)) = close(

S

(TE)

;S

1)

Thus, we can replace

S

0 with

S

in typing 3.12 and use the inductive assumption for the two antecedents, yielding the desired result

A

S(TE)=

S

(

A

TE) via notation 3.3.

2

The above lemma provides a way to use the assumption sets to obtain new correct typings from old typings for the same expression. We can compare this with the lemma 2.3 which establishes the substitution transformation using the type environments.

Now we come back to the algorithm

W

0 (refer gure 3.6) and show that the assumptions collected there actually correspond to the typing generated by the algorithm. The proof of this lemma is very similar to that of the soundness theorem 2.7.

Lemma 3.4

If assumption set

A

is collected in the call

W

0(TE

;e

) = (

S;;A

) then

A

`

e

:

is a valid typing that corresponds to the typing

S

(TE)`

e

:

generated through the soundness theorem 2.7.

Proof:

We prove this by structural induction on the expression

e

.

Case1: |

e

)

x

| From gure 3.6,

W

0(TE

;x

) = (

ID;

0

;

f(

x

7!

0)g). Clearly,f(

x

7!

0)g`

x

:

0 is a valid typing corresponding to ID(TE)`

x

:

0 directly from its denition in notation 3.3.

Case2: |

e

)

x:e

1 | From gure 3.6, the internal recursive call to

W

0 for the body

e

1

yields (

S

1

;

1

;A

1). So, inductively we obtain the following valid typing,

A

1 `

e

1 :

1

where

S

1(TE) +f

x

7!

S

1

g `

e

1 :

1

Using notation 3.3 for the above typing, we arrive at the following,

A

1nfxg `

x:e

1:

S

1

!

1 (3.13)

where

S

1(TE) `

x:e

1:

S

1

!

1

As we can see from gure 3.6,

A

1nfxg is exactly the assumption set returned from

W

0, so that typing 3.13 is what we want.

Case3: |

e

)

e

1

e

2 | Again, the internal recursive calls to

W

0 inductively yield the following,

A

1 `

e

1 :

1 (3.14)

where

S

1(TE) `

e

1 :

1

and

A

2 `

e

2 :

2 (3.15)

where

S

2

S

1(TE) `

e

2 :

2

The substitution

S

2 does not involve any type variable from genvars(

A

1) (see nota-tion 3.6) because its type variables can only come from either the free type variables of TE, or absolutely fresh type variables generated while typing

e

2. Similarly, the substitu-tion

S

3 =

U

(

S

2

1

;

2 !

) does not involve any type variables from either genvars(

A

1) or genvars(

A

2). Therefore, lemma 3.3 can be applied to the typings 3.14 and 3.15 giving,

S

3

S

2(

A

1) `

e

1:

S

3

S

2

1

=)

S

3

S

2(

A

1) `

e

1:

S

3

2!

S

3

(3.16)

and

S

3(

A

2) `

e

2:

S

3

2 (3.17)

Using notation 3.3 and the typings 3.16 and 3.17 we obtain the following valid typing,

S

3

S

2(

A

1)[

S

3(

A

2) `

e

1

e

2 :

S

3

where

S

3

S

2

S

1(TE) `

e

1

e

2 :

S

3

which exactly corrresponds to the assumption set returned from the outer call to

W

0. Case4: |

e

)let

x

=

e

1 in

e

2 | Just like in the last case, the internal recursive calls to

W

0inductively yield the following,

A

1 `

e

1 :

1 (3.18)

where

S

1(TE) `

e

1 :

1

and

A

2 `

e

2 :

2 (3.19)

where

S

2

S

1(TE) +f

x

7!

g `

e

2 :

2

and

= close(

S

2

S

1(TE)

;S

2

1) As in lemma 3.3.

We always allocate fresh type variables in generating instances of previously closed type-schemes, including the type-scheme generated from the currentlet-binding. There-fore, the type variables of

S

2 are either from these fresh type variables or from the free type variables of TE, neither of which can appear in genvars(

A

1). So just like the pre-vious case, we can apply

S

2 to typing 3.18 using lemma 3.3 and then combine it with typing 3.19 using notation 3.3 to obtain the following valid typing,

S

2(

A

1)[

A

2nfxg ` let

x

=

e

1 in

e

2 :

2 where

S

2

S

1(TE) ` let

x

=

e

1 in

e

2 :

2

which exactly corresponds to the assumption set returned from the outer call to

W

0.

2

The above lemma validates the assumptions collected by algorithm

W

0 in the sense that the collected assumptions denote a valid typing. The fact that these assumptions describe a minimum specication of the interface between the binding currently being processed and the external environment (lemma 3.1), and that we can manipulate them as pseudo-type environ-ments (lemma 3.3), permits us to use them as the basis of our incremental book-keeping.