Second Order Types Giuseppe Castagna: Foundation of OOP Tutorial - - PDF document

second order types
SMART_READER_LITE
LIVE PREVIEW

Second Order Types Giuseppe Castagna: Foundation of OOP Tutorial - - PDF document

Second Order Types Giuseppe Castagna: Foundation of OOP Tutorial Slides) 68 0 0 Loss of information Consider the function I = x T .x : T T By the rule for application I : T T M : U < T I ( M ) : T Therefore ( x a :


slide-1
SLIDE 1

Second Order Types

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 68

slide-2
SLIDE 2

Loss of information Consider the function I = λxT .x : T → T By the rule for application I : T → T M : U < T I(M) : T Therefore (λx a:int .x)a = 1 b = 2 : a : int

  • Second order

I : ∀X ≤ T.X → X Two ways:

  • 1. Implicit polymorphism
  • 2. Explicit polymorphism

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 69 62

slide-3
SLIDE 3

Implicit polymorphism No types in terms λx.x : ∀α.α → α (λx.x)3 : int x : α x : α

[α = β]

λx.x : α → β 3 : int

[α = int]

(λx.x)3 : β Subtyping λx.((λy.x)(x. + 3)) : ∀α≤ : int .α → α Therefore λx.((λy.x)(x.+3))( = 1 m = true) : : int m : bool

  • Giuseppe Castagna: Foundation of OOP Tutorial Slides)

70

slide-4
SLIDE 4

Inference with subtyping x:α y:γ x : α

α = β

x:α λy.x : γ → β x:α 3 : int x:α x:α

α ≤ :

  • x:α x. :

δ = int ≤int

x:α x. + 3 : δ

δ≤γ

x:α (λy.x)(x. + 3) : β λx.((λy.x)(x. + 3)) : α → β Resulting type ∀≤int . ∀α≤ : . α → α Simplified ∀α≤ :int . α → α

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 71

slide-5
SLIDE 5

Explicit polymorphism ΛX.λxX.x : ∀X.X → X The programmer specifies the type (ΛX.λxX.x)(int)(3)

  • (λxint.x)(3)

Subtyping ΛX ≤ a:int .λxX.x The application (ΛX ≤ a:int .λxX.x)( a:int b:int ) has type

  • a:int b:int

→ a:int b:int

  • thus

(ΛX ≤ a:int .λxX .x)( a:int b:int )(a = 1 b = 3) has type

  • a:int b:int
  • Giuseppe Castagna: Foundation of OOP Tutorial Slides)

72

slide-6
SLIDE 6

F≤ Types T ::= X | Top | T → T | ∀(X≤T)T Terms a ::= x | (λxT .a) | a(a) |

top | ΛX≤T.a | a(T)

Reduction (β) (λxT .a)(b) a[xT := b] (β∀) (ΛX≤T.a)(T ) a[X := T ]

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 73

slide-7
SLIDE 7

Subtyping (refl) C T≤T (trans) C T1 ≤ T2 C T2 ≤ T3 C T1 ≤ T3 (taut) C X ≤ C(X) (Top) C T ≤ Top (→) C T1≤S1 C S2≤T2 C S1 → S2≤T1 → T2 (∀) C T1≤S1 C (X≤T1) S2≤T2 C ∀(X≤S1)S2 ≤ ∀(X≤T1)T2

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 74

slide-8
SLIDE 8

Type system [Vars] C ; Γ x: Γ(x) [→Intro] C ; Γ (x: T) a: T C ; Γ (λxT .a): T → T [→Elim] C ; Γ a: S → T C ; Γ b: S C ; Γ a(b): T [Top] C ; Γ top: Top [∀Intro] C (X≤T) ; Γ a: T C ; Γ ΛX≤T.a: ∀(X≤T)T [∀Elim] C ; Γ a: ∀(X≤S)T C ; Γ a(S): T[X := S] [Subsump] C ; Γ a: T C T ≤ T C ; Γ a: T

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 75

slide-9
SLIDE 9

Transitivity elimination c : : = IdA | XT | TopT | c → c | ∀(X≤c)c | c c

(refl)

C IdA: A ≤ A

(trans)

C c: T1 ≤ T2 C c: T2 ≤ T3 C c c: T1 ≤ T3

(taut)

C ∪ X≤T} XT : X ≤ T

(Top)

C TopT : T ≤ Top

(→)

C c1: T

1 ≤ T1

C c2: T2 ≤ T

2

C c1 → c2: T1 → T2 ≤ T

1 → T 2 (∀)

C c1: T

1 ≤ T1

C ∪ X≤T

1} c2: T2 ≤ T 2

C ∀(X≤c1)c2: ∀(X≤T1)T2 ≤ ∀(X≤T

1)T 2

Theorem 5 There is a 1-1 correspondence be- tween well-typed coerce expressions and sub- typing derivations.

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 76

slide-10
SLIDE 10

The rewriting system (Asc) (c d) e

  • c (d e)

(→) (c → d) (c → d)

  • (c c) → (d d)

(→) (c → d) ((c → d) e)

  • ((c c) → (d d)) e

(∀) (∀(X≤c)d) (∀(X≤c)d)

  • ∀(X≤c c)(d d[XT : = c XS])

(∀) (∀(X≤c)d) ((∀(X≤c)d) e)

  • (∀(X≤c c)(d d[XT:=c XS]))e

Normal forms are subterms of (c → d) e1 . . . en or of (∀(X≤ c)d) e1 . . . en where c ci d di are in normal form and e1 . . . en are either Xt or TopT . They normal forms correspond to derivations in which every left premise of a (trans) rule is a leaf. Thus, the rewriting system pushes the transitivity up to the leaves.

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 76

slide-11
SLIDE 11

Example (c → d) ((c → d) e) ((c c) → (d d)) e Theorem 6 (Soundness) If c

d and C c: Δ then C d: Δ Theorem 7 (Weak normalization) Every in- nermost strategy for terminates.

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 76

slide-12
SLIDE 12

Coherence Let c : S ≤ T (idl) IdT c

  • c

(idr) c IdS

  • c

(top)

TopT c

TopS (varTop) XTop TopX Consider the composition of the rewriting sys- tems: Theorem 8 (normal forms) Every well-typed coerce expression in normal form has the form c0 c1 ... cn with n ≥ 0, where c0 can be any co- erce expression different from a composition

  • f other coerce expressions) whose subformu-

lae are in normal form, and c1 . . . cn are vari- ables. Theorem 9 For every provable subtyping judg- ment, there exists only one coerce expression in normal form proving it.

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 76

slide-13
SLIDE 13

Coherence Theorem 10 (coherence) Let Π1 and Π2 be two proofs of the same judgment C Δ. If c1 and c2 are the corresponding coerce ex- pressions then c1 and c2 are equal modulo the rewriting system. Shape of NFs and the subtyping algorithm The normal forms of Theorem 8 correspond to derivations in which every application of a (trans) rule has as left premise an application

  • f the rule (taut).

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 76

slide-14
SLIDE 14

Subtyping algorithm (AlgRefl) C X≤X (AlgTrans) C C(X) ≤ T C X ≤ T (Top) C T ≤ Top (→) C T1≤S1 C S2≤T2 C S1 → S2≤T1 → T2 (∀) C T1≤S1 C (X≤T1) S2≤T2 C ∀(X≤S1)S2 ≤ ∀(X≤T1)T2

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 76

slide-15
SLIDE 15

Typing algorithm [Vars] C ; Γ x: Γ(x) [→I] C ; Γ (x: T) a: T C ; Γ (λxT .a): T → T [→E] C; Γ a: U C; Γ b: S C S≤S C; Γ a(b): T

BC(U) = S → T

[Top] C ; Γ top: Top [∀I] C (X≤T) ; Γ a: T C ; Γ ΛX≤T.a: ∀(X≤T)T [∀E] C ; Γ a: U C S ≤ S C ; Γ a(S): T[X := S]

BC(U) = ∀(X≤S)T

Definition 2

BC(T) =

  • BC(C(X))

if T ≡ X T

  • therwise

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 77

slide-16
SLIDE 16

Typing and subtyping algorithms are sound and complete Sound and complete does not mean decidable let ¬T and ∀(X)T denote T → Top and ∀(X≤Top)T:

X0 ≤ ∀(Y )¬(∀(Z ≤ Y )¬Y )

  • X0

≤ ∀(X1≤X0)¬X0 by applying AlgTrans: X0 ≤ ∀(Y )¬(∀(Z ≤ Y )¬Y ) ∀(X1)¬(∀(X2≤X1)¬X1) ≤ ∀(X1≤X0)¬X0 by applying (∀): X0 ≤ ∀(Y )¬(∀(Z ≤ Y )¬Y ) X1≤X0 ¬(∀(X2≤X1)¬X1) ≤ ¬X0 by the contravariance of (→): X0 ≤ ∀(Y )¬(∀(Z ≤ Y )¬Y ) X1≤X0 X0 ≤ ∀(X2≤X1)¬X1 the same judgement as the one we started from.

Just semi-decidability holds Kernel-Fun: compare quantifications with equal bounds.

Giuseppe Castagna: Foundation of OOP Tutorial Slides) 78