lean4-htt/tests/elab/ppMVars.lean
Kyle Miller 8f1c18d9f4
feat: pretty print level metavariables using index (#13030)
This PR improves pretty printing of level metavariables: they now print
with a per-definition index rather than their per-module internal
identifiers. Furthermore, `+` is printed uniformly in level expressions
with surrounding spaces. **Breaking metaprogramming change:** level
pretty printing should use `delabLevel` or `MessageData.ofLevel`;
functions such as `format` or `toString` do not have access to the
indices, since they are stored in the current metacontext. Absent index
information, metavariables print with the raw internal identifier as
`?_mvar.nnn`. **Note:** The heartbeat counter also increases quicker due
to counting allocations that record level metavariable indices. In some
tests we needed to increase `maxHeartbeats` by 20–50% to compensate,
without a corresponding slowdown.
2026-04-01 22:34:29 +00:00

161 lines
2.9 KiB
Text

import Lean.Elab.BuiltinNotation
import Lean.Meta.Basic
/-!
# Testing `pp.mvars`
-/
open Lean Meta
/-!
Default values
-/
/-- info: ?a : Nat -/
#guard_msgs in #check (?a : Nat)
/--
trace: this : PUnit.{?u.4}
⊢ True
-/
#guard_msgs (trace, drop all) in
set_option pp.universes true in
example : True := by
have : PUnit := PUnit.unit
trace_state
sorry
/-!
No such mvar, pretty print using the mvarid rather than the index.
-/
/-- info: ?_mvar.222222 -/
#guard_msgs in #eval do
let e := Expr.mvar (.mk (.num `_uniq 222222))
logInfo m!"{e}"
/-!
Turning off `pp.mvars`
-/
section
set_option pp.mvars false
/-- info: ?_ : Nat -/
#guard_msgs in #check (?a : Nat)
/-- info: ?_ : Nat -/
#guard_msgs in #check (_ : Nat)
/-- trace: ⊢ Sort _ -/
#guard_msgs (trace, drop all) in
example : (by_elab do return .sort (.mvar (.mk (.num `_uniq 1)))) := by
trace_state
sorry
/-- trace: ⊢ Type _ -/
#guard_msgs (trace, drop all) in
example : Type _ := by
trace_state
sorry
end
/-!
Turning off `pp.mvars.levels`
-/
section
set_option pp.mvars.levels false
/-- info: ?a : Nat -/
#guard_msgs in #check (?a : Nat)
/-- info: ?m.1 : Nat -/
#guard_msgs in #check (_ : Nat)
/-- trace: ⊢ Sort _ -/
#guard_msgs (trace, drop all) in
example : (by_elab do return .sort (.mvar (.mk (.num `_uniq 1)))) := by
trace_state
sorry
/-- trace: ⊢ Type _ -/
#guard_msgs (trace, drop all) in
example : Type _ := by
trace_state
sorry
end
/-!
Turning off `pp.mvars.anonymous`
-/
section
set_option pp.mvars.anonymous false
/-- info: ?a : Nat -/
#guard_msgs in #check (?a : Nat)
/-- info: ?_ : Nat -/
#guard_msgs in #check by_elab do
-- Control the mvarId with something that's too big to happen naturally:
let mvarId : MVarId := .mk (.num `_uniq 222222222)
let lctx ← getLCtx
let type := mkConst ``Nat
Lean.MonadMCtx.modifyMCtx fun mctx => mctx.addExprMVarDecl mvarId .anonymous lctx {} type .natural 0
return .mvar mvarId
/-- trace: ⊢ Sort _ -/
#guard_msgs (trace, drop all) in
example : (by_elab do return .sort (.mvar (.mk (.num `_uniq 1)))) := by
trace_state
sorry
/-- trace: ⊢ Type _ -/
#guard_msgs (trace, drop all) in
example : Type _ := by
trace_state
sorry
end
/-!
Turning off `pp.mvars` and turning on `pp.mvars.withType`.
-/
section
set_option pp.mvars false
set_option pp.mvars.withType true
/-- info: (?_ : Nat) : Nat -/
#guard_msgs in #check (?a : Nat)
/-- info: (?_ : Nat) : Nat -/
#guard_msgs in #check (_ : Nat)
end
/-!
Turning on `pp.mvars.withType`.
-/
section
set_option pp.mvars.withType true
/-- info: (?a : Nat) : Nat -/
#guard_msgs in #check (?a : Nat)
end
/-!
Delayed assignment metavariables respecting `pp.mvars.anonymous`
-/
section
set_option pp.mvars.anonymous false
/-- info: fun x => ?a : (x : Nat) → ?_ x -/
#guard_msgs in #check fun _ : Nat => ?a
set_option pp.mvars.delayed true
/-- info: fun x => ?_ x : (x : Nat) → ?_ x -/
#guard_msgs in #check fun _ : Nat => ?a
end