A Bidirectional Krivine...

95
Mikaël Mayer Ravi Chugh A Bidirectional Krivine Evaluator Bx 2019

Transcript of A Bidirectional Krivine...

Page 1: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

Mikaël Mayer Ravi Chugh

A Bidirectional Krivine Evaluator

Bx 2019  

Page 2: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

A Bidirectional Krivine Evaluator

Bx 2019  

2  

Page 3: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

Bidirectional Evaluation

OOPSLA 2018  

A Bidirectional Krivine Evaluator

Bx 2019  

3  

Page 4: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

4  

( Edit Program; Run; View Output )+

Page 5: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

5  

( Edit Program; Run; View Output )+

Page 6: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

6  

( Edit Program; Run; View Output )+

Page 7: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

7  

( Edit Program; Run; View Output )+

( Edit Program; Run; View Output )+

( Edit Program; Run; View Output )+

( Edit Program; Run; View Output )+

( Edit Program; Run; View Output )+

( Edit Program; Run; View Output )+

( Edit Program; Run; View Output )+

( Edit Program; Run; View Output )+

( Edit Program; Run; View Output )+

( Edit Program; Run; View Output )+

Page 8: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

8  

( Edit Program; Run; View Output; Edit Output )+

Synthesize Program Repair

Our Goal

Page 9: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

9  

let f x = x ++ " 2018" in  

Page 10: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

10  

let f x = x ++ " 2018" in f "B" ++ " @ " ++ f "PLW"    

Page 11: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

11  

let f x = x ++ " 2018" in f "B" ++ " @ " ++ f "PLW"    

"B 2018 @ PLW 2018"  ⇒  

Page 12: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

12  

let f x = x ++ " 2018" in f "B" ++ " @ " ++ f "PLW"    

"B 2018 @ PLW 2018"  ⇒   ⇝"Bx 2019 @ PLW 2018"  

Page 13: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

13  

let f x = x ++ " 2018" in f "B" ++ " @ " ++ f "PLW"    

"B 2018 @ PLW 2018"  ⇒   ⇝"Bx 2019 @ PLW 2018"  ⇐  let f x = x ++ " 2019" in

f "Bx" ++ " @ " ++ f "PLW"  

Page 14: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

14  

let f x = x ++ " 2018" in f "B" ++ " @ " ++ f "PLW"    

"B 2018 @ PLW 2018"  ⇒ ⇝"Bx 2019 @ PLW 2018"  ⇐let f x = x ++ " 2019" in

f "Bx" ++ " @ " ++ f "PLW"  

Page 15: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

15  

"Bx 2019 @ PLW 2019"  

let f x = x ++ " 2019" in f "Bx" ++ " @ " ++ f "PLW"  ⇒  

let f x = x ++ " 2018" in f "B" ++ " @ " ++ f "PLW"    

"B 2018 @ PLW 2018"  ⇒ ⇝"Bx 2019 @ PLW 2018"  ⇐let f x = x ++ " 2019" in

f "Bx" ++ " @ " ++ f "PLW"  

Page 16: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

16  

"B 2018 @ PLW 2018"  

let f x = x ++ " 2018" in f "B" ++ " @ " ++ f "PLW"   ⇒   ⇝

⇐   "Bx 2019 @ PLW 2018"  

let f x = x ++ " 2019" in f "Bx" ++ " @ " ++ f "PLW"  

"Bx 2019 @ PLW 2019"  

let f x = x ++ " 2019" in f "Bx" ++ " @ " ++ f "PLW"  ⇒  

Page 17: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

17  

"B 2018 @ PLW 2018"  

let f x = x ++ " 2018" in f "B" ++ " @ " ++ f "PLW"   ⇒   ⇝

⇐   "Bx 2019 @ PLW 2018"  

let f x = x ++ " 2019" in f "Bx" ++ " @ " ++ f "PLW"  

"Bx 2019 @ PLW 2019"  

let f x = x ++ " 2019" in f "Bx" ++ " @ " ++ f "PLW"  ⇒  

Unrestricted function  

Page 18: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

18  

"B 2018 @ PLW 2018"  

let f x = x ++ " 2018" in f "B" ++ " @ " ++ f "PLW"   ⇒   ⇝

⇐   "Bx 2019 @ PLW 2018"  

let f x = x ++ " 2019" in f "Bx" ++ " @ " ++ f "PLW"  

"Bx 2019 @ PLW 2019"  

let f x = x ++ " 2019" in f "Bx" ++ " @ " ++ f "PLW"  ⇒  

Unrestricted function  

Repair "code" + "data"  

Page 19: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

19  

"B 2018 @ PLW 2018"  

let f x = x ++ " 2018" in f "B" ++ " @ " ++ f "PLW"   ⇒   ⇝

⇐   "Bx 2019 @ PLW 2018"  

let f x = x ++ " 2019" in f "Bx" ++ " @ " ++ f "PLW"  

"Bx 2019 @ PLW 2019"  

let f x = x ++ " 2019" in f "Bx" ++ " @ " ++ f "PLW"  ⇒  

Unrestricted function  

Repair "code" + "data"  

Extra repairs  

Page 20: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

20  

''  ⇒   ⇝

⇐  

⇒  

Unrestricted function  

Repair "code" + "data"  

Extra repairs  

Page 21: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

21  

⇒   ⇝

⇐  

⇒  

Unrestricted function  

Repair "code" + "data"  

Extra repairs  

y''  L' x'  L.get  

Page 22: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

22  

⇒   ⇝

⇐  

⇒  

Unrestricted function  

Repair "code" + "data"  

Extra repairs  

y''  L' x'  L.get  

y''  L' x'  L.put  

Page 23: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

23  

y''  L' x'   ⇒   ⇝

⇐   y''  L' x'  

y''  L' x'   ⇒  

Unrestricted function  

Repair "code" + "data"  

Extra repairs  

L.get  

L.get  

L.put  

Page 24: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

24  

y''  f' x'   ⇒   ⇝

⇐   y''  f'' x'  

y''  f'' x'   ⇒  

Unrestricted function  

Repair "code" + "data"  

Extra repairs  

Page 25: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

25  

v''  e'   ⇒   ⇝

⇐   v''  e'  

v''  e'   ⇒  

Page 26: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

26  

v''  e'   ⇒   ⇝

⇐   v''  e'  

v''  e'   ⇒  

Every exp (code + data)…���  

… runs backward and updated  

Page 27: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

27  

v''  e'   ⇒   ⇝

⇐   v''  e'  

v''  e'   ⇒  

Every exp (code + data)…���  

… runs backward and updated  

Don't (necessarily) enforce PutGet law  

Page 28: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

28  

e') ⇒'v'  

e') ⇐ 'v'  

⇝Bidirectional Evaluation

Page 29: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

29  

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐ 'v'  

⇝Bidirectional Evaluation

Environment-style semantics���simplifies the system  

Page 30: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

30  

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐ 'v'  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

Page 31: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

31  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

Page 32: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

32  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

Page 33: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

33  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

(– ⊢ 2018) ⇒'2018  

2019  

Page 34: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

34  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

(– ⊢ 2018) ⇒'2018  

(– ⊢ 2019) ⇐' 2019  

Page 35: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

35  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

Page 36: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

36  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

(– ⊢ let x = 2018 in x) ⇒'2018  

2019  

Page 37: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

37  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

((x ↦ 2018) ⊢ x) ⇒'2018  

2019  

Page 38: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

38  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

((x ↦ 2018) ⊢ x) ⇒'2018  

((x ↦ 2019) ⊢ x) ⇐' 2019  

Page 39: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

39  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

(– ⊢ let x = 2018 in x) ⇒'2018  

(– ⊢ let x = 2019 in x) ⇐ '2019  

Page 40: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

40  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

Page 41: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

41  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

(– ⊢ let x = 18 in (x, x)) ⇒'(18, 18)  

(19, 18)  

Page 42: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

42  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

((x ↦ 18 ) ⊢ (x, x)) ⇒'(18, 18)  

(19, 18)  

Page 43: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

43  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

((x ↦ 18 ) ⊢ (x, x)) ⇒'(18, 18)  

((x ↦ 19 ) ⊢ (x, x)) ⇐' (19, 18)  

⇝ 18?���

19?  

Page 44: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

44  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

((x ↦ 18 ) ⊢ (x, x)) ⇒'(18, 18)  

((x ↦ 19 ) ⊢ (x, x)) ⇐' (19, 20)  

⇝ 18?���

19?���20?  

Page 45: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

45  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

Merge: E1 ⊕conservative E2

(x↦19) ⊕conservative (x↦20) undefined

Enables a PutGet theorem  

Page 46: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

46  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

Merge: E1 ⊕conservative E2

(x↦19) ⊕conservative (x↦20) undefined

Enables a PutGet theorem  

Merge: E1 ⊕optimistic E2

(x↦19) ⊕ optimistic (x↦20) = 19 or 20 (or 18)  Enables many desirable interactions  

Page 47: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

47  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

((x ↦ 18 ) ⊢ (x, x)) ⇒'(18, 18)  

((x ↦ 19 ) ⊢ (x, x)) ⇐' (19, 20)  

⇝ 18?���

19?���20?  

Page 48: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

48  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

(– ⊢ let x = 18 in (x, x)) ⇒'(18, 18)  

(– ⊢ let x = 18 in (x, x)) ⇐' (19, 20)  

⇝ 18?���

19?���20?  

Ask user to choose  

Page 49: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

49  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

Page 50: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

50  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

(– ⊢ ) ⇒  let f = λx. x ++ "18"���in f "B "  

"B 18"  

"Bx 19"  

Page 51: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

51  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

((f ↦ λx. x ++ "18") ⊢ f "B ") ⇒   "B 18"  

"Bx 19"  

Page 52: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

52  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

((x ↦ "B ") ⊢ x ++ "18") ⇒   "B 18"  

"Bx 19"  

Page 53: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

53  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

((x ↦ "B ") ⊢ x ++ "18") ⇒  

((x ↦ "Bx ") ⊢ x ++ "19") ⇐  

"B 18"  

"Bx 19"  

"data"   "code"  

Page 54: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

54  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

((f ↦ λx. x ++ "19") ⊢ f "Bx ") ⇐  

"B 18"  ((f ↦ λx. x ++ "18") ⊢ f "B ") ⇒  

"Bx 19"  

"data"  "code"  

Page 55: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

55  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

v ::= c | [E] λx.e | (v1, v2)

E ::= – | (E, x↦v)

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

"B 18"  (– ⊢ ) ⇒  let f = λx. x ++ "18"���in f "B "  

let f = λx. x ++ "19"���in f "Bx "  

(– ⊢ ) ⇐  

"Bx 19"  

"data"  "code"  

Page 56: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

56  

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

Page 57: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

57  

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

(– ⊢ "B " ++ "18") ⇒   "B 18"  ⇝

"Bx 19"  

Page 58: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

58  

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

(– ⊢ "B " ++ "18") ⇒  

(– ⊢ "Bx " ++ "19") ⇐'  

"B 18"  ⇝

"Bx 19"  

Primitive lens: ((++)get , (++)put)  

Page 59: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

59  

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

| appLens (eget, eput) ex

(– ⊢ "B " ++ "18") ⇒  

(– ⊢ "Bx " ++ "19") ⇐'  

"B 18"  ⇝

"Bx 19"  

Libraries and users can���define custom lenses  

Primitive lens: ((++)get , (++)put)  

Page 60: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

60  

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

| appLens (eget, eput) ex

Page 61: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

61  

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

| appLens (eget, eput) ex

Bidirectional Programs���with are Lenses

Page 62: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

62  

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐'v'  

⇝ ion  

e ::= c | λx.e | x | (e1, e2) | e1 e2 | …

| appLens (eget, eput) ex

Bidirectional Programs���with are Lenses

Page 63: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

63  

Sketch-n-Sketch

Page 64: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

64  

Sketch-n-Sketch

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐ 'v'  

Page 65: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

65  

Sketch-n-Sketch

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐ 'v'  

⇝Elm-like PL +

Page 66: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

66  

Sketch-n-Sketch

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐ 'v'  

⇝Elm-like PL + HTML GUI

Page 67: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

67  

Sketch-n-Sketch

(E ⊢ e') ⇒'v'  

(E' ⊢ e') ⇐ 'v'  

⇝Elm-like PL + HTML GUI

Demo

Page 68: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

Bidirectional Evaluation

OOPSLA 2018  

Bidirectional Programs���with are Lenses

68  

Page 69: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

Bidirectional Evaluation

OOPSLA 2018  

A Bidirectional Krivine Evaluator

Bx 2019  

69  

Page 70: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

70  

Bidirectional CBV Evaluation

Page 71: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

71  

Bidirectional CBV Evaluation

Page 72: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

72  

Bidirectional CBV Evaluation

Most rules���are "mechanical"

Page 73: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

73  

Bidirectional CBV Evaluation

Most rules���are "mechanical"

App rule calls���forward evaluator

Page 74: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

74  

Bidirectional CBV Evaluation

Most rules���are "mechanical"

App rule calls���forward evaluator

Page 75: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

75  

Page 76: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

76  

Call-by-Value

Evaluate function to closure

Evaluate argument to value

Page 77: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

77  

Call-by-Value

Evaluate function to closure

Evaluate argument to value

Call-by-Name

Delay argument evaluation

Page 78: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

78  

Call-by-Value

Evaluate function to closure

Evaluate argument to value

Call-by-Name

Delay argument evaluation

Krivine Machine (for CBN)

Delay function and argument:

Page 79: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

79  

Call-by-Value

Evaluate function to closure

Evaluate argument to value

Call-by-Name

Delay argument evaluation

1Krivine Machine (for CBN)

Delay function and argument:

Add all args to stack… 1

Page 80: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

80  

Call-by-Value

Evaluate function to closure

Evaluate argument to value

Call-by-Name

Delay argument evaluation

Krivine Machine (for CBN)

Delay function and argument:

Add all args to stack…

"CBN apply" left-to-right

1

2

1

2

Page 81: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

81  

Page 82: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

82  

Bidirectional Krivine Evaluation

Page 83: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

83  

Bidirectional Krivine Evaluation

All rules, including���function application,���

are "mechanical"

Backward evaluator���does not call forward���

evaluator at all!

Page 84: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

84  

Page 85: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

85  

Future Work: Bi-Eval as a BX

Mechanically derive backward���evaluators (or abstract machines)?

Page 86: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

86  

Future Work: Bi-Eval as a BX

Mechanically derive backward���evaluators (or abstract machines)?

Scale design and implementation���

of bi-evaluation to larger languages���

Page 87: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

87  

Future Work: Bi-Eval as a BX

Mechanically derive backward���evaluators (or abstract machines)?

Scale design and implementation���

of bi-evaluation to larger languages������

Bidirectional JavaScript���λ-calc + ref + proto objects + label/break + exceptions

Page 88: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

88  

Related Work Bidirectional Evaluation

Page 89: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

89  

Func. Program Slicing [Perera et al., ICFP 2012]

Forward evaluator records full trace for output value  

Backward evaluator slices exp. based on "don't-care" sub-values

Related Work Bidirectional Evaluation

Page 90: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

90  

Func. Program Slicing [Perera et al., ICFP 2012]

HOBiT [Matsuda and Wang, ESOP 2018]

Forward evaluator records full trace for output value  

Backward evaluator slices exp. based on "don't-care" sub-values

Instead of lens combinators, staged evaluator for λ-calculus

First evaluation stage eliminates function calls (e1 e2)

Bidirectional evaluation for "residual" first-order programs

More direct support for "branch switching" than our approach  

Related Work Bidirectional Evaluation

Page 91: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

Bidirectional Evaluation

OOPSLA 2018  

Bidirectional Programs���with are Lenses

91  

Page 92: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

Bidirectional Evaluation

OOPSLA 2018  

A Bidirectional Krivine Evaluator

Bx 2019  

Towards���Bi-Eval "For Free"

92  

Page 93: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

github.com/MikaelMayer/Editor  

Bidirectional Evaluation

OOPSLA 2018  

A Bidirectional Krivine Evaluator

Bx 2019  

ravichugh.github.io/sketch-­‐n-­‐sketch  

Sketch-n-Sketch Tharzen Editor

Page 94: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )

github.com/MikaelMayer/Editor  

Bidirectional Evaluation

OOPSLA 2018  

A Bidirectional Krivine Evaluator

Bx 2019  

Thanks!

ravichugh.github.io/sketch-­‐n-­‐sketch  

Sketch-n-Sketch

@MikaelMayer @ravi_chugh

Tharzen Editor

Page 95: A Bidirectional Krivine Evaluatorpeople.cs.uchicago.edu/~rchugh/static/talks/bx-2019-sns.pptx.pdf · Instead of lens combinators, staged evaluator for λ-calculus )