Free Theorems The Basics Janis Voigtl ander Technische Universit - - PowerPoint PPT Presentation

free theorems the basics
SMART_READER_LITE
LIVE PREVIEW

Free Theorems The Basics Janis Voigtl ander Technische Universit - - PowerPoint PPT Presentation

Free Theorems The Basics Janis Voigtl ander Technische Universit at Dresden January 6, 2006 Outline Example in Haskell Parametric polymorphism Polymorphic lambda calculus Parametricity theorem Back to Haskell 2 Haskell Example:


slide-1
SLIDE 1

Free Theorems — The Basics

Janis Voigtl¨ ander

Technische Universit¨ at Dresden

January 6, 2006

slide-2
SLIDE 2

Outline

Example in Haskell Parametric polymorphism Polymorphic lambda calculus Parametricity theorem Back to Haskell

2

slide-3
SLIDE 3

Haskell Example:

filter :: ∀α. (α → Bool) → [α] → [α] filter p [] = [] filter p (x : xs) = if p x then x : filter p xs else filter p xs

3

slide-4
SLIDE 4

Haskell Example:

filter :: ∀α. (α → Bool) → [α] → [α] filter p [] = [] filter p (x : xs) = if p x then x : filter p xs else filter p xs Claim: filter p (map h l) = map h (filter (p ◦ h) l) (1) Can be proved by induction on l, using the definition of filter.

3

slide-5
SLIDE 5

Haskell Example: Theorems for free! [Wadler 1989]

filter :: ∀α. (α → Bool) → [α] → [α] Claim: filter p (map h l) = map h (filter (p ◦ h) l) (1) Can be derived from the parametric polymorphic type of filter!

3

slide-6
SLIDE 6

Haskell Example: Theorems for free! [Wadler 1989]

filter :: ∀α. (α → Bool) → [α] → [α] Claim: filter p (map h l) = map h (filter (p ◦ h) l) (1) Can be derived from the parametric polymorphic type of filter! Where is the magic? Where is the induction?

3

slide-7
SLIDE 7

Parametric Polymorphism, Intuitively

◮ filter :: ∀α. (α → Bool) → [α] → [α] must work uniformly

for every instantiation of α.

4

slide-8
SLIDE 8

Parametric Polymorphism, Intuitively

◮ filter :: ∀α. (α → Bool) → [α] → [α] must work uniformly

for every instantiation of α.

◮ The output list can only contain elements from the input list l.

4

slide-9
SLIDE 9

Parametric Polymorphism, Intuitively

◮ filter :: ∀α. (α → Bool) → [α] → [α] must work uniformly

for every instantiation of α.

◮ The output list can only contain elements from the input list l. ◮ Which, and in which order/multiplicity, can only be decided

based on l and the input predicate p.

4

slide-10
SLIDE 10

Parametric Polymorphism, Intuitively

◮ filter :: ∀α. (α → Bool) → [α] → [α] must work uniformly

for every instantiation of α.

◮ The output list can only contain elements from the input list l. ◮ Which, and in which order/multiplicity, can only be decided

based on l and the input predicate p.

◮ The only means for this decision are to inspect the length of l

and to check the outcome of p on its elements.

4

slide-11
SLIDE 11

Parametric Polymorphism, Intuitively

◮ filter :: ∀α. (α → Bool) → [α] → [α] must work uniformly

for every instantiation of α.

◮ The output list can only contain elements from the input list l. ◮ Which, and in which order/multiplicity, can only be decided

based on l and the input predicate p.

◮ The only means for this decision are to inspect the length of l

and to check the outcome of p on its elements.

◮ The lists (map h l) and l always have equal length.

4

slide-12
SLIDE 12

Parametric Polymorphism, Intuitively

◮ filter :: ∀α. (α → Bool) → [α] → [α] must work uniformly

for every instantiation of α.

◮ The output list can only contain elements from the input list l. ◮ Which, and in which order/multiplicity, can only be decided

based on l and the input predicate p.

◮ The only means for this decision are to inspect the length of l

and to check the outcome of p on its elements.

◮ The lists (map h l) and l always have equal length. ◮ Applying p to an element of (map h l) always has the same

  • utcome as applying (p ◦ h) to the corresponding element of l.

4

slide-13
SLIDE 13

Parametric Polymorphism, Intuitively

◮ filter :: ∀α. (α → Bool) → [α] → [α] must work uniformly

for every instantiation of α.

◮ The output list can only contain elements from the input list l. ◮ Which, and in which order/multiplicity, can only be decided

based on l and the input predicate p.

◮ The only means for this decision are to inspect the length of l

and to check the outcome of p on its elements.

◮ The lists (map h l) and l always have equal length. ◮ Applying p to an element of (map h l) always has the same

  • utcome as applying (p ◦ h) to the corresponding element of l.

◮ filter with p always chooses “the same” elements from

(map h l) for output as does filter with (p ◦ h) from l,

4

slide-14
SLIDE 14

Parametric Polymorphism, Intuitively

◮ filter :: ∀α. (α → Bool) → [α] → [α] must work uniformly

for every instantiation of α.

◮ The output list can only contain elements from the input list l. ◮ Which, and in which order/multiplicity, can only be decided

based on l and the input predicate p.

◮ The only means for this decision are to inspect the length of l

and to check the outcome of p on its elements.

◮ The lists (map h l) and l always have equal length. ◮ Applying p to an element of (map h l) always has the same

  • utcome as applying (p ◦ h) to the corresponding element of l.

◮ filter with p always chooses “the same” elements from

(map h l) for output as does filter with (p ◦ h) from l, except that it outputs their images under h.

4

slide-15
SLIDE 15

Parametric Polymorphism, Intuitively

◮ filter :: ∀α. (α → Bool) → [α] → [α] must work uniformly

for every instantiation of α.

◮ The output list can only contain elements from the input list l. ◮ Which, and in which order/multiplicity, can only be decided

based on l and the input predicate p.

◮ The only means for this decision are to inspect the length of l

and to check the outcome of p on its elements.

◮ The lists (map h l) and l always have equal length. ◮ Applying p to an element of (map h l) always has the same

  • utcome as applying (p ◦ h) to the corresponding element of l.

◮ filter with p always chooses “the same” elements from

(map h l) for output as does filter with (p ◦ h) from l, except that it outputs their images under h.

◮ (filter p (map h l)) is equivalent to (map h (filter (p ◦ h) l)).

4

slide-16
SLIDE 16

Parametric Polymorphism, Intuitively

◮ filter :: ∀α. (α → Bool) → [α] → [α] must work uniformly

for every instantiation of α.

◮ The output list can only contain elements from the input list l. ◮ Which, and in which order/multiplicity, can only be decided

based on l and the input predicate p.

◮ The only means for this decision are to inspect the length of l

and to check the outcome of p on its elements.

◮ The lists (map h l) and l always have equal length. ◮ Applying p to an element of (map h l) always has the same

  • utcome as applying (p ◦ h) to the corresponding element of l.

◮ filter with p always chooses “the same” elements from

(map h l) for output as does filter with (p ◦ h) from l, except that it outputs their images under h.

◮ (filter p (map h l)) is equivalent to (map h (filter (p ◦ h) l)). ◮ That is what we wanted to prove!

4

slide-17
SLIDE 17

Parametric Polymorphism, More Formally

Question: What functions are in ∀α. (α → Bool) → [α] → [α] ? Approach: Give denotations of types as sets.

5

slide-18
SLIDE 18

Parametric Polymorphism, More Formally

Question: What functions are in ∀α. (α → Bool) → [α] → [α] ? Approach: Give denotations of types as sets. [ [Bool] ]θ = {True, False} = B [ [Nat] ]θ = {0, 1, 2, . . . } = N

5

slide-19
SLIDE 19

Parametric Polymorphism, More Formally

Question: What functions are in ∀α. (α → Bool) → [α] → [α] ? Approach: Give denotations of types as sets. [ [Bool] ]θ = {True, False} = B [ [Nat] ]θ = {0, 1, 2, . . . } = N [ [(τ1, τ2)] ]θ = [ [τ1] ]θ × [ [τ2] ]θ [ [[τ]] ]θ = {[x1, . . . , xn] | n ≥ 0, xi ∈ [ [τ] ]θ}

5

slide-20
SLIDE 20

Parametric Polymorphism, More Formally

Question: What functions are in ∀α. (α → Bool) → [α] → [α] ? Approach: Give denotations of types as sets. [ [Bool] ]θ = {True, False} = B [ [Nat] ]θ = {0, 1, 2, . . . } = N [ [(τ1, τ2)] ]θ = [ [τ1] ]θ × [ [τ2] ]θ [ [[τ]] ]θ = {[x1, . . . , xn] | n ≥ 0, xi ∈ [ [τ] ]θ} [ [τ1 → τ2] ]θ = {f : [ [τ1] ]θ → [ [τ2] ]θ}

5

slide-21
SLIDE 21

Parametric Polymorphism, More Formally

Question: What functions are in ∀α. (α → Bool) → [α] → [α] ? Approach: Give denotations of types as sets. [ [Bool] ]θ = {True, False} = B [ [Nat] ]θ = {0, 1, 2, . . . } = N [ [(τ1, τ2)] ]θ = [ [τ1] ]θ × [ [τ2] ]θ [ [[τ]] ]θ = {[x1, . . . , xn] | n ≥ 0, xi ∈ [ [τ] ]θ} [ [τ1 → τ2] ]θ = {f : [ [τ1] ]θ → [ [τ2] ]θ} [ [∀α. τ] ]θ = ?

5

slide-22
SLIDE 22

Parametric Polymorphism, More Formally

Question: What functions are in ∀α. (α → Bool) → [α] → [α] ? Approach: Give denotations of types as sets. [ [Bool] ]θ = {True, False} = B [ [Nat] ]θ = {0, 1, 2, . . . } = N [ [(τ1, τ2)] ]θ = [ [τ1] ]θ × [ [τ2] ]θ [ [[τ]] ]θ = {[x1, . . . , xn] | n ≥ 0, xi ∈ [ [τ] ]θ} [ [τ1 → τ2] ]θ = {f : [ [τ1] ]θ → [ [τ2] ]θ} [ [∀α. τ] ]θ = ?

◮ g ∈ [

[∀α. τ] ]θ should be a “collection” of values: for every type τ ′, there is an instance of type τ[τ ′/α].

5

slide-23
SLIDE 23

Parametric Polymorphism, More Formally

Question: What functions are in ∀α. (α → Bool) → [α] → [α] ? Approach: Give denotations of types as sets. [ [Bool] ]θ = {True, False} = B [ [Nat] ]θ = {0, 1, 2, . . . } = N [ [(τ1, τ2)] ]θ = [ [τ1] ]θ × [ [τ2] ]θ [ [[τ]] ]θ = {[x1, . . . , xn] | n ≥ 0, xi ∈ [ [τ] ]θ} [ [τ1 → τ2] ]θ = {f : [ [τ1] ]θ → [ [τ2] ]θ} [ [∀α. τ] ]θ = ?

◮ g ∈ [

[∀α. τ] ]θ should be a “collection” of values: for every type τ ′, there is an instance of type τ[τ ′/α].

◮ [

[∀α. τ] ]θ = {g : Set → Value | ∀S ∈ Set. (g S) ∈ [ [τ] ]θ[α→S]} is maybe a good start, together with [ [α] ]θ = θ(α).

5

slide-24
SLIDE 24

Parametric Polymorphism, More Formally

Question: What functions are in ∀α. (α → Bool) → [α] → [α] ? Approach: Give denotations of types as sets. [ [Bool] ]θ = {True, False} = B [ [Nat] ]θ = {0, 1, 2, . . . } = N [ [(τ1, τ2)] ]θ = [ [τ1] ]θ × [ [τ2] ]θ [ [[τ]] ]θ = {[x1, . . . , xn] | n ≥ 0, xi ∈ [ [τ] ]θ} [ [τ1 → τ2] ]θ = {f : [ [τ1] ]θ → [ [τ2] ]θ} [ [∀α. τ] ]θ = ?

◮ g ∈ [

[∀α. τ] ]θ should be a “collection” of values: for every type τ ′, there is an instance of type τ[τ ′/α].

◮ [

[∀α. τ] ]θ = {g : Set → Value | ∀S ∈ Set. (g S) ∈ [ [τ] ]θ[α→S]} is maybe a good start, together with [ [α] ]θ = θ(α).

◮ But this may contain “ad-hoc” polymorphic functions!

5

slide-25
SLIDE 25

Unwanted Ad-Hoc Polymorphism: Example

◮ With the proposed definition,

[ [∀α. (α, α) → α] ]∅ = {g | ∀S ∈ Set. (g S) : S × S → S}.

6

slide-26
SLIDE 26

Unwanted Ad-Hoc Polymorphism: Example

◮ With the proposed definition,

[ [∀α. (α, α) → α] ]∅ = {g | ∀S ∈ Set. (g S) : S × S → S}.

◮ But this also allows

g B (x, y) = not x g N (x, y) = y + 1 , which is not possible in Haskell at type ∀α. (α, α) → α.

6

slide-27
SLIDE 27

Unwanted Ad-Hoc Polymorphism: Example

◮ With the proposed definition,

[ [∀α. (α, α) → α] ]∅ = {g | ∀S ∈ Set. (g S) : S × S → S}.

◮ But this also allows

g B (x, y) = not x g N (x, y) = y + 1 , which is not possible in Haskell at type ∀α. (α, α) → α.

◮ To prevent this, compare/relate

(g B) : B × B → B and (g N) : N × N → N , ensuring that they “behave identically”. But how?

6

slide-28
SLIDE 28

Key Idea [Reynolds 1983]

Use relations to tie instances together.

7

slide-29
SLIDE 29

Key Idea [Reynolds 1983]

Use relations to tie instances together. In the example:

◮ Choose an R ⊆ B × N.

7

slide-30
SLIDE 30

Key Idea [Reynolds 1983]

Use relations to tie instances together. In the example:

◮ Choose an R ⊆ B × N. ◮ Say that (x1, y1) ∈ B × B and (x2, y2) ∈ N × N are related

if (x1, x2) ∈ R and (y1, y2) ∈ R.

7

slide-31
SLIDE 31

Key Idea [Reynolds 1983]

Use relations to tie instances together. In the example:

◮ Choose an R ⊆ B × N. ◮ Say that (x1, y1) ∈ B × B and (x2, y2) ∈ N × N are related

if (x1, x2) ∈ R and (y1, y2) ∈ R.

◮ Say that f1 : B × B → B and f2 : N × N → N are related

if they map related arguments to related results.

7

slide-32
SLIDE 32

Key Idea [Reynolds 1983]

Use relations to tie instances together. In the example:

◮ Choose an R ⊆ B × N. ◮ Say that (x1, y1) ∈ B × B and (x2, y2) ∈ N × N are related

if (x1, x2) ∈ R and (y1, y2) ∈ R.

◮ Say that f1 : B × B → B and f2 : N × N → N are related

if they map related arguments to related results.

◮ Then (g B) and (g N) with

g B (x, y) = not x g N (x, y) = y + 1 are not related if we choose, e.g., R = {(True, 1)}.

7

slide-33
SLIDE 33

Key Idea [Reynolds 1983]

Use relations to tie instances together. In the example:

◮ Choose an R ⊆ B × N. ◮ Say that (x1, y1) ∈ B × B and (x2, y2) ∈ N × N are related

if (x1, x2) ∈ R and (y1, y2) ∈ R.

◮ Say that f1 : B × B → B and f2 : N × N → N are related

if they map related arguments to related results.

◮ Then (g B) and (g N) with

g B (x, y) = not x g N (x, y) = y + 1 are not related if we choose, e.g., R = {(True, 1)}. Reynolds: g ∈ [ [∀α. τ] ]θ only if for every S1, S2, R ⊆ S1 × S2, (g S1) is related to (g S2) by the “propagation” of R according to τ.

7

slide-34
SLIDE 34

Polymorphic Lambda Calculus [Girard 1972, Reynolds 1974]

Types: τ := α | τ → τ | ∀α. τ Terms: t := x | λx : τ. t | t t | Λα. t | t τ

8

slide-35
SLIDE 35

Polymorphic Lambda Calculus [Girard 1972, Reynolds 1974]

Types: τ := α | τ → τ | ∀α. τ Terms: t := x | λx : τ. t | t t | Λα. t | t τ Γ, x : τ ⊢ x : τ

8

slide-36
SLIDE 36

Polymorphic Lambda Calculus [Girard 1972, Reynolds 1974]

Types: τ := α | τ → τ | ∀α. τ Terms: t := x | λx : τ. t | t t | Λα. t | t τ Γ, x : τ ⊢ x : τ Γ, x : τ1 ⊢ t : τ2 Γ ⊢ (λx : τ1. t) : τ1 → τ2

8

slide-37
SLIDE 37

Polymorphic Lambda Calculus [Girard 1972, Reynolds 1974]

Types: τ := α | τ → τ | ∀α. τ Terms: t := x | λx : τ. t | t t | Λα. t | t τ Γ, x : τ ⊢ x : τ Γ, x : τ1 ⊢ t : τ2 Γ ⊢ (λx : τ1. t) : τ1 → τ2 Γ ⊢ t : τ1 → τ2 Γ ⊢ u : τ1 Γ ⊢ (t u) : τ2

8

slide-38
SLIDE 38

Polymorphic Lambda Calculus [Girard 1972, Reynolds 1974]

Types: τ := α | τ → τ | ∀α. τ Terms: t := x | λx : τ. t | t t | Λα. t | t τ Γ, x : τ ⊢ x : τ Γ, x : τ1 ⊢ t : τ2 Γ ⊢ (λx : τ1. t) : τ1 → τ2 Γ ⊢ t : τ1 → τ2 Γ ⊢ u : τ1 Γ ⊢ (t u) : τ2 α, Γ ⊢ t : τ Γ ⊢ (Λα. t) : ∀α. τ

8

slide-39
SLIDE 39

Polymorphic Lambda Calculus [Girard 1972, Reynolds 1974]

Types: τ := α | τ → τ | ∀α. τ Terms: t := x | λx : τ. t | t t | Λα. t | t τ Γ, x : τ ⊢ x : τ Γ, x : τ1 ⊢ t : τ2 Γ ⊢ (λx : τ1. t) : τ1 → τ2 Γ ⊢ t : τ1 → τ2 Γ ⊢ u : τ1 Γ ⊢ (t u) : τ2 α, Γ ⊢ t : τ Γ ⊢ (Λα. t) : ∀α. τ Γ ⊢ t : ∀α. τ Γ ⊢ (t τ ′) : τ[τ ′/α]

8

slide-40
SLIDE 40

Polymorphic Lambda Calculus [Girard 1972, Reynolds 1974]

Types: τ := α | τ → τ | ∀α. τ Terms: t := x | λx : τ. t | t t | Λα. t | t τ Γ, x : τ ⊢ x : τ [ [x] ]θ,σ = σ(x) Γ, x : τ1 ⊢ t : τ2 Γ ⊢ (λx : τ1. t) : τ1 → τ2 [ [λx : τ1. t] ]θ,σ a = [ [t] ]θ,σ[x→a] Γ ⊢ t : τ1 → τ2 Γ ⊢ u : τ1 Γ ⊢ (t u) : τ2 [ [t u] ]θ,σ = [ [t] ]θ,σ [ [u] ]θ,σ α, Γ ⊢ t : τ Γ ⊢ (Λα. t) : ∀α. τ [ [Λα. t] ]θ,σ S = [ [t] ]θ[α→S],σ Γ ⊢ t : ∀α. τ Γ ⊢ (t τ ′) : τ[τ ′/α] [ [t τ ′] ]θ,σ = [ [t] ]θ,σ [ [τ ′] ]θ

8

slide-41
SLIDE 41

Parametricity Theorem [Reynolds 1983, Wadler 1989]

Given τ and environments θ1, θ2, ρ with ρ(α) ⊆ θ1(α) × θ2(α), define ∆τ,ρ ⊆ [ [τ] ]θ1 × [ [τ] ]θ2 as follows:

9

slide-42
SLIDE 42

Parametricity Theorem [Reynolds 1983, Wadler 1989]

Given τ and environments θ1, θ2, ρ with ρ(α) ⊆ θ1(α) × θ2(α), define ∆τ,ρ ⊆ [ [τ] ]θ1 × [ [τ] ]θ2 as follows: ∆α,ρ = ρ(α)

9

slide-43
SLIDE 43

Parametricity Theorem [Reynolds 1983, Wadler 1989]

Given τ and environments θ1, θ2, ρ with ρ(α) ⊆ θ1(α) × θ2(α), define ∆τ,ρ ⊆ [ [τ] ]θ1 × [ [τ] ]θ2 as follows: ∆α,ρ = ρ(α) ∆τ1→τ2,ρ = {(f1, f2) | ∀(a1, a2) ∈ ∆τ1,ρ. (f1 a1, f2 a2) ∈ ∆τ2,ρ}

9

slide-44
SLIDE 44

Parametricity Theorem [Reynolds 1983, Wadler 1989]

Given τ and environments θ1, θ2, ρ with ρ(α) ⊆ θ1(α) × θ2(α), define ∆τ,ρ ⊆ [ [τ] ]θ1 × [ [τ] ]θ2 as follows: ∆α,ρ = ρ(α) ∆τ1→τ2,ρ = {(f1, f2) | ∀(a1, a2) ∈ ∆τ1,ρ. (f1 a1, f2 a2) ∈ ∆τ2,ρ} ∆∀α. τ,ρ = {(g1, g2) | ∀R ⊆ S1 × S2. (g1 S1, g2 S2) ∈ ∆τ,ρ[α→R]}

9

slide-45
SLIDE 45

Parametricity Theorem [Reynolds 1983, Wadler 1989]

Given τ and environments θ1, θ2, ρ with ρ(α) ⊆ θ1(α) × θ2(α), define ∆τ,ρ ⊆ [ [τ] ]θ1 × [ [τ] ]θ2 as follows: ∆α,ρ = ρ(α) ∆τ1→τ2,ρ = {(f1, f2) | ∀(a1, a2) ∈ ∆τ1,ρ. (f1 a1, f2 a2) ∈ ∆τ2,ρ} ∆∀α. τ,ρ = {(g1, g2) | ∀R ⊆ S1 × S2. (g1 S1, g2 S2) ∈ ∆τ,ρ[α→R]} Then, for every closed term t of closed type τ: ([ [t] ]∅,∅, [ [t] ]∅,∅) ∈ ∆τ,∅.

9

slide-46
SLIDE 46

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations.

10

slide-47
SLIDE 47

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate.

10

slide-48
SLIDE 48

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: Γ, x : τ1 ⊢ t : τ2 Γ ⊢ (λx : τ1. t) : τ1 → τ2

10

slide-49
SLIDE 49

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: Γ, x : τ1 ⊢ t : τ2 ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ

10

slide-50
SLIDE 50

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: ∀(a1, a2) ∈ ∆τ1,ρ. ([ [t] ]θ1,σ1[x→a1], [ [t] ]θ2,σ2[x→a2]) ∈ ∆τ2,ρ ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ

10

slide-51
SLIDE 51

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: ∀(a1, a2) ∈ ∆τ1,ρ. ([ [t] ]θ1,σ1[x→a1], [ [t] ]θ2,σ2[x→a2]) ∈ ∆τ2,ρ ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ Γ ⊢ t : τ1 → τ2 Γ ⊢ u : τ1 Γ ⊢ (t u) : τ2

10

slide-52
SLIDE 52

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: ∀(a1, a2) ∈ ∆τ1,ρ. ([ [t] ]θ1,σ1[x→a1], [ [t] ]θ2,σ2[x→a2]) ∈ ∆τ2,ρ ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ Γ ⊢ t : τ1 → τ2 Γ ⊢ u : τ1 ([ [t u] ]θ1,σ1, [ [t u] ]θ2,σ2) ∈ ∆τ2,ρ

10

slide-53
SLIDE 53

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: ∀(a1, a2) ∈ ∆τ1,ρ. ([ [t] ]θ1,σ1[x→a1], [ [t] ]θ2,σ2[x→a2]) ∈ ∆τ2,ρ ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [u] ]θ1,σ1, [ [u] ]θ2,σ2) ∈ ∆τ1,ρ ([ [t u] ]θ1,σ1, [ [t u] ]θ2,σ2) ∈ ∆τ2,ρ

10

slide-54
SLIDE 54

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: ∀(a1, a2) ∈ ∆τ1,ρ. ([ [t] ]θ1,σ1[x→a1], [ [t] ]θ2,σ2[x→a2]) ∈ ∆τ2,ρ ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [u] ]θ1,σ1, [ [u] ]θ2,σ2) ∈ ∆τ1,ρ ([ [t u] ]θ1,σ1, [ [t u] ]θ2,σ2) ∈ ∆τ2,ρ α, Γ ⊢ t : τ Γ ⊢ (Λα. t) : ∀α. τ

10

slide-55
SLIDE 55

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: ∀(a1, a2) ∈ ∆τ1,ρ. ([ [t] ]θ1,σ1[x→a1], [ [t] ]θ2,σ2[x→a2]) ∈ ∆τ2,ρ ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [u] ]θ1,σ1, [ [u] ]θ2,σ2) ∈ ∆τ1,ρ ([ [t u] ]θ1,σ1, [ [t u] ]θ2,σ2) ∈ ∆τ2,ρ α, Γ ⊢ t : τ ([ [Λα. t] ]θ1,σ1, [ [Λα. t] ]θ2,σ2) ∈ ∆∀α. τ,ρ

10

slide-56
SLIDE 56

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: ∀(a1, a2) ∈ ∆τ1,ρ. ([ [t] ]θ1,σ1[x→a1], [ [t] ]θ2,σ2[x→a2]) ∈ ∆τ2,ρ ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [u] ]θ1,σ1, [ [u] ]θ2,σ2) ∈ ∆τ1,ρ ([ [t u] ]θ1,σ1, [ [t u] ]θ2,σ2) ∈ ∆τ2,ρ ∀R ⊆ S1 × S2. ([ [t] ]θ1[α→S1],σ1, [ [t] ]θ2[α→S2],σ2) ∈ ∆τ,ρ[α→R] ([ [Λα. t] ]θ1,σ1, [ [Λα. t] ]θ2,σ2) ∈ ∆∀α. τ,ρ

10

slide-57
SLIDE 57

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: ∀(a1, a2) ∈ ∆τ1,ρ. ([ [t] ]θ1,σ1[x→a1], [ [t] ]θ2,σ2[x→a2]) ∈ ∆τ2,ρ ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [u] ]θ1,σ1, [ [u] ]θ2,σ2) ∈ ∆τ1,ρ ([ [t u] ]θ1,σ1, [ [t u] ]θ2,σ2) ∈ ∆τ2,ρ ∀R ⊆ S1 × S2. ([ [t] ]θ1[α→S1],σ1, [ [t] ]θ2[α→S2],σ2) ∈ ∆τ,ρ[α→R] ([ [Λα. t] ]θ1,σ1, [ [Λα. t] ]θ2,σ2) ∈ ∆∀α. τ,ρ Γ ⊢ t : ∀α. τ Γ ⊢ (t τ ′) : τ[τ ′/α]

10

slide-58
SLIDE 58

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: ∀(a1, a2) ∈ ∆τ1,ρ. ([ [t] ]θ1,σ1[x→a1], [ [t] ]θ2,σ2[x→a2]) ∈ ∆τ2,ρ ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [u] ]θ1,σ1, [ [u] ]θ2,σ2) ∈ ∆τ1,ρ ([ [t u] ]θ1,σ1, [ [t u] ]θ2,σ2) ∈ ∆τ2,ρ ∀R ⊆ S1 × S2. ([ [t] ]θ1[α→S1],σ1, [ [t] ]θ2[α→S2],σ2) ∈ ∆τ,ρ[α→R] ([ [Λα. t] ]θ1,σ1, [ [Λα. t] ]θ2,σ2) ∈ ∆∀α. τ,ρ Γ ⊢ t : ∀α. τ ([ [t τ ′] ]θ1,σ1, [ [t τ ′] ]θ2,σ2) ∈ ∆τ[τ ′/α],ρ

10

slide-59
SLIDE 59

Proof Sketch

Prove the following more general statement: Γ ⊢ t : τ implies ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ,ρ , provided (σ1(x), σ2(x)) ∈ ∆τ ′,ρ for every x : τ ′ in Γ by induction on the structure of typing derivations. The base case is immediate. In the step cases: ∀(a1, a2) ∈ ∆τ1,ρ. ([ [t] ]θ1,σ1[x→a1], [ [t] ]θ2,σ2[x→a2]) ∈ ∆τ2,ρ ([ [λx : τ1. t] ]θ1,σ1, [ [λx : τ1. t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆τ1→τ2,ρ ([ [u] ]θ1,σ1, [ [u] ]θ2,σ2) ∈ ∆τ1,ρ ([ [t u] ]θ1,σ1, [ [t u] ]θ2,σ2) ∈ ∆τ2,ρ ∀R ⊆ S1 × S2. ([ [t] ]θ1[α→S1],σ1, [ [t] ]θ2[α→S2],σ2) ∈ ∆τ,ρ[α→R] ([ [Λα. t] ]θ1,σ1, [ [Λα. t] ]θ2,σ2) ∈ ∆∀α. τ,ρ ([ [t] ]θ1,σ1, [ [t] ]θ2,σ2) ∈ ∆∀α. τ,ρ ([ [t τ ′] ]θ1,σ1, [ [t τ ′] ]θ2,σ2) ∈ ∆τ[τ ′/α],ρ

10

slide-60
SLIDE 60

Adding Datatypes

Types: τ := · · · | Bool | [τ] Terms: t := · · · | True | False | []τ | t : t | case t of {· · · }

11

slide-61
SLIDE 61

Adding Datatypes

Types: τ := · · · | Bool | [τ] Terms: t := · · · | True | False | []τ | t : t | case t of {· · · } Γ ⊢ True : Bool , Γ ⊢ False : Bool , Γ ⊢ []τ : [τ] Γ ⊢ t : τ Γ ⊢ u : [τ] Γ ⊢ (t : u) : [τ] Γ ⊢ t : Bool Γ ⊢ u : τ Γ ⊢ v : τ Γ ⊢ (case t of {True → u ; False → v}) : τ Γ ⊢ t : [τ ′] Γ ⊢ u : τ Γ, x1 : τ ′, x2 : [τ ′] ⊢ v : τ Γ ⊢ (case t of {[] → u ; (x1 : x2) → v}) : τ

11

slide-62
SLIDE 62

Adding Datatypes

Types: τ := · · · | Bool | [τ] Terms: t := · · · | True | False | []τ | t : t | case t of {· · · } Γ ⊢ True : Bool , Γ ⊢ False : Bool , Γ ⊢ []τ : [τ] Γ ⊢ t : τ Γ ⊢ u : [τ] Γ ⊢ (t : u) : [τ] Γ ⊢ t : Bool Γ ⊢ u : τ Γ ⊢ v : τ Γ ⊢ (case t of {True → u ; False → v}) : τ Γ ⊢ t : [τ ′] Γ ⊢ u : τ Γ, x1 : τ ′, x2 : [τ ′] ⊢ v : τ Γ ⊢ (case t of {[] → u ; (x1 : x2) → v}) : τ With the straightforward extension of term-semantics and with ∆Bool,ρ = {(True, True), (False, False)} ∆[τ],ρ = {([x1, . . . , xn], [y1, . . . , yn]) | n ≥ 0, (xi, yi) ∈ ∆τ,ρ} , the parametricity theorem still holds.

11

slide-63
SLIDE 63

Adding General Recursion

Terms: t := · · · | fix t

12

slide-64
SLIDE 64

Adding General Recursion

Terms: t := · · · | fix t Γ ⊢ t : τ → τ Γ ⊢ (fix t) : τ

12

slide-65
SLIDE 65

Adding General Recursion

Terms: t := · · · | fix t Γ ⊢ t : τ → τ Γ ⊢ (fix t) : τ To provide semantics, types are interpreted as pointed complete partial orders now. [ [fix t] ]θ,σ =

  • i≥0

([ [t] ]i

θ,σ ⊥).

12

slide-66
SLIDE 66

Adding General Recursion

Terms: t := · · · | fix t Γ ⊢ t : τ → τ Γ ⊢ (fix t) : τ To provide semantics, types are interpreted as pointed complete partial orders now. [ [fix t] ]θ,σ =

  • i≥0

([ [t] ]i

θ,σ ⊥).

The parametricity theorem still holds, provided all relations are strict and continuous.

12

slide-67
SLIDE 67

Back to Haskell

The original example filter :: ∀α. (α → Bool) → [α] → [α] filter p [] = [] filter p (x : xs) = if p x then x : filter p xs else filter p xs has a “desugaring” in the extended calculus as follows: fix (λf : (∀α. (α → Bool) → [α] → [α]). Λα. λp : (α → Bool). λl : [α]. case l of {[] → []α ; (x : xs) → case p x of {True → x : (f α p xs) ; False → f α p xs}})

13

slide-68
SLIDE 68

The Magic Dissolves

Given g of type ∀α. (α → Bool) → [α] → [α], by the parametricity theorem: (g, g) ∈ ∆∀α. (α→Bool)→[α]→[α],∅

14

slide-69
SLIDE 69

The Magic Dissolves

Given g of type ∀α. (α → Bool) → [α] → [α], by the parametricity theorem: (g, g) ∈ ∆∀α. (α→Bool)→[α]→[α],∅ ⇒ ∀R ∈ Rel. (g, g) ∈ ∆(α→Bool)→[α]→[α],[α→R] by definition of ∆

14

slide-70
SLIDE 70

The Magic Dissolves

Given g of type ∀α. (α → Bool) → [α] → [α], by the parametricity theorem: (g, g) ∈ ∆∀α. (α→Bool)→[α]→[α],∅ ⇒ ∀R ∈ Rel. (g, g) ∈ ∆(α→Bool)→[α]→[α],[α→R] ⇒ ∀R ∈ Rel, (a1, a2) ∈ ∆α→Bool,[α→R]. (g a1, g a2) ∈ ∆[α]→[α],[α→R] by definition of ∆

14

slide-71
SLIDE 71

The Magic Dissolves

Given g of type ∀α. (α → Bool) → [α] → [α], by the parametricity theorem: (g, g) ∈ ∆∀α. (α→Bool)→[α]→[α],∅ ⇒ ∀R ∈ Rel. (g, g) ∈ ∆(α→Bool)→[α]→[α],[α→R] ⇒ ∀R ∈ Rel, (a1, a2) ∈ ∆α→Bool,[α→R]. (g a1, g a2) ∈ ∆[α]→[α],[α→R] ⇒ ∀R ∈ Rel, (a1, a2) ∈ ∆α→Bool,[α→R], (l1, l2) ∈ ∆[α],[α→R]. (g a1 l1, g a2 l2) ∈ ∆[α],[α→R] by definition of ∆

14

slide-72
SLIDE 72

The Magic Dissolves

Given g of type ∀α. (α → Bool) → [α] → [α], by the parametricity theorem: (g, g) ∈ ∆∀α. (α→Bool)→[α]→[α],∅ ⇒ ∀R ∈ Rel. (g, g) ∈ ∆(α→Bool)→[α]→[α],[α→R] ⇒ ∀R ∈ Rel, (a1, a2) ∈ ∆α→Bool,[α→R]. (g a1, g a2) ∈ ∆[α]→[α],[α→R] ⇒ ∀R ∈ Rel, (a1, a2) ∈ ∆α→Bool,[α→R], (l1, l2) ∈ ∆[α],[α→R]. (g a1 l1, g a2 l2) ∈ ∆[α],[α→R] ⇒ ∀(a1, a2) ∈ ∆α→Bool,[α→h], (l1, l2) ∈ (map h). (g a1 l1, g a2 l2) ∈ (map h) by instantiating R = h and realizing that ∆[α],[α→h] = map h for every function h

14

slide-73
SLIDE 73

The Magic Dissolves

Given g of type ∀α. (α → Bool) → [α] → [α], by the parametricity theorem: (g, g) ∈ ∆∀α. (α→Bool)→[α]→[α],∅ ⇒ ∀R ∈ Rel. (g, g) ∈ ∆(α→Bool)→[α]→[α],[α→R] ⇒ ∀R ∈ Rel, (a1, a2) ∈ ∆α→Bool,[α→R]. (g a1, g a2) ∈ ∆[α]→[α],[α→R] ⇒ ∀R ∈ Rel, (a1, a2) ∈ ∆α→Bool,[α→R], (l1, l2) ∈ ∆[α],[α→R]. (g a1 l1, g a2 l2) ∈ ∆[α],[α→R] ⇒ ∀(a1, a2) ∈ ∆α→Bool,[α→h], (l1, l2) ∈ (map h). (g a1 l1, g a2 l2) ∈ (map h) ⇒ ∀(l1, l2) ∈ (map h). (g (p ◦ h) l1, g p l2) ∈ (map h) by instantiating (a1, a2) = (p ◦ h, p) ∈ ∆α→Bool,[α→h] for every function h and every p.

14

slide-74
SLIDE 74

The Magic Dissolves

Given g of type ∀α. (α → Bool) → [α] → [α], by the parametricity theorem: (g, g) ∈ ∆∀α. (α→Bool)→[α]→[α],∅ ⇒ ∀R ∈ Rel. (g, g) ∈ ∆(α→Bool)→[α]→[α],[α→R] ⇒ ∀R ∈ Rel, (a1, a2) ∈ ∆α→Bool,[α→R]. (g a1, g a2) ∈ ∆[α]→[α],[α→R] ⇒ ∀R ∈ Rel, (a1, a2) ∈ ∆α→Bool,[α→R], (l1, l2) ∈ ∆[α],[α→R]. (g a1 l1, g a2 l2) ∈ ∆[α],[α→R] ⇒ ∀(a1, a2) ∈ ∆α→Bool,[α→h], (l1, l2) ∈ (map h). (g a1 l1, g a2 l2) ∈ (map h) ⇒ ∀(l1, l2) ∈ (map h). (g (p ◦ h) l1, g p l2) ∈ (map h) for every function h and every p. This is exactly the claim (1) for g = filter!

14

slide-75
SLIDE 75

References

J.-Y. Girard. Interpr´ etation functionelle et ´ elimination des coupures dans l’arithm´ etique d’ordre sup´ erieure. PhD thesis, Universit´ e Paris VII, 1972. J.C. Reynolds. Towards a theory of type structure. In Colloque sur la Programmation, Proceedings, pages 408–423. Springer-Verlag, 1974. J.C. Reynolds. Types, abstraction and parametric polymorphism. In Information Processing, Proceedings, pages 513–523. Elsevier Science Publishers B.V., 1983.

  • P. Wadler.

Theorems for free! In Functional Programming Languages and Computer Architecture, Proceedings, pages 347–359. ACM Press, 1989.

15