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

Aperta Adatta ai principianti
#16,681 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
compilers

Direzione di ricerca

Leggi src/kernel/inductive.cpp, partendo da add_inductive_fn::is_reflexive() e confrontandolo con check_positivity() nello stesso file, che chiama già whnf prima di verificare is_pi. Applica la modifica suggerita in modo che il tipo dell'argomento del costruttore venga ridotto con whnf(binding_domain(t)) prima del controllo is_pi/has_ind_occ. Fatto significa che la riproduzione Fn/T dell'issue stampa isReflexive=true dopo aver ricostruito Lean, con i test esistenti del kernel che continuano a passare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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;
Lingua principale
Lean
Stelle
10.3k
Fork
1.1k
Merge medio
19h 8m
PR unite (30g)
421

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.