space-efficient manifest contractsmichael/papers/popl2015_space_presentatio… · manifest...

123
Space-Efficient Manifest Contracts Michael Greenberg Princeton University POPL 2015

Upload: others

Post on 10-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Space-EfficientManifest Contracts

Michael Greenberg Princeton University

POPL 2015

Page 2: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

(First-order) contracts• Specifications

• Written in code

• Checked at runtime

2

Page 3: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

(First-order) contracts

assert(n≥0)

• Specifications

• Written in code

• Checked at runtime

2

Page 4: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

(First-order) contracts

assert(n≥0)

sqrt : {x:Float | x≥0} → Float

• Specifications

• Written in code

• Checked at runtime

2

Page 5: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Higher-order contracts

({x:Int | x≥0} → {x:Int | x≥0}) → {y:Int | y≥0}

You give a function f on Nats, I return a Nat

“even-odd rule”—Findler and Felleisen

20023

Page 6: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Higher-order contracts

({x:Int | x≥0} → {x:Int | x≥0}) → {y:Int | y≥0}

You give a function f on Nats, I return a Nat

“even-odd rule”—Findler and Felleisen

20023

Page 7: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Higher-order contracts

({x:Int | x≥0} → {x:Int | x≥0}) → {y:Int | y≥0}

You give a function f on Nats, I return a NatIf you don’t get a Nat, oops—you blame me

“even-odd rule”—Findler and Felleisen

20023

Page 8: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Higher-order contracts

({x:Int | x≥0} → {x:Int | x≥0}) → {y:Int | y≥0}

You give a function f on Nats, I return a NatIf you don’t get a Nat, oops—you blame me

If f is called with a negative number, oops—you blame me

“even-odd rule”—Findler and Felleisen

20023

Page 9: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Higher-order contracts

({x:Int | x≥0} → {x:Int | x≥0}) → {y:Int | y≥0}

You give a function f on Nats, I return a NatIf you don’t get a Nat, oops—you blame me

If f is called with a negative number, oops—you blame me

If f returns a negative, oops—I blame you

“even-odd rule”—Findler and Felleisen

20023

Page 10: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Checking contracts at runtime

4

Nat 7

Page 11: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Checking contracts at runtime

4

Nat 7 7

Page 12: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Checking contracts at runtime

4

Nat 7 7

Nat -1

Page 13: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Checking contracts at runtime

4

Nat 7 7

Nat -1 blame

Page 14: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Checking contracts at runtime

4

Nat 7 7

Nat -1 blame

→Pos predPos

Page 15: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Checking contracts at runtime

4

Nat 7 7

Nat -1 blame

→Pos pred 1)( Pos

Page 16: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Checking contracts at runtime

4

Nat 7 7

Nat -1 blame

→Pos pred 1)(Pos( pred ( 1))

Pos

Pos

Page 17: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Checking contracts at runtime

4

Nat 7 7

Nat -1 blame

→Pos pred 1)(Pos( pred ( 1))

Pos

Pos blame

Page 18: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Bad space behavior

5

Nat → Nat f v)(

Nat (f( Nat v))

Page 19: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Bad space behavior

5

Nat → Nat f v)(

Nat (f( Nat v))My paper: a solution!

Page 20: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Function proxies

List...head : {l:α list| not (null l)}→α

Set...min : {l:α set| not (empty) l}→α

6

Page 21: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Function proxies

List...head : {l:α list| not (null l)}→α

Set...min : {l:α set| not (empty) l}→α

6

Page 22: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Function proxies

List

...head = fun x. …

Set

empty = null

min = head

7

not null → α )(

not empty → α )(

Page 23: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Tail calls

let odd = (λn:Int. if (n==0) then false else even (n-1))

let even = (λn:Int. if (n==0) then true else odd (n-1))

8

→→

Page 24: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Tail calls

let odd = (λn:Int. if (n==0) then false else even (n-1))

let even = (λn:Int. if (n==0) then true else odd (n-1))

8

→→

Page 25: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Tail calls

let odd = (λn:Int. if (n==0) then false else even (n-1))

let even = (λn:Int. if (n==0) then true else odd (n-1))

8

→→

Page 26: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

What tail calls?

even ( 3)

9

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 27: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

What tail calls?

even ( 3)

odd ( 2)

9

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 28: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

What tail calls?

even ( 3)

odd ( 2)

even ( 1)

9

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 29: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

What tail calls?

even ( 3)

odd ( 2)

even ( 1)

odd ( 0)

9

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 30: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

What tail calls?

even ( 3)

odd ( 2)

even ( 1)

odd ( 0)

false

9

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 31: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

What tail calls?

even ( 3)

odd ( 2)

even ( 1)

odd ( 0)

false

9

Contracts break

tail calls!

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 32: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Bad space behavior

Functional Programming - Tail Calls = Bad News

• Contracts change asymptotic space behavior• Big barrier to adoption

10

Page 33: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Space-efficientmanifest contracts

a semantics for manifest contracts

checks consume constant space

behave just like classic contracts

11

Page 34: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Westward the Course of Empire Takes Its Way Emanuel Leutze

Page 35: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Contracts Made Manifest

13

Are contracts

types?

ManifestLatent

yesno

Greenberg, Pierce, and Weirich POPL 2010

Page 36: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Casts

I know e has type T1

Treat it as type T2 If I’m wrong, blame ℓ

14

<T1⇒T2>ℓ e

Page 37: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Casts

<T1⇒T2>ℓ eB ::= Bool | ...T ::= {x:B | e} | T1→T2

15

Page 38: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Casts between refinements

<{x:Int | true}⇒{x:Int | x≥0}>ℓ 7 ⟼* 7

16

Page 39: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Casts between refinements

<{x:Int | true}⇒{x:Int | x≥0}>ℓ 7 ⟼* 7

<{x:Int | true}⇒{x:Int | x≥0}>ℓ -1 ⟼* blame ℓ

16

Page 40: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Casts between functions

<T1 → T2⇒U1 → U2>ℓ f

...is a value a/k/a function proxy.

17

Page 41: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Casts between functions

(<T1 → T2⇒U1 → U2>ℓ f) v ⟼

<T2⇒U2>ℓ (f (<U1⇒T1>ℓ v))

18

Page 42: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

19

Page 43: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

(<{x:Int|true}→{x:Int|true}⇒ {x:Int|x≥0}→{x:Int|x≥0}>ℓ λx:{x:Int|true}. x-1) 0 ⟼

19

Page 44: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

(<{x:Int|true}→{x:Int|true}⇒ {x:Int|x≥0}→{x:Int|x≥0}>ℓ λx:{x:Int|true}. x-1) 0 ⟼

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (λx:{x:Int|true}. x-1 (<{x:Int|x≥0}⇒{x:Int|true}>ℓ 0)) ⟼*

19

Page 45: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

(<{x:Int|true}→{x:Int|true}⇒ {x:Int|x≥0}→{x:Int|x≥0}>ℓ λx:{x:Int|true}. x-1) 0 ⟼

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (λx:{x:Int|true}. x-1 (<{x:Int|x≥0}⇒{x:Int|true}>ℓ 0)) ⟼*

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (λx:{x:Int|true}. x-1 0) ⟼*

19

Page 46: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

(<{x:Int|true}→{x:Int|true}⇒ {x:Int|x≥0}→{x:Int|x≥0}>ℓ λx:{x:Int|true}. x-1) 0 ⟼

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (λx:{x:Int|true}. x-1 (<{x:Int|x≥0}⇒{x:Int|true}>ℓ 0)) ⟼*

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (λx:{x:Int|true}. x-1 0) ⟼*

<{x:Int|true}⇒{x:Int|x≥0}>ℓ -1 ⟼* blame ℓ

19

Page 47: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

(<{x:Int|true}→{x:Int|true}⇒ {x:Int|x≥0}→{x:Int|x≥0}>ℓ λx:{x:Int|true}. x-1) 0 ⟼

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (λx:{x:Int|true}. x-1 (<{x:Int|x≥0}⇒{x:Int|true}>ℓ 0)) ⟼*

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (λx:{x:Int|true}. x-1 0) ⟼*

<{x:Int|true}⇒{x:Int|x≥0}>ℓ -1 ⟼* blame ℓ

19

Page 48: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Pop quiz

20

<(Nat→Nat)→Nat⇒(Pos→Pos)→Pos>ℓWhen we execute

will we check Nat or Pos in the domain’s domain?

Page 49: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #1: use coercions

21

<T1⇒T2>ℓ

Page 50: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Coercions between predicates

22

<{x:Int | true}⇒{x:Int | x≥0}>ℓ 7 ⟼* 7

<{x:Int | true}⇒{x:Int | x≥0}>ℓ -1 ⟼* blame ℓ

Page 51: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Coercions between predicates

22

<{x:Int | true}⇒{x:Int | x≥0}>ℓ 7 ⟼* 7

<{x:Int | true}⇒{x:Int | x≥0}>ℓ -1 ⟼* blame ℓ

Totally ignored!

Page 52: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Coercions between predicates

23

<{x:Int | true}⇒{x:Int | x≥0}>ℓ 7 ⟼* 7

<{x:Int | true}⇒{x:Int | x≥0}>ℓ -1 ⟼* blame ℓ

Nat

Page 53: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Coercions between functions

(<{x:Int|true}→{x:Int|true}⇒{x:Int|x≥0}→{x:Int|x≥0}>ℓ f) v⟼

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (f (<{x:Int|x≥0}⇒{x:Int|true}>ℓ v))

24

Page 54: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Coercions between functions

(<{x:Int|true}→{x:Int|true}⇒{x:Int|x≥0}→{x:Int|x≥0}>ℓ f) v⟼

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (f (<{x:Int|x≥0}⇒{x:Int|true}>ℓ v))

24

Nat

Page 55: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Coercions between functions

(<{x:Int|true}→{x:Int|true}⇒{x:Int|x≥0}→{x:Int|x≥0}>ℓ f) v⟼

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (f (<{x:Int|x≥0}⇒{x:Int|true}>ℓ v))

24

IntNat

Page 56: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Coercions between functions

25

Int Nat→

(<{x:Int|true}→{x:Int|true}⇒{x:Int|x≥0}→{x:Int|x≥0}>ℓ f) v⟼

<{x:Int|true}⇒{x:Int|x≥0}>ℓ (f (<{x:Int|x≥0}⇒{x:Int|true}>ℓ v))

Page 57: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Coercions between functions

26

<T1 → T2⇒U1 → U2>ℓ

→<U1⇒T1>ℓ <T2⇒U2>ℓ

Page 58: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Coercions between functions

26

<T1 → T2⇒U1 → U2>ℓ

→<U1⇒T1>ℓ <T2⇒U2>ℓ

Page 59: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Coercions between functions

26

<T1 → T2⇒U1 → U2>ℓ

→<U1⇒T1>ℓ <T2⇒U2>ℓ

Page 60: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Makeup exam

27

<(Nat→Nat)→Nat⇒(Pos→Pos)→Pos>ℓWhen we execute

will we check Nat or Pos in the domain’s domain?

Page 61: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Makeup exam

27

<(Nat→Nat)→Nat⇒(Pos→Pos)→Pos>ℓWhen we execute

Pos→Pos→( )Nat

will we check Nat or Pos in the domain’s domain?

Page 62: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Bodies in Urban Spaces Willi Dorner / Studio 70

Page 63: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #2: avoid redundant checks

29

Nat 6EvenNat

Page 64: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #2: avoid redundant checks

29

Nat 6EvenNat 6EvenNat

Page 65: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #2: avoid redundant checks

29

Nat 6EvenNat 6EvenNat 6Nat

Page 66: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #2: avoid redundant checks

29

Nat 6Even

6

Nat 6EvenNat 6Nat

Page 67: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #2: avoid redundant checks

29

Nat 6Even

6

Nat 6EvenNat

Nat 7EvenNat

6Nat

Page 68: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #2: avoid redundant checks

29

Nat 6Even

6

Nat 6EvenNat

Nat 7EvenNat 7EvenNat

6Nat

Page 69: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #2: avoid redundant checks

29

blame

Nat 6Even

6

Nat 6EvenNat

Nat 7EvenNat 7EvenNat

6Nat

Page 70: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #2: avoid redundant checks

29

blame

Nat 6Even

6

Nat 6EvenNat

Nat 7EvenNat

Nat -1EvenNat

7EvenNat

6Nat

Page 71: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #2: avoid redundant checks

29

blame

blame

Nat 6Even

6

Nat 6EvenNat

Nat 7EvenNat

Nat -1EvenNat

7EvenNat

6Nat

Page 72: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Insight #2: avoid redundant checks

29

blame

blame

Nat 6Even

6

Never fails!

Nat 6EvenNat

Nat 7EvenNat

Nat -1EvenNat

7EvenNat

6Nat

Page 73: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

30

even ( 3)

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 74: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

30

even ( 3)

odd ( 2)

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 75: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

30

even ( 3)

odd ( 2)

even ( 1)

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 76: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

30

even ( 3)

odd ( 2)

even ( 1)

odd ( 0)

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 77: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

30

even ( 3)

odd ( 2)

even ( 1)

odd ( 0)

Redundant!

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 78: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

30

even ( 3)

odd ( 2)

even ( 1)

odd ( 0)

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 79: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

30

even ( 3)

odd ( 2)

even ( 1)

odd ( 0)

false

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 80: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

30

even ( 3)

odd ( 2)

even ( 1)

odd ( 0)

false

Redundant!

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 81: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

30

even ( 3)

odd ( 2)

even ( 1)

odd ( 0)

false

let odd = (λn:Int. … even (n-1))

let even = (λn:Int. … odd (n-1))

→→

Page 82: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Redundant checks• Same color — same check

• Formally: decidable pre-order on refinement types

• Is this enough?

31

Page 83: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

How many checks?

Finitely many

…because of simple types!

Page 84: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

How many checks?

Finitely many

…because of simple types!

Types:

Page 85: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Bounds

Finitely many types

Appear once, at most

What’s the worst that can happen?

Types:

Page 86: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Bounds

Finitely many types

Appear once, at most

What’s the worst that can happen?

→→

Types:

Page 87: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Bounds

Finitely many types

Appear once, at most

What’s the worst that can happen?

→→

Types:

Page 88: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Bounds

Finitely many types

Appear once, at most

What’s the worst that can happen?

→→

Types:

Page 89: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Bounds

Finitely many types

Appear once, at most

What’s the worst that can happen?

→→

Types:

Page 90: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

How do we merge lists of checks?

34

ee

Invariant: the checks on the stack have no redundancy.

We’ll merge the new checks in, dropping redundant checks.

Page 91: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Eliminating redundant checks

How do we merge lists of checks?

34

e e’( )e

Invariant: the checks on the stack have no redundancy.

We’ll merge the new checks in, dropping redundant checks.

Page 92: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

How do we merge lists of checks?

35

ee

+ = ?

Page 93: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

How do we merge lists of checks?

35

e e’( )e

+ = ?

Page 94: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

How do we merge lists of checks?

35

e e’( )e

+ = ?new

Page 95: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

How do we merge lists of checks?

35

e e’( )e

+ = ?new old

Page 96: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

36

+ =

e e’( )e

How do we merge lists of checks?

Page 97: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

36

+ =

e e’( )e

How do we merge lists of checks?

Page 98: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

36

+ =

e e’( )e

How do we merge lists of checks?

Page 99: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

36

+ =

e e’( )e

How do we merge lists of checks?

Page 100: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

36

+ =

e e’( )e

How do we merge lists of checks?

Page 101: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

36

+ =

e e’( )e

How do we merge lists of checks?

Page 102: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

36

+ =

e e’( )e

How do we merge lists of checks?

Page 103: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

36

+ =

e e’( )e

How do we merge lists of checks?

Page 104: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging, in detail

+ =

new old

How do we merge lists of checks?

e e’( )e

Go from new to old

Drop redundant checks on the old coercion

Page 105: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function proxies

38

→→ )( f( )v→ )( f v

Page 106: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function proxies

38

→→ )( f( )v→ )( f v

Page 107: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function proxies

38

→→ )( f( )v

→ )( f v( )

→ )( f v

Page 108: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function proxies

38

→→ )( f( )v

→ )( f v( )

→ )( f v

Page 109: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function proxies

38

→→ )( f( )v

→ )( f v( )

→ )( f v

(f v)( )

Page 110: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function proxies

38

→→ )( f( )v

→ )( f v( )

→ )( f v

(f v)( )

oldnew

Page 111: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function checks

39

→ →+ =

Domain: new to oldCodomain: old to new

new old

Page 112: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function checks

39

→ →+ =

Domain: new to oldCodomain: old to new

→+

new old

Page 113: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function checks

39

→ →+ =

Domain: new to oldCodomain: old to new

→+

new old

+

Page 114: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function checks

40

→ →+ =

Domain: new to oldCodomain: old to new

new old

Page 115: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Merging function checks

40

→ →+ =

Domain: new to oldCodomain: old to new

new oldGo from right to left

Drop redundant checks

Contravariance

Page 116: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Proofs

41

Lemma [Eidetic canonical forms (A.15)]: If ; `E e : T andvalE e then:

– If T = {x :B | e

0}, then e = k and ty(k) = B ande

0[e/x ] �!⇤

E true.– If T = T21!T22, then either e = �x :T . e

0 or e =

hT11!T12c1 7!c2) T21!T22i• �x :T11. e

0.

Lemma [Eidetic progress (A.16)]: If ; `E e : T , then either:

1. resultE e , i.e., e = *l or valE e; or2. there exists an e

0 such that e �!E e

0.

Lemma [Eidetic preservation (A.20)]: If ; `E e : T and e �!E

e

0 then ; `E e

0: T .

Eidetic �H shares source programs (Definition 3.1) with classic�H. We can therefore say that classic and eidetic �H are really justmodes of a single language.

Lemma [Source program typing for eidetic �H (A.21)]:Source programs are well typed in C iff they are well typed in E,i.e.:

– � `C e : T as a source program iff � `E e : T as a sourceprogram.

– `C T as a source program iff `E T as a source program.– `C � as a source program iff `E � as a source program.

5. Soundness for space efficiencyWe want space efficiency to be sound: it would be space efficientto never check anything. Classic �H is normative: the more a modebehaves like classic �H, the “sounder” it is.

A single property summarizes how a space-efficient calculusbehaves with respect to classic �H: cast congruence. In classic�H, if e1 �!C e2 then hT1

•)T2il e1 and hT1•)T2il e2 be-

have identically. This cast congruence principle is easy to see,because E CASTINNERC applies freely. In eidetic �H, however,E CASTINNER can only apply when E CASTMERGEE doesn’t.Merged casts may not behave the same as running the two castsseparately. Eidetic �H recovers a complete cast congruence, justlike classic �H has. Diagrammatically:

e1 e2

+

hT1•)T2il e1 hT1

•)T2il e2

resultE e

E

⇤E ⇤

E

The proof is in Appendix B, but it is worth observing here thateidetic �H needs a proof of idempotency to justify the way it usesreflexivity to eliminate redundant coercions: checking a propertyonce is as good as checking it twice. Naturally, this property onlyholds without state.

Our proofs relating classic �H and eidetic �H are by logicalrelations, found in Figure 6. In the extended version, the sound-ness proofs for all three different space-efficient modes use a singlemode-indexed logical relation. Here we give its restriction to eidetic�H. As far as alternative techniques go, an induction over evalua-tion derivations wouldn’t give us enough information about evalua-tions that return lambda abstractions. Other contextual equivalencetechniques (e.g., bisimulation) would probably work, too.

Value rules e1 ⇠E e2 : T

k ⇠E k : {x :B | e} () ty(k) = B ^ e[k/x ] �!⇤E true

e11 ⇠E e21 : T1!T2 () valC e1 ^ valE e2 ^8e12 ⇠E e22 : T1. e11 e12 'E e21 e22 : T2

Term rules e1 'E e2 : T

e1 'E e2 : T

()✓e1 �!⇤

C *l ^e2 �!⇤

E *l

◆_

0

@e1 �!⇤

C e

01 ^ valC e

01 ^

e2 �!⇤E e

02 ^ valE e

02 ^

e

01 ⇠E e

02 : T

1

A

Type rules T1 ⇠E T2

{x :B | e1} ⇠E {x :B | e2} ()8e01 ⇠E e

02 : {x :B | true}. e1[e01/x ] 'E e2[e

02/x ] : {x :Bool | true}

T11!T12 ⇠E T21!T22 () T11 ⇠E T21 ^ T12 ⇠E T22

Closing substitutions and open terms � |=E �

� ` e1 'E e2 : T

� |=E � () 8x 2 dom(�). �1(x) ⇠E �2(x) : �(x)

� ` e1 'E e2 : T () 8� |=E �. �1(e1) 'E �2(e2) : T

Figure 6. Blame-exact, symmetric logical relation between classic�H and eidetic �H

Mode Cast size Pending castsClassic (m = C) 2Wh + L 1Eidetic (m = E) s2

L+WB |e|

Table 1. Space efficiency of �H

Lemma [Similar casts are logically related (B.3)]: If T1 ⇠E T

01

and T2 ⇠E T

02 and e1 ⇠E e2 : T1, then hT1

•)T2il e1 'E

hT 01

•)T

02il e2 : T2.

Lemma [Relating classic and eidetic source programs (B.4)]:

1. If � `C e : T as a source program then � ` e 'E e : T .2. If `C T as a source program then T ⇠E T .

6. Bounds for space efficiencyWe have claimed that eidetic �H is space efficient: what do wemean? What sort of space efficiency have we achieved? We sum-marize the results in Table 1; proofs are in Appendix C. From ahigh level, there are only a finite number of types that appear inour programs, and this set of types can only reduce as the programruns. We can effectively code each type in the program as an inte-ger, allowing us to efficiently run the � predicate.

Suppose that a type of height h can be represented in Wh bitsand a label in L bits. (Type heights are defined in Figure 7 inAppendix C.) Casts in classic �H each take up 2Wh + L bits: twotypes and a blame label. Coercions in eidetic �H have a differentform: the only types recorded are those of height 1, i.e., refinementsof base types. Pessimistically, each of these may appear at everyposition in a function coercion c1 7! c2. We use s to indicate the“size” of a function type, i.e., the number of positions it has. Asa first pass, a set of refinements and blame labels take up 2

L+W1

space. But in fact these coercions must all be between refinements

Soundness Congruence lemma

Classic semantics and

space-efficient semantics behave identically

Page 117: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Congruence lemma

42

e e’

e’’

e e’

Page 118: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Congruence lemma

43

e1 e1’

e’’

e1’e1( ) ( )

Our step: check

Page 119: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Congruence lemma

44

e1 e1’

e’’

e1’e1( ) ( )

e1 e1’

Our step: check

Page 120: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Outlook

45

Types:

• Use coercions, not casts

• Merge redundant checks

+ =

Page 121: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Outlook• Can we scale to dependency?

46

Types:+ =

• Simple types—finite number

• Dependent types—infinite number

Page 122: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Outlook• Can we scale to dependency and effects?

47

• Idea: partial orders/lattices

+ =

≤ 5

≤ 6

≤ 7

≤ 8

≤ 9

keep lowest check

≤ xx: →

Page 123: Space-Efficient Manifest Contractsmichael/papers/popl2015_space_presentatio… · Manifest Contracts Michael Greenberg Princeton University POPL 2015 (First-order) contracts

Space-EfficientManifest Contracts

Michael Greenberg Princeton University

POPL 2015