Which recursive typings suffice on a union of intervals?

A recursive call returns the same number at every admitted value of its erased type parameter \(k\). Each typing can therefore rule out a different hypothetical result. The previous example needed keys from two separated intervals. InvarGenT now checks it on Table 4.4 pass 2 and the compiled program prints zero. The change also exposes a limit. Even when the admitted keys form a single interval at every input, a flag can change its upper endpoint in a way that defeats every finite family of affine min/max expressions. Two-interval program · Flag-controlled program · Proofs

Testing an entire interval with one typing

Fix the observed inputs and a hypothetical payload \(r\). Suppose the admitted keys are a union of \(m\) nonempty closed integer intervals \(I_j\). Suppose the package accepts that payload at \(k\) exactly when a key-independent condition \(H(r)\) holds and \(k\) belongs to none of \(p\) forbidden intervals \(B_i(r)\). Use the projected package relation here, so a private witness can have a different value at each typing. Discard empty forbidden intervals and take any representative \(t_i\) inside each remaining one.

For each pair \((j,i)\), type the call at the projection of \(t_i\) onto \(I_j=[a_j,b_j]\),

\[q_{ji}=\min(b_j,\max(a_j,t_i)).\]

These \(mp\) probes suffice to detect every forbidden intersection. If \(k\in I_j\cap B_i(r)\), then \(q_{ji}\) lies between \(k\) and \(t_i\). Both belong to \(B_i(r)\), and the forbidden interval contains every integer between them. Conversely, if \(q_{ji}\in B_i(r)\), this probe is itself a member of the intersection. Thus, on an inhabited domain, the intersection of all admitted typings is exactly \(H(r)\) together with \(q_{ji}\notin B_i(r)\) for every pair. For \(p=0\), one ordinary opening supplies \(H(r)\). The proof allows overlapping components, singletons and unbounded forbidden intervals. The count bounds the additional typings for the supplied decomposition. Finding the decomposition and certifying its probes take separate work. Projection and exact intersection

For point holes there is a converse on any key set \(D\). Suppose each proposed map \(q_i(x)\) is admitted. Testing these maps detects every singleton hole \(\{x\}\) exactly if, for every \(x\in D\), some \(q_i(x)=x\). Necessity follows by choosing that very singleton. For sufficiency, a forbidden admitted point is hit by its covering map. On a union of intervals, clipping to each component gives this coverage.

A recursive program with two legal projections

For \(n\ge2\) the written keys are \([1,n]\cup[3n+1,4n]\). The package places \(r\) in \([-n,n]\) and excludes the keys \(h_1=n+1+r\) and \(h_2=3n+r\). The base returns zero. The step returns zero unless the recursive result enters a positive or negative guard whose body has type Bool. Both guards must be proved unreachable. Program

Open at 1. A negative \(r\) places \(h_1\) in the first interval, so its projection there equals \(h_1\) and the corresponding package excludes \(r\). A positive \(r\) places \(h_2\) in the second interval and is excluded there. At zero, both raw keys lie in the gap. The projections instead choose \(n\) and \(3n+1\), both legal, and both packages permit zero. These two probes suffice because the other two target–interval pairs cannot hit a hole. The old projections onto the enclosing interval \([1,4n]\) both remained in the gap at zero. The admission trace records zero of two licensed for that trial, then two of two licensed in the final trial. All of this happens before the induction jump. The positive replay preserves the entire public domain and both written exclusions. Machine-checked instance · Trace, signatures and executable replay

The compiler first needs a proposed enclosing interval with a lower endpoint it can certify for the primary call. It then splits the key-dependent numeric alternatives in the written precondition. For each feasible linear branch it extracts lower and upper bounds expressible using indices exposed by the argument types. It skips a pairing when the numeric solver proves that the package exclusion already holds throughout the branch for every possible payload allowed by the key-independent clauses. Testing only the two boundaries would lose a whole component lying strictly inside a forbidden interval. Branch assumptions guide this search. Only total definitions of fresh minima and maxima enter the generated constraints from the decomposition. The final admission check proves every selected key legal against the exported recursive predicate. A variant adds \(r=k+j\) to the package. At zero the two projected typings require different private values of \(j\). A depth-two run with an effect in the recursive argument prints !!0. Compiler replay

The exported predicate matters even when the written intervals have simple endpoints. Take \(n\ge4\) and change the base to \(-2\). The base can satisfy its package only when \(k\ne n-1\) on integer keys in the written domain. At \(r=-2\) the lower-band probe requests exactly \(n-1\). InvarGenT rejects the guarded program. A control with numeric bodies returning \(-2\) preserves both bands apart from that missing key and prints \(-2\). Removing just the inferred predicate from the final admission goals makes the compiler accept the original body. The resulting OCaml executes the erased negative guard and raises Assert_failure. Mutation and live control

A single interval whose endpoint switches

Let the runtime flag \(f\) be zero or one, and let \(n\ge2\). At \(f=0\) only key 0 is admitted. At \(f=1\) every key in \([0,n]\) is admitted. The package promises \(0\le r\le f\) and forbids \(k=n+1-r\). The base returns zero. The step returns zero after ruling out a recursive result of one. At \(f=0\) the bound already rules it out. At \(f=1,r=1\) precisely key \(n\) rules it out. Zero is consistent with every legal key in both states. Source and both flag values

No finite pool of integer-affine min/max expressions can recover this full guarantee by uniform staged admission. Fix the original erased key to 0, which is legal in both states. Its package permits both the actual observation \((f,r)=(0,0)\) and the hypothetical \((1,1)\) at the same width. A proposed opening independent of the payload is also a member of this expression class. Each expression changes by at most some constant \(C\) independent of \(n\). For an affine leaf \(an+bf+cr+d\) the change is \(b+c\). Addition adds the absolute change bounds, negation preserves them, and min and max take the larger bound. Choose \(n\) larger than every bound in the pool. An expression licensed at the actual observation has value 0 there, so it cannot reach \(n\) at the hypothetical observation. Every key that can be licensed at both observations still permits the hypothetical one. This argument survives any number of admission rounds. The actual zero survives each round, and the first round that removed one would need precisely the probe the bound rules out. Finite-pool obstruction

A single conditional probe suffices. Choose \(k=0\) at \(r=0\) and \(k=n\) at \(r=1\). On observed integer payloads its total, single-valued graph has a presentation in the thesis’s numeric language,

\[0\le k\le n,\qquad (r\le0\lor n\le k),\qquad(1\le r\lor k\le0).\]

From \(r=1\) the original package gives \(f=1\), so the upper endpoint is then admitted. The graph proof uses integrality. At \(0<r<1\) the two disjunctions would require both endpoints at once. To use this presentation in the compiler, a check has to establish coverage on the possible observations and admission against the inferred domain. Current InvarGenT rejects the program at budgets 2, 40 and 10,000. The traced key trials license zero of one proposal and stop on pass 2. Giving the package the extra equality \(r=0\), or using a numeric zero in the guarded body, lets the program check at budget 2 and print zero for both flags. Both controls retain the written input domain. Totality and admission · Compiler checks

There is a shorter proof if the consumer only needs to erase this particular guard. Assume the guard \(1\le r\) and the original package bound \(r\le f\le1\). Then \(r=f=1\). The input predicate admits \(k=n\) under this assumption, and its package excludes \(r=1\). This proof works over rational indices too. More generally, a guard \(G\) is unreachable under the full intersection exactly when every observation satisfying the initial package and \(G\) has some admitted key whose postcondition it violates. The key’s admission obligation then carries the consumer’s guard as a premise. This gives a concrete alternative to synthesizing a total selector. Guard coverage equivalence and the upper-key proof

For a fixed inferred domain and fixed probes this also gives a one-pass update. Write \(A_i(r)\) for admission of probe \(i\) and \(P_i(r)\) for its postcondition, with its private witnesses existentially quantified. Refine the initial set \(S\) to

\[S' = S\cap\{r \mid \forall i.\ A_i(r)\Rightarrow P_i(r)\}.\]

Every actual result in \(S\) is retained because its typing supplies \(P_i(r)\) whenever admission holds. Applying the same update again gives \(S'\). In the circular counterexample, both admissions fail at the reachable payload 3, so both implications hold there and the negative guard stays reachable. The implementation currently licenses a probe for all surviving observations before importing its package. The one-pass calculation describes the stronger proof available if the admission condition travels with each use of that package. An inferred domain can still change on the next outer pass of Table 4.4. Conditional soundness and idempotence · Circular control

The original common-atom induction jump still has a counterexample with a two-inequality solution. Current InvarGenT finishes that example on pass 9. The switched interval locates a separate obstruction before the jump, with the single equality \(r=0\) sufficient to finish the proof. Original update recurrence · Repaired jump

The next review implements the guard-local check, runs this flag program at both inputs and proves a lower bound even for uniform admission inside a guard. Guard-local repair

The compiler change in this review is revision 98a3e17. All 356 active tests passed in the OCaml 5.3 compatibility build, with ten existing TODOs. All 182 previous example signatures matched byte for byte. The compiler replays used OCaml 5.5.1.

Run Lean 4.19 on thesis-review-36.lean. With OCaml and Python’s z3-solver, run python3 thesis-review-36.py /path/to/InvarGenT.native. The optional --before takes revision b56ec02. The --trace build uses the review 22, 23, 24, 26 and 35 diagnostics. For --written-only, remove the renamed actual conjuncts from the goals of check in check_key_admission, retaining the written goals, coordinate equalities and original premises. Admission diagnostics · Earlier diagnostics