lean4 icon indicating copy to clipboard operation
lean4 copied to clipboard

`noConfusionType`/`noConfusion` for enum inductives get a hygienic universe parameter name

Open keithadler opened this issue 2 months ago • 0 comments

Prerequisites

  • [x] Check that your issue is not already filed
  • [x] Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
  • [x] Test your test case against the latest nightly release

Description

For an enumeration-style inductive (all constructors nullary), the generated noConfusionType and noConfusion declarations carry a universe parameter whose name has macro scopes. It shows up as v✝ in #print and in pp.all output, and the pp.all output of those declarations therefore cannot be re-parsed. Non-enum inductives and structures get a plain u.

Steps to Reproduce

import Lean
open Lean Meta

inductive E | a | b
inductive N | mk (n : Nat)

def lps (n : Name) : MetaM Unit := do
  let some ci := (← getEnv).find? n | throwError "no {n}"
  IO.println s!"{n}: {ci.levelParams.map (·.toString)}"

#eval lps ``E.noConfusionType   -- E.noConfusionType: [v._@.<file>.<n>._hygCtx._hyg.5]
#eval lps ``E.noConfusion       -- E.noConfusion:     [v._@.<file>.<n>._hygCtx._hyg.6]
#eval lps ``N.noConfusionType   -- N.noConfusionType: [u]

#print E.noConfusionType
-- @[reducible] def E.noConfusionType.{v._@.<file>.<n>._hygCtx._hyg.5} : Sort v✝ → E → E → Sort v✝ :=
--   fun P x y => noConfusionTypeEnum E.ctorIdx P x y

Expected behavior: a plain universe parameter name (u or v), as for non-enum inductives.

Actual behavior: a hygienic name; Sort v✝ in output.

Versions

  • Lean 4.35.0-nightly-2026-09-04, arm64-apple-darwin24.6.0 macOS
  • Lean 4.33.1 (stable) — same
  • Lean 4.20.0 — same (long-standing)

Additional Information

Found by a mechanical pp.all round-trip check over every declaration in Init and Std; all 86 noConfusion/noConfusionType declarations that failed to re-parse belong to enum inductives (Ordering, Float.Model.UnpackedFloat.Sign, ByteArray.utf8DecodeChar?.FirstByte, the Std.Time and Std.Http enums, …), and no non-enum one failed. Bool is unaffected. The cause is in Lean/Meta/Constructions/NoConfusion.lean: mkNoConfusionEnum creates the universe with let v ← mkFreshUserName \v(lines 377 and 404 on today's master), andmkFreshUserName returns a name with macro scopes. The non-enum path (mkNoConfusionType/mkNoConfusion, lines 81/216) instead reuses casesOnInfo.levelParams, which is why those come out as a plain u. A fresh *non-hygienic* name that avoids info.levelParams` would give the enum path the same behaviour.

Purely cosmetic in ordinary use — #check @E.noConfusionType re-instantiates the universe and prints u_1 — but it leaks in #print and breaks pp.all round-tripping.

Investigation assisted by Claude; repros run and checked manually.

Impact

Add :+1: to issues you consider important. If others are impacted by this issue, please ask them to add :+1: to it.

keithadler avatar Sep 04 '26 22:09 keithadler