-- Every named constant of the two libraries with a hash of its type (and of its value when it is a definition) in which -- an auxiliary constant counts by what it is, not by what it is called: private declarations, nested-proof lemmas, -- matchers and automatically named local instances get their names from the module and from the order of elaboration, -- and a fold changes both. A theorem met inside a term counts by its statement alone. open Lean namespace FoldCheck def isAux (n : Name) : Bool := n.components.any fun c => match c with | .str _ s => s.startsWith "_" || (s.startsWith "match_" && (s.drop 6).all Char.isDigit) || (s.startsWith "proof_" && (s.drop 6).all Char.isDigit) | .num _ _ => true | .anonymous => false def userName (n : Name) : Name := if isPrivateName n then ((n.components.dropWhile fun c => !c.isNum).drop 1).foldl (· ++ ·) `_private else n abbrev M := StateM (Std.HashMap Name UInt64 × Std.HashSet Name) mutual partial def constHash (env : Environment) (n : Name) : M UInt64 := do if let some h := (← get).1[n]? then return h if (← get).2.contains n then return hash (userName n) let some ci := env.find? n | return hash (userName n) modify fun (m, v) => (m, v.insert n) let th ← exprHash env ci.type let (k, vh) ← match ci with | .thmInfo _ => pure ((1 : UInt64), (0 : UInt64)) | .defnInfo d => do pure (2, ← exprHash env d.value) | .opaqueInfo d => do pure (3, ← exprHash env d.value) | .ctorInfo c => pure (4, c.cidx.toUInt64) | .inductInfo _ => pure (5, 0) | .recInfo _ => pure (6, 0) | _ => pure (7, 0) let h := mixHash k (mixHash th vh) modify fun (m, v) => (m.insert n h, v.erase n) return h partial def exprHash (env : Environment) (e : Expr) : M UInt64 := do for u in e.getUsedConstants do if isAux u then discard <| constHash env u let memo := (← get).1 let e' := e.replace fun | .const n ls => if isAux n then some (.const (Name.mkNum `_h (memo.getD n 0).toNat) ls) else none | _ => none return e'.hash end end FoldCheck open FoldCheck in #eval show CoreM Unit from do let env ← getEnv let mods := env.header.moduleNames let proj (m : Name) : Bool := m.getRoot == `NavierStokes || m.getRoot == `Euler let h ← IO.FS.Handle.mk "/out/content.tsv" IO.FS.Mode.write let mut st : Std.HashMap Name UInt64 × Std.HashSet Name := ({}, {}) let mut n := 0 for (c, ci) in env.constants.map₁.toList do let some idx := env.getModuleIdxFor? c | continue unless proj mods[idx.toNat]! do continue if isAux c then continue let (th, st1) := (exprHash env ci.type).run st st := st1 let mut v := "-" match ci with | .defnInfo d => let (vh, st2) := (exprHash env d.value).run st; st := st2; v := toString vh | .opaqueInfo d => let (vh, st2) := (exprHash env d.value).run st; st := st2; v := toString vh | _ => pure () let kind := match ci with | .thmInfo _ => "theorem" | .defnInfo _ => "def" | .axiomInfo _ => "axiom" | .opaqueInfo _ => "opaque" | .inductInfo _ => "inductive" | .ctorInfo _ => "ctor" | .recInfo _ => "rec" | .quotInfo _ => "quot" h.putStrLn s!"{c}\t{kind}\t{th}\t{v}" n := n + 1 IO.println s!"{n} named constants written"