SLIDE 1
Designing proof systems from programming features: states and exceptions considered as dual effects
Dominique Duval
LJK, University of Grenoble
July 5., 2011 – PPS – Groupe de Travail S´ emantique
SLIDE 2 Outline
Introduction
- 1. Duality, at the semantics level
- 2. Duality, at the logical level
- 3. About “decorated” proofs
Conclusion
SLIDE 3 The Curry Howard Lambek correspondence
intuitionistic typed lambda cartesian closed logic calculus categories propositions types
proofs terms morphisms What about non-functional features in programming languages? i.e., what about computational effects?
- Claim. Each computational effect has an associated logic
In this talk: The effects of states and exceptions, with their logics
SLIDE 4
A surprising result
There is a symmetry between the logics for states and exceptions, based on the well-known categorical duality: for states for exceptions X → X × S X → X + E with fixed S with fixed E
SLIDE 5 Outline
- 1. A symmetry between states and exceptions
at the semantics level
- 2. A symmetry between states and exceptions
at the logical level
- 3. About “decorated” proofs
Reference: J.-G. Dumas, D. Duval, L. Fousse, J.-C. Reynaud States and exceptions considered as dual effects http://arxiv.org/abs/1001.1662 (v4)
SLIDE 6 Outline
Introduction
- 1. Duality, at the semantics level
- 2. Duality, at the logical level
- 3. About “decorated” proofs
Conclusion
SLIDE 7
Exceptions: values
When dealing with exceptions, there are two kinds of values:
◮ non-exceptional values ◮ exceptions
X + Exc = X Exc
SLIDE 8 Exceptions: functions
f : X + Exc → Y + Exc
◮ f throws an exception if it may
map a non-exceptional value to an exception X
Exc Exc
◮ f catches an exception if it may
map an exception to a non-exceptional value X
Exc Exc
SLIDE 9 Exceptions: the KEY THROW operations
Exc = set of exceptions ExCstr = set of exception constructors (or exception types) For each i ∈ ExCstr:
◮ Pari = set of parameters ◮ ti : Pari → Exc = the KEY THROW operations
- r ti : Pari + Exc → Exc such that ∀ e ∈ Exc, ti(e) = e
Pari
Exc Exc – ti throws exceptions of constructor i – ti propagates exceptions E.g. Exc =
i Pari with the ti’s as coprojections
SLIDE 10 Exceptions: the KEY CATCH operations
For each i ∈ ExCstr:
◮ ci : Exc → Pari + Exc = the KEY CATCH operations
∀ p ∈ Pari
- ci(ti(p)) = p ∈ Pari ⊆ Pari + Exc
ci(tj(p)) = tj(p) ∈ Exc ⊆ Pari + Exc (∀ j = i) ∅
Exc Exc – ci catches exceptions of constructor i – ci propagates exceptions of constructor j = i E.g. Exc =
i Pari with the ti’s as coprojections:
these equations define the ci’s
SLIDE 11
Exceptions: encapsulation
The key throwing and catching operations are encapsulated for building the usual raising and handling constructions
◮ The usual raising construction throws an exception
viewed as an element of some type X
◮ The usual handling construction catches an exception
inside a block carefully delimitated
SLIDE 12 Exceptions: the RAISE (or THROW) construction
The usual raising construction throws an exception viewed as an element of some type X
◮ From key throwing (ti)
to raising (raisei,Y or throwi,Y ): raisei,Y (a) = ti(a) ∈ Y + Exc Pari
raisei,Y
=
Exc
⊆
SLIDE 13 Exceptions: the HANDLE (or TRY...CATCH) construction
The usual handling construction catches an exception inside a block carefully deliminated
◮ From key catching (ci)
to catching (catch i {g}): Pari
⊆ g
ci
Pari + Exc
=
Y + Exc Exc
⊆ ⊆
- ◮ From catching (catch i {g})
to handling (f handle i ⇒ g or try {f }catch i {g}): Y
⊆ ⊆
f
Y + Exc
=
Y + Exc Exc
⊆ catch i {g}
SLIDE 14 States
St = set of states Loc = set of locations For each i ∈ Loc:
◮ Vali = set of values ◮ li : St → Vali = lookup function
- r li : St → Vali × St such that ∀s ∈ St, li(s) = (−, s)
◮ ui : Vali × St → St = update function
∀ v ∈ Vali ∀ s ∈ St
lj(ui(v, s)) = lj(s) (∀ j = i) E.g. St =
i Vali with the li’s as projections:
these equations define the ui’s
SLIDE 15 Duality of semantics
States Exceptions i ∈ Loc, Vali i ∈ ExCstr, Pari St (=
i∈Loc Vali)
Exc (=
i∈ExCstr Pari)
li : St → Vali Exc ← Pari : ti ui : Vali × St → St Pari + Exc ← Exc : ci Vali × St
pr
Vali
id
li
Vali
=
Pari + Exc Pari
in
ci
ti
Vali × St
pr ui
St
lj Valj id
lj
Valj
=
Pari + Exc Exc
in
tj
ci
tj
SLIDE 16
◮ So, there is a duality between states and exceptions,
at the semantics level, involving a set of states St and a set of exceptions Exc.
◮ But states and exceptions are computational effects:
the “type of states” and the “type of exceptions” are hidden, they do not appear explicitly in the syntax.
◮ In fact, the duality at the semantics level
comes from a duality of states and exceptions seen as computational effects, at the logical level.
SLIDE 17 Outline
Introduction
- 1. Duality, at the semantics level
- 2. Duality, at the logical level
- 3. About “decorated” proofs
Conclusion
SLIDE 18 Monads for effects
[Moggi 1991] The basic idea behind the categorical semantics
- f effects is that we distinguish the object X of values
from the object TX of computations (for some endofunctor T) Programs of type Y with a parameter of type X
- ught to be interpreted by morphisms with codomain TY ,
but for their domain there are two alternatives, either X or TX.
- 1. Moggi chooses the first alternative:
a program X → Y is interpreted by a morphism X → TY Then T must be a monad – for substitution with a strength – for the context
- 2. The second alternative would be:
a program X → Y is interpreted by a morphism TX → TY
SLIDE 19 Monads for effects: exceptions
The monad of exceptions is TX = X + Exc.
A program of type Y with a parameter of type X is interpreted by a morphism X → Y + Exc. = ⇒ it may throw an exception = ⇒ it cannot catch an exception
A program of type Y with a parameter of type X is interpreted by a morphism X + Exc → Y + Exc. = ⇒ it may throw an exception = ⇒ it may catch an exception
SLIDE 20 Effects, more generally
- Claim. A computational effect is
an apparent lack of soundness There is a computational effect when:
◮ at first sight, the intended semantics
is not a model of the syntax
◮ but the syntax may be “decorated”
so as to recover soundness The monads approach from this point of view: – operations are decorated as values or computations and every value can be seen as a computation – a computation f c : X → Y stands for f : X → TY – a value f v : X → Y stands for f : X → Y
ηY
→ TY
SLIDE 21
States, apparently
The intended semantics (one location): l : St → Val u : Val × St → St ∀ v ∈ Val ∀ s ∈ St l(u(v, s)) = v IS NOT a model of the apparent syntax Apparent l : ✶ → V u : V → ✶ l ◦ u = id : V → V
SLIDE 22
States, explicitly
The intended semantics (one location) l : St → Val u : Val × St → St ∀ v ∈ Val ∀ s ∈ St l(u(v, s)) = v IS a model of the explicit syntax Explicit l : S → V u : V × S → S l ◦ u = pr : V × S → V
SLIDE 23 States, equationally
There are two equational logics “for states”: Apparent logic Explicit logic NOT sound sound close to the syntax FAR from the syntax
- Claim. There is a third logic for states – NOT “truly” equational:
Decorated logic sound close to the syntax
SLIDE 24 States as effect: decorations
The apparent syntax may be decorated:
- An operation f : X → Y is decorated as
f (0) : X → Y if f is pure f (1) : X → Y if f is an accessor (cf. const methods in C++) f (2) : X → Y if f is a modifier
- An equation f = g is decorated as
f =(sg) g (strong) if f and g coincide on results and on states f =(wk) g (weak) if f and g coincide on results (only) Apparent Decorated l : ✶ → V l(1) : ✶ → V u : V → ✶ u(2) : V → ✶ l ◦ u = idV : V → V l ◦ u =(wk) idV : V → V
SLIDE 25 States as effect: expliciting the decorations
The decorated syntax may be explicited
f (0) : X → Y as f : X → Y f (1) : X → Y as f : X × S → Y f (2) : X → Y as f : X × S → Y × S
f =(sg) g as f = g : X × S → Y × S f =(wk) g as prY ◦ f = prY ◦ g : X × S → Y Decorated Explicit l(1) : ✶ → V l : ✶ × S → V u(2) : V → ✶ u : V × S → S l ◦ u =(wk) idV : V × S → V l ◦ u = prV : V × S → V
SLIDE 26 States as effect: three logics
Decorated l(1) : ✶ → V u(2) : V → ✶ l ◦ u =(wk) idV
l : ✶ → V u : V → ✶ l ◦ u = idV Explicit l : S → V u : V × S → S l ◦ u = prV The intended semantics
◮ IS NOT a model of the apparent syntax (effect) ◮ IS a model of the explicit syntax (obviously) ◮ IS a model of the decorated syntax (by adjunction)
SLIDE 27
Exceptions as effect
The intended semantics (one exception constructor): t : Par → Exc c : Exc → Par + Exc ∀ p ∈ Par c(t(p)) = p IS NOT a model of the apparent syntax IS a model of the explicit syntax Apparent Explicit t : P → ✵ t : P → E c : ✵ → P c : E → P + E c ◦ t = id : P → P c ◦ t = in : P → P + E
SLIDE 28 Exceptions as effect: decorations
The apparent syntax may be decorated:
- An operation f : X → Y is decorated as
f (0) : X → Y if f is pure f (1) : X → Y if f is a propagator (it may throw exceptions) f (2) : X → Y if f is a catcher (it may throw and catch exc.)
- An equation f = g is decorated as
f =(sg) g (strong) if f and g coincide on exc. and on values f =(wk) g (weak) if f and g coincide on values (only) Apparent Decorated t : P → ✵ t(1) : P → ✵ c : ✵ → P c(2) : ✵ → P c ◦ t = id : P → P c(2) ◦ t(1) =(wk) id(0) : P → P
SLIDE 29 Exceptions as effect: expliciting the decorations
The decorated syntax may be explicited
f (0) : X → Y as f : X → Y f (1) : X → Y as f : X → Y + E f (2) : X → Y as f : X + E → Y + E
f =(sg) g as f = g : X × S → Y × S f =(wk) g as f ◦ inX = g ◦ inX : X → Y + E Decorated Explicit t(1) : P → ✵ t : P → E c(2) : ✵ → P c : E → P + E c(2) ◦ t(1) =(wk) id(0) : P → P c ◦ t = in : P → P + E
SLIDE 30 Exceptions as effect: three logics
Decorated t(1) : P → ✵ c(2) : ✵ → P c ◦ t =(wk) idP
t : P → ✵ c : ✵ → P c ◦ t = idP Explicit t : P → E c : E → P + E c ◦ t = inP The intended semantics
◮ IS NOT a model of the apparent syntax (effect) ◮ IS a model of the explicit syntax (obviously) ◮ IS a model of the decorated syntax (by adjunction)
SLIDE 31 Duality of effects
States Exceptions i ∈ Loc, Vi i ∈ ExCstr, Pi ✶ ✵ l(1)
i
: ✶ → Vi ✵ ← Pi : t(1)
i
u(2)
i
: Vi → ✶ Pi ← ✵ : c(2)
i
Vi
id
Vi
id
li
Vi
=(wk)
Pi Pi
id
ci
ti
Vi
✶
lj Vj id
lj
Vj
=(wk)
Pi ✵
tj
ci
tj
SLIDE 32 Outline
Introduction
- 1. Duality, at the semantics level
- 2. Duality, at the logical level
- 3. About “decorated” proofs
Conclusion
SLIDE 33 Operations and equations
- The monads approach leads to Lawvere theories
for getting operations and equations [Plotkin&Power 2001] This can be extended
◮ with exception monads [Schroeder&Mossakowski 2004] ◮ with coalgebras [Levy 2006] ◮ with handlers [Plotkin&Pretnar 2009]
Then
– lookup, update, raise are algebraic operations – handle IS NOT an algebraic operation
- Our approach generalizes algebraic specifications
it involves (decorated) operations and equations
Then
– catching exceptions is symmetric to updating states
SLIDE 34 A framework for effects
A language without effects is defined with respect to one logic L A language with effects is defined with respect to a span of logics Ldeco
Lexpl Morphisms of logics are defined in the category of diagrammatic logics [Duval&Lair 2002]. This is based on:
◮ Adjunctions [Kan 1958] ◮ Categories of fractions [Gabriel&Zisman 1967] ◮ Limit sketches [Ehresmann 1968]
SLIDE 35 One logic: models
A diagrammatic logic is a left adjoint functor L with a full and faithful right adjoint R S
L
T
R (f.f.)
induced by a morphism of limit sketches
◮ S is the category of specifications ◮ T is the category of theories ◮ Each specification Σ presents the theory LΣ ◮ A model M : Σ → Θ is an “oblique” morphism:
M : L Σ → Θ in T
M : Σ → R Θ in S
SLIDE 36 One logic: proofs
T is a category of fractions on S: a fraction is a cospan in S with numerator σ and denominator τ such that L τ is invertible in T Σ1
σ
Σ′
2
τ
- This fraction can be seen as
◮ an instance of the specification Σ1 in Σ2 ◮ or an inference rule with hypothesis Σ2 and conclusion Σ1
The inference step is the composition of fractions: applying a rule with hypothesis H and conclusion C to an instance of H in Σ yields an instance of C in Σ.
SLIDE 37 A category of logics
A morphism of logics F : L1 → L2 is a pair of left adjoint functors (FS, FT) in a commutative square S1
L1
T1
FT
L2
T2
∼ =
induced by a commutative square of limit sketches This yields the category of diagrammatic logics
SLIDE 38 Decorated proofs
Ldeco
Fapp
Lexpl In this talk, for states and exceptions, Lapp and Lexpl are (variants of) equational logic. Each decorated proof is mapped to an equational proof
◮ either by dropping the decorations (by Fapp)
→ an “uninteresting” proof
◮ or by expliciting the decorations (by Fexpl)
→ a “complicated” proof
SLIDE 39 Some decorated rules for states (1)
(0-to-1) f (0) f (1) (1-to-2) f (1) f (2) (sg-subs) g(2)
1
=(sg) g(2)
2
(g1 ◦ f )(2) =(sg) (g2 ◦ f )(2) (sg-repl) f (2)
1
=(sg) f (2)
2
(g ◦ f1)(2) =(sg) (g ◦ f2)(2) (wk-subs) g(2)
1
=(wk) g(2)
2
(g1 ◦ f )(2) =(wk) (g2 ◦ f )(2) (wk-repl) f (2)
1
=(wk) f (2)
2
g(0) (g ◦ f1)(2) =(wk) (g ◦ f2)(2)
SLIDE 40 Some decorated rules for states (2)
(sg-to-wk) f (2) =(sg) g(2) f (2) =(wk) g(2) (wk-to-sg) f (1) =(wk) g(1) f (1) =(sg) g(1)
and the lookup’s form a “decorated product” (l(1)
j
)j∈Loc such that f (2) =(sg) g(2) ⇐ ⇒ ∀j ∈ Loc, (lj ◦ f )(2) =(wk) (lj ◦ g)(2) Vi X
f
✶
li
. . . . . Vj
SLIDE 41 A decorated proof (for states)
- Proposition. For every i ∈ Loc:
◮ Semantically: ∀s ∈ St, ui(li(s), s) = s ◮ Explicitly: ui ◦ li = idS ◮ Decorated: u(2) i
i
=(sg) id(0)
✶
j
i
i
=(wk) l(1)
j When j = i: li ◦ ui =(wk) idVi (wk-subs) li ◦ ui ◦ li =(wk) li When j = i: lj ◦ ui =(wk) lj ◦ Vi (wk-subs) lj ◦ ui ◦ li =(wk) lj ◦ Vi ◦ li . . . Vi ◦ li =(sg) id✶ (sg-repl) lj ◦ Vi ◦ li =(sg) lj (sg-to-wk) lj ◦ Vi ◦ li =(wk) lj (wk-trans) lj ◦ ui ◦ li =(wk) lj
SLIDE 42 Decorated rules and proofs (for exceptions)
Decorated rules and proofs for exceptions are dual to decorated rules and proofs for states.
- Proposition. For every i ∈ ExCstr:
◮ Semantically: ∀e ∈ Exc, ti(ci(e)) = e ◮ Explicitly: ti ◦ ci = idE ◮ Decorated: t(1) i
i
=(sg) id(0)
✶
- Proof. Dual to the proof for states.
SLIDE 43 More decorated proofs (for states)
Equations from [Plotkin&Power 2002] as stated in [Melli` es 2010]
◮ Interaction update-update:
storing a value v and then a value v′ at the same location i is just like storing the value v′ in the location i. ∀i ∈ Loc,
u(2)
i
- (ui × idVi)(2) =(sg) u(2)
i
2
◮ Commutation update-update:
the order of storing in two different locations i and j does not matter. ∀i = j ∈ Loc,
u(2)
j
- (ui × idVj)(2) =(sg) u(2)
i
Decorated proofs in [Dumas&Duval&Fousse&Reynaud 2011]
SLIDE 44 More decorated proofs (for exceptions)
◮ Interaction catch-catch:
when catching an exception constructor i twice, the second catcher is never used. ∀i ∈ ExCstr,
try {f }catch i {g}catch i {h} =(sg) try {f }catch i {g}
◮ Commutation catch-catch:
when catching two different exception constructors i and j, the order of catching does not matter. ∀i = j ∈ ExCstr,
try {f }catch i {g}catch j {h} =(sg) try {f }catch j {h}catch i {g}
Proof.
- 1. Start from the previous equations for states
- 2. Dualize
- 3. Encapsulate
SLIDE 45 Outline
Introduction
- 1. Duality, at the semantics level
- 2. Duality, at the logical level
- 3. About “decorated” proofs
Conclusion
SLIDE 46
Conclusion
◮ An effect is an apparent lack of soundness ◮ Designing proof systems from programming features:
each computational effect has an associated logic
◮ States and exceptions may be considered as dual effects
Future work
◮ Using a proof assistant (Coq) for decorated proofs ◮ Combining effects by composing the spans of logics
SLIDE 47 A question
[Melli` es 2010] About the notion of monad and the notion of sheaf on a Grothendieck topology: It is fascinating to observe that the most promising links between mathematics and programming languages emerged at these somewhat himalayan heights. Mount Everest, 8 848 m.
- Question. What is the “height” of our (naive?) approach?
Grand pic de Belledonne, 2 977 m.
SLIDE 48
Thanks for your attention