fix: instantiate mvars of indices before instantiating fvars (#4717)

When elaborating the headers of mutual indexed inductive types, mvars
have to be synthesized and instantiated before replacing the fvars
present there. Otherwise, some fvars present in uninstantiated mvars may
be missed and lead to an error later.
Closes #3242 (again)
This commit is contained in:
Arthur Adjedj 2024-08-16 17:19:48 +02:00 committed by GitHub
parent 72f2e7aab1
commit eb15c08ea0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View file

@ -771,6 +771,7 @@ private def mkInductiveDecl (vars : Array Expr) (views : Array InductiveView) :
let isUnsafe := view0.modifiers.isUnsafe
withRef view0.ref <| Term.withLevelNames allUserLevelNames do
let rs ← elabHeader views
Term.synthesizeSyntheticMVarsNoPostponing
withInductiveLocalDecls rs fun params indFVars => do
trace[Elab.inductive] "indFVars: {indFVars}"
let mut indTypesArray := #[]
@ -778,11 +779,19 @@ private def mkInductiveDecl (vars : Array Expr) (views : Array InductiveView) :
let indFVar := indFVars[i]!
Term.addLocalVarInfo views[i]!.declId indFVar
let r := rs[i]!
let type := r.type |>.abstract r.params |>.instantiateRev params
/- At this point, because of `withInductiveLocalDecls`, the only fvars that are in context are the ones related to the first inductive type.
Because of this, we need to replace the fvars present in each inductive type's header of the mutual block with those of the first inductive.
However, some mvars may still be uninstantiated there, and might hide some of the old fvars.
As such we first need to synthesize all possible mvars at this stage, instantiate them in the header types and only
then replace the parameters' fvars in the header type.
See issue #3242 (`https://github.com/leanprover/lean4/issues/3242`)
-/
let type ← instantiateMVars r.type
let type := type.replaceFVars r.params params
let type ← mkForallFVars params type
let ctors ← withExplicitToImplicit params (elabCtors indFVars indFVar params r)
indTypesArray := indTypesArray.push { name := r.view.declName, type, ctors }
Term.synthesizeSyntheticMVarsNoPostponing
let numExplicitParams ← fixedIndicesToParams params.size indTypesArray indFVars
trace[Elab.inductive] "numExplicitParams: {numExplicitParams}"
let indTypes := indTypesArray.toList

View file

@ -11,3 +11,11 @@ inductive R (F: αα → Prop) (a : α) : α → Prop where
inductive And₂ (F: αα → Prop) (a : α) : α → (Nat → α) → Nat → Prop where
| mk (b : α) (f : Nat → α) (n : Nat): R F a (f n) → F (f n) b → And₂ F a b f n
end
structure Salg (n k: Nat) where
D: Type
mutual
inductive Ins (salg: Salg n k) : salg.D → Prop
inductive Out (salg: Salg n k) : salg.D → Prop
end