slides

Transcription

slides
A Polymorphic Type System
for the λ-calculus with Constructors
Barbara Petit
LIP - ENS Lyon
15 mai 2009
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
1 / 20
Introduction
The λC -calculus of Arbiser, Miquel & Rı̀os (2006) :
Encompasses ML-style pattern matching
Variadic constructors
Separation property
But unconventional operational semantics, not obviously
corresponding to any kind of cut elimination.
Challenge :
Type it.
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
2 / 20
Introduction
The λC -calculus of Arbiser, Miquel & Rı̀os (2006) :
Encompasses ML-style pattern matching
Variadic constructors
Separation property
But unconventional operational semantics, not obviously
corresponding to any kind of cut elimination.
Challenge :
Type it.
Related Works :
ρ-calculus (Cristea, Kirchner, Liquori)
Pure Pattern Calculus (Jay, Kesner)
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
2 / 20
1
The λ-calculus with constructors
2
Type system
3
Realisability semantic
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
3 / 20
λC -terms.
λ-calculus :
Constructors :
x | λx.t | tu
c, c1 , c2 . . ., true, 0, succ, none...
... constants with no fixed arity.
Case bindings : θ = {c1 7→ u1 ; . . . ; cn 7→ un }
Case constructs :
{|θ|} · t
Barbara Petit (LIP - ENS Lyon)
(ci 6= cj for i 6= j)
A Polymorphic Type System for the λ-calculus with Constructors
4 / 20
λC -terms.
λ-calculus :
Constructors :
x | λx.t | tu
c, c1 , c2 . . ., true, 0, succ, none...
... constants with no fixed arity.
Case bindings : θ = {c1 7→ u1 ; . . . ; cn 7→ un }
Case constructs :
{|θ|} · t
(ci 6= cj for i 6= j)
Case binding + Constructor = Pattern matching
If := λbxy .{|true 7→ x; false 7→ y |} · b
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
4 / 20
Commutation Case/application
{|θ|}.(tu) → ({|θ|}.t)u
pred := λx.{|0 7→ 0; succ 7→ λy .y |} · x
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
5 / 20
Commutation Case/application
{|θ|}.(tu) → ({|θ|}.t)u
pred := λx.{|0 7→ 0; succ 7→ λy .y |} · x
pred(succ p)→{|0 7→ 0; succ 7→ λy .y |} · (succ p)
Barbara Petit (LIP - ENS Lyon)
AppLam
A Polymorphic Type System for the λ-calculus with Constructors
5 / 20
Commutation Case/application
{|θ|}.(tu) → ({|θ|}.t)u
pred := λx.{|0 7→ 0; succ 7→ λy .y |} · x
pred(succ p)→{|0 7→ 0; succ 7→ λy .y |} · (succ p)
→({|0 7→ 0; succ 7→ λy .y |} · succ) p
Barbara Petit (LIP - ENS Lyon)
AppLam
CaseApp
A Polymorphic Type System for the λ-calculus with Constructors
5 / 20
Commutation Case/application
{|θ|}.(tu) → ({|θ|}.t)u
pred := λx.{|0 7→ 0; succ 7→ λy .y |} · x
pred(succ p)→{|0 7→ 0; succ 7→ λy .y |} · (succ p)
AppLam
→({|0 7→ 0; succ 7→ λy .y |} · succ) p
CaseApp
→(λy .y ) p
CaseCons
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
5 / 20
Commutation Case/application
{|θ|}.(tu) → ({|θ|}.t)u
pred := λx.{|0 7→ 0; succ 7→ λy .y |} · x
pred(succ p)→{|0 7→ 0; succ 7→ λy .y |} · (succ p)
AppLam
→({|0 7→ 0; succ 7→ λy .y |} · succ) p
CaseApp
→(λy .y ) p
CaseCons
→p
AppLam
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
5 / 20
Reduction rules
AppLam
LamApp
CaseCons
Barbara Petit (LIP - ENS Lyon)
(λx.t)u → t{x ← u}
λx.tx
→ t
{|θ|} · c → t
(x ∈
/ FV(t))
((c 7→ t) ∈ θ)
A Polymorphic Type System for the λ-calculus with Constructors
6 / 20
Reduction rules
AppLam
LamApp
CaseCons
CaseApp
Barbara Petit (LIP - ENS Lyon)
(λx.t)u → t{x ← u}
λx.tx
→ t
{|θ|} · c → t
(x ∈
/ FV(t))
((c 7→ t) ∈ θ)
{|θ|} · (tu) → ({|θ|} · t)u
A Polymorphic Type System for the λ-calculus with Constructors
6 / 20
Reduction rules
(λx.t)u → t{x ← u}
AppLam
λx.tx
LamApp
→ t
{|θ|} · c → t
CaseCons
CaseApp
{|θ|} · (tu) → ({|θ|} · t)u
CaseLam
{|θ|} · λx.t → λx.{|θ|} · t
CaseCase
(x ∈
/ FV(t))
((c 7→ t) ∈ θ)
(x ∈
/ FV(θ))
for confluence
{|θ|} · ({|φ|} · t) → {|θ ◦ φ|} · t
with θ ◦ {c1 7→ t1 ; . . . ; cn 7→ tn } := c1 7→ {|θ|} · t1 ; . . . ; cn 7→ {|θ|} · tn
for separation
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
6 / 20
1
The λ-calculus with constructors
2
Type system
3
Realisability semantic
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
7 / 20
A polymorphic type system
The type system includes system F with subtyping :
T , U :=
−
(x:T )∈Γ
Γ`x :T
Γ, x : U ` t : T
Γ ` λx.t : U → T
Barbara Petit (LIP - ENS Lyon)
X | T → U | ∀X .T
Γ`t:U →T Γ`u:U
Γ ` tu : T
Γ`t:T
X ∈TV(Γ)
/
Γ ` t : ∀X .T
A Polymorphic Type System for the λ-calculus with Constructors
8 / 20
A polymorphic type system
The type system includes system F with subtyping :
T , U :=
−
(x:T )∈Γ
Γ`x :T
Γ, x : U ` t : T
Γ ` λx.t : U → T
T 4T
Γ`t:U →T Γ`u:U
Γ ` tu : T
Γ`t:T
X ∈TV(Γ)
/
Γ ` t : ∀X .T
Γ`t:U U 4T
Γ`t:T
T 4 U U 4 U0
T 4 U0
T 4U
X ∈TV(T
/
)
T 4 ∀X .U
Barbara Petit (LIP - ENS Lyon)
X | T → U | ∀X .T
U0 4 U T 4 T 0
U → T 4 U0 → T 0
−
...
∀X .T 4 T {X ← U}
A Polymorphic Type System for the λ-calculus with Constructors
8 / 20
Typing data-structures
Example : Option type.
T opt
u : T opt
⇒
Barbara Petit (LIP - ENS Lyon)
:=
none ∪ some T
(u = none) or (u = some t) with t : T
A Polymorphic Type System for the λ-calculus with Constructors
9 / 20
Typing data-structures
Example : Option type.
T opt
u : T opt
⇒
:=
none ∪ some T
(u = none) or (u = some t) with t : T
Data-types :
A type c for every constructor c
A type application for data-types :
(ti : Ti )ni=1 ⇒ ct1 . . . tn : cT1 . . . Tn
Union type
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
9 / 20
The problematic application type
A type application to type applications ?
`t: T
`u: U
` tu : TU
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
10 / 20
The problematic application type
A type application to type applications ?
`t: T
`u: U
` tu : TU
δ := λx.xx ∆ := ∀X .(X → X ) → ∀X .(X → X )
; `δ: ∆
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
10 / 20
The problematic application type
A type application to type applications ?
`t: T
`u: U
` tu : TU
δ := λx.xx ∆ := ∀X .(X → X ) → ∀X .(X → X )
; `δ: ∆
; ` δδ : ∆∆
/
Restricted application type
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
10 / 20
Syntax of Types
DataTypes :
D, E :=
c | DT
Types :
T , U :=
D |T →U
Two classes of types
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
11 / 20
Syntax of Types
DataTypes :
D, E :=
c | DT | D ∪ E | D ∩ E
Types :
T , U :=
D | T →U | T ∪U | T ∩U
Union and intersection
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
11 / 20
Syntax of Types
c | DT | D ∪ E | D ∩ E
| ∀X .D
DataTypes :
D, E :=
Types :
T , U := X | D | T → U | T ∪ U | T ∩ U
| ∀X .T
Quantification
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
11 / 20
Syntax of Types
DataTypes :
D, E := α | c | DT | D ∪ E | D ∩ E
| ∀X .D | ∀α.D
Types :
T , U := X | D | T → U | T ∪ U | T ∩ U
| ∀X .T | ∀α.T
Two spaces of type variables
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
11 / 20
Typing the case binding (I)
Example nat ≡ 0 ∪ succ nat.
θpred := { 0 7→ 0 ;
succ 7→ λy .y }
θpred : nat → nat
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
12 / 20
Typing the case binding (I)
Example nat ≡ 0 ∪ succ nat.
θpred := { 0 7→ 0 ;
succ 7→ λy .y }
θpred : nat → nat
θpred :
Barbara Petit (LIP - ENS Lyon)
0 → nat
( succ nat) → nat
A Polymorphic Type System for the λ-calculus with Constructors
12 / 20
Typing the case binding (I)
Example nat ≡ 0 ∪ succ nat.
θpred := { 0 7→ 0 ;
succ 7→ λy .y }
0 : nat
λy .y : nat → nat
θpred : nat → nat
θpred :
Barbara Petit (LIP - ENS Lyon)
0 → nat
( succ nat) → nat
A Polymorphic Type System for the λ-calculus with Constructors
12 / 20
Typing the case binding (I)
Example nat ≡ 0 ∪ succ nat.
θpred := { 0 7→ 0 ;
succ 7→ λy .y }
λy .y : nat → nat
0 : nat
θpred : nat → nat
θpred :
0 → nat
( succ nat) → nat
Barbara Petit (LIP - ENS Lyon)
,→?
θpred : 0∪(succ nat) → nat
A Polymorphic Type System for the λ-calculus with Constructors
12 / 20
Typing the case binding (II)
~ i → Ti n
Γ ` ui : U
i=1
T ~
Γ`θ:
ci Ui → Ti
i
with θ = {c1 7→ u1 , . . . , cn 7→ un }
Vectorial notation :
~ ,U
~ := ∅ | T
~ ;U
T
c∅= c
∅ → U= U
Barbara Petit (LIP - ENS Lyon)
~ ; T ) = (c T
~ )T
c(T
~ ;T) → U = T
~ → (T → U)
(T
A Polymorphic Type System for the λ-calculus with Constructors
13 / 20
Typing the case binding (II)
~ i → Ti n
Γ ` ui : U
i=1
T ~
Γ`θ:
ci Ui → Ti
i
with θ = {c1 7→ u1 , . . . , cn 7→ un }
Vectorial notation :
~ ,U
~ := ∅ | T
~ ;U
T
c∅= c
∅ → U= U
~ ; T ) = (c T
~ )T
c(T
~ ;T) → U = T
~ → (T → U)
(T
(T1 → U1 ) ∩ (T2 → U2 ) 4 T1 ∪ T2 → U1 ∪ U2
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
13 / 20
Typing the case construct
~ →T
Γ`t:U
Γ ` θ : T → T0
~ → T0
Γ ` {|θ|} · t : U
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
14 / 20
Typing the case construct
~ →T
Γ`t:U
Γ ` θ : T → T0
~ → T0
Γ ` {|θ|} · t : U
`0:nat
`λy .y :nat→nat
`θpred :0→nat ∩ (succ nat)→nat
0→nat ∩ (succ nat)→nat 4 nat→nat
x:nat `θpred :nat→nat
x:nat `x:nat
x:nat `{|θpred |}·x:nat
`λx.{|θpred |}·x:nat→nat
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
14 / 20
Typing data-structures
`c: c
`0: 0
Barbara Petit (LIP - ENS Lyon)
` succ : succ
` succ 0 : succ nat?
A Polymorphic Type System for the λ-calculus with Constructors
15 / 20
Typing data-structures
`c: c
D 4 T → DT
`0: 0
` succ : succ
` succ 0 : succ nat
`succ: succ
succ 4nat→succ nat
`succ: nat→succ nat
`succ: nat→nat
Barbara Petit (LIP - ENS Lyon)
succ nat4nat
A Polymorphic Type System for the λ-calculus with Constructors
15 / 20
Typing data-structures
`c: c
D 4 T → DT
`0: 0
` succ : succ
` succ 0 : succ nat
`succ: succ
succ 4nat→succ nat
`succ: nat→succ nat
`succ: nat→nat
succ nat4nat
+ usual intersection and union subtyping rules
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
15 / 20
1
The λ-calculus with constructors
2
Type system
3
Realisability semantic
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
16 / 20
Reducibility candidates
Goal :
`t: T
;
t ∈ [T ]
( [T ] ∈ CR )
S ⊆ Λ0 is a reducibility candidate if :
(CR1) Perfect normalisation : S ⊆ PN0
(CR2) Stability by reduction : t ∈ S ⇒ Red1 (t) ⊆ S
(CR3) Stability by neutral expansion : If t is neutral, then
Red1 (t) ⊆ S ⇒ t ∈ S
t defined : no subterm {|θ|} · c with c ∈
/ dom(θ)
t perfectly normalising : t is SN and every reduct is defined
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
17 / 20
Reducibility candidates
Goal :
`t: T
;
t ∈ [T ]
( [T ] ∈ CR )
S ⊆ Λ0 is a reducibility candidate if :
(CR1) Perfect normalisation : S ⊆ PN0
(CR2) Stability by reduction : t ∈ S ⇒ Red1 (t) ⊆ S
(CR3) Stability by neutral expansion : If t is neutral, then
Red1 (t) ⊆ S ⇒ t ∈ S
t defined : no subterm {|θ|} · c with c ∈
/ dom(θ)
t perfectly normalising : t is SN and every reduct is defined
t value : t = λx.t0 or t = ct1 . . . tk
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
17 / 20
Interpretation of types
Arrow types :
[T → U ] = {t ∈ Λ0 /u ∈ [T ] ⇒ tu ∈ [U ]}
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
18 / 20
Interpretation of types
Arrow types :
[T → U ] = {t ∈ Λ0 /u ∈ [T ] ⇒ tu ∈ [U ]}
Data types :
D ∈ CR data-candidate : t value ∈ D ⇒ t = ct1 . . . tk
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
18 / 20
Interpretation of types
Arrow types :
[T → U ] = {t ∈ Λ0 /u ∈ [T ] ⇒ tu ∈ [U ]}
Data types :
D ∈ CR data-candidate : t value ∈ D ⇒ t = ct1 . . . tk
Union types :
CR stable by union ([Riba])
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
18 / 20
Perfect normalisation
Under the rug : An intermediate calculus λC ’
Barbara Petit (LIP - ENS Lyon)
`t:T
⇒
t̃ ∈ [T ]
t̃ ∈ PN0
⇒ t ∈ PN0
A Polymorphic Type System for the λ-calculus with Constructors
19 / 20
Perfect normalisation
Under the rug : An intermediate calculus λC ’
`t:T
⇒
t̃ ∈ [T ]
t̃ ∈ PN0
⇒ t ∈ PN0
Typed λC is perfectly normalising
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
19 / 20
Conclusion
Further work :
Fixpoint operator
KAM with a case stack ?
Decidable fragment ?
Logical interpretation of data types (CPS ?)
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
20 / 20
Conclusion
Further work :
Fixpoint operator
KAM with a case stack ?
Decidable fragment ?
Logical interpretation of data types (CPS ?)
Thank you !
Barbara Petit (LIP - ENS Lyon)
A Polymorphic Type System for the λ-calculus with Constructors
20 / 20