lecture 1 algebraic effects i
play

Lecture 1: Algebraic Effects I Gordon Plotkin Laboratory for the - PowerPoint PPT Presentation

Moggis Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Lecture 1: Algebraic Effects I Gordon Plotkin Laboratory for the Foundations of Computer Science, School of Informatics, University of Edinburgh 20th


  1. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Lecture 1: Algebraic Effects I Gordon Plotkin Laboratory for the Foundations of Computer Science, School of Informatics, University of Edinburgh 20th Estonian Winter School in Computer Science Plotkin Lecture 1: Algebraic Effects I

  2. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Outline Moggi’s Monads As Notions of Computation 1 Algebraic Effects 2 Introduction Equational theories Finitary equational theories Algebraic operations and generic effects Prospectus and Exercises 3 Plotkin Lecture 1: Algebraic Effects I

  3. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Outline Moggi’s Monads As Notions of Computation 1 Algebraic Effects 2 Introduction Equational theories Finitary equational theories Algebraic operations and generic effects Prospectus and Exercises 3 Plotkin Lecture 1: Algebraic Effects I

  4. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises The typed λ -calculus: syntax Raw Syntax Types σ ::= b | σ → τ M ::= c | x | λ x : σ. M | MN Terms Typing Environments Γ ::= x 1 : σ 1 , . . . , x n : σ n Judgements Γ ⊢ M : σ Plotkin Lecture 1: Algebraic Effects I

  5. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises The typed λ -calculus: typing rules Variables x 1 : σ 1 , . . . , x n : σ n ⊢ x i : σ i ( 1 ≤ i ≤ n ) Constants Γ ⊢ c : σ ( as given ) Abstractions Γ , x : σ ⊢ M : τ Γ ⊢ λ x : σ. M : σ → τ Applications Γ ⊢ M : σ → τ Γ ⊢ N : σ Γ ⊢ MN : τ Plotkin Lecture 1: Algebraic Effects I

  6. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises The typed λ -calculus: semantics in Set Types [ [ σ ] ] ∈ Set Basic Types [ [ b ] ] = ( as given ) Function spaces [ [ σ → τ ] ] = [ [ σ ] ] ⇒ [ [ τ ] ] Environments [ [ x 1 : σ 1 , . . . , x n : σ n ] ] = [ [ σ 1 ] ] × . . . × [ [ σ n ] ] Plotkin Lecture 1: Algebraic Effects I

  7. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Semantics: very explicit Terms Γ ⊢ M : σ [ [ M ] ] : [ [Γ] ] → [ [ σ ] ] Variables [ [ x i ] ]( a 1 , . . . , a n ) = a i Constants [ [ c ] ]( a 1 , . . . , a n ) = ( as given ) Abstractions [ [ λ x : σ. M ] ]( a 1 , . . . , a n ) = a ∈ [ [ σ ] ] �→ [ [ M ] ]( a 1 , . . . , a n , a ) Applications [ [ MN ] ]( a 1 , . . . , a n ) = [ [ M ] ]( a 1 , . . . , a n )([ [ N ] ]( a 1 , . . . , a n )) Plotkin Lecture 1: Algebraic Effects I

  8. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Semantics of Exceptions Suppose programs can raise exceptions e ∈ E . Then we want: Γ ⊢ M : σ ] → ([ [ [ M ] ] : [ [Γ] [ σ ] ]+ E ) (Remember: X + Y = ( { 0 } × X ) ∪ ( { 1 } × Y )) Function spaces [ [ σ → τ ] ] = [ [ σ ] ] ⇒ ([ [ τ ] ]+ E ) Variables [ [ x i ] ]( a 1 , . . . , a n ) = inl ( a i ) = η ( a i ) Constants [ [ c ] ]( a 1 , . . . , a n ) = ( as given ) Plotkin Lecture 1: Algebraic Effects I

  9. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Semantics of Exceptions (cntnd.) Abstractions [ [ λ x : σ. M ] ]( a 1 , . . . , a n ) = η ( a ∈ [ [ σ ] ] �→ [ [ M ] ]( a 1 , . . . , a n , a )) Applications For M : σ → τ , N : σ [ [ MN ] ]( γ ) = E-ap ([ [ M ] ]( γ ) , [ [ N ] ]( γ )) where E-ap : ([ [ σ → τ ] ] + E ) × ([ [ σ ] ] + E ) − → ([ [ τ ] ] + E )  inr ( e ) ( if a = inr ( e ))  inr ( e ′ ) ( if a = inl ( f ) and b = inr ( e ′ ) E-ap ( a , b ) = f ( c ) ( if a = inl ( f ) and b = inr ( c )  Plotkin Lecture 1: Algebraic Effects I

  10. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Moggi’s insight: a categorical view of - + E Functorial action f : X → Y f + E : X + E → Y + E where: � inl ( f ( b )) ( if a = inl ( b )) ( f + E )( a ) = inr ( e )) ( if a = inr ( e )) Monadic structure Unit η : X → X + E µ : ( X + E ) + E → X + E Multiplication  inl ( e ) ( if a = inr ( e ))  inr ( e ′ ) ( if a = inl ( inr ( e ))) µ ( a ) = b ( if a = inl ( inl ( b ))  Plotkin Lecture 1: Algebraic Effects I

  11. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises A categorical view of - + E (cntnd.) Strength Left strength lst : X × ( Y + E ) − → ( X × Y ) + E Right strength rst : ( X + E ) × Y − → ( X × Y ) + E � inr ( e ) ( if b = inr ( e )) lst ( a , b ) = inl ( a , c ) ( if b = inl ( c )) Plotkin Lecture 1: Algebraic Effects I

  12. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Putting these together rst (( X ⇒ ( Y + E )) + E ) × ( X + E ) − → (( X ⇒ ( Y + E )) × ( X + E )) + E lst + E − − − → (( X ⇒ ( Y + E )) × X ) + E ) + E µ − → (( X ⇒ ( Y + E )) × X ) + E ap + E − − − → ( Y + E ) + E µ − → Y + E Plotkin Lecture 1: Algebraic Effects I

  13. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Strong monads Summarising, one needs a operator T ( X ) on Set , the category of sets, equipped with: T ( · ) A functorial action Set ( X , Y ) − − − − → Set ( T ( X ) , T ( Y )) This makes T a functor η X A unit X − − − → T ( X ) µ X A multiplication T ( T ( X )) − − − → T ( X ) These make T a monad st X , Y A (left) strength X × T ( Y ) − − − − → T ( X × Y ) This makes T a strong monad Note, can derive the right strength: st Y , X T ( twist ) twist T ( X ) × Y − − − → Y × T ( X ) − − − → T ( Y × X ) − − − − → T ( X × Y ) Plotkin Lecture 1: Algebraic Effects I

  14. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Moggi’s insight (cntnd.) Other computational effects can also be modelled by strong monads T , e.g. in Set : → ( S × Y ) S State S × X − → S × Y can be rewritten as X − and T state ( X ) = ( S × X ) S is a strong monad. Finite Nondeterminism T SL ( X ) = F + ( X ) the collection of non-empty finite subsets of X . Continuations T cont ( X ) = R R X . Functions X → T cont ( Y ) can be rewritten as R Y − → R X . and there are many other examples, including combinations, such as this for state plus exceptions: T ( X ) = ( S × ( X + E )) S In Cpo one has similar examples, generally including lifting to accommodate recursion, so that T ( P ) is a cppo, e.g., for state plus nontermination: T ( P ) = (( S × P ) ⊥ ) S Plotkin Lecture 1: Algebraic Effects I

  15. Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Semantics of application for any monad rst T ( X ⇒ T ( Y )) × T ( X ) − → T (( X ⇒ T ( Y )) × T ( X )) T ( lst ) − − − → T ( T (( X ⇒ T ( Y )) × X )) µ − → T (( X ⇒ T ( Y )) × X ) T ( ap ) − − − → T ( T ( Y )) µ − → T ( Y ) Plotkin Lecture 1: Algebraic Effects I

  16. Moggi’s Monads As Notions of Computation Introduction Algebraic Effects Equational theories Prospectus and Exercises Outline Moggi’s Monads As Notions of Computation 1 Algebraic Effects 2 Introduction Equational theories Finitary equational theories Algebraic operations and generic effects Prospectus and Exercises 3 Plotkin Lecture 1: Algebraic Effects I

  17. Moggi’s Monads As Notions of Computation Introduction Algebraic Effects Equational theories Prospectus and Exercises Outline Moggi’s Monads As Notions of Computation 1 Algebraic Effects 2 Introduction Equational theories Finitary equational theories Algebraic operations and generic effects Prospectus and Exercises 3 Plotkin Lecture 1: Algebraic Effects I

  18. Moggi’s Monads As Notions of Computation Introduction Algebraic Effects Equational theories Prospectus and Exercises Two questions So we have a denotational semantics. How about an operational one? How do effects arise, i.e., how do we “construct” them in a programming language? - Answering the first question immediately leads to the second. - Answering that leads to understanding where Moggi’s monads come from. Plotkin Lecture 1: Algebraic Effects I

  19. Moggi’s Monads As Notions of Computation Introduction Algebraic Effects Equational theories Prospectus and Exercises An example: finite nondeterminism Take T SL ( X ) = F + ( X ) the collection of non-empty finite subsets of X . To create the effects we add an effect constructor to the language: M : σ N : σ M + N : σ with semantics ]( γ ) ∪ [ [ [ M + N ] ]( γ ) = [ [ M ] [ N ] ]( γ ) So T SL ( X ) is an algebra (when equipped) with the binary operation ∪ : T SL ( X ) × T SL ( X ) → T SL ( X ) . But which algebra is it? Plotkin Lecture 1: Algebraic Effects I

  20. Moggi’s Monads As Notions of Computation Introduction Algebraic Effects Equational theories Prospectus and Exercises Nondeterminism as an algebraic effect There is a natural equational theory, with signature + : 2, and set of axioms SL (for semilattices) given by: Associativity ( x + y ) + z = x + ( y + z ) Commutativity x + y = y + x x + x = Absorption x The above algebra on F + ( X ) satisfies these equations, interpreting + as ∪ . Further: F + is the free algebra monad. Plotkin Lecture 1: Algebraic Effects I

Recommend


More recommend