Skip to content

fix: Level.mkData_eq and Expr.mkData_eq assert a value where C aborts - #6

Merged
vasnesterov merged 1 commit into
masterfrom
fix/mkdata-panic-axioms
Aug 22, 2026
Merged

vasnesterov merged 1 commit into
masterfrom
fix/mkdata-panic-axioms

Conversation

@vasnesterov

Copy link
Copy Markdown
Owner

Draft — depends on #5. The build is red at exactly four goals, and all four
are the hereditary bvar bound that #5 introduces. This change cannot land alone.

The bug

axiom Level.mkData_eq : @mkData = @mkData'
axiom Expr.mkData_eq  : @mkData = @mkData'

Out of range, the real mkData does not return a different value — the C
function calls lean_internal_panic and aborts the process. The models
return Lean's panic! = default, i.e. 0. So both axioms assert a return value
on a branch where nothing is returned.

Level.mkData_eq is additionally jointly inconsistent with two other
whitelisted axioms, Level.hasParam_eq and Level.hasMVar_eq: a level of depth
≥ 2²⁴ gets data = 0, so hasParam reads false while hasParam' is true.
(Argued rather than mechanised — the missing arithmetic step is already in the
repo as Level.mkData_depth.)

The fix

Both carry the range hypothesis — d < 2^24 for Level, br ≤ 2^20 - 1 for
Expr. I made the value arguments implicit, so call sites read
rw [mkData_eq H] rather than rw [mkData_eq _ _ _ _ H].

Cost — smaller than it looks, except for one place

Five of the six users already carry exactly that hypothesis
(mkData_depth (H : d < 2^24), mkData_flags (H : br ≤ 2^20-1), …) and needed
a one-token change.

The sixth, mkData_flags_of_false, is the single place that used the panic
branch as a fact — it case-split on the bound and closed the out-of-range
branch with rfl against the default data. With the hypothesis present it and
its four _of_false corollaries become literally their unconditional
counterparts, so they are deleted rather than duplicated. A comment at
Verify/Expr.lean:306 records what they were and why. Their four users are
rewritten to apply mkData_hasFVar etc., which surfaces the real obligation
rather than hiding it.

What breaks

Verify/Expr.lean:388  Lean.Expr.hasFVar_eq        ⊢ i + 1 ≤ 2 ^ 20 - 1
Verify/Expr.lean:428  Lean.Expr.hasExprMVar_eq    ⊢ i + 1 ≤ 2 ^ 20 - 1
Verify/Expr.lean:468  Lean.Expr.hasLevelMVar_eq   ⊢ i + 1 ≤ 2 ^ 20 - 1
Verify/Expr.lean:508  Lean.Expr.hasLevelParam_eq  ⊢ i + 1 ≤ 2 ^ 20 - 1

Four goals, all the same goal — and it is exactly Expr.BVarBounded from #5.
The blast radius was measured, not guessed: stubbing those four and building the
complete Lean4Lean.Verify target completes all 1197 jobs, so nothing else in
the tree is disturbed, including everything downstream of the four @[simp]
theorems. Stub reverted; the branch has no sorry, no new axiom, no
native_decide.

So #5 and this should be reviewed, and probably merged, together. They are
separate bugs with separate witnesses, but they share one repair.

Two places the correction could be better

  • The corrected statements keep the models' own internal
    if depth > 2^24-1 then panic! / assert! branch on the right-hand side,
    which is now provably dead under the hypothesis. Stating the equation directly
    against the in-range body would be cleaner and would let the three
    Verify/Level.lean proofs drop their
    if_neg (Nat.not_lt.2 (Nat.le_sub_one_of_lt H)) prefix. Kept as-is for a
    minimal diff.
  • mkData_flags_of_false was load-bearing in a way worth noticing: it is the one
    lemma in the repo whose content was "the panic branch returns default".
    Deleting it is right, but it means the four bvar cases now carry an
    obligation that was previously discharged by an appeal to abort-as-a-value.

Note on the freeze

Verify/Axioms.lean is frozen per CLAUDE.md; this is a proposal for sign-off.
Axiom names are unchanged, so guard 1's list still matches.

🤖 Generated with Claude Code

Both Level.mkData_eq and Expr.mkData_eq equate a model with a function
that, out of range, calls lean_internal_panic and aborts rather than
returning anything. Level.mkData_eq is additionally *jointly
inconsistent* with hasParam_eq and hasMVar_eq: a level of depth at least
2^24 gets data = 0, so hasParam reads false while hasParam' is true.

Both now carry the range hypothesis, with the value arguments made
implicit so call sites read `rw [mkData_eq H]` rather than threading four
placeholders.

Five of the six users already carried exactly that hypothesis and needed
a one-token change. The sixth, mkData_flags_of_false, is the single place
that used the panic branch as a *fact*; with the hypothesis present it and
its four corollaries become literally their unconditional counterparts,
so they are deleted rather than duplicated, with a comment recording what
they were and why.

The build is red at exactly four goals, all the same goal, and all of
them the hereditary bvar bound from the looseBVarRange_eq fix. The blast
radius was measured by stubbing those four and rebuilding: all 1197 jobs
complete, so nothing else is disturbed. This change cannot land on its
own.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@vasnesterov
vasnesterov merged commit 8d7fa4f into master Aug 22, 2026
vasnesterov pushed a commit that referenced this pull request Aug 23, 2026
…er, not the worker

**The criterion answered a different question than expected.** Asked
which side condition the consumers satisfy for free, an axiom-cone scan
(`Lean.collectAxioms`) over every non-internal `Lean4Lean.*` declaration
returned:

| axiom | dependents |
|---|---|
| **`Lean.Expr.liftLooseBVars_eq`** | **0** |
| the other 13 in scope | 35 - 162 each |

Zero for a structural reason, not by luck: **the checker never calls
`Expr.liftLooseBVars`.** No occurrence outside `Verify/` and
`Experimental/`, and the only textual matches inside `Verify/` are
theorems about the *model* `liftLooseBVars'`, none about the opaque
constant. The axiom is `@[simp]`, so it has no explicit call sites
either way -- and the cone scan is exactly what settles that, since a
`@[simp]` lemma that fires does appear in the proof term.

**So the fix is subtraction, not domestication:** deleting it cannot
break a proof, and it removes a live false axiom. Fallback if a reviewer
prefers to keep the statement: the C guard is
`!lean_is_scalar(s) || !lean_is_scalar(d)`, so **both** arguments need
bounding -- `s < 2^63 ∧ d < 2^63`, not `d < 2^63` alone. A `USize`
restatement does not fit without changing the signature.

**The guard class, stated as method in §11.1.** §7.1's entries describe
the *worker* functions faithfully; what they skip is the `extern "C"`
wrapper, which is where argument validation lives.

> For any `@[extern]` axiom, read the wrapper first and enumerate every
> early return before reading the algorithm.

A `Nat` crossing into C is boxed, hence always bignum-guarded, and the
guard either panics or silently substitutes a fallback the axiom must
then match. **Seven of fourteen axioms in scope sit on one.**

**Four more failed attacks (#4-#7), recorded with their failure step.**

- **#6 `findAux_isSome` survives** -- both sides use the *same* `==` and
  the functions are parallel clause-for-clause; the shared panic-index
  agrees whatever `default : Entry` is.
- **#4/#5 survive a non-reflexive `BEq`.** `PartialEquivBEq` requires
  symmetry and transitivity but **not** reflexivity, so
  `⟨fun _ _ => false⟩` is legal and `LawfulHashable` is vacuous -- a key
  can be inserted twice and the filter deletes nothing. Both axioms
  still hold. Hand-executed, not machine-checked, and marked so.
- **#7 `structEq_eq` survives, but §7.3's reason was wrong**, in a way
  worth keeping. It claims `Substring.Raw.Internal.beq` *is* the `BEq`
  instance. Not definitionally:
  `example : @Substring.Raw.Internal.beq = (· == ·) := rfl` **fails**.
  The identity holds *through the linker*, via an `@[extern]`/`@[export]`
  pairing -- weaker assurance, and the reason this axiom can never be
  discharged by unfolding. The attack it prompted (position-sensitivity
  like `Substring.Raw.sameAs`) fails under differential test, with both
  negative controls checked.

Still declined: **digama0#31's dependency on #15**, since from one side it is
the weaker test. Ready to pair with the `Level` stream.

§11.9 keeps the three evidence strengths separate and now names the cone
scan as the strongest instrument in the pass -- a complete search over
the environment rather than a sample -- which is why #21's
recommendation is *delete* rather than *weaken*.

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