Docsity

Constraint-Based Analysis: Dataflow and Type Equations - Prof. Michael W. Hicks, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC 631, Fall 2006 1
Constraint-Based Analysis
(Lecture slides from Alex Aiken, CS
294 lecture 4)
CMSC 631, Fall 2006 2
Type Inference Problems
• Type inference problems are described as:
󲰐i τi1 = τi2
τ = c(τ, . . ., τ) | α
• c is a constructor (may be 0-ary)
– Like function arrow, product, or ref
• System of equations
• Arbitrary expressions on lhs and rhs
• Domain is terms
CMSC 631, Fall 2006 3
Dataflow Problems
• Recall Gen/Kill data flow problems look like
– In(S) = ∪s in pred(S) Out(s)
– Out(S) = Gen(S) ∪ (In(S) - Kill(S))
• These can be though of as constraints
– In(S) and Out(S) are variables
– We don’t need =, since we’re really computing least
solutions
CMSC 631, Fall 2006 4
Dataflow Problems as Constraints
• So we can rewrite those equations as
– vIn(S) ⊇ ∪s in pred(S) vOut(s)
– vOut(S) ⊇ E1 ∪ (vIn(S) ∩ E2)
CMSC 631, Fall 2006 5
Dataflow Problems
• Classical dataflow equations are described as:
󲰐i vi ⊇ Ei
E = E∪E | E∩E | v | a
• v is a variable, a is an atom
• System of inclusion constraints
• Only variables on lhs
• Domain is atoms
CMSC 631, Fall 2006 6
Summary
• Dataflow analysis
– Inclusion constraints over atoms
• Type inference
– Equations over terms
• Two very different theories
– With different applications
– Developed over decades
• But are they really independent?
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Constraint-Based Analysis: Dataflow and Type Equations - Prof. Michael W. Hicks and more Study notes Computer Science in PDF only on Docsity!

CMSC 631, Fall 2006 1 Constraint-Based Analysis (Lecture slides from Alex Aiken, CS 294 lecture 4) CMSC 631, Fall 2006 2 Type Inference Problems

  • Type inference problems are described as:

i τi1 =^ τi

τ = c(τ,.. ., τ) | α

  • c is a constructor (may be 0-ary)
    • Like function arrow, product, or ref
  • System of equations
  • Arbitrary expressions on lhs and rhs
  • Domain is terms CMSC 631, Fall 2006 3 Dataflow Problems
  • Recall Gen/Kill data flow problems look like

– In(S) = ∪s in pred(S) Out(s)

  • Out(S) = Gen(S) ∪ (In(S) - Kill(S))
  • These can be though of as constraints
  • In(S) and Out(S) are variables
  • We don’t need =, since we’re really computing least solutions CMSC 631, Fall 2006 4 Dataflow Problems as Constraints
  • So we can rewrite those equations as

– vIn(S) ⊇ ∪s in pred(S) vOut(s)

  • vOut(S) ⊇ E1 ∪ (vIn(S) ∩ E2) CMSC 631, Fall 2006 5 Dataflow Problems
  • Classical dataflow equations are described as:

i vi ⊇^ Ei

E = E∪E | E∩E | v | a

  • v is a variable, a is an atom
  • System of inclusion constraints
  • Only variables on lhs
  • Domain is atoms CMSC 631, Fall 2006 6 Summary
    • Dataflow analysis
      • Inclusion constraints over atoms
    • Type inference
      • Equations over terms
    • Two very different theories
      • With different applications
      • Developed over decades
    • But are they really independent?

CMSC 631, Fall 2006 7 Set Constraints

  • The set expressions are: E ::= 0 | α | E ∪ E | E ∩ E | ¬E | c(E,…,E) | c-i(E)
  • A system of set constraints is i Ei1 ⊆^ Ei
  • Constructors c
  • Set variables α CMSC 631, Fall 2006 8 Semantics of Set Expressions E ::= 0 | α | E ∪ E | E ∩ E | ¬E | c(E,…,E) | c-i(E)
    • One interpretation: Set expressions denote subsets of the Herbrand Universe H - H 0 = Ø - Hi+1 = Hi ∪ { c(t 1 ,…,tn) | ti ∈ Hi, c ∈ C } - H is the least upper bound of the series H 0 ⊆ H 1 ⊆ …
    • Notice that c(…, Ø,…) can never be in H CMSC 631, Fall 2006 9 Semantics of Set Expressions (Cont.)
  • An assignment maps variables to sets of terms: σ: Vars → 2 H
  • Extend σ to all set expressions: σ(0) = Ø σ(E 1 ∪ E 2 ) = σ(E 1 ) ∪ σ(E 2 ) σ(E 1 ∩ E 2 ) = σ(E 1 ) ∩ σ(E 2 ) σ(¬E) = H - σ(E) σ(c(E 1 ,…,En)) = {c(t 1 ,…,tn) | ti ∈ σ(Ei)} σ(c-i(E)) = { ti | c(t 1 ,…,tn) ∈ σ(E) } CMSC 631, Fall 2006 10 Solutions
  • An assignment σ is a solution of the constraints if for all i, σ(Ei1) ⊆ σ(Ei2) CMSC 631, Fall 2006 11 Set Constraints
  • Set constraints generalize
    • Dataflow equations (add terms)
    • Type equations (add inclusion constraints)
    • And more (add projections) Dataflow Equations Type Equations Set Constraints CMSC 631, Fall 2006 12 Notes on Projection
  • Projection can model data selectors
  • Car, cdr, hd, tl, etc.
  • But projections have another interesting property:

CMSC 631, Fall 2006 19 Solving Set Constraints

  • The usual strategy:
    • Rewrite constraints, preserving solutions
    • When all possible rewrites have been done, the system is in “solved form” - Solutions are manifest
  • Note: there are different notions of “solve”
    • Has at least one solution (yes/no)
    • Describe one solution (e.g., the least)
    • Describe all solutions CMSC 631, Fall 2006 20 Resolution Rules 1
      • Trivial constraints: CMSC 631, Fall 2006 21 Resolution Rules 2 More interesting constraints: CMSC 631, Fall 2006 22 Resolution Rules 3
    • And more interesting constraints:
    • These rules preserve all solutions for non- strict constructors - c(…,0,…) ≠ 0
    • Warning: c can’t be the function constructor CMSC 631, Fall 2006 23 Resolution Rules 4
  • Note how the rules preserve R and L:
  • We can also have constructors with contravariant arguments; e.g., → L ::= … | R → L R ::= … | L → R CMSC 631, Fall 2006 24 An Observation
  • Note the resolution rules do not create new expressions
  • Only subexpressions are used, e.g.,

CMSC 631, Fall 2006 25 A Graph Interpretation

  • Treat each subexpression as a node in a graph
  • Constraints L ⊆ R are directed edges from L to R
  • Recast resolution rules as graph transformations CMSC 631, Fall 2006 26 Resolution on Graphs 1 c(…)^ α^ R CMSC 631, Fall 2006 27 Resolution on Graphs 2 c(…) α (^) L R CMSC 631, Fall 2006 28 Resolution on Graphs 3 L 1 c(L 1 ,L 2 ) L 2 R 1 c(R 1 ,R 2 ) R 2 CMSC 631, Fall 2006 29 The Other Constraints
  • Skip presentation of rules for other constraints - Trivial constraints - Intersection/union constraints
  • Easily handled
    • In practice, edges from these constraints are not explicitly represented anyway
    • Tend to keep only constraints on variables CMSC 631, Fall 2006 30 Notes
      • The process of adding edges according to a set of rules is called closing the graph
      • The closed graph gives the solution of the constraints

CMSC 631, Fall 2006 37 A Definition

  • Assume all bound variables are unique
    • So a bound variable uniquely identifies a function
    • Can be done by renaming variables
  • For each application e 1 e 2 , what is the set of lambda terms L(e 1 ) to which e 1 may evaluate? - L(…) is a set of static, or syntactic, lambdas - L(…) defines a call graph - The set of functions that may be called by an application CMSC 631, Fall 2006 38 A More General Definition - To compute L(…) for applications, we will need to compute it for every expression - Define: L(e) is the set of syntactic lambda abstractions to which e may evaluate - The problem is to compute L(e) for every expression e CMSC 631, Fall 2006 39 Defining L(…) λx.e L(λx.e) = λx.e e 1 e 2 for each λx.e in L(e 1 ) L(e 2 ) ⊆ L(x) L(e) ⊆ L(e 1 e 2 ) The value of the application includes the value of the function body The actual argument of the call flows to the formal argument CMSC 631, Fall 2006 40 Rephrasing the Constraints with ⊆ The following constraints have the same least solution as the original constraints: λx.e λx.e ⊆ L(λx.e) e 1 e 2 λx.e 0 ⊆ L(e 1 ) ⇒ (L(e 2 ) ⊆ L(x) L(e 0 ) ⊆ L(e 1 e 2 )) Note: Each L(e) is a constraint variable Each λx.e is a constant CMSC 631, Fall 2006 41 Example ((λx.x) (λy.y)) (λz.z) λx.x ⊆ L(λx.x) λy.y ⊆ L(λy.y) λz.z ⊆ L(λz.z) L(λy.y) ⊆ L(x) L(x) ⊆ L((λx.x) (λy.y)) L(λz.z) ⊆ L(y) L(y) ⊆ L(((λx.x) (λy.y)) (λz.z)) Least solution: L(λx.x) = λx.x L(λy.y) = λy.y L(λz.z) = λz.z L(λy.y) = L(x) = L((λx.x) (λy.y)) L(λz.z) = L(y) = L(((λx.x) (λy.y)) (λz.z)) CMSC 631, Fall 2006 42 The Example ((λx.x) (λy.y)) (λz.z) with Graphs z y λz.z x λx.x λy.y (λx.x) (λy.y) ((λx.x) (λy.y)) (λz.z) λx.x λy.y λz.z

CMSC 631, Fall 2006 43 The Solution for ((λx.x) (λy.y)) (λz.z) z y λz.z x λx.x λy.y (λx.x) (λy.y) ((λx.x) (λy.y)) (λz.z) λx.x λy.y λz.z The solution is given by edges ( λ x.e,*) CMSC 631, Fall 2006 44 Control Flow Graphs in OO Languages

  • Consider a method call e 0 .f(e 1 ,…,en)
  • To build a control-flow graph, we need to know which f methods may be called - Depends on the class of e 0 at runtime
  • The problem:
    • For each expression, estimate the set of classes it could evaluate to at run time CMSC 631, Fall 2006 45 An OO Language P ::= C 1... Cn E C ::= class ClassId [inherits ClassId] var Id 1... Idk M 1... Mn M ::= method MId(Id) E E ::= Id := E | E.MId(E,…,E) | E;E | new ClassId | if E E E CMSC 631, Fall 2006 46 Constraints id := e C(e) ⊆ C(id) C(e) ⊆ C(id := e) e 1 ; e 2 C(e 2 ) ⊆ C(e 1 ; e 2 ) new A { A } ⊆ C(new A) if e 1 e 2 e 3 C(e 2 ) ⊆ C(if e 1 e 2 e 3 ) C(e 3 ) ⊆ C(if e 1 e 2 e 3 ) e 0 .f(e 1 ) for each class A with a method f(x) e A in C(e 0 ) ⇒ C(e 1 ) ⊆ C(x) & C(e) ⊆ C(e 0 .f(e 1 )) CMSC 631, Fall 2006 47 Notes
  • Receiver class analysis of OO languages and control flow analysis of functional languages are the same problem
  • Receiver class analysis is important in practice
  • Heavily object-oriented code pays a high price for the indirection in method calls
  • If we can show that only one method can be called, the function can be statically bound
  • Or even inlined and optimized CMSC 631, Fall 2006 48 Type Safety
  • Notice that our OO language is untyped
  • We can run (new A).f(0) even if A has no f method
  • Gives a runtime error
  • By adding upper bounds to the constraints, we can make receiver class analysis into a type inference procedure for our language

CMSC 631, Fall 2006 55 Things We Didn’t Talk About

  • Polymorphism
    • Context-free reachability & polymorphic recursion
  • Effect Systems
    • A computation has a type & an effect
    • E.g., the set of memory locations written
    • Mixed constraint systems
  • Other constraint languages
    • There are some besides = and ⊆