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

constraint based analysis
SMART_READER_LITE
LIVE PREVIEW

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

Constraint-based Analysis Harry Xu CS 253/INF 212 Spring 2013 Acknowledgements Many slides in this file were taken from Prof. Crista Lopes slides on functional programming as well as slides provided by the authors of the book Principles of


slide-1
SLIDE 1

Constraint-based Analysis

Harry Xu CS 253/INF 212 Spring 2013

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Lambda Calculus

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

slide-4
SLIDE 4

Syntax

M ::= x (variable) | λx.M (abstraction) | MM (application) Nothing else!

– No numbers – No arithmetic operations – No loops – No etc.

Symbolic computation

slide-5
SLIDE 5

Syntax reminder

λx.M  function(x) { M } LM, e.g. λx.N y  apply L to M

L M anonymous functions

slide-6
SLIDE 6

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
slide-7
SLIDE 7

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

slide-8
SLIDE 8

Renaming of bound variables

λx.M = λy.(M[y/x]) if y not in FV(M)

α-conversion

i.e. you can replace x with y aka “renaming”

slide-9
SLIDE 9

Operational Semantics

  • Evaluating function application: (λx.e1) e2

– Replace every x in e1 with e2 – Evaluate the resulting term – Return the result of the evaluation

  • Formally: “β-reduction” (aka “substitution”)

– (λ x.e1) e2 →β e1[e2/x] – A term that can be β-reduced is a redex (reducible expression) – We omit β when obvious

slide-10
SLIDE 10

Note again

  • Computation = pure symbolic manipulation

– Replace some symbols with other symbols

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

Boolean Values and Conditionals

  • True = λx.λy.x
  • False = λx.λy.y
  • If-then-else = λa.λb.λc. a b c
slide-14
SLIDE 14

Boolean Values and Conditionals

  • If True M N = (λa.λb.λc.abc) True M N

 (λb.λc.True b c) M N  (λc.True M c) N  True M N = (λx.λy.x) M N  (λy.M) N  M

If

slide-15
SLIDE 15

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. (fN x)

Church numerals

slide-16
SLIDE 16

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)  λf. λx. f (f x)

1 2 !

slide-17
SLIDE 17

Closures

  • Function with free variables that are bound to

values in the enclosing environment

(lambda (x) (lambda (y) x+y)) closure

slide-18
SLIDE 18

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

slide-19
SLIDE 19

Blocks

let x = a * a y = b * b in (x - y)/(x + y)

  • a variable can have at most one definition

in a block

  • ordering of bindings does not matter
slide-20
SLIDE 20

Layout Convention in Haskell

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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.

slide-23
SLIDE 23

Dynamic Dispatch Problem

slide-24
SLIDE 24

Example

slide-25
SLIDE 25

A Simple Functional Language

slide-26
SLIDE 26

Examples

slide-27
SLIDE 27

0-CFA Analysis

  • Abstract domains (i.e., maps)
  • Specification of the analysis
slide-28
SLIDE 28

Abstract Domains

slide-29
SLIDE 29

Example

slide-30
SLIDE 30

A More Complicated Example

slide-31
SLIDE 31

Abstract Domains

slide-32
SLIDE 32

Specification of 0-CFA

slide-33
SLIDE 33

Clauses for 0-CFA (1)

slide-34
SLIDE 34

Clauses for 0-CFA (2)

slide-35
SLIDE 35

Clauses for 0-CFA (3)

slide-36
SLIDE 36

Clauses for 0-CFA (4)

slide-37
SLIDE 37

Constraint-based 0-CFA (1)

slide-38
SLIDE 38

Constraint-based 0-CFA (2)

slide-39
SLIDE 39

Constraint-based 0-CFA (3)

slide-40
SLIDE 40

Constraint-based 0-CFA (4)

slide-41
SLIDE 41

Solving the Constraints (1)

slide-42
SLIDE 42

Solving the Constraints (2)

slide-43
SLIDE 43

Example

slide-44
SLIDE 44

Iteration Steps

slide-45
SLIDE 45

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

slide-46
SLIDE 46

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