Rank-2 Intersection Types for Cost Analysis of Functional Programs - - PowerPoint PPT Presentation

rank 2 intersection types for cost analysis of functional
SMART_READER_LITE
LIVE PREVIEW

Rank-2 Intersection Types for Cost Analysis of Functional Programs - - PowerPoint PPT Presentation

Rank-2 Intersection Types for Cost Analysis of Functional Programs oes 1 Kevin Hammond 1 ario Florido 2 Hugo R. Sim M Pedro B. Vasconcelos 1 1 University of St Andrews 2 Universidade do Porto TYPES, 2006 Motivation For doing cost analysis


slide-1
SLIDE 1

Rank-2 Intersection Types for Cost Analysis of Functional Programs

Hugo R. Sim˜

  • es1

Kevin Hammond1 M´ ario Florido2 Pedro B. Vasconcelos1

1University of St Andrews 2Universidade do Porto

TYPES, 2006

slide-2
SLIDE 2

Motivation

For doing cost analysis in general

◮ Compiler optimization ◮ Parallel computing ◮ Real-time systems ◮ . . .

This work in particular

◮ Reducing size-aliasing ◮ Increasing the set of typable programs

slide-3
SLIDE 3

Motivation

For doing cost analysis in general

◮ Compiler optimization ◮ Parallel computing ◮ Real-time systems ◮ . . .

This work in particular

◮ Reducing size-aliasing ◮ Increasing the set of typable programs

slide-4
SLIDE 4

Outline

Motivation Sized-Time Rank-2 Intersection Type System Language Sized-Time Rank-2 Intersection Types Typing Rules Examples Reducing Size-Aliasing Increasing the Set of Typable Programs Correctness Results Conclusions and Future Work

slide-5
SLIDE 5

Language L

◮ Language:

e ::= x | λx.e | e1e2 | n | true | false | [ ] | e1 :: e2 | if e0 then e1 else e2 | p1(e) | p2(e1, e2)

◮ β-reduction: (λx.e1)e2 →β e1[e2/x] ◮ Evaluation order: call-by-value ◮ Weak normal forms: (λx.e) →β

slide-6
SLIDE 6

Sized-Time Rank-2 ITS

Judgements A ⊢ e : v Types u ::= α | Bool | Natz | Listzu | u1

z

→u2 v ::= u | u1∧...∧un

z

→v z ::= l | n | z1 + z2 | ω Rank-2 Intersection Types

slide-7
SLIDE 7

Sized-Time Rank-2 ITS

Judgements A ⊢ e : v Types u ::= α | Bool | Natz | Listzu | u1

z

→u2 v ::= u | u1∧...∧un

z

→v z ::= l | n | z1 + z2 | ω Sized-Types

slide-8
SLIDE 8

Sized-Time Rank-2 ITS

Judgements A ⊢ e : v Types u ::= α | Bool | Natz | Listzu | u1

z

→u2 v ::= u | u1∧...∧un

z

→v z ::= l | n | z1 + z2 | ω List5Nat10 Sized-Types

slide-9
SLIDE 9

Sized-Time Rank-2 ITS

Judgements A ⊢ e : v & z Types u ::= α | Bool | Natz | Listzu | u1

z

→u2 v ::= u | u1∧...∧un

z

→v z ::= l | n | z1 + z2 | ω Type and Effect Systems

slide-10
SLIDE 10

Typing Rules

[Var∧2st] {x : u} ⊢ x : u & 0 x ∈ FV(e) A, x : u1∧...∧un ⊢ e : v & z [Abs∧2st] A ⊢ λx.e : u1∧...∧un

z

→v & 0 x / ∈ FV(e) u ∈ T0 A ⊢ e : v & z [AbsVac∧2st] A ⊢ λx.e : u z →v & 0 A0 ⊢ e1 : u1∧...∧un

z3

→v & z1 (∀i ∈ {1, ..., n}) Ai ⊢ e2 : ui & z2 [App∧2st] A0∧A1∧...∧An ⊢ e1 e2 : v & 1+z1+z2+z3 A1 ⊢ e : v1 & z1 A2 ≤1 A1 v1 ≤2 v2 z1 ≤ z2 [Sub∧2st] A2 ⊢ e : v2 & z2

slide-11
SLIDE 11

Typing Rules (cont.)

n ∈ N [Nat∧2st] ∅ ⊢ n : Natn & 0 b ∈ {true, false} [Bool∧2st] ∅ ⊢ b : Bool & 0 u ∈ T0 [Nil∧2st] ∅ ⊢ [ ] : List0u & 0 A1 ⊢ e1 : u & z1 A2 ⊢ e2 : Listzu & z2 [Cons∧2st] A1∧A2 ⊢ e1 :: e2 : List1

+ zu & z1+z2

A0 ⊢ e0 : Bool & z0 A1 ⊢ e1 : u & z A2 ⊢ e2 : u & z [If∧2st] A0∧A1∧A2 ⊢ if e0 then e1 else e2 : u & z0+z

slide-12
SLIDE 12

Subtyping Relations

(≤2) u u′ [simple≤2] u ≤2 u′ u′

1∧...∧u′ m ≤1 u1∧...∧un

v ≤2 v′ z ≤ z′ [rank2≤2] u1∧...∧un

z

→v ≤2 u′

1∧...∧u′ m z′

→v′ (≤1) n ≥ m ∃i1, ..., im ∈{1, ..., n} : ui1 u′

1, ..., uim u′ m [rank1≤1]

u1∧...∧un ≤1 u′

1∧...∧u′ m

() u = u′ [reflex] u u′ u′

1 u1

u2 u′

2

z ≤ z′ [abs] u1

z

→u2 u′

1 z′

→u′

2

z ≤ z′ [nat] Natz Natz′ z ≤ z′ u u′ [list] Listzu Listz′u′

slide-13
SLIDE 13

Example: reducing size-aliasing

...based on Hindley-Milner . . twice ≡ λf x.f (f x) : (a

l

→ a) 0 → a 2+l+l → a succ ≡ λy.add(y, 1) : Natm → Natm

+ 1

. . . A ⊢ e1 : u

z3

→u′ & z1 A ⊢ e2 : u & z2 [AppHMst] A ⊢ e1 e2 : u′ & 1+z1+z2+z3 . . twice succ : ?

slide-14
SLIDE 14

Example: reducing size-aliasing

...based on Hindley-Milner . . twice ≡ λf x.f (f x) : (a

l

→ a) 0 → a 2+l+l → a succ ≡ λy.add(y, 1) : Natm → Natm

+ 1

. . . A ⊢ e1 : u

z3

→u′ & z1 A ⊢ e2 : u & z2 [AppHMst] A ⊢ e1 e2 : u′ & 1+z1+z2+z3 . . twice succ : Natω 2 →Natω

slide-15
SLIDE 15

Example: reducing size-aliasing

...based on a rank-2 ITS . . twice ≡ λf x.f (f x) : (a

l1

→b) ∧ (b

l2

→c) 0 → a

2+l1+l2

→ c succ ≡ λy.add(y, 1) : Natm → Natm

+ 1

. A0 ⊢ e1 : u1∧...∧un

z3

→v & z1 (∀i ∈ {1, ..., n}) Ai ⊢ e2 : ui & z2 [App∧2st] A0∧A1∧...∧An ⊢ e1 e2 : v & 1+z1+z2+z3 . . twice succ : ?

slide-16
SLIDE 16

Example: reducing size-aliasing

...based on a rank-2 ITS . . twice ≡ λf x.f (f x) : (a

l1

→b) ∧ (b

l2

→c) 0 → a

2+l1+l2

→ c succ ≡ λy.add(y, 1) : Natm → Natm

+ 1

. A0 ⊢ e1 : u1∧...∧un

z3

→v & z1 (∀i ∈ {1, ..., n}) Ai ⊢ e2 : ui & z2 [App∧2st] A0∧A1∧...∧An ⊢ e1 e2 : v & 1+z1+z2+z3 . . twice succ : Natm 2 →Natm

+ 2

slide-17
SLIDE 17

Example: increasing the set of typable programs

...based on Hindley-Milner . . twice ≡ λf x.f (f x) : (a

l

→ a) 0 → a 2+l+l → a tolist ≡ λy.[y] : α 0 → List1α . . . A ⊢ e1 : u

z3

→u′ & z1 A ⊢ e2 : u & z2 [AppHMst] A ⊢ e1 e2 : u′ & 1+z1+z2+z3 . . twice tolist : ?

slide-18
SLIDE 18

Example: increasing the set of typable programs

...based on Hindley-Milner . . twice ≡ λf x.f (f x) : (a

l

→ a) 0 → a 2+l+l → a tolist ≡ λy.[y] : α 0 → List1α . . . A ⊢ e1 : u

z3

→u′ & z1 A ⊢ e2 : u & z2 [AppHMst] A ⊢ e1 e2 : u′ & 1+z1+z2+z3 . . twice tolist : not typable

slide-19
SLIDE 19

Example: increasing the set of typable programs

...based on a rank-2 ITS . . twice ≡ λf x.f (f x) : (a

l1

→b) ∧ (b

l2

→c) 0 → a

2+l1+l2

→ c tolist ≡ λy.[y] : α 0 → List1α . A0 ⊢ e1 : u1∧...∧un

z3

→v & z1 (∀i ∈ {1, ..., n}) Ai ⊢ e2 : ui & z2 [App∧2st] A0∧A1∧...∧An ⊢ e1 e2 : v & 1+z1+z2+z3 . . twice tolist : ?

slide-20
SLIDE 20

Example: increasing the set of typable programs

...based on a rank-2 ITS . . twice ≡ λf x.f (f x) : (a

l1

→b) ∧ (b

l2

→c) 0 → a

2+l1+l2

→ c tolist ≡ λy.[y] : α 0 → List1α . A0 ⊢ e1 : u1∧...∧un

z3

→v & z1 (∀i ∈ {1, ..., n}) Ai ⊢ e2 : ui & z2 [App∧2st] A0∧A1∧...∧An ⊢ e1 e2 : v & 1+z1+z2+z3 . . twice tolist : α 2 →List1List1α

slide-21
SLIDE 21

Correctness Results

◮ Theorem

(Conservative extension) If A ⊢∧2 e : v then there exists A′ ⊢ e : v′ & z′ for some A′, v′ and z′.

◮ Theorem

(Subject reduction) If e → e′ and A ⊢ e : v & z, then there exists a judgement A′ ⊢ e′ : v & z where A′ ⊆ A.

◮ Theorem

(Cost correctness) If e → e′ is a β-reduction and A ⊢ e : v & z then there exists z′ such that 1+z′ ≤ z and A′ ⊢ e′ : v & z′ where A′ ⊆ A.

◮ Theorem

(At least the same results as in the Hindley-Milner approach) If A ⊢HMst e : u & z then there exists A′ ⊢ e∗ : u & z for some A′ where e∗ = replace let by app in e.

slide-22
SLIDE 22

Correctness Results

◮ Theorem

(Conservative extension) If A ⊢∧2 e : v then there exists A′ ⊢ e : v′ & z′ for some A′, v′ and z′.

◮ Theorem

(Subject reduction) If e → e′ and A ⊢ e : v & z, then there exists a judgement A′ ⊢ e′ : v & z where A′ ⊆ A.

◮ Theorem

(Cost correctness) If e → e′ is a β-reduction and A ⊢ e : v & z then there exists z′ such that 1+z′ ≤ z and A′ ⊢ e′ : v & z′ where A′ ⊆ A.

◮ Theorem

(At least the same results as in the Hindley-Milner approach) If A ⊢HMst e : u & z then there exists A′ ⊢ e∗ : u & z for some A′ where e∗ = replace let by app in e.

slide-23
SLIDE 23

Conclusions and Future Work

Conclusions

◮ More precise size information ◮ Cost analysis: good application domain for intersection

types Future Work

◮ Cost inference algorithm ◮ Recursion