On the expressive power of user-defined effects: effect handlers, - - PowerPoint PPT Presentation

on the expressive power of user defined effects effect
SMART_READER_LITE
LIVE PREVIEW

On the expressive power of user-defined effects: effect handlers, - - PowerPoint PPT Presentation

On the expressive power of user-defined effects: effect handlers, monadic reflection, and delimited control Work in Progress Ohad Kammar <ohad.kammar@cs.ox.ac.uk> joint work with Yannick Forster, Sam Lindley, and Matija Pretnar The 4 th


slide-1
SLIDE 1 T H E U N I V E R S I T Y O F E D I N B U R G H

On the expressive power of user-defined effects: effect handlers, monadic reflection, and delimited control

Work in Progress Ohad Kammar <ohad.kammar@cs.ox.ac.uk> joint work with Yannick Forster, Sam Lindley, and Matija Pretnar The 4th Southern REPLS Imperial College 27 September 2016

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-2
SLIDE 2

Effect oriented programming

Native effects

◮ I/O. ◮ Mutable state. ◮ Randomness and non-determinism.

User-defined effects

◮ Parsing. ◮ Constraint solving. ◮ Proof-search tactics. ◮ Redefine existing effects?

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-3
SLIDE 3

A brief history of functional programming and effects

A love-hate story

Effects are harmful. . .

◮ Disallow useful compiler optimisations. ◮ Break referential transparency. ◮ Depart from the λ-calculus (βη-equality, confluence).

But are useful!

A rift and a bridge

ML and Scheme vs. Haskell. Monads [Moggi’89, Wadler’91], now in Haskell, ML, Scheme, F∗, C++. . .

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-4
SLIDE 4

Algebraic effects

Monad issues

◮ No interface for effects. ◮ Compositionality and modularity issues. ◮ Steep learning curve.

Plotkin-Power-Pretnar-Bauer

◮ Add effect operations to Moggi’s theory

[Plotkin-Power’02,’03].

◮ Add exception handlers, and more generally, effect handlers

[Plotkin-Pretnar’09].

◮ Programming with algebraic effects and handlers

[Bauer-Pretnar’15].

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-5
SLIDE 5

Delimited control

Goto on steroids

Effect handlers are a new kind of delimited control effect. But(!):

◮ Clean denotational semantics. [Plotkin-Pretnar’09] ◮ Clean program logic. [Pretnar’s thesis] ◮ Clean meta-theory: strong normalisation and type-and-effect

systems [K-Lindley-Oury’13], unrestricted polymorphism [K-Pretnar’16], . . .

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-6
SLIDE 6

Basic research question

Monads, handlers, and delimited control

◮ How do these abstractions compare? ◮ How to compare these abstractions?

Macro expressibility [Fellisen’90]

◮ Expressive power of Turing-complete languages. ◮ Computability and complexity reductions are too crude. ◮ Macro translations:

◮ Keep shared fragment identical. ◮ Compositional a.k.a. local a.k.a. homomorphic. Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-7
SLIDE 7

Contribution: bird’s eye

MAM

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-8
SLIDE 8

Contribution: bird’s eye

MAM EFF MON DEL

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-9
SLIDE 9

Contribution: bird’s eye

MAM EFF MON DEL

= = =

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-10
SLIDE 10

Contribution: bird’s eye

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-11
SLIDE 11

Contribution: bird’s eye

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢

∃ ∃ ∃ = = =

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-12
SLIDE 12

Contribution: bird’s eye

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢

∃ ∃ ∃ = = =

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-13
SLIDE 13

Contribution: bird’s eye

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢

∃ = =

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-14
SLIDE 14

Contribution: bird’s eye

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢ MAM+

EFF+

DEL+

MON+

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-15
SLIDE 15

Contribution: bird’s eye

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢ MAM+

EFF+

DEL+

MON+

= = =

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-16
SLIDE 16

Talk structure

◮ Short tutorial on algebraic effects, monadic reflection, and

delimited control Rationale: Most fiddly task was simplifying and unifying calculi.

◮ Time pertmitting: discuss the negative result.

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-17
SLIDE 17

The lambda calculus with effects (mam)

Syntax

V , W ::= values x variable | () unit value | (V1, V2) pairing | inji V variant constructor | {M} thunk M, N ::= computations split(V , x1.x2.M) pattern matching: product | case0(V ) void | case(V , inj1 x1.M1 , inj2 x2.M2) variants | V ! force | return V returner | let x ← M in N sequencing (monadic bind) | λx.M function abstraction | M V function application | M1, M2 computation pair | prji M projection

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-18
SLIDE 18

The lambda calculus with effects (mam)

Syntax (CBPV)

V , W ::= values x variable | () unit value | (V1, V2) pairing | inji V variant constructor | {M} thunk M, N ::= computations split(V , x1.x2.M) pattern matching: product | case0(V ) void | case(V , inj1 x1.M1 , inj2 x2.M2) variants | V ! force | return V returner | let x ← M in N sequencing (monadic bind) | λx.M function abstraction | M V function application | M1, M2 computation pair | prji M projection

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-19
SLIDE 19

The lambda calculus with effects (mam)

Operational semantics

Reduction frames and contexts B ::= let x ← [ ] in N | [ ] V | prji [ ] basic frames F ::= B computation frames C ::= evaluation context [ ] hole C[F[ ]] layered frame Beta reduction M β M′ (β.×) split((V1, V2), x1.x2.M) β M[V1/x1, V2/x2] (β.+) case(inji V , inj1 x1.M1, inj2 x2.M2) β Mi[V /xi] (β.U) {M}! β M (β.F) let x ← return V in M β M[V /x] (β.→) (λx.M) V β M[V /x] (β.&) prji M1, M2 β Mi Reduction M M′ M β M′ C[M] C[M′]

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-20
SLIDE 20

The lambda calculus with effects (mam)

Types

E ::= effects ∅ pure effect K ::= kinds | Eff effects Val values | CompE E-computations | Ctxt environments A, B ::= value types α type variable | 1 unit | A1 × A2 value products | empty | A1 + A2 variant | UE C thunks C, D ::= computation types FA returners | A → C functions | C1 & C2 computation products Θ ::= α1, . . . , αn type environments Γ, ∆ ::= x1 : A1, . . . , xn : An environments

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-21
SLIDE 21

The lambda calculus with effects (mam)

Denotational semantics

Standard, using sets and functions. Using Hermida’s [’93] lifting:

Theorem (adequacy)

Denotational equivalence implies contextural equivalence: for all Θ; Γ ⊢E P, Q : X, if ⟦P⟧ = ⟦Q⟧ then P ≃ Q.

Corollary (soundness and strong normalisation)

All well-typed closed ground returners reduce to a normal form: for all ; ⊢∅ M : FG there exists some ; ⊢ V : G such that ⟦return V ⟧ = ⟦M⟧ and M ⋆ return V

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-22
SLIDE 22

Effect handlers

Syntax [K-Lindley-Oury’13]

M, N ::= . . . computations |

  • p V
  • peration call

| handle M with H handling construct H ::= handlers {return x → M} return clause | H ⊎ {op p k → N}

  • peration clause

(where op does not occur in H)

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-23
SLIDE 23

Effect handlers

Operational semantics

Reduction frames and contexts · · · F ::= . . . computation frame | handle [ ] with H H ::= [ ] | H[B[ ]] hoisting context Beta reduction · · · (handle.F) handle (return V ) with H β M[V /x] where Hreturn = λx.M (handle.op) handle H[op V ] with H β N[V /p, {λx.handle H[return x] with H}/k] where Hop = λp k.N and x / ∈ FV (H, H)

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-24
SLIDE 24

Effect handlers

Types

E ::= . . . effects | {op : A → B} ⊎ E arity assignment K ::= . . . kinds | Hndlr handlers R ::= A E ⇒E′ C handler types · · · Computation typing Θ; Γ ⊢E M : C (Θ ⊢k Γ : Ctxt, E : Eff, C : CompE ) · · · (op : A → B) ∈ E Θ; Γ ⊢ V : A Θ; Γ ⊢E op V : FB Θ; Γ ⊢E M : FA Θ; Γ ⊢ H : A E ⇒E′ C Θ; Γ ⊢E′ handle M with H : C Handler typing Θ; Γ ⊢ H : R (Θ ⊢k Γ : Ctxt, R : Hndlr) E = {opi : Ai → Bi}i H = {return x → M} ⊎ {opi p k → Ni}i [Θ; Γ, p : Ai, k : UE′(Bi → C) ⊢E′ Ni : C]i Θ; Γ, x : A ⊢E′ M : C Θ; Γ ⊢ H : A E ⇒E′ C

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-25
SLIDE 25

Effect handlers

Denotational semantics

Using free monads for a signature. Using a folklore lifting [cf. K’14] we have adequacy, soundness and strong normalisation.

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-26
SLIDE 26

Monadic reflection

Syntax [Filinski’94-10]

T ::= mon(M, N) monads M, N ::= . . . computations | ˆ µ(N) reflect | [N]T reify

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-27
SLIDE 27

Monadic reflection

Operational semantics

Reduction frames and contexts F ::= B | [[ ]]T computation frames H ::= [ ] | H[B[ ]] hoisting contexts · · · Beta reduction · · · (reify) [return V ]T β NuV (reflect) [H[ˆ µ(N)]]T β Nb {N} {(λx.[H[return x]]T )} for every T = mon(Nu, Nb).

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-28
SLIDE 28

Monadic reflection

Types

E ::= . . . effects E ≺ α.C, N, M layered monad Monad typing Θ ⊢m T : E (Θ ⊢k E : Eff) Θ, α; ⊢E Nu : α → C Θ, α, β; ⊢E Nb : UE C → UE (α → C[β/α]) → C[β/α] Θ ⊢m mon(Nu, Nb) : E ≺ α.C, Nu, Nb Computation typing Θ; Γ ⊢E M : C (Θ ⊢k Γ : Ctxt, E : Eff, C : CompE ) · · · Θ ⊢m T : E ≺ α.C, Nu, Nb Θ; Γ ⊢E≺α.C,Nu,Nb N : A Θ; Γ ⊢E [N]T : C[A/α] Θ; Γ ⊢E N : C[A/α] Θ; Γ ⊢E≺α.C,Nu,Nb ˆ µ(N) : FA

Caveats

Essentially top level monad declarations to avoid dependent types. Requires type variables.

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-29
SLIDE 29

Monadic reflection

Denotational semantics

Partial semantics due to the invalidity of the monad laws. Using TT-lifting, we have adequacy, soundness and strong normalisation for terms whose semantics is defined.

Lemma (Finite denotation property)

For any tuple θ = Xαα∈Θ of finite sets, if the types A and C have well-defined denotations for θ, they denote finite sets.

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-30
SLIDE 30

Delimited control

Syntax

M, N ::= . . . computations | S0k.M shift-0 | M|x.N reset

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-31
SLIDE 31

Delimited control

Operational semantics

Reduction frames and contexts · · · F ::= . . . computation frame | [ ]|x.N H ::= [ ] | H[B[ ]] hoisting context Beta reduction · · · (reset) (return V )|x.M β M[V /x] (shift0) H[S0k.M]|x.N β M[λy.H[return y]|x.N/k]

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-32
SLIDE 32

Delimited control

Types [Danvy and Filinski, sketched]

E ::= . . . effects | E, A Computation typing Θ; Γ ⊢E M : C (Θ ⊢k Γ : Ctxt, E : Eff, C : CompE ) · · · Θ; Γ, k : UE (B → FA) ⊢E M : FA Θ; Γ ⊢E,A S0k.M : FB Θ; Γ ⊢E,A M : FA Θ; Γ, x : A ⊢E N : C Θ; Γ ⊢E M|x.N : C

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-33
SLIDE 33

Delimited control

Denotational semantics

An adequate denotational semantics is an open problem. Perhaps [Atkey’06]’s parameterised monads?

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-34
SLIDE 34

Untyped translations

MAM EFF MON DEL

= = =

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-35
SLIDE 35

Typed translations

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-36
SLIDE 36

Typed translations

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢

∃ ∃ ∃ = = =

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-37
SLIDE 37

Typed translations

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢

∃ ∃ ∃ = = =

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-38
SLIDE 38

Typed translations

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢

∃ = =

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-39
SLIDE 39

Richer types

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢ MAM+

EFF+

DEL+

MON+

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-40
SLIDE 40

Richer types

MAM EFF MON DEL MAM⊢ EFF⊢ DEL⊢ MON⊢ MAM+

EFF+

DEL+

MON+

= = =

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-41
SLIDE 41

Richer types

Conjectures

◮ Handlers + polymorphic arities express delimited control and

monadic reflection

◮ Delimited control + effect polymorphism express effect

handlers and monadic reflection.

◮ Parameterised monadic reflection and polymorphism express

effect handlers and delimited control.

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

slide-42
SLIDE 42

Summary and conclusions

◮ Rigorous set-up for comparing user-defined effect abstractions. ◮ New and folklore macro translations. ◮ Inexpressivity result via a denotational invariant. ◮ Type system extensions accepting the translations.

Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects