Skip to content

Commit 6f8c9ae

Browse files
kim-emleanprover-community-bot
authored andcommitted
feat: command to list declarations with long names (#3122)
This adds commands `#long_names` and `#long_instances`, useful for finding declarations with excessively long names. ~~I also add explicit names to some of the files that produce instances with names over 80 characters; can do more later.~~ Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
1 parent bc67aad commit 6f8c9ae

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

Mathlib.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,8 @@ import Mathlib.Lean.LocalContext
12421242
import Mathlib.Lean.Message
12431243
import Mathlib.Lean.Meta
12441244
import Mathlib.Lean.Meta.Simp
1245+
import Mathlib.Lean.Name
1246+
import Mathlib.Lean.SMap
12451247
import Mathlib.LinearAlgebra.AffineSpace.AffineEquiv
12461248
import Mathlib.LinearAlgebra.AffineSpace.AffineMap
12471249
import Mathlib.LinearAlgebra.AffineSpace.AffineSubspace
@@ -1839,6 +1841,7 @@ import Mathlib.Util.AssertNoSorry
18391841
import Mathlib.Util.AtomM
18401842
import Mathlib.Util.Export
18411843
import Mathlib.Util.IncludeStr
1844+
import Mathlib.Util.LongNames
18421845
import Mathlib.Util.MemoFix
18431846
import Mathlib.Util.Syntax
18441847
import Mathlib.Util.SynthesizeUsing

Mathlib/Lean/Name.lean

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/-
2+
Copyright (c) 2023 Scott Morrison. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Scott Morrison
5+
-/
6+
import Std.Data.HashMap
7+
import Mathlib.Lean.SMap
8+
import Mathlib.Lean.Expr.Basic
9+
10+
/-!
11+
# Additional functions on `Lean.Name`.
12+
13+
We provide `Name.getModule : Name → CoreM (Option Name)`,
14+
and `allNames` and `allNamesByModule`.
15+
-/
16+
17+
open Lean Meta Elab
18+
19+
private def isBlackListed (declName : Name) : CoreM Bool := do
20+
if declName.toString.startsWith "Lean" then return true
21+
let env ← getEnv
22+
pure $ declName.isInternal'
23+
|| isAuxRecursor env declName
24+
|| isNoConfusion env declName
25+
<||> isRec declName <||> isMatcher declName
26+
27+
/--
28+
Retrieve all names in the environment satisfying a predicate.
29+
-/
30+
def allNames (p : Name → Bool) : CoreM (Array Name) := do
31+
(← getEnv).constants.foldM (init := #[]) fun names n _ => do
32+
if p n && !(← isBlackListed n) then
33+
return names.push n
34+
else
35+
return names
36+
37+
/--
38+
Retrieve all names in the environment satisfying a predicate,
39+
gathered together into a `HashMap` according to the module they are defined in.
40+
-/
41+
def allNamesByModule (p : Name → Bool) : CoreM (Std.HashMap Name (Array Name)) := do
42+
(← getEnv).constants.foldM (init := Std.HashMap.empty) fun names n _ => do
43+
if p n && !(← isBlackListed n) then
44+
let some m ← findModuleOf? n | return names
45+
-- TODO use `Std.HashMap.modify` when we bump Std4 (or `alter` if that is written).
46+
match names.find? m with
47+
| some others => return names.insert m (others.push n)
48+
| none => return names.insert m #[n]
49+
else
50+
return names

Mathlib/Lean/SMap.lean

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/-
2+
Copyright (c) 2023 Scott Morrison. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Scott Morrison
5+
-/
6+
import Lean.Data.SMap
7+
8+
/-!
9+
# Extra functions on Lean.SMap
10+
-/
11+
12+
/-- Monadic fold over a staged map. -/
13+
def Lean.SMap.foldM {m : Type w → Type w} [Monad m] [BEq α] [Hashable α]
14+
(f : σ → α → β → m σ) (init : σ) (map : SMap α β) : m σ := do
15+
map.map₂.foldlM f (← map.map₁.foldM f init)

Mathlib/Util/LongNames.lean

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/-
2+
Copyright (c) 2023 Scott Morrison. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Scott Morrison
5+
-/
6+
import Mathlib.Lean.Name
7+
import Mathlib.Lean.Expr.Basic
8+
9+
/-!
10+
# Commands `#long_names` and `#long_instances`
11+
12+
For finding declarations with excessively long names.
13+
-/
14+
15+
open Lean Meta Elab
16+
17+
/-- Helper function for `#long_names` and `#long_instances`. -/
18+
def printNameHashMap (h : Std.HashMap Name (Array Name)) : IO Unit :=
19+
for (m, names) in h.toList do
20+
IO.println "----"
21+
IO.println $ m.toString ++ ":"
22+
for n in names do
23+
IO.println n
24+
25+
/--
26+
Lists all declarations with a long name, gathered according to the module they are defined in.
27+
Use as `#long_names` or `#long_names 100` to specify the length.
28+
-/
29+
elab "#long_names " N:(num)? : command =>
30+
Command.runTermElabM fun _ => do
31+
let N := N.map TSyntax.getNat |>.getD 50
32+
let namesByModule ← allNamesByModule (fun n => n.toString.length > N)
33+
let namesByModule := namesByModule.filter fun m _ => m.getRoot.toString = "Mathlib"
34+
printNameHashMap namesByModule
35+
36+
/--
37+
Lists all instances with a long name beginning with `inst`,
38+
gathered according to the module they are defined in.
39+
This is useful for finding automatically named instances with absurd names.
40+
41+
Use as `#long_names` or `#long_names 100` to specify the length.
42+
-/
43+
elab "#long_instances " N:(num)?: command =>
44+
Command.runTermElabM fun _ => do
45+
let N := N.map TSyntax.getNat |>.getD 50
46+
let namesByModule ← allNamesByModule
47+
(fun n => n.getString.startsWith "inst" && n.getString.length > N)
48+
let namesByModule := namesByModule.filter fun m _ => m.getRoot.toString = "Mathlib"
49+
printNameHashMap namesByModule

0 commit comments

Comments
 (0)