Importing two modules that each realized `f.induct` fails with "environment already contains"

Aperta
#16,789 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
compilers

Direzione di ricerca

Start by reading subsumesInfo in src/Lean/Environment.lean around the reported lines 2633–2665, then reproduce the import failure with the four-module example using lake build Repro.E. The issue identifies isPropCheap and the bound-variable conclusion of the induction principles as the suspected cause. Done means the reproducer builds while duplicate realized principles merge safely.

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

Descrizione

P-medium
Prerequisites
  • Check that your issue is not already filed.
  • Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release.
    (The test case needs four modules, so it was built locally with lake build on nightly-2026-11-26.)
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    (Not a soundness bug.)
Description

In a module, a reference to a functional induction principle such as f.induct of an imported function f realizes the principle in the referring module. If two modules each realize it on the main elaboration branch, and neither imports the other, a module that imports both fails:

error: Repro/E.lean:1:0: import Repro.D failed, environment already contains 'f.induct_unfolding' from Repro.C

The main branch covers a proof elaborated under set_option Elab.async false, a theorem's statement, a definition's value, and the code of a command elaborator. Equation theorems such as f.eq_1 realized in the same way merge on import, and so do the induction principles when the files are not modules or when the third module uses import all.

The cause appears to be subsumesInfo in src/Lean/Environment.lean (v4.36.0 lines 2633 to 2665). Without import all, finalizeImport reads each module at the exported level, where both copies of the principle are axioms. Two axioms merge only if isPropCheap accepts their type, and isPropCheap requires the conclusion, after the binders, to be an application of a constant whose type ends in Prop. The conclusions of f.induct, f.induct_unfolding, and f.fun_cases_unfolding are applications of the motive, a bound variable, so the two copies are refused although their type is a proposition. f.eq_1 concludes in Eq, a constant, and merges.

When the principle is realized in a proof elaborated on its own branch, as theorem proofs are by default, the third module builds. On v4.36.0 the realizing modules then export the principle as a theorem, through the allRealizations fallback that #16623 describes, and two theorems merge. On the nightly they do not export it (#16623). Whether two such modules can be imported together therefore depends on where the principle is first named and on Elab.async.

Context

A verified compiler's command elaborator proves the termination of each loop it generates with fun_induction on the source function. Every module that compiled the same function exported f.induct and f.induct_unfolding as axioms, and a module that imported two of them failed as above. The compiler now computes that proof on an environment branch of its own, with Environment.addConstAsync, as elabMutualDef does for a theorem's proof.

Steps to Reproduce
  1. Create a Lake package with these files.

    lakefile.toml:

    name = "repro"
    defaultTargets = ["Repro"]
    
    [[lean_lib]]
    name = "Repro"
    

    lean-toolchain:

    leanprover/lean4:v4.36.0
    

    Repro/A.lean:

    module
    
    @[expose] public section
    
    def f (n : Nat) : Nat := if n = 0 then 0 else f (n - 1)
    termination_by n
    decreasing_by omega
    

    Repro/C.lean:

    module
    
    public import Repro.A
    
    set_option Elab.async false in
    public theorem c (n : Nat) : f n = 0 := by
      fun_induction f n <;> simp_all
    

    Repro/D.lean: the same as C.lean, with d in place of c.

    Repro/E.lean:

    module
    
    public import Repro.C
    public import Repro.D
    
  2. Run lake build Repro.E.

Expected behavior: Repro.E builds, with one copy of f.induct_unfolding, as it does when the option is removed from C and D, when E uses import all, or when the files are not modules.

Actual behavior (v4.36.0 and nightly-2026-11-26):

error: Repro/E.lean:1:0: import Repro.D failed, environment already contains 'f.induct_unfolding' from Repro.C
Versions

Lean 4.36.0 (commit 611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9) and Lean 4.38.0-nightly-2026-11-26 (commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee), Ubuntu 24.04.5 on aarch64.

Additional Information

Each variant was built with lake build on both versions, with the same result on each.

Variant of C and D, or of E Result
As above Fails, f.induct_unfolding
Without set_option Elab.async false Builds
E adds import all Repro.C and import all Repro.D Builds
All four files without module, public, and @[expose] public section Builds
No option; the principle in a statement: public theorem c (_ : @f.induct = @f.induct) : True := trivial Fails, f.induct
rw [f.eq_1] in place of fun_induction Builds
Structural recursion, def g : List Nat → Nat | [] => 0 | _ :: xs => g xs, with fun_induction g xs Fails, g.induct_unfolding
fun_cases f n in place of fun_induction f n Fails, f.fun_cases_unfolding

The .olean of C holds f.induct_unfolding as an axiom with the option and as a theorem without it (v4.36.0), as Lean.readModuleData shows.

The comment on subsumesInfo explains why two axioms are not merged in general: they may come from definitions whose different values would make the merged environment inconsistent. A functional induction principle concludes in its motive, whose type ends in Prop. A check that also accepts a bound variable at the head of the conclusion, when that binder's type ends in Prop after as many arguments as the conclusion applies, would merge these copies.

Related: #15888 reports the same error outside the module system, where a duplicate _sparseCasesOn auxiliary definition collides, and #12825, fixed by #12964, the same collision inside modules. Here no auxiliary collides: the import refuses the realized principle itself, because both exported copies are axioms.

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Lingua principale
Lean
Stelle
10.3k
Fork
1.1k
Merge medio
16h 45m
PR unite (30g)
361

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.