Optimality for Dynamic Patternsblsk/Docs/Balabonski-Talk-PPDP10.pdf · T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 1/20 Optimality for Dynamic Patterns

Post on 23-Jul-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 1/20

Optimalityfor Dynamic Patterns

Thibaut Balabonski

Laboratoire PPSCNRS and Universite Paris Diderot

PPDP 2010: July 28

Computing with data structures

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 2/20

type ’a tree =

| Data of ’a

| Node of ’a tree * ’a tree

let rec upd f t = match t with

| Data a -> Data (f a)

| Node l r -> Node (upd f l) (upd f r)

Pattern matching:

• tests the shape.

• extracts substructures.

New polymorphisms

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 3/20

• Pattern polymorphism: parameters in patterns.

• Path polymorphism: shape irrelevance.

let rec upd c f t = match t with

| c d -> c (f d)

| x y -> (upd c f x) (upd c f y)

| a -> a

Instantiation of the parameter c

• Constructors like Data.

• Functions building patterns.

Dynam

icpa

ttern

s

New polymorphisms

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 3/20

• Pattern polymorphism: parameters in patterns.

• Path polymorphism: shape irrelevance.

let rec upd c f t = match t with

| Data d -> Data (f d)

| x y -> (upd c f x) (upd c f y)

| a -> a

Instantiation of the parameter c

• Constructors like Data.

• Functions building patterns.

Dynam

icpa

ttern

s

New polymorphisms

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 3/20

• Pattern polymorphism: parameters in patterns.

• Path polymorphism: shape irrelevance.

let rec upd c f t = match t with

| (fun x -> Node (Data x) (Data )) d ->

| x y -> (upd c f x) (upd c f y)

| a -> a

Instantiation of the parameter c

• Constructors like Data.

• Functions building patterns.

Dynam

icpa

ttern

s

New polymorphisms

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 3/20

• Pattern polymorphism: parameters in patterns.

• Path polymorphism: shape irrelevance.

let rec upd c f t = match t with

| Node (Data d) (Data ) -> Node ...

| x y -> (upd c f x) (upd c f y)

| a -> a

Instantiation of the parameter c

• Constructors like Data.

• Functions building patterns.

Dynam

icpa

ttern

s

New polymorphisms

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 3/20

• Pattern polymorphism: parameters in patterns.

• Path polymorphism: shape irrelevance.

let rec upd c f t = match t with

| c d -> c (f d)

| x y -> (upd c f x) (upd c f y)

| a -> a

Instantiation of the parameter c

• Constructors like Data.

• Functions building patterns.

Dynam

icpa

ttern

s

Collateral damage

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 4/20

Usual compile-time analyzes

| Node l (Data d) ->

| Node (Data d) (Node ) ->

| Node (Node l r) (Node ) ->

This requires static patterns!

Goal

Run-time sharing mechanismsfor dynamic patterns.

Collateral damage

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 4/20

Usual compile-time analyzes

| Node l (Data d) ->

| Node (Data d) (Node ) ->

| Node (Node l r) (Node ) ->

This requires static patterns!

Goal

Run-time sharing mechanismsfor dynamic patterns.

Collateral damage

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 4/20

Usual compile-time analyzes

| Node l (Data d) ->

| Node (Data d) (Node ) ->

| Node (Node l r) (Node ) ->

This requires static patterns!

Goal

Run-time sharing mechanismsfor dynamic patterns.

Roadmap

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 5/20

1 Is optimal sharing a reasonable solution?

2 Families of terms: learning from history

3 (Some syntax)

4 Recording the history

5 From history to memory

Is optimal sharing a reasonable solution?

There is some mismatch betweentheoretical optimality and practical efficiency.

Families of terms: learning from history

(Some syntax)

Recording the history

From history to memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 6/20

The trap of optimality

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 6/20

Sharing

Extra cost

Usual implementations

β-Optimality

The trap of optimality

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 6/20

Sharing

Extra cost

Usual implementations

β-Optimality

The trap of optimality

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 6/20

Sharing

Extra cost

Usual implementations

β-Optimality

Weak reduction

IOptimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 7/20

Usual implementations

• Passing arguments to functions.

• No partial evaluation.

⇒ No evaluation under a binder (λ).

Confluent weak reduction

No reduction betweena bounded variable and its binder.

Families of terms: learning from history

Terms with same history should not be considereddifferent.

Is optimal sharing a reasonable solution?

(Some syntax)

Recording the history

From history to memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 8/20

Optimality and history

IFamilies and history

Optimal optimality?

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 8/20

Optimality commandment

At each step you shall reduce a whole family.

• Family: all redexes sharing a common history.

• History: all events that contributed to thecreation of a redex.

• Redex: REDucible EXpression.

Screening the history

IFamilies and history

Optimal optimality?

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 9/20

Which reduction steps are relevant?

Back to basics

• analysis of direct contribution.

• how can redexes be created?

And later

• record contributions in labels.

(Some syntax)

Digression: the Weak Pure Pattern Calculus.Simple syntax, complex reduction.

Is optimal sharing a reasonable solution?

Families of terms: learning from history

Recording the history

From history to memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 10/20

Syntactic trees

I(Some syntax)

Optimal optimality?

Families and history

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 10/20

@

A

λx

Bx x

Syntactic trees

I(Some syntax)

Optimal optimality?

Families and history

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 10/20

@

A

λθ

Bx x

Pxc

Pattern matching: a complex operation

I(Some syntax)

Optimal optimality?

Families and history

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 11/20

@

λθ

Bxi xj

PP

xi xj Ai Aj

A

B

Ai Aj

Failure

If A and P incompatible, reduction to ⊥.

Partial definition

Matching undefined for improper terms.

Pattern matching: a meta-operation

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 12/20

a/θ x = x ← a x ∈ θ

x/θ x = x 6∈ θ

a1a2/θ p1p2 = a1/θ p1 ] a2/θ p2 a1a2, p1p2 matchable

a/θ p = ⊥ a, p matchable

Undefined otherwise!

Matchable forms

• x t1 ... tn

• [θ]p b

Recording the history

Labels to record contributionsand define redex families.

Is optimal sharing a reasonable solution?

Families of terms: learning from history

(Some syntax)

From history to memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 13/20

History and contributions materialized

IRecording the history

Optimal optimality?

Families and history

(Some syntax)

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 13/20

Contributions can be recorded by labels.

N

ε

• Family: all redexes sharing a common label.

Forward: The consequences of my deeds

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 14/20

@

λθ

xi xj

Ω

?

initial labels ¡, ¢, £...atomic labels α ::= ¡ | pΩq | |Ω, α|

labels Γ, ∆, Ω ::= α1α2 . . . αn

Forward: The consequences of my deeds

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 14/20

@

λθ

xi xj

Ω

λ

λ

?

initial labels ¡, ¢, £...atomic labels α ::= ¡ | pΩq | |Ω, α|

labels Γ, ∆, Ω ::= α1α2 . . . αn

Forward: The consequences of my deeds

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 14/20

@

λθ

xi xj

Ω

W

λ

λ

?

initial labels ¡, ¢, £...atomic labels α ::= ¡ | pΩq | |Ω, α|

labels Γ, ∆, Ω ::= α1α2 . . . αn

Forward: The consequences of my deeds

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 14/20

@

λθ

xi xj

Ω

W

λ

λ

?

initial labels ¡, ¢, £...atomic labels α ::= ¡ | pΩq | |Ω, α|

labels Γ, ∆, Ω ::= α1α2 . . . αn

Forward: The consequences of my deeds

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 14/20

@

λθ

xi xj

Ω

?

|Ω, α|

pΩq

initial labels ¡, ¢, £...atomic labels α ::= ¡ | pΩq | |Ω, α|

labels Γ, ∆, Ω ::= α1α2 . . . αn

Backward: Where do I come from?

IRecording the history

Optimal optimality?

Families and history

(Some syntax)

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 15/20

@

λθ

α

Γ

∆pat

∆arg

Name of the redex

Ω = α Γ ∆pat ∆arg

Pattern matching and history extraction

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 16/20

Y /θ α : Zm = α : Y /θ Zm m ∈ l ,wY /θ xl = (ε, x ← Y ) x ∈ θY /θ xw = (|Y |, x ← Y ) x ∈ θ

Y /θ ([τ ]p b)l = (ε,⊥)Y /θ ([τ ]p b)w = (|Y |,⊥)

α : Z/θ Xm = α : Z/θ Xm matchabilityx/θ xm = (ε, ) x 6∈ θ

A1A2/θ P1P2m = A1/θ P1w A2/θ P2l matchabilityY /θ Xm = (|Y ||X |,⊥) matchabilityY /θ Xm = undefined otherwise

|[θ]P B| = ε|x | = ε

|T1T2| = |T1||α : Z | = α|Z |

(∆2, σ2) (∆2,⊥) undef.(∆1, σ1) (∆1∆2, σ1 ] σ2) (∆1∆2,⊥) undef.(∆1,⊥) (∆1,⊥) (∆1,⊥) (∆1,⊥)undef. undef. undef. undef.

From history to memory

From labelled terms to directed acyclic graphs.

Is optimal sharing a reasonable solution?

Families of terms: learning from history

(Some syntax)

Recording the history

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 17/20

Labels as memory locations

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 17/20

y ι

N

δ

?@α

y ι

N ′

N ′δ′

δ′

@

@y

N

@

@y

N ′

Sharing property: equal labels ⇒ equal terms.

Labels as memory locations

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 17/20

y ι

N

δ

?@α

y ι

N ′

N ′δ′

δ′

@

@y

N

@

@y

N ′

Graph reduction

Sharing property: equal labels ⇒ equal terms.

Labels as memory locations

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 17/20

y ι

N

δ ?@α

y ι

N ′

N ′δ′

δ′

@

@y

N

@

@y

N ′

Graph reduction

Labelled development?

Sharing property: equal labels ⇒ equal terms.

From labels to graph reduction

IHistory and memory

Optimal optimality?

Families and history

(Some syntax)

Recording the history

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 18/20

Complete labelled reduction:development of all redexes with a given label.

Sharing theorem

Complete labelled reductionpreserves

the sharing property.

Hypothesis: start with initial labels.Key: contribution is characterized by labels.

Conclusion

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 19/20

Present resultsA causality analysis can yield

• a concrete DAG implementation.

• a proof of optimality of call-by-need.

Through PPC, this work reaches as well λ-calculusand pattern matching a la ML.

Future work

• Sharing in functions with multiple cases.

• Effective reduction strategy.

A broader picture

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 20/20

Pure PatternCalculus

Sharing inWeak PPCPPDP’10

ExplicitMatchingHOR’10

Call-by-needAlternatives

EfficientEvaluation

...and questions...

A broader picture

I

Optimal optimality?

Families and history

(Some syntax)

Recording the history

History and memory

T. Balabonski — Optimality for Dynamic Patterns PPDP 2010: July 28 — 20/20

Pure PatternCalculus

Sharing inWeak PPCPPDP’10

ExplicitMatchingHOR’10

Call-by-needAlternatives

EfficientEvaluation

...and questions...

top related