SLIDE 1 Rank-2 Intersection Types for Cost Analysis of Functional Programs
Hugo R. Sim˜
Kevin Hammond1 M´ ario Florido2 Pedro B. Vasconcelos1
1University of St Andrews 2Universidade do Porto
TYPES, 2006
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Conclusions and Future Work
Conclusions
◮ More precise size information ◮ Cost analysis: good application domain for intersection
types Future Work
◮ Cost inference algorithm ◮ Recursion