Section 19.2.6

Partial order, lattice, join, and meet

Definition

A partial order is a relation ⊑\sqsubseteq on a set that is reflexive (a⊑aa \sqsubseteq a), antisymmetric (a⊑ba \sqsubseteq b and b⊑ab \sqsubseteq a imply a=ba = b) and transitive (a⊑ba \sqsubseteq b and b⊑cb \sqsubseteq c imply a⊑ca \sqsubseteq c); two elements may be incomparable. The join a⊔ba \sqcup b is the least upper bound of aa and bb: the lowest element above both. The meet a⊓ba \sqcap b is their greatest lower bound: the highest element below both. A lattice is a set with a partial order in which every two elements have a join and a meet.

The idea

A static analysis §3.1 meets one problem wherever two paths of a program come together. After if (c) x = 0; else x = 2; the analysis holds one fact for each path, x=0x = 0 and x=2x = 2, and must continue with a single fact that is true whichever path ran. Any fact that allows both values will do, such as “xx is an integer”, but a weaker fact proves less. The analysis wants the most precise fact that covers both.

A partial order makes “covers” exact. Read a⊑ba \sqsubseteq b as “aa is below bb”, meaning that bb allows every possibility that aa allows, so aa is at least as precise as bb. The facts that cover both inputs are the upper bounds, and the most precise of them is the join. The order is partial because some facts are not comparable: x=0x = 0 and x=2x = 2 each allow a value that the other excludes, so neither is below the other. Meet is the mirror image. When two facts hold at once, such as a summary and the condition of a branch just taken, the meet is the largest fact below both; for sets and for intervals it keeps exactly the possibilities that both allow. A lattice is an ordered set in which both operations always exist, so an analysis never meets a pair of facts that it cannot merge or combine.

Sets ordered by inclusion are the model to keep in mind. The sets {0}\{0\} and {2}\{2\} are incomparable, their join is the union {0,2}\{0, 2\}, and their meet is the intersection ∅\emptyset, the empty set. Intervals behave like sets that are forced to have no gaps, and that constraint is where information is lost.

How it works

The parts are defined in this order.

  1. Order. Fix a set LL and a relation ⊑\sqsubseteq on it with the three properties of the definition; LL with ⊑\sqsubseteq is a partially ordered set. Elements aa and bb are comparable when a⊑ba \sqsubseteq b or b⊑ab \sqsubseteq a, and incomparable otherwise. An order in which every pair is comparable, such as ≤\le on numbers, is a total order.

  2. Bounds. An upper bound of aa and bb is any uu with a⊑ua \sqsubseteq u and b⊑ub \sqsubseteq u. A lower bound is any ll with l⊑al \sqsubseteq a and l⊑bl \sqsubseteq b.

  3. Join and meet. The join is the upper bound that lies below every other upper bound. The meet is the lower bound that lies above every other lower bound. Either may fail to exist. When one exists it is unique: two candidates would each lie below the other, and antisymmetry makes them equal.

  4. Lattice. If every pair of elements has a join and a meet, LL is a lattice. If every subset of LL, infinite subsets and the empty subset included, has a least upper bound (an element above every member that lies below every other such element) and a greatest lower bound (defined the same way with below and above swapped), LL is a complete lattice. A complete lattice has a least element ⊥\bot (“bottom”) and a greatest element ⊤\top (“top”).

Three lattices show the range of the idea.

A static analysis uses all three parts. The order compares results: when a newly computed summary lies below the previous one, the round has added no new possibility, which is how an analysis of a loop detects that it has reached a fixed point §3.22. Join merges the summaries of paths that meet. Meet applies what a branch condition reveals: after the test x<5x < 5 succeeds, the summary [0,10][0, 10] becomes [0,10]⊓[−∞,4]=[0,4][0, 10] \sqcap [-\infty, 4] = [0, 4].

Worked example

Take the program if (c) x = 0; else x = 2; y = 10 / (x - 1);, where c is any condition. The real values of x at the division are 0 and 2. With sets as summaries, the join is {0}∪{2}={0,2}\{0\} \cup \{2\} = \{0, 2\}, two values, and x−1x - 1 takes the values 0−1=−10 - 1 = -1 and 2−1=12 - 1 = 1. The divisor is never 0.

With intervals, the two paths give [0,0][0, 0] and [2,2][2, 2]. Their join is [min(0,2),max(0,2)]=[0,2][\min(0, 2), \max(0, 2)] = [0, 2], which stands for the three integers 0, 1 and 2. Subtracting 1 from both bounds gives [−1,1][-1, 1] for the divisor, an interval that contains 0. The analysis therefore reports a possible division by zero. The report is a false alarm §1.22: the value 1 for x, and so the divisor 0, comes from the join, not from any run.

The join kept both real values and added one value that no path produced. Joining [1,2][1, 2] and [5,6][5, 6] gives [1,6][1, 6] and adds two such values, 3 and 4. Joining [0,1][0, 1] and [2,3][2, 3] gives [0,3][0, 3] and adds none, because the union {0,1,2,3}\{0, 1, 2, 3\} has no gap. The meet of two intervals never adds values: [0,5]⊓[3,9]=[max(0,3),min(5,9)]=[3,5][0, 5] \sqcap [3, 9] = [\max(0, 3), \min(5, 9)] = [3, 5], and the integers common to both intervals are exactly 3, 4 and 5.

Stated precisely

Let LL be a set and ⊑\sqsubseteq a relation on LL. The relation is a partial order when, for all a,b,ca, b, c in LL: a⊑aa \sqsubseteq a (reflexivity); a⊑ba \sqsubseteq b and b⊑ab \sqsubseteq a imply a=ba = b (antisymmetry); a⊑ba \sqsubseteq b and b⊑cb \sqsubseteq c imply a⊑ca \sqsubseteq c (transitivity). An element jj of LL is the join a⊔ba \sqcup b when a⊑ja \sqsubseteq j, b⊑jb \sqsubseteq j, and every uu with a⊑ua \sqsubseteq u and b⊑ub \sqsubseteq u satisfies j⊑uj \sqsubseteq u. An element mm is the meet a⊓ba \sqcap b when m⊑am \sqsubseteq a, m⊑bm \sqsubseteq b, and every ll with l⊑al \sqsubseteq a and l⊑bl \sqsubseteq b satisfies l⊑ml \sqsubseteq m. LL is a lattice when every pair of elements has a join and a meet, and a complete lattice when every subset SS of LL has a least upper bound and a greatest lower bound, defined in the same way with SS in place of the pair.

In a lattice, join and meet are each commutative (a⊔b=b⊔aa \sqcup b = b \sqcup a), associative ((a⊔b)⊔c=a⊔(b⊔c)(a \sqcup b) \sqcup c = a \sqcup (b \sqcup c)) and idempotent (a⊔a=aa \sqcup a = a), and the same three laws hold for ⊓\sqcap. The order can be recovered from either operation: a⊑ba \sqsubseteq b exactly when a⊔b=ba \sqcup b = b, and exactly when a⊓b=aa \sqcap b = a.

For integer intervals, write γ(a)\gamma(a) for the set of integers that the interval aa stands for, with γ(⊥)=∅\gamma(\bot) = \emptyset. Then γ(a⊔b)⊇γ(a)∪γ(b),γ(a⊓b)=γ(a)∩γ(b).\gamma(a \sqcup b) \supseteq \gamma(a) \cup \gamma(b), \qquad \gamma(a \sqcap b) = \gamma(a) \cap \gamma(b). The inclusion for join is an equality exactly when γ(a)∪γ(b)\gamma(a) \cup \gamma(b) has no gap: when aa or bb is ⊥\bot, when the two intervals share an integer, or when one ends at the integer just before the other begins. Otherwise γ(a⊔b)\gamma(a \sqcup b) also contains every integer in the gap. The integer intervals with ⊥\bot form a complete lattice.

Limits and common mistakes

Conventions differ. This book writes a⊑ba \sqsubseteq b for “aa is at least as precise as bb”, so the merge where paths meet is a join and ⊥\bot stands for no state at all. Many texts on data-flow analysis §3.15 order facts the other way and merge with a meet. Check which convention a text uses before reading “join” or “top”.

A partial order need not have joins. Order the four sets {1}\{1\}, {2}\{2\}, {1,2,3}\{1, 2, 3\} and {1,2,4}\{1, 2, 4\} by inclusion. Both larger sets are upper bounds of {1}\{1\} and {2}\{2\}, but neither lies below the other, so {1}\{1\} and {2}\{2\} have no least upper bound in this collection. An analysis whose facts were ordered this way would have no single best merge.

The loss from a join is precision, not soundness §3.7. Every value of either input survives, so an analysis that computes an overapproximation §19.2.5 of the real values still does after a join. The cost is false alarms, as in the example. A more expressive representation can keep the gap, at a price in memory and time: a set of intervals instead of one; a second fact such as “xx is even” kept beside the interval, which together with [0,2][0, 2] leaves exactly {0,2}\{0, 2\}; or a path-sensitive analysis §3.11, which keeps the two paths apart and risks path explosion §3.26, since nn branches in sequence can give 2n2^n paths.

Some lattices have chains that never end. The intervals [0,0]⊑[0,1]⊑[0,2]⊑⋯[0, 0] \sqsubseteq [0, 1] \sqsubseteq [0, 2] \sqsubseteq \cdots keep growing, so repeated joins around a loop need not stop. A widening operator §3.23 forces them to stop.

Questions answered

Why does interval join lose information?

Interval join loses information because an interval can describe only a range of integers without gaps, so the smallest interval that covers two intervals must also include every integer lying between them, including integers that neither input contained. The join of two summaries is the most precise summary that covers both; for intervals [a,b][a, b] and [c,d][c, d] it is [min(a,c),max(b,d)][\min(a, c), \max(b, d)].

If one path gives x=0x = 0 and the other x=2x = 2, the exact set of values is {0,2}\{0, 2\}, two values, but the join [0,2][0, 2] stands for three values, 0, 1 and 2. The value 1 came from no path. Every real value is kept, so the result is still sound, but the extra value can cause false alarms: an analysis of 10/(x−1)10 / (x - 1) would now report a possible division by zero. No loss occurs when the two intervals overlap or touch, because then their union has no gap. Keeping the gap needs a more expressive representation, such as a set of intervals or a separate fact that xx is even, at a cost in memory and time.

Related entries: Abstract domain (§3.6), Interval analysis (§3.9), Set inclusion and policy order (§15.40), Fixed point (§3.22), Widening and narrowing (§3.23).