Section 3.23

Widening and narrowing

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 “xx is between 0 and 100”. Widening, written ∇\nabla, 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 Δ\Delta, 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 [l,u][l, u] 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 [0,0][0, 0], round 2 gives [0,1][0, 1], round 3 gives [0,2][0, 2]: 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: [0,1][0, 1] becomes [0,+∞][0, +\infty], meaning “0 or more”. The jump cannot miss a real value, because every value x can take lies in [0,+∞][0, +\infty]; 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:

The analysis runs in two phases.

  1. Ascending phase. Start from X0=⊥X_0 = \bot and compute Xi+1=Xi∇F#(Xi)X_{i+1} = X_i \mathbin{\nabla} F^{\#}(X_i). The interval widening keeps each bound that did not move and sends each bound that moved outwards to infinity: [a,b]∇[c,d]=[l,u],l={aif c≥a−∞if c<a,u={bif d≤b+∞if d>b[a, b] \mathbin{\nabla} [c, d] = [l, u], \quad l = \begin{cases} a & \text{if } c \ge a \\ -\infty & \text{if } c < a \end{cases}, \quad u = \begin{cases} b & \text{if } d \le b \\ +\infty & \text{if } d > b \end{cases} and ⊥∇y=y\bot \mathbin{\nabla} y = y. Stop as soon as F#(Xi)⊑XiF^{\#}(X_i) \sqsubseteq X_i, meaning that one more round adds nothing, and call the result X*X^{*}.

  2. Descending phase. Start from Y0=X*Y_0 = X^{*} and compute Yi+1=YiΔF#(Yi)Y_{i+1} = Y_i \mathbin{\Delta} F^{\#}(Y_i) until nothing changes. The interval narrowing replaces only infinite bounds, by the newly computed ones: [a,b]Δ[c,d]=[l,u],l={cif a=−∞aotherwise,u={dif b=+∞botherwise[a, b] \mathbin{\Delta} [c, d] = [l, u], \quad l = \begin{cases} c & \text{if } a = -\infty \\ a & \text{otherwise} \end{cases}, \quad u = \begin{cases} d & \text{if } b = +\infty \\ b & \text{otherwise} \end{cases} Each infinite bound is replaced at most once, so this phase ends quickly.

  3. 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, x<100x < 100 means x≤99x \le 99, so one round is F#(H)=[0,0]⊔((H⊓[−∞,99])+1),F^{\#}(H) = [0, 0] \sqcup \big((H \sqcap [-\infty, 99]) + 1\big), where HH is the summary at the loop head and +1+1 adds 1 to both bounds. Without widening, the head summary grows ⊥,[0,0],[0,1],…,[0,100]\bot, [0, 0], [0, 1], \dots, [0, 100], and the 102nd evaluation of F#F^{\#} returns [0,100][0, 100] again.

With widening and narrowing:

  1. F#(⊥)=[0,0]F^{\#}(\bot) = [0, 0], so X1=⊥∇[0,0]=[0,0]X_1 = \bot \mathbin{\nabla} [0, 0] = [0, 0].

  2. F#([0,0])=[0,0]⊔[1,1]=[0,1]F^{\#}([0, 0]) = [0, 0] \sqcup [1, 1] = [0, 1]. The upper bound moved from 0 to 1, so X2=[0,0]∇[0,1]=[0,+∞]X_2 = [0, 0] \mathbin{\nabla} [0, 1] = [0, +\infty].

  3. F#([0,+∞])=[0,0]⊔([0,99]+1)=[0,0]⊔[1,100]=[0,100]F^{\#}([0, +\infty]) = [0, 0] \sqcup ([0, 99] + 1) = [0, 0] \sqcup [1, 100] = [0, 100]. This lies inside [0,+∞][0, +\infty], so the ascending phase stops with X*=[0,+∞]X^{*} = [0, +\infty].

  4. Narrowing: Y1=[0,+∞]Δ[0,100]=[0,100]Y_1 = [0, +\infty] \mathbin{\Delta} [0, 100] = [0, 100], because the upper bound was infinite.

  5. F#([0,100])=[0,0]⊔([0,99]+1)=[0,100]F^{\#}([0, 100]) = [0, 0] \sqcup ([0, 99] + 1) = [0, 100], and [0,100]Δ[0,100]=[0,100][0, 100] \mathbin{\Delta} [0, 100] = [0, 100]: no change, so the analysis stops.

Four evaluations of F#F^{\#} replace 102, and the result is exact: 0≤x≤1000 \le x \le 100 at the head, 0≤x≤990 \le x \le 99 in the body, and x=100x = 100 after the loop, where only values with x≥100x \ge 100 leave.

Without the descending phase the head summary stays [0,+∞][0, +\infty], and the summary after the loop is [100,+∞][100, +\infty] instead of [100,100][100, 100]; the body keeps [0,99][0, 99], 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 [0,+∞][0, +\infty], and F#([0,+∞])=[0,0]⊔[1,+∞]=[0,+∞]F^{\#}([0, +\infty]) = [0, 0] \sqcup [1, +\infty] = [0, +\infty]: stable, and narrowing finds no finite bound to use. The body summary [0,+∞][0, +\infty] cannot show that a[x] stays inside a 100-element array. Plain iteration would have reached [0,100][0, 100], and so does widening with the threshold 100: [0,0]∇[0,1]=[0,100][0, 0] \mathbin{\nabla} [0, 1] = [0, 100], and F#([0,100])=[0,0]⊔([0,99]+1)=[0,100]F^{\#}([0, 100]) = [0, 0] \sqcup ([0, 99] + 1) = [0, 100], stable at once, with [0,99][0, 99] in the body.

Stated precisely

Let AA be the abstract domain §3.6, the set of summaries, ordered by ⊑\sqsubseteq, with least element ⊥\bot; write b⊒ab \sqsupseteq a for a⊑ba \sqsubseteq b. Let F#:A→AF^{\#} : A \to A be monotone (a⊑ba \sqsubseteq b implies F#(a)⊑F#(b)F^{\#}(a) \sqsubseteq F^{\#}(b)) 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 ∇:A×A→A\nabla : A \times A \to A with two properties:

  1. covering: a⊑a∇ba \sqsubseteq a \mathbin{\nabla} b and b⊑a∇bb \sqsubseteq a \mathbin{\nabla} b for all a,ba, b;

  2. stabilization: for every increasing sequence b0⊑b1⊑…b_0 \sqsubseteq b_1 \sqsubseteq \dots, the sequence a0=b0a_0 = b_0, ai+1=ai∇bi+1a_{i+1} = a_i \mathbin{\nabla} b_{i+1} is constant after finitely many steps.

The XiX_i of the ascending phase increase by covering, so the F#(Xi)F^{\#}(X_i) increase by monotonicity, and stabilization makes the XiX_i constant; then covering gives F#(Xi)⊑Xi+1=XiF^{\#}(X_i) \sqsubseteq X_{i+1} = X_i. The result X*X^{*} is therefore a post-fixed point, F#(X*)⊑X*F^{\#}(X^{*}) \sqsubseteq X^{*}, and the soundness §3.7 argument of abstract interpretation shows that it covers every reachable state §3.21.

A narrowing is an operator Δ:A×A→A\Delta : A \times A \to A such that

  1. if b⊑ab \sqsubseteq a, then b⊑aΔb⊑ab \sqsubseteq a \mathbin{\Delta} b \sqsubseteq a;

  2. for every decreasing sequence b0⊒b1⊒…b_0 \sqsupseteq b_1 \sqsupseteq \dots, the sequence a0=b0a_0 = b_0, ai+1=aiΔbi+1a_{i+1} = a_i \mathbin{\Delta} b_{i+1} is constant after finitely many steps.

Every YiY_i of the descending phase is again a post-fixed point: if F#(Yi)⊑YiF^{\#}(Y_i) \sqsubseteq Y_i, the first property gives F#(Yi)⊑Yi+1⊑YiF^{\#}(Y_i) \sqsubseteq Y_{i+1} \sqsubseteq Y_i, and monotonicity gives F#(Yi+1)⊑F#(Yi)⊑Yi+1F^{\#}(Y_{i+1}) \sqsubseteq F^{\#}(Y_i) \sqsubseteq Y_{i+1}. 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 AA is a complete lattice §19.2.6 (every set of summaries has a smallest summary covering all of them) and F#F^{\#} 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 x≤nx \le n. 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: [0,1]∇[0,2]=[0,+∞][0, 1] \mathbin{\nabla} [0, 2] = [0, +\infty], while the larger first argument gives [0,2]∇[0,2]=[0,2][0, 2] \mathbin{\nabla} [0, 2] = [0, 2]. 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 [0,+∞]⊒[1,+∞]⊒[2,+∞]⊒…[0, +\infty] \sqsupseteq [1, +\infty] \sqsupseteq [2, +\infty] \sqsupseteq \dots, so applying F#F^{\#} 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 F#(X)⊑XF^{\#}(X) \sqsubseteq X 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 [0,1][0, 1] to [0,+∞][0, +\infty] so that the analysis terminates, and the invariant 0≤x≤1000 \le x \le 100 becomes 0≤x0 \le x. After the loop the analysis then knows only x≥100x \ge 100 instead of x=100x = 100. 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 0≤x≤990 \le x \le 99.

Narrowing, a second phase that reruns the loop from the widened summary and keeps any finite bound that appears, recovers 0≤x≤1000 \le x \le 100 in this loop. It cannot recover a bound that the domain cannot express: with the test x != 100 the interval stays [0,+∞][0, +\infty], and a bound relative to another variable, such as x≤nx \le n, 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].