Flow Analysis, Linearity and PTIME. David Van Horn and Harry Mairson

Similar documents
Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Fundamentals and lambda calculus

Relating Complexity and Precision in Control Flow Analysis

Linear lambda calculus and PTIME-completeness

Lambda Calculus and Extensions as Foundation of Functional Programming

Lambda Calculus-2. Church Rosser Property

CMSC 330: Organization of Programming Languages

Introduction to Lambda Calculus. Lecture 7 CS /08/09

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lambda Calculus and Type Inference

CITS3211 FUNCTIONAL PROGRAMMING

Control Flow Analysis with SAT Solvers

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Lecture 5: The Untyped λ-calculus

Activity. CSCI 334: Principles of Programming Languages. Lecture 4: Fundamentals II. What is computable? What is computable?

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Lecture Notes: Control Flow Analysis for Functional Languages

Sharing in the weak lambda-calculus

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

dynamically typed dynamically scoped

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Lambda Calculus. Variables and Functions. cs3723 1

Introduction to the λ-calculus

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Flow-Sensitive Type Recovery in Linear-Log Time

lambda calculus History 11/28/13 Jianguo Lu A formal system designed to inves:gate func:on defini:on, func:on applica:on, and recursion.

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Chapter 5: The Untyped Lambda Calculus

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

Lambda Calculus. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

CMSC 330: Organization of Programming Languages. Operational Semantics

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

CIS 500 Software Foundations Fall October 2

Lecture Notes on Data Representation

More Lambda Calculus and Intro to Type Systems

Lazy Modules. Keiko Nakata Institute of Cybernetics at Tallinn University of Technology

Harvard School of Engineering and Applied Sciences Computer Science 152

Lexicografie computationala Feb., 2012

Lambda Calculus. Lecture 4 CS /26/10

The Untyped Lambda Calculus

Compilers: The goal. Safe. e : ASM

λ calculus is inconsistent

VU Semantik von Programmiersprachen

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

CSCI.4430/6969 Programming Languages Lecture Notes

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

Last class. CS Principles of Programming Languages. Introduction. Outline

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Introduction to lambda calculus Part 3

If a program is well-typed, then a type can be inferred. For example, consider the program

Recursive Definitions, Fixed Points and the Combinator

Abstract register machines Lecture 23 Tuesday, April 19, 2016

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

CMSC330. Objects, Functional Programming, and lambda calculus

Lambda Calculus. Lambda Calculus

Polymorphism and Type Inference

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

1 Introduction. 3 Syntax

International School on Rewriting (ISR 2015) Rewriting Techniques for Correctness of Program Transformations

DATABASE THEORY. Lecture 11: Introduction to Datalog. TU Dresden, 12th June Markus Krötzsch Knowledge-Based Systems

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

The Polymorphic Blame Calculus and Parametricity

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

Numerical Optimization

Type Systems Winter Semester 2006

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

Concepts of programming languages

Functional Programming Languages (FPL)

Flow Analysis. Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

Introductory Example

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Chapter 11 :: Functional Languages

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Functional programming languages

CSE 505: Concepts of Programming Languages

Introduction to the Lambda Calculus

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

The Typed λ Calculus and Type Inferencing in ML

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

More Lambda Calculus and Intro to Type Systems

CSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016

Hiding local state in direct style: a higher-order anti-frame rule

Formal Systems and their Applications

BRICS. Control-Flow Analysis of Functional Programs. BRICS RS J. Midtgaard: Control-Flow Analysis of Functional Programs

In class 75min: 2:55-4:10 Thu 9/30.

Transcription:

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