Flow Analysis, Linearity and PTIME David Van Horn and Harry Mairson
What is a lower bound? Higher-order control-flow analysis in retrospect: Lessons learned, lessons abandoned. Shivers, Best of PLDI retrospective: In the ensuing years, researchers have expended a great deal of effort deriving clever ways to tame the cost of the analysis. But a fundamental questions remains: To what extent is this possible? Flow Analysis, Linearity and PTIME p.1/5
Lower bounds on AI Observation. For any (useful) abstract interpretation, there exists a class of programs on which AI corresponds with concrete interpretation. Programming within this class may lead to interesting lower bounds. Flow Analysis, Linearity and PTIME p.2/5
Goal Give a transparent proof of the correspondence between evaluation and flow analysis for linear programs. Outline: Inspirations Flow analysis and linearity onsequences Flow Analysis, Linearity and PTIME p.3/5
Inspirations Flow Analysis, Linearity and PTIME p.4/5
Inspiration Linear lambda calculus and PTIME-completeness, Mairson, JFP Special Issue on Functional Pearls. The normal form of a linear λ-term is isomorphic to its most general type. Simple typing: surely a kind of static analysis. What happens with other kinds? Flow Analysis, Linearity and PTIME p.5/5
Linearity and simple types When a program is linear, there is no approximation. approximation: λx : σ.... x... x... both occurrences of x must be of type σ linearity avoids the approximation T : α β α F : α β β ( Not T ) : α β β F The same is true for flow analyses (roughly). Flow Analysis, Linearity and PTIME p.6/5
Inspiration, II ontrol-flow Analysis of Functional Programs, Midtgaard, AM omputing Surveys. (To appear. BRIS tech. report RS-07-18.) Flow analysis abound. (170+ citations and growing). Flow analysis: surely a kind of static analysis. What happens in face of linearity? Flow Analysis, Linearity and PTIME p.7/5
Outline Inspirations X Flow analysis and linearity onsequences Flow Analysis, Linearity and PTIME p.8/5
Flow analysis and linearity Flow Analysis, Linearity and PTIME p.9/5
What is flow analysis? An approximation to operational behavior of programs. let f = λx.x in ( f f )(λy.y) f may be bound to λx.x. x may be bound to λy.y or λx.x. ( f f ) may eval to λy.y or λx.x.... program may eval to λy.y or λx.x. A typical example showing the imprecision of analysis. Flow Analysis, Linearity and PTIME p.10/5
Abstract cache We have a simple language with labelled syntax: e ::= tℓ expressions (or labeled terms) t ::= x ee λx.e terms (or unlabeled expressions) b (ℓ) = {λx, λy,...} b (ℓ) = {λx,...} subexpression ℓ may eval to λx,... b (y) = {λx,...} variable y may be bound to λx,... Flow Analysis, Linearity and PTIME p.11/5
Abstraction of evaluation E J x Kρ = ρ( x ) E Jλx.eKρ = hλx.e, ρ fv(e)i E Je1 e2 Kρ = let hλx.e0, ρ i = E Je1 K in let c = E Je2 K in E J e0 K ρ [ x 7 c ] b (ℓ) E JPℓ K = hλx.e, ρi = λx.e Flow Analysis, Linearity and PTIME p.12/5
A typical specification b = P b = b = b = xℓ (λx.e)ℓ ( t ℓ1 t ℓ2 ) ℓ b is a valid analysis of P b(x) b (ℓ) b (ℓ) λx.e b = tℓ1 b = tℓ2 b (ℓ1 ) : λx.tℓ0 b = tℓ0 b (ℓ2 ) b ( x ) b (ℓ0 ) b (ℓ) Flow Analysis, Linearity and PTIME p.13/5
Single and loving it Jagannathan, et al. POPL 98 b (ℓ) = {λx } Subexpression ℓ may must eval to λx. (An idea also used by Might and Shivers, IFP 06, abstract counting.) Flow Analysis, Linearity and PTIME p.14/5
Staying single Key to staying single: linearity (1 is the loneliest number). Each variable occurs at most once, so each abstraction λx.e can be applied to at most one argument, and the abstracted value can be bound to at most one argument. b = e and e is linear, b is a complete description of Theorem. If running the program. let f 1 = λx1.x1 in let f 2 = λx2.x2 in ( f 1 f 2 )(λy.y) Flow Analysis, Linearity and PTIME p.15/5
0FA E J K? b = x ℓ b = (λx.e)ℓ b = (tℓ1 tℓ2 )ℓ 1 2 b (x) b (ℓ) b (ℓ) λx.e b = tℓ2 b = tℓ1 2 1 ℓ b (ℓ1 ) : λx.t 0 0 b = tℓ0 0 b (ℓ2 ) b ( x ) How can we reconcile superficial distinctions between these two? b (ℓ0 ) b (ℓ) E JxKρ = ρ( x ) E Jλx.eKρ = hλx.e, ρ fv(e)i E Je1 e2 Kρ = let hλx.e0, ρ i let c = E Je1 K in = E Je2 K in E Je0 Kρ [ x 7 c] Flow Analysis, Linearity and PTIME p.16/5
The path ahead 1. Specialize the 0FA algorithm for linear terms. 2. Derive an evaluator for linear terms. 3. Un-specialize to reveal typical evaluator. Flow Analysis, Linearity and PTIME p.17/5
The path ahead 1. Specialize the 0FA algorithm for linear terms. 2. Derive an evaluator for linear terms. 3. Un-specialize to reveal typical evaluator. Keep in mind: 1. Single and loving it 2. Every subterm is evaluated just once. 3. Every variable is bound just once. Flow Analysis, Linearity and PTIME p.17/5
An algorithm for 0FA General 0FA algorithm: mutates (initially empty) cache. b ( x )] b [ℓ 7 + AJ x ℓ K = b [ℓ 7 {λx.e}] AJ(λx.e)ℓ K = AJ(tℓ1 tℓ2 )ℓ K = AJtℓ1 K; AJtℓ2 K; b (ℓ1 ) do for each λx.tℓ0 in b [ x 7 + b (ℓ2 )]; AJtℓ0 K; b [ℓ 7 + b (ℓ0 )] b [ℓ 7 v ] means update b so b (ℓ) = v b [ℓ 7 + v ] means update b so b (ℓ) = v b (ℓ) Flow Analysis, Linearity and PTIME p.18/5
An algorithm for linear 0FA b ( x )] b [ℓ 7 + AJ x ℓ K = b [ℓ 7 {λx.e}] AJ(λx.e)ℓ K = AJ(tℓ1 tℓ2 )ℓ K = AJtℓ1 K; AJtℓ2 K; b (ℓ1 ) do for each λx.tℓ0 in b (ℓ0 )] b (ℓ2 )]; AJtℓ0 K; b [ℓ 7 + b [ x 7 + Observation: By singularity, the for each is unneeded. Flow Analysis, Linearity and PTIME p.19/5
An algorithm for linear 0FA b ( x )] b [ℓ 7 + AJ x ℓ K = b [ℓ 7 {λx.e}] AJ(λx.e)ℓ K = AJ(tℓ1 tℓ2 )ℓ K = AJtℓ1 K; AJtℓ2 K; b (ℓ1 ) in let { λx.tℓ0 } = b (ℓ0 )] b (ℓ2 )]; AJtℓ0 K; b [ℓ 7 + b [ x 7 + Observation: By singularity, the for each is unneeded. Again by singularity, the 7 + can be replaced with 7. Flow Analysis, Linearity and PTIME p.20/5
An algorithm for linear 0FA b [ℓ 7 b ( x )] AJ x ℓ K = b [ℓ 7 {λx.e}] AJ(λx.e)ℓ K = AJ(tℓ1 tℓ2 )ℓ K = AJtℓ1 K; AJtℓ2 K; b (ℓ1 ) in let { λx.tℓ0 } = b [ x 7 b (ℓ2 )]; AJtℓ0 K; b [ℓ 7 b (ℓ0 )] Observation: By singularity, the for each is unneeded. Again by singularity, the 7 + can be replaced with 7. Flow Analysis, Linearity and PTIME p.21/5
The path ahead 1. Specialize the 0FA algorithm for linear terms. X 2. Derive an evaluator for linear terms. 3. Un-specialize to reveal typical evaluator. Flow Analysis, Linearity and PTIME p.22/5
An algorithm for linear 0FA b [ℓ 7 b ( x )] AJ x ℓ K = b [ℓ 7 {λx.e}] AJ(λx.e)ℓ K = AJ(tℓ1 tℓ2 )ℓ K = AJtℓ1 K; AJtℓ2 K; b (ℓ1 ) in let { λx.tℓ0 } = b [ x 7 b (ℓ2 )]; AJtℓ0 K; b [ℓ 7 b (ℓ0 )] Observation: Values aren t returned; they re written into the cache. The cache is playing the role of the environment. Flow Analysis, Linearity and PTIME p.23/5
An algorithm for linear 0FA b [ℓ 7 ρb( x )] AJ x ℓ K = b [ℓ 7 {λx.e}] AJ(λx.e)ℓ K = AJ(tℓ1 tℓ2 )ℓ K = AJtℓ1 K; AJtℓ2 K; b (ℓ1 ) in let { λx.tℓ0 } = b (ℓ2 )]; AJtℓ0 K; b [ℓ 7 b (ℓ0 )] ρb[ x 7 Observation: Values aren t returned; they re written into the cache. The cache is playing the role of the environment. Flow Analysis, Linearity and PTIME p.24/5
An algorithm for linear 0FA AJ x ℓ K = ρb( x ) AJ(λx.e)ℓ K = λx.e AJ(tℓ1 tℓ2 )ℓ K = let λx.tℓ0 = AJtℓ1 K in let v = AJtℓ2 K in ρb[ x 7 v]; A J t ℓ0 K Observation: Each variable is bound just once. The cache is playing the role of a global environment Flow Analysis, Linearity and PTIME p.25/5
An algorithm for linear 0FA AJ x ℓ Kρ = ρ( x ) AJ(λx.e)ℓ Kρ = hλx.e, ρi AJ(tℓ1 tℓ2 )ℓ Kρ = let hλx.tℓ0, ρ i = AJtℓ1 Kρ1 in let c = AJtℓ2 Kρ2 in A J t ℓ0 K ρ [ x 7 c ] where ρi = ρ fv(tℓi ) and ρ1 ρ2 = Each variable is bound just once. The cache is playing the role of a global environment. Flow Analysis, Linearity and PTIME p.26/5
A linear evaluator AJ x ℓ Kρ = ρ( x ) AJ(λx.e)ℓ Kρ = hλx.e, ρi AJ(tℓ1 tℓ2 )ℓ Kρ = let hλx.tℓ0, ρ i = AJtℓ1 Kρ1 in let c = AJtℓ2 Kρ2 in A J t ℓ0 K ρ [ x 7 c ] where ρi = ρ fv(tℓi ) and ρ1 ρ2 = Observation: This is just an evaluator for linear programs. Flow Analysis, Linearity and PTIME p.27/5
The path ahead 1. Specialize the 0FA algorithm for linear terms. X 2. Derive an evaluator for linear terms. X 3. Un-specialize to reveal typical evaluator. Flow Analysis, Linearity and PTIME p.28/5
A linear evaluator E J x ℓ Kρ = ρ( x ) E J(λx.e)ℓ Kρ = hλx.e, ρi E J(tℓ1 tℓ2 )ℓ Kρ = let hλx.tℓ0, ρ i = E Jtℓ1 Kρ1 in let c = E Jtℓ2 Kρ2 in E J t ℓ0 K ρ [ x 7 c ] where ρi = ρ fv(tℓi ) and ρ1 ρ2 = Observation: This is just an evaluator for linear programs. Flow Analysis, Linearity and PTIME p.29/5
A linear evaluator E J x ℓ Kρ = ρ( x ) E J(λx.e)ℓ Kρ = hλx.e, ρi E J(tℓ1 tℓ2 )ℓ Kρ = let hλx.tℓ0, ρ i = E Jtℓ1 Kρ1 in let c = E Jtℓ2 Kρ2 in E J t ℓ0 K ρ [ x 7 c ] where ρi = ρ fv(tℓi ) and ρ1 ρ2 = Observation: This is just an evaluator. Flow Analysis, Linearity and PTIME p.30/5
A linear evaluator E J x ℓ Kρ = ρ( x ) E J(λx.e)ℓ Kρ = hλx.e, ρ i where ρ = ρ fv(e) E J(tℓ1 tℓ2 )ℓ Kρ = let hλx.tℓ0, ρ i = E Jtℓ1 Kρ in let c = E Jtℓ2 Kρ in E J t ℓ0 K ρ [ x 7 c ] where ρi = ρ fv(tℓi ) and ρ1 ρ2 = Observation: This is just an evaluator. Flow Analysis, Linearity and PTIME p.31/5
A typical evaluator E J x ℓ Kρ = ρ( x ) E J(λx.e)ℓ Kρ = hλx.e, ρ i where ρ = ρ fv(e) E J(tℓ1 tℓ2 )ℓ Kρ = let hλx.tℓ0, ρ i = E Jtℓ1 Kρ in let c = E Jtℓ2 Kρ in E J t ℓ0 K ρ [ x 7 c ] Flow Analysis, Linearity and PTIME p.32/5
Outline Inspirations X Flow analysis and linearity X onsequences Flow Analysis, Linearity and PTIME p.33/5
onsequences Flow Analysis, Linearity and PTIME p.34/5
PTIME Because evaluation of linear terms is complete for PTIME, so is 0FA. - fun - fun - fun - fun Andgate p q k= Orgate p q k= Notgate p k= opygate p k= linear ML terms Difficult to parallelize effectively. Difficult to solve in limited space. Flow Analysis, Linearity and PTIME p.35/5
Further approximations The best 0FA algorithm is cubic (and unlikely to be improved: Heintze and McAllester, LIS 97). Several further approximations to 0FA have been developed: Henglein s simple closure analysis Ashley and Dybvig s Sub-0FA Heintze and McAllester subtransitive flow analysis Mossin flow graphs But... on linear programs they are all equivalent to each other and to evaluation. They are all complete for PTIME. Flow Analysis, Linearity and PTIME p.36/5
Henglein s simple flow analysis Idea: replace with =. But: Makes no difference for a linear program! Flow Analysis, Linearity and PTIME p.37/5
Ashley and Dybvig s Sub-0FA Idea: limit the number of passes over the program. But: Just 1 will do for a linear program! Flow Analysis, Linearity and PTIME p.38/5
Beyond 0FA kfa (k > 0) is complete for EXPTIME (IFP 2008). There is no tractable algorithm for kfa. Theory and practice meet. But the exact subset of kfa is only complete for PTIME, i.e. today s technique does not lead to interesting lower bounds. Flow Analysis, Linearity and PTIME p.39/5
What has been done? Given a transparent proof of the correspondence between evaluation and flow analysis for linear programs. Identified a core language on which several variants of flow analysis (and evaluation) coincide. Shown all of these variants are PTIME complete. Sketched some ideas for abstract interpretation in general. Flow Analysis, Linearity and PTIME p.40/5
What has been done? Given a transparent proof of the correspondence between evaluation and flow analysis for linear programs. Identified a core language on which several variants of flow analysis (and evaluation) coincide. Shown all of these variants are PTIME complete. Sketched some ideas for abstract interpretation in general. Moltes Gràcies Flow Analysis, Linearity and PTIME p.40/5