26
T H E U N IV E RS 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 Yannick Forster, Ohad Kammar, Sam Lindley, and Matija Pretnar 22 nd ACM SIGPLAN International Conference on Functional Programming 4 September 2017 Oxford, United Kingdon, EU Y. Forster, O. Kammar , S. Lindley, and M. Pretnar On the expressive power of user-defined effects 1

NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

TH

E

UN I V E

RS

IT

Y

OF

ED

I N BU

RG

H

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

and delimited control

Yannick Forster, Ohad Kammar,Sam Lindley, and Matija Pretnar

22nd ACM SIGPLANInternational Conference on Functional Programming

4 September 2017Oxford, United Kingdon, EU

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 1

Page 2: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

User-defined effects

User-defined state

toggle = { x ← get!;

y ← not! x ;

put! y ;

x}Direct-style

get = { λs.( s, s )}put = {λs ′.λ .((), s ′)}runState = λc .λs.c! stoggle = {λs. (x , s)← get! s;

y ← not! x ;

( , s)← put! y s;

(x , s)}State-passing

Macro-expressibility

A macro translation:

I Local

I Preserves core constructs

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 2

Page 3: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

User-defined effects

User-defined state

toggle = { x ← get!;

y ← not! x ;

put! y ;

x}Direct-style

get = { λs.( s, s )}put = {λs ′.λ .((), s ′)}runState = λc .λs.c! stoggle = {λs. (x , s)← get! s;

y ← not! x ;

( , s)← put! y s;

(x , s)}State-passing

Macro-expressibility

A macro translation:

I Local

I Preserves core constructs

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 2

Page 4: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Goal

Relative expressiveness in language design

Compare and contrast:

I Algebraic effects and handlers

I Monads

I Delimited control

Small print

I Large design space

I Inexpressivity is brittle

Takeaway message

Expressibility must be stated as formal translations between calculi.

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 3

Page 5: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Goal

Relative expressiveness in language design

Compare and contrast:

I Algebraic effects and handlers

I Monads

I Delimited control

Small print

I Large design space:deep handlers, shallow handlers, parameterised monads,graded monads, shift vs. shift0, answer-type modification

I Inexpressivity is brittle:adding inductive or polymorphic types invalidates our proofs

Takeaway message

Expressibility must be stated as formal translations between calculi.

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 3

Page 6: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Goal

Relative expressiveness in language design

Compare and contrast:

I Algebraic effects and handlers

I Monads

I Delimited control

Small print

I Large design space

I Inexpressivity is brittle

Takeaway message

Expressibility must be stated as formal translations between calculi.

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 3

Page 7: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Contribution

cbpv

establishedstatus

I Syntax, operationalsemantics

I Macro translationsI Denotational semantics for

eff,monI Meta-theory:

I Type safetyI Adequacy and soundness

for eff,mon

I Formalisation in Abella

I Typeability preservationproofs

I Inexpressibility proofs

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 4

Page 8: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Contribution

cbpv eff

mon

del

establishedstatus

I Syntax, operationalsemantics

I Macro translationsI Denotational semantics for

eff,monI Meta-theory:

I Type safetyI Adequacy and soundness

for eff,mon

I Formalisation in Abella

I Typeability preservationproofs

I Inexpressibility proofs

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 4

Page 9: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Contribution

eff

mon

del

establishedstatus

I Syntax, operationalsemantics

I Macro translations

I Denotational semantics foreff,mon

I Meta-theory:I Type safetyI Adequacy and soundness

for eff,mon

I Formalisation in Abella

I Typeability preservationproofs

I Inexpressibility proofs

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 4

Page 10: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Contribution

eff

mon

del

typed eff

typed del

typed mon

establishedstatus

I Syntax, operationalsemantics

I Macro translationsI Denotational semantics for

eff,monI Meta-theory:

I Type safetyI Adequacy and soundness

for eff,mon

I Formalisation in Abella

I Typeability preservationproofs

I Inexpressibility proofs

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 4

Page 11: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Contribution

eff

mon

del

typed eff

typed del

typed mon

establishedstatus

@

@

I Syntax, operationalsemantics

I Macro translationsI Denotational semantics for

eff,monI Meta-theory:

I Type safetyI Adequacy and soundness

for eff,mon

I Formalisation in Abella

I Typeability preservationproofs

I Inexpressibility proofs

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 4

Page 12: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Contribution

eff

mon

del

typed eff

typed del

typed mon

established

conjectured

status

@?

@?

@?

@

@

I Syntax, operationalsemantics

I Macro translationsI Denotational semantics for

eff,monI Meta-theory:

I Type safetyI Adequacy and soundness

for eff,mon

I Formalisation in Abella

I Typeability preservationproofs

I Inexpressibility proofs

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 4

Page 13: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Contribution (this talk)

eff

mon

del

typed eff

typed del

typed mon

established

conjectured

status

@?

@?

@?

@

@

I Syntax, operationalsemantics

I Macro translationsI Denotational semantics for

eff,monI Meta-theory:

I Type safetyI Adequacy and soundness

for eff,mon

I Formalisation in Abella

I Typeability preservationproofs

I Inexpressibility proofs

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 4

Page 14: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Effect handlers

User-defined statetoggle = {x ← get ();

State = {get : 1→ bit, put : bit→ 1} : Eff

y ← not! x ;put y ;x}

: UStateFbit

HST = {return x 7→ λs.return xget k 7→ λs.k! s sput s ′ k 7→ λ .k! () s ′}

: bit State⇒∅ bit→ Fbit

runState = {λc .handle c! with HST}

: U∅((UStateFbit)→ bit→ Fbit)

runState! toggle True ∗ (handle True with HST ) False ∗ True

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 5

Page 15: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Effect handlers

User-defined statetoggle = {x ← get ();

State = {get : 1→ bit, put : bit→ 1} : Eff

y ← not! x ;put y ;x}

: UStateFbit

HST = {return x 7→ λs.return xget k 7→ λs.k! s sput s ′ k 7→ λ .k! () s ′}

: bit State⇒∅ bit→ Fbit

runState = {λc .handle c! with HST}

: U∅((UStateFbit)→ bit→ Fbit)

runState! toggle True ∗ (handle True with HST ) False ∗ True

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 5

Page 16: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Effect handlers

User-defined statetoggle = {x ← get (); State = {get : 1→ bit, put : bit→ 1} : Eff

y ← not! x ;put y ;x} : UStateFbit

HST = {return x 7→ λs.return xget k 7→ λs.k! s sput s ′ k 7→ λ .k! () s ′} : bit State⇒∅ bit→ Fbit

runState = {λc .handle c! with HST} : U∅((UStateFbit)→ bit→ Fbit)

runState! toggle True ∗ (handle True with HST ) False ∗ True

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 5

Page 17: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Monadic reflection

User-defined statetoggle = {x ← get!;

y ← not! x ;put! y ;x}

: UStateFbit

get = { µ(λs.(s , s))}

: UStateFbit

put = {λs ′.µ(λ .((), s ′))}

: UState(bit→ F1)

runState = {λc .[c!]TState}

: U∅((UStateFbit)→ bit→ F (bit× bit))

TState = where {return x = λs.(x , s);f�=k = λs.(x , s ′)← f s;

k! x s ′}

runState! toggle True ? return (True, False)

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 6

Page 18: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Monadic reflection

User-defined statetoggle = {x ← get!;

y ← not! x ;put! y ;x}

: UStateFbit

get = { µ(λs.(s , s))}

: UStateFbit

put = {λs ′.µ(λ .((), s ′))}

: UState(bit→ F1)

runState = {λc .[c!]TState}

: U∅((UStateFbit)→ bit→ F (bit× bit))

TState = where {return x = λs.(x , s);f�=k = λs.(x , s ′)← f s;

k! x s ′}runState! toggle True ? return (True, False)

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 6

Page 19: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Monadic reflection

User-defined statetoggle = {x ← get!;

y ← not! x ;put! y ;x} : UStateFbit

get = { µ(λs.(s , s))} : UStateFbitput = {λs ′.µ(λ .((), s ′))} : UState(bit→ F1)

runState = {λc .[c!]TState} : U∅((UStateFbit)→ bit→ F (bit× bit))

State = ∅ ≺ instance monad (α.bit→ F (α× bit))where {

return x = λs.(x , s);f�=k = λs.(x , s ′)← f s;

k! x s ′} : Eff

runState! toggle True ? return (True, False)

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 6

Page 20: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Translation: mon→eff

µ(N) B reflect {N}[M]TB handle M with TT B {return x 7→ Nu

reflect y f 7→ Nb}

Theorem (Correctness)

eff simulates mon:M N =⇒ M + N

This translation does not preserve typability:

[ b ← µ({λ(b, f ).b});f ← µ({λ(b, f ).f });

f ! b]TReader

(injtrue (), {λb.return b})

Reader = ∅ ≺ instance monad (α.bit× U∅ (bit→ F bit)→ Fα)where {return x = λe.return x ;

m�=f = λe.x ← m! e; f ! x e}

I Reflection at different type

I Remedy: effects withpolymorphic arities

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 7

Page 21: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Delimited control

toggle = {x ← get!;

State = ∅,bit→ Fbit : Eff

y ← not! x ;put! y ;x}

: UStateFbit

get = { S0k .λs.k! s s}

: UStateFbit

put = {λs ′.S0k .λ .k! () s ′}

: UState(bit→ F1)

runState = {λc . 〈c!|x .λs.x〉}

: U∅((UStateFbit)→ bit→ Fbit)

runState! toggle True ∗ 〈True|x .λs.x〉 False ∗ return True

(shift-zero and dollar without answer-type modification)

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 8

Page 22: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Delimited control

toggle = {x ← get!;

State = ∅,bit→ Fbit : Eff

y ← not! x ;put! y ;x}

: UStateFbit

get = { S0k .λs.k! s s}

: UStateFbit

put = {λs ′.S0k .λ .k! () s ′}

: UState(bit→ F1)

runState = {λc . 〈c!|x .λs.x〉}

: U∅((UStateFbit)→ bit→ Fbit)

runState! toggle True ∗ 〈True|x .λs.x〉 False ∗ return True

(shift-zero and dollar without answer-type modification)

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 8

Page 23: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Delimited control

toggle = {x ← get!; State = ∅,bit→ Fbit : Effy ← not! x ;put! y ;x} : UStateFbit

get = { S0k .λs.k! s s} : UStateFbitput = {λs ′.S0k .λ .k! () s ′} : UState(bit→ F1)runState = {λc . 〈c!|x .λs.x〉} : U∅((UStateFbit)→ bit→ Fbit)

runState! toggle True ∗ 〈True|x .λs.x〉 False ∗ return True

(shift-zero and dollar without answer-type modification)

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 8

Page 24: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Translation: eff→del

op V B S0k .λh.h! (injop (V , {λy .k! y h}))handle M with H B 〈M|Hret〉 {Hops}handle M with

{return x 7→ Nret}] {opi p k 7→ Ni}i

ret

B x .λh.Nret

handle M with{return x 7→ Nret}

] {opi p k 7→ Ni}i

ops

Bλy .case y of {

(injopi (p, k)→ Ni )i}

Theorem (Correctness)

del simulates eff up to congruence:

M N =⇒ M +cong N

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 9

Page 25: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Inexpressivity

TheoremThe following macro translations do not exist:

I typed eff→typed mon satisfying: M N =⇒ M ' N.

I typed eff→typed del satisfying: M N =⇒ M ' N.

Proof sketch:

Lemma (finite denotation property)

Every closed type X denotes a finite set ⟦X ⟧.Take

tick0 B return () tickn+1 B tick(); tickn

and note:m 6= n =⇒ tickn 6' tickm

A macro translation typed eff→typed mon yields acontradiction using these two facts and mon’s adequacy.

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 10

Page 26: NIVE · graded monads, shift vs. shift0, answer-type modi cation IInexpressivity is brittle: adding inductive or polymorphic types invalidates our proofs Takeaway message Expressibility

Contribution

eff

mon

del

typed eff

typed del

typed mon

established

conjectured

status

@?

@?

@?

@

@

I Syntax, operationalsemantics

I Macro translations

I Denotational semantics foreff,mon

I Meta-theory:I Type safetyI Adequacy and soundness

for eff,mon

I Formalisation in Abella

I Typeability preservationproofs

I Inexpressibility proofs

Expressibility must be stated asformal translations betweencalculi.

Y. Forster, O. Kammar, S. Lindley, and M. Pretnar On the expressive power of user-defined effects 11