The lambda calculus with constructors

The lambda calculus with constructors
Categorical semantic and Continuations
Barbara Petit
Focus - Univ. Bologna
CaCos 2012
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
1 / 20
Outline
1
Introduction: λ-calculus and abstract machines
2
The λ-calculus with constructors
3
CPS and Categorical models
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
Lambda Calculus and KAM
Terms:
β-reduction:
t, u := x | λx.t | t u
(λx.t) u → t[u/x]
Processes:
t
Push
Pop
tu
λx.t0
Barbara Petit (Focus - Univ. Bologna)
?
?
π
? u·π
u1 · · · un
t
? u·π
t0 [u/x] ?
π
The lambda calculus with constructors
2 / 20
Lambda Calculus and KAM
Terms:
β-reduction:
t, u := x | λx.t | t u
(λx.t) u → t[u/x]
Processes:
t
Push
Pop
tu
λx.t0
?
?
π
? u·π
u1 · · · un
t
? u·π
t0 [u/x] ?
π
Example:
(λf.λx.f xx) add 3
(λf.λx.f xx) add 3 ? (λf.λx.f xx) add
(λf.λx.f xx)
λx.add xx
add 3 3
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
?
3·
? add · 3 · ?
3·
?
2 / 20
Stack Abstract Machines
Enlight the interaction values/destructors (ie functions/arguments),
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
3 / 20
Stack Abstract Machines
Enlight the interaction values/destructors (ie functions/arguments),
Semantic: notion of duality (term/context, player/opponent etc.),
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
3 / 20
Stack Abstract Machines
Enlight the interaction values/destructors (ie functions/arguments),
Semantic: notion of duality (term/context, player/opponent etc.),
Implement a strategy,
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
3 / 20
Stack Abstract Machines
Enlight the interaction values/destructors (ie functions/arguments),
Semantic: notion of duality (term/context, player/opponent etc.),
Implement a strategy,
Provide cps translations.
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
3 / 20
Outline
1
Introduction: λ-calculus and abstract machines
2
The λ-calculus with constructors
3
CPS and Categorical models
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
λ-calculus with pattern matching
Terms:
Case binding:
Barbara Petit (Focus - Univ. Bologna)
t, u := x | λx.t | t u
| c | {|θ|} · t
θ, φ := {c1 7→ u1 ; · · · ; ck 7→ uk }
The lambda calculus with constructors
4 / 20
λ-calculus with pattern matching
t, u := x | λx.t | t u
| c | {|θ|} · t
Terms:
Case binding:
θ, φ := {c1 7→ u1 ; · · · ; ck 7→ uk }
Case binding + Constructor = Pattern matching
{|c1 7→ u1 ; · · · ; ck 7→ uk |} · ci
→
ui
If := λbxy. {| true 7→ x ; false 7→ y |} · b
If true 1 0 →∗ {| true 7→ 1 ; false 7→ 0 |} · true
→ 1
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
4 / 20
λ-calculus with pattern matching
t, u := x | λx.t | t u
| c | {|θ|} · t
Terms:
Case binding:
θ, φ := {c1 7→ u1 ; · · · ; ck 7→ uk }
Case binding + Constructor = Pattern matching
{|c1 7→ u1 ; · · · ; ck 7→ uk |} · ci
→
ui
If := λbxy. {| true 7→ x ; false 7→ y |} · b
If true 1 0 →∗ {| true 7→ 1 ; false 7→ 0 |} · true
→ 1
Only constant constructors can be matched!
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
4 / 20
New rules for the KAM
u
eliminates
functions
θ
eliminates
constructors
term ? Stack of eliminators
Push
Pop
Pushc
Popc
tu
λx.t0
?
π
? u·π
{|θ|} · t ?
c
?
Barbara Petit (Focus - Univ. Bologna)
π
θ·π
t
? u·π
t0 [u/x] ?
π
t
u
The lambda calculus with constructors
?
?
θ·π
π
(c 7→u ∈ θ)
5 / 20
Example
If := λbxy. {| true 7→ x ; false 7→ y |} · b
If true t u If true t ? u
If true ? t · u
If ? true · t · u
λxy. {| true 7→ x ; false 7→ y |} · true ? t · u
λy. {| true 7→ t ; false 7→ y |} · true ? u
{| true 7→ t ; false 7→ u |} · true ? true ? { true 7→ t ; false 7→ u }
t ? Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
6 / 20
Blocked configuration
{| 0 7→ t ; S 7→ t0 |} · (S n)
Barbara Petit (Focus - Univ. Bologna)
? S n ? {0 7→ t ; S 7→ t0 }
S ? n · {0 7→ t ; S 7→ t0 }
The lambda calculus with constructors
7 / 20
Blocked configuration
{| 0 7→ t ; S 7→ t0 |} · (S n)
? S n ? {0 7→ t ; S 7→ t0 }
S ? n · {0 7→ t ; S 7→ t0 }
In ML:
S [n] ? {0 7→ t ; S x 7→ t0 x}
t0 n ? t0
? n
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
7 / 20
Blocked configuration
{| 0 7→ t ; S 7→ t0 |} · (S n)
? S n ? {0 7→ t ; S 7→ t0 }
S ? n · {0 7→ t ; S 7→ t0 }
In ML:
S [n] ? {0 7→ t ; S x 7→ t0 x}
t0 n ? t0
? n
Same behaviour:
S
t0
? {0 7→ t ; S 7→ t0 } · n
? n
Commutation case-binding / argument
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
7 / 20
Commutation between eliminators
u · θ · π
'
t ? θ1 · u1 · u2 · θ2 · u3
θ2 · θ1
?
θ · u · π
' t ? u1 · u2 · u3 · θ1 · θ2
' t ? θ1 · θ2 · u1 · u2 · u3
t
?
u1 · u2 · u3
Two stacks.
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
8 / 20
Abstract machine with two stacks
τ
τ
Pop
Push
Popc
Pushc
?
?
λx.t
tu
? u·π
? π
τ ·θ ?
c
?
τ ? {|θ|} · t ?
π
π
τ
τ
? t[u/x] ? π
?
t
? u·π
τ ?
τ ·θ ?
θc
t
?
?
π
π
Blocked configurations:
? c ? u1 · · · uk
θ1 · · · θk ? λx.t ?
τ ·θ
?
c
?
cu1 . . . uk
λx.{|θ1 |} · · · · {|θk |} · t
π
match failure
with c∈dom(θ)
/
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
9 / 20
The λC -calculus [AMR06]
Terms:
Case bindings:
t, u :=
θ, φ :=
x | λx.t | tu | c | {|θ|} · t
{c1 7→ u1 ; · · · ; ck 7→ uk }
Commutation Case/Application:
{|θ|} · (tu) → ({|θ|} · t) u
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
(CaseApp)
10 / 20
The λC -calculus [AMR06]
Terms:
Case bindings:
t, u :=
θ, φ :=
x | λx.t | tu | c | {|θ|} · t
{c1 7→ u1 ; · · · ; ck 7→ uk }
Commutation Case/Application:
{|θ|} · (tu) → ({|θ|} · t) u
(CaseApp)
pred := λx. {|0 7→ 0; S 7→ λy.y |} · x
pred (S p) → {|0 7→ 0; S 7→ λy.y|} · (S p)
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
LamApp
10 / 20
The λC -calculus [AMR06]
Terms:
Case bindings:
t, u :=
θ, φ :=
x | λx.t | tu | c | {|θ|} · t
{c1 7→ u1 ; · · · ; ck 7→ uk }
Commutation Case/Application:
{|θ|} · (tu) → ({|θ|} · t) u
(CaseApp)
pred := λx. {|0 7→ 0; S 7→ λy.y |} · x
pred (S p) → {|0 7→ 0; S 7→ λy.y|} · (S p)
→ ({|0 →
7 0; S 7→ λy.y|} · S ) p
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
LamApp
CaseApp
10 / 20
The λC -calculus [AMR06]
Terms:
Case bindings:
t, u :=
θ, φ :=
x | λx.t | tu | c | {|θ|} · t
{c1 7→ u1 ; · · · ; ck 7→ uk }
Commutation Case/Application:
{|θ|} · (tu) → ({|θ|} · t) u
(CaseApp)
pred := λx. {|0 7→ 0; S 7→ λy.y |} · x
pred (S p) →
→
→
→
{|0 7→ 0; S 7→ λy.y|} · (S p)
({|0 7→ 0; S 7→ λy.y|} · S ) p
(λy.y) p
p
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
LamApp
CaseApp
CaseCons
LamApp
10 / 20
Reduction rules
AppLam
CaseCons
CaseApp
Barbara Petit (Focus - Univ. Bologna)
(λx.t) u
{|θ|} · c
{|θ|} · (tu)
→ t[u/x]
→ t
→ ({|θ|} · t)u
The lambda calculus with constructors
((c 7→ t) ∈ θ)
11 / 20
Reduction rules
AppLam
CaseCons
CaseApp
CaseLam
(λx.t) u
{|θ|} · c
{|θ|} · (tu)
{|θ|} · λx.t
→ t[u/x]
→ t
→ ({|θ|} · t)u
→ λx.{|θ|} · t
((c 7→ t) ∈ θ)
(x ∈
/ fv(θ))
Confluent calculus
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
11 / 20
Reduction rules
(λx.t) u
λx.tx
AppLam
LamApp
→ t[u/x]
→ t
CaseCons
CaseApp
CaseLam
{|θ|} · c
{|θ|} · (tu)
{|θ|} · λx.t
→ t
→ ({|θ|} · t)u
→ λx.{|θ|} · t
CaseCase
{|θ|} · ({|φ|} · t)
→ {|θ ◦ φ|} · t
(x ∈
/ fv(t))
((c 7→ t) ∈ θ)
(x ∈
/ fv(θ))
with θ ◦ {c1 7→ t1 ; . . . ; cn 7→ tn } := c1 7→ {|θ|} · t1 ; . . . ; cn 7→ {|θ|} · tn
Confluent calculus
Barbara Petit (Focus - Univ. Bologna)
Separation property
The lambda calculus with constructors
11 / 20
Characteristic of the λC -calculus
Composed data structures and ML-like pattern matching:
(cons a) l
cons(a,l)
match l with
| Nil
-> Nil
| Cons(x,y) -> y
Barbara Petit (Focus - Univ. Bologna)
{|nil 7→ nil; cons 7→ λxy.y|} · l
The lambda calculus with constructors
12 / 20
Characteristic of the λC -calculus
Composed data structures and ML-like pattern matching:
(cons a) l
cons(a,l)
match l with
| Nil
-> Nil
| Cons(x,y) -> y
{|nil 7→ nil; cons 7→ λxy.y|} · l
Variadic constructors: {|θ|} · (cons a l) → ({|θ|} · cons a)l
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
12 / 20
Characteristic of the λC -calculus
Composed data structures and ML-like pattern matching:
(cons a) l
cons(a,l)
match l with
| Nil
-> Nil
| Cons(x,y) -> y
{|nil 7→ nil; cons 7→ λxy.y|} · l
Variadic constructors: {|θ|} · (cons a l) → ({|θ|} · cons a)l
Case analysis on the head-position subterm:
t
Barbara Petit (Focus - Univ. Bologna)
:= {|θ|} · (λx1 . . . xn . y u1 . . . uk )
→∗ λx1 . . . xn . {|θ|} · (y u1 . . . uk )
→∗ λx1 . . . xn . ({|θ|} · y) u1 . . . uk
The lambda calculus with constructors
12 / 20
Outline
1
Introduction: λ-calculus and abstract machines
2
The λ-calculus with constructors
3
CPS and Categorical models
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
Continuation Passing Style translation
[Plotkin75] : Cbn can be simulated by cbv via an abstract machine.
λC -calculus → λ-calculus with pairs.
t
7→
t∗
t∗ = λk.let hxθ , xπ i := k in |xθ ? t ? xπ | .
Case bindings encoded with n-tuples.
Theorem (Simulation):
If t1 → t2
Barbara Petit (Focus - Univ. Bologna)
then t∗1 →∗ t∗2 .
The lambda calculus with constructors
13 / 20
Categories for the λ-calculus
Castesian Closed Categories: objects, morphisms, products,
projections, exponent etc.
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
14 / 20
Categories for the λ-calculus
Castesian Closed Categories: objects, morphisms, products,
projections, exponent etc.
CCCs capture the simply typed λ-calculus:
x1 : T1 ,...,xk : Tk ` t: U
[t]
[T1 ] × · · · × [Tk ]
[U ]
CCCs with an object D ' DD capture the untyped λ-calculus
(with η):
fv(t)={x1 ,...,xk }
Barbara Petit (Focus - Univ. Bologna)
D × ··· × D
The lambda calculus with constructors
[t]
D
14 / 20
Interpreting case bindings
λ-calculus with n constructors {c1 , . . . , cn }.
Terms: points of D. Case bindings: points of Dn .
[ {c1 7→ u1 ; · · · ; cm 7→ un } ] = h [u1 ]; · · · ; [un ] i
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
15 / 20
Interpreting case bindings
λ-calculus with n constructors {c1 , . . . , cn }.
Terms: points of D. Case bindings: points of Dn .
[ {c1 7→ u1 ; · · · ; cm 7→ un } ] = h [u1 ]; · · · ; [un ] i
[ {c2 7→ u; c3 7→ u0 } ] = h ; [u]; [u0 ]; ; · · · ; i
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
15 / 20
Categorical models of the λη -calculus
Model of untyped λη -calculus: M = (D, app, lam)
lam :
DD → D
(x 7→ t)7→ λx.t
app : D→ DD
t 7→(x 7→ tx)
app
D
'
DD
lam
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
16 / 20
Categorical models of the λC -calculus
Model of untyped λC -calculus: M = (D, app, lam, case, c∗ , )
lam :
DD → D
(x 7→ t)7→ λx.t
case : Dn × D→ D
(θ, t) 7→{|θ|} · t
app : D→ DD
t 7→(x 7→ tx)
c∗ : 1 →
7→
D
c
app
D
'
DD
+ five commuting diagrams
Dn
lam
CaseCons:
πin
D
Barbara Petit (Focus - Univ. Bologna)
'
The lambda calculus with constructors
Dn × 1
id ×c∗i
case
Dn × D
16 / 20
λC -models
M = (D, app, lam, case, c∗ )
Theorem (Soundness):
t 'λC t0
Barbara Petit (Focus - Univ. Bologna)
interpretation function:
t 7→ [t]M ∈ D
For any λC -model M,
=⇒
[t]M = [t0 ]M .
The lambda calculus with constructors
17 / 20
λC -models
M = (D, app, lam, case, c∗ )
Theorem (Soundness):
t 'λC t0
interpretation function:
t 7→ [t]M ∈ D
For any λC -model M,
=⇒
[t]M = [t0 ]M .
Theorem (Completeness):
If t and t0 do not reduce on a match-failure, then
[t]M = [t0 ]M
for all M
=⇒
t 'λC t0 .
(The proof relies on a syntactic model in the category of PERs)
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
17 / 20
CPS and models
Generic model
λ:
M = (D, app, lam)
D ' DD
?
λC :
(D, app, lam, case, c∗ )
D ' DD
some commuting diagrams
t 7→ [t∗ ]M
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
18 / 20
CPS and models
Continuation model
Generic model
λ:
M = (D, app, lam)
D ' DD
CPS
D=R
C
C ' RC × C
?
λC :
(D, app, lam, case, c∗ )
D ' DD
some commuting diagrams
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
18 / 20
CPS and models
λ:
Generic model
Continuation model
M = (D, app, lam)
D ' DD
C ' RC × C
S=C
λC :
(D, app, lam, case, c∗ )
D ' DD
some commuting diagrams
Barbara Petit (Focus - Univ. Bologna)
CPS
The lambda calculus with constructors
C ' Dn × S
S 'D ×S
(where D = RC )
18 / 20
Continuation model
In a ccc with three particular objects s. t.
C ' Dn × S
,
S 'D ×S
,
D ' RC
,
we can define lam, app, case (c∗i )i≤n and .
Example in Set:
lam : DD → D ' DD → RC
D
= fun f D 7→
fun k C 7→
n
let hθD , hxD , π S ii = k in f (x) hθ, πi
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
19 / 20
Continuation model
In a ccc with three particular objects s. t.
C ' Dn × S
,
S 'D ×S
,
D ' RC
,
we can define lam, app, case (c∗i )i≤n and .
Example in Set:
c∗i : 1 → D ' RC
=
fun k C 7→
n
let hθD , π S i = k in projni (θ)
Theorem :
Every continuation model is a λC -model.
Scott D∞ models are such models ,̈
^
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
19 / 20
Conclusion
A categorical definition of model for the λC -calculus
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
,̈
^
20 / 20
Conclusion
A categorical definition of model for the λC -calculus
Non syntactical models exist
Barbara Petit (Focus - Univ. Bologna)
,̈
^
,̈
^
The lambda calculus with constructors
20 / 20
Conclusion
A categorical definition of model for the λC -calculus
Non syntactical models exist
Continuation model
Barbara Petit (Focus - Univ. Bologna)
,̈
^
,̈
^
λµ -calculus with constructors
The lambda calculus with constructors
?
20 / 20
Conclusion
A categorical definition of model for the λC -calculus
Non syntactical models exist
Continuation model
,̈
^
,̈
^
λµ -calculus with constructors
Define an interpretation of case bindings with sums
?
?
for a “complete” completeness
Barbara Petit (Focus - Univ. Bologna)
The lambda calculus with constructors
20 / 20