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 in general ◮ Compiler optimization ◮ Parallel computing ◮ Real-time systems ◮ . . . This work in particular ◮ Reducing size-aliasing ◮ Increasing the set of typable programs
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
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
Language L ◮ Language: x | λ x . e | e 1 e 2 e ::= | n | true | false | [ ] | e 1 :: e 2 | if e 0 then e 1 else e 2 | p 1 ( e ) | p 2 ( e 1 , e 2 ) ◮ β -reduction: ( λ x . e 1 ) e 2 → β e 1 [ e 2 / x ] ◮ Evaluation order: call-by-value ◮ Weak normal forms: ( λ x . e ) �→ β
Sized-Time Rank-2 ITS Judgements A ⊢ e : v Types α | Bool | Nat z | List z u | u 1 z u ::= → u 2 z v ::= u | u 1 ∧ ... ∧ u n → v l | n | z 1 + z 2 | ω z ::= Rank-2 Intersection Types
Sized-Time Rank-2 ITS Judgements A ⊢ e : v Types α | Bool | Nat z | List z u | u 1 z u ::= → u 2 z v ::= u | u 1 ∧ ... ∧ u n → v l | n | z 1 + z 2 | ω z ::= Sized-Types
Sized-Time Rank-2 ITS Judgements A ⊢ e : v Types α | Bool | Nat z | List z u | u 1 z u ::= → u 2 z v ::= u | u 1 ∧ ... ∧ u n → v l | n | z 1 + z 2 | ω z ::= List 5 Nat 10 Sized-Types
Sized-Time Rank-2 ITS Judgements A ⊢ e : v & z Types α | Bool | Nat z | List z u | u 1 z u ::= → u 2 z v ::= u | u 1 ∧ ... ∧ u n → v l | n | z 1 + z 2 | ω z ::= Type and Effect Systems
Typing Rules [ Var ∧ 2 st ] { x : u } ⊢ x : u & 0 x ∈ FV ( e ) A , x : u 1 ∧ ... ∧ u n ⊢ e : v & z [ Abs ∧ 2 st ] z A ⊢ λ x . e : u 1 ∧ ... ∧ u n → v & 0 x / ∈ FV ( e ) u ∈ T 0 A ⊢ e : v & z [ AbsVac ∧ 2 st ] A ⊢ λ x . e : u z → v & 0 z 3 A 0 ⊢ e 1 : u 1 ∧ ... ∧ u n → v & z 1 ( ∀ i ∈ { 1 , ..., n } ) A i ⊢ e 2 : u i & z 2 [ App ∧ 2 st ] A 0 ∧ A 1 ∧ ... ∧ A n ⊢ e 1 e 2 : v & 1 + z 1 + z 2 + z 3 A 1 ⊢ e : v 1 & z 1 A 2 ≤ 1 A 1 v 1 ≤ 2 v 2 z 1 ≤ z 2 [ Sub ∧ 2 st ] A 2 ⊢ e : v 2 & z 2
Typing Rules (cont.) n ∈ N b ∈ { true , false } [ Nat ∧ 2 st ] [ Bool ∧ 2 st ] ∅ ⊢ n : Nat n & 0 ∅ ⊢ b : Bool & 0 u ∈ T 0 [ Nil ∧ 2 st ] ∅ ⊢ [ ] : List 0 u & 0 A 2 ⊢ e 2 : List z u & z 2 [ Cons ∧ 2 st ] A 1 ⊢ e 1 : u & z 1 A 1 ∧ A 2 ⊢ e 1 :: e 2 : List 1 z u & z 1 + z 2 + A 0 ⊢ e 0 : Bool & z 0 A 1 ⊢ e 1 : u & z A 2 ⊢ e 2 : u & z [ If ∧ 2 st ] A 0 ∧ A 1 ∧ A 2 ⊢ if e 0 then e 1 else e 2 : u & z 0 + z
Subtyping Relations u � u ′ [ simple ≤ 2 ] u ≤ 2 u ′ ( ≤ 2 ) u ′ 1 ∧ ... ∧ u ′ v ≤ 2 v ′ z ≤ z ′ m ≤ 1 u 1 ∧ ... ∧ u n [ rank2 ≤ 2 ] z z ′ → v ≤ 2 u ′ 1 ∧ ... ∧ u ′ → v ′ u 1 ∧ ... ∧ u n m ( ≤ 1 ) ∃ i 1 , ..., i m ∈{ 1 , ..., n } : u i 1 � u ′ 1 , ..., u i m � u ′ n ≥ m m [ rank1 ≤ 1 ] u 1 ∧ ... ∧ u n ≤ 1 u ′ 1 ∧ ... ∧ u ′ m ( � ) u ′ u 2 � u ′ z ≤ z ′ 1 � u 1 u = u ′ 2 [ abs � ] [ reflex � ] z z ′ u � u ′ → u 2 � u ′ → u ′ u 1 1 2 z ≤ z ′ z ≤ z ′ u � u ′ [ nat � ] [ list � ] Nat z � Nat z ′ List z u � List z ′ u ′
Example: reducing size-aliasing ...based on Hindley-Milner . . l → a ) 0 → a 2 + l + l twice ≡ λ f x . f ( f x ) : ( a → a 0 succ ≡ λ y . add ( y , 1 ) : Nat m → Nat m 1 + . . . → u ′ & z 1 z 3 A ⊢ e 1 : u A ⊢ e 2 : u & z 2 [ App HM st ] A ⊢ e 1 e 2 : u ′ & 1 + z 1 + z 2 + z 3 . . twice succ : ?
Example: reducing size-aliasing ...based on Hindley-Milner . . l → a ) 0 → a 2 + l + l twice ≡ λ f x . f ( f x ) : ( a → a 0 succ ≡ λ y . add ( y , 1 ) : Nat m → Nat m 1 + . . . → u ′ & z 1 z 3 A ⊢ e 1 : u A ⊢ e 2 : u & z 2 [ App HM st ] A ⊢ e 1 e 2 : u ′ & 1 + z 1 + z 2 + z 3 . . twice succ : Nat ω 2 → Nat ω
Example: reducing size-aliasing ...based on a rank-2 ITS . . l 1 l 2 2 + l 1 + l 2 → c ) 0 twice ≡ λ f x . f ( f x ) : ( a → b ) ∧ ( b → a → c 0 succ ≡ λ y . add ( y , 1 ) : Nat m → Nat m 1 + . z 3 A 0 ⊢ e 1 : u 1 ∧ ... ∧ u n → v & z 1 ( ∀ i ∈ { 1 , ..., n } ) A i ⊢ e 2 : u i & z 2 [ App ∧ 2 st ] A 0 ∧ A 1 ∧ ... ∧ A n ⊢ e 1 e 2 : v & 1 + z 1 + z 2 + z 3 . . twice succ : ?
Example: reducing size-aliasing ...based on a rank-2 ITS . . l 1 l 2 2 + l 1 + l 2 → c ) 0 twice ≡ λ f x . f ( f x ) : ( a → b ) ∧ ( b → a → c 0 succ ≡ λ y . add ( y , 1 ) : Nat m → Nat m 1 + . z 3 A 0 ⊢ e 1 : u 1 ∧ ... ∧ u n → v & z 1 ( ∀ i ∈ { 1 , ..., n } ) A i ⊢ e 2 : u i & z 2 [ App ∧ 2 st ] A 0 ∧ A 1 ∧ ... ∧ A n ⊢ e 1 e 2 : v & 1 + z 1 + z 2 + z 3 . . twice succ : Nat m 2 → Nat m 2 +
Example: increasing the set of typable programs ...based on Hindley-Milner . . → a ) 0 l → a 2 + l + l twice ≡ λ f x . f ( f x ) : ( a → a : α 0 → List 1 α tolist ≡ λ y . [ y ] . . . → u ′ & z 1 z 3 A ⊢ e 1 : u A ⊢ e 2 : u & z 2 [ App HM st ] A ⊢ e 1 e 2 : u ′ & 1 + z 1 + z 2 + z 3 . . twice tolist : ?
Example: increasing the set of typable programs ...based on Hindley-Milner . . → a ) 0 l → a 2 + l + l twice ≡ λ f x . f ( f x ) : ( a → a : α 0 → List 1 α tolist ≡ λ y . [ y ] . . . → u ′ & z 1 z 3 A ⊢ e 1 : u A ⊢ e 2 : u & z 2 [ App HM st ] A ⊢ e 1 e 2 : u ′ & 1 + z 1 + z 2 + z 3 . . twice tolist : not typable
Example: increasing the set of typable programs ...based on a rank-2 ITS . . l 1 → c ) 0 l 2 2 + l 1 + l 2 twice ≡ λ f x . f ( f x ) : ( a → b ) ∧ ( b → a → c : α 0 → List 1 α tolist ≡ λ y . [ y ] . z 3 A 0 ⊢ e 1 : u 1 ∧ ... ∧ u n → v & z 1 ( ∀ i ∈ { 1 , ..., n } ) A i ⊢ e 2 : u i & z 2 [ App ∧ 2 st ] A 0 ∧ A 1 ∧ ... ∧ A n ⊢ e 1 e 2 : v & 1 + z 1 + z 2 + z 3 . . twice tolist : ?
Example: increasing the set of typable programs ...based on a rank-2 ITS . . l 1 → c ) 0 l 2 2 + l 1 + l 2 twice ≡ λ f x . f ( f x ) : ( a → b ) ∧ ( b → a → c : α 0 → List 1 α tolist ≡ λ y . [ y ] . z 3 A 0 ⊢ e 1 : u 1 ∧ ... ∧ u n → v & z 1 ( ∀ i ∈ { 1 , ..., n } ) A i ⊢ e 2 : u i & z 2 [ App ∧ 2 st ] A 0 ∧ A 1 ∧ ... ∧ A n ⊢ e 1 e 2 : v & 1 + z 1 + z 2 + z 3 . . twice tolist : α 2 → List 1 List 1 α
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 ⊢ HM st e : u & z then there exists A ′ ⊢ e ∗ : u & z for some A ′ where e ∗ = replace let by app in e.
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 ⊢ HM st e : u & z then there exists A ′ ⊢ e ∗ : u & z for some A ′ where e ∗ = replace let by app in e.
Conclusions and Future Work Conclusions ◮ More precise size information ◮ Cost analysis: good application domain for intersection types Future Work ◮ Cost inference algorithm ◮ Recursion
Recommend
More recommend