Functions vs. inference rules

Typage
Master 2: Languages et Programmation
Lecture 7
Delia Kesner, Giovanni Bernardi
IRIF
1
Plan
1.
2.
3.
4.
5.
Questions
History
Recap
Proof theoretic approach
Examples examples examples
set-theoretic approach
2
Questions questions questions . . .
1. What is a complete lattice?
3
Questions questions questions . . .
1. What is a complete lattice?
2. What is a complete partial order (CPO) ?
4
Questions questions questions . . .
1. What is a complete lattice?
2. What is a complete partial order (CPO) ?
3. What does the Knaster-Tarski theorem state ?
5
Questions questions questions . . .
1. What is a complete lattice?
2. What is a complete partial order (CPO) ?
3. What does the Knaster-Tarski theorem state ?
4. When are two trees t and s equal ?
6
1908, Russell
These fallacies [. . . ] are to be avoided by what
may be called the “vicious-circle principle;”
i.e., [. . . ] whatever contains an apparent
variable must be of a different type from the
possile values of that variable [. . . ] This is the
guiding principle in what follows.
1968, Morris
This construction is shown to be lacking [. . . ]
the type system makes the λ-calculus an
uninteresting programming language; i.e. one
without non-terminating computations.
7
1908, Russell
1996
These fallacies [. . . ] are to be avoided by what
may be called the “vicious-circle principle;”
i.e., [. . . ] whatever contains an apparent
variable must be of a different type from the
possile values of that variable [. . . ] This is the
guiding principle in what follows.
1968, Morris
This construction is shown to be lacking [. . . ]
the type system makes the λ-calculus an
uninteresting programming language; i.e. one
without non-terminating computations.
8
λ-calculus
typing rules from [Cardone and Coppo, 1991]
M, N ::= x | c | MN | λx.M
An equi-recursive system
Γ, x : A ` x : A
Γ, c : A ` c : Ac (c)
Γ, x : A ` M : B
Γ ` λx.M : A → B
Γ`M:A→B Γ`N:A
Γ ` MN : B
Γ`M:B
A≈B
Γ`M:A
Powerfull type system, for istance we can type Y
9
Equivalence for µ-types
A ::= T | x | µx.A | A × A | A → A
I
I
µx.T binds x in T , free and bound variables as expected
µ-types are closed and contractive terms
when are two types equal ?
?
µy .y
= µx.z
µy .y
= µx.x
?
?
µx.(int × x) = int × µx.(int × x)
µx.x → x
?
= (µx.x → x) → (µx.x → x)
10
Type equivalence
syntactic approach
A ::= T | x | µx.A | A × A | A → A
F
:
parts(Types2µ ) → parts(Types2µ )
∆
F (R) = { (c, c) | c ∈ T }
∪ { (A1 × A2 , B1 × B2 ) |
∀i ∈ {1, 2}. Ai
R Bi }
∪ { (A1 → A2 , B1 → B2 ) | B1 R A1 , A2 R B2 }
∪ { (A, µx.B) | A R B{x/B} }
∪ { (µx.A, B) | A{x/A} R B }
I
hparts(Types2µ ), ⊆i complete lattice, F monotone
νF exists
by Knaster-Tarski
I
Let
I
∆
≤sbt = νF ,
∆
≈ = ≤sbt ∩ ≤−1
sbt
11
Type equivalence
syntactic approach
your reaction last week
A ::= T | x | µx.A | A × A | A → A
F
:
parts(Types2µ ) → parts(Types2µ )
∆
F (R) = { (c, c) | c ∈ T }
∪ { (A1 × A2 , B1 × B2 ) |
∀i ∈ {1, 2}. Ai
R Bi }
∪ { (A1 → A2 , B1 → B2 ) | B1 R A1 , A2 R B2 }
∪ { (A, µx.B) | A R B{x/B} }
∪ { (µx.A, B) | A{x/A} R B }
I
hparts(Types2µ ), ⊆i complete lattice, F monotone
νF exists
by Knaster-Tarski
I
Let
I
∆
≤sbt = νF ,
∆
≈ = ≤sbt ∩ ≤−1
sbt
12
Let’s change perspective
inference rule
premise1 . . . premisen
conclusion
side condition
example
Γ`M :B
Γ`M :A A≈B
13
Back to basics
A, B ::= int | real | A → A
ground subtyping relation
int ≤g int
real ≤g real
int ≤g real
How to define subtyping ≤sbt on types A, B, . . .?
14
Back to basics
A, B ::= int | real | A → A
ground subtyping relation
int ≤g int
real ≤g real
int ≤g real
How to define subtyping ≤sbt on types A, B, . . .? Induction
inference rules
c1 ≤sbt c2
c1 ≤g c2
B1 ≤sbt A1 A2 ≤sbt B2
A1 → A2 ≤sbt B1 → B2
≤sbt contains all pairs (A, B) s.t.
I
I
towards set theory
we can derive A ≤sbt B,
with a finite derivation tree
15
Back to basics
A A,
derivation
depth| 2A(i.e.
finite)
B ::= tree
int of
| real
→A
ground subtyping relation
int ≤sbtintreal
int ≤gint
int≤sbt real
real ≤g real
≤g real
real → int ≤sbt int → real
How to define subtyping ≤sbt on types A, B, . . .? Induction
inference rules
c1 ≤sbt c2
c1 ≤g c2
B1 ≤sbt A1 A2 ≤sbt B2
A1 → A2 ≤sbt B1 → B2
≤sbt contains all pairs (A, B) s.t.
I
I
towards set theory
we can derive A ≤sbt B,
with a finite derivation tree
16
Back to basics
A A,
derivation
depth| 2A(i.e.
finite)
B ::= tree
int of
| real
→A
ground subtyping relation
int ≤sbtintreal
int ≤gint
int≤sbt real
real ≤g real
≤g real
real → int ≤sbt int → real
How to define subtyping ≤sbt on types A, B, . . .? Induction
inference rules
c1 ≤sbt c2
c1 ≤g c2
B1 ≤sbt A1 A2 ≤sbt B2
A1 → A2 ≤sbt B1 → B2
≤sbt contains all pairs (A, B) s.t.
towards set theory
How
to
express
this
using
sets/functions
?
I we can derive A ≤sbt B,
I
with a finite derivation tree
17
From rules to functions
inference rules
c1 ≤sbt c2
c1 ≤g c2
B1 ≤sbt A1 A2 ≤sbt B2
A1 → A2 ≤sbt B1 → B2
What do the rules mean?
18
From rules to functions
inference rules
(c1 , c2 )
c1 ≤g c2
(B1 , A1 ) (A2 , B2 )
(A1 → A2 , B1 → B2 )
What do the rules mean?
19
From rules to functions
inference rules
(c1 , c2 )
c1 ≤g c2
(B1 , A1 ) (A2 , B2 )
(A1 → A2 , B1 → B2 )
What do the rules mean?
F
:
parts(Types2 ) → parts(Types2 )
∆
F (R) = { (c1 , c2 ) | c1 ≤g c2 }
∪ { (A1 → A2 , B1 → B2 ) | B1 R A1 , A2 R B2 }
20
From rules to functions
inference rules
(c1 , c2 )
c1 ≤g c2
(B1 , A1 ) (A2 , B2 )
(A1 → A2 , B1 → B2 )
What do the rules mean?
F
:
parts(Types2 ) → parts(Types2 )
∆
F (R) = {(int, int)}
∪ {(real, real)}
∪ {(int, real)}
∪ { (A1 → A2 , B1 → B2 ) | B1 R A1 , A2 R B2 }
21
From derivation trees to function application
∆
F (R) = {(int, int), (real, real), (int, real)}
∪ { (A1 → A2 , B1 → B2 ) | B1 R A1 , A2 R B2 }
Let’s use F ,
F 0 (∅) = ∅
by convention
F 1 (∅) =
F 2 (∅) =
22
From derivation trees to function application
∆
F (R) = {(int, int), (real, real), (int, real)}
∪ { (A1 → A2 , B1 → B2 ) | B1 R A1 , A2 R B2 }
Let’s use F ,
F 0 (∅) = ∅
by convention
F 1 (∅) = {(int, int), (real, real), (int, real)} = ≤g
F 2 (∅) =
23
From derivation trees to function application
∆
F (R) = {(int, int), (real, real), (int, real)}
∪ { (A1 → A2 , B1 → B2 ) | B1 R A1 , A2 R B2 }
Let’s use F ,
F 0 (∅) = ∅
by convention
F 1 (∅) = {(int, int), (real, real), (int, real)} = ≤g
F 2 (∅) = {(real → int, int → real), (int → int, int → int), . . .}
∪ ≤g
..
..
.
.
24
From derivation trees to function application
A derivation tree of depth 2
∆
F (R) = {(int, int), (real, real), (int, real)}
∪ { (A
A2 , B1 →(int,
B2 )real)
| B1 R A1 , A2 R B2 }
1 →
(int,
real)
(real → int, int → real)
Let’s use F ,
F 0 (∅) = ∅
by convention
F 1 (∅) = {(int, int), (real, real), (int, real)} = ≤g
F 2 (∅) = {(real → int, int → real), (int → int, int → int), . . .}
∪ ≤g
..
..
.
.
25
From derivation trees to function application
Lemma
..
.
A derivation tree (A, B) has depth n iff (A, B) ∈ F n (∅).
≤sbt contains all pairs (A, B) s.t.
I
I
we can derive A ≤sbt B,
with a finite derivation tree
∆
Equivalent definition: ≤sbt =
S∞
n=0 F
n
(∅)
but then . . .
26
From derivation trees to function application
Lemma
..
.
A derivation tree (A, B) has depth n iff (A, B) ∈ F n (∅).
≤sbt contains all pairs (A, B) s.t.
I
I
we can derive A ≤sbt B,
with a finite derivation tree
∆
Equivalent definition: ≤sbt =
but then . . . Kleene
S∞
n=0 F
n
(∅)
fixed point theorem ensures
≤sbt = µF
27
Type equivalence
syntactic approach
A ::= int | real | x | µx.A | A → A
F
:
parts(Types2µ ) → parts(Types2µ )
∆
F (R) = ≤g
∪ { (A1 → A2 , B1 → B2 ) | B1 R A1 , A2 R B2 }
∪ { (A, µx.B) | A R B{x/B} }
∪ { (µx.A, B) | A{x/A} R B }
I
hparts(Types2µ ), ⊆i complete lattice, F monotone
νF exists
by Knaster-Tarski
I
Let
I
∆
≤sbt = νF ,
∆
≈ = ≤sbt ∩ ≤−1
sbt
28
Type equivalence
syntactic approach
A ::= int | real | x | µx.A | A → A
inference rules
c1 ≤sbt c2
c1 ≤g c2
B1 ≤sbt A1 A2 ≤sbt B2
A1 → A2 ≤sbt B1 → B2
A ≤sbt B{x/B}
A ≤sbt µx.B
A{x/A} ≤sbt B
µx.A ≤sbt B
≤sbt contains all pairs (A, B) s.t.
I
I
we can derive A ≤sbt B
with a finite or a circular derivation tree
29
Type equivalence
syntactic approach
A ::= int | real | x | µx.A | A → A
inference rules
c1 ≤sbt c2
c1 ≤g c2
B1 ≤sbt A1 A2 ≤sbt B2
A1 → A2 ≤sbt B1 → B2
A ≤sbt B{x/B}
A ≤sbt µx.B
A{x/A} ≤sbt B
µx.A ≤sbt B
≤sbt contains all pairs (A, B) s.t.
I
I
we can derive A ≤sbt B
with a finite or a circular derivation tree
?? circular ??
30
Material
I
Chapter 3 “Introduction to Bisimulation and Coinduction”
I
Chapter 21 “Types and Programming Languages”
31
Pour le TP
Implement
I decision procedure for =α over Types
µ
I decision procedure for ≈
I Project: type inference for recursive types have fun!
32