Complexity Analysis by Polymorphic Sized Type Inference and Constraint Solving. ELICA meeting Martin Avanzini 1 (Joint work with Ugo Dal Lago 2 ) 1 University of Innsbruck 2 Università di Bologna & INRIA, Sophia Antipolis
Motivation automation and modular • worst-case time-complexity analysis of functional programs • analysis should be intensionally strong , precise , amendable to
filter p Nil Example else filter p xs foldr f b Nil f :: List Int → List Int → List ( Int × Int ) f ms ns = filter (/=) ( product ms ns ) product :: ∀ αβ. List α → List β → List ( α × β ) product ms ns = foldr ( λ m ps . foldr ( λ n . Cons ( n , m )) ps ns ) Nil ms filter :: ∀ α. ( α → Bool ) → List α → List α = Nil filter p ( Cons x xs )= if p x then Cons x ( filter p xs ) foldr :: ∀ αβ. ( α → β → β ) → β → List α → β = b foldr f b ( Cons x xs )= f x ( foldr f b xs )
filter p Nil Example else filter p xs foldr f b Nil f :: List Int → List Int → List ( Int × Int ) f ms ns = filter (/=) ( product ms ns ) product :: ∀ αβ. List α → List β → List ( α × β ) product ms ns = foldr ( λ m ps . foldr ( λ n . Cons ( n , m )) ps ns ) Nil ms filter :: ∀ α. ( α → Bool ) → List α → List α = Nil filter p ( Cons x xs )= if p x then Cons x ( filter p xs ) foldr :: ∀ αβ. ( α → β → β ) → β → List α → β = b foldr f b ( Cons x xs )= f x ( foldr f b xs )
Higher-order combinators are hard to analyse… i m complexity depends not only on arguments, but also size analysis crucial step in runtime analysis complexity O k n where k is the length of xs . e n e Nil e e Cons append xs e 3. foldr m O n i n complexity O e n e 2. foldr flip append Nil e complexity O n m , where m binds length of e i ’s e n e 1. foldr append Nil e on the environment foldr ( ◦ ) b [ e 1 , e 2 , . . . , e n ] = e 1 ◦ ( e 2 ◦ ( . . . ( e n ◦ b ) . . . )) complexity depends very much on how ( ◦ ) uses its arguments
Higher-order combinators are hard to analyse… O n complexity depends not only on arguments, but also size analysis crucial step in runtime analysis complexity O k n where k is the length of xs . e n e Nil e e Cons append xs e 3. foldr m i m i n complexity O e n e 2. foldr flip append Nil e on the environment foldr ( ◦ ) b [ e 1 , e 2 , . . . , e n ] = e 1 ◦ ( e 2 ◦ ( . . . ( e n ◦ b ) . . . )) complexity depends very much on how ( ◦ ) uses its arguments 1. foldr append Nil [ e 1 , e 2 , . . . , e n ] ⇒ complexity O ( n · m ) , where m binds length of e i ’s
Higher-order combinators are hard to analyse… e Cons append xs e complexity depends not only on arguments, but also size analysis crucial step in runtime analysis complexity O k n where k is the length of xs . e n e Nil e 3. foldr on the environment foldr ( ◦ ) b [ e 1 , e 2 , . . . , e n ] = e 1 ◦ ( e 2 ◦ ( . . . ( e n ◦ b ) . . . )) complexity depends very much on how ( ◦ ) uses its arguments 1. foldr append Nil [ e 1 , e 2 , . . . , e n ] ⇒ complexity O ( n · m ) , where m binds length of e i ’s 2. foldr ( flip append ) Nil [ e 1 , e 2 , . . . , e n ] i =0 i · m ) = O ( n 2 · m ) ⇒ complexity O ( ∑ n − 1
Higher-order combinators are hard to analyse… e Cons append xs e complexity depends not only on arguments, but also size analysis crucial step in runtime analysis complexity O k n where k is the length of xs . e n e Nil e 3. foldr on the environment foldr ( ◦ ) b [ e 1 , e 2 , . . . , e n ] = e 1 ◦ ( e 2 ◦ ( . . . ( e n ◦ b ) . . . )) complexity depends very much on how ( ◦ ) uses its arguments 1. foldr append Nil [ e 1 , e 2 , . . . , e n ] ⇒ complexity O ( n · m ) , where m binds length of e i ’s 2. foldr ( flip append ) Nil [ e 1 , e 2 , . . . , e n ] i =0 i · m ) = O ( n 2 · m ) ⇒ complexity O ( ∑ n − 1
size analysis crucial step in runtime analysis Higher-order combinators are hard to analyse… complexity depends not only on arguments, but also on the environment foldr ( ◦ ) b [ e 1 , e 2 , . . . , e n ] = e 1 ◦ ( e 2 ◦ ( . . . ( e n ◦ b ) . . . )) complexity depends very much on how ( ◦ ) uses its arguments 1. foldr append Nil [ e 1 , e 2 , . . . , e n ] ⇒ complexity O ( n · m ) , where m binds length of e i ’s 2. foldr ( flip append ) Nil [ e 1 , e 2 , . . . , e n ] i =0 i · m ) = O ( n 2 · m ) ⇒ complexity O ( ∑ n − 1 3. foldr ( λ e . Cons ( append xs e )) Nil [ e 1 , e 2 , . . . , e n ] ⇒ complexity O ( k · n ) where k is the length of xs .
size analysis crucial step in runtime analysis Higher-order combinators are hard to analyse… complexity depends not only on arguments, but also on the environment foldr ( ◦ ) b [ e 1 , e 2 , . . . , e n ] = e 1 ◦ ( e 2 ◦ ( . . . ( e n ◦ b ) . . . )) complexity depends very much on how ( ◦ ) uses its arguments 1. foldr append Nil [ e 1 , e 2 , . . . , e n ] ⇒ complexity O ( n · m ) , where m binds length of e i ’s 2. foldr ( flip append ) Nil [ e 1 , e 2 , . . . , e n ] i =0 i · m ) = O ( n 2 · m ) ⇒ complexity O ( ∑ n − 1 3. foldr ( λ e . Cons ( append xs e )) Nil [ e 1 , e 2 , . . . , e n ] ⇒ complexity O ( k · n ) where k is the length of xs .
Sized-types 2. extended type system that enables reasoning about sizes 3. inference generates set of constraints, solved by external tool (e.g. SMT solver) 1. annotate datatypes with sizes List 1 α, List 2 α, List 3 α, . . . append :: ∀ ij . List i α → List j α → List i + j α
From sized-types to time complexity N e e t in e Succ t foldr N List foldr f t in let e foldr f t foldr N List N foldr f b t t f x t foldr f b t t foldr List N N t foldr f b Nil b Succ t foldr f b Cons x xs t let e t foldr f b xs t in let e idea: instrument program to compute complexity ⟨ τ → ρ ⟩ ⇒ ⟨ τ ⟩ → N → ( ⟨ ρ ⟩ × N );
From sized-types to time complexity idea: instrument program to compute complexity ⟨ τ → ρ ⟩ ⇒ ⟨ τ ⟩ → N → ( ⟨ ρ ⟩ × N ); foldr 3 :: ∀ αβ. ⟨ α → β → β ⟩ → ⟨ β ⟩ → ⟨ List α ⟩ → N → ( ⟨ β ⟩ × N ) t = ( b , Succ t ) foldr 3 f b Nil foldr 3 f b ( Cons x xs ) t = let ( e 1 , t 1 ) = foldr 3 f b xs t in let ( e 2 , t 2 ) = f x t 1 in let ( e 3 , t 3 ) = e 2 e 1 t 2 in ( e 3 , Succ t 3 ) foldr 1 :: ∀ αβ. ⟨ α → β → β ⟩ → N → ( ⟨ β → List α → β ⟩ × N ) foldr 1 f t = ( foldr 2 f , t ) foldr 2 :: ∀ αβ. ⟨ α → β → β ⟩ → ⟨ β ⟩ → N → ( ⟨ List α → β ⟩ × N ) foldr 2 f b t = ( foldr 3 f b , t )
1 : type recursive calls with type polymorphic in size indices Practical sized-type analysis (i) rev Nil monotype, … extension • consider reversal of lists: rev :: ∀ α. List α → List α → List α ys = ys rev ( Cons x xs ) ys = rev xs ( Cons x ys ) • usual let-polymorphism requires that recursive call is typed under • in sized-type setting, types of e.g. second argument changes from List i α to List i +1 α
Practical sized-type analysis (i) rev Nil monotype, … extension • consider reversal of lists: rev :: ∀ α. List α → List α → List α ys = ys rev ( Cons x xs ) ys = rev xs ( Cons x ys ) • usual let-polymorphism requires that recursive call is typed under • in sized-type setting, types of e.g. second argument changes from List i α to List i +1 α 1 : type recursive calls with type polymorphic in size indices
• even when specializing • concerns all function that use functional argument more than 2 : arbitrary-rank index polymorphic klm N j N i N i foldr L j ij N i i L j k L l L m L k m j N j Practical sized-type analysis (ii) twice extension once, in particular recursive higher-order functions N i N i N j N j ij twice to N, type in prenex form not enough l • consider higher-order combinator twice : twice :: ∀ α. ( α → α ) → α → α twice f x = f ( f x ) • term twice Succ , where Succ :: ∀ i . N i → N i +1 , cannot be typed
• concerns all function that use functional argument more than 2 : arbitrary-rank index polymorphic Practical sized-type analysis (ii) N i L k m L m L l k L j L j ij N i klm foldr N i j N j N j i twice extension once, in particular recursive higher-order functions l • consider higher-order combinator twice : twice :: ∀ α. ( α → α ) → α → α twice f x = f ( f x ) • term twice Succ , where Succ :: ∀ i . N i → N i +1 , cannot be typed • even when specializing α to N, type in prenex form not enough twice :: ∀ ij . ( N j → N j +1 ) → N i → N i +2
2 : arbitrary-rank index polymorphic Practical sized-type analysis (ii) N i L k m L m L l k L j L j ij N i klm foldr N i j N j N j i twice extension once, in particular recursive higher-order functions l • consider higher-order combinator twice : twice :: ∀ α. ( α → α ) → α → α twice f x = f ( f x ) • term twice Succ , where Succ :: ∀ i . N i → N i +1 , cannot be typed • even when specializing α to N, type in prenex form not enough twice :: ∀ ij . ( N j → N j +1 ) → N i → N i +2 • concerns all function that use functional argument more than
Recommend
More recommend