Zulip Chat Archive

Stream: lean4

Topic: current mvar


Kenny Lau (Aug 12 2025 at 11:38):

This is just for fun basically, so,

import Lean
-- import Mathlib

open Lean Meta

def currentMVar : MetaM Nat := do
  let ⟨.num `_uniq n  mkFreshMVarId | throwError "aaa"
  return n

#eval currentMVar
/-
977 with import Mathlib
887 with import Lean
-/

I wrote a code to get the "current number of metavariables", and found that it's 887 with just import Lean, an 977 with import Mathlib

Kenny Lau (Aug 12 2025 at 11:38):

does this mean there are 90 unassigned metavariables in Mathlib?

Aaron Liu (Aug 12 2025 at 11:40):

no???

Aaron Liu (Aug 12 2025 at 11:41):

it means some instance synthesis is happening probably

Kenny Lau (Aug 12 2025 at 11:41):

what determines the number i get?

Aaron Liu (Aug 12 2025 at 11:41):

the metavariable counter resets for every file

Aaron Liu (Aug 12 2025 at 11:41):

the number you get is just sequential based on how many numbers

Aaron Liu (Aug 12 2025 at 11:42):

based on how many times the name generator was incremented

Aaron Liu (Aug 12 2025 at 11:46):

how many times it was incremented since the file was initialized

Robin Arnez (Aug 23 2025 at 13:30):

That number gets incremented all the time but never decremented. Most notably it gets used for mkFreshMVarId and mkFreshFVarId which get indirectly called tons of times in pretty much every meta program. You can see this by just querying the name generator twice:

import Lean

open Lean Meta

/-- info: { namePrefix := `_uniq, idx := 1147 } -/
#guard_msgs in
#eval (getNGen : MetaM Lean.NameGenerator)

/-- info: { namePrefix := `_uniq, idx := 2293 } -/
#guard_msgs in
#eval (getNGen : MetaM Lean.NameGenerator)

As you can see, the single #eval command is already responsible for 1146 increments (it's 1 in its initial state)


Last updated: Dec 20 2025 at 21:32 UTC