2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus...

125
2-4-2 / Type systems Polymorphism Franc ¸ois Pottier September 29 and October 6, 2009 1 / 125

Transcript of 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus...

Page 1: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

2-4-2 / Type systemsPolymorphism

Francois Pottier

September 29 and October 6, 2009

1 / 125

Page 2: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Contents

Why polymorphism?

Polymorphic λ-calculus

Damas and Milner’s type system

Type soundness

Polymorphism and references

Bibliography

2 / 125

Page 3: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

What is polymorphism?

Polymorphism is the ability for a term to simultaneously admit severaldistinct types.

3 / 125

Page 4: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Why polymorphism?

Polymorphism is indispensable [Reynolds, 1974]: if a function thatsorts a list is independent of the type of the list elements, then itshould be directly applicable to lists of integers, lists of Booleans, etc.

In short, it should have polymorphic type:

∀X.(X → X → bool)→ list X → list X

which instantiates to the monomorphic types:

(int→ int→ bool)→ list int→ list int(bool→ bool→ bool)→ list bool→ list bool

. . .

4 / 125

Page 5: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Why polymorphism?

In the absence of polymorphism, the only ways of achieving this effectwould be:

• to manually duplicate the list sorting function at every type(no-no!);

• to use subtyping and claim that the function sorts lists ofvalues of any type:

(> → > → bool)→ list > → list >

(The type > is the type of all values, and the supertype of alltypes.) This leads to loss of information and subsequentlyrequires introducing an unsafe downcast operation. This was theapproach followed in Java before generics were introduced in 1.5.

5 / 125

Page 6: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Polymorphism seems almost free

Polymorphism is already implicitly present in simply-typed λ-calculus.Indeed, we have checked (in fact, inferred) that the type:

(X1 → X2)→ X1 → X1 → X2 × X2

is a principal type for the term λfxy.(f x, f y).

By saying that this term admits the polymorphic type:

∀X1X2.(X1 → X2)→ X1 → X1 → X2 × X2

we make polymorphism internal to the type system.

6 / 125

Page 7: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Towards type abstraction

Polymorphism is a step on the road towards type abstraction.

Intuitively, if a function that sorts a list has polymorphic type:

∀X.(X → X → bool)→ list X → list X

then it knows nothing about X – it is parametric in X – so it mustmanipulate the list elements abstractly: it can copy them around,pass them as arguments to the comparison function, but it cannotdirectly inspect their structure.

In short, within the code of the list sorting function, the variable X isan abstract type.

7 / 125

Page 8: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Parametricity

In the presence of polymorphism (and in the absence of effects), atype can reveal a lot of information about the terms that inhabit it.For instance, the polymorphic type

∀X.X → X

has only one inhabitant, namely the identity. Similarly, the type of thelist sorting function reveals a “free theorem” about its behavior!

This phenomenon was studied by Reynolds [1983] and byWadler [1989, 2007], among others. An account based on anoperational semantics is offered by Pitts [2000].

8 / 125

Page 9: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Ad hoc versus parametric

Let me begin a short digression.

The term “polymorphism” dates back to a 1967 paper byStrachey [2000], where ad hoc polymorphism and parametricpolymorphism were distinguished.

I see two different (and sometimes incompatible) ways of defining thisdistinction...

9 / 125

Page 10: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Ad hoc versus parametric: first definition

Here is one definition of the distinction:

With parametric polymorphism, a term can admit several types, all ofwhich are instances of a single polymorphic type:

int→ int, bool→ bool, . . .∀X.X → X

With ad hoc polymorphism, a term can admit a collection of unrelatedtypes:

int→ int→ int, float→ float→ float, . . .but not ∀X.X → X → X

10 / 125

Page 11: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Ad hoc versus parametric: second definition

Here is another definition:

With parametric polymorphism, untyped programs have a well-definedsemantics. (Think of the identity function.) Types are used only torule out unsafe programs.

With ad hoc polymorphism, untyped programs do not have asemantics: the meaning of a term can depend upon its type (e.g.2 + 2), or, even worse, upon its type derivation (e.g. show . read).

11 / 125

Page 12: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Ad hoc versus parametric: type classes

By the first definition, Haskell’s type classes [Hudak et al., 2007] area form of (bounded) parametric polymorphism: terms have principal(qualified) type schemes, such as:

∀X.Num X ⇒ X → X → X

Yet, by the second definition, type classes are a form of ad hocpolymorphism: untyped programs do not have a semantics.

End of digression – in this course, we are interested only in thesimplest form of parametric polymorphism.

12 / 125

Page 13: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Contents

Why polymorphism?

Polymorphic λ-calculus

Damas and Milner’s type system

Type soundness

Polymorphism and references

Bibliography

13 / 125

Page 14: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Polymorphic λ-calculus

The polymorphic λ-calculus (also known as: the second-order λ-calculus;F2; System F) was independently defined by Girard (1972) andReynolds [1974].

Compared to the simply-typed λ-calculus, types are extended:

T ::= . . . | ∀X.T

How are the syntax and semantics of terms extended? There areseveral variants, depending on whether one adopts a type-passing or atype-erasing interpretation of polymorphism...

14 / 125

Page 15: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type-passing versus type-erasing interpretations

In the type-passing view, types exist at runtime: a value of type ∀X.Tis a function that expects a type as an argument. The semanticsinvolves typed terms and computation over types.

In the type-erasing view, types are erased prior to runtime: a value oftype ∀X.T is a value that happens to simultaneously have type T forevery X. The semantics involves untyped terms.

(Even under the type-erasing view, one sometimes works withtype-annotated terms, for the purposes of decidable type-checking.This will be the case, for instance, when we study type-preservingclosure conversion.)

15 / 125

Page 16: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type-passing polymorphic λ-calculus

In the type-passing variant [Reynolds, 1974], there are term-levelconstructs for introducing and eliminating the universal quantifier:

TAbsΓ; X ` t : T

Γ ` ΛX.t : ∀X.T

TApp

Γ ` t : ∀X.TΓ ` t T ′ : [X 7� T ′]T

Type variables are explicitly bound and appear in type environments.

The operational semantics is extended accordingly:

t ::= . . . | ΛX.t | t Tv ::= . . . | ΛX.tE ::= . . . | [] T

(ΛX.t) T �� [X 7� T]t

16 / 125

Page 17: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type-passing versus type-erasing: pros and cons

The type-passing interpretation has a number of disadvantages.

• because it alters the semantics, it does not fit our view that theuntyped semantics should pre-exist and that a type system isonly a predicate that selects a subset of the well-behaved terms.

• because it requires both values and types to exist at runtime, itcan lead to a duplication of machinery. Compare type-preservingclosure conversion in type-passing [Minamide et al., 1996] and intype-erasing [Morrisett et al., 1999] styles.

An apparent advantage of the type-passing interpretation is to allowtypecase; however, typecase can be simulated in a type-erasing systemby viewing runtime type descriptions as values [Crary et al., 2002].

In the following, we focus on the type-erasing variant, which does notalter the syntax or semantics of untyped terms.

17 / 125

Page 18: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type-erasing polymorphic λ-calculus

The syntax and semantics of terms are unchanged.

The typing rules that introduce and eliminate the universal quantifierare non-syntax-directed:

∀-IntroΓ ` t : T X # Γ

Γ ` t : ∀X.T

∀-ElimΓ ` t : ∀X.T

Γ ` t : [X 7� T ′]T

Because this type-erasing variant of System F allows evaluation undera universal introduction rule, it exhibits an interaction with references,while the type-passing variant does not. (Details later on...)

Here, type variables are not explicitly introduced (but this is just amatter of style).

Why the side condition X # Γ?...

18 / 125

Page 19: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

On the side condition X # Γ

Omitting the side condition leads to unsoundness:

∀-Intro2Abs

∀-Elim

Broken ∀-Intro

x : X1 ` x : X1

x : X1 ` x : ∀X1.X1x : X1 ` x : X2

∅ ` λx.x : X1 → X2

∅ ` λx.x : ∀X1.∀X2.X1 → X2

This is a type derivation for a type cast (Objective Caml’s Obj.magic).

19 / 125

Page 20: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

On the side condition X # Γ

Omitting the side condition leads to unsoundness:

∀-Intro2Abs

∀-Elim

Broken ∀-Introx : X1 ` x : X1

x : X1 ` x : ∀X1.X1

x : X1 ` x : X2∅ ` λx.x : X1 → X2

∅ ` λx.x : ∀X1.∀X2.X1 → X2

This is a type derivation for a type cast (Objective Caml’s Obj.magic).

20 / 125

Page 21: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

On the side condition X # Γ

Omitting the side condition leads to unsoundness:

∀-Intro2Abs

∀-Elim

Broken ∀-Introx : X1 ` x : X1

x : X1 ` x : ∀X1.X1x : X1 ` x : X2

∅ ` λx.x : X1 → X2

∅ ` λx.x : ∀X1.∀X2.X1 → X2

This is a type derivation for a type cast (Objective Caml’s Obj.magic).

21 / 125

Page 22: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

On the side condition X # Γ

Omitting the side condition leads to unsoundness:

∀-Intro2

Abs

∀-Elim

Broken ∀-Introx : X1 ` x : X1

x : X1 ` x : ∀X1.X1x : X1 ` x : X2

∅ ` λx.x : X1 → X2

∅ ` λx.x : ∀X1.∀X2.X1 → X2

This is a type derivation for a type cast (Objective Caml’s Obj.magic).

22 / 125

Page 23: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

On the side condition X # Γ

Omitting the side condition leads to unsoundness:

∀-Intro2Abs

∀-Elim

Broken ∀-Introx : X1 ` x : X1

x : X1 ` x : ∀X1.X1x : X1 ` x : X2

∅ ` λx.x : X1 → X2

∅ ` λx.x : ∀X1.∀X2.X1 → X2

This is a type derivation for a type cast (Objective Caml’s Obj.magic).

23 / 125

Page 24: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

On the side condition X # Γ

A good intuition is: a judgement Γ ` t : T corresponds to the logicalassertion ∀X.(Γ⇒ T ), where X are the free type variables of thejudgement.

In that view, ∀-Intro corresponds to the axiom:

∀X.(P ⇒ Q) ≡ P ⇒ (∀X.Q) if X # P

24 / 125

Page 25: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

On the side condition X # Γ

Quiz: why is there no such side condition in a presentation ofSystem F where type variables are explicitly bound in the typeenvironment? Or is there one, and where? back

Answer: no such condition is needed in rule TAbs, because (1) in thepremise of TAbs, the environment is extended with an explicit bindingof X, and (2) the definition of environment lookup, not shown earlier,contains a side condition:

(Γ; X)(x) = Γ(x) if X # Γ(x)

The details vary, but the side condition exists in all presentations.

25 / 125

Page 26: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

On the side condition X # Γ

Quiz: why is there no such side condition in a presentation ofSystem F where type variables are explicitly bound in the typeenvironment? Or is there one, and where? back

Answer: no such condition is needed in rule TAbs, because (1) in thepremise of TAbs, the environment is extended with an explicit bindingof X, and (2) the definition of environment lookup, not shown earlier,contains a side condition:

(Γ; X)(x) = Γ(x) if X # Γ(x)

The details vary, but the side condition exists in all presentations.

26 / 125

Page 27: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

An example

Here is a version of the term λfxy.(f x, f y) that carries explicit typeabstractions and annotations:

ΛX1.ΛX2.λf : X1 → X2.λx : X1.λy : X1.(f x, f y)

This term admits the polymorphic type:

∀X1.∀X2.(X1 → X2)→ X1 → X1 → X2 × X2

Quite unsurprising, right?

27 / 125

Page 28: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

An example

Perhaps more surprising is the fact that this untyped term can bedecorated in a different way:

ΛX1.ΛX2.λf : ∀X.X → X.λx : X1.λy : X2.(f X1 x, f X2 y)

This term admits the polymorphic type:

∀X1.∀X2.(∀X.X → X)→ X1 → X2 → X1 × X2

This begs the question: ...

28 / 125

Page 29: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Incomparable types in System F

Which of the two is more general?

∀X1.∀X2.(X1 → X2)→ X1 → X1 → X2 × X2∀X1.∀X2.(∀X.X → X)→ X1 → X2 → X1 × X2

One requires x and y to admit a common type, while the otherrequires f to be polymorphic.

Neither of these types can be an instance of the other, for anyreasonable definition of the word “instance”, because each has aninhabitant that does not admit the other as a type.

(Exercise: find these inhabitants!)

29 / 125

Page 30: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Incomparable types in System F

Which of the two is more general?

∀X1.∀X2.(X1 → X2)→ X1 → X1 → X2 × X2∀X1.∀X2.(∀X.X → X)→ X1 → X2 → X1 × X2

One requires x and y to admit a common type, while the otherrequires f to be polymorphic.

Neither of these types can be an instance of the other, for anyreasonable definition of the word “instance”, because each has aninhabitant that does not admit the other as a type.

(Exercise: find these inhabitants!)

30 / 125

Page 31: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Notions of instance

It seems plausible that the untyped term λfxy.(f x, f y) does notadmit a type of which both of these types are instances.

But, in order to prove this, one must fix what it means for T2 to bean instance of T1 – or, equivalently, for T1 to be more general than T2.

Several definitions are possible...

31 / 125

Page 32: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Syntactic notions of instance

In System F , “to be an instance” is usually defined by the rule:

InstGenY # ∀X.T

∀X.T ≤ ∀Y .[~X 7� ~T]T

One can show that, if T1 ≤ T2, then any term that has type T1 alsohas type T2; that is, the following rule is derivable:

SubΓ ` t : T1 T1 ≤ T2

Γ ` t : T2

(Not-so-easy exercise: prove it!)

32 / 125

Page 33: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Another syntactic notion of instance: System Fη

Mitchell [1988] defines System Fη, a version of System F extendedwith a richer instance relation:

InstGenY # ∀X.T

∀X.T ≤ ∀Y .[~X 7� ~T]T

Distributivity

∀X.(T1 → T2) ≤ (∀X.T1)→ (∀X.T2)

Congruence-→T2 ≤ T1 T ′1 ≤ T ′2T1 → T ′1 ≤ T2 → T ′2

Congruence-∀T1 ≤ T2

∀X.T1 ≤ ∀X.T2

Transitivity

T1 ≤ T2 T2 ≤ T3T1 ≤ T3

In System Fη, Sub is an explicit rule.

System Fη can also be defined as the closure of System F underη-equality.

Why is a rich notion of instance potentially interesting?

33 / 125

Page 34: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

A definition of principal typings

Ideally, a type system should satisfy the principal typingsproperty [Wells, 2002]:

Every well-typed term t admits a principal typing – one whoseinstances are exactly the typings of t.

Whether this property holds depends on a definition of instance. Themore liberal the instance relation, the more hope there is of havingprincipal typings.

34 / 125

Page 35: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

A “semantic” notion of instance

Wells [2002] notes that, once a type system is fixed, a most liberalnotion of instance can be defined, a posteriori, by:

A typing θ1 is more general than a typing θ2 if and only ifevery term that admits θ1 admits θ2 as well.

This is the largest reasonable notion of instance: ≤ is defined as thelargest relation such that a subtyping principle is admissible.

This definition can be used to prove that a system does not haveprincipal typings, under any reasonable definition of “instance”.

35 / 125

Page 36: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Which systems have principal typings?

We have seen that simply-typed λ-calculus has principal typings, withrespect to a substitution-based notion of instance.

Wells [2002] shows that neither System F nor System Fη haveprincipal typings.

It was shown earlier that System Fη’s instance relation isundecidable [Wells, 1995, Tiuryn and Urzyczyn, 2002] and that typeinference for both System F and System Fη isundecidable [Wells, 1999].

36 / 125

Page 37: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Which systems have principal typings?

There are still a few positive results...

Some systems of intersection types have principaltypings [Wells, 2002] – but they are very complex and have yet tosee a practical application.

Damas and Milner’s type system (coming up next) has principal typesand decidable type inference.

37 / 125

Page 38: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Other approaches to type inference in System F

In System F , one can still perform bottom-up type checking, providedtype abstractions and type applications are explicit.

One can perform incomplete forms of type inference, such as localtype inference [Pierce and Turner, 2000, Odersky et al., 2001].

Finally, one can design restrictions or variants of the system thathave decidable type inference. Damas and Milner’s type system is oneexample; MLF [Le Botlan and Remy, 2003] is a more expressive, andmore complex, approach.

38 / 125

Page 39: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Contents

Why polymorphism?

Polymorphic λ-calculus

Damas and Milner’s type system

Type soundness

Polymorphism and references

Bibliography

39 / 125

Page 40: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Damas and Milner’s type system

Damas and Milner’s type system [Milner, 1978] offers a restrictedform of polymorphism, while avoiding the difficulties associated withtype inference in System F .

This type system is at the heart of Standard ML, Objective Caml, andHaskell.

40 / 125

Page 41: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Some intuitions

The type inference algorithm should be a simple extension of thealgorithm that was developed for simply-typed λ-calculus.

To this end, it should exploit polymorphism where obviously available,but should not try to guess where polymorphism is necessary.

In other words, it should continue to rely on first-order unification:that is, type variables should continue to stand for types withoutquantifiers.

41 / 125

Page 42: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Some intuitions

For instance, this term should be well-typed:

let f = λz.z in (f 0, f true)

Indeed, f is known to be bound to λz.z, a term whose principal type(∀X.X → X) can be inferred as in simply-typed λ-calculus.

On the other hand, this term should be ill-typed:

λf.(f 0, f true)

Indeed, the type of f is unknown, so it must be a monomorphic type.Under this constraint, this term cannot be well-typed.

In short, let-bound variables receive possibly polymorphic types, whileλ-bound variables must receive monomorphic types.

42 / 125

Page 43: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Some intuitions

There is a simple intuition behind Damas and Milner’s type system: aclosed term has type T if and only if its let-normal form has type Tin simply-typed λ-calculus.

A term’s let-normal form is obtained by iterating the rewrite rule:

let x = t1 in t2 �� t1; [x 7� t1]t2

This intuition suggests type-checking and type inference algorithms.But these algorithms are not practical, because:

• they have exponential complexity;

• separate compilation prevents reduction to let-normal form.

43 / 125

Page 44: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Some intuitions

In the following, we study a direct presentation of Damas and Milner’stype system, which does not involve let-normal forms.

It is practical, because:

• it leads to an efficient type inference algorithm;

• it supports separate compilation.

44 / 125

Page 45: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Terms

Terms are now given by:

t ::= x | λx.t | t t | let x = t in t | . . .

The let construct is no longer sugar for a β-redex: it is now aprimitive form.

45 / 125

Page 46: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Types and type schemes

The syntax of types is unchanged with respect to simply-typedλ-calculus:

T ::= X | T → T | . . .

A separate category of type schemes is introduced:

S ::= ∀X.T

These correspond to the principal type schemes of simply-typedλ-calculus. All quantifiers must appear in prenex position, so typeschemes are less expressive than System F types.

46 / 125

Page 47: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Typing judgements

A type environment Γ is now a finite sequence of bindings of variablesto type schemes.

Judgements now take the form:

Γ ` t : S

Types form a subset of type schemes, so type environments andjudgements can contain types too.

47 / 125

Page 48: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Typing rules

Here is a standard, non-syntax-directed presentation.

VarΓ(x) = S

Γ ` x : S

AbsΓ; x : T ` t : T ′

Γ ` λx.t : T → T ′

App

Γ ` t1 : T → T ′ Γ ` t2 : T

Γ ` t1 t2 : T ′

LetΓ ` t1 : S Γ; x : S ` t2 : T

Γ ` let x = t1 in t2 : T

GenΓ ` t : TX # Γ

Γ ` t : ∀X.T

InstΓ ` t : ∀X.T

Γ ` t : [~X 7� ~T]T

Let moves a type scheme into the environment, which Var can exploit.

Abs and App are unchanged. λ-bound variables receive a monotype.

Gen and Inst are as in type-erasing System F , except they introduce or

eliminate multiple universal quantifiers at once. Type variables are instantiated

with monotypes.

48 / 125

Page 49: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Example

Here is a simple type derivation that exploits polymorphism:

Let

Gen

Abs

Varz : X ` z : X

∅ ` λz.z : X → X

∅ ` λz.z : ∀X.X → X

Γ ` f : ∀X.X → XVar

Γ ` f : int→ intInst

Γ ` f 0 : intApp

Γ ` f : ∀X.X → XVar

Γ ` f : bool→ boolInst

Γ ` f true : boolApp

Γ ` (f 0, f true) : int × boolPair

∅ ` let f = λz.z in (f 0, f true) : int × bool

(Γ stands for f : ∀X.X → X.)

Gen is used above Let (at left), and Inst is used below Var. In fact,every type derivation can be put in this form. forward

49 / 125

Page 50: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

A non-example

As announced, this term is ill-typed:

λf.(f 0, f true)

Indeed, this term contains no “let” construct, so it is type-checkedexactly as in simply-typed λ-calculus, where it is ill-typed, because theequation:

int→ T1 = bool→ T2

has no solution.

Recall that this term is well-typed in type-erasing System F .

50 / 125

Page 51: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

A non-example

As announced, this term is ill-typed:

λf.(f 0, f true)

Indeed, this term contains no “let” construct, so it is type-checkedexactly as in simply-typed λ-calculus, where it is ill-typed, because theequation:

int→ T1 = bool→ T2

has no solution.

Recall that this term is well-typed in type-erasing System F .

51 / 125

Page 52: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Contents

Why polymorphism?

Polymorphic λ-calculus

Damas and Milner’s type system

Type soundness

Polymorphism and references

Bibliography

52 / 125

Page 53: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Outline

Type soundness for Damas and Milner’s type system is proved usingthe standard syntactic method [Wright and Felleisen, 1994].

Before reviewing the Subject Reduction proof, we need two steppingstones:

• a Type Substitution lemma;

• a syntax-directed presentation of the type system.

53 / 125

Page 54: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Renamings

Definition

A renaming ρ is a total, bijective mapping of type variables to typevariables whose domain is finite. The domain of ρ is the set of thetype variables X such that ρ(X) 6= X. The support of ρ is its domain.

Renamings apply to types, type schemes, and type environments:

ρ(T1 → T2) = ρ(T1)→ ρ(T2)ρ(∀X.T ) = ∀ρ(X).ρ(T )

ρ(∅) = ∅ρ(Γ; x : S) = ρ(Γ); x : ρ(S)

54 / 125

Page 55: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Renamings

The skeleton of a type derivation is its underlying rule name tree. Twoderivations are isomorphic when they have the same skeleton.

Lemma (Renaming)

For every derivation of Γ ` t : S, there exists an isomorphic derivation ofρ(Γ) ` t : ρ(S).

Proof.

No typing rule is sensitive to the choice of type variable names.

55 / 125

Page 56: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Substitutions

Definition

A substitution ϕ is a total mapping of type variables to types whosedomain is finite. The domain of ϕ is the set of the type variables Xsuch that ϕ(X) 6= X. The codomain of ϕ is the set of the typevariables that appear free in the image of its domain. The support ofϕ is the union of its domain and codomain. X # ϕ holds if and only ifX is not in the support of ϕ.

Substitutions apply to types, type schemes, and type environments:

ϕ(T1 → T2) = ϕ(T1)→ ϕ(T2)ϕ(∀X.T ) = ∀X.ϕ(T ) if X # ϕ

ϕ(∅) = ∅ϕ(Γ; x : S) = ϕ(Γ); x : ϕ(S)

56 / 125

Page 57: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution

Lemma (Type substitution)

For every derivation of Γ ` t : S, there exists an isomorphic derivation ofϕ(Γ) ` t : ϕ(S).

Proof.

By structural induction over derivations. Only two cases are ofinterest, namely Gen and Inst. See next slides...

57 / 125

Page 58: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Gen

The hypothesis is:

Γ ` t : T X # Γ

Γ ` t : ∀X.T

The goal is:ϕ(Γ) ` t : ϕ(∀X.T )

How to proceed? (Hint: what do we know about ϕ(∀X.T )?)

58 / 125

Page 59: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Gen

We distinguish two cases:

• first, the ideal case where X # ϕ holds; there, the goal becomes:

ϕ(Γ) ` t : ∀X.ϕ(T )

• then, the general case.

59 / 125

Page 60: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Gen / ideal case

Invoking the induction hypothesis yields ϕ(Γ) ` t : ϕ(T ).

The freshness hypothesis X # ϕ and the premise X # Γ, imply X # ϕ(Γ)(lemma – exercise!).

We now build a new instance of Gen:

ϕ(Γ) ` t : ϕ(T ) X # ϕ(Γ)

ϕ(Γ) ` t : ∀X.ϕ(T )

This is the goal.

60 / 125

Page 61: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Gen / ideal case

Invoking the induction hypothesis yields ϕ(Γ) ` t : ϕ(T ).

The freshness hypothesis X # ϕ and the premise X # Γ, imply X # ϕ(Γ)(lemma – exercise!).

We now build a new instance of Gen:

ϕ(Γ) ` t : ϕ(T ) X # ϕ(Γ)

ϕ(Γ) ` t : ∀X.ϕ(T )

This is the goal.

61 / 125

Page 62: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Gen / ideal case

Invoking the induction hypothesis yields ϕ(Γ) ` t : ϕ(T ).

The freshness hypothesis X # ϕ and the premise X # Γ, imply X # ϕ(Γ)(lemma – exercise!).

We now build a new instance of Gen:

ϕ(Γ) ` t : ϕ(T ) X # ϕ(Γ)

ϕ(Γ) ` t : ∀X.ϕ(T )

This is the goal.

62 / 125

Page 63: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Gen / general case

What if X # ϕ does not hold?

Recall that the hypothesis is:

Γ ` t : T X # Γ

Γ ` t : ∀X.T

This is where the premise X # Γ plays a role.

Because the X do not appear free in the conclusion, they can berenamed in the premises (via the renaming lemma) without affectingthe conclusion. In a sense, they are internal to this sub-derivation.

We are then back to the ideal case, with a different choice of X.

63 / 125

Page 64: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Gen / general case

What if X # ϕ does not hold?

Recall that the hypothesis is:

Γ ` t : T X # Γ

Γ ` t : ∀X.T

This is where the premise X # Γ plays a role.

Because the X do not appear free in the conclusion, they can berenamed in the premises (via the renaming lemma) without affectingthe conclusion. In a sense, they are internal to this sub-derivation.

We are then back to the ideal case, with a different choice of X.

64 / 125

Page 65: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Gen / general case

What if X # ϕ does not hold?

Recall that the hypothesis is:

Γ ` t : T X # Γ

Γ ` t : ∀X.T

This is where the premise X # Γ plays a role.

Because the X do not appear free in the conclusion, they can berenamed in the premises (via the renaming lemma) without affectingthe conclusion. In a sense, they are internal to this sub-derivation.

We are then back to the ideal case, with a different choice of X.

65 / 125

Page 66: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst

The hypothesis is:

Γ ` t : ∀X.TΓ ` t : [~X 7� ~T]T

The goal is:ϕ(Γ) ` t : ϕ([~X 7� ~T]T )

How to proceed?

66 / 125

Page 67: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case

We again begin with the ideal case where X # ϕ holds.

By the induction hypothesis, we have:

ϕ(Γ) ` t : ϕ(∀X.T )

which, by the freshness hypothesis, can be written:

ϕ(Γ) ` t : ∀X.ϕ(T )

We now build a new instance of Inst:

ϕ(Γ) ` t : ∀X.ϕ(T )ϕ(Γ) ` t : [X 7� ϕ(~T )]ϕ(T )

Is this the goal ϕ(Γ) ` t : ϕ([~X 7� ~T]T )?

67 / 125

Page 68: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case

We again begin with the ideal case where X # ϕ holds.

By the induction hypothesis, we have:

ϕ(Γ) ` t : ϕ(∀X.T )

which, by the freshness hypothesis, can be written:

ϕ(Γ) ` t : ∀X.ϕ(T )

We now build a new instance of Inst:

ϕ(Γ) ` t : ∀X.ϕ(T )ϕ(Γ) ` t : [X 7� ϕ(~T )]ϕ(T )

Is this the goal ϕ(Γ) ` t : ϕ([~X 7� ~T]T )?

68 / 125

Page 69: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case

We again begin with the ideal case where X # ϕ holds.

By the induction hypothesis, we have:

ϕ(Γ) ` t : ϕ(∀X.T )

which, by the freshness hypothesis, can be written:

ϕ(Γ) ` t : ∀X.ϕ(T )

We now build a new instance of Inst:

ϕ(Γ) ` t : ∀X.ϕ(T )ϕ(Γ) ` t : [X 7� ϕ(~T )]ϕ(T )

Is this the goal ϕ(Γ) ` t : ϕ([~X 7� ~T]T )?

69 / 125

Page 70: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case

We again begin with the ideal case where X # ϕ holds.

By the induction hypothesis, we have:

ϕ(Γ) ` t : ϕ(∀X.T )

which, by the freshness hypothesis, can be written:

ϕ(Γ) ` t : ∀X.ϕ(T )

We now build a new instance of Inst:

ϕ(Γ) ` t : ∀X.ϕ(T )ϕ(Γ) ` t : [X 7� ϕ(~T )]ϕ(T )

Is this the goal ϕ(Γ) ` t : ϕ([~X 7� ~T]T )?

70 / 125

Page 71: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case

There remains to check that, under the hypothesis X # ϕ, the

substitutions ϕ1 = ϕ ◦ [~X 7� ~T] and ϕ2 = [~X 7� ϕ(~T )] ◦ ϕ coincide.

This is done by applying both substitutions to an arbitrary variable X.

We distinguish two sub-cases: X ∈ X and X 6∈ X.

71 / 125

Page 72: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case / sub-case X ∈ X

Recall ϕ1 = ϕ ◦ [~X 7� ~T] and ϕ2 = [~X 7� ϕ(~T )] ◦ ϕ.

For some index i, X is Xi, the i-th element of the vector ~X. Then,ϕ1(X) is ϕ(Ti), where Ti is the i-th element of the vector ~T .

X ∈ X and X # ϕ imply X # ϕ, so X is not in the domain of ϕ, soϕ(X) is X. There follows that ϕ2(X) is also ϕ(Ti).

72 / 125

Page 73: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case / sub-case X ∈ X

Recall ϕ1 = ϕ ◦ [~X 7� ~T] and ϕ2 = [~X 7� ϕ(~T )] ◦ ϕ.

For some index i, X is Xi, the i-th element of the vector ~X. Then,ϕ1(X) is ϕ(Ti), where Ti is the i-th element of the vector ~T .

X ∈ X and X # ϕ imply X # ϕ, so X is not in the domain of ϕ, soϕ(X) is X. There follows that ϕ2(X) is also ϕ(Ti).

73 / 125

Page 74: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case / sub-case X ∈ X

Recall ϕ1 = ϕ ◦ [~X 7� ~T] and ϕ2 = [~X 7� ϕ(~T )] ◦ ϕ.

For some index i, X is Xi, the i-th element of the vector ~X. Then,ϕ1(X) is ϕ(Ti), where Ti is the i-th element of the vector ~T .

X ∈ X and X # ϕ imply X # ϕ, so X is not in the domain of ϕ, soϕ(X) is X. There follows that ϕ2(X) is also ϕ(Ti).

74 / 125

Page 75: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case / sub-case X 6∈ X

Recall ϕ1 = ϕ ◦ [~X 7� ~T] and ϕ2 = [~X 7� ϕ(~T )] ◦ ϕ.

Then, ϕ1(X) is ϕ(X).

X # ϕ and X # X imply X # ϕ(X), which implies that ϕ2(X) is ϕ(X).

75 / 125

Page 76: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case / sub-case X 6∈ X

Recall ϕ1 = ϕ ◦ [~X 7� ~T] and ϕ2 = [~X 7� ϕ(~T )] ◦ ϕ.

Then, ϕ1(X) is ϕ(X).

X # ϕ and X # X imply X # ϕ(X), which implies that ϕ2(X) is ϕ(X).

76 / 125

Page 77: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Inst / ideal case / sub-case X 6∈ X

Recall ϕ1 = ϕ ◦ [~X 7� ~T] and ϕ2 = [~X 7� ϕ(~T )] ◦ ϕ.

Then, ϕ1(X) is ϕ(X).

X # ϕ and X # X imply X # ϕ(X), which implies that ϕ2(X) is ϕ(X).

77 / 125

Page 78: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Gen / general case

What if X # ϕ does not hold?

Recall that the hypothesis is:

Γ ` t : ∀X.TΓ ` t : [~X 7� ~T]T

Because X is mute in the premise (where it is bound) and in theconclusion (where it is substituted out), it can be renamed withoutaffecting either of them.

We are then back to the ideal case, with a different choice of X.

78 / 125

Page 79: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Type substitution: Gen / general case

What if X # ϕ does not hold?

Recall that the hypothesis is:

Γ ` t : ∀X.TΓ ` t : [~X 7� ~T]T

Because X is mute in the premise (where it is bound) and in theconclusion (where it is substituted out), it can be renamed withoutaffecting either of them.

We are then back to the ideal case, with a different choice of X.

79 / 125

Page 80: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Reasoning up to alpha-conversion

What if you don’t believe me!?

Isn’t there too much handwaving in these alpha-conversion arguments?

True. It would be easy to get one of them wrong.

Confidence can be increased via mechanized proof-checking.

However, how to understand name binding, and how to deal with it ina logic or a proof assistant, is still partly an open issue.

For theoretical bases, see Gabbay and Pitts [2002] andPitts [2006]. There is also work within proof assistants, e.g.Coq [Aydemir et al., 2008, Chlipala, 2008],Isabelle/HOL [Urban and Tasson, 2005],Twelf [Harper and Licata, 2007, Pientka, 2007].

80 / 125

Page 81: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Annihilation

An instance of Gen followed with an instance of Inst annihilate.

Lemma (Annihilation)

If Γ ` t : S admits a derivation with skeleton ∆/Gen/Inst, then itadmits a derivation with skeleton ∆.

Proof.

By the Type Substitution lemma (see next slides)...

81 / 125

Page 82: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Annihilation

Up to a renaming of Gen’s premise, the hypothesis is:

Inst

GenΓ ` t : T X # Γ

Γ ` t : ∀X.TΓ ` t : [~X 7� ~T]T

where the derivation of Γ ` t : T has skeleton ∆.

By the Type Substitution lemma, there is a derivation of:

[~X 7� ~T]Γ ` t : [~X 7� ~T]T

with skeleton ∆.

Because X # Γ, this is exactly:

Γ ` t : [~X 7� ~T]T

82 / 125

Page 83: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Annihilation

Up to a renaming of Gen’s premise, the hypothesis is:

Inst

GenΓ ` t : T X # Γ

Γ ` t : ∀X.TΓ ` t : [~X 7� ~T]T

where the derivation of Γ ` t : T has skeleton ∆.

By the Type Substitution lemma, there is a derivation of:

[~X 7� ~T]Γ ` t : [~X 7� ~T]T

with skeleton ∆.

Because X # Γ, this is exactly:

Γ ` t : [~X 7� ~T]T

83 / 125

Page 84: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Annihilation

Up to a renaming of Gen’s premise, the hypothesis is:

Inst

GenΓ ` t : T X # Γ

Γ ` t : ∀X.TΓ ` t : [~X 7� ~T]T

where the derivation of Γ ` t : T has skeleton ∆.

By the Type Substitution lemma, there is a derivation of:

[~X 7� ~T]Γ ` t : [~X 7� ~T]T

with skeleton ∆.

Because X # Γ, this is exactly:

Γ ` t : [~X 7� ~T]T

84 / 125

Page 85: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Annihilation

In Damas and Milner’s type system, back a non-trivial instance ofGen cannot appear above Abs, App, Let (at right), or Gen. It canappear above Let (at left) or Inst.

A non-trivial instance of Inst cannot appear below Abs, App, Let, orInst. It can appear below Var or Gen.

The Annihilation lemma implies that disallowing Gen above Inst removesno expressive power.

In summary, Gen is useful only above Let (at left), or possibly at theroot of the derivation; and Inst is useful only below Var.

This leads to an alternative formulation of the type system...

85 / 125

Page 86: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Typing rules

Here is the standard, syntax-directed presentation of Damas andMilner’s type system.

VarInstΓ(x) = ∀X.T

Γ ` x : [~X 7� ~T]T

AbsΓ; x : T ` t : T ′

Γ ` λx.t : T → T ′

App

Γ ` t1 : T → T ′ Γ ` t2 : T

Γ ` t1 t2 : T ′

GenLetΓ ` t1 : T1 X # ΓΓ; x : ∀X.T1 ` t2 : T2

Γ ` let x = t1 in t2 : T2

Judgements are now of the form Γ ` t : T .

86 / 125

Page 87: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Correspondence

The two presentations are equivalent:

Lemma (Equivalence)

Let X # Γ. The non-syntax-directed presentation derives Γ ` t : ∀X.T ifand only if the syntax-directed presentation derives Γ ` t : T .

This is good to know in itself.

Furthermore, this means that, in the subject reduction proof thatfollows, we can deconstruct syntax-directed derivations (nice, becausethere are fewer) and build non-syntax-directed derivations (nice,because there are more).

87 / 125

Page 88: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Value substitution

As in the simply-typed λ-calculus, we prove a straightforward valuesubstitution lemma:

Lemma (Value substitution)

x : S, Γ ` t : T and x 6∈ dom(Γ) and ∅ ` v : S imply Γ ` [x 7� v]t : T .

Here, the lemma is formulated in terms of the original presentation ofthe system.

By the way, this means that, if a term is well-typed, then so is itslet-normal form. The converse is also true, and will be shown when westudy type inference.

88 / 125

Page 89: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Subject reduction

To prove subject reduction, we assume that the syntax-directedpresentation derives Γ ` t : T , we assume t �� t′, and check thatΓ ` t′ : T holds in the original presentation.

The proof is immediate:

• Case (β): deconstruct App and Abs, then apply the valuesubstitution lemma;

• Case (let): deconstruct GenLet, then apply Gen and the valuesubstitution lemma;

• Case (context): routine.

Progress is proved just as in the simply-typed λ-calculus, working onthe syntax-directed presentation.

89 / 125

Page 90: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Bottom line

In summary, Type Substitution and Annihilation are the key propertiesthat make the type system sound.

For further reading, see Wright and Felleisen [1994], Pierce [2002],Pottier and Remy [2005].

90 / 125

Page 91: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Contents

Why polymorphism?

Polymorphic λ-calculus

Damas and Milner’s type system

Type soundness

Polymorphism and references

Bibliography

91 / 125

Page 92: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Hints of unsoundness

In a previous session, we noted that the program:

let x = ref 3 in (x := 1; !x)

does not have the same semantics as its let-normal form:

ref 3; (ref 3) := 1; !(ref 3)

In the presence of effects, a term and its let-normal form do nothave the same semantics, so the naıve approach to polymorphism,based on let-normal forms, back has no reason to be sound.

Damas and Milner’s type system, which (we will prove) derives thesame (monomorphic) typings as the naıve approach, has no reason tobe sound either...

92 / 125

Page 93: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Hints of unsoundness

In the last course, we also noted that type soundness strongly relieson the fact that every reference cell has a fixed type.

So, it is important to rule out polymorphic references: cells that admitmultiple types at once. In short, a type of the form:

∀X.ref T

(where X appears in T ) should never be inhabited.

Right?

93 / 125

Page 94: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Unsoundness revealed

Right! Yet, if naıvely extended with references, Damas and Milner’stype system allows constructing polymorphic references.

This well-typed program, where x receives the type scheme∀X.ref (X → X), goes wrong:

let x = ref (λz.z) in x := (λz.z + 1); !x true

The cell x is written at type int→ int, then read at type bool→ bool.

94 / 125

Page 95: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

The interaction of polymorphism and references

We have proved type soundness for references without polymorphism,and for polymorphism without references, but the combination fails. Ah!

Let’s review the proof for references and polymorphism together.

95 / 125

Page 96: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Augmenting typing judgements

First, we augment typing judgements so that they take the form:

M, Γ ` t : S

where M is a store typing, which maps memory locations to...

types.

No choice here: the syntax of types is T ::= . . . | ref T , notT ::= . . . | ref S, so the contents of a cell must have monomorphic type.

This restriction is imposed by the design of ML. It is not required forsoundness. In System F with references, a type of the form ref (∀X.T )would be fine.

96 / 125

Page 97: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Augmenting typing judgements

First, we augment typing judgements so that they take the form:

M, Γ ` t : S

where M is a store typing, which maps memory locations to... types.

No choice here: the syntax of types is T ::= . . . | ref T , notT ::= . . . | ref S, so the contents of a cell must have monomorphic type.

This restriction is imposed by the design of ML. It is not required forsoundness. In System F with references, a type of the form ref (∀X.T )would be fine.

97 / 125

Page 98: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Clarifying the typing rules

The two novel rules of Damas and Milner’s type system become:

GenM, Γ ` t : T X # Γ

M, Γ ` t : ∀X.T

InstM, Γ ` t : ∀X.T

M, Γ ` t : [~X 7� ~T]T

Right?

No way! This version of Gen is broken. Because X can appear in M,the Type Substitution lemma does not hold. So...

98 / 125

Page 99: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Clarifying the typing rules

The two novel rules of Damas and Milner’s type system become:

GenM, Γ ` t : T X # Γ

M, Γ ` t : ∀X.T

InstM, Γ ` t : ∀X.T

M, Γ ` t : [~X 7� ~T]T

Right?

No way! This version of Gen is broken. Because X can appear in M,the Type Substitution lemma does not hold. So...

99 / 125

Page 100: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Clarifying the typing rules

The correct rule is, of course:

GenM, Γ ` t : T X # M, Γ

M, Γ ` t : ∀X.T

Mysterious slogan #1: one must not generalize a type variable thatappears in the store typing. Aha!

This version satisfies Type Substitution.

Yet, the counter-example program shows that Subject Reduction isstill broken... Where is the bug?

100 / 125

Page 101: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Nailing the bug

The problem lies in the (context) case of the Subject Reduction proof,and more specifically in the case of reduction under Gen (that is,reduction under the left-hand side of GenLet).

The hypotheses are:

M, ∅ ` t : T X # M

M, ∅ ` t : ∀X.Tand ` µ : M and t/µ �� t′/µ′

By the induction hypothesis, there exists M′ such that:

M′, ∅ ` t′ : T and ` µ′ : M′ and M ⊆ M′

Here, we are stuck. We would like to build a new instance of Gen, butwe are missing X # M′.

101 / 125

Page 102: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Nailing the bug

The problem lies in the (context) case of the Subject Reduction proof,and more specifically in the case of reduction under Gen (that is,reduction under the left-hand side of GenLet).

The hypotheses are:

M, ∅ ` t : T X # M

M, ∅ ` t : ∀X.Tand ` µ : M and t/µ �� t′/µ′

By the induction hypothesis, there exists M′ such that:

M′, ∅ ` t′ : T and ` µ′ : M′ and M ⊆ M′

Here, we are stuck. We would like to build a new instance of Gen, butwe are missing X # M′.

102 / 125

Page 103: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Nailing the bug

The problem lies in the (context) case of the Subject Reduction proof,and more specifically in the case of reduction under Gen (that is,reduction under the left-hand side of GenLet).

The hypotheses are:

M, ∅ ` t : T X # M

M, ∅ ` t : ∀X.Tand ` µ : M and t/µ �� t′/µ′

By the induction hypothesis, there exists M′ such that:

M′, ∅ ` t′ : T and ` µ′ : M′ and M ⊆ M′

Here, we are stuck. We would like to build a new instance of Gen, butwe are missing X # M′.

103 / 125

Page 104: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Nailing the bug

Mysterious slogan #2: one must not generalize a type variable thatmight, after evaluation of the term, enter the store typing. Aha!

This is what happens in the counter-example:

let x = ref (λz.z : X → X) in x := (λz.z + 1); !x true

The type variable X is generalized by GenLet. Yet, when ref (λz.z)reduces, X → X becomes the type of the newly allocated cell, so itappears in the new store typing.

This is all well and good, but how do we enforce slogan #2? Shouldwe somehow restrict X so as to ensure X # M′?

104 / 125

Page 105: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Fixing the bug

A number of rather complex historic approaches have been followed:see Leroy [1992] for a survey.

Then came Wright [1995], who suggested an amazingly simple solution,known as the value restriction: only values can be polymorphic.

GenM, Γ ` v : T X # M, Γ

M, Γ ` v : ∀X.T

The problematic proof case vanishes: we now never reduce under Gen.Subject Reduction holds again.

105 / 125

Page 106: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Consequences

In its syntax-directed presentation, the system becomes:

GenLetΓ ` v1 : T1 X # ΓΓ; x : ∀X.T1 ` t2 : T2

Γ ` let x = v1 in t2 : T2

MonoLetΓ ` t1 : T1 (t1 not a value)

Γ; x : T1 ` t2 : T2

Γ ` let x = t1 in t2 : T2

106 / 125

Page 107: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Consequences

The problematic program is now ill-typed:

let x = ref (λz.z) in x := (λz.z + 1); !x true

Indeed, ref (λz.z) is not a value, so Gen is not applicable. The variablex must receive a monotype, but none is suitable.

107 / 125

Page 108: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Consequences

With the value restriction, some pure programs become ill-typed, eventhough they were well-typed in the absence of references. This style ofintroducing references in ML is not a conservative extension.

This definition cannot receive a polymorphic type scheme:

let f = map id list T → list T , for any type T

A common work-around is to perform a manual η-expansion:

let f xs = map id xs ∀X.list X → list X

Of course, in the presence of side effects, η-expansion is notsemantics-preserving, so this must not be done blindly.

108 / 125

Page 109: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

In practice

The value restriction can be slightly relaxed by delimiting a syntacticcategory of so-called non-expansive terms – terms whose evaluationdefinitely will not allocate new reference cells. Non-expansive termsform a strict superset of values.

Garrigue [2004] relaxes the value restriction in a more subtle way,which is justified by a subtyping argument.

Objective Caml implements both refinements.

109 / 125

Page 110: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Conclusion

Experience has shown that the value restriction is tolerable. Eventhough it is not conservative, the search for better solutions hasbeen pretty much abandoned.

110 / 125

Page 111: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Conclusion

In a type-and-effectsystem [Lucassen and Gifford, 1988, Talpin and Jouvelot, 1994], orin a type-and-capability system [Chargueraud and Pottier, 2008], thetype system indicates which expressions may allocate new references,and at which type.

There, the value restriction is no longer necessary.

However, if one extends a type-and-capability system with a mechanismfor hiding state, the need for the value restriction re-appears.

111 / 125

Page 112: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Contents

Why polymorphism?

Polymorphic λ-calculus

Damas and Milner’s type system

Type soundness

Polymorphism and references

Bibliography

112 / 125

Page 113: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

Bibliography I

(Most titles are clickable links to online versions.)

Aydemir, B., Chargeraud, A., Pierce, B., Pollack, R., and Weirich, S.2008.Engineering formal metatheory.In ACM Symposium on Principles of Programming Languages (POPL).3–15.

Chargueraud, A. and Pottier, F. 2008.Functional translation of a calculus of capabilities.In ACM International Conference on Functional Programming (ICFP).213–224.

113 / 125

Page 114: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ II

Bibliography]Bibliography

Chlipala, A. 2008.Parametric higher-order abstract syntax for mechanized semantics.

In ACM International Conference on Functional Programming (ICFP).143–156.

Crary, K., Weirich, S., and Morrisett, G. 2002.Intensional polymorphism in type erasure semantics.Journal of Functional Programming 12, 6 (Nov.), 567–600.

Gabbay, M. J. and Pitts, A. M. 2002.A new approach to abstract syntax with variable binding.Formal Aspects of Computing 13, 3–5 (July), 341–363.

114 / 125

Page 115: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ III

[][

Garrigue, J. 2004.Relaxing the value restriction.In Functional and Logic Programming. Lecture Notes in ComputerScience, vol. 2998. Springer, 196–213.

Harper, R. and Licata, D. R. 2007.Mechanizing metatheory in a logical framework.Journal of Functional Programming 17, 4–5, 613–673.

Hudak, P., Hughes, J., Peyton Jones, S., and Wadler, P. 2007.A history of Haskell: being lazy with class.In ACM SIGPLAN Conference on History of Programming Languages.

115 / 125

Page 116: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ IV

[][

Le Botlan, D. and Remy, D. 2003.MLF: Raising ML to the power of system F .In ACM International Conference on Functional Programming (ICFP).27–38.

Leroy, X. 1992.Typage polymorphe d’un langage algorithmique.Ph.D. thesis, Universite Paris 7.

Lucassen, J. M. and Gifford, D. K. 1988.Polymorphic effect systems.In ACM Symposium on Principles of Programming Languages (POPL).47–57.

116 / 125

Page 117: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ V

[][

Milner, R. 1978.A theory of type polymorphism in programming.Journal of Computer and System Sciences 17, 3 (Dec.), 348–375.

Minamide, Y., Morrisett, G., and Harper, R. 1996.Typed closure conversion.In ACM Symposium on Principles of Programming Languages (POPL).271–283.

Mitchell, J. C. 1988.Polymorphic type inference and containment.Information and Computation 76, 2–3, 211–249.

117 / 125

Page 118: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ VI

[][

Morrisett, G., Walker, D., Crary, K., and Glew, N. 1999.From system F to typed assembly language.ACM Transactions on Programming Languages and Systems 21, 3(May), 528–569.

Odersky, M., Zenger, M., and Zenger, C. 2001.Colored local type inference.In ACM Symposium on Principles of Programming Languages (POPL).41–53.

118 / 125

Page 119: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ VII

[][

Pientka, B. 2007.Proof pearl: The power of higher-order encodings in the logicalframework LF.In International Conference on Theorem Proving in Higher OrderLogics (TPHOLs). Lecture Notes in Computer Science, vol. 4732.Springer, 246–261.

Pierce, B. C. 2002.Types and Programming Languages.MIT Press.

119 / 125

Page 120: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ VIII

[][

Pierce, B. C. and Turner, D. N. 2000.Local type inference.ACM Transactions on Programming Languages and Systems 22, 1(Jan.), 1–44.

Pitts, A. M. 2000.Parametric polymorphism and operational equivalence.Mathematical Structures in Computer Science 10, 321–359.

Pitts, A. M. 2006.Alpha-structural recursion and induction.Journal of the ACM 53, 459–506.

120 / 125

Page 121: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ IX

[][

Pottier, F. and Remy, D. 2005.The essence of ML type inference.In Advanced Topics in Types and Programming Languages, B. C.Pierce, Ed. MIT Press, Chapter 10, 389–489.

Reynolds, J. C. 1974.Towards a theory of type structure.In Colloque sur la Programmation. Lecture Notes in ComputerScience, vol. 19. Springer, 408–425.

Reynolds, J. C. 1983.Types, abstraction and parametric polymorphism.In Information Processing 83. Elsevier Science, 513–523.

121 / 125

Page 122: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ X

[][

Strachey, C. 2000.Fundamental concepts in programming languages.Higher-Order and Symbolic Computation 13, 1–2 (Apr.), 11–49.

Talpin, J.-P. and Jouvelot, P. 1994.The type and effect discipline.Information and Computation 11, 2, 245–296.

Tiuryn, J. and Urzyczyn, P. 2002.The subtyping problem for second-order types is undecidable.Information and Computation 179, 1, 1–18.

122 / 125

Page 123: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ XI

[][

Urban, C. and Tasson, C. 2005.Nominal techniques in Isabelle/HOL.In International Conference on Automated Deduction (CADE).Lecture Notes in Computer Science, vol. 3632. Springer, 38–53.

Wadler, P. 1989.Theorems for free!In Conference on Functional Programming Languages and ComputerArchitecture (FPCA). 347–359.

Wadler, P. 2007.The Girard-Reynolds isomorphism (second edition).Theoretical Computer Science 375, 1–3 (May), 201–226.

123 / 125

Page 124: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ XII

[][

Wells, J. B. 1995.The undecidability of Mitchell’s subtyping relation.Technical Report 95-019, Computer Science Department, BostonUniversity. Dec.

Wells, J. B. 1999.Typability and type checking in system F are equivalent andundecidable.Annals of Pure and Applied Logic 98, 1–3, 111–156.

124 / 125

Page 125: 2-4-2 / Type systems Polymorphismgallium.inria.fr/~fpottier/mpri/cours02.pdf · Ad hoc versus parametric: type classes By the first definition, Haskell’s type classes [Hudak et

[ XIII

[][

Wells, J. B. 2002.The essence of principal typings.In International Colloquium on Automata, Languages andProgramming. Lecture Notes in Computer Science, vol. 2380.Springer, 913–925.

Wright, A. K. 1995.Simple imperative polymorphism.Lisp and Symbolic Computation 8, 4 (Dec.), 343–356.

Wright, A. K. and Felleisen, M. 1994.A syntactic approach to type soundness.Information and Computation 115, 1 (Nov.), 38–94.

125 / 125