Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

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

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

Lecture 5: The Untyped λ-calculus

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

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

Lambda Calculus. Lecture 4 CS /26/10

Lambda Calculus. Variables and Functions. cs3723 1

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

The Untyped Lambda Calculus

Type Systems Winter Semester 2006

Pure Lambda Calculus. Lecture 17

CMSC 330: Organization of Programming Languages

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

VU Semantik von Programmiersprachen

CIS 500 Software Foundations Fall September 25

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

The Untyped Lambda Calculus

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Chapter 5: The Untyped Lambda Calculus

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

CMSC 330: Organization of Programming Languages

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

Fundamentals and lambda calculus

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

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

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

The Untyped Lambda Calculus

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

1 Scope, Bound and Free Occurrences, Closed Terms

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

Introductory Example

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

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Concepts of programming languages

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

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

Introduction to the Lambda Calculus

CITS3211 FUNCTIONAL PROGRAMMING

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

CMSC 330: Organization of Programming Languages

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

CMSC330. Objects, Functional Programming, and lambda calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

Introduction to the λ-calculus

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

3.1 λ-calculus: Syntax

Chapter 5: The Untyped Lambda Calculus

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

Lambda Calculus. Lambda Calculus

CSE 505: Concepts of Programming Languages

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

Lambda Calculus and Extensions as Foundation of Functional Programming

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus

CS 6110 S14 Lecture 1 Introduction 24 January 2014

dynamically typed dynamically scoped

Elixir, functional programming and the Lambda calculus.

More Lambda Calculus and Intro to Type Systems

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

Recursive Definitions, Fixed Points and the Combinator

Programming Languages

Formal Systems and their Applications

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

The Untyped Lambda Calculus

Part I. Historical Origins

10.6 Theoretical Foundations

Untyped Lambda Calculus

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

Functional Programming

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

Untyped Lambda Calculus

Introduction to the Lambda Calculus. Chris Lomont

CS 314 Principles of Programming Languages. Lecture 21

More Lambda Calculus and Intro to Type Systems

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

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

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Functional Languages and Higher-Order Functions

Functional Programming

Implementing functional languages with abstract machines

Lexicografie computationala Feb., 2012

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

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

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

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

To figure this out we need a more precise understanding of how ML works

Handout 10: Imperative programs and the Lambda Calculus

Lecture Notes: Control Flow Analysis for Functional Languages

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

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.

Lambda Calculus and Type Inference

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Organization of Programming Languages CS3200/5200N. Lecture 11

A Functional Evaluation Model

The Calculator CS571. Abstract syntax of correct button push sequences. The Button Layout. Notes 16 Denotational Semantics of a Simple Calculator

CS 242. Fundamentals. Reading: See last slide

CIS 500 Software Foundations Midterm I

Transcription:

Constraint-based Analysis Harry Xu CS 253/INF 212 Spring 2013

Acknowledgements Many slides in this file were taken from Prof. Crista Lope s slides on functional programming as well as slides provided by the authors of the book Principles of Program Analysis available at http://www2.imm.dtu.dk/~hrni/ppa/ppasup200 4.html

Lambda Calculus Lambda calculus is a formal system for expressing computation by way of variable binding and substitution

Syntax M ::= x (variable) λx.m (abstraction) MM (application) Nothing else! No numbers No arithmetic operations No loops No etc. Symbolic computation

Syntax reminder anonymous functions λx.m function(x) { M } LM, e.g. λx.n y apply L to M L M

Terminology bound variables λx.m The binding operator λ binds the variable x in the λ-term x.m M is called the scope of x x is said to be a bound variable

Terminology free variables Free variables are all symbols that aren t bound (duh) FV(x) = {x} FV(MN) = FV(M) U FV(N) FV(x.M) = FV(M) x

Renaming of bound variables λx.m = λy.(m[y/x]) if y not in FV(M) i.e. you can replace x with y aka renaming α-conversion

Operational Semantics Evaluating function application: (λx.e 1 ) e 2 Replace every x in e 1 with e 2 Evaluate the resulting term Return the result of the evaluation Formally: β-reduction (aka substitution ) (λ x.e 1 ) e 2 β e 1 [e 2 /x] A term that can be β-reduced is a redex (reducible expression) We omit β when obvious

Note again Computation = pure symbolic manipulation Replace some symbols with other symbols

Scoping etc. Scope of λ extends as far to the right as possible λx.λy.xy is λx.(λy.(x y)) Function application is left-associative xyz means (xy)z

Multiple arguments λ(x,y).e??? Doesn t exist Solution: λx.λy.e [remember, (λx.(λy.e))] A function that takes x and returns another function that takes y and returns e (λx.λy.e) a b (λy.e[a/x]) b e[a/x][b/y] Currying after Curry: transformation of multi-arg functions into higher-order functions Multiple argument functions are nothing but syntactic sugar

Boolean Values and Conditionals True = λx.λy.x False = λx.λy.y If-then-else = λa.λb.λc. a b c

Boolean Values and Conditionals If True M N = (λa.λb.λc.abc) True M N If (λb.λc.true b c) M N (λc.true M c) N True M N = (λx.λy.x) M N (λy.m) N M

Numbers Numbers are counts of things, any things. Like function applications! 0 = λf. λx. x 1 = λf. λx. (f x) 2 = λf. λx. (f (f x)) 3 = λf. λx. (f (f (f x))) N = λf. λx. (f N x) Church numerals

Successor succ = λn. λf. λx. f (n f x) Want to try it on succ(1)? λn. λf. λx. f (n f x) (λf. λx. (f x)) λf. λx. f ((λf. λx. (f x)) f x) 1 λf. λx. f (f x) 2!

Closures Function with free variables that are bound to values in the enclosing environment (lambda (x) (lambda (y) x+y)) closure

Function Execution by Substitution plus x y = x + y 1. plus 2 3 2 + 3 5 2. plus (2*3) (plus 4 5) plus 6 (4+5) plus 6 9 6 + 9 15 (2*3) +(plus 4 5) 6 + (4+5) 6 + 9 15 The final answer did not depend upon the order in which reductions were performed

Blocks let in x = a * a y = b * b (x - y)/(x + y) a variable can have at most one definition in a block ordering of bindings does not matter

Layout Convention in Haskell This convention allows us to omit many delimiters let in x = a * a y = b * b (x - y)/(x + y) is the same as let in { x = a * a ; y = b * b ;} (x - y)/(x + y)

-renaming let y = 2 * 2 x = 3 + 4 z = let x = 5 * 5 w = x + y * x in w in x + y + z let y = 2 * 2 x = 3 + 4 z = let x = 5 * 5 w = x + y * x in w in x + y + z

Lexical Scoping let y = 2 * 2 x = 3 + 4 z = let x = 5 * 5 w = x + y * x in w in x + y + z Lexically closest definition of a variable prevails.

Dynamic Dispatch Problem

Example

A Simple Functional Language

Examples

0-CFA Analysis Abstract domains (i.e., maps) Specification of the analysis

Abstract Domains

Example

A More Complicated Example

Abstract Domains

Specification of 0-CFA

Clauses for 0-CFA (1)

Clauses for 0-CFA (2)

Clauses for 0-CFA (3)

Clauses for 0-CFA (4)

Constraint-based 0-CFA (1)

Constraint-based 0-CFA (2)

Constraint-based 0-CFA (3)

Constraint-based 0-CFA (4)

Solving the Constraints (1)

Solving the Constraints (2)

Example

Iteration Steps

K-CFA An abstract value in K-CFA is a calling context that records the last k dynamic call points (i.e., call sites) Contexts are sequences of labels of length at most k and they will be updated whenever a function application is analyzed

K-CFA for Imperative Languages A calling context is a sequence of call sites Compute a solution for a function under each such calling context Scalability is the biggest challenge