fix: set default value of pp.instantiateMVars to true and make the option be effective (#4558)

Before, `pp.instantiateMVars` generally had no effect because most call
sites for the pretty printer instantiated metavariables first, but now
this functionality is entrusted upon the `pp.instantiateMVars` option.

This also has an effect in hovers, where metavariables can be unfolded
one assignment at a time. However, the goal state still sees all
metavariables instantiated due to the fact that the algorithm relies on
expression equality post-instantiation (see
`Lean.Widget.goalToInteractive`).

Closes #4406
This commit is contained in:
Kyle Miller 2024-07-02 15:59:44 -07:00 committed by GitHub
parent d39c4d6a1c
commit 3f2cf8bf27
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 33 additions and 4 deletions

View file

@ -80,7 +80,7 @@ register_builtin_option pp.numericTypes : Bool := {
descr := "(pretty printer) display types of numeric literals"
}
register_builtin_option pp.instantiateMVars : Bool := {
defValue := false -- TODO: default to true?
defValue := true
group := "pp"
descr := "(pretty printer) instantiate mvars before delaborating"
}

View file

@ -50,7 +50,7 @@ def makePopup : WithRpcRef InfoWithCtx → RequestM (RequestTask InfoPopup)
| ⟨i⟩ => RequestM.asTask do
i.ctx.runMetaM i.info.lctx do
let type? ← match (← i.info.type?) with
| some type => some <$> (ppExprTagged =<< instantiateMVars type)
| some type => some <$> ppExprTagged type
| none => pure none
let exprExplicit? ← match i.info with
| Elab.Info.ofTermInfo ti =>

View file

@ -69,8 +69,8 @@ builtin_initialize ppExt : EnvExtension PPFns ←
registerEnvExtension ppFnsRef.get
def ppExprWithInfos (ctx : PPContext) (e : Expr) : IO FormatWithInfos := do
let e := instantiateMVarsCore ctx.mctx e |>.1
if pp.raw.get ctx.opts then
let e := instantiateMVarsCore ctx.mctx e |>.1
return format (toString e)
else
try

View file

@ -76,7 +76,7 @@ where
def ppExprTagged (e : Expr) (explicit : Bool := false) : MetaM CodeWithInfos := do
if pp.raw.get (← getOptions) then
return .text (toString e)
return .text (toString (← instantiateMVars e))
let delab := open PrettyPrinter.Delaborator in
if explicit then
withOptionAtCurrPos pp.tagAppFns.name true do
@ -86,6 +86,11 @@ def ppExprTagged (e : Expr) (explicit : Bool := false) : MetaM CodeWithInfos :=
else
withOptionAtCurrPos pp.proofs.name true do
delab
let mut e := e
-- When hovering over a metavariable, we want to see its value, even if `pp.instantiateMVars` is false.
if explicit && e.isMVar then
if let some e' ← getExprMVarAssignment? e.mvarId! then
e := e'
let ⟨fmt, infos⟩ ← PrettyPrinter.ppExprWithInfos e (delab := delab)
let tt := TaggedText.prettyTagged fmt
let ctx := {

24
tests/lean/run/4406.lean Normal file
View file

@ -0,0 +1,24 @@
import Lean.Elab.Command
/-!
# Issue 4406: make sure `pp.instantiateMVars` has an effect
-/
open Lean Meta
set_option pp.mvars false
/-- info: ?_ -/
#guard_msgs in
set_option pp.instantiateMVars false in
run_meta do
let mvarId ← mkFreshExprMVar (.some (.const ``Nat []))
mvarId.mvarId!.assign (mkNatLit 0)
logInfo m!"{mvarId}"
/-- info: 0 -/
#guard_msgs in
run_meta do
let mvarId ← mkFreshExprMVar (.some (.const ``Nat []))
mvarId.mvarId!.assign (mkNatLit 0)
logInfo m!"{mvarId}"