states and exceptions are dual effects
play

States and exceptions are dual effects Jean-Guillaume Dumas, - PowerPoint PPT Presentation

States and exceptions are dual effects Jean-Guillaume Dumas, Dominique Duval, Laurent Fousse, Jean-Claude Reynaud LJK, University of Grenoble August 29, 2010 Workshop on Categorical Logic in Brno (this is a long version of the talk presented


  1. States and exceptions are dual effects Jean-Guillaume Dumas, Dominique Duval, Laurent Fousse, Jean-Claude Reynaud LJK, University of Grenoble August 29, 2010 Workshop on Categorical Logic in Brno (this is a long version of the talk presented at the workshop)

  2. Outline Introduction States Diagrammatic logics Exceptions Conclusion

  3. Semantics of computational effects? The categorical semantics of functional programming languages is based on the Curry-Howard-Lambek correspondence: logic programming categories propositions types objects proofs terms morphisms intuitionistic simply typed cartesian closed logic lambda calculus categories What about categorical semantics of non-functional programming languages, i.e., languages with effects? programming categories effect categorical structure ?? (global) states ?? exceptions ??

  4. Effects as monads Moggi [1989], cf. Haskell: Programs of type B with a parameter of type A are interpreted by morphisms from A to T ( B ) . p : A → B is interpreted as p : A → T ( B ) States. p : A → B is interpreted as p : A × St → B × St , or p : A → ( B × St ) St , where St is the set of states Exceptions. p : A → B is interpreted as p : A → Exc , where Exc is the set of exceptions effect monad ( T , η, µ ) T ( X ) = ( X × St ) St states exceptions T ( X ) = X + Exc Note. What about the handling (catching) of exceptions?

  5. Effects as Lawvere theories Plotkin & Power [2001]: Use the connection between monads and Lawvere theories to give operations a primitive role, with the monad as derived States. Loc is the set of locations, Val is the set of values ( St = Val Loc is the set of states) Exceptions. Exc is the set of exceptions effect Lawvere theory generated by lookup : Val → Loc states update : 1 → Loc × Val with 7 equations raise e : 0 → 1 for e ∈ Exc exceptions with no equation Note. What about the handling (catching) of exceptions?

  6. Effects as zooms (= spans of logics) Following Moggi’s remark: p : A → B is interpreted as p : A → T ( B ) More generally, we claim that an effect occurs when there is an apparent mismatch between syntax and semantics ◮ Without effects: a unique logic for syntax and semantics ◮ With effects: a logic for the (apparent) syntax, another logic for the semantics, and a span of logics (= a zoom) relating them

  7. Notes About the authors Our background lies in computer algebra: abstract algebra, algorithmic, programmation (exact, efficient, generic,...) in languages such as Axiom, C, C++,...

  8. Notes About the authors Our background lies in computer algebra: abstract algebra, algorithmic, programmation (exact, efficient, generic,...) in languages such as Axiom, C, C++,... About terminology SPECIFICATION vs. THEORY In this talk, a logical theory is “saturated”: every theorem that can be deduced from the theory belongs to the theory. We call specification a family of axioms and theorems that may be non-saturated. A specification presents (= generates) a theory, and several different specifications may present the same theory.

  9. Notes About the authors Our background lies in computer algebra: abstract algebra, algorithmic, programmation (exact, efficient, generic,...) in languages such as Axiom, C, C++,... About terminology SPECIFICATION vs. THEORY In this talk, a logical theory is “saturated”: every theorem that can be deduced from the theory belongs to the theory. We call specification a family of axioms and theorems that may be non-saturated. A specification presents (= generates) a theory, and several different specifications may present the same theory. About terminology SYNTAX vs. SEMANTICS In this talk, the syntax may include some axioms (logical semantics) and the semantics is denotational

  10. Outline Introduction States Diagrammatic logics Exceptions Conclusion

  11. Imperative programming In imperative programming the state of the memory may be observed (lookup) and modified (update) However, the state never appears explicitly in the syntax: there no “type of states” After updating a location X to a value n : ◮ a lookup to X returns n ◮ while a lookup to Y ( � = X ) returns the same value as the lookup to Y before updating X . This can be written in a loose functorial style: � lookup X ( update X ( n )) = n lookup Y ( update X ( n )) = lookup Y ( ) This is now formalized, by defining three specifications

  12. The apparent specification Notations: Loc = { X , Y , ... } = the set of locations 1 for “Unit”, with ( ) A : A → 1 for all A From the syntax we get the apparent equational specification Σ 1 : for each location i ∈ Loc : ◮ a type V i for the values of i ◮ two functions: � lookup l i : 1 → V i update u i : V i → 1 ◮ equations: � l i ◦ u i = id V i l j ◦ u i = l j ◦ ( ) V i for all j � = i EFFECT: the intended semantics is not a model of Σ 1 .

  13. The explicit specification Let S be the “type of states”. From the semantics we get the explicit equational specification Σ 2 : For each location i ∈ Loc : ◮ V i � l i : S → V i ◮ u i : V i × S → S � l i ◦ u i = pr V i ◮ l j ◦ u i = l j ◦ pr S for all j � = i EFFECT: the intended semantics is a model of Σ 2 , but Σ 2 does not fit with the syntax, because of the “type of states” S

  14. Decorations Let us introduce decorations for functions: ◮ (0) for pure functions ◮ (1) for accessors (= inspectors) ◮ (2) for modifiers AND for equations: ◮ ∼ for weak equations (equality on values only) ◮ = for strong equations (equality on values and state)

  15. The decorated specification With the decorations we form the decorated specification Σ 0 : for each location i ∈ Loc : ◮ V i � l (1) : 1 → V i i ◮ u (2) : V i → 1 i � l (1) ◦ u (2) ∼ id (0) i i V i ◮ l (1) ◦ u (2) ∼ l (1) ◦ ( ) (0) V i for all j � = i j i j

  16. Claim. The decorated specification Σ 0 is “the most relevant”: ◮ both the apparent and the explicit specification may be recovered from Σ 0 ◮ Σ 0 fits with the syntax (no type S ) ◮ the intended semantics is a “decorated model” of Σ 0 ◮ “decorated proofs” may be performed from Σ 0

  17. � � Three specifications l (1) : 1 → V i i u (2) : V i → 1 i l (1) ◦ u (2) ∼ id (0) � � F 1 i i F 2 V i l (1) ◦ u (2) ∼ l (1) ◦ ( ) (0) j i j V i DECORATED: Σ 0 l i : 1 → V i l i : S → V i u i : V i → 1 u i : V i × S → S l i ◦ u i = id V i l i ◦ u i = pr V i l j ◦ u i = l j ◦ ( ) V i l j ◦ u i = l j ◦ pr S APPARENT: Σ 1 EXPLICIT: Σ 2 ◮ F 1 : from decorated to apparent: wipe out all decorations ◮ F 2 : from decorated to explicit: according to the decoration

  18. � � Three logics Claim: the 3 specifications are defined in 3 “logics” related by a “span of logics”: F 1 F 2 DECORATED: L 0 APPARENT: L 1 EXPLICIT: L 2 ◮ What is a logic? ◮ What is a morphism of logics?

  19. Outline Introduction States Diagrammatic logics Exceptions Conclusion

  20. � Diagrammatic logic A diagrammatic logic is a functor L L � T S with a full and faithful right adjoint R ⊥ R

  21. � � � � � Diagrammatic logic A diagrammatic logic is a functor L L � T S with a full and faithful right adjoint R ⊥ R induced by a morphism of limit sketches ( S and T are locally presentable categories) e E S E T Y S Y T || || ( Y = the contravariant Yoneda functor) L � T S ⊥ R

  22. � � � � � Diagrammatic logic A diagrammatic logic is a functor L L � T S with a full and faithful right adjoint R ⊥ R induced by a morphism of limit sketches ( S and T are locally presentable categories) e E S E T Y S Y T || || ( Y = the contravariant Yoneda functor) L � T S ⊥ Ex. Monadic equational logic R ◮ S : “linear” sketches (= graphs with some composition) ◮ T : categories Ex. Equational logic ◮ S : finite product sketches ◮ T : categories with finite products

  23. � Models ◮ T : category of theories ◮ S : category of specifications ◮ Σ is a presentation of L (Σ) for every specification Σ R full and faithful ⇐ ⇒ R (Θ) is a presentation of Θ for every theory Θ ◮ a model M of a specification Σ with values in a theory Θ is a morphism L Σ → Θ in T , i.e., a morphism Σ → R Θ in S L � T S ⊥ ∈ ∈ R � Θ Σ M Ex. Monadic equational logic with Θ set the category of sets ◮ Σ nat : N , z : 1 → N , s : N → N ◮ M nat : Σ nat → Θ set : M ( N ) = N , M ( z ) = 0, M ( s )( x ) = x + 1

  24. � � � � � � � � Inference rules [Gabriel-Zisman 1967] R is full and faithful ⇐ ⇒ (up to equiv.) L is a localization: L makes some morphisms in S invertible in T ◮ an entailment is a morphism τ in S with L τ invertible in T : τ Σ ′ Σ � � � ◮ an instance of Σ 0 in Σ is a cospan in S with τ an entailment: � Σ ′ σ τ Σ 0 Σ � � � ◮ an inference rule with hypothesis H and conclusion C is an instance of C in H : H ′ H C � � � f = g Ex. Substitution rule f ◦ h = g ◦ h ◮ H : f , g , h , f = g ◮ H ′ : f , g , h , f = g , f ◦ h = g ◦ h with H H ′ the inclusion � � � � H ′ such that a �→ f ◦ h , b �→ g ◦ h ◮ C : a , b , a = b with C

Recommend


More recommend