Type-Based Reasoning about Efficiency D. Seidel J. Voigtl ander - - PowerPoint PPT Presentation

type based reasoning about efficiency
SMART_READER_LITE
LIVE PREVIEW

Type-Based Reasoning about Efficiency D. Seidel J. Voigtl ander - - PowerPoint PPT Presentation

Type-Based Reasoning about Efficiency D. Seidel J. Voigtl ander University of Bonn August 3rd, 2011 Supported by the DFG under grant VO 1512/1-1. Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g ( a :


slide-1
SLIDE 1

Type-Based Reasoning about Efficiency

  • D. Seidel∗
  • J. Voigtl¨

ander

University of Bonn

August 3rd, 2011

∗Supported by the DFG under grant VO 1512/1-1.

slide-2
SLIDE 2

Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g (a : as) = (g a) : (map g as)

1

slide-3
SLIDE 3

Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g (a : as) = (g a) : (map g as) Some invocations: map succ [1, 2, 3] = [2, 3, 4]

1

slide-4
SLIDE 4

Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g (a : as) = (g a) : (map g as) Some invocations: map succ [1, 2, 3] = [2, 3, 4] map not [True, False] = [False, True]

1

slide-5
SLIDE 5

Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g (a : as) = (g a) : (map g as) Some invocations: map succ [1, 2, 3] = [2, 3, 4] map not [True, False] = [False, True] map even [1, 2, 3] = [False, True, False]

1

slide-6
SLIDE 6

Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g (a : as) = (g a) : (map g as) Some invocations: map succ [1, 2, 3] = [2, 3, 4] map not [True, False] = [False, True] map even [1, 2, 3] = [False, True, False] map not [1, 2, 3]

1

slide-7
SLIDE 7

Parametric Polymorphism in Haskell A standard function: map :: (α → β) → [α] → [β] map g [ ] = [ ] map g (a : as) = (g a) : (map g as) Some invocations: map succ [1, 2, 3] = [2, 3, 4] map not [True, False] = [False, True] map even [1, 2, 3] = [False, True, False] map not [1, 2, 3]

1

slide-8
SLIDE 8

Parametric Polymorphism in Haskell A standard function: map :: (α → β) → [α] → [β] map g [ ] = [ ] map g (a : as) = (g a) : (map g as) Some invocations: map succ [1, 2, 3] = [2, 3, 4] map not [True, False] = [False, True] map even [1, 2, 3] = [False, True, False] map not [1, 2, 3] rejected at compile-time

1

slide-9
SLIDE 9

Parametric Polymorphism in Haskell A standard function: map :: (α → β) → [α] → [β] Some invocations: map succ [1, 2, 3] = [2, 3, 4] map not [True, False] = [False, True] map even [1, 2, 3] = [False, True, False] map not [1, 2, 3] rejected at compile-time

1

slide-10
SLIDE 10

Another Example reverse :: [α] → [α] reverse [ ] = [ ] reverse (a : as) = (reverse as) + + [a]

2

slide-11
SLIDE 11

Another Example reverse :: [α] → [α] reverse [ ] = [ ] reverse (a : as) = (reverse as) + + [a] For every choice of g and l: reverse (map g l) = map g (reverse l) Provable by induction.

2

slide-12
SLIDE 12

Another Example reverse :: [α] → [α] reverse [ ] = [ ] reverse (a : as) = (reverse as) + + [a] For every choice of g and l: reverse (map g l) = map g (reverse l) Provable by induction. Or as a “free theorem” [Wadler, FPCA’89].

2

slide-13
SLIDE 13

Another Example reverse :: [α] → [α] For every choice of g and l: reverse (map g l) = map g (reverse l) Provable by induction. Or as a “free theorem” [Wadler, FPCA’89].

2

slide-14
SLIDE 14

Another Example reverse :: [α] → [α] tail :: [α] → [α] For every choice of g and l: reverse (map g l) = map g (reverse l) tail (map g l) = map g (tail l)

2

slide-15
SLIDE 15

Another Example reverse :: [α] → [α] tail :: [α] → [α] f :: [α] → [α] For every choice of g and l: reverse (map g l) = map g (reverse l) tail (map g l) = map g (tail l) f (map g l) = map g (f l)

2

slide-16
SLIDE 16

Automatic Generation of Free Theorems At http://www-ps.iai.uni-bonn.de/ft:

3

slide-17
SLIDE 17

Automatic Generation of Free Theorems

3

slide-18
SLIDE 18

Applications

◮ Short Cut Fusion [Gill et al., FPCA’93]

4

slide-19
SLIDE 19

Applications

◮ Short Cut Fusion [Gill et al., FPCA’93] ◮ The Dual of Short Cut Fusion

[Takano & Meijer, FPCA’95], [Svenningsson, ICFP’02]

◮ . . .

4

slide-20
SLIDE 20

Applications

◮ Short Cut Fusion [Gill et al., FPCA’93] ◮ The Dual of Short Cut Fusion

[Takano & Meijer, FPCA’95], [Svenningsson, ICFP’02]

◮ . . . ◮ Knuth’s 0-1-principle and the like

[Day et al., Haskell’99], [V., POPL’08]

4

slide-21
SLIDE 21

Applications

◮ Short Cut Fusion [Gill et al., FPCA’93] ◮ The Dual of Short Cut Fusion

[Takano & Meijer, FPCA’95], [Svenningsson, ICFP’02]

◮ . . . ◮ Knuth’s 0-1-principle and the like

[Day et al., Haskell’99], [V., POPL’08]

◮ Bidirectionalization [V., POPL’09]

4

slide-22
SLIDE 22

Applications

◮ Short Cut Fusion [Gill et al., FPCA’93] ◮ The Dual of Short Cut Fusion

[Takano & Meijer, FPCA’95], [Svenningsson, ICFP’02]

◮ . . . ◮ Knuth’s 0-1-principle and the like

[Day et al., Haskell’99], [V., POPL’08]

◮ Bidirectionalization [V., POPL’09] ◮ . . . ◮ Testing polymorphic properties

[Bernardy et al., ESOP’10]

4

slide-23
SLIDE 23

What About Efficiency? f :: α → Nat f :: α → α → α f :: α → (α, α) f (g x) = f x f (g x) (g y) = g (f x y) f (g x) = let y = f x in (g (fst y), g (snd y))

5

slide-24
SLIDE 24

What About Efficiency? f :: α → Nat f :: α → α → α f :: α → (α, α) f (g x) = f x f (g x) (g y) = g (f x y) f (g x) = let y = f x in (g (fst y), g (snd y)) call-by- value

> > <

5

slide-25
SLIDE 25

What About Efficiency? f :: α → Nat f :: α → α → α f :: α → (α, α) f (g x) = f x f (g x) (g y) = g (f x y) f (g x) = let y = f x in (g (fst y), g (snd y)) call-by- value

> > <

call-by- name

= = < (?)

5

slide-26
SLIDE 26

What About Efficiency? f :: α → Nat f :: α → α → α f :: α → (α, α) f (g x) = f x f (g x) (g y) = g (f x y) f (g x) = let y = f x in (g (fst y), g (snd y)) call-by- value

> > <

call-by- name

= = < (?)

call-by- need

= = <

5

slide-27
SLIDE 27

What About Efficiency? f :: α → Nat f :: α → α → α f :: α → (α, α) f (g x) = f x f (g x) (g y) = g (f x y) f (g x) = let y = f x in (g (fst y), g (snd y)) call-by- value

> > <

call-by- name

= = < (?)

call-by- need

= = <

5

slide-28
SLIDE 28

What About Efficiency? Now, how about, say f :: α → α → (α, α) ?

6

slide-29
SLIDE 29

What About Efficiency? Now, how about, say f :: α → α → (α, α) ? Or f :: [α] → [α] ?

6

slide-30
SLIDE 30

What About Efficiency? Now, how about, say f :: α → α → (α, α) ? Or f :: [α] → [α] ? And how to actually establish such statements?

6

slide-31
SLIDE 31

But isn’t it Somehow Trivial? Recall f :: α → Nat, with standard free theorem: f (g x) = f x for all choices of types τ1, τ2, function g :: τ1 → τ2, and x :: τ1.

7

slide-32
SLIDE 32

But isn’t it Somehow Trivial? Recall f :: α → Nat, with standard free theorem: f (g x) = f x for all choices of types τ1, τ2, function g :: τ1 → τ2, and x :: τ1. Clearly, this means that f is a constant function, i.e., for all x and y: f y = f x

7

slide-33
SLIDE 33

But isn’t it Somehow Trivial? Recall f :: α → Nat, with standard free theorem: f (g x) = f x for all choices of types τ1, τ2, function g :: τ1 → τ2, and x :: τ1. Clearly, this means that f is a constant function, i.e., for all x and y: f y = f x So, “obviously”, f (g x) ⊒ f x where ⊒ means “the same result, and equally fast

  • r slower”.

7

slide-34
SLIDE 34

But isn’t it Somehow Trivial? Recall f :: α → Nat, with standard free theorem: f (g x) = f x for all choices of types τ1, τ2, function g :: τ1 → τ2, and x :: τ1. Clearly, this means that f is a constant function, i.e., for all x and y: f y = f x So, “obviously”, f (g x) ⊒ f x where ⊒ means “the same result, and equally fast

  • r slower”. What’s wrong with this reasoning?

7

slide-35
SLIDE 35

But isn’t it Somehow Trivial? Consider: f x = if x == 0 g x = 0 then 0 else f (x − 1)

8

slide-36
SLIDE 36

But isn’t it Somehow Trivial? Consider: f x = if x == 0 g x = 0 then 0 else f (x − 1) Certainly, for all x and y: f y = f x

8

slide-37
SLIDE 37

But isn’t it Somehow Trivial? Consider: f x = if x == 0 g x = 0 then 0 else f (x − 1) Certainly, for all x and y: f y = f x But certainly not, for x > 0: f (g x) ⊒ f x

8

slide-38
SLIDE 38

But isn’t it Somehow Trivial? Consider: f :: Nat → Nat g :: α → Nat f x = if x == 0 g x = 0 then 0 else f (x − 1) Certainly, for all x and y: f y = f x But certainly not, for x > 0: f (g x) ⊒ f x

8

slide-39
SLIDE 39

But isn’t it Somehow Trivial? Consider: f :: Nat → Nat g :: α → Nat f x = if x == 0 g x = 0 then 0 else f (x − 1) Certainly, for all x and y: f y = f x But certainly not, for x > 0: f (g x) ⊒ f x Exploiting polymorphism is really essential, and not just for the extensional statements!

8

slide-40
SLIDE 40

Free Theorems, Formally — In a Nutshell

Syntax for a typed λ-calculus: τ ::= α | Nat | τ → τ | . . . t ::= x | n | t + t | λx :: τ.t | t t | . . .

9

slide-41
SLIDE 41

Free Theorems, Formally — In a Nutshell

Syntax for a typed λ-calculus: τ ::= α | Nat | τ → τ | . . . t ::= x | n | t + t | λx :: τ.t | t t | . . . Semantics: αθ = θ(α) Natθ = N τ1 → τ2θ = τ2θ

τ1θ

θ ∈ SetTVar

9

slide-42
SLIDE 42

Free Theorems, Formally — In a Nutshell

Syntax for a typed λ-calculus: τ ::= α | Nat | τ → τ | . . . t ::= x | n | t + t | λx :: τ.t | t t | . . . Semantics: αθ = θ(α) xσ = σ(x) Natθ = N nσ = n τ1 → τ2θ = τ2θ

τ1θ

λx :: τ.tσ = λv.tσ[x→v] θ ∈ SetTVar t1 t2σ = t1σ t2σ

9

slide-43
SLIDE 43

Free Theorems, Formally — In a Nutshell

Syntax for a typed λ-calculus: τ ::= α | Nat | τ → τ | . . . t ::= x | n | t + t | λx :: τ.t | t t | . . . Semantics: αθ = θ(α) xσ = σ(x) Natθ = N nσ = n τ1 → τ2θ = τ2θ

τ1θ

λx :: τ.tσ = λv.tσ[x→v] θ ∈ SetTVar t1 t2σ = t1σ t2σ Logical relation: ∆α,ρ = ρ(α) ∆Nat,ρ = idN ∆τ1→τ2,ρ = {(f, g) | ∀(x, y) ∈ ∆τ1,ρ. (f x, g y) ∈ ∆τ2,ρ} with ρ ∈ RelTVar

9

slide-44
SLIDE 44

Free Theorems, Formally — In a Nutshell

Syntax for a typed λ-calculus: τ ::= α | Nat | τ → τ | . . . t ::= x | n | t + t | λx :: τ.t | t t | . . . Semantics: αθ = θ(α) xσ = σ(x) Natθ = N nσ = n τ1 → τ2θ = τ2θ

τ1θ

λx :: τ.tσ = λv.tσ[x→v] θ ∈ SetTVar t1 t2σ = t1σ t2σ Logical relation: ∆α,ρ = ρ(α) ∆Nat,ρ = idN ∆τ1→τ2,ρ = {(f, g) | ∀(x, y) ∈ ∆τ1,ρ. (f x, g y) ∈ ∆τ2,ρ} with ρ ∈ RelTVar Theorem: for closed term t of type τ, (t∅, t∅) ∈ ∆τ,ρ.

9

slide-45
SLIDE 45

An Example Derivation, for f :: α → Nat ∀ρ, t closed with t :: τ. (t∅, t∅) ∈ ∆τ,ρ

10

slide-46
SLIDE 46

An Example Derivation, for f :: α → Nat ∀ρ, t closed with t :: τ. (t∅, t∅) ∈ ∆τ,ρ ⇒ (t = f and τ = α → Nat) ∀ρ. (f∅, f∅) ∈ ∆α→Nat,ρ

10

slide-47
SLIDE 47

An Example Derivation, for f :: α → Nat ∀ρ, t closed with t :: τ. (t∅, t∅) ∈ ∆τ,ρ ⇒ (t = f and τ = α → Nat) ∀ρ. (f∅, f∅) ∈ ∆α→Nat,ρ ⇔ (∆τ1→τ2,ρ = {(f, g) | ∀(x, y) ∈ ∆τ1,ρ. (f x, g y) ∈ ∆τ2,ρ}) ∀ρ, (x, y) ∈ ∆α,ρ. (f∅ x, f∅ y) ∈ ∆Nat,ρ

10

slide-48
SLIDE 48

An Example Derivation, for f :: α → Nat ∀ρ, t closed with t :: τ. (t∅, t∅) ∈ ∆τ,ρ ⇒ (t = f and τ = α → Nat) ∀ρ. (f∅, f∅) ∈ ∆α→Nat,ρ ⇔ (∆τ1→τ2,ρ = {(f, g) | ∀(x, y) ∈ ∆τ1,ρ. (f x, g y) ∈ ∆τ2,ρ}) ∀ρ, (x, y) ∈ ∆α,ρ. (f∅ x, f∅ y) ∈ ∆Nat,ρ ⇒ (∆α,ρ = ρ(α), ρ(α) := g ∈ τ2∅

τ1∅, ∆Nat,ρ = idN)

∀g ∈ τ2∅

τ1∅, x ∈ τ1∅. f∅ x = f∅ (g x)

10

slide-49
SLIDE 49

An Example Derivation, for f :: α → Nat ∀ρ, t closed with t :: τ. (t∅, t∅) ∈ ∆τ,ρ ⇒ (t = f and τ = α → Nat) ∀ρ. (f∅, f∅) ∈ ∆α→Nat,ρ ⇔ (∆τ1→τ2,ρ = {(f, g) | ∀(x, y) ∈ ∆τ1,ρ. (f x, g y) ∈ ∆τ2,ρ}) ∀ρ, (x, y) ∈ ∆α,ρ. (f∅ x, f∅ y) ∈ ∆Nat,ρ ⇒ (∆α,ρ = ρ(α), ρ(α) := g ∈ τ2∅

τ1∅, ∆Nat,ρ = idN)

∀g ∈ τ2∅

τ1∅, x ∈ τ1∅. f∅ x = f∅ (g x)

⇒ (term semantics) ∀g :: τ1 → τ2, x :: τ1. f x∅ = f (g x)∅

10

slide-50
SLIDE 50

Bringing Costs into the Picture New semantics: x¢

σ

= (σ(x), 0) n¢

σ

= (n, 0) λx :: τ.t¢

σ = (λv.1 ⊲t¢ σ[x→v], 0)

t1 t2¢

σ

= t1¢

σ ¢ t2¢ σ

where: c ⊲(v, c′) = (v, c + c′) and f ¢ x = (c + c′) ⊲(g v) if f = (g, c), x = (v, c′)

11

slide-51
SLIDE 51

Bringing Costs into the Picture New semantics: x¢

σ

= (σ(x), 0) n¢

σ

= (n, 0) λx :: τ.t¢

σ = (λv.1 ⊲t¢ σ[x→v], 0)

t1 t2¢

σ

= t1¢

σ ¢ t2¢ σ

where: c ⊲(v, c′) = (v, c + c′) and f ¢ x = (c + c′) ⊲(g v) if f = (g, c), x = (v, c′) Note: t :: α → Nat implies t¢

∅ ∈ C(C(N)θ(α)),

for every θ ∈ SetTVar, where C(S) = {(v, c) | v ∈ S, c ∈ Z}.

11

slide-52
SLIDE 52

How about the Logical Relation? Tempting would be: ∆¢

α,ρ

= ρ(α) ∆¢

Nat,ρ = idN×Z

∆¢

τ1→τ2,ρ = {(f, g) | ∀(x, y) ∈ ∆¢ τ1,ρ.

(f ¢ x, g ¢ y) ∈ ∆¢

τ2,ρ}

with ρ(α1), ρ(α2), . . . ⊆ C(Si) × C(Ti)

12

slide-53
SLIDE 53

How about the Logical Relation? Tempting would be: ∆¢

α,ρ

= ρ(α) ∆¢

Nat,ρ = idN×Z

∆¢

τ1→τ2,ρ = {(f, g) | ∀(x, y) ∈ ∆¢ τ1,ρ.

(f ¢ x, g ¢ y) ∈ ∆¢

τ2,ρ}

with ρ(α1), ρ(α2), . . . ⊆ C(Si) × C(Ti) But NO, does not work!

12

slide-54
SLIDE 54

How about the Logical Relation? Tempting would be: ∆¢

α,ρ

= ρ(α) ∆¢

Nat,ρ = idN×Z

∆¢

τ1→τ2,ρ = {(f, g) | ∀(x, y) ∈ ∆¢ τ1,ρ.

(f ¢ x, g ¢ y) ∈ ∆¢

τ2,ρ}

with ρ(α1), ρ(α2), . . . ⊆ C(Si) × C(Ti) But NO, does not work! It would allow us to conclude from: ∀ρ, f :: α → Nat. (f¢

∅, f¢ ∅) ∈ ∆¢ α→Nat,ρ

that: ∀g :: τ1 → τ2, x :: τ1. f x¢

∅ = f (g x)¢ ∅

12

slide-55
SLIDE 55

How about the Logical Relation? Much more disciplined: ∆¢

α,ρ

= C(ρ(α)) ∆¢

Nat,ρ = idN×Z

∆¢

τ1→τ2,ρ = {(f, g) | cost(f) = cost(g)

∧ ∀(x, y) ∈ ∆¢

τ1,ρ. (f ¢ x, g ¢ y) ∈ ∆¢ τ2,ρ}

with ρ(α1), ρ(α2), . . . ⊆ Si ×Ti, where C(R) = {((u, c), (v, c) | (u, v) ∈ R, c ∈ Z}

13

slide-56
SLIDE 56

How about the Logical Relation? Much more disciplined: ∆¢

α,ρ

= C(ρ(α)) ∆¢

Nat,ρ = idN×Z

∆¢

τ1→τ2,ρ = {(f, g) | cost(f) = cost(g)

∧ ∀(x, y) ∈ ∆¢

τ1,ρ. (f ¢ x, g ¢ y) ∈ ∆¢ τ2,ρ}

with ρ(α1), ρ(α2), . . . ⊆ Si ×Ti, where C(R) = {((u, c), (v, c) | (u, v) ∈ R, c ∈ Z} Now indeed . . . Theorem: for closed term t of type τ, (t¢

∅, t¢ ∅) ∈ ∆¢ τ,ρ

13

slide-57
SLIDE 57

Now, What about our Example f :: α → Nat ? ∀ρ. (f¢

∅, f¢ ∅) ∈ ∆¢ α→Nat,ρ

14

slide-58
SLIDE 58

Now, What about our Example f :: α → Nat ? ∀ρ. (f¢

∅, f¢ ∅) ∈ ∆¢ α→Nat,ρ

⇒ (∆¢

τ1→τ2,ρ= {(f, g) | cost(f) = cost(g)

∧∀(x, y) ∈ ∆¢

τ1,ρ. (f ¢ x, g ¢ y) ∈ ∆¢ τ2,ρ})

∀ρ, (x, y) ∈ ∆¢

α,ρ. (f¢ ∅ ¢ x, f¢ ∅ ¢ y) ∈ ∆¢ Nat,ρ

14

slide-59
SLIDE 59

Now, What about our Example f :: α → Nat ? ∀ρ. (f¢

∅, f¢ ∅) ∈ ∆¢ α→Nat,ρ

⇒ (∆¢

τ1→τ2,ρ= {(f, g) | cost(f) = cost(g)

∧∀(x, y) ∈ ∆¢

τ1,ρ. (f ¢ x, g ¢ y) ∈ ∆¢ τ2,ρ})

∀ρ, (x, y) ∈ ∆¢

α,ρ. (f¢ ∅ ¢ x, f¢ ∅ ¢ y) ∈ ∆¢ Nat,ρ

⇒ (∆¢

α,ρ = C(ρ(α)), ρ(α) := R ∈ Rel, ∆¢ Nat,ρ = idN×Z)

∀R ∈ Rel, (x, y) ∈ C(R). f¢

∅ ¢ x = f¢ ∅ ¢ y

14

slide-60
SLIDE 60

Now, What about our Example f :: α → Nat ? ∀ρ. (f¢

∅, f¢ ∅) ∈ ∆¢ α→Nat,ρ

⇒ (∆¢

τ1→τ2,ρ= {(f, g) | cost(f) = cost(g)

∧∀(x, y) ∈ ∆¢

τ1,ρ. (f ¢ x, g ¢ y) ∈ ∆¢ τ2,ρ})

∀ρ, (x, y) ∈ ∆¢

α,ρ. (f¢ ∅ ¢ x, f¢ ∅ ¢ y) ∈ ∆¢ Nat,ρ

⇒ (∆¢

α,ρ = C(ρ(α)), ρ(α) := R ∈ Rel, ∆¢ Nat,ρ = idN×Z)

∀R ∈ Rel, (x, y) ∈ C(R). f¢

∅ ¢ x = f¢ ∅ ¢ y

How to choose R to bring g :: τ1 → τ2 into play?

14

slide-61
SLIDE 61

Now, What about our Example f :: α → Nat ? ∀ρ. (f¢

∅, f¢ ∅) ∈ ∆¢ α→Nat,ρ

⇒ (∆¢

τ1→τ2,ρ= {(f, g) | cost(f) = cost(g)

∧∀(x, y) ∈ ∆¢

τ1,ρ. (f ¢ x, g ¢ y) ∈ ∆¢ τ2,ρ})

∀ρ, (x, y) ∈ ∆¢

α,ρ. (f¢ ∅ ¢ x, f¢ ∅ ¢ y) ∈ ∆¢ Nat,ρ

⇒ (∆¢

α,ρ = C(ρ(α)), ρ(α) := R ∈ Rel, ∆¢ Nat,ρ = idN×Z)

∀R ∈ Rel, (x, y) ∈ C(R). f¢

∅ ¢ x = f¢ ∅ ¢ y

How to choose R to bring g :: τ1 → τ2 into play? Problem: {(x, y) | g¢

∅ ¢ x = y} not C(R) for any R

14

slide-62
SLIDE 62

Now, What about our Example f :: α → Nat ? Problem: {(x, y) | g¢

∅ ¢ x = y} not C(R) for any R

Solution: but {(c ⊲ appCost(g, x) ⊲ x, c ⊲ y) | g¢

∅ ¢ x = y, c ∈ Z} = C(Rg)

for Rg = {(val(x), val(g¢

∅ ¢ x)) | x ∈ τ1¢ ∅}

where appCost(g, x) = cost(g¢

∅ ¢ x) − cost(x)

15

slide-63
SLIDE 63

Now, What about our Example f :: α → Nat ? Problem: {(x, y) | g¢

∅ ¢ x = y} not C(R) for any R

Solution: but {(c ⊲ appCost(g, x) ⊲ x, c ⊲ y) | g¢

∅ ¢ x = y, c ∈ Z} = C(Rg)

for Rg = {(val(x), val(g¢

∅ ¢ x)) | x ∈ τ1¢ ∅}

where appCost(g, x) = cost(g¢

∅ ¢ x) − cost(x)

Now: ∀R ∈ Rel, (x, y) ∈ C(R). f¢

∅ ¢ x = f¢ ∅ ¢ y

⇒ ∀x ∈ τ1¢

∅. f¢ ∅ ¢ (appCost(g, x) ⊲ x)

= f¢

∅ ¢ (g¢ ∅ ¢ x)

15

slide-64
SLIDE 64

Now, What about our Example f :: α → Nat ? Now: ∀R ∈ Rel, (x, y) ∈ C(R). f¢

∅ ¢ x = f¢ ∅ ¢ y

⇒ ∀x ∈ τ1¢

∅. f¢ ∅ ¢ (appCost(g, x) ⊲ x)

= f¢

∅ ¢ (g¢ ∅ ¢ x)

15

slide-65
SLIDE 65

Now, What about our Example f :: α → Nat ? Now: ∀R ∈ Rel, (x, y) ∈ C(R). f¢

∅ ¢ x = f¢ ∅ ¢ y

⇒ ∀x ∈ τ1¢

∅. f¢ ∅ ¢ (appCost(g, x) ⊲ x)

= f¢

∅ ¢ (g¢ ∅ ¢ x)

⇒ ∀x :: τ1. appCost(g, x¢

∅) ⊲(f¢ ∅ ¢ x¢ ∅)

= f¢

∅ ¢ (g¢ ∅ ¢ x¢ ∅)

15

slide-66
SLIDE 66

Now, What about our Example f :: α → Nat ? Now: ∀R ∈ Rel, (x, y) ∈ C(R). f¢

∅ ¢ x = f¢ ∅ ¢ y

⇒ ∀x ∈ τ1¢

∅. f¢ ∅ ¢ (appCost(g, x) ⊲ x)

= f¢

∅ ¢ (g¢ ∅ ¢ x)

⇒ ∀x :: τ1. appCost(g, x¢

∅) ⊲(f¢ ∅ ¢ x¢ ∅)

= f¢

∅ ¢ (g¢ ∅ ¢ x¢ ∅)

⇒ ∀x :: τ1. appCost(g, x¢

∅) ⊲f x¢ ∅ = f (g x)¢ ∅

15

slide-67
SLIDE 67

Now, What about our Example f :: α → Nat ? Now: ∀R ∈ Rel, (x, y) ∈ C(R). f¢

∅ ¢ x = f¢ ∅ ¢ y

⇒ ∀x ∈ τ1¢

∅. f¢ ∅ ¢ (appCost(g, x) ⊲ x)

= f¢

∅ ¢ (g¢ ∅ ¢ x)

⇒ ∀x :: τ1. appCost(g, x¢

∅) ⊲(f¢ ∅ ¢ x¢ ∅)

= f¢

∅ ¢ (g¢ ∅ ¢ x¢ ∅)

⇒ ∀x :: τ1. appCost(g, x¢

∅) ⊲f x¢ ∅ = f (g x)¢ ∅

Hence: f x ⊏ f (g x)

15

slide-68
SLIDE 68

Let’s Try another Example, f :: α → α ∀ρ. (f¢

∅, f¢ ∅) ∈ ∆¢ α→α,ρ

16

slide-69
SLIDE 69

Let’s Try another Example, f :: α → α ∀ρ. (f¢

∅, f¢ ∅) ∈ ∆¢ α→α,ρ

⇒ ∀ρ, (x, y) ∈ ∆¢

α,ρ. (f¢ ∅ ¢ x, f¢ ∅ ¢ y) ∈ ∆¢ α,ρ

16

slide-70
SLIDE 70

Let’s Try another Example, f :: α → α ∀ρ. (f¢

∅, f¢ ∅) ∈ ∆¢ α→α,ρ

⇒ ∀ρ, (x, y) ∈ ∆¢

α,ρ. (f¢ ∅ ¢ x, f¢ ∅ ¢ y) ∈ ∆¢ α,ρ

⇒ ∀g :: τ1 → τ2, (x, y) ∈ C(Rg). (f¢

∅ ¢ x, f¢ ∅ ¢ y) ∈ C(Rg)

16

slide-71
SLIDE 71

Let’s Try another Example, f :: α → α ∀ρ. (f¢

∅, f¢ ∅) ∈ ∆¢ α→α,ρ

⇒ ∀ρ, (x, y) ∈ ∆¢

α,ρ. (f¢ ∅ ¢ x, f¢ ∅ ¢ y) ∈ ∆¢ α,ρ

⇒ ∀g :: τ1 → τ2, (x, y) ∈ C(Rg). (f¢

∅ ¢ x, f¢ ∅ ¢ y) ∈ C(Rg)

⇒ ∀g :: τ1 → τ2, x ∈ τ1¢

∅.

(f¢

∅ ¢ (appCost(g, x) ⊲ x),

∅ ¢ (g¢ ∅ ¢ x)) ∈ C(Rg)

16

slide-72
SLIDE 72

Let’s Try another Example, f :: α → α ∀ρ. (f¢

∅, f¢ ∅) ∈ ∆¢ α→α,ρ

⇒ ∀ρ, (x, y) ∈ ∆¢

α,ρ. (f¢ ∅ ¢ x, f¢ ∅ ¢ y) ∈ ∆¢ α,ρ

⇒ ∀g :: τ1 → τ2, (x, y) ∈ C(Rg). (f¢

∅ ¢ x, f¢ ∅ ¢ y) ∈ C(Rg)

⇒ ∀g :: τ1 → τ2, x ∈ τ1¢

∅.

(f¢

∅ ¢ (appCost(g, x) ⊲ x),

∅ ¢ (g¢ ∅ ¢ x)) ∈ C(Rg)

Does this imply g¢

∅ ¢ (f¢ ∅ ¢ x) = f¢ ∅ ¢ (g¢ ∅ ¢ x) ?

16

slide-73
SLIDE 73

Let’s Try another Example, f :: α → α ∀g :: τ1 → τ2, x ∈ τ1¢

∅.

(appCost(g, x) ⊲(f¢

∅ ¢ x),

∅ ¢ (g¢ ∅ ¢ x)) ∈ C(Rg)

Does this imply g¢

∅ ¢ (f¢ ∅ ¢ x) = f¢ ∅ ¢ (g¢ ∅ ¢ x) ?

Let’s see: C(Rg) = {(c ⊲ appCost(g, x) ⊲ x, c ⊲ y) | g¢

∅ ¢ x = y, c ∈ Z}

16

slide-74
SLIDE 74

Let’s Try another Example, f :: α → α ∀g :: τ1 → τ2, x ∈ τ1¢

∅.

(appCost(g, x) ⊲(f¢

∅ ¢ x),

∅ ¢ (g¢ ∅ ¢ x)) ∈ C(Rg)

Does this imply g¢

∅ ¢ (f¢ ∅ ¢ x) = f¢ ∅ ¢ (g¢ ∅ ¢ x) ?

Let’s see: C(Rg) = {(c ⊲ appCost(g, x′) ⊲ x′, c ⊲ y) | g¢

∅ ¢ x′ = y, c ∈ Z}

16

slide-75
SLIDE 75

Let’s Try another Example, f :: α → α ∀g :: τ1 → τ2, x ∈ τ1¢

∅.

(appCost(g, x) ⊲(f¢

∅ ¢ x),

∅ ¢ (g¢ ∅ ¢ x)) ∈ C(Rg)

Let’s see: C(Rg) = {(c ⊲ appCost(g, x′) ⊲ x′, c ⊲ y) | g¢

∅ ¢ x′ = y, c ∈ Z}

Actually, the above only imply: ∀x.∃x′. appCost(g, x) ⊲(g¢

∅ ¢ (f¢ ∅ ¢ x))

= appCost(g, x′) ⊲(f¢

∅ ¢ (g¢ ∅ ¢ x))

16

slide-76
SLIDE 76

Let’s Try another Example, f :: α → α Define: Rg

x = {(val(x¢ ∅), val(g¢ ∅ ¢ x¢ ∅))}

17

slide-77
SLIDE 77

Let’s Try another Example, f :: α → α Define: Rg

x = {(val(x¢ ∅), val(g¢ ∅ ¢ x¢ ∅))}

Then: C(Rg

x ) = {(c ⊲ appCost(g, x¢ ∅) ⊲x¢ ∅,

c ⊲(g¢

∅ ¢ x¢ ∅)) | c ∈ Z}

17

slide-78
SLIDE 78

Let’s Try another Example, f :: α → α Define: Rg

x = {(val(x¢ ∅), val(g¢ ∅ ¢ x¢ ∅))}

Then: C(Rg

x ) = {(c ⊲ appCost(g, x¢ ∅) ⊲x¢ ∅,

c ⊲(g¢

∅ ¢ x¢ ∅)) | c ∈ Z}

Thus: · · · ⇒ ∀g :: τ1 → τ2, x :: τ1, (x, y) ∈ C(Rg

x ).

(f¢

∅ ¢ x, f¢ ∅ ¢ y) ∈ C(Rg x )

⇒ ∀g :: τ1 → τ2, x :: τ1. (f¢

∅ ¢ (appCost(g, x¢ ∅) ⊲x¢ ∅),

∅ ¢ (g¢ ∅ ¢ x¢ ∅)) ∈ C(Rg x )

17

slide-79
SLIDE 79

Let’s Try another Example, f :: α → α Then: C(Rg

x ) = {(c ⊲ appCost(g, x¢ ∅) ⊲x¢ ∅,

c ⊲(g¢

∅ ¢ x¢ ∅)) | c ∈ Z}

Thus: · · · ⇒ ∀g :: τ1 → τ2, x :: τ1, (x, y) ∈ C(Rg

x ).

(f¢

∅ ¢ x, f¢ ∅ ¢ y) ∈ C(Rg x )

⇒ ∀g :: τ1 → τ2, x :: τ1. (f¢

∅ ¢ (appCost(g, x¢ ∅) ⊲x¢ ∅),

∅ ¢ (g¢ ∅ ¢ x¢ ∅)) ∈ C(Rg x )

⇒ ∀g :: τ1 → τ2, x :: τ1. g¢

∅ ¢ (f¢ ∅ ¢ x¢ ∅) = f¢ ∅ ¢ (g¢ ∅ ¢ x¢ ∅)

17

slide-80
SLIDE 80

Other Examples For f :: α → α → α, g (f x y) ⊏ f (g x) (g y)

18

slide-81
SLIDE 81

Other Examples For f :: α → α → α, g (f x y) ⊏ f (g x) (g y) For f :: α → (α, α), mapPair (g, g) (f x) ⊐ f (g x)

18

slide-82
SLIDE 82

Other Examples For f :: α → α → α, g (f x y) ⊏ f (g x) (g y) For f :: α → (α, α), mapPair (g, g) (f x) ⊐ f (g x) For f :: [α] → Nat, f l ⊏ f (map g l)

18

slide-83
SLIDE 83

Other Examples For f :: α → α → α, g (f x y) ⊏ f (g x) (g y) For f :: α → (α, α), mapPair (g, g) (f x) ⊐ f (g x) For f :: [α] → Nat, f l ⊏ f (map g l) For f :: [α] → [α], get conditional statements about relative efficiency of map g (f l) and f (map g l).

18

slide-84
SLIDE 84

Conclusion We did:

◮ develop of “cost-aware” logical relation and its

parametricity theorem,

19

slide-85
SLIDE 85

Conclusion We did:

◮ develop of “cost-aware” logical relation and its

parametricity theorem,

◮ realize that more effort has to go into the actual

deriving of concrete free theorems,

19

slide-86
SLIDE 86

Conclusion We did:

◮ develop of “cost-aware” logical relation and its

parametricity theorem,

◮ realize that more effort has to go into the actual

deriving of concrete free theorems,

◮ establish a number of building blocks/heuristics

for that.

19

slide-87
SLIDE 87

Conclusion We did:

◮ develop of “cost-aware” logical relation and its

parametricity theorem,

◮ realize that more effort has to go into the actual

deriving of concrete free theorems,

◮ establish a number of building blocks/heuristics

for that. Further plans:

◮ mechanize derivation of cost-aware statements,

19

slide-88
SLIDE 88

Conclusion We did:

◮ develop of “cost-aware” logical relation and its

parametricity theorem,

◮ realize that more effort has to go into the actual

deriving of concrete free theorems,

◮ establish a number of building blocks/heuristics

for that. Further plans:

◮ mechanize derivation of cost-aware statements, ◮ apply to automatic program transformations,

19

slide-89
SLIDE 89

Conclusion We did:

◮ develop of “cost-aware” logical relation and its

parametricity theorem,

◮ realize that more effort has to go into the actual

deriving of concrete free theorems,

◮ establish a number of building blocks/heuristics

for that. Further plans:

◮ mechanize derivation of cost-aware statements, ◮ apply to automatic program transformations, ◮ extend to full recursion, other language features,

19

slide-90
SLIDE 90

Conclusion We did:

◮ develop of “cost-aware” logical relation and its

parametricity theorem,

◮ realize that more effort has to go into the actual

deriving of concrete free theorems,

◮ establish a number of building blocks/heuristics

for that. Further plans:

◮ mechanize derivation of cost-aware statements, ◮ apply to automatic program transformations, ◮ extend to full recursion, other language features, ◮ investigate call-by-name/call-by-need,

19

slide-91
SLIDE 91

Conclusion We did:

◮ develop of “cost-aware” logical relation and its

parametricity theorem,

◮ realize that more effort has to go into the actual

deriving of concrete free theorems,

◮ establish a number of building blocks/heuristics

for that. Further plans:

◮ mechanize derivation of cost-aware statements, ◮ apply to automatic program transformations, ◮ extend to full recursion, other language features, ◮ investigate call-by-name/call-by-need, ◮ use more realistic cost measures?

19

slide-92
SLIDE 92

References I

J.-P. Bernardy, P. Jansson, and K. Claessen. Testing polymorphic properties. In European Symposium on Programming, Proceedings, volume 6012 of LNCS, pages 125–144. Springer-Verlag, 2010.

  • B. Bjerner and S. Holmstr¨
  • m.

A compositional approach to time analysis of first order lazy functional programs. In Functional Programming Languages and Computer Architecture, Proceedings, pages 157–165. ACM Press, 1989. N.A. Day, J. Launchbury, and J. Lewis. Logical abstractions in Haskell. In Haskell Workshop, Proceedings. Technical Report UU-CS-1999-28, Utrecht University, 1999.

20

slide-93
SLIDE 93

References II

  • A. Gill, J. Launchbury, and S.L. Peyton Jones.

A short cut to deforestation. In Functional Programming Languages and Computer Architecture, Proceedings, pages 223–232. ACM Press, 1993.

  • Y. Liu and G. G´
  • mez.

Automatic accurate cost-bound analysis for high-level languages. IEEE Transactions on Computers, 50(12):1295–1309, 2001. J.C. Reynolds. Types, abstraction and parametric polymorphism. In Information Processing, Proceedings, pages 513–523. Elsevier, 1983.

21

slide-94
SLIDE 94

References III

  • M. Rosendahl.

Automatic complexity analysis. In Functional Programming Languages and Computer Architecture, Proceedings, pages 144–156. ACM Press, 1989.

  • D. Sands.

A na¨ ıve time analysis and its theory of cost equivalence. Journal of Logic and Computation, 5(4):495–541, 1995.

  • J. Svenningsson.

Shortcut fusion for accumulating parameters & zip-like functions. In International Conference on Functional Programming, Proceedings, pages 124–132. ACM Press, 2002.

22

slide-95
SLIDE 95

References IV

  • A. Takano and E. Meijer.

Shortcut deforestation in calculational form. In Functional Programming Languages and Computer Architecture, Proceedings, pages 306–313. ACM Press, 1995.

  • J. Voigtl¨

ander. Much ado about two: A pearl on parallel prefix computation. In Principles of Programming Languages, Proceedings, pages 29–35. ACM Press, 2008.

  • J. Voigtl¨

ander. Bidirectionalization for free! In Principles of Programming Languages, Proceedings, pages 165–176. ACM Press, 2009.

23

slide-96
SLIDE 96

References V

  • P. Wadler.

Strictness analysis aids time analysis. In Principles of Programming Languages, Proceedings, pages 119–132. ACM Press, 1988.

  • P. Wadler.

Theorems for free! In Functional Programming Languages and Computer Architecture, Proceedings, pages 347–359. ACM Press, 1989.

24

slide-97
SLIDE 97

A “Real” Example: Fusion [Gill et al., FPCA’93] Extensional free theorem: For every f :: (τ → α → α) → α → α, foldr k z (f (:) [ ]) = f k z The whole point of fusion: We expect, foldr k z (f (:) [ ]) ⊒ f k z A counterexample (in call-by-value setting): f :: (Nat → α → α) → α → α f k z = case [k 5 z] of {[ ] → z; x : xs → z}

25