Kernel: `is_reflexive()` does not call `whnf`, misclassifying reflexive inductives defined via type aliases
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
Research direction
Read src/kernel/inductive.cpp, starting at add_inductive_fn::is_reflexive() and comparing it with check_positivity() in the same file, which already calls whnf before testing is_pi. Apply the suggested change so the constructor argument type is reduced with whnf(binding_domain(t)) before the is_pi/has_ind_occ check. Done means the issue's Fn/T reproduction prints isReflexive=true after rebuilding Lean, with existing kernel tests still passing.
Written by the indexing model from the issue text.
Description
Description
In src/kernel/inductive.cpp, add_inductive_fn::is_reflexive() checks whether a constructor argument is a Pi type syntactically without reducing it via whnf:
https://github.com/leanprover/lean4/blob/b82e465/src/kernel/inductive.cpp#L325-L340
bool is_reflexive() {
for (size_t idx = 0; idx < m_ind_types.size(); idx++) {
inductive_type const & ind_type = m_ind_types[idx];
for (constructor const & cnstr : ind_type.get_cnstrs()) {
expr t = constructor_type(cnstr);
while (is_pi(t)) {
expr arg_type = binding_domain(t);
if (is_pi(arg_type) && has_ind_occ(arg_type))
return true;
expr local = mk_local_decl_for(t);
t = instantiate(binding_body(t), local);
}
}
}
return false;
}
In contrast, check_positivity() in the same file calls t = whnf(t); before testing is_pi(t), so a constructor whose recursive argument is a Pi type hidden behind a delta-reducible definition (type alias) is accepted by the kernel.
However, because is_reflexive() does not call whnf(binding_domain(t)), is_pi(arg_type) evaluates to false, causing InductiveVal.isReflexive to be recorded as false even though the inductive type is reflexive.
Minimal Reproduction
import Lean
def Fn (α : Type) : Type := Nat → α
inductive T : Type where
| mk : Fn T → T
#eval do
let env ← Lean.getEnv
let some (.inductInfo ival) := env.find? ``T | throwError "not found"
IO.println s!"isRec={ival.isRec}, isReflexive={ival.isReflexive}"
-- Expected: isRec=true, isReflexive=true
-- Actual: isRec=true, isReflexive=false
Impact
Downstream elaborators/derivers rely on InductiveVal.isReflexive as a guard (e.g., src/Lean/Elab/ConfigEval/DeriveEvalExpr.lean:78 and src/Lean/Elab/ConfigEval/DeriveEvalTerm.lean:150: if ival.isNested || ival.isReflexive then), which will be bypassed for reflexive inductives defined through type aliases.
Suggested Fix
Reduce binding_domain(t) with whnf in is_reflexive():
expr arg_type = whnf(binding_domain(t));
if (is_pi(arg_type) && has_ind_occ(arg_type))
return true;
- Dominant language
- Lean
- Stars
- 10.3k
- Forks
- 1.1k
- Avg merge
- 19h 8m
- Merged PRs (30d)
- 421
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16305 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16299 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16257 ·
Maintainers usually reply within 1 day
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
leanprover/lean4#15683 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
leanprover/lean4#15626 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
swc-project/swc-next-issues#817 ·
-
Magma mode: div:=, mod:= and the other keyword assignments (join:=, meet:=, diff:=, sdiff:=, and:=, or:=, xor:=) are refusedPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
sagemathinc/sagebrush#1683 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
microts: a model local that every branch assigns raises unused_assignments in the generated RustOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
pocket-stack/pocketjs#756 ·
-
bot-triaged module: aotinductor module: compile-time module: inductor module: performance module: rocm oncall: export oncall: pt2 triaged
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day