Definition
A partial order is a relation on a set that is reflexive (), antisymmetric ( and imply ) and transitive ( and imply ); two elements may be incomparable. The join is the least upper bound of and : the lowest element above both. The meet 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, and , and must continue with a single fact that is true whichever path ran. Any fact that allows both values will do, such as “ 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 as “ is below ”, meaning that allows every possibility that allows, so is at least as precise as . 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: and 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 and are incomparable, their join is the union , and their meet is the intersection , 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.
Order. Fix a set and a relation on it with the three properties of the definition; with is a partially ordered set. Elements and are comparable when or , and incomparable otherwise. An order in which every pair is comparable, such as on numbers, is a total order.
Bounds. An upper bound of and is any with and . A lower bound is any with and .
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.
Lattice. If every pair of elements has a join and a meet, is a lattice. If every subset of , 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), is a complete lattice. A complete lattice has a least element (“bottom”) and a greatest element (“top”).
Three lattices show the range of the idea.
Sets under inclusion. The subsets of a set , ordered by . Join is union, meet is intersection, and .
Integer intervals. An interval stands for the integers from to . A bound may be or , and an extra element , below every interval, stands for the empty set. Intervals are ordered by inclusion of the integer sets they stand for, so for intervals that are not , exactly when and . Then , and , or when that lower bound exceeds that upper bound.
Divisibility. The positive integers, with meaning that divides . The numbers 4 and 6 are incomparable, since neither divides the other. Their join is their least common multiple, 12, and their meet is their greatest common divisor, 2. The join is not the larger number; only in a total order is the join of two elements the larger one.
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 succeeds, the summary becomes .
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 , two values, and takes the values and . The divisor is never 0.
With intervals, the two paths give and . Their join is , which stands for the three integers 0, 1 and 2. Subtracting 1 from both bounds gives 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 and gives and adds two such values, 3 and 4. Joining and gives and adds none, because the union has no gap. The meet of two intervals never adds values: , and the integers common to both intervals are exactly 3, 4 and 5.
Stated precisely
Let be a set and a relation on . The relation is a partial order when, for all in : (reflexivity); and imply (antisymmetry); and imply (transitivity). An element of is the join when , , and every with and satisfies . An element is the meet when , , and every with and satisfies . is a lattice when every pair of elements has a join and a meet, and a complete lattice when every subset of has a least upper bound and a greatest lower bound, defined in the same way with in place of the pair.
In a lattice, join and meet are each commutative (), associative () and idempotent (), and the same three laws hold for . The order can be recovered from either operation: exactly when , and exactly when .
For integer intervals, write for the set of integers that the interval stands for, with . Then The inclusion for join is an equality exactly when has no gap: when or is , when the two intervals share an integer, or when one ends at the integer just before the other begins. Otherwise also contains every integer in the gap. The integer intervals with form a complete lattice.
Limits and common mistakes
Conventions differ. This book writes for “ is at least as precise as ”, so the merge where paths meet is a join and 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 , , and by inclusion. Both larger sets are upper bounds of and , but neither lies below the other, so and 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 “ is even” kept beside the interval, which together with leaves exactly ; or a path-sensitive analysis §3.11, which keeps the two paths apart and risks path explosion §3.26, since branches in sequence can give paths.
Some lattices have chains that never end. The intervals 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 and it is .
If one path gives and the other , the exact set of values is , two values, but the join 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 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 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).