refinement types for algebraic effects
play

Refinement Types for Algebraic Effects Danel Ahman Gordon Plotkin - PowerPoint PPT Presentation

Refinement Types for Algebraic Effects Danel Ahman Gordon Plotkin LFCS, University of Edinburgh TYPES 2015 Tallinn Todays plan Value ref. types Computation ref. types Algebraic theories Value


  1. Refinement Types for Algebraic Effects Danel Ahman Gordon Plotkin LFCS, University of Edinburgh TYPES 2015 Tallinn

  2. � � � � � � � � Today’s plan Value ref. types Computation ref. types Algebraic theories Value types Computation types + some examples

  3. Refinement types For extending base language’s type system to allow more precise specifications in types ⊢ Odd : Ref(Nat) ⊢ Even : Ref(Nat) make it possible to internalize meta-theorems n : Odd , m : Odd ⊢ n + m : Even and also program optimizations In this talk, we discuss propositional ref. types for example Even and Odd, as above [Freeman, Pfenning ’91] Ideas also apply to FOL-based ref. types [Denney ’98] for example { x : σ | ϕ ( x ) } but with some additional technical challenges

  4. Computational effects Ever-present in the various languages we work with regardless of being lazy, strict, object-oriented, ... First unifying account using monads, e.g.: [Moggi ’89] non-determinism: T X = P + fin ( X ) read-only memory: T X = S → X write-only memory: T X = M × X ( M a monoid ) read-write memory / global state: T X = S → ( S × X ) And also more recent generalizations from TYPES ’13 [Ahman, Uustalu ’14] update monads: T X = S → ( P × X ) ( P a monoid ) dep. typed update monads: T X = Π s : S . ( P s × X ) ( where ( S , ↓ , P , o , ⊕ ) a directed container )

  5. Refinement types for computational effects Plenty of work in the literature that either target particular computational effects, or cover particular kinds of specifications For example: { P } σ { Q } pre- and postconditions for state Hoare Type Theory [Nanevski et. al. ’08] Refined state monad in F7 [Borgstr¨ om et. al. ’11] Dijkstra Monad in F* [Swamy et. al. ’13] sessions and protocols !Bool . ?Nat . S for I/O trace effects [Skalka, Smith, van Horn ’08] session typed languages [Honda ’93] [and many others] effect annotations ε in type-and-effect systems sets of operation symbols [Kammar, Plotkin ’12] ordered monoids [Katsumata ’14]

  6. Computational effects, algebraically Take algebraic theories as a primitive, rather than the monads they generate [Plotkin,Power ’02] in this talk: “standard” n -ary operations op : n not in this talk: operations with parameters and binding For example: non-determinism: T X = P + fin ( X ) x or x = x x or y = y or x x or ( y or z ) = ( x or y ) or z state: T X = 2 → (2 × X ) (where S = 2) lkp(upd 0 ( x ) , upd 1 ( x )) = x upd i (upd j ( x )) = upd j ( x ) upd i (lkp( x 0 , x 1 )) = upd i ( x i )

  7. Effectful programs as computation trees Algebraic modeling of effects is somewhat eyeopening Immediately allows to think of programs such as let f = λ b : bool . return ¬ b in let x = lkp in let y = f x in let = output y in = if x = 1 then upd y in let return y as computation trees lkp output 1 output 0 1 upd 0 0

  8. Ref. types for algebraic effects Reason about effectful programs as if they would simply be comp. trees built from operations Would like to build: single trees from operations combine them into finite and infinite sets of trees with clean and finite syntax Define effect refinements, based on modal formulae ψ ::= [ ] | � op � ( ψ 1 , . . . , ψ n ) | ⊥ | ψ 1 ∨ ψ 2 | X | µ X .ψ where “holes” [ ] are placeholders for leaves op. modalities � op � are used to build trees from ops. Note: effect refs. are indifferent wrt. specific algebras

  9. Ref. types for algebraic effects Think effect refinements as a small logic on comp. trees ψ ::= [ ] | � op � ( ψ 1 , . . . , ψ n ) | ⊥ | ψ 1 ∨ ψ 2 | X | µ X .ψ They also come with a satisfiability / subtyping relation ∆ ⊢ ψ 1 ⊑ ψ 2 ⊑ includes standard logic also want ⊑ to include algebraic properties of � op � ’s can’t just include all the axioms, e.g., ψ = � lkp � ( ψ, ψ ) [Gautam ’57] need to include derivable semi-linear equations x ⊢ t = u derivable in T eff � t linear in � Vars ( u ) ⊆ Vars ( t ) x ∆ ⊢ ψ 1 . . . ∆ ⊢ ψ n ∆ ⊢ t • [ � x ] ⊑ u • [ � ψ/� ψ/� x ]

  10. � � � � � � About the semantics of effect refinements Recall: effect refs. are indifferent wrt. specific algebras Concretely, they can be interpreted as monotone maps � ∆ ⊢ ψ � A : P (U A ) × � ∆ � A − → P (U A ) (the first argument corresponds to holes [ ]) More abstractly, we interpret them as functors on fibres � ∆ ⊢ ψ � A : RefAlg A × � ∆ � A − → RefAlg A where RefAlg results from change-of-base situation in ˆ F RefAlg R ⊥ � ˆ U r U ∗ ( r ) F V Alg ⊥ U When ⊢ ψ then we have � ⊢ ψ � : RefAlg − → RefAlg

  11. Adding ref. types to effectful languages Fairly straightforward to add them to effectful languages, e.g., FGCBV or CBPV: [Levy et. al. ’03] [Levy ’04] For example, CBPV types A ::= b | 1 | 0 | A 1 × A 2 | A 1 + A 2 | U C C ::= F A | 1 | C 1 × C 2 | A − → C turn into ref. types inspired by effect refinements σ ::= b | 1 | 0 | σ 1 × σ 2 | σ 1 + σ 2 | ˆ U τ | σ 1 ∨ σ 2 | ⊥ A τ ::= ˆ F σ | 1 | τ 1 × τ 2 | σ − → τ | � op � C ( τ 1 , . . . , τ n ) | X | µ X . τ | τ 1 ∨ τ 2 | ⊥ C With the accompanying subtyping relations ∆ ⊢ σ 1 ⊑ A σ 2 ∆ ⊢ τ 1 ⊑ C τ 2 extended with rules for subtyping effect refinements

  12. Adding ref. types to effectful languages The term syntax is as in CBPV ::= x | � V 1 , V 2 � | . . . V ::= return V | M 1 to x : σ in M 2 | . . . M The typing judgments for CBPV become Γ ⊢ v V : σ Γ ⊢ c M : τ with the typing rules modified accordingly, e.g.: Γ ⊢ v V : σ Γ ⊢ c M 1 : τ 1 . . . Γ ⊢ c M n : τ n c return V : ˆ Γ ⊢ F σ Γ ⊢ c op( M 1 , . . . , M n ) : � op � C ( τ 1 , . . . , τ n ) c M 1 : ψ [ˆ Γ ⊢ Γ , x : σ ⊢ F σ ] c M 2 : τ Γ ⊢ c M 1 to x : σ in M 2 : ψ [ τ ] where ψ [ τ ] denotes “filling” of holes [ ] in ψ with τ

  13. � � � � � � About the semantics of ref. typed CBPV Recall the picture for interpreting effect refs. ˆ F R RefAlg ⊥ � ˆ U U ∗ ( r ) r F Alg V ⊥ U Assume r to have suitable structure for types Ref. typed CBPV interpreted in the total categories: � ⊢ σ : Ref( A ) � ∈ obj ( R ) such that r ( � σ � ) = � A � � ⊢ τ : Ref( C ) � ∈ obj (RefAlg) such that U ∗ ( r )( � τ � ) = � C � � Γ ⊢ v V : σ � : � Γ � − → � σ � → (ˆ � Γ ⊢ c M : τ � : � Γ � − U ◦ � ψ � )( � τ � )

  14. Applications: Type-and-effect systems Effect annotations ε in effect-and-type systems usually consist of sets of operation / effect symbols To represent type-and-effect systems in our system, we define effect refinements ψ ε by def � = µ X . [ ] ∨ � op � ( X , . . . , X ) ψ ε op: n ∈ ε So we can talk of effect-and-type judgements Γ ⊢ M : σ ! ε as ref. typed judgements c M : ψ ε [ˆ Γ ⊢ F σ ]

  15. Applications: Optimizations With a PER-based semantics also possible to validate effect-dependent optimizations [Benton et. al. ’06-’09] [Kammar, Plotkin ’12] For example: discard t ( x , . . . , x ) = x in T eff for all ψ -terms c M : ψ [ˆ Γ ⊢ F σ ] Γ ⊢ c N : τ Γ ⊢ c M to x : σ in N = N : ψ [ τ ] copy t ( t ( x 11 , . . . , x 1 n ) , . . . , t ( x n 1 , . . . , x nn )) = t ( x 11 , . . . , x nn ) for all ψ -terms c M : ψ [ˆ Γ ⊢ F σ ] Γ , x : σ, y : σ ⊢ c N : τ Γ ⊢ c M to x : σ in ( M to y : σ in N ) = M to x : σ in N [ x / y ] : ψ [ τ ]

  16. Applications: Optimizations But we can also validate more involved optimizations effect refs. contain more temporal information Dead code elimination in stateful computation � � Γ ⊢ c M : ψ � upd l , 0 � ([ τ ]) ∨ � upd l , 1 � ([ τ ]) � lkp l � �∈ ψ � � �� Γ ⊢ c upd l , i ( M ) = M : � upd l , i � � upd l , 0 � ([ τ ]) ∨ � upd l , 1 � ([ τ ]) ψ Plus various other patterns describing how write- and read-information propagates through the terms

  17. Applications: Hoare Logic Pre- and post-conditions on state turn out to be yet another example of formulae on computation trees Lack of value parameters ⇒ combinatorial definition Take the predicates on state to be P , Q ⊆ { 0 , 1 } Hoare refinement { P } σ { Q } defined by case analysis on P def i � upd i � ([ˆ j � upd j � ([ˆ {∅} σ { Q } � lkp � ( � F σ ]) , � = F σ ])) def q � upd q � ([ˆ j � upd j � ([ˆ {{ 0 }} σ { Q } � lkp � ( � F σ ]) , � = F σ ])) def i � upd i � ([ˆ q � upd q � ([ˆ � lkp � ( � F σ ]) , � {{ 1 }} σ { Q } = F σ ])) def q � upd q � ([ˆ q ′ � upd q ′ � ([ˆ � lkp � ( � F σ ]) , � {{ 0 , 1 }} σ { Q } = F σ ])) where i , j ∈ { 0 , 1 } and q , q ′ ∈ Q

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