209
Parametric Polymorphism Through Run-time Sealing or, Theorems for Low, Low Prices! Amal Ahmed Lindsey Kuper Jacob Matthews Northeastern University Programming Languages Seminar February 23, 2011 1 Thursday, February 24, 2011

Parametric Polymorphism Through Run-time Sealinglkuper/talks/multilang-param/multilang... · Parametric Polymorphism Through Run-time Sealing or, ... Northeastern University Programming

Embed Size (px)

Citation preview

Parametric Polymorphism Through Run-time Sealingor, Theorems for Low, Low Prices!

Amal AhmedLindsey Kuper

Jacob Matthews

Northeastern UniversityProgramming Languages Seminar

February 23, 20111

Thursday, February 24, 2011

What is parametricity?

2

Thursday, February 24, 2011

Data abstraction

3

Thursday, February 24, 2011

Data abstraction

3

Separation of implementation and interface

Thursday, February 24, 2011

Data abstraction

3

Counter = ∃α. {new : α, inc : α → α, get : α → Nat}

Separation of implementation and interface

Thursday, February 24, 2011

Data abstraction

3

Counter = ∃α. {new : α, inc : α → α, get : α → Nat}

c1 = {new = 0, inc = λx: Nat. x + 1, get = λx: Nat. x}

ctr1 = pack Nat, c1 as Counter

Separation of implementation and interface

Thursday, February 24, 2011

Data abstraction

3

Counter = ∃α. {new : α, inc : α → α, get : α → Nat}

c1 = {new = 0, inc = λx: Nat. x + 1, get = λx: Nat. x}

ctr1 = pack Nat, c1 as Counter

c2 = {new = 0, inc = λx: Int. x - 1, get = λx: Int. toNat(0 - x)}

ctr2 = pack Int, c2 as Counter

Separation of implementation and interface

Thursday, February 24, 2011

Data abstraction

3

Counter = ∃α. {new : α, inc : α → α, get : α → Nat}

c1 = {new = 0, inc = λx: Nat. x + 1, get = λx: Nat. x}

ctr1 = pack Nat, c1 as Counter

c2 = {new = 0, inc = λx: Int. x - 1, get = λx: Int. toNat(0 - x)}

ctr2 = pack Int, c2 as Counter

indistinguishable

Separation of implementation and interface

Thursday, February 24, 2011

Existential types...

4

c1 = {new = 0, inc = λx: Nat. x + 1, get = λx: Nat. x}

ctr1 = pack Nat, c1 as Counter

c2 = {new = 0, inc = λx: Int. x - 1, get = λx: Int. toNat(0 - x)}

ctr2 = pack Int, c2 as Counter

indistinguishable

Thursday, February 24, 2011

Existential types...

4

c1 = {new = 0, inc = λx: Nat. x + 1, get = λx: Nat. x}

ctr1 = pack Nat, c1 as Counter

c2 = {new = 0, inc = λx: Int. x - 1, get = λx: Int. toNat(0 - x)}

ctr2 = pack Int, c2 as Counter

indistinguishable

■ If two expressions have the same existential type, no program context can distinguish them.

Thursday, February 24, 2011

Existential types...

4

c1 = {new = 0, inc = λx: Nat. x + 1, get = λx: Nat. x}

ctr1 = pack Nat, c1 as Counter

c2 = {new = 0, inc = λx: Int. x - 1, get = λx: Int. toNat(0 - x)}

ctr2 = pack Int, c2 as Counter

indistinguishable

■ If two expressions have the same existential type, no program context can distinguish them.

∃α.τ ctr1

∃α.τ ctr2

client

Thursday, February 24, 2011

Existential types...and their dual, universal types

5

■ No two program contexts (instantiations) can cause an expression of type ∀α.τ to behave differently.

Λα.e

Natvalues

Intvalues

■ If two expressions have the same existential type, no program context can distinguish them.

∃α.τ ctr1

∃α.τ ctr2

client

Thursday, February 24, 2011

Existential types...and their dual, universal types

6

■ No two program contexts (instantiations) can cause an expression of type ∀α.τ to behave differently.

Λα.e

Natvalues

Intvalues

Thursday, February 24, 2011

Existential types...and their dual, universal types

6

f : ∀α. α → α

■ No two program contexts (instantiations) can cause an expression of type ∀α.τ to behave differently.

Λα.e

Natvalues

Intvalues

Thursday, February 24, 2011

Existential types...and their dual, universal types

6

f : ∀α. α → α

Nat

■ No two program contexts (instantiations) can cause an expression of type ∀α.τ to behave differently.

Λα.e

Natvalues

Intvalues

Thursday, February 24, 2011

Existential types...and their dual, universal types

6

f : ∀α. α → α

Nat Int

■ No two program contexts (instantiations) can cause an expression of type ∀α.τ to behave differently.

Λα.e

Natvalues

Intvalues

Thursday, February 24, 2011

Existential types...and their dual, universal types

6

f : ∀α. α → α

Nat Int

indistinguishable asfar as f is concerned

■ No two program contexts (instantiations) can cause an expression of type ∀α.τ to behave differently.

Λα.e

Natvalues

Intvalues

Thursday, February 24, 2011

Existential types...and their dual, universal types

6

f : ∀α. α → α

Nat Int

indistinguishable asfar as f is concerned

Bool

■ No two program contexts (instantiations) can cause an expression of type ∀α.τ to behave differently.

Λα.e

Natvalues

Intvalues

Thursday, February 24, 2011

Existential types...and their dual, universal types

6

f : ∀α. α → α

Nat Int

indistinguishable asfar as f is concerned

Bool%*$@!

■ No two program contexts (instantiations) can cause an expression of type ∀α.τ to behave differently.

Λα.e

Natvalues

Intvalues

Thursday, February 24, 2011

Existential types...and their dual, universal types

6

f : ∀α. α → α

Nat Int

indistinguishable asfar as f is concerned

f = Λα. λx: α. x

Bool%*$@!

■ No two program contexts (instantiations) can cause an expression of type ∀α.τ to behave differently.

Λα.e

Natvalues

Intvalues

Thursday, February 24, 2011

Existential types...and their dual, universal types

7

Thursday, February 24, 2011

Existential types...and their dual, universal types

7

parametricity

∃α.τ

∀α.τ

representation independence

Thursday, February 24, 2011

Breaking parametricity

8

Thursday, February 24, 2011

9

How to break parametricity in one easy step

Λα. λx: α. (if (nat? x) (+ x 1) x)

Thursday, February 24, 2011

9

How to break parametricity in one easy step

Λα. λx: α. (if (nat? x) (+ x 1) x)

behaves differently at run-time depending on how α is instantiated

Thursday, February 24, 2011

9

How to break parametricity in one easy step

Λα. λx: α. (if (nat? x) (+ x 1) x)

behaves differently at run-time depending on how α is instantiated

Putting dynamically typed code in an otherwise statically typed program

provides a way to “smuggle values past the type system”

(Abadi et al., 1989)

Thursday, February 24, 2011

A two-language system

10

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!!

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!!

!

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!!

!!. "!

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!!

!1 !2

!!. "!

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!!

...

!1 !2

!!. "!

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!!

(!"!!)

...

!1 !2

!!. "!

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!

!

!

(!"!!)

...

!1 !2

!!. "!

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!

!! : " . "!

!

(!"!!)

...

!1 !2

!!. "!

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!

!1 !2

!! : " . "!

!

(!"!!)

...

!1 !2

!!. "!

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!

...

!1 !2

!! : " . "!

!

(!"!!)

...

!1 !2

!!. "!

Thursday, February 24, 2011

A two-language system

10

■ How can we assign a type to a program that’s written in two languages?

■ We’ll combine a minimal “Scheme” and a minimal “ML” in a multi-language embedding (Matthews & Findler, 2007):

!

(!!" !)...

!1 !2

!! : " . "!

!

(!"!!)

...

!1 !2

!!. "!

Thursday, February 24, 2011

Using a Scheme procedure in ML

11

(!1!!2!" (!!. ")) !"# (!! : "1. (!2!" (!!. ") ("!!1 !)))

Thursday, February 24, 2011

Using a Scheme procedure in ML

11

(!1!!2!" (!!. ")) !"# (!! : "1. (!2!" (!!. ") ("!!1 !)))

have to choose some type at which to embed the procedure

Thursday, February 24, 2011

Using a Scheme procedure in ML

11

(!1!!2!" (!!. ")) !"# (!! : "1. (!2!" (!!. ") ("!!1 !)))

have to choose some type at which to embed the procedure

Thursday, February 24, 2011

Using a Scheme procedure in ML

11

(!1!!2!" (!!. ")) !"# (!! : "1. (!2!" (!!. ") ("!!1 !)))

have to choose some type at which to embed the procedure

Thursday, February 24, 2011

Using a Scheme procedure in ML

11

(!1!!2!" (!!. ")) !"# (!! : "1. (!2!" (!!. ") ("!!1 !)))

direction of conversion reverses for arguments

have to choose some type at which to embed the procedure

Thursday, February 24, 2011

A first attempt at polymorphism

12

(!!. "!" (!!. ")) !"# (!". ("

!" (!!. ")))

Thursday, February 24, 2011

A first attempt at polymorphism

12

(!!. "!" (!!. ")) !"# (!". ("

!" (!!. ")))

embedding a Scheme procedure in ML at a universal type

Thursday, February 24, 2011

A first attempt at polymorphism

12

(!!. "!" (!!. ")) !"# (!". ("

!" (!!. ")))

embedding a Scheme procedure in ML at a universal type

Thursday, February 24, 2011

A first attempt at polymorphism

12

(!!. "!" (!!. ")) !"# (!". ("

!" (!!. ")))

embedding a Scheme procedure in ML at a universal type

Thursday, February 24, 2011

A first attempt at polymorphism

12

(!!. "!" (!!. ")) !"# (!". ("

!" (!!. ")))

embedding a Scheme procedure in ML at a universal type

evaluation stops here, and continues when we apply to a concrete type:(!!. !) "#$ !"# ![! := "#$]

Thursday, February 24, 2011

(!!. !"!!" (!!. !)) !"# 3

!" (!". (!"!!" (!!. !)) !"# 3

!" (!"#"!"#!" (!!. !)) 3

!" (!$ : !"#. (!"#!" (!!. !) ("!!"# $))) 3

!" (!"#!" (!!. !) ("!!"# 3))

!" (!"#!" (!!. !) 3)

!" (!"#!" 3)

!" 3

.

A first attempt at polymorphism: example

13

Thursday, February 24, 2011

(!!. !"!!" (!!. !)) !"# 3

!" (!". (!"!!" (!!. !)) !"# 3

!" (!"#"!"#!" (!!. !)) 3

!" (!$ : !"#. (!"#!" (!!. !) ("!!"# $))) 3

!" (!"#!" (!!. !) ("!!"# 3))

!" (!"#!" (!!. !) 3)

!" (!"#!" 3)

!" 3

.

A first attempt at polymorphism: example

13

Thursday, February 24, 2011

(!!. !"!!" (!!. !)) !"# 3

!" (!". (!"!!" (!!. !)) !"# 3

!" (!"#"!"#!" (!!. !)) 3

!" (!$ : !"#. (!"#!" (!!. !) ("!!"# $))) 3

!" (!"#!" (!!. !) ("!!"# 3))

!" (!"#!" (!!. !) 3)

!" (!"#!" 3)

!" 3

.

A first attempt at polymorphism: example

13

Thursday, February 24, 2011

(!!. !"!!" (!!. !)) !"# 3

!" (!". (!"!!" (!!. !)) !"# 3

!" (!"#"!"#!" (!!. !)) 3

!" (!$ : !"#. (!"#!" (!!. !) ("!!"# $))) 3

!" (!"#!" (!!. !) ("!!"# 3))

!" (!"#!" (!!. !) 3)

!" (!"#!" 3)

!" 3

.

A first attempt at polymorphism: example

13

Thursday, February 24, 2011

(!!. !"!!" (!!. !)) !"# 3

!" (!". (!"!!" (!!. !)) !"# 3

!" (!"#"!"#!" (!!. !)) 3

!" (!$ : !"#. (!"#!" (!!. !) ("!!"# $))) 3

!" (!"#!" (!!. !) ("!!"# 3))

!" (!"#!" (!!. !) 3)

!" (!"#!" 3)

!" 3

.

A first attempt at polymorphism: example

13

Thursday, February 24, 2011

(!!. !"!!" (!!. !)) !"# 3

!" (!". (!"!!" (!!. !)) !"# 3

!" (!"#"!"#!" (!!. !)) 3

!" (!$ : !"#. (!"#!" (!!. !) ("!!"# $))) 3

!" (!"#!" (!!. !) ("!!"# 3))

!" (!"#!" (!!. !) 3)

!" (!"#!" 3)

!" 3

.

A first attempt at polymorphism: example

13

first-order values are assumed to be

convertible

Thursday, February 24, 2011

(!!. !"!!" (!!. !)) !"# 3

!" (!". (!"!!" (!!. !)) !"# 3

!" (!"#"!"#!" (!!. !)) 3

!" (!$ : !"#. (!"#!" (!!. !) ("!!"# $))) 3

!" (!"#!" (!!. !) ("!!"# 3))

!" (!"#!" (!!. !) 3)

!" (!"#!" 3)

!" 3

.

A first attempt at polymorphism: example

13

first-order values are assumed to be

convertible

Thursday, February 24, 2011

(!!. !"!!" (!!. !)) !"# 3

!" (!". (!"!!" (!!. !)) !"# 3

!" (!"#"!"#!" (!!. !)) 3

!" (!$ : !"#. (!"#!" (!!. !) ("!!"# $))) 3

!" (!"#!" (!!. !) ("!!"# 3))

!" (!"#!" (!!. !) 3)

!" (!"#!" 3)

!" 3

.

A first attempt at polymorphism: example

13

first-order values are assumed to be

convertible

Thursday, February 24, 2011

(!!. !"!!" (!!. !)) !"# 3

!" (!". (!"!!" (!!. !)) !"# 3

!" (!"#"!"#!" (!!. !)) 3

!" (!$ : !"#. (!"#!" (!!. !) ("!!"# $))) 3

!" (!"#!" (!!. !) ("!!"# 3))

!" (!"#!" (!!. !) 3)

!" (!"#!" 3)

!" 3

.

A first attempt at polymorphism: example

13

first-order values are assumed to be

convertible

Thursday, February 24, 2011

(!!. !"!!" (!!. !)) !"# 3

!" (!". (!"!!" (!!. !)) !"# 3

!" (!"#"!"#!" (!!. !)) 3

!" (!$ : !"#. (!"#!" (!!. !) ("!!"# $))) 3

!" (!"#!" (!!. !) ("!!"# 3))

!" (!"#!" (!!. !) 3)

!" (!"#!" 3)

!" 3

.

A first attempt at polymorphism: example

13

first-order values are assumed to be

convertible

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (!"#"!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!!"# $)))

# (!$ : !"#. (!"#!" (!!. (+ ! 1)) ("!!"# $)))

How parametricity breaks

14

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (!"#"!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!!"# $)))

# (!$ : !"#. (!"#!" (!!. (+ ! 1)) ("!!"# $)))

How parametricity breaks

14

well-typed expression of type ∀α. α → α

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (!"#"!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!!"# $)))

# (!$ : !"#. (!"#!" (!!. (+ ! 1)) ("!!"# $)))

How parametricity breaks

14

well-typed expression of type ∀α. α → α

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (!"#"!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!!"# $)))

# (!$ : !"#. (!"#!" (!!. (+ ! 1)) ("!!"# $)))

How parametricity breaks

14

well-typed expression of type ∀α. α → α

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (!"#"!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!!"# $)))

# (!$ : !"#. (!"#!" (!!. (+ ! 1)) ("!!"# $)))

How parametricity breaks

14

well-typed expression of type ∀α. α → α

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (!"#"!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!!"# $)))

# (!$ : !"#. (!"#!" (!!. (+ ! 1)) ("!!"# $)))

How parametricity breaks

14

well-typed expression of type ∀α. α → α

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (!"#"!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!!"# $)))

# (!$ : !"#. (!"#!" (!!. (+ ! 1)) ("!!"# $)))

How parametricity breaks

14

well-typed expression of type ∀α. α → α

not the identity function!

Thursday, February 24, 2011

What went wrong?

15

not the identity function!

The problem:Scheme is able to observe the concrete

choice of type for α and behave accordingly.

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (!"#"!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!!"# $)))

# (!$ : !"#. (!"#!" (!!. (+ ! 1)) ("!!"# $)))

Thursday, February 24, 2011

Restoring parametricity

16

Thursday, February 24, 2011

Data abstraction, revisited

17

Thursday, February 24, 2011

Data abstraction, revisited

■ Using type abstraction to enforce data abstraction is a static, compile-time approach

17

Thursday, February 24, 2011

Data abstraction, revisited

■ Using type abstraction to enforce data abstraction is a static, compile-time approach

17

c1 = {new = 0, inc = λx: Nat. x + 1, get = λx: Nat. x}

ctr1 = pack Nat, c1 as Counter

c2 = {new = 0, inc = λx: Int. x - 1, get = λx: Int. toNat(0 - x)}

ctr2 = pack Int, c2 as Counter

indistinguishable

Thursday, February 24, 2011

Data abstraction, revisited

■ Using type abstraction to enforce data abstraction is a static, compile-time approach

17

c1 = {new = 0, inc = λx: Nat. x + 1, get = λx: Nat. x}

ctr1 = pack Nat, c1 as Counter

c2 = {new = 0, inc = λx: Int. x - 1, get = λx: Int. toNat(0 - x)}

ctr2 = pack Int, c2 as Counter

indistinguishableat compile time

Thursday, February 24, 2011

Another approach to data abstraction

18

Thursday, February 24, 2011

Another approach to data abstraction

18

■ Programs can create unique seals in their local scope and hand out opaque, sealed values to clients

Thursday, February 24, 2011

Another approach to data abstraction

18

■ Programs can create unique seals in their local scope and hand out opaque, sealed values to clients

(define create-seal) (gensym))

(define (seal-value v seal) (lambda (s) (if (eq? s seal) v (error ...))))

(define (unseal sealed-v seal) (sealed-v seal))

Thursday, February 24, 2011

Another approach to data abstraction

18

■ Programs can create unique seals in their local scope and hand out opaque, sealed values to clients

(define create-seal) (gensym))

(define (seal-value v seal) (lambda (s) (if (eq? s seal) v (error ...))))

(define (unseal sealed-v seal) (sealed-v seal))

sealed value 2

client

sealed value 1

Thursday, February 24, 2011

Another approach to data abstraction

18

■ Programs can create unique seals in their local scope and hand out opaque, sealed values to clients

(define create-seal) (gensym))

(define (seal-value v seal) (lambda (s) (if (eq? s seal) v (error ...))))

(define (unseal sealed-v seal) (sealed-v seal))

sealed value 2

client

sealed value 1

indistinguishableat run-time

Thursday, February 24, 2011

! (!". !) # !"# !, s ![" := $s; #%]

Updating our system to use dynamic sealing

■ Operational semantics defined not just on expressions, but on configurations that include a seal store

19

Thursday, February 24, 2011

! (!". !) # !"# !, s ![" := $s; #%]

Updating our system to use dynamic sealing

■ Operational semantics defined not just on expressions, but on configurations that include a seal store

19

Thursday, February 24, 2011

! (!". !) # !"# !, s ![" := $s; #%]

Updating our system to use dynamic sealing

■ Operational semantics defined not just on expressions, but on configurations that include a seal store

19

contains all seals generatedduring evaluation so far

Thursday, February 24, 2011

! (!". !) # !"# !, s ![" := $s; #%]

Updating our system to use dynamic sealing

■ Operational semantics defined not just on expressions, but on configurations that include a seal store

19

contains all seals generatedduring evaluation so far

Thursday, February 24, 2011

! (!". !) # !"# !, s ![" := $s; #%]

Updating our system to use dynamic sealing

■ Operational semantics defined not just on expressions, but on configurations that include a seal store

19

instead of regular type substitution, sealing

substitution

contains all seals generatedduring evaluation so far

Thursday, February 24, 2011

! (!". !) # !"# !, s ![" := $s; #%]

Updating our system to use dynamic sealing

■ Operational semantics defined not just on expressions, but on configurations that include a seal store

19

if you think this looks stateful, you’re right

instead of regular type substitution, sealing

substitution

contains all seals generatedduring evaluation so far

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ $)))

# (!$ : !"#. (#s;!"#$!" ("!#s;!"#$ $)))

!" (!$ : !"#. $)

Back to our example...

20

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ $)))

# (!$ : !"#. (#s;!"#$!" ("!#s;!"#$ $)))

!" (!$ : !"#. $)

Back to our example...

20

well-typed expression of type ∀α. α → α

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ $)))

# (!$ : !"#. (#s;!"#$!" ("!#s;!"#$ $)))

!" (!$ : !"#. $)

Back to our example...

20

well-typed expression of type ∀α. α → α

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ $)))

# (!$ : !"#. (#s;!"#$!" ("!#s;!"#$ $)))

!" (!$ : !"#. $)

Back to our example...

20

well-typed expression of type ∀α. α → α

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ $)))

# (!$ : !"#. (#s;!"#$!" ("!#s;!"#$ $)))

!" (!$ : !"#. $)

Back to our example...

20

well-typed expression of type ∀α. α → α

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ $)))

# (!$ : !"#. (#s;!"#$!" ("!#s;!"#$ $)))

!" (!$ : !"#. $)

Back to our example...

20

well-typed expression of type ∀α. α → α

opaque value

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ $)))

# (!$ : !"#. (#s;!"#$!" ("!#s;!"#$ $)))

!" (!$ : !"#. $)

Back to our example...

20

well-typed expression of type ∀α. α → α

opaque value

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ $)))

# (!$ : !"#. (#s;!"#$!" ("!#s;!"#$ $)))

!" (!$ : !"#. $)

Back to our example...

20

well-typed expression of type ∀α. α → α

opaque value

Thursday, February 24, 2011

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"#

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"#

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)))

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ $)))

# (!$ : !"#. (#s;!"#$!" ("!#s;!"#$ $)))

!" (!$ : !"#. $)

Back to our example...

20

well-typed expression of type ∀α. α → α

the identity function! :D

opaque value

Thursday, February 24, 2011

Another example

21

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2)))) !"# 5

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) 5

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ $))) 5

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ 5))

!"% (#s;!"#$!" 2)

!" %&&'&( )&*+,&-./

Thursday, February 24, 2011

Another example

21

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2)))) !"# 5

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) 5

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ $))) 5

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ 5))

!"% (#s;!"#$!" 2)

!" %&&'&( )&*+,&-./

Thursday, February 24, 2011

Another example

21

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2)))) !"# 5

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) 5

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ $))) 5

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ 5))

!"% (#s;!"#$!" 2)

!" %&&'&( )&*+,&-./

Thursday, February 24, 2011

Another example

21

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2)))) !"# 5

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) 5

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ $))) 5

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ 5))

!"% (#s;!"#$!" 2)

!" %&&'&( )&*+,&-./

Thursday, February 24, 2011

Another example

21

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2)))) !"# 5

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) 5

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ $))) 5

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ 5))

!"% (#s;!"#$!" 2)

!" %&&'&( )&*+,&-./

Thursday, February 24, 2011

Another example

21

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2)))) !"# 5

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) 5

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ $))) 5

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ 5))

!"% (#s;!"#$!" 2)

!" %&&'&( )&*+,&-./

Thursday, February 24, 2011

Another example

21

can’t unseal something that isn’t a seal

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2)))) !"# 5

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) 5

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ $))) 5

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ 5))

!"% (#s;!"#$!" 2)

!" %&&'&( )&*+,&-./

Thursday, February 24, 2011

Another example

21

can’t unseal something that isn’t a seal

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2)))) !"# 5

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) 5

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ $))) 5

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ 5))

!"% (#s;!"#$!" 2)

!" %&&'&( )&*+,&-./

Thursday, February 24, 2011

Proving parametricity

22

Thursday, February 24, 2011

23

When are two expressions indistinguishable?

■ The property we really want is contextual equivalence: e1 and e2, when dropped into the same context, have the same observable behavior.

e1 e2

Thursday, February 24, 2011

23

When are two expressions indistinguishable?

■ The property we really want is contextual equivalence: e1 and e2, when dropped into the same context, have the same observable behavior.

e1 e2

(if (> ⃞ 0) 5 500)

(if ⃞ 5 500)

Thursday, February 24, 2011

24

A different notion of equivalence

■ Because contextual equivalence is hard to show directly, we need a different notion of equivalence.

■ We’ll define our own equivalence relation and show that it is sound with respect to contextual equivalence.

e1 e2

Thursday, February 24, 2011

Reflexivity: the Fundamental Property

■ In order to be an equivalence relation, our relation has to be reflexive: every expression must be related to itself.

■ But this corresponds nicely to what we mean by parametricity anyway!

25

e e

open expressions, two different

closing type environments

Thursday, February 24, 2011

26

What’s “logical” about it?

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

■ The relation we’re defining is called a logical relation. Why?

Thursday, February 24, 2011

26

What’s “logical” about it?

!"#

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

■ The relation we’re defining is called a logical relation. Why?

Thursday, February 24, 2011

26

What’s “logical” about it?

!"#

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

■ The relation we’re defining is called a logical relation. Why?

Thursday, February 24, 2011

26

What’s “logical” about it?

!"#

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

■ The relation we’re defining is called a logical relation. Why?

Thursday, February 24, 2011

26

What’s “logical” about it?

!"#

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

■ The relation we’re defining is called a logical relation. Why?

Thursday, February 24, 2011

26

What’s “logical” about it?

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

■ The relation we’re defining is called a logical relation. Why?

Thursday, February 24, 2011

26

What’s “logical” about it?

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

■ The relation we’re defining is called a logical relation. Why?

Thursday, February 24, 2011

26

What’s “logical” about it?

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

■ The relation we’re defining is called a logical relation. Why?

■ A logical relation “respects the actions of the logical operators...that correspond to the language’s type constructors” (Crary, 2005)

Thursday, February 24, 2011

27

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

! they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

! they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

???

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

! they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

???

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

! they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

(!!. "! : !. ...!...) #1 (!!. "! : !. ...!...) #2

???

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

! they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

(!!. "! : !. ...!...) #1 (!!. "! : !. ...!...) #2

related at type α iff they’re in some relation R that relates values of type τ1 and τ2

???

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

! they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

(!!. "! : !. ...!...) #1 (!!. "! : !. ...!...) #2

related at type α iff they’re in some relation R that relates values of type τ1 and τ2

We parameterize the ML side of our relation with a type interpretation δ mapping type variables α to triples (τ1, τ2, R)

???

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

! they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

(!!. "! : !. ...!...) #1 (!!. "! : !. ...!...) #2

related at type α iff they’re in some relation R that relates values of type τ1 and τ2

We parameterize the ML side of our relation with a type interpretation δ mapping type variables α to triples (τ1, τ2, R)

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

! they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

!

!!. "

they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

!

!!. "

they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)???

Thursday, February 24, 2011

27

A type-indexed relation

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

!

!!. "

they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

Thursday, February 24, 2011

28

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

!

!!. "

they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

Thursday, February 24, 2011

28

Would something like this work for Scheme?

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

!

!!. "

they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

Thursday, February 24, 2011

28

Would something like this work for Scheme?

!"#

!1 ! !2

!1 ! !2

Two values of type...

...are related if...

they’re equal

their first components are related at type τ1

and their second components are related at type τ2

given values related at type τ1

they produce expressions related at type τ2

!

!!. "

they belong to the relation R in δ(α)

given types τ1 and τ2 and a relation Rthey produce expressions related at type τ

under a δ extended with α → (τ1, τ2, R)

■ Since Scheme only has one (static) type, a relation defined inductively on the structure of types would be ill-founded

Thursday, February 24, 2011

28

Would something like this work for Scheme?

■ Since Scheme only has one (static) type, a relation defined inductively on the structure of types would be ill-founded

Thursday, February 24, 2011

Solving the ill-foundedness problem

29

Thursday, February 24, 2011

Solving the ill-foundedness problem

29

■ For Scheme values, index the relation by number of steps available for future computation

Thursday, February 24, 2011

Solving the ill-foundedness problem

29

Values of the syntactic form...

...are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j steps

???

■ For Scheme values, index the relation by number of steps available for future computation

Thursday, February 24, 2011

Solving the ill-foundedness problem

29

n

Values of the syntactic form...

...are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j steps

???

■ For Scheme values, index the relation by number of steps available for future computation

Thursday, February 24, 2011

Solving the ill-foundedness problem

29

n

Values of the syntactic form...

...are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j steps

???

■ For Scheme values, index the relation by number of steps available for future computation

Thursday, February 24, 2011

Solving the ill-foundedness problem

29

n

(!"#$ %1 %2)

Values of the syntactic form...

...are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j steps

???

■ For Scheme values, index the relation by number of steps available for future computation

Thursday, February 24, 2011

Solving the ill-foundedness problem

29

n

(!"#$ %1 %2)

Values of the syntactic form...

...are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j steps

???

■ For Scheme values, index the relation by number of steps available for future computation

Thursday, February 24, 2011

Solving the ill-foundedness problem

29

n

(!"#$ %1 %2)

(!!. ")

Values of the syntactic form...

...are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j steps

???

■ For Scheme values, index the relation by number of steps available for future computation

Thursday, February 24, 2011

Solving the ill-foundedness problem

29

n

(!"#$ %1 %2)

(!!. ")

Values of the syntactic form...

...are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j steps

???

■ For Scheme values, index the relation by number of steps available for future computation

Thursday, February 24, 2011

5 6

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

Thursday, February 24, 2011

5 6

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

Thursday, February 24, 2011

5 6

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

Thursday, February 24, 2011

5 6

(!!. 5) (!!. 6)

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

Thursday, February 24, 2011

5 6

(!!. 5) (!!. 6)

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

Substitution uses up 1 step

(!!. 5) 1 !"# 5[! := 1] !"# 5(!!. 6) 1 !"# 6[! := 1] !"# 6

Thursday, February 24, 2011

5 6

(!!. 5) (!!. 6)

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

Substitution uses up 1 step

(!!. 5) 1 !"# 5[! := 1] !"# 5(!!. 6) 1 !"# 6[! := 1] !"# 6

Thursday, February 24, 2011

5 6

(!!. 5) (!!. 6)

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

Thursday, February 24, 2011

5 6

(!!. 5) (!!. 6)

(!!. (!". 5)) (!!. (!". 6))

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

Thursday, February 24, 2011

5 6

(!!. 5) (!!. 6)

(!!. (!". 5)) (!!. (!". 6))

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

Thursday, February 24, 2011

5 6

(!!. 5) (!!. 6)

(!!. (!". 5)) (!!. (!". 6))

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

(!!. (!". 5)) 1 !"# (!". 5)[! := 1] !"# (!". 5)(!!. (!". 6)) 1 !"# (!". 6)[! := 1] !"# (!". 6)

Thursday, February 24, 2011

5 6

(!!. 5) (!!. 6)

(!!. (!". 5)) (!!. (!". 6))

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

(!!. (!". 5)) 1 !"# (!". 5)[! := 1] !"# (!". 5)(!!. (!". 6)) 1 !"# (!". 6)[! := 1] !"# (!". 6)

Thursday, February 24, 2011

5 6

(!!. 5) (!!. 6)

(!!. (!". 5)) (!!. (!". 6))

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

Thursday, February 24, 2011

5 6

(!!. 5) (!!. 6)

(!!. (!". 5)) (!!. (!". 6))

v1 v2Related

(indistinguishable) for...

0 steps

1 step

2 steps

Examples of related Scheme values

30

■ Intuitively, wrapping layers of λ around values makes them indistinguishable for 1 more step

Thursday, February 24, 2011

A step-indexed relation

31

n

(!"#$ %1 %2)

(!!. ")

Two values of the syntactic

form......are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j stepsgiven values related for i < j steps

they produce expressions related for i steps ???

Thursday, February 24, 2011

A step-indexed relation

31

n

(!"#$ %1 %2)

(!!. ")

Two values of the syntactic

form......are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j stepsgiven values related for i < j steps

they produce expressions related for i steps

Thursday, February 24, 2011

The little step-indexer

32

Thursday, February 24, 2011

The little step-indexer■ Chapter 9 of The Little Schemer gives

examples of functions length0, length≤1, length≤2, and so on

32

Thursday, February 24, 2011

The little step-indexer■ Chapter 9 of The Little Schemer gives

examples of functions length0, length≤1, length≤2, and so on

■ length≤j takes a list and returns the length of that list, as long as that length is ≤j; otherwise, length≤j goes into an infinite loop

32

Thursday, February 24, 2011

The little step-indexer■ Chapter 9 of The Little Schemer gives

examples of functions length0, length≤1, length≤2, and so on

■ length≤j takes a list and returns the length of that list, as long as that length is ≤j; otherwise, length≤j goes into an infinite loop

32

■ Think of the subscript ≤j as a behavioral contract guaranteeing that length≤j belongs to a certain type for up to j steps of execution

Thursday, February 24, 2011

The little step-indexer■ Chapter 9 of The Little Schemer gives

examples of functions length0, length≤1, length≤2, and so on

■ length≤j takes a list and returns the length of that list, as long as that length is ≤j; otherwise, length≤j goes into an infinite loop

32

■ Think of the subscript ≤j as a behavioral contract guaranteeing that length≤j belongs to a certain type for up to j steps of execution

■ This is exactly the intuition behind the step-indexed model of recursive types (Appel & McAllester, 2001)

Thursday, February 24, 2011

A step-indexed relation

33

n

(!"#$ %1 %2)

(!!. ")

Two values of the syntactic

form......are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j stepsgiven values related for i < j steps

they produce expressions related for i steps the inner ML expressions are related for j-1 steps

Thursday, February 24, 2011

A step-indexed relation

33

n

(!"#$ %1 %2)

(!!. ")

(!"!s;!"!)

Two values of the syntactic

form......are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j stepsgiven values related for i < j steps

they produce expressions related for i steps the inner ML expressions are related for j-1 steps

Thursday, February 24, 2011

A step-indexed relation

33

n

(!"#$ %1 %2)

(!!. ")

(!"!s;!"!)

Two values of the syntactic

form......are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j stepsgiven values related for i < j steps

they produce expressions related for i steps the inner ML expressions are related for j-1 steps???

Thursday, February 24, 2011

A step-indexed relation

33

n

(!"#$ %1 %2)

(!!. ")

(!"!s;!"!)

Two values of the syntactic

form......are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j stepsgiven values related for i < j steps

they produce expressions related for i steps the inner ML expressions are related for j-1 steps

Thursday, February 24, 2011

A step-indexed relation

33

n

(!"#$ %1 %2)

(!!. ")

(!"!s;!"!)

Two values of the syntactic

form......are related for j steps if...

they’re equal

their first components are related for j stepsand

their second components are related for j stepsgiven values related for i < j steps

they produce expressions related for i steps the inner ML expressions are related for j-1 steps

step indices “leak” back into the ML relation

Thursday, February 24, 2011

34

(!"!s;!"!) the inner ML expressions

are related for j-1 steps

Thursday, February 24, 2011

But wait!

34

(!"!s;!"!) the inner ML expressions

are related for j-1 steps

Thursday, February 24, 2011

But wait!

34

(!"!s;!"!) the inner ML expressions

are related for j-1 steps

(!"!s1;!1" !1) (!"!s2;!2" !2)

At what type are v1 and v2 related?

Thursday, February 24, 2011

But wait!

34

(!"!s;!"!) the inner ML expressions

are related for j-1 steps

(!"!s1;!1" !1) (!"!s2;!2" !2)

At what type are v1 and v2 related?

■ The type of these sealed values was originally a type variable...

Thursday, February 24, 2011

But wait!

34

(!"!s;!"!) the inner ML expressions

are related for j-1 steps

(!"!s1;!1" !1) (!"!s2;!2" !2)

At what type are v1 and v2 related?

■ The type of these sealed values was originally a type variable...

■ We need a dynamic counterpart to δ

Thursday, February 24, 2011

Possible worlds

■ An idea from modal logic (Kripke, 1963)■ Useful for reasoning about properties that only

hold under certain conditions

35

Thursday, February 24, 2011

What’s in a world?

36

“Meanwhile, in the world where e1 and e2 are related...”

Thursday, February 24, 2011

What’s in a world?

36

seals s1 generated during evaluation of e1

“Meanwhile, in the world where e1 and e2 are related...”

Thursday, February 24, 2011

What’s in a world?

36

seals s1 generated during evaluation of e1

seals s2 generated during evaluation of e2

“Meanwhile, in the world where e1 and e2 are related...”

Thursday, February 24, 2011

What’s in a world?

36

seals s1 generated during evaluation of e1

seals s2 generated during evaluation of e2

mappingsα → (s1, s2)

“Meanwhile, in the world where e1 and e2 are related...”

Thursday, February 24, 2011

What’s in a world?

36

seals s1 generated during evaluation of e1

seals s2 generated during evaluation of e2

mappingsα → (τ1, τ2, R)

mappingsα → (s1, s2)

“Meanwhile, in the world where e1 and e2 are related...”

Thursday, February 24, 2011

What’s in a world?

36

seals s1 generated during evaluation of e1

seals s2 generated during evaluation of e2

mappingsα → (τ1, τ2, R)

mappingsα → (s1, s2)

■ Worlds capture the relationship between static type variables and dynamic seals

“Meanwhile, in the world where e1 and e2 are related...”

Thursday, February 24, 2011

Relatedness in a world

37

(!"!s1;!1" !1) (!"!s2;!2" !2)

At what type are v1 and v2 related?

Thursday, February 24, 2011

Relatedness in a world

■ The answer: v1 and v2 must belong to a relation R that relates values of type τ1 and τ2

37

(!"!s1;!1" !1) (!"!s2;!2" !2)

At what type are v1 and v2 related?

Thursday, February 24, 2011

Relatedness in a world

■ The answer: v1 and v2 must belong to a relation R that relates values of type τ1 and τ2

■ We can find R in the current world

37

(!"!s1;!1" !1) (!"!s2;!2" !2)

At what type are v1 and v2 related?

Thursday, February 24, 2011

A possible-worlds model

38

Thursday, February 24, 2011

A possible-worlds model

■ Expressions are now related at a type, for a given number of steps, and in a world

38

Thursday, February 24, 2011

A possible-worlds model

■ Expressions are now related at a type, for a given number of steps, and in a world

■ Whenever we do type application, we extend the current world with new seals s1 and s2 and new bindings for α

38

Thursday, February 24, 2011

A possible-worlds model

■ Expressions are now related at a type, for a given number of steps, and in a world

■ Whenever we do type application, we extend the current world with new seals s1 and s2 and new bindings for α

■ Whenever we need to determine relatedness of sealed values, we consult the current world to find the R that would relate them

38

Thursday, February 24, 2011

A possible-worlds model

■ Expressions are now related at a type, for a given number of steps, and in a world

■ Whenever we do type application, we extend the current world with new seals s1 and s2 and new bindings for α

■ Whenever we need to determine relatedness of sealed values, we consult the current world to find the R that would relate them

■ Upshot of all this: now we can prove parametricity!

38

Thursday, February 24, 2011

Sage advice

39

Thursday, February 24, 2011

Sage advice

39

Thursday, February 24, 2011

The Fundamental Property / Parametricity

40

Thursday, February 24, 2011

The Fundamental Property / Parametricity

40

■ The bridge lemma:

Thursday, February 24, 2011

The Fundamental Property / Parametricity

40

!" #$%&'(( )1 '*+ )2,

-. (j, w, )1, )2) ! VS

/0)* (j, w, (!1(")!" )1), (!2(")!" )2)) ! VM !!"""

1" #$%&'(( !1 '*+ !2,

-. (j, w, !1, !2) ! VM !!""/0)* (j, w, ("!!1(")

!1), ("!!2(")!2)) ! VS "

■ The bridge lemma:

Thursday, February 24, 2011

The Fundamental Property / Parametricity

40

!" #$%&'(( )1 '*+ )2,

-. (j, w, )1, )2) ! VS

/0)* (j, w, (!1(")!" )1), (!2(")!" )2)) ! VM !!"""

1" #$%&'(( !1 '*+ !2,

-. (j, w, !1, !2) ! VM !!""/0)* (j, w, ("!!1(")

!1), ("!!2(")!2)) ! VS "

■ The bridge lemma: carries relatednessbetween languages

Thursday, February 24, 2011

The Fundamental Property / Parametricity

40

!" #$%&'(( )1 '*+ )2,

-. (j, w, )1, )2) ! VS

/0)* (j, w, (!1(")!" )1), (!2(")!" )2)) ! VM !!"""

1" #$%&'(( !1 '*+ !2,

-. (j, w, !1, !2) ! VM !!""/0)* (j, w, ("!!1(")

!1), ("!!2(")!2)) ! VS "

■ The bridge lemma: carries relatednessbetween languages

Thursday, February 24, 2011

The Fundamental Property / Parametricity

40

!" #$%&'(( )1 '*+ )2,

-. (j, w, )1, )2) ! VS

/0)* (j, w, (!1(")!" )1), (!2(")!" )2)) ! VM !!"""

1" #$%&'(( !1 '*+ !2,

-. (j, w, !1, !2) ! VM !!""/0)* (j, w, ("!!1(")

!1), ("!!2(")!2)) ! VS "

■ The bridge lemma: carries relatednessbetween languages

■ From there we can show the Fundamental Property:

Thursday, February 24, 2011

The Fundamental Property / Parametricity

40

!" #$%&'(( )1 '*+ )2,

-. (j, w, )1, )2) ! VS

/0)* (j, w, (!1(")!" )1), (!2(")!" )2)) ! VM !!"""

1" #$%&'(( !1 '*+ !2,

-. (j, w, !1, !2) ! VM !!""/0)* (j, w, ("!!1(")

!1), ("!!2(")!2)) ! VS "

■ The bridge lemma: carries relatednessbetween languages

!" #$ !;" !M ! : ! % &'() !;" !M ! !M ! : ! "

*" #$ !;" !S ( : "#"% &'() !;" !S ( !S ( : "#""

■ From there we can show the Fundamental Property:

Thursday, February 24, 2011

The Fundamental Property / Parametricity

40

!" #$%&'(( )1 '*+ )2,

-. (j, w, )1, )2) ! VS

/0)* (j, w, (!1(")!" )1), (!2(")!" )2)) ! VM !!"""

1" #$%&'(( !1 '*+ !2,

-. (j, w, !1, !2) ! VM !!""/0)* (j, w, ("!!1(")

!1), ("!!2(")!2)) ! VS "

■ The bridge lemma: carries relatednessbetween languages

!" #$ !;" !M ! : ! % &'() !;" !M ! !M ! : ! "

*" #$ !;" !S ( : "#"% &'() !;" !S ( !S ( : "#""

■ From there we can show the Fundamental Property:

Thursday, February 24, 2011

Parametric contracted Scheme terms

41

Thursday, February 24, 2011

Parametric contracted Scheme terms

■ One way to enforce a contract τ on a Scheme expression is by exporting it into ML at the type τ and then importing it back into Scheme...

41

Thursday, February 24, 2011

Parametric contracted Scheme terms

■ One way to enforce a contract τ on a Scheme expression is by exporting it into ML at the type τ and then importing it back into Scheme...

41

!! = (!"! (!

"! !))

Thursday, February 24, 2011

Parametric contracted Scheme terms

■ One way to enforce a contract τ on a Scheme expression is by exporting it into ML at the type τ and then importing it back into Scheme...

41

!! = (!"! (!

"! !))

■ ...so we can leverage our parametricity result to immediately show that contracted Scheme terms behave parametrically too

Thursday, February 24, 2011

Conclusion

42

Thursday, February 24, 2011

The three points I want you to remember

43

Thursday, February 24, 2011

The three points I want you to remember

■ Aside from giving us free theorems, parametricity makes existential-style data abstraction possible.

43

Thursday, February 24, 2011

The three points I want you to remember

■ Aside from giving us free theorems, parametricity makes existential-style data abstraction possible.

■ Parametricity breaks when we incorporate dynamically typed code into otherwise statically typed programs, but we can restore it using dynamic seal generation.

43

Thursday, February 24, 2011

The three points I want you to remember

■ Aside from giving us free theorems, parametricity makes existential-style data abstraction possible.

■ Parametricity breaks when we incorporate dynamically typed code into otherwise statically typed programs, but we can restore it using dynamic seal generation.

■ Seal generation is a stateful notion akin to dynamic memory allocation, so we can use possible worlds to reason about the semantics of seals in order to prove parametricity.

43

Thursday, February 24, 2011

44Photo by mroach on Flickr. Thanks!

Thanks!Email: [email protected]

Web: www.cs.indiana.edu/~lkuperResearch group: lambda.soic.indiana.edu

Thursday, February 24, 2011

Detailed non-parametricity example

45

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"# 5

!" (!"#"!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !))) 5

!" (!$ : !"#. (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!!"# $))) 5

!" (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!!"# 5))

!" (!"#!" (!!. ("#$ (%&'? !) (+ ! 1) !)) 5)

!" (!"#!" ("#$ (%&'? 5) (+ 5 1) 5))

!" (!"#!" ("#$ 0 (+ 5 1) 5))

!" (!"#!" (+ 5 1))

!" (!"#!" 6)

!" 6

Thursday, February 24, 2011

Detailed dynamic sealing example

46

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) !))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) !)))) !"# 5

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !))) 5

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ $))) 5

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ 5))

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) !)) ("!#s;!"#$ 5))

!" (#s;!"#$!" ("#$ (%&'? ("!#s;!"#$ 5)) (+ ("!#s;!"#$ 5) 1) ("!#s;!"#$ 5)))

!" (#s;!"#$!" ("#$ 1 (+ ("!#s;!"#$ 5) 1) ("!#s;!"#$ 5)))

!" (#s;!"#$!" ("!#s;!"#$ 5))

!" 5

Thursday, February 24, 2011

Another detailed dynamic sealing example

47

(!!. !"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) !"# 5

!" (!". (!"!!" (!!. ("#$ (%&'? !) (+ ! 1) 2)))) !"# 5

!" (#s;!"#$"#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2))) 5

!" (!$ : !"#. (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ $))) 5

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ 5))

!" (#s;!"#$!" (!!. ("#$ (%&'? !) (+ ! 1) 2)) ("!#s;!"#$ 5))

!" (#s;!"#$!" ("#$ (%&'? ("!#s;!"#$ 5)) (+ ("!#s;!"#$ 5) 1) 2))

!" (#s;!"#$!" ("#$ 1 (+ ("!#s;!"#$ 5) 1) 2))

!" (#s;!"#$!" 2)

!" %&&'&( )&*+,&-./

Thursday, February 24, 2011