Skip to content

fix: primitive-definition recognizer does not establish what the model assumes - #2

Closed
vasnesterov wants to merge 1 commit into
masterfrom
fix/primitive-recognizer
Closed

vasnesterov wants to merge 1 commit into
masterfrom
fix/primitive-recognizer

Conversation

@vasnesterov

Copy link
Copy Markdown
Owner

Three defects in Environment.checkPrimitiveDef (Lean4Lean/Primitive.lean).
Each lets a declaration pass the check without the property the abstract model
then assumes of it, so checkPrimitiveDef.WF — the statement that the recognizer
establishes VEnv.HasPrimitives — was false as written in three independent
ways.

This check is lean4lean-only (see divergences.md): Lean itself does not verify
that primitives are declared with the right types and definitional behaviour,
because it ships its prelude and an alternative prelude is unsupported. lean4lean
does verify it, and needs to for soundness.

1. Char.ofNat / String.ofList recognized up to defeq

VEnv.HasPrimitives pins these constants' VConstant to a literal syntactic
type, and TrConstant reads v.type off structurally via TrExprS. But the
recognizer only checked isDefEq v.type q(Nat → Char).

Declaring Char.ofNat at (fun _ : Nat => Nat → Char) Nat.zero passes that
check and translates to a structurally different VConstant, refuting
PrimitiveResult.preserves — which quantifies over every ci' with
TrDefVal, so no proof can dodge it.

Fixed by comparing with Expr.eqv. That is the right notion here: the toolchain
declares both at exactly the expected shape up to binder name, and TrExprS
ignores precisely binder names and binder info.

2. The Nat equations were checked on ill-typed terms

defeq1/defeq2 compared ∀ _ : Nat, e₁ against ∀ _ : Nat, e₂ — a
deliberately ill-typed pseudo-type, used to get under a binder. Nat.add x 0 is
not a type, so TrExprS.forallE's hypothesis is unsatisfiable, no witness
exists, and every one of those checks carried no semantic content: the
isDefEq specification's hypothesis could never be discharged.

Nat.land and Nat.lor additionally bound their Bool variable at type Nat,
which is not merely untranslatable but plainly ill-typed.

Fixed by checking under withLocalDecl-bound free variables, as the Nat.div
and Nat.mod branches already did. Same decision procedure — isDefEq on an
arrow reduces via isDefEqForall, which introduces exactly such a variable.

3. Branches depended on constants they never required

Every arithmetic branch built Nat/Bool literals, and depended on other
primitives' semantics, without requiring any of them in the environment. Each was
implied by a later checkType — but only recoverable by inverting "type
checking succeeded", and the TrExprS witnesses the isDefEq specification
demands cannot be built before that inversion is available.

Now explicit env.contains guards.

Testing

The recognizer is strictly stricter than before, so this needed regression
testing rather than just a build:

  • Kernel Arena lean4lean-local: 185 correct / 6 either / 0 incorrect —
    identical to the baseline.
  • lake build lean4lean and lake build Lean4Lean.Tests green, including the
    run_meta self-test that runs checkPrimitiveDef over all 24 entries of
    Environment.primitives in the live environment.
  • All three Guard.lean checks still pass; no new axiom/sorry/partial/
    extern/implemented_by.

divergences.md is updated to describe the added strictness.

Not fixed here

A fourth defect remains, filed separately: checkPrimitiveDef runs before
checkConstantVal, so it calls isDefEq on untyped input and records the result
in the EquivManager, whose well-formedness invariant demands translation
witnesses that untyped input does not have. That one is a broken postcondition
rather than a soundness hole — both lean4lean and Lean's own kernel reject the
witness declaration with unknown constant.

🤖 Generated with Claude Code

…aims

Three defects in Environment.checkPrimitiveDef, each of which let a
declaration pass the check without the property the abstract model then
assumes of it. This check is lean4lean-only (see divergences.md): Lean
itself does not verify that primitives are declared with the right types
and definitional behaviour, because it ships its prelude.

Char.ofNat and String.ofList were recognized up to definitional equality
while the model pins their VConstant to a literal syntactic type, and the
translation reads the declared type off structurally. Declaring
Char.ofNat at `(fun _ : Nat => Nat → Char) Nat.zero` passed the check and
produced a different constant. They now compare with Expr.eqv, which is
exactly what the translation sees through -- the toolchain declares both
at the expected shape up to binder name.

The Nat equations were compared as `∀ _ : Nat, e₁` against
`∀ _ : Nat, e₂`, a deliberately ill-typed pseudo-type used to get under a
binder. Since `Nat.add x 0` is not a type, those comparisons had no
translation witness at all and so carried no semantic content. They are
now checked under withLocalDecl-bound free variables, as the Nat.div and
Nat.mod branches already did. Nat.land and Nat.lor were additionally
binding their Bool variable at type Nat.

And every arithmetic branch built Nat and Bool literals, and depended on
other primitives' semantics, without requiring any of them present. Each
was implied by a later checkType, but only by inverting "type checking
succeeded" -- and the witnesses the specification demands cannot be built
before that inversion is available. They are now explicit guards.

The recognizer is strictly stricter than before. Kernel Arena is
unchanged at 185 correct / 6 either / 0 incorrect, and the run_meta
self-test over all 24 entries of Environment.primitives still passes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@vasnesterov

Copy link
Copy Markdown
Owner Author

Can it be splitted into 3 (or 4) PRs, one for each problem?

@vasnesterov

Copy link
Copy Markdown
Owner Author

Split as requested. Four PRs, one per defect, each independent and applying to pristine master — they compose in any order under a normal 3-way merge (verified: applying all three of the first landed ones to pristine gives back exactly this branch, modulo the fourth fix below).

The fourth was not in this PR at all — it was found afterwards, while assembling the proof, and it turned out to have a second instance I had not seen: the equations' right-hand sides also mention primitives whose types HasPrimitives never pins (Nat.pred in the sub branch, the boolean combinator in land/lor/xor), so those were unchecked too.

On testing: each of the four makes the recognizer strictly stricter, and the combined change was arena-verified at 185 correct / 6 either / 0 incorrect — the baseline. Since each split is a subset of that strictness, none can reject anything the combined version accepted, so a per-branch arena run is not needed to establish no regression.

@vasnesterov
vasnesterov deleted the fix/primitive-recognizer branch August 22, 2026 12:55
@vasnesterov
vasnesterov restored the fix/primitive-recognizer branch August 23, 2026 03:31
vasnesterov pushed a commit that referenced this pull request Sep 5, 2026
…lent to the hole, not stronger

Neither proved nor refuted, and reduced in two independent directions that make its truth unnecessary. The
uninhabitedness premise is provably free at one binder — the statement with it deleted is equivalent at every
ordered environment — so any proof must come from the contexts and the derivation alone. And the context guard
can be kept at no cost, giving a route at cone 3415 with zero holes and zero watched names whose residual is
*equivalent* to the hole, where yesterday's route was strictly stronger than what it proves. The move earlier
rounds missed: do not reduce to one binder, hand the residual the whole remaining lift at the first uninhabited
peel, and the guard never crosses an uninhabited binder.

It also ordered holes #1 and #2, with the borrow smaller than #2 itself, and a like-for-like comparison that
favours the new route on both axes: 3674 against 3698 constants and four by-policy names against six, from a
strictly weaker starting residual.

The finding that corrects my brief: there are six statements in this family, the four on the abstract side keep
both context guards and are equivalences with their targets, and only the two on the verification side gave a
guard away — so the statement I handed over as "the reduced target" was the lossiest member of its own family.
Three of its priors were wrong in direction, the most in this series, and it says the result came from the two
that surprised it rather than from its bet.

The price is stated rather than buried: nothing is discharged, and the repair costs known non-vacuity, since the
tree's only uninhabited-binder witness has an ill-formed context — so the new residuals are vacuous exactly if
the hole is already closed.

Its checklist correction is adopted: in a corner with sibling statements, build the family table by hypothesis
shape *first*. Extremes are for a single statement; a family table prices every route before you pick one, and
all three of this round's results came from one such grep after a third of the round went to extremes.

Two of its three instrument claims I adjudicated rather than adopting. That a bare build does not re-run the
guards is refuted — it emitted all three lines; the true and sharper point is that guard 2 audits one cone, so a
leaf's hole is invisible to it by design, which is why the census exists beside it. That the fixed-root census
script cannot see orphan modules is confirmed, and I have been using the whole-population variant throughout, so
the figures I have reported stand.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant