Skip to main content

Module termination

Module termination 

Source
Expand description

Termination checking for fixpoints.

This module implements the syntactic guard condition that ensures all recursive functions terminate. Without this check, the type system would be unsound — we could “prove” False by writing fix f. f.

§The measure

The guard decides the LEXICOGRAPHIC product of the structural-subterm order ◁ over an ORDERED list of COLUMNS:

  • Column 0 is the Giménez structural argument — the binder the body’s match discriminates on, or, when the body does not match on a binder, the first inductive-typed one. This is what lets an INDEXED family recurse: le.rec’s fixpoint is fix rec. λm:Nat. λh:le n m. match h … and the structural argument is the PROOF h (position 1), not the Nat index m.
  • Columns 1… are the remaining inductive-typed binders of the λ-telescope, in positional order.

A recursive call — to this fixpoint or a sibling in the same mutual block — is admitted iff there is a column i whose argument is a strict subterm of column i’s parameter and, for every earlier column j < i, the argument is a strict subterm OR definitionally that column’s parameter. Succ m1 inside match m with | Succ m1 => … is definitionally m, so Ackermann’s inner call ack (Succ m1) n2 holds column 0 EQUAL and decreases column 1 — which is exactly termination_by m n => (m, n).

The lexicographic product of finitely many copies of a well-founded relation is well-founded, and ◁ (strict structural subterm) is well-founded on well-typed values, so an admitted call chain cannot be infinite. Note that ◁ is one relation over ALL terms — it does not depend on the column’s type — which is what makes a mutual block’s members share a single measure even when their columns are typed differently.

§What is NOT in the measure

“Smaller in SOME column” is not a decrease: a subterm of column 1 says nothing about column 0, and a measure that let one discharge the other would accept f n2 m1, which grows. The scan therefore stops at the first column that is neither smaller nor equal.

§The subterm analysis

Deciding t ◁ p is more than “is t a variable the match bound”. Following Coq’s subterm_specif, the guard also walks β-redexes, δ-unfolds a transparent definition applied to arguments, and analyses a match on a term already known ≤ p as the MEET over its branches (each branch’s fields being themselves subterms of p). pred n δ-unfolds to match n with | Zero => Zero | Succ k => k, whose Zero branch yields Zero — NOT a strict subterm of n when n IS Zero — so the meet refuses it, exactly as Coq does.

§The refutation refinement — decreasing_by, discharged in the kernel

One branch of that meet can be RULED OUT. In case isFalse of a match whose discriminant is a δ-transparent, kernel-checked DECISION PROCEDURE applied to the recursion’s own data, the case binds hn : ¬(x = C) for a NULLARY constructor C. Reaching that branch requires the discriminant to have ι-reduced to isFalse _; since the decision procedure is a closed, kernel-checked definition, its reduction at a closed x ↝ C would have to produce a closed inhabitant of ¬(C = C) — a closed proof of False. So where the axioms in scope are consistent, the branch is unreachable at x ↝ C and the C branch of the meet drops out. For a two-constructor inductive that pins x’s head to the other constructor, and pred n becomes n’s Succ field — a genuine strict subterm.

This is the ONE rule here whose soundness is conditional on the consistency of the axioms in scope rather than unconditional, so it is fenced hard: the discriminant’s head must be a Context::is_definition (never a bound variable, never an axiom), every argument of its spine must be a column parameter, a known subterm, a constructor application or a literal, the refuted constructor must be NULLARY, the refined variable must be a live column’s own parameter, and it must be the discriminant’s ONLY free variable. The argument above needs the decision closed at x ↝ C: with a second open argument, a hypothesis e : False say, a checked procedure can answer isFalse (λh. e) at C under e’s binder, the branch is reached, and the recursion never ends there. crates/logicaffeine_tests/tests/G_recursor_extra.rs pins each fence with the neighbouring shape it must still refuse, and crates/logicaffeine_tests/tests/kernel_paradox_corpus.rs the closedness fence.

Functions§

check_termination
Check that a Fix term satisfies the syntactic guard condition.
check_termination_mutual
Check that a MUTUAL block of fixpoints terminates — the mutual Giménez guard.
structural_columns
THE ι-SPECIFICATION — which argument makes a Fix unroll.
structural_position
The single argument POSITION a reducer needs: the Giménez structural column, or None when the guard admits no column at all and therefore no ι rule applies to this fixpoint.