When does a recursive package have a least postcondition?

InvarGenT now accepts the local efunction whose two binary postconditions defeated every outer budget at revision 82587e2. The helper returns a number at the input index \(i\). The caller opens its package at \(i\le0\) and asks whether the result can be at least 1. Inference can answer that question with its proposed package relation still in scope. There is a least relation for this helper once its parameter is fixed. Choosing the parameter itself presents a different mathematical problem. Original joint clauses and certificate · Executable comparisons

Check a proposed exclusion against the answer

Write \(P\) for the facts needed to reach an arm, \(E\) for the obligations of evaluating its guard, \(G\) for the successful comparison and \(B\) for the body obligations. The original clause is \(P\Rightarrow E\land(G\Rightarrow B)\). On a retry, require \(P\Rightarrow E\land(G\Rightarrow\bot)\) at a selected arm. Every solution to this stronger clause satisfies the original with the same predicates and witness strategy. On each play reaching \(P\), it establishes \(E\). A successful comparison would contradict the added obligation. This argument applies to a set of arms under one shared prefix. An indexed constructor pattern can supply the success premise on its own. Replacing an arm’s body leaves the evaluation of its scrutinee and guards in place. Same-strategy proof · Operational replacement lemma

The compiler tries ordinary inference first. After a failure it can propose such an exclusion inside the body of a let that may have opened a package. It retains the constructor premises, guard operands, other clients, and recursive definitions. The solver infers a candidate from the retained positive clauses and the program’s own assertions. For the proposed exclusions, it substitutes the final unary and binary predicates, adds the final residual constraints, and checks that each success premise is unsatisfiable. If a proposed exclusion fails, its original body can be restored. The compiler applies the same target and arithmetic restrictions as the earlier guard erasure. Guard evaluation and native integers

At revision e9d7a45, the equality helper repeats the original 28 attempts. Once the original search reports its type failure, the retry makes six more attempts, passes 0 through 5. The complete item succeeds at budget 6 and takes the same path at 10,000. At that revision budget 5 exhausts the original search before the retry can start. InvarGenT at a3151c0 accepts the same program at budget 5. The exported type matches inference on the definition alone. The previously successful one-call program retains the type selected by that call. A second test places a live arm between two impossible ones. Its OCaml body increments a counter exactly once. Moving the helper’s output to a genuinely attainable endpoint leaves the incompatible calls ill-typed. A type-indexed version returns Key Marker and excludes the constructor for Key Cargo without a numeric guard. Programs, signatures, runtime counts and pass trace

With \(m\ge2\) unresolved arms the implementation first tries them together. A failure naming the body of a proposed exclusion lets it restore that body and retry the smaller group. It also tries the individual sites. The descending sequence of groups of size at least two has at most \(m-1\) members, followed by at most \(m\) singleton attempts. The initial query using only independent guard proofs, if present, is additional. This bound counts solver invocations. A failed query or an untried subset supplies no unreachability proof.

When independent inference is enough

Fix the input predicates, the parameter tuple of each result, and any legal first-order witness choices shared with the clients. Suppose the remaining producer clauses have the form \(R_1(a_1)\land\cdots\land R_n(a_n)\Rightarrow R(b)\) after instantiation. Keep the ground instances whose first-order premises hold. A base result has \(n=0\). Several mutually recursive postconditions fit these rules by tagging a tuple with its predicate name.

Let \(L\) contain exactly the conclusions with finite derivation trees. Attaching the finitely many premise trees beneath a rule instance puts its conclusion in \(L\), so \(L\) satisfies every rule. Conversely, induction on a tree shows that every producer solution contains its conclusion. Thus \(L\) is the least producer solution. Suppose also that shrinking these relations preserves the remaining client obligations, as it does for openings used only as hypotheses. Then some producer solution satisfies the clients if and only if \(L\) does. The implication from an arbitrary solution keeps the same choices at the shared interface. To infer a written scheme from the reverse implication, \(L\) itself must be expressible, or one must find an expressible closed enlargement which still satisfies the clients. Least relation, elimination and written-answer theorems

For relay with the parameter fixed to its input, the rules put \((i,i)\) into the relation and repack each recursive result at the same \(i\). The diagonal is already closed. A larger producer relation can admit a positive result at zero and fail the client. A uniform size bound gives no reason to choose the diagonal over that larger relation.

A second helper may keep the preceding result \(x\) or return \(x-1\). Its declared step guarantees \(x\le o+1\) and \(o\le x\). Starting with the empty relation, exact round \(k+1\) over the integers gives the band \(i-k\le o\le i\). The point \(i-k-1\) first appears at the next round. The union has the one-inequality description \(o\le i\). Even an exact, expressible least postcondition of constant size can require infinitely many exact finite rounds. InvarGenT’s induction jump recovers this inequality. The successful retry ends on pass 9 and the joint program needs outer budget 10. Reversing the step lets a call at zero return one. The compiler rejects the incompatible demands, and a well-typed client executes that arm. Finite stages and their union · Checked decreasing and increasing programs

Two choices of package parameter

Give the helper two arguments of indices \(i\) and \(-i\) and let it return the first. Either index is available to carry as the parameter \(k\). Write \(X(i,k)\) for the available package at a call and \(R(k,o)\) for its payload. The obligations below admit the call at every \(i\), require the producer to supply its actual output, and let the client demand that output back. \[ \forall i.\,\exists k.\,X(i,k),\qquad X(i,k)\Rightarrow R(k,i),\qquad X(i,k)\land R(k,o)\Rightarrow o=i. \]

One solution uses \(k=i\) and \(o=k\). The other uses \(k=-i\) and \(o=-k\). No feasible result relation is contained in both, even if its call predicate can be chosen freely. At \(i=1\) there must be a \(k\) such that \(R(k,1)\). Containment would give both \(k=1\) and \(k=-1\). Yet in each solution the relation visible after opening, \(\exists k.\,X(i,k)\land R(k,o)\), is exactly \(o=i\). Intersecting private postconditions before aligning their coordinates destroys a real call. Jointly checking the chosen call predicate and result relation preserves it. A supplier with an unfixed existential output choice can likewise lack a least relation even over two Boolean values. No-common-result and observation proofs

The source example supplies the opposed indices through a constructor and an external function with the precondition \(i+j=0\). InvarGenT infers the local helper and runs the client at \((-1,1)\). Inferring the helper separately makes the representation visible. With the arguments in source order the generated package carries \(n\) and holds \(-n\). Exchanging the arguments at every call makes it carry \(n\) and hold \(n\). Both clients receive the original first value. The iteration must align these coordinates before comparing successive binary relations. Its unary predicate determines which input instances that alignment must cover. Runnable opposed-index example · Both generated constructor annotations

Which inputs may share one parameter?

Suppose the available supplier outputs at input \(i\) form a set \(S_i\) and the client allows a set \(A_i\). Propose a parameter \(k=c(i)\). A relation satisfying \(S_i\subseteq R(c(i))\subseteq A_i\) for every \(i\) exists exactly when \[ c(i)=c(j)\quad\Longrightarrow\quad S_i\subseteq A_j \] for every ordered pair of inputs. Necessity follows by opening the same package parameter at \(j\). For sufficiency take \(R(k)\) to be the union of the supplier sets over all \(i\) with \(c(i)=k\). When clients demand the exact output \(f(i)\), the test says that \(f\) must be constant on each set of inputs sharing a parameter. Either signed coordinate passes for the opposed-index helper. A constant parameter would merge 1 and \(-1\) and fail. Parameter compatibility and exact-output proofs

In a recursive definition, changing the parameter can change the supplier sets themselves. Consider two input states. The first supplies 0. The second supplies 1 and has the additional rule that a recursive result of 0 supplies 2. With separate parameters the second rule never fires, and both clients can require an output at most 1. Merging the parameters makes 0 available to the second rule. It now supplies 2, which violates the client. Comparing the old outputs on the merged inputs would pass the displayed test. Applying the producer rules on the proposed interface before checking the clients detects the failure. Finite counterexample

A constant-size solution the joint updates still miss

The two-state rules yield a small source program. Constructors A : State 0 and B : State 1 expose the two inputs. At depth zero the corresponding results are 0 and 1. A successor at A returns 0. A successor at B calls run B d, returns 2 if the opened result is nonpositive, and otherwise returns 1. Two attached calls use both states and require results at most 1. Runnable unannotated program

A certificate for the joint clauses uses one unary arrow predicate and one binary package predicate. Admit the instances of \(\mathrm{State}(i)\to\mathrm{Nat}\to K(\langle i\rangle)\) and put \(\mathrm{Num}(i)\) at parameter \(\langle i\rangle\). The parameter-membership obligation at a definition uses its input index. The base and successor result types can both be chosen as \(\mathrm{Num}(i)\) before the constructor tests. At A they become \(\mathrm{Num}(0)\). At B the recursive call chooses parameter 1 before opening its result. Every admitted payload then has index 1, so the comparison with zero fails. The other arm supplies 1. Choose the initial function instance at 0 and the two external instances at 0 and 1. These witnesses also discharge the two client bounds. Lean checks the index obligations with the result witnesses in that order. Certificate proof

Writing the type \(\forall i.\,\mathrm{State}(i)\to\mathrm{Nat}\to\exists().\,\mathrm{Num}(i)\) gives executable OCaml. Giving the package just the common interval \([0,1]\) fails. If the branch triggered by zero also returns 1, that interval suffices and the written version runs. These controls test the effect of the parameter on the recursive supplier.

At InvarGenT revision e9d7a45, the unannotated example fails at every outer budget with the other settings at their defaults. Budget zero exhausts the first update. At every positive budget the original search and its exclusion retry each complete pass 0 and fail in numeric abduction on pass 1. Neither search reaches the pass-1 budget comparison, a finished-proposal replay, or final answer extraction. The complete pass and discard traces agree at budgets 1 and 10,000. Immediately before both pass-1 abduction calls, the component proposal is \(\exists t.\,\delta'=()\land\delta=t\). The proposed arrow also returns a package indexed by \(()\). The result predicate at that moment admits every payload type and carries no state coordinate. Removing the attached clients still fails. Even replacing 2 by 1 in the unannotated source leaves the early failure. A diagnosis of the pass-1 failure must therefore also explain the harmless variant. Interface trace · Budget, annotation and trace controls

Packing inside the two state arms now lets inference recover the displayed scheme at budget 3. Observing the joined result through an index-preserving function gives an all-budget failure at revision fc7c3ab. A local match contract now handles that observation. Placement repair and historical counterexample · Current inference

Reproduction

Run python3 thesis-review-23.py /path/to/InvarGenT.native with OCaml on the path. The optional --before argument takes an executable at 82587e2. Use --before-placement for the original state-coordinate failure at e9d7a45. For --trace, build e9d7a45 with both the pass-trace and interface patches. Lean 4.19 checks the proof file with the standard library. Test driver · Pass trace · Interface trace · Lean proofs

InvarGenT commit e9d7a45 implements the exclusion check. The regression suite passed 325 active tests with 10 existing TODOs. All 161 previous example interfaces reproduced byte for byte. Both the default command and the fully annotated compiler generated executable OCaml for the original package example and the opposed-index variant.