Skip to content

feat: verify primitive-model conservation through declaration checking - #32

Draft
kim-em wants to merge 8 commits into
digama0:masterfrom
kim-em:agent/hasprimitives-conservation
Draft

feat: verify primitive-model conservation through declaration checking#32
kim-em wants to merge 8 commits into
digama0:masterfrom
kim-em:agent/hasprimitives-conservation

Conversation

@kim-em

@kim-em kim-em commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

This PR adds an end-to-end addDecl.WF theorem showing that successful declaration checking preserves a verified environment for the currently modeled fragment. It replaces ad hoc recognition of non-structural Nat primitives with checked reduction certificates, proves conservation of VEnv.HasPrimitives, and rebuilds the declaration-level verification around those results. The diff contains 24,247 insertions and 1,264 deletions across 33 files.

The final theorem is deliberately restricted by Declaration.SupportedByModel:

def Declaration.SupportedByModel : Declaration → Prop
  | .inductDecl .. => False
  | .mutualDefnDecl vs => (vs.map (fun v => v.name)).Nodup
  | _ => True

Inductive declarations are therefore out of scope entirely. For mutual definitions, addMutual.WF takes name uniqueness as a hypothesis. Master derived that fact from the executable name checker; this branch still rejects duplicate names, but its proof interface no longer derives the fact from that check.

This does not mean that the kernel is verified. The branch removes one admitted proof and adds none, but addDecl.WF still depends on sorryAx through admitted lemmas elsewhere in Lean4Lean/Verify/ that are already present on master. #print axioms Lean4Lean.addDecl.WF reports 34 dependencies, including sorryAx and two bv_decide axioms originating in Lean4Lean/Verify/Expr.lean, with no native_decide axiom.

The executable certificate checker narrows the alternative preludes that lean4lean accepts. The Nat.gcd and Nat.bitwise certificates require their two-Nat well-founded state to use PSigma.mk. Execution with Lean 4.33.0-rc2 confirms that the shipped Nat.gcd uses that representation, so the shipped prelude is unaffected. Ordinary user declarations cannot reach this difference because checkPrimitiveDefCore dispatches on the reserved declaration names Nat.gcd and Nat.bitwise. An alternative prelude can reach it by defining one of those constants through a one-argument well-founded helper over another state type such as Nat × Nat. doc/wf-certificate-design.md describes the comparison and proposes the corresponding divergences.md entry.

Review the change top-down in this order:

  1. Read Lean4Lean/Primitive.lean first. Commit 2fc4f84 changes that file by 1,223 insertions and 127 deletions and contains the executable acceptance delta: the generic well-founded reduction record, the GCD and bitwise specializations, and the closed equations for the fuel-based mod and div helpers.
  2. Read the bottom of Lean4Lean/Verify/Environment.lean next, especially Declaration.SupportedByModel, addMutual.WF, and addDecl.WF, to see the final claim and every additional hypothesis.
  3. Follow Nat.gcd end to end through NatGcdFixCertificate, checkNatGcdFixCertificate.WF, NatGcdFixCertificate.NormalizedValid.reflects, and the GCD branch of primitive conservation. This is the representative well-founded case.
  4. Read the shared translation, type-checker, condition-reflection, and mod/div infrastructure. Commit 27304be adds 9,808 lines across the nine reflection-toolbox files.
  5. Read the remaining primitive cases in Lean4Lean/Verify/Primitive.lean. Commit b2d9169 adds 8,957 lines covering the per-primitive conservation development; the bitwise proof is the largest specialization, while the direct-equation primitives follow the same checker-to-model pattern.

Validation on the exact pushed tree succeeds with:

  • lake build: 161 jobs
  • lake build Lean4Lean.Experimental: 68 jobs
  • lake build Lean4Lean.Tests: 63 jobs
  • lake exe lean4lean Init.Core: 1,035 declarations checked
  • lake exe lean4lean --fresh Init.Core: 3,953 declarations checked

All commands exit successfully.

🤖 Prepared with Claude Code

@kim-em
kim-em force-pushed the agent/hasprimitives-conservation branch from 9b323af to 8bee9c5 Compare August 4, 2026 01:34
@kim-em
kim-em force-pushed the agent/hasprimitives-conservation branch from 041c498 to 2283ffe Compare August 4, 2026 02:10
@kim-em
kim-em marked this pull request as ready for review August 6, 2026 03:00
@kim-em
kim-em force-pushed the agent/hasprimitives-conservation branch from eee5b1a to 40635ec Compare August 12, 2026 01:35
johnchandlerburnham added a commit to argumentcomputer/lean4lean that referenced this pull request Aug 20, 2026
The reconcile merge 29d67a7 absorbed all five digama commits; the
ladder entry is deleted per convention. Section 2 records: remote drift
reconciled (watch items PR digama0#43/digama0#32/digama0#27 stand, re-checked at every
checkpoint boundary), ladder position = lane phase (Lane R L4L-16N,
Lane V checker pre-closure, Lane D D-ladder volume, 16E in slack). The
V6 route-map row carries the banked repair recipe and its
dependent-match residual (one focused Lane-V session); the collision
risk row notes the absorption. reduceProjCore.WF discharge is ledger
D022 (upstream-contribution candidate); D012 narrowed; D021 added.
@kim-em
kim-em marked this pull request as draft August 22, 2026 02:38
@kim-em
kim-em force-pushed the agent/hasprimitives-conservation branch from f01ded2 to 56af22f Compare August 22, 2026 03:36
kim-em added 7 commits August 22, 2026 03:51
List.Forall2 helpers, and the VDecl/VEnv/Typing lemmas the conservation
argument needs about environment extension.
Nat.mod, Nat.div, Nat.gcd and Nat.bitwise are defined by non-structural
recursion, so recognizing them requires unfolding a WellFounded.fix or a
fuel-carrying go function and checking the result against the expected
equations. Replace the ad-hoc unfolding with an explicit certificate: a
NatWellFoundedCoreResult recording the unfolded shape, specialized to
NatGcdFixCertificate or NatBitwiseFixCertificate and checked against the
expected top/zero/succ equations.

This is the only part of the branch that changes what the kernel accepts.
addDefinition now establishes the header and body typing facts before
calling checkPrimitiveDef, because verifying checkPrimitiveDef needs those
facts to justify its own isDefEq calls; the reserved-name check moves after
the primitive result is available. addMutual checks each member with fresh
caches and makes the block available through opaque headers rather than
axioms, so no member's body can unfold another's unchecked value.

Does not build on its own: Verify/Environment.lean is repaired in a later
commit.
M.WF.sandbox, for computations that cannot invalidate verifier state, and
the ReflectsNat* predicates that say what it means for an abstract
environment to reflect a Nat operation. VEnv.HasPrimitives gains natPred
and natBitwise fields.
Reflection predicates, the HasPrimitives extension lemmas, the certificate
validity arguments, and a checkPrimitiveDef.<op>.WF_typed theorem for each
of the 17 primitives, plus checkPrimitiveInductive.WF.

Closedness side conditions are discharged by the fvars_closed tactic, which
reduces FVarsIn structurally. Deciding hasFVar/hasMVar instead would not
work: those read the cached Expr.data word built by the extern mkData,
which the kernel cannot reduce.
The fuel and fixpoint reasoning for Nat.bitwise, Nat.mod and Nat.div,
separated from the per-primitive conservation theorems that consume it.
Rebuild the environment verification layer on the restructured checkers:
per-declaration WF theorems for the new addDefinition and addMutual, the
mutual header/body relations, and the environment model for opaque mutual
headers. addDecl.WF now discharges the primitive cases from the previous
commits instead of admitting checkPrimitiveDef.WF.

Verify/Environment/Extension.lean is superseded and retains only what is
still referenced.
@kim-em
kim-em force-pushed the agent/hasprimitives-conservation branch from 56af22f to 5aaa700 Compare August 22, 2026 03:54
Record what the certificates are, why Nat.mod, Nat.div, Nat.gcd and
Nat.bitwise need them, and what the checker verifies.

Document the one acceptance difference from the previous checker: the gcd
and bitwise certificates require the well-founded state to be built with
PSigma.mk, where the previous checker was generic in the state type. Lean's
shipped Nat.gcd uses exactly that representation and dispatch is by reserved
name, so ordinary code is unaffected; the restriction is on which
alternative preludes lean4lean accepts.
@kim-em kim-em changed the title Verify HasPrimitives conservation feat: verify primitive-model conservation through declaration checking Aug 22, 2026
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