lean4-htt/tests/elab/ppMotives.lean
Joachim Breitner 26ad4d6972
feat: name the functional argument to brecOn in structural recursion (#12987)
This PR extracts the functional (lambda) passed to `brecOn` in
structural
recursion into a named `_f` helper definition (e.g. `foo._f`), similar
to
how well-founded recursion uses `._unary`. This way the functional shows
up
with a helpful name in kernel diagnostics rather than as an anonymous
lambda.

The `_f` definition is added with `.abbrev` kernel reducibility hints
and
the `@[reducible]` elaborator attribute, so the kernel unfolds it
eagerly
after `brecOn` iota-reduces. For inductive predicates, the previous
inline
lambda behavior is kept.

To ensure that parent definitions still get the correct reducibility
height
(since `getMaxHeight` ignores `.abbrev` definitions), each `_f`'s body
height is registered via a new `defHeightOverrideExt` environment
extension.
`getMaxHeight` checks this extension for all definitions, making the
height
computation transparent to the extraction.

This change improves code size (a bit). It may regress kernel reduction
times,
especially if a function defined by structural recursion is used in
kernel reduction
proofs on the hot path. Functions defined by structural recursion are
not particularly
fast to reduce anyways (due to the `.brecOn` construction), so already
now it may be
worth writing a kernel-reduction-friendly function manually (using the
recursor directly,
avoiding overloaded operations). This change will guide you in knowing
which function to
optimize.


🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:40:18 +00:00

34 lines
595 B
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

def myAdd : Nat → Nat → Nat
| 0, m => m
| n+1, m => (myAdd n m).succ
set_option pp.motives.pi false
#print myAdd._f
set_option pp.motives.pi true
#print myAdd._f
set_option linter.unusedVariables false in
theorem ex : ∀ {α β : Sort u} (h : α = β) (a : α), cast h a ≍ a
| α, _, rfl, a => HEq.refl a
set_option pp.motives.nonConst false
#print ex
set_option pp.motives.nonConst true
#print ex
noncomputable def fact (n : Nat) : Nat :=
Nat.recOn n 1 (fun n acc => (n+1)*acc)
set_option pp.motives.all false
#print fact
set_option pp.motives.all true
#print fact