implicit self-adjusting computation for purely functional ... · implicit self-adjusting...

49
Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dunfield Matthew A. Hammer Umut A. Acar MPI-SWS September 19, 2011

Upload: others

Post on 27-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Implicit Self-Adjusting Computationfor Purely Functional Programs

Yan ChenJoshua Dunfield Matthew A. Hammer Umut A. Acar

MPI-SWS

September 19, 2011

Page 2: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Incremental/Dynamic Problems

Input: 3, 5, 8, 2, 10, 4, 9, 1

Output: Max = 10

I Linear scan: O(n)

I Priority queue: O(log n)

Yan Chen Implicit Self-Adjusting Computation 2

Page 3: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Incremental/Dynamic Problems

Input: 3, 5, 8, 2, 10, 4, 9, 1

Output: Max = 10 9

I Linear scan: O(n)

I Priority queue: O(log n)

Yan Chen Implicit Self-Adjusting Computation 2

Page 4: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Motivation

Incremental changes are ubiquitous and hard.

Problem Static Incremental/Dynamic

Max [folklore 1950s]O(n)

[Williams 1964]O(log n)

GraphConnectivity

[Strassen 1969]O(n2.8)

[Thorup 2000]O (log n(log log n)3)for edge updates

PlanarConvex Hull

[Graham 1972]O(n log n)

[Brodal et al. 2002]O(log n)

...

Compilation Whole-program Separate

Yan Chen Implicit Self-Adjusting Computation 3

Page 5: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Challenge

How can we incrementalize a static algorithm?

StaticAlgorithms

IncrementalAlgorithms10+ years of research

fun sumOfSquares (x, y) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

10

x2 + y 2

1x2 9 y 2

1x 3 y

2x

4x2

13

x2 + y 2

Dependency Graph

Yan Chen Implicit Self-Adjusting Computation 4

Page 6: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Challenge

How can we incrementalize a static algorithm?

StaticAlgorithms

IncrementalAlgorithms10+ years of research

fun sumOfSquares (x, y) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

10

x2 + y 2

1x2 9 y 2

1x 3 y

2x

4x2

13

x2 + y 2

Dependency Graph

Yan Chen Implicit Self-Adjusting Computation 4

Page 7: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Challenge

How can we incrementalize a static algorithm?

StaticAlgorithms

IncrementalAlgorithms10+ years of research

fun sumOfSquares (x, y) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

10

x2 + y 2

1x2 9 y 2

1x 3 y2x

4x2

13

x2 + y 2

Dependency Graph

Yan Chen Implicit Self-Adjusting Computation 4

Page 8: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Challenge

How can we incrementalize a static algorithm?

StaticAlgorithms

IncrementalAlgorithms10+ years of research

fun sumOfSquares (x, y) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

10

x2 + y 2

1x2 9 y 2

1x 3 y2x

4x2

13

x2 + y 2

Dependency Graph

Yan Chen Implicit Self-Adjusting Computation 4

Page 9: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Challenge

How can we incrementalize a static algorithm?

StaticAlgorithms

IncrementalAlgorithms10+ years of research

fun sumOfSquares (x, y) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

10

x2 + y 2

1x2 9 y 2

1x 3 y2x

4x2

13

x2 + y 2

Dependency Graph

Yan Chen Implicit Self-Adjusting Computation 4

Page 10: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Explicit Self-Adjusting Computation

Rewrite program to construct dependency graph

10

x2 + y 2

1x2 9 y 2

1x 3 y2x

4x2

13

x2 + y 2

fun sumOfSquares (x:int

mod

, y:int) =

let

val x2 =

mod (read x as x’ in

write (

x

* x

’))

val y2 = y * y

in

mod (read x2 as x2’ in

write (

x2

+ y2

))

end

Yan Chen Implicit Self-Adjusting Computation 5

Page 11: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Explicit Self-Adjusting Computation

Rewrite program to construct dependency graph

10

x2 + y 2

1x2 9 y 2

1x 3 y2x

4x2

13

x2 + y 2

fun sumOfSquares (x:int mod, y:int) =

let

val x2 = mod (read x as x’ in

write (x’ * x’))

val y2 = y * y

in

mod (read x2 as x2’ in

write (x2’ + y2))

end

Yan Chen Implicit Self-Adjusting Computation 5

Page 12: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Challenges of Explicit Self-Adjusting Computation

I The explicit library is not a natural way of programming.

I Efficiency is highly sensitive to program details.

I Different requirements lead to different functions.

I Function rewriting can spread to large amounts of code.

fun sumOfSquares (x:int mod, y:int) =

let

val x2 = mod (read x as x’ in

write (x’ * x’))

val y2 = y * y

in

mod (read x2 as x2’ in

write (x2’ + y2))

end

Yan Chen Implicit Self-Adjusting Computation 6

Page 13: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Challenges of Explicit Self-Adjusting Computation

I The explicit library is not a natural way of programming.

I Efficiency is highly sensitive to program details.

I Different requirements lead to different functions.

I Function rewriting can spread to large amounts of code.

fun sumOfSquares (x:int mod, y:int) =

let

val x2 = mod (read x as x’ in

write (x’ * x’))

val y2 = y * y

in

mod (read x2 as x2’ in

write (x2’ + y2))

end

Yan Chen Implicit Self-Adjusting Computation 6

Page 14: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Challenges of Explicit Self-Adjusting Computation

I The explicit library is not a natural way of programming.

I Efficiency is highly sensitive to program details.

I Different requirements lead to different functions.

I Function rewriting can spread to large amounts of code.

fun sumOfSquares (x:int mod, y:int) =

let

val x2 = mod (read x as x’ in

write (x’ * x’ + y * y))

in

x2

end

Yan Chen Implicit Self-Adjusting Computation 6

Page 15: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Challenges of Explicit Self-Adjusting Computation

I The explicit library is not a natural way of programming.

I Efficiency is highly sensitive to program details.

I Different requirements lead to different functions.

I Function rewriting can spread to large amounts of code.

fun sumOfSquares (x:int, y:int mod) =

let

val x2 = x * x

val res = mod (read y as y’ in

write (x2 + y’ * y’))

in

res

end

Yan Chen Implicit Self-Adjusting Computation 6

Page 16: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Challenges of Explicit Self-Adjusting Computation

I The explicit library is not a natural way of programming.

I Efficiency is highly sensitive to program details.

I Different requirements lead to different functions.

I Function rewriting can spread to large amounts of code.

fun sumOfSquares (x:int, y:int mod) =

let

val x2 = x * x

val res = mod (read y as y’ in

write (x2 + y’ * y’))

in

res

end

Yan Chen Implicit Self-Adjusting Computation 6

Page 17: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Bridge the Gap

ML Code

fun sumOfSquares ( x , y ) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

↪→?

x Changeable

y Stable

Explicit Self-Adjusting Code

fun sumOfSquares (x:int mod, y:int) =

let

val x2 = mod (read x as x’ in

write (x’ * x’))

val y2 = y * y

in

mod (read x2 as x2’ in

write (x2’ + y2))

end

Yan Chen Implicit Self-Adjusting Computation 7

Page 18: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Our Approach

New! Implicit Self-Adjusting Computation

ML codewith levelannotations

TypeInference

Translation

Self-AdjustingProgram

I Annotate input types — no code modification required.

I Automatically infer dependencies from type annotations.

I Polymorphism enables different versions of code.

I Type-directed translation produces an efficientself-adjusting program.

Yan Chen Implicit Self-Adjusting Computation 8

Page 19: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Our Approach

New! Implicit Self-Adjusting Computation

ML codewith levelannotations

TypeInference

Translation

Self-AdjustingProgram

I Annotate input types — no code modification required.

I Automatically infer dependencies from type annotations.

I Polymorphism enables different versions of code.

I Type-directed translation produces an efficientself-adjusting program.

Yan Chen Implicit Self-Adjusting Computation 8

Page 20: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Source Language

I Pure λ-calculus with level annotations.

I Use level C to mark changeable data.

Levels δ : := S | C | αTypes τ : := intδ | (τ1 × τ2)δ | (τ1 + τ2)δ | (τ1 → τ2)

δ

val sumOfSquares: intα1 * intα2 -> intα3

Represents:

val sumOfSquaresSS: intS * intS -> intS

val sumOfSquaresSC: intS * intC -> intC

val sumOfSquaresCS: intC * intS -> intC

val sumOfSquaresCC: intC * intC -> intC

Yan Chen Implicit Self-Adjusting Computation 9

Page 21: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Source Language

I Pure λ-calculus with level annotations.

I Use level C to mark changeable data.

Levels δ : := S | C | αTypes τ : := intδ | (τ1 × τ2)δ | (τ1 + τ2)δ | (τ1 → τ2)

δ

val sumOfSquares: intα1 * intα2 -> intα3

Represents:

val sumOfSquaresSS: intS * intS -> intS

val sumOfSquaresSC: intS * intC -> intC

val sumOfSquaresCS: intC * intS -> intC

val sumOfSquaresCC: intC * intC -> intC

Yan Chen Implicit Self-Adjusting Computation 9

Page 22: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Overview

ML codewith levelannotations

TypeInference

Translation

Self-AdjustingProgram

Yan Chen Implicit Self-Adjusting Computation 10

Page 23: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type System

I Identify affected computation

I Any data that depends on changeable data must bechangeable.

I Identify reusable computation:

I Non-interference property

fun sumOfSquares ( x :intC, y :intS) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

Information flow!

Yan Chen Implicit Self-Adjusting Computation 11

Page 24: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type System

I Identify affected computationI Any data that depends on changeable data must be

changeable.

I Identify reusable computation:

I Non-interference property

fun sumOfSquares ( x :intC, y :intS) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

Information flow!

Yan Chen Implicit Self-Adjusting Computation 11

Page 25: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type System

I Identify affected computationI Any data that depends on changeable data must be

changeable.I Identify reusable computation:

I Non-interference property

fun sumOfSquares ( x :intC, y :intS) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

Information flow!

Yan Chen Implicit Self-Adjusting Computation 11

Page 26: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type System

I Identify affected computationI Any data that depends on changeable data must be

changeable.I Identify reusable computation:

I Non-interference property

fun sumOfSquares ( x :intC, y :intS) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

Information flow!

Yan Chen Implicit Self-Adjusting Computation 11

Page 27: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type System

I Identify affected computationI Any data that depends on changeable data must be

changeable.I Identify reusable computation:

I Non-interference property

fun sumOfSquares ( x :intC, y :intS) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

Information flow!

Yan Chen Implicit Self-Adjusting Computation 11

Page 28: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type Inference for Concrete Levels

Infer types for all subterms

fun sumOfSquares ( x :intC, y :intS) : int

C

=

let

val x2 :int

C

= x * x

val y2 :int

S

= y * y

val res :int

C

= x2 + y2

in

res

end

Yan Chen Implicit Self-Adjusting Computation 12

Page 29: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type Inference for Concrete Levels

Infer types for all subterms

fun sumOfSquares ( x :intC, y :intS) : int

C

=

let

val x2 :intC = x * x

val y2 :int

S

= y * y

val res :int

C

= x2 + y2

in

res

end

Yan Chen Implicit Self-Adjusting Computation 12

Page 30: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type Inference for Concrete Levels

Infer types for all subterms

fun sumOfSquares ( x :intC, y :intS) : int

C

=

let

val x2 :intC = x * x

val y2 :intS = y * y

val res :int

C

= x2 + y2

in

res

end

Yan Chen Implicit Self-Adjusting Computation 12

Page 31: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type Inference for Concrete Levels

Infer types for all subterms

fun sumOfSquares ( x :intC, y :intS) : int

C

=

let

val x2 :intC = x * x

val y2 :intS = y * y

val res :intC = x2 + y2

in

res

end

Yan Chen Implicit Self-Adjusting Computation 12

Page 32: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type Inference for Concrete Levels

Infer types for all subterms

fun sumOfSquares ( x :intC, y :intS) : intC =

let

val x2 :intC = x * x

val y2 :intS = y * y

val res :intC = x2 + y2

in

res

end

Yan Chen Implicit Self-Adjusting Computation 12

Page 33: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Type Inference for Level Polymorphism

C ∧ D ; Γ `S v1 : τ′ C ; Γ, x : ∀~α[D]. τ ′′ `ε e2 : τ

Generate fresh level variables︷ ︸︸ ︷~α∩FV (C , Γ) = ∅

Subsumption︷ ︸︸ ︷C τ ′ <: τ ′′

C ∧ ∃~α.D ; Γ `ε let x = v1 in e2︸ ︷︷ ︸Value Restriction

: τ(SLetV)

val sumOfSquares: intα1 * intα2 -> intα3

[α3 ≥ α1 ∧ α3 ≥ α2]

I Our typing rules and constraints fall within the HM(X)framework [Odersky et al. 1999], permitting inference ofprincipal types via constraint solving.

Yan Chen Implicit Self-Adjusting Computation 13

Page 34: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Overview

ML codewith levelannotations

TypeInference

Translation

Self-AdjustingProgram

Yan Chen Implicit Self-Adjusting Computation 14

Page 35: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Target Language

I Modal type system

I eC has no return value, and can only end with write orchangeable function application.

Types τ : := τ mod | · · ·Expressions e : := eS | eC

Stable eS : := let x=eS in eS

expressions | mod eC create| · · ·

Changeable eC : := let x=eS in eC

expressions | read x as y in eC dereference| write(x) store| · · ·

Yan Chen Implicit Self-Adjusting Computation 15

Page 36: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Overview

ML codewith levelannotations

TypeInference

Translation

Self-AdjustingProgram

Yan Chen Implicit Self-Adjusting Computation 16

Page 37: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation

Γ ` e : τ ↪→δ

Source expression Target expression

v w⇓⇓

≡Correctness

fun sumOfSquares ( x , y ) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

↪→C

fun sumOfSquares ( x , y ) =

let

val x2 = mod (read x as x’ in

write (x’ * x’) )

val y2 = y * y

in

read x2 as x2’ in

write (x2’ + y2 )

end

Yan Chen Implicit Self-Adjusting Computation 17

Page 38: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation

Γ ` e : τ ↪→δ

Source expression Target expression

v w⇓⇓

≡Correctness

fun sumOfSquares ( x , y ) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

↪→C

fun sumOfSquares ( x , y ) =

let

val x2 = mod (read x as x’ in

write (x’ * x’) )

val y2 = y * y

in

read x2 as x2’ in

write (x2’ + y2 )

end

Yan Chen Implicit Self-Adjusting Computation 17

Page 39: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation

Γ ` e : τ ↪→δ

Source expression Target expression

v w⇓⇓

≡Correctness

fun sumOfSquares ( x , y ) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

↪→C

fun sumOfSquares ( x , y ) =

let

val x2 = mod (read x as x’ in

write (x’ * x’) )

val y2 = y * y

in

read x2 as x2’ in

write (x2’ + y2 )

end

Yan Chen Implicit Self-Adjusting Computation 17

Page 40: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation

Γ ` e : τ ↪→δ

Source expression Target expression

v w⇓⇓

≡Correctness

fun sumOfSquares ( x , y ) =

let

val x2 = x * x

val y2 = y * y

in

x2 + y2

end

↪→C

fun sumOfSquares ( x , y ) =

let

val x2 = mod (read x as x’ in

write (x’ * x’) )

val y2 = y * y

in

read x2 as x2’ in

write (x2’ + y2 )

end

Yan Chen Implicit Self-Adjusting Computation 17

Page 41: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation Example

Typing Environment Γ : x2:intC, y2:intS

, res:intC

Γ ` val res = x2 + y2 in res : intC

↪→S

val res =

mod (read x2 as x2’ in

write (x2’ +

y2

))

in

res

Yan Chen Implicit Self-Adjusting Computation 18

Page 42: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation Example

Typing Environment Γ : x2:intC, y2:intS

, res:intC

Γ ` val res = x2 + y2 in res : intC

↪→S

val res =

mod (read x2 as x2’ in

write (x2’ +

y2

))

in

res

Yan Chen Implicit Self-Adjusting Computation 18

Page 43: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation Example

Typing Environment Γ : x2:intC, y2:intS

, res:intC

Γ ` val res = x2 + y2 in res : intC

↪→S

val res =

mod (

read x2 as x2’ in

write (

x2’ + y2

))

in

res

Yan Chen Implicit Self-Adjusting Computation 18

Page 44: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation Example

Typing Environment Γ : x2:intC, y2:intS

, res:intC

Γ ` val res = x2 + y2 in res : intC

↪→S

val res =

mod (

read x2 as x2’ in

write (x2’ + y2)

)

in

res

Yan Chen Implicit Self-Adjusting Computation 18

Page 45: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation Example

Typing Environment Γ : x2:intC, y2:intS , res:intC

Γ ` val res = x2 + y2 in res : intC

↪→S

val res = mod (read x2 as x2’ in

write (x2’ + y2))

in

res

Yan Chen Implicit Self-Adjusting Computation 18

Page 46: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation Example

Typing Environment Γ : x2:intC, y2:intS , res:intC

Γ ` val res = x2 + y2 in res : intC

↪→S

val res = mod (read x2 as x2’ in

write (x2’ + y2))

in

res

Yan Chen Implicit Self-Adjusting Computation 18

Page 47: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Translation — Monomorphization

Γ, x : ∀~α[D]. τ ′ ` e : τ ↪→δ

e ′

Generate all satisfying instances︷ ︸︸ ︷For all ~δi s.t. ~α = ~δi D,

Γ ` v : [~δi/~α]τ′ ↪→

S e ′i

Γ ` let x = v in e : τ ↪→δ

let {x~δi= e ′

i }i in e ′ (LetV)

val sumOfSquares: intα1 * intα2 -> intα3

[α3 ≥ α1 ∧ α3 ≥ α2]

↪→δ

val sumOfSquaresSS: intS * intS -> intS

val sumOfSquaresSC: intS * intC -> intC

val sumOfSquaresCS: intC * intS -> intC

val sumOfSquaresCC: intC * intC -> intC

I Dead-code elimination can remove unused functions.I The functions that are used would have to be

handwritten in an explicit setting.

Yan Chen Implicit Self-Adjusting Computation 19

Page 48: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Theoretical Results

ML

Explicit SAC

e e : τ

eδ : τ ′

v

w

Type inference Evaluation

in k steps

Evaluation

in Θ(k) steps

Type-DirectedTranslation

TypeSoundness

ObservationalEquivalence

Yan Chen Implicit Self-Adjusting Computation 20

Page 49: Implicit Self-Adjusting Computation for Purely Functional ... · Implicit Self-Adjusting Computation for Purely Functional Programs Yan Chen Joshua Dun eld Matthew A. Hammer Umut

Summary

I Implicit Self-Adjusting ComputationI Automatic dependency tracking based on type

annotationI Type-directed translation for self-adjusting

computation

I Automatically make ML programs self-adjusting

I Formal proofs of translation soundness and asymptoticcomplexity

I Implementation and preliminary results presented atWorkshop on ML

See paper!

Yan Chen Implicit Self-Adjusting Computation 21