Can finitely many term schemes cover all safe calls?
One recursive function suffices to produce infinitely many maximal conjunctive input types. Its indices are finite constructor terms. It uses no arithmetic and terminates on every pair of represented inputs. Its caller needs just one test on the indices, \(a\ne b\). The compiler can verify each fixed positive gap between the two types. No finite list of sound equational schemes covers all the calls that pass the test. InvarGenT program · Lean proof
A total comparison of type indices
Let \(B\) denote the type
Base and \(S\) the unary
type constructor Step. A value of Rep a
witnesses its index. The constructor Root represents \(B\), and Next x represents
\(S(a)\) when \(x\) represents \(a\). Write \(S^n
B\) for a type with \(n\)
constructors above its base. Every inhabited index has this form.
The function compare returns a value of
Comparison(a,b). Its Same constructor requires
\(a=b\). Its Apart
constructor is available at any pair of types. The implementation
returns Same on two roots and Apart when only
one argument is a root. On two successors it compares the children and
returns the corresponding constructor. Induction on the first argument
proves termination and shows that Same is returned exactly
on equal indices. The caller distinct returns true on
Apart and asserts that Same is unreachable.
Thus its greatest safe domain on inhabited indices is precisely
disequality. The type of the constructor rules out Same off
the diagonal. The implementation witnesses failure everywhere on the
inhabited diagonal.
For each \(d>0\), the domain \((h,S^d h)\) is safe by the occurs check. InvarGenT accepts a written scheme with this domain and its mirror. The replay checks both orientations for \(d=1,2,4,8\). An unrestricted pair or an equal pair fails inference. The program executes both a successful distinctness check and the comparison of two equal nontrivial types. Executable checks
Infinitely many maximal typings
Each forward offset \((h,S^d h)\) is
already maximal among safe input patterns. A pattern covering this
offset must admit every represented type on its first coordinate,
including both a root and a successor. Hence this coordinate is a bare
hole. The second coordinate can retain at most \(d\) successors followed by a hole, since it
must cover both \(S^d B\) and
arbitrarily long chains. If the two holes are independent, choose their
values to put both coordinates at the same depth. Safety therefore
forces one shared hole. Matching the pair at the base then forces
exactly \(d\) successors. Distinct
positive offsets give incomparable maximal typings. Lean theorem
offset_maximal
On represented unary indices we can classify every maximal
pattern. In addition to the offsets there are the domains \((S^n B,S^{n+1}h)\) and their mirrors. These
keep the shorter input fixed and admit every longer input. To see
completeness, inspect the two tails after the written successors. If
both tails are holes, safety forces a shared hole and a nonzero
difference, so a fixed offset covers the pattern. If the left side is
fixed at depth \(n\) and the right ends
in a hole, the right prefix must already be longer than \(n\). Otherwise a grounding can reach
equality. The case with the right side fixed is symmetric. Two unequal
fixed terms fit the domain determined by their shorter index. Each
fixed-shorter domain is maximal: it contains two different longer inputs
at the same shorter input, ruling out every offset, and its shortest
allowed longer input prevents a stricter cutoff from covering it. Thus
each unequal ground pair belongs to exactly two maximal
domains, one remembering its offset and one remembering its shorter
index. The compiler replay checks both directions of the fixed-shorter
scheme at two depths. Lean theorems
classify and cutoff_maximal
A finite collection of patterns also fails if it attempts to cover
the offsets piecemeal. Take \(n>0\) larger than every written
constructor depth in the collection. Consider the safe input \((S^n B,S^{2n} B)\). A pattern matching it
has a prefix of successors ending in a hole on both sides. Independent
holes would admit an equal pair. A shared hole forces the difference of
depths to be the difference of the two written prefix lengths. That
difference has magnitude less than \(n\), whereas the example has difference
\(n\). Every safe pattern in the
collection therefore misses this pair. Projecting private equational
witnesses still leaves a single pattern, so they do not enlarge what a
finite family can cover. Lean uses natural numbers to encode unary
chains. A constructor outside this unary signature cannot occur in a
pattern matching the displayed pair. Lean theorem
no_finite_cover · Projection to public
coordinates
A finite description of the exact precondition
For a fixed branch let \(S(x,w)\) be the pattern of states reaching a check, and \(T(x,w)\) the pattern satisfying it. Here \(x\) collects the public inputs and \(w\) collects all branch-local observations. Existential witnesses belonging to the check may be projected inside \(T\). Either pattern may be empty. The obligation on a ground input is that every reached state passes. Form two public patterns \[ L(x)=\exists w\,S(x,w),\qquad G(x)=L(x)\land\forall w\,(S(x,w)\Rightarrow T(x,w)). \] The weakest input precondition for this check is \(L(x)\Rightarrow G(x)\). An input outside \(L\) cannot reach the check. At an input inside \(L\), the check succeeds exactly on \(G\). For the comparison’s forbidden arm, \(L\) is the diagonal and \(G\) is empty. The single implication retains all safe inputs.
Both sides of the implication are computable by unification. Project \(S\) onto \(x\) to obtain \(L\). To obtain \(G\), write \(S\) as \((p(a),q(a,u))\), where \(u\) are the private holes absent from the input. Use two copies of \(q\), with all private holes set to \(B\) in one and to distinct positive successor tags in the other. Unify the two public-and-local tuples with independently renamed copies of \(T\), keeping the public variables \(a\) shared. Project the result onto \(p(a)\). At a fixed input, the two probes generalize to the whole reached fibre, so admission of both by \(T\) proves admission of every state. This is the two-probe theorem applied with the public input retained in the checked tuple. Empty reach gives empty \(L\) and \(G\). Two-probe proof · Symbolic implementation
For a finite list of such obligations, keep one implication per check and conjoin them. Checking a proposed conjunctive scheme \(Q\) requires only the tests \(Q\cap L_i\subseteq G_i\). Unify the left-hand patterns, renaming their holes apart. If the intersection is inhabited, check that \(G_i\) matches the resulting pattern with its remaining holes rigid. On failure, at least one of the two tagged ground probes of the intersection witnesses an unsafe input for this obligation. The replay checks 5,448 proposed public patterns and validates 2,256 returned counterexamples against direct one-sided matching. This result assumes fixed descriptions of the reached and accepted states. Recursive invariant discovery can still be needed to obtain them. Lean equivalences · Executable checker
There is a useful sufficient condition for eliminating these implications again. Over a finite ranked signature, if each \(L_i\) is linear (no repeated hole), its complement is a finite union of patterns. A mismatch is witnessed by a different constructor at one of the finitely many specified positions, with matching constructors along the path to it. Linearity makes all remaining holes independent. Union with \(G_i\) and intersection across checks preserve finite representability. The repeated hole in the diagonal explains precisely where this construction breaks in the example. First-order complement construction in Momigliano and Pfenning, §2
The same form survives a sequence of calls
A contract can now give its domain \(D\) by a conjunction of pattern implications and give its possible results, on that domain, by one positive pattern \(H(x,y)\). This form is closed under sequential composition of fixed contracts. Suppose the first call returns \(y\), and a second call can inspect both \(x\) and \(y\). For each second-call requirement \(L_i(x,y)\Rightarrow G_i(x,y)\), the first call must guarantee \[ \forall y\,\bigl(H(x,y)\land L_i(x,y)\Rightarrow G_i(x,y)\bigr). \] Unify \(H\) with \(L_i\) to obtain the reaching pattern. The preceding construction turns this universal into one implication on \(x\). Keep the first call’s own requirements and add each translated requirement. An admitted first call whose result section is empty makes every second-call requirement vacuous. If the second call’s positive result pattern is \(J(x,y,z)\), the positive result of the composition is \(\exists y\,(H(x,y)\land J(x,y,z))\). On the computed domain every intermediate result already passes the second call’s requirements. Unifying and projecting therefore gives the exact result relation there.
Consequently a straight-line sequence of fixed calls contributes at most the sum of their numbers of domain implications and retains one positive result pattern. Private result types are fresh at each call. Shared variables in a single result stay shared. For example, the replay sends \((a,b)\) to \((b,(a,a))\), demands that the first component differ from the copies, then requires the original \(b\) to be \(B\) and returns \((a,a)\). Both associations of the three calls give \(a\ne b\) and \(b=B\), with the same repeated result. Term sizes can still grow during projection. Lean composition equivalences · Symbolic composition
Keeping the inferred type across the compiler interface
A recursive function opens Hide x and returns a closure
comparing its argument with \(x\). Its
result packages a function on the private type.
InvarGenT previously printed Hidden → Nat → ∃a.a → Bool. In
the source grammar the existential dot binds more tightly than an arrow.
Copying that text into a declaration gave a function taking three
arguments, the last an existential package. The generated OCaml for the
original had two arguments and returned the package. The repaired
signature prints Hidden → Nat → ∃a.(a → Bool). The printer
also encloses a closed existential used as a type-constructor argument,
so Wrap (∃a.a) can be read back. The parser regression
compares both the public type and the underlying package definitions. Signature replay
The index comparison exposed a separate export failure. When printing
fully annotated OCaml, InvarGenT gave the whole inner match the
result annotation (_ step, _ step) comparison. OCaml then
failed to type the branch returning Same, although it
accepted the ordinary export. For a match result containing unnamed type
variables, the exporter now places the result annotation inside each
branch. Each branch can use the refinements learned from its own
pattern. Both exports compile and run. The command-line driver now
propagates the OCaml verifier’s failure status and uses the selected
output path on an annotation retry. The replay also checks verification
in a directory with spaces and verifies that a retry respects an
existing protected output file. Full
comparison and verification replay
Under recursion, even a finite Boolean combination of public term patterns can fail to describe the greatest safe domain. For one unary input controlled by finitely many states, the eventual cycle decides exactly when such a guard exists. Recursive term guards