lecture 4 effect handlers
play

Lecture 4: Effect Handlers Gordon Plotkin Laboratory for the - PowerPoint PPT Presentation

Effect Deconstructors Concurrency Lecture 4: Effect Handlers Gordon Plotkin Laboratory for the Foundations of Computer Science, School of Informatics, University of Edinburgh 20th Estonian Winter School in Computer Science Plotkin Lecture 4:


  1. Effect Deconstructors Concurrency Lecture 4: Effect Handlers Gordon Plotkin Laboratory for the Foundations of Computer Science, School of Informatics, University of Edinburgh 20th Estonian Winter School in Computer Science Plotkin Lecture 4: Effect Handlers

  2. Effect Deconstructors Concurrency Outline Effect Deconstructors 1 Concurrency 2 Plotkin Lecture 4: Effect Handlers

  3. Effect Deconstructors Concurrency Outline Effect Deconstructors 1 Concurrency 2 Plotkin Lecture 4: Effect Handlers

  4. Effect Deconstructors Concurrency Simple exception handler The construct is: M σ handled with { raise e �→ N σ e } e ∈ E : σ assuming a finite set of exceptions E = def M [ [ exc ] ] This evidently does not arise from a definable 1 + | E | -ary operation using the exceptions theory. Even worse, it cannot be an operation of any algebraic theory. For suppose we have a suitable operation handle : ε ; 1 , exc say. Then, in general, we will not have: E [ handle ( M , x : exc . N ( x ))] = handle ( E [ M ] , x : exc . E [ N ( x )]) Plotkin Lecture 4: Effect Handlers

  5. Effect Deconstructors Concurrency Failure to be algebraic Take E = ( λ y : nat . raise e 1 )[ · ] , M = 3, and N ( x ) = raise e 2 , where e 1 � = e 2 . Then we have: | = E [ handle ( M , x : exc . N ( x ))] = def ( λ y : nat . raise e 1 ) handle ( 3 , x : exc . N ( x )) = ( λ y : nat . raise e 1 ) 3 = raise e 1 and: | = handle ( E [ M ] , x : exc . E [ N ( x )]) = handle (( λ y : nat . raise e 1 ) 3 , x : exc . ( λ y : nat . raise e 1 ) N ( x )) = handle ( raise e 1 , x : exc . ( λ y : nat . raise e 1 ) N ( x )) = ( λ y : nat . raise e 1 ) N ( e 1 ) = def ( λ y : nat . raise e 1 ) raise e 2 = raise e 2 and the two are different. Plotkin Lecture 4: Effect Handlers

  6. Effect Deconstructors Concurrency Understanding the Benton and Kennedy exception handler algebraically Simple exception handler M σ + E handled with { raise e �→ N σ + E } e ∈ E : σ + E e with E finite. (We are mixing syntax and semantics.) Benton and Kennedy exception handler M σ + E handled with { raise e �→ N A e } e ∈ E to x : σ in N ( x ) : A Analysis of the semantics of the BK exception handler M ∈ T Ax ( σ ) = σ + E . { raise e �→ N A e } e ∈ E specifies a model of Ax with carrier A (any algebra is!). x : σ. N ( x ) σ − − − − − → A The semantics of the BK exception handler is (that of) ( x : σ. N ( x )) † ( M ) Plotkin Lecture 4: Effect Handlers

  7. Effect Deconstructors Concurrency The general algebraic situation The free model principle, that, for any algebra A over A satisfying equational axioms Ax , and for any f : X → A there exists a unique homomorphism f † : T Ax ( X ) → A such that the following diagram commutes X η f ✲ f † ❄ ✲ A T Ax ( X ) suggests a syntax for, and an interpretation of, effect deconstructors . Continuing to mix syntax and semantics, we write: M T Ax ( X ) handled with A to x : X in N ( x ) : A Plotkin Lecture 4: Effect Handlers

  8. Effect Deconstructors Concurrency Additions to the λ -calculus: Handlers for simple operations Handlers H ::= { op ( k 1 : T τ, . . . , k n : T τ ) = B op } op : n where T ( τ ) = def unit → τ Γ , k 1 : T τ, . . . , k n : T τ ⊢ B op : τ ( i = 1 , n ) Γ ⊢ { op ( k 1 : T τ, . . . , k n : T τ ) = B op } op : n : τ handler Handling M ::= M handled with H to x : σ in N Γ ⊢ M : σ Γ ⊢ H : τ handler Γ , x : σ ⊢ N : τ Γ ⊢ M handled with H to x : σ in N : τ Warning!! Not all handlers are correct , i.e., define models, so semantics of a λ -calculus term may not be defined. Plotkin Lecture 4: Effect Handlers

  9. Effect Deconstructors Concurrency How the handlers work Suppose H ::= { op ( k 1 : T τ, . . . , k n : T τ ) = B op ( k 1 , . . . , k n ) } op : n then x : σ ⊢ x handled with H to x : σ in N = N and ⊢ op ( M 1 , . . . , M n ) handled with H to x : σ in N = B op ( K 1 , . . . , K n ) where K i = [ M i handled with H to x : σ in N ] (and [ M ] = λ x : unit . M ) Plotkin Lecture 4: Effect Handlers

  10. Effect Deconstructors Concurrency An example: changing the contents of a read-only memory, holding a boolean Assume there is only one location, storing booleans. The handler A “ temporary state" handler H ro is given by: b : bool ⊢ { lookup ( k 1 : T ( τ ) , k 2 : T ( τ )) = if b then k 1 ( ∗ ) else k 2 ( ∗ ) } : τ handler Handling To evaluate a computation ⊢ M : σ , continuing with x : σ ⊢ N : τ and forcing any lookup ’s to give a value b we use: b : bool ⊢ M handled with H ro to x : σ in N : τ One may prefer a syntax allowing parametric handlers parameterised on arbitrary types. Plotkin Lecture 4: Effect Handlers

  11. Effect Deconstructors Concurrency How this handler works The handler H ro is: { lookup ( k 1 : T ( τ ) , k 2 : T ( τ )) = if b then k 1 ( ∗ ) else k 2 ( ∗ ) } It works as follows: ⊢ lookup ( M 1 , M 2 ) handled with H ro to x : σ in N = if b then [ M 1 handled with H ro to x : σ in N ]( ∗ ) else [ M 2 handled with H ro to x : σ in N ]( ∗ ) = if b then M 1 handled with H ro to x : σ in N else M 2 handled with H ro to x : σ in N Plotkin Lecture 4: Effect Handlers

  12. Effect Deconstructors Concurrency Additions to the λ -calculus: General operations Handlers H ::= { op x : s ( k 1 : s 1 → τ, . . . , k n : s n → τ ) = B op } op : s ; s 1 ,..., s m Γ , x : s , k 1 : s 1 → τ, . . . , k n : s n → τ ⊢ B op : τ ( i = 1 , n ) Γ ⊢ { op x : s ( k 1 : s 1 → τ, . . . , k n : s n → τ ) = B op } op : s ; s 1 ,..., s m : τ handler Plotkin Lecture 4: Effect Handlers

  13. Effect Deconstructors Concurrency How these handlers work Suppose H is { op x : s ( k 1 : s 1 → τ, . . . , k n : s n → τ ) = B op ( x , k 1 , . . . , k n ) } op : s ; s 1 ,..., s m then ⊢ op A ( x 1 : s 1 . M 1 , . . . , x n : s n . M n ) handled with H to x : σ in N = let x : s be A in B op ( x , K 1 , . . . , K n ) where K i = λ x : s . M i handled with H to x : σ in N Plotkin Lecture 4: Effect Handlers

  14. Effect Deconstructors Concurrency An example: rollback When a computation raises an exception while modifying the memory, e.g., when a connection drops during a database transaction, we may want to revert all modifications made during the computation. This behaviour is termed rollback. Signature The (disjoint) union of that for (global) state and exceptions. Axioms The union of the two sets of equations for global state and for exceptions, together with two commutation equations: lookup l ( m : nat . raise e ) = raise e update l , v ( raise e ) = raise e of which the first is redundant. Monad T ( X ) = (( S × X ) + E ) S Plotkin Lecture 4: Effect Handlers

  15. Effect Deconstructors Concurrency Exception handler for rollback Assume there is only one location l 0 . The handler A “rollback to n " handler H rollback is given by: n : nat ⊢ raise e : exc = update l 0 , n ( N raise ( e )) : τ handler where e : exc ⊢ N raise : τ . Handling To evaluate a computation ⊢ M : σ , continuing with x : σ ⊢ N : τ if no exception is raised, and otherwise rolling back to the initial state and executing N raise with the exception raised, we use: ⊢ lookup l 0 ( n : nat . M handled with H rollback to x : σ in N )) : τ Note: One again may prefer a syntax allowing parametric handlers parameterised on arbitrary types. In the above one would then take n as a parameter, rather than a free variable, and replace N raise by a parameter of type exc → τ . Plotkin Lecture 4: Effect Handlers

  16. Effect Deconstructors Concurrency Additions to the λ -calculus: Handlers with parameters for simple operations Handlers H ::= { op ( k 1 : π → τ, . . . , k n , : π → τ ) @ p : π = B op } op : n Γ , k 1 : π → τ, . . . , k n : π → τ, p : π ⊢ B op : τ ( op : n ) Γ ⊢ { op ( k 1 : π → τ, . . . , k n : π → τ ) @ p : π = B op } op : n : π → τ handler Handling M ::= M handled with H @ P to x : σ in N Γ ⊢ M : σ Γ ⊢ H : π → τ handler Γ ⊢ P : π Γ , x : σ ⊢ N : τ Γ ⊢ M handled with H @ P to x : σ in N : τ Plotkin Lecture 4: Effect Handlers

  17. Effect Deconstructors Concurrency How parameterised simple handlers work Suppose H is { op ( k 1 : π → τ, . . . , k n , : π → τ ) @ p : π = B op ( p , k 1 , . . . , k n ) } op : n then ⊢ op ( M 1 , . . . , M n ) handled with H @ P to x : σ in N = let p : π be P in B op ( p , K 1 , . . . , K n ) where K i = λ p : π. M i handled with H @ p to x : σ in N Plotkin Lecture 4: Effect Handlers

  18. Effect Deconstructors Concurrency A parameterised handler example: changing the contents of a boolean read-only memory Assume there is only one location, storing booleans. The handler The handler H ro is (now): { lookup ( k 1 : bool → τ, k 2 : bool → τ ) @ b : bool = if b then k 1 ( b ) else k 2 ( b ) } : bool → τ handler Handling To evaluate a computation ⊢ M : σ , continuing with x : σ ⊢ N : τ and forcing any lookup ’s to give a value P we use: ⊢ M handled with H ro @ P to x : σ in N : τ Plotkin Lecture 4: Effect Handlers

  19. Effect Deconstructors Concurrency How this handler works The handler H ro is: { lookup ( k 1 : bool → τ, k 2 : bool → τ ) @ b : bool = if b then k 1 ( b ) else k 2 ( b ) } It works as follows: ⊢ lookup ( M 1 , M 2 ) handled with H ro @ true to x : σ in N = if true then ( λ b : bool . M 1 handled with H ro @ b to x : σ in N )( true ) else ( λ b : bool . M 2 handled with H ro @ b to x : σ in N )( true ) = M 1 handled with H ro @ true to x : σ in N Plotkin Lecture 4: Effect Handlers

Recommend


More recommend