constraint based analysis
play

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


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

  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

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

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

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

  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

  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

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

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

  10. Note again • Computation = pure symbolic manipulation – Replace some symbols with other symbols

  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

  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

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

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

  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))) Church numerals – … – N = λf . λx . (f N x)

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

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

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

  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

  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)

  21.  -renaming let let y = 2 * 2 y = 2 * 2 x = 3 + 4 x = 3 + 4 z = let z = let x = 5 * 5 x ’ = 5 * 5  w = x + y * x w = x’ + y * x’ in in w w in in x + y + z x + y + z

  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.

  23. Dynamic Dispatch Problem

  24. Example

  25. A Simple Functional Language

  26. Examples

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

  28. Abstract Domains

  29. Example

  30. A More Complicated Example

  31. Abstract Domains

  32. Specification of 0-CFA

  33. Clauses for 0-CFA (1)

  34. Clauses for 0-CFA (2)

  35. Clauses for 0-CFA (3)

  36. Clauses for 0-CFA (4)

  37. Constraint-based 0-CFA (1)

  38. Constraint-based 0-CFA (2)

  39. Constraint-based 0-CFA (3)

  40. Constraint-based 0-CFA (4)

  41. Solving the Constraints (1)

  42. Solving the Constraints (2)

  43. Example

  44. Iteration Steps

  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

  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

Recommend


More recommend