Skip to content

Commit

Permalink
fix: instantiate mvars of indices before instantiating fvars (leanpro…
Browse files Browse the repository at this point in the history
…ver#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 leanprover#3242 (again)
  • Loading branch information
arthur-adjedj authored Aug 16, 2024
1 parent 72f2e7a commit eb15c08
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Lean/Elab/Inductive.lean
Original file line number Diff line number Diff line change
Expand Up @@ -771,18 +771,27 @@ 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 := #[]
for i in [:views.size] do
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
Expand Down
8 changes: 8 additions & 0 deletions tests/lean/run/3242.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit eb15c08

Please sign in to comment.