Skip to content

docs: document defeq and type inference related functions - #12

Merged
digama0 merged 3 commits into
digama0:masterfrom
srghma:defeq-docs3
Aug 4, 2026
Merged

docs: document defeq and type inference related functions#12
digama0 merged 3 commits into
digama0:masterfrom
srghma:defeq-docs3

Conversation

@srghma

@srghma srghma commented May 17, 2026

Copy link
Copy Markdown
Contributor

Squash merge of PR #2 by rish987.
Reference: #2

Adds documentation for defeq and type-inference related functions to improve codebase understandability.

srghma and others added 3 commits August 4, 2026 22:23
Squash merge of PR digama0#2 by rish987.
Reference: digama0#2

Adds documentation for defeq and type-inference related functions to improve codebase understandability.
Reflow the docstrings added in the previous commit to the style used in the
rest of the codebase: text starts on the `/--` line, `-/` closes the last line,
and lines are wrapped at 100 columns (also applied to the new `--` comments,
four of which ran to 100-187 columns).

Incidental fixes while rewrapping: backtick and modernize the `cheapBetaReduce`
example (Lean 3 `λ x, x` -> `fun x => x`, and the body is `xᵢ`, not `x₁`);
`inferConstant` documents `.const name ls`, not `.const e ls`; `->` -> `→` and
`cheapProj = true` -> `cheapProj := true`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The docstrings added two commits ago were written against an older lean4lean
and describe several things the code does not do. Rewritten against the
current implementation:

* `unfoldDefinitionCore` takes a `.const`, not an application with a constant
  head (that is `unfoldDefinition`); its doc had been copied from `isDelta`.
* `isDelta` also requires the right number of universe levels, and the question
  of which constants delta-reduce is already settled by `ConstantInfo.deltaValue?`,
  so point at it rather than restating it.
* `quickIsDefEq` defers constants and free variables too, not just applications
  and projections, and it refutes as well as confirms. Same correction in the
  `lazyDeltaReductionStep` and `lazyDeltaReduction` docs, which reused the wording.
* `lazyDeltaReductionStep` hands `.unknown` back to `isDefEqCore'`, not `isDefEq`.
* `reduceNat` was missing `Nat.succ`, `land`, `lor`, `xor`, `shiftLeft` and
  `shiftRight`, and `Nat.beq`/`Nat.ble` yield `Bool`, not `Nat`, literals.
* `isDefEqOffset` decides `0 ≡ 0` before looking at successors.
* `cheapBetaReduce` also reduces a body with no loose bvars, and leaves `e`
  alone in every other case, which is the point of the name.
* `toCtorWhenStruct`'s `String` example predates `String` becoming a two-field
  structure over `ByteArray`; use `Prod`. Likewise `tryStringLitExpansionCore`
  matches `String.ofList`, which is no longer the constructor.
* `inductiveReduceRec` applies the rule to the motives and minor premises as
  well as the parameters, accepts literal major premises, and re-applies the
  arguments past the major premise.
* `inferType` also throws on resource exhaustion, so not "if and only if".
* `isDefEqCore` referred to a `check` function; it is `checkType`.
* `whnfFVar` uses `whnfCore`, and `whnfCore`'s `cheapRec` is never set.
* `RecM.run` and `lazyDeltaReduction` take their limits from `FuelConfig`.

All three `FIXME(kernel)` comments are rewritten as statements: each asked a
question the code answers -- `cheapProj := true` leaves head projections
unreduced, the recursive `whnfCore` call does reach `reduceRecursor`, and the
eta-struct case is redundant work that the kernel performs identically -- so
none of them is a divergence, and none stays a FIXME.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@digama0
digama0 merged commit 924e7d8 into digama0:master Aug 4, 2026
1 check passed
digama0 added a commit that referenced this pull request Aug 4, 2026
`whnfCore`'s `cheapRec` flag has never been set to `true` anywhere in this
repo, so every `if cheapRec then ... else whnf e` took the `whnf` branch and
the flag only cost us an argument to thread through eight functions.

It is dead upstream too, so this does not diverge from kernel behavior. The
flag was added in 2019 for one caller outside the kernel, `csimp`'s
`is_stuck_at_cases`, which wanted to look through recursor applications
without paying for delta-reduction. When lean4 commit 14260f454b split `cheap`
into `cheap_rec`/`cheap_proj` so `is_def_eq` could use lazy projections alone,
that caller moved to a new `whnf_core_cheap` wrapper; lean4#9275 then deleted
the old compiler, and with it the only thing that ever passed
`cheap_rec = true`. The wrapper survives in `type_checker.h` with no callers.

Dropping it makes `reduceRecursor`'s `cheapProj` unused as well, since its
sole use was inside the `cheapRec` branch, so that goes too. The `whnfCore`
docstring added in #12 loses its `cheapRec` half, keeping a note that the
kernel still carries the flag.

The `Verify` proofs about these functions go through unchanged apart from the
dropped argument, which is the check that behavior is unaffected.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.

2 participants