Definition
Widening and narrowing are two operators used in abstract interpretation §3.4, a method that summarizes all values the variables can hold at each program point, such as “ is between 0 and 100”. Widening, written , replaces a loop summary that is still growing by a larger one, for example by moving a rising bound to infinity, so that the computation stops. Narrowing, written , then shrinks the result toward the exact summary while still covering every possible value. Widening trades precision for termination; narrowing recovers part of it.
The idea
Interval analysis §3.9, which summarizes each variable by a range of possible values, shows the problem. Take the loop x = 0; while (x < n) x = x + 1;, where n is an input. The analysis computes the range of x at the loop head, the test that runs before every iteration, by going round the loop until the range stops changing. Round 1 gives , round 2 gives , round 3 gives : each round admits one more iteration. Because n can be any value, no round is the last one, and the analysis never finishes. With a constant bound of one million it would finish, after a million rounds.
Widening breaks the chain by extrapolating. When a bound moves between two rounds, the analysis assumes that the bound will keep moving and sets it to infinity at once: becomes , meaning “0 or more”. The jump cannot miss a real value, because every value x can take lies in ; what it loses is precision. Narrowing then runs the loop once more, starting from the widened range. If something in the loop caps the variable, such as a test x < 100, that extra round produces a finite bound again, and narrowing keeps it.
How it works
The parts, in plain words:
Intervals. A summary allows the integers from to ; may be and may be . The empty summary (“bottom”) allows no value and means that the point has not been reached yet.
Order, join and meet. means that every value allows, also allows. The join is the smallest interval covering both, so . The meet keeps the values both allow, so .
Transfer function (read “F sharp”): one round of the loop on summaries. It maps the summary at the loop head to the values that can reach the head: the entry values joined with the values that one more iteration produces.
Widening points: a set of program points, usually the loop heads, through which every cycle of the control-flow graph §3.13 (the map of which instruction can follow which) passes. Stopping the growth there stops it everywhere.
The analysis runs in two phases.
Ascending phase. Start from and compute . The interval widening keeps each bound that did not move and sends each bound that moved outwards to infinity: and . Stop as soon as , meaning that one more round adds nothing, and call the result .
Descending phase. Start from and compute until nothing changes. The interval narrowing replaces only infinite bounds, by the newly computed ones: Each infinite bound is replaced at most once, so this phase ends quickly.
Use. Check the property against the final summaries, for example that an array §14.5 index stays in range.
Two common variants keep important constants. Widening with thresholds sends a moving bound to the next value in a fixed list, often the constants that appear in the program, and to infinity only beyond the last one. Delayed widening uses the plain join for the first few rounds and widens only after them.
Worked example
Take x = 0; while (x < 100) x = x + 1; over mathematical integers. For integers, means , so one round is where is the summary at the loop head and adds 1 to both bounds. Without widening, the head summary grows , and the 102nd evaluation of returns again.
With widening and narrowing:
, so .
. The upper bound moved from 0 to 1, so .
. This lies inside , so the ascending phase stops with .
Narrowing: , because the upper bound was infinite.
, and : no change, so the analysis stops.
Four evaluations of replace 102, and the result is exact: at the head, in the body, and after the loop, where only values with leave.
Without the descending phase the head summary stays , and the summary after the loop is instead of ; the body keeps , because the test caps x on every pass. A write to buf[x] after the loop, into a buffer of 101 bytes, can then no longer be proved in bounds: the analysis raises a false alarm §1.22.
Narrowing does not always succeed. Change the test to x != 100. An interval cannot leave out a value in its middle, so the test removes 100 only when 100 is an endpoint. Widening again gives , and : stable, and narrowing finds no finite bound to use. The body summary cannot show that a[x] stays inside a 100-element array. Plain iteration would have reached , and so does widening with the threshold 100: , and , stable at once, with in the body.
Stated precisely
Let be the abstract domain §3.6, the set of summaries, ordered by , with least element ; write for . Let be monotone ( implies ) and sound for one round of the loop, meaning that its result covers every value a real round can produce, where a round includes the entry values, as in the example. A widening is an operator with two properties:
covering: and for all ;
stabilization: for every increasing sequence , the sequence , is constant after finitely many steps.
The of the ascending phase increase by covering, so the increase by monotonicity, and stabilization makes the constant; then covering gives . The result is therefore a post-fixed point, , and the soundness §3.7 argument of abstract interpretation shows that it covers every reachable state §3.21.
A narrowing is an operator such that
if , then ;
for every decreasing sequence , the sequence , is constant after finitely many steps.
Every of the descending phase is again a post-fixed point: if , the first property gives , and monotonicity gives . Narrowing therefore never loses soundness, and each step is at least as precise as the one before.
Neither phase promises the least fixed point §3.22, the most precise summary that one more round leaves unchanged. When is a complete lattice §19.2.6 (every set of summaries has a smallest summary covering all of them) and is monotone, the least fixed point lies below every post-fixed point (the Knaster–Tarski theorem). The result of widening and narrowing is sound but can be less precise than the least fixed point.
Limits and common mistakes
Widening forgets every bound that moves during the ascending phase, including bounds the program respects. Narrowing restores a bound only if one round from the widened summary produces it, typically because a test or an assignment caps the variable with a constant. It fails when the cap is something the domain cannot express: the test x != 100 above, or a test x < n against another variable, since an interval cannot state . Keeping that bound needs a relational abstract domain §15.12, which records constraints between variables.
Widening is not monotone, and its result depends on the order of evaluation: , while the larger first argument gives . Two analyzers with the same domain can report different invariants for the same loop.
Plain descending iteration may not stop. Intervals contain infinite decreasing sequences such as , so applying downwards until nothing changes can run forever. A narrowing operator, or a fixed limit on descending rounds, ends it; stopping after any number of descending rounds is sound, because each one yields a post-fixed point.
Stopping the ascending phase early is unsound. Only a summary with covers every reachable state; a summary taken when a time budget runs out may miss values. As long as the ascending phase reaches its stopping condition, a loss of precision causes false alarms, never missed errors.
For machine integers the infinite bound stands for the largest value of the type. Once the bound is lost, the analysis cannot exclude that x + 1 wraps around to the smallest value.
Questions answered
Which useful invariant is lost when a bound widens to infinity?
The finite bound itself is lost, together with every fact that depends on it. For x = 0; while (x < 100) x = x + 1;, interval analysis (which summarizes each variable by a range of values) widens the summary at the loop head: it jumps from to so that the analysis terminates, and the invariant becomes . After the loop the analysis then knows only instead of . It can no longer prove that buf[x] stays inside a 101-byte buffer, that x + 1 does not overflow, or that a size computed from x fits. Facts inside the loop body survive when the loop test caps the variable again, here .
Narrowing, a second phase that reruns the loop from the widened summary and keeps any finite bound that appears, recovers in this loop. It cannot recover a bound that the domain cannot express: with the test x != 100 the interval stays , and a bound relative to another variable, such as , needs a relational abstract domain. Widening with thresholds, which jumps to a program constant such as 100 before trying infinity, keeps constant bounds of this kind. The loss never makes the result unsound; it causes false alarms.
Related entries: Fixed point (§3.22), Loop invariant (§3.24), Abstract interpretation (§3.4), Interval analysis (§3.9), Partial order, lattice, join, and meet (§19.2.6), Inductive safety closure and widening termination (§19.2.8), Relational abstract domain (§15.12). Further reading: [68, 69].