monadic reflection in lax logic
play

Monadic reflection in Lax Logic Tristan Crolard LACL University of - PowerPoint PPT Presentation

Monadic reflection in Lax Logic Tristan Crolard LACL University of East Paris TPDC11 Novi Sad May 29, 2011 Introduction Revisit Representing monads [Filinski, 1994] from a logical standpoint. Goal: understand the


  1. Monadic reflection in Lax Logic Tristan Crolard LACL – University of East Paris TPDC’11 Novi Sad – May 29, 2011

  2. Introduction • Revisit “ Representing monads ” [Filinski, 1994] from a logical standpoint. • Goal: understand the logical meaning of shift / reset in the restricted framework of a major application, i.e. implementing monadic reflection. • Through the formulas-as-types interpretation, a monad ♦ ⊔ corresponds to the modality from Lax logic [Curry, 1952]: ⊢ unit : ϕ ⇒ ♦ ϕ ⊢ bind : ( ϕ ⇒ ♦ ψ ) ⇒ ♦ ϕ ⇒ ♦ ψ • Monadic reflection is given by these logical rules: Γ ⊢ t : ϕ Γ ⊢ t : ♦ ϕ [ reify ] [ reflect ] Γ ⊢ [ t ] : ♦ ϕ Γ ⊢ µ ( t ) : ϕ • In this talk, we consider only provability .

  3. Example: the exception monad � f a | ( inr b ) � inr b . : type . ε ♦ ϕ = ϕ ∨ ε . = λa : ϕ. inl a . unit = λt. case t of ( inl a ) bind f � h � a | ( inr e ) � h . Defining raise and handle in direct style: raise = µ ( inr t ) . t handle e = case [ t ] of ( inl a )

  4. Moggi’s monadic translation (CBV) Translation of types ( σ atomic): σ ♦ ≡ σ • ( ϕ ⇒ ψ ) ♦ ≡ ϕ ♦ ⇒ ♦ ψ ♦ • ( ♦ ψ ) ♦ ≡ ♦ ϕ ♦ • Translation of terms: x ♦ ≡ unit x • ( λx.t ) ♦ ≡ unit λx.t ♦ • ( t 1 t 2 ) ♦ ≡ bind ♦ �� ♦ � � • bind f t 2 λf. t 1 µ ( t ) ♦ ≡ bind id t ♦ • [ t ] ♦ ≡ unit t ♦ • Lemma. If Γ ⊢ t : ϕ is derivable then Γ ♦ ⊢ t ♦ : ♦ ϕ ♦ is derivable. Proof. Γ ♦ ⊢ t ♦ : ♦ ϕ ♦ Γ ♦ ⊢ t ♦ : ♦♦ ϕ ♦ Γ ♦ ⊢ unit t ♦ : ♦♦ ϕ ♦ [ unit ] Γ ♦ ⊢ bind id t ♦ : ♦ ϕ ♦ [ join ]

  5. Filinski’s CPS-translation (CBV) Define ∇ ϕ = ( ϕ ⇒♦ o ) ⇒♦ o where o is some universal answer type (not sound). Translation of types ( σ atomic): σ ∇ ≡ σ • ( ϕ ⇒ ψ ) ∇ ≡ ϕ ∇ ⇒ ∇ ψ ∇ • ( ♦ ψ ) ∇ ≡ ♦ ϕ ∇ • Translation of terms: x ∇ ≡ λk.k x • ( λx.t ) ∇ ≡ λk.k ( λx.t ∇ ) • ∇ ( λf.t 2 ∇ ( λa.f a k )) ( t 1 t 2 ) ∇ ≡ λk.t 1 • µ ( t ) ∇ ≡ λk.t ∇ ( bind k ) • [ t ] ∇ ≡ λk.k ( t ∇ unit ) •

  6. Delimited control Reflect/reify are definable in direct style from shift / reset [Filinski, 1994] = reset ( unit t ) . [ t ] µ ( t ) = shift ( λk. bind k t ) . That is, these equations are valid: ( reset ( unit t )) ∇ = [ t ] ∇ • ( shift ( λk. bind k t )) ∇ = µ ( t ) ∇ • where: reset ∇ = λm.λc. ( c ( m id )) • shift ∇ = λh.λc. ( h ( λv.λc ′ .c ′ ( c v )) id ) •

  7. Answer type polymorphism Footnote from “ Representing monads ”: “Alternatively, with a little more care, we can take ∇ ϕ = ∀ α ( ϕ →♦ α ) →♦ α ; it is straightforward to check that both the term translation and the operations defined in the following can in fact be typed according to this schema.” So, let us do it carefully : • Formalization in Twelf (work in progress) • Experimenting with TeXmacs as a front end (the Twelf source is generated from the slides)

  8. Plan of the rest of the talk • Formalize System F in Twelf • Check that the operations are well-typed in direct style • Check that the CPS-translations of the operations are well-typed • Interpret the logical type of shift for the usual monads: − continuation monad − state monad − exception monad

  9. System F (HOAS) Types : type . type ⊔ ⇒ ⊔ : type → type → type . � 2 ⊔ ∧ ⊔ : type → type → type . ⊔ ∨ ⊔ : type → type → type . ∀ ⊔ . ⊔ : ( type → type ) → type . binding 1 in ∀ ⊔ . ⊔ void = ∀ β.β .

  10. Terms : type . term � 3 Abstraction λ ⊔ : ⊔ . ⊔ : type → ( term → term ) → term . binding in 1 λ ⊔ : ⊔ . ⊔ Application : term → term → term . ⊔ ⊔ � 2 Polymorphic abstraction Λ ⊔ . ⊔ : ( type → term ) → term . binding 1 in Λ ⊔ . ⊔

  11. Instantiation ⊔ { ⊔ } : term → type → term . � 4 Derived syntax for let let ⊔ : ⊔ = ⊔ in ⊔ = [ τ ] [ u ] [ t ] ( λx : τ.t [ x ]) u . binding 1 in let ⊔ : ⊔ = ⊔ in ⊔ Pairing � ⊔ , ⊔ � : term → term → term . � 4 Pattern matching � 4 let � ⊔ , ⊔ � = ⊔ in ⊔ : term → ( term → term → term ) → term . binding 2 in let � ⊔ , ⊔ � = ⊔ in ⊔ binding 1 in let � ⊔ , ⊔ � = ⊔ in ⊔

  12. Injections inl ⊔ : term → term . � ⊔ | ( inr ⊔ ) � ⊔ inr ⊔ : term → term . � 5 � ⊔ | ( inr ⊔ ) � ⊔ Pattern matching � 3 � ⊔ | ( inr ⊔ ) � ⊔ case ⊔ of ( inl ⊔ ) : term → ( term → term ) → ( term → term ) → term . binding 4 in case ⊔ of ( inl ⊔ ) binding 2 in case ⊔ of ( inl ⊔ ) Monadic constants unit : term . bind : term . Delimited control operators reset : term . shift : term .

  13. Typing judgment ⊢ ⊔ : ⊔ : term → type → type . { x } ⊢ x : ϕ → ⊢ t [ x ] : ψ [ lam ] ⊢ λx : ϕ.t [ x ] : ϕ ⇒ ψ ⊢ t 1 : ϕ ⇒ ψ ⊢ t 2 : ϕ [ app ] ⊢ t 1 t 2 : ψ { α } ⊢ t [ α ] : ψ [ α ] [ abs ] ⊢ Λ α.t [ α ] : ∀ α.ψ [ α ] ⊢ t : ∀ α.ψ [ α ] [ inst ] ⊢ t { ϕ } : ψ [ ϕ ]

  14. Typing judgment ⊢ t 1 : ϕ ⊢ t 2 : ψ [ pair ] ⊢ � t 1 , t 2 � : ϕ ∧ ψ { x } ⊢ x : ϕ → ( { y } ⊢ y : ψ → ⊢ u [ x ][ y ] : τ ) ⊢ t : ϕ ∧ ψ [ match ] ⊢ let � x, y � = t in u [ x ][ y ] : τ ⊢ t : ψ [ inr ] ⊢ inr t : ϕ ∨ ψ � u 1 [ x ] | ( inr y ) � u 2 [ y ] : φ ⊢ t : ϕ [ inl ] ⊢ inl t : ϕ ∨ ψ { x } ⊢ x : ϕ → ⊢ u 1 [ x ] : φ { y } ⊢ y : ψ → ⊢ u 2 [ y ] : φ ⊢ t : ϕ ∨ ψ [ case ] ⊢ case t of ( inl x )

  15. Lax logic Primitive monad or lax modality [Curry, 1952] ♦ ⊔ : type → type . ⊢ unit : ϕ ⇒ ♦ ϕ [ unit ] ⊢ bind : ( ϕ ⇒ ♦ ψ ) ⇒ ♦ ϕ ⇒ ♦ ψ [ bind ]

  16. Delimited control Fixed answer type : type . o ⊢ reset : ♦ ϕ ⇒ ♦ ϕ [ reset ] ⊢ shift : (( ϕ ⇒ ♦ o ) ⇒ ♦ o ) ⇒ ϕ [ shift ]

  17. Monadic reflection Reflect/reify are definable from shift / reset : = reset ( unit t ) . [ t ] µ ( t ) = shift ( λk : ϕ ⇒ ♦ o. bind k t ) . Lemma. The following typing rules are derivable: ⊢ t : ϕ ⊢ t : ♦ ϕ [ reify ] [ reflect ] ⊢ [ t ] : ♦ ϕ ⊢ µ ( t ) : ϕ %solve ⊢ t : ϕ → ⊢ [ t ] : ♦ ϕ %solve ⊢ t : ♦ ϕ → ⊢ µ ( t ) : ϕ

  18. Polymorphic monadic reflection Polymorphic type for shift : ⊢ shift : ∀ α. (( ϕ ⇒ ♦ α ) ⇒ ♦ α ) ⇒ ϕ [ shift ] Reflect is still definable from shift : µ ( t ) = shift (Λ α.λk : ϕ ⇒ ♦ α. bind k t ) . Lemma. The following typing rule is derivable: ⊢ t : ♦ ϕ [ reflect ] ⊢ µ ( t ) : ϕ %solve ⊢ t : ♦ ϕ → ⊢ µ ( t ) : ϕ Remark. ∀ α. (( ϕ ⇒ ♦ α ) ⇒ ♦ α ) is equivalent to ♦ ϕ . %solve ⊢ t : ♦ ϕ → ⊢ Λ α.λk : ϕ ⇒ ♦ α. bind k t : ∀ α. (( ϕ ⇒ ♦ α ) ⇒ ♦ α ) %solve ⊢ f : ∀ α. (( ϕ ⇒ ♦ α ) ⇒ ♦ α ) → ⊢ ( f { ϕ } unit ) : ♦ ϕ

  19. � Derived typing rule for reify lemma ⊢ t : ϕ ⊢ [ t ] : ♦ ϕ � Proof. D of 1 [ unit ] � ⊢ t : ϕ ⊢ unit : ϕ ⇒ ♦ ϕ � [ reset ] [ app ] D of 1 ⊢ reset : ♦ ϕ ⇒ ♦ ϕ ⊢ unit t : ♦ ϕ [ app ] [&] � ⊢ t : ϕ ⊢ reset ( unit t ) : ♦ ϕ %mode + D of 1 − D of 2 %worlds () D of 1 D of 2 %total {} D of 1 D of 2

  20. � Derived typing rule for reflect lemma ⊢ t : ♦ ϕ ⊢ µ ( t ) : ϕ Proof. D of k [ bind ] ⊢ k : ϕ ⇒ ♦ α D of t � [ app ] ⊢ bind k : ♦ ϕ ⇒ ♦ α ⊢ t : ♦ ϕ [ D of k ] [ app ] ⊢ bind k t : ♦ α [ k ] ⊢ k : ϕ ⇒ ♦ α → ⊢ bind k t : ♦ α { k } ⊢ k : ϕ ⇒ ♦ α → ⊢ bind k t : ♦ α [ α ] [ lam ] ⊢ λk : ϕ ⇒ ♦ α. bind k t : ( ϕ ⇒ ♦ α ) ⇒ ♦ α [ shift ] � [ abs ] D of t ⊢ Λ α.λk : ϕ ⇒ ♦ α. bind k t : ∀ α. (( ϕ ⇒ ♦ α ) ⇒ ♦ α ) [ app ] [& � ⊢ t : ♦ ϕ ⊢ shift (Λ α.λk : ϕ ⇒ ♦ α. bind k t ) : ϕ � %mode + D of 1 − D of 2 %worlds () D of 1 D of 2 %total {} D of 1 D of 2

  21. Different continuation monads 1. Continuation monad ∇ ϕ = ( ϕ ⇒ o ) ⇒ o . 2. Modal continuation monad ∇ ϕ = ( ϕ ⇒ ♦ o ) ⇒ ♦ o . 3. Polymorphic continuation monad ∇ ϕ = ∀ α. ( ϕ ⇒ α ) ⇒ α . 4. Polymorphic modal continuation monad ∇ ϕ = ∀ α. ( ϕ ⇒ ♦ α ) ⇒ ♦ α . Remark. Cases 1 and 3 are obtained by taking ♦ as the identity monad.

  22. Modal continuation monad ∇ ϕ = ( ϕ ⇒ ♦ o ) ⇒ ♦ o . = λt : ϕ.λk : ϕ ⇒ ♦ o. ( k t ) . unit ∇ = λk : ϕ ⇒ ∇ ψ.λm : ∇ ϕ.λc : ψ ⇒ ♦ o.m ( λv : ϕ.k v c ) . bind ∇ %solve ⊢ unit ∇ : ϕ ⇒ ∇ ϕ %solve ⊢ bind ∇ : ( ϕ ⇒ ∇ ψ ) ⇒ ∇ ϕ ⇒ ∇ ψ

  23. Polymorphic modal continuation monad ∇ ϕ = ∀ α. ( ϕ ⇒ ♦ α ) ⇒ ♦ α . = λt : ϕ. Λ α.λk : ϕ ⇒ ♦ α. ( k t ) . unit ∇ = λm : ∇ ϕ.λk : ϕ ⇒ ∇ ψ. Λ α.λc : ψ ⇒ ♦ α.m { α } ( λv : ϕ. ( k v ) { α } c ) . bind ∇ %solve ⊢ unit ∇ : ϕ ⇒ ∇ ϕ %solve ⊢ bind ∇ : ∇ ϕ ⇒ ( ϕ ⇒ ∇ ψ ) ⇒ ∇ ψ

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend