complexity analysis by polymorphic sized type inference
play

Complexity Analysis by Polymorphic Sized Type Inference and - PowerPoint PPT Presentation

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


  1. 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

  2. Motivation automation and modular • worst-case time-complexity analysis of functional programs • analysis should be intensionally strong , precise , amendable to

  3. 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 )

  4. 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 )

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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 .

  10. 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 .

  11. 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 α

  12. 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 );

  13. 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 )

  14. 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 α

  15. 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

  16. • 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

  17. • 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

  18. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend