fix: First miss-handling of free variables

main
Henrik Böving 2023-10-17 20:21:14 +02:00
parent f15d561411
commit bc9cba13b1
1 changed files with 35 additions and 31 deletions

View File

@ -15,33 +15,37 @@ def NameInfo.ofTypedName (n : Name) (t : Expr) : MetaM NameInfo := do
let env ← getEnv let env ← getEnv
return { name := n, type := ← prettyPrintTerm t, doc := ← findDocString? env n} return { name := n, type := ← prettyPrintTerm t, doc := ← findDocString? env n}
partial def typeToArgsType (e : Expr) : (Array ((Option Name) × Expr × BinderInfo) × Expr) := partial def argTypeTelescope {α : Type} (e : Expr) (k : Array ((Option Name) × Expr × BinderInfo) → Expr → MetaM α) : MetaM α :=
let helper := fun name type body data => go e #[]
where
go (e : Expr) (args : Array ((Option Name) × Expr × BinderInfo)) : MetaM α := do
let helper := fun name type body bi =>
-- Once we hit a name with a macro scope we stop traversing the expression -- Once we hit a name with a macro scope we stop traversing the expression
-- and print what is left after the : instead. The only exception -- and print what is left after the : instead. The only exception
-- to this is instances since these almost never have a name -- to this is instances since these almost never have a name
-- but should still be printed as arguments instead of after the :. -- but should still be printed as arguments instead of after the :.
if data.isInstImplicit && name.hasMacroScopes then if bi.isInstImplicit && name.hasMacroScopes then
let arg := (none, type, data) let arg := (none, type, bi)
let (args, final) := typeToArgsType (Expr.instantiate1 body (mkFVar ⟨name⟩)) Meta.withLocalDecl name bi type fun fvar => do
(#[arg] ++ args, final) go (Expr.instantiate1 body fvar) (args.push arg)
else if name.hasMacroScopes then else if name.hasMacroScopes then
(#[], e) k args e
else else
let arg := (some name, type, data) let arg := (some name, type, bi)
let (args, final) := typeToArgsType (Expr.instantiate1 body (mkFVar ⟨name⟩)) Meta.withLocalDecl name bi type fun fvar => do
(#[arg] ++ args, final) go (Expr.instantiate1 body fvar) (args.push arg)
match e.consumeMData with match e.consumeMData with
| Expr.forallE name type body binderInfo => helper name type body binderInfo | Expr.forallE name type body binderInfo => helper name type body binderInfo
| _ => (#[], e) | _ => k args e
def Info.ofConstantVal (v : ConstantVal) : MetaM Info := do def Info.ofConstantVal (v : ConstantVal) : MetaM Info := do
let (args, type) := typeToArgsType v.type argTypeTelescope v.type fun args type => do
let args ← args.mapM (fun (n, e, b) => do return Arg.mk n (← prettyPrintTerm e) b) let args ← args.mapM (fun (n, e, b) => do return Arg.mk n (← prettyPrintTerm e) b)
let nameInfo ← NameInfo.ofTypedName v.name type let nameInfo ← NameInfo.ofTypedName v.name type
match ← findDeclarationRanges? v.name with match ← findDeclarationRanges? v.name with
-- TODO: Maybe selection range is more relevant? Figure this out in the future -- TODO: Maybe selection range is more relevant? Figure this out in the future
| some range => return { | some range =>
return {
toNameInfo := nameInfo, toNameInfo := nameInfo,
args, args,
declarationRange := range.range, declarationRange := range.range,