relational semantics for effect based program
play

Relational semantics for effect-based program transformations: - PowerPoint PPT Presentation

Relational semantics for effect-based program transformations: higher-order store Martin Hofmann Ludwig-Maximilians-Universit at M unchen IFIP Working Group 2.8, June 2009 mh (lmumun) Relational semantics for effects IFIP 2.8 1 / 22


  1. Relational semantics for effect-based program transformations: higher-order store Martin Hofmann Ludwig-Maximilians-Universit¨ at M¨ unchen IFIP Working Group 2.8, June 2009 mh (lmumun) Relational semantics for effects IFIP 2.8 1 / 22

  2. Effect-dependent program equivalences x = e ; y = e ; e ′ ( x , y ) x = e ; e ′ ( x , x ) is equivalent to provided that x , y are fresh and e ’s reads and writes are disjoint and e does not allocate, or none of the above, but somehow e ′ doesn’t care. mh (lmumun) Relational semantics for effects IFIP 2.8 2 / 22

  3. Effect-dependent program equivalences x = e ; y = e ; e ′ ( x , y ) x = e ; e ′ ( x , x ) is equivalent to provided that x , y are fresh and e ’s reads and writes are disjoint and e does not allocate, or none of the above, but somehow e ′ doesn’t care. Ongoing research programme: Justify such conditional equivalences by interpreting effectful types as relations (“logical relation”) Global integer references (APLAS06) Dynamically allocated integer references with regions (PPDP07) Ultimate goal: Dynamically allocated references of arbitrary type. Acknowledgements: Nick Benton, Lennart Beringer, Andrew Kennedy (collaborators) MOBIUS (IST-FET-15905). mh (lmumun) Relational semantics for effects IFIP 2.8 2 / 22

  4. This talk Global references of arbitrary (including functional) type Relational semantics requires solving mixed-variance equations. Existing solution theory found insufficient. Extension to solution theory Definition of logical relation that proves soundness of effect-dependent program equivalences Fly in the ointment: in latent effects of stored functions we cannot distinguish reading and writing. mh (lmumun) Relational semantics for effects IFIP 2.8 3 / 22

  5. Syntax x | n | true | false | x 1 op x 2 | () | ( x 1 , x 2 ) | x . 1 | e ::= x . 2 | x 1 x 2 | let x ⇐ e 1 in e 2 | ! ℓ | ℓ := x | if x then e 2 else e 3 | rec f x . e | λ x . e In examples we use ML notation such as this val f = fn g => fn n => if n=0 then 1 else n * g (n-1); val r = ref (fn x => 0); val fac = fn n => (r := (fn x => f (!r) x); !r n); mh (lmumun) Relational semantics for effects IFIP 2.8 4 / 22

  6. Denotational semantics ∼ V { wrong } + unit ( 1 ) + int ( Z ) + bool ( B ) + = pair ( V × V ) + fun ( V → C ) S → ( S × V ) ⊥ C = S = L → V V is the least predomain solving this. Predomain: CPO not nec. with ⊥ . NB C happens to have least element λ x . ⊥ . We have retracts p i : ♠ → ♠ where ♠ ∈ { V , S , C } . mh (lmumun) Relational semantics for effects IFIP 2.8 5 / 22

  7. Properties of the retracts p i ( wrong ) = wrong p i ( int ( n )) = int ( n ) p i ( unit ()) = unit () p i ( bool ( x )) = bool ( x ) p i ( pair ( v 1 , v 2 )) = pair ( p i ( v 1 ) , p i ( v 2 )) p i ( fun ( f )) = fun ( p i ; f ; p i ) p 0 ( f )( s ) = ⊥ p i +1 ( f )( s ) = ⊥ if f ( p i ( s )) = ⊥ p i +1 ( f )( s ) = ( p i ( s 1 ) , p i ( v )) if f ( p i ( s )) = ( s 1 , v ) p i ( s )( ℓ ) = p i ( s ( ℓ )) Moreover, p i ⊑ p i +1 and p i ; p j = p min( i , j ) and � i p i ( x ) = x for all x ∈ V ∪ S ∪ C . Useful for proving properties/defining functions over V . mh (lmumun) Relational semantics for effects IFIP 2.8 6 / 22

  8. Semantics of untyped language � e � θ ∈ C when θ : FV ( e ) → V � x � θ s = ( s , θ ( x )) � x y � θ s = f ( θ ( y )) s where θ ( x ) = fun ( f ) � let x ⇐ e 1 in e 2 � θ s = � e 2 � θ [ x �→ v ] s 1 when � e 1 � θ s = ( s 1 , v ) � if x then e 2 else e 3 � θ = � e 2 � θ , when θ ( x ) = bool ( true ) � ! ℓ � θ s = ( s , s .ℓ ) � ℓ := y � θ s = ( s [ ℓ �→ θ ( y )] , unit ()) � rec f x . e � θ s = ( s , fun ( g )) where g = � i g i and g 0 = λ x .λ s . ⊥ and g i +1 = λ v . � e � θ [ x �→ v , f �→ fun ( g i )] � λ x . e � θ s = ( s , fun ( f )) where f v = � e � θ [ x �→ v ] � e � θ s = wrong , if no clause applies mh (lmumun) Relational semantics for effects IFIP 2.8 7 / 22

  9. Types Effects ( ε ): Finite subsets of { rd ℓ , wr ℓ | ℓ ∈ L } . Types: ε int | unit | bool | A × B | A → B A , B , C ::= Store type (Σ): ℓ 1 : A 1 , . . . , ℓ n : A n . Typing context (Θ): x 1 : A 1 , . . . , x m : A m . Typing judgement: Π; Σ; Θ ⊢ e : A , ε . Here Π ⊆ L , all ℓ appearing in jugement are listed in Π. mh (lmumun) Relational semantics for effects IFIP 2.8 8 / 22

  10. Typing rules Π; Σ; Θ ⊢ n : int ( t-int ) x ∈ dom (Θ) Π ⊢ Θ ok ( t-var ) Π; Σ; Θ ⊢ x : Θ( x ) Π; Σ; Θ Π; Σ; Θ ⊢ ! ℓ : Σ( ℓ ) , { rd ℓ } ( t-read ) Π; Σ; Θ ⊢ y : Σ( ℓ ) Π; Σ; Θ ⊢ ℓ := y : unit , { wr ℓ } ( t-write ) Π; Σ; Θ ⊢ e : A , ε 1 ε 1 ⊆ ε 2 A < : B ( t-sub ) Π; Σ; Θ ⊢ e : B , ε 2 ε Π; Σ; Θ ⊢ x : A → B Π; Σ; Θ ⊢ y : A ( t-app ) Π; Σ; Θ ⊢ x y : B , ε mh (lmumun) Relational semantics for effects IFIP 2.8 9 / 22

  11. Typing rules, cont’d Π; Σ; Θ , x : A ⊢ e : B , ε ( t-lam ) ε Π; Σ; Θ ⊢ λ x . e : A → B Π; Σ; Θ ⊢ x : bool Π; Σ; Θ ⊢ e 1 : A , ε Π; Σ; Θ ⊢ e 2 : A , ε ( t-if ) Π; Σ; Θ ⊢ if x then e 1 else e 2 : A , ε Π; Σ; Θ ⊢ e 1 : A 1 , ε 1 Π; Σ; Θ , x : A 1 ⊢ e 2 : A 2 , ε 2 ( t-let ) Π; Σ; Θ ⊢ let x ⇐ e 1 in e 2 : A 2 , ε 1 ∪ ε 2 Π; Σ; Θ ⊢ x : A Π; Σ; Θ ⊢ y : B ( t-pair ) Π; Σ; Θ ⊢ ( x , y ) : A × B ε Π; Σ; Θ , f : A → B , x : A ⊢ e : B , ε ( t-rec ) ε Π; Σ; Θ ⊢ rec f x . e : A → B mh (lmumun) Relational semantics for effects IFIP 2.8 10 / 22

  12. Subtyping A < : A ( s-refl ) A 1 < : A 2 B 1 < : B 2 ( s-prod ) A 1 × B 1 < : A 2 × B 2 A 2 < : A 1 B 1 < : B 2 ε 1 ⊆ ε 2 ( s-arr ) ε 1 ε 2 A 1 → B 1 < : A 2 → B 2 mh (lmumun) Relational semantics for effects IFIP 2.8 11 / 22

  13. Example again val f = fn g => fn n => if n=0 then 1 else n * g (n-1); val r = ref (fn x => 0); val fac = fn n => (r := (fn x => f (!r) x); !r n); r ; r : int rd r → int ; ∅ ⊢ f : ( int rd r → int ) → int rd r → int rd r , wr r r ; r : int rd r → int ; ∅ ⊢ fac : int → int . More examples: Vector multiplication, event handling. mh (lmumun) Relational semantics for effects IFIP 2.8 12 / 22

  14. Equational theory ∀ θ. � e 1 � θ = � e 2 � θ Π; Σ; Θ ⊢ e i : A , ε ( e-basic ) Π; Σ; Θ ⊢ e 1 = e 2 : A , ε Sym,Trans,Cong. Π; Σ; Θ ⊢ e : A , ε rds ( ε ) ∩ wrs ( ε ) = ∅ x / ∈ dom (Θ) ( e-dup ) Π; Σ; Θ ⊢ let x ⇐ e in pair ( x , x ) = let x ⇐ e in let y ⇐ e in pair ( x , y ) : A × A , ε mh (lmumun) Relational semantics for effects IFIP 2.8 13 / 22

  15. Typing rules cont’d Π; Σ; Θ ⊢ e i : A i , ε i ∀ i = 1 , 2 . rds ( ε i ) ∩ wrs ( ε 3 − i ) = ∅ wrs ( ε i ) ∩ wrs ( ε 3 − i ) = ∅ x i ∩ ( dom (Θ) ∪ { x 3 − i } ) = ∅ ( e-swap ) Π; Σ; Θ ⊢ let x 1 ⇐ e 1 in let x 2 ⇐ e 2 in pair ( x 1 , x 2 ) = let x 2 ⇐ e 2 in let x 1 ⇐ e 1 in pair ( x 1 , x 2 ) : A 1 × A 2 , ε 1 ∪ ε 2 Π; Σ; Θ ⊢ e 1 : A , ∅ Π; Σ; Θ , x : A , y : B ⊢ e 2 : C , ε x � = y ( e-hoist ) Π; Σ; Θ ⊢ let ⇐ e 1 in λ y : B . let x ⇐ e 1 in e 2 = ε let x ⇐ e 1 in λ y : B . e 2 : B → C , ∅ Goal: Semantic interpretation of eq.thy as logical relation. Justifies soundness eq.thy for obs.eq. Allows for semantic reasoning (justify obs.eq using the log.rel rather than rules) mh (lmumun) Relational semantics for effects IFIP 2.8 14 / 22

  16. The logical relation Define � Π; Σ ⊢ A � ⊆ V × V � Π; Σ ⊢ A , ε � ⊆ C × C � Π; Σ ⊢ ε � ⊆ sets of relations on S � Π; Σ ⊢ A , ε � = per ( T O E ( A )) ⇒ ∀ s s ′ s 1 s ′ 1 v v ′ . ∀ R ∈ E . ( sRs ′ ⇒ ( f , f ′ ) ∈ T O E ( A ) ⇐ ( f s = ⊥ ⇔ f ′ s ′ = ⊥ ) ∧ (( f s ) = ( s 1 , v ) ∧ ( f ′ s ′ ) = ( s ′ 1 , v ′ ) ⇒ s 1 Rs ′ 1 ∧ ( v , v ′ ) ∈ � Π; Σ ⊢ A � ) mh (lmumun) Relational semantics for effects IFIP 2.8 15 / 22

  17. Logical relation cont’d � Π; Σ ⊢ unit � = Unit � Π; Σ ⊢ int � = Int � Π; Σ ⊢ bool � = Bool � Π; Σ ⊢ A × B � = Prod � Π; Σ ⊢ A � , � Π; Σ ⊢ B � ε � Π; Σ ⊢ A → B � = Arr � Π; Σ ⊢ A � , � Π; Σ ⊢ B , ε � ) Problem: It is not clear whether � . . . � satisfying these exists! mh (lmumun) Relational semantics for effects IFIP 2.8 16 / 22

  18. Logical relation cont’d � Π; Σ ⊢ unit � = Unit � Π; Σ ⊢ int � = Int � Π; Σ ⊢ bool � = Bool � Π; Σ ⊢ A × B � = Prod � Π; Σ ⊢ A � , � Π; Σ ⊢ B � ε � Π; Σ ⊢ A → B � = Arr � Π; Σ ⊢ A � , � Π; Σ ⊢ B , ε � ) Problem: It is not clear whether � . . . � satisfying these exists! We can show existence for a special case: latent effects of stored functions “storable”, i.e. both rd ℓ , wr ℓ or ℓ not mentioned at all. mh (lmumun) Relational semantics for effects IFIP 2.8 16 / 22

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