Kernel: `is_reflexive()` does not call `whnf`, misclassifying reflexive inductives defined via type aliases

Open Beginner friendly
#16,681 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
compilers

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.