Repository navigation
Expand file tree
/
Copy pathUnused.lean
More file actions
51 lines (49 loc) · 2.33 KB
/
Copy pathUnused.lean
File metadata and controls
51 lines (49 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import FeProofs
/-! Lists the declarations of `FeProofs.*` modules that no capstone theorem uses: a contract's
Stele certificate (`fe…ContractCorrect`), a refinement certificate (`fe…Refines…`) or a
conservation theorem (`…conserves…`).
Run: `lake env lean scripts/Unused.lean`. -/
open Lean Meta
def closureOf (env : Environment) (roots : Array Name) : NameSet := Id.run do
let mut seen : NameSet := {}
let mut stack := roots.toList
while !stack.isEmpty do
match stack with
| [] => break
| n :: rest =>
stack := rest
if seen.contains n then continue
seen := seen.insert n
if let some ci := env.find? n then
for m in ci.getUsedConstantsAsSet do
unless seen.contains m do stack := m :: stack
return seen
#eval show CoreM Unit from do
let env ← getEnv
let roots := env.constants.toList.filterMap fun (n, _) =>
let last := n.lastComponentAsString
let has (part : String) := (last.splitOn part).length > 1
if (`FeProofs).isPrefixOf n && (last.endsWith "ContractCorrect" || last.startsWith "fe" && has "Refines"
|| has "conserves") then some n else none
let used := closureOf env roots.toArray
let mut byMod : Std.HashMap Name (Array (Nat × Name)) := {}
let mut total := 0
for (n, ci) in env.constants.toList do
if ci matches .recInfo _ then continue
if ci.type.isConstOf ``ParserDescr || ci.type.isConstOf ``TrailingParserDescr then continue
let some idx := env.getModuleIdxFor? n | continue
let mod := env.header.moduleNames[idx.toNat]!
unless (`FeProofs).isPrefixOf mod do continue
if (`FeProofs.RDTactic).isPrefixOf n then continue
if used.contains n || n.isInternalDetail || isAuxRecursor env n || isNoConfusion env n then continue
if (← isMatcher n) || isPrivateName n && n.isInternal then continue
-- equation lemmas Lean generates on demand (`f.eq_1`, `f.eq_def`)
if isEqnLikeSuffix n.lastComponentAsString then continue
let line := (← findDeclarationRanges? n).map (·.range.pos.line) |>.getD 0
byMod := byMod.insert mod ((byMod.getD mod #[]).push (line, n))
total := total + 1
IO.println s!"unused: {total}"
for (mod, ds) in byMod.toArray.qsort (fun a b => a.1.toString < b.1.toString) do
IO.println s!"== {mod} ({ds.size})"
for (l, n) in ds.qsort (fun a b => a.1 < b.1) do
IO.println s!" {l}\t{n}"