Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
kim-em committed Oct 28, 2024
1 parent 8738783 commit 0c61e26
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions ImportGraph/RequiredModules.lean
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,25 @@ partial def Environment.transitivelyRequiredModules' (env : Environment) (module
for m in modules do
let mut r : NameSet := {}
for n in env.header.moduleData[(env.header.moduleNames.getIdx? m).getD 0]!.constNames do
-- This is messy: Mathlib is big enough that writing a recursive function causes a stack overflow.
-- So we use an explicit stack instead. We visit each constant twice:
-- once to record the constants transitively used by it,
-- and again to record the modules which defined those constants.
let mut stack : Array (ConstantInfo × Option NameSet) := #[⟨← getConstInfo n, none⟩]
while !stack.isEmpty do
let (ci, used?) := stack.back
stack := stack.pop
match used? with
| none =>
stack := stack.pop
if !c2m.contains ci.name then
let used := ci.getUsedConstantsAsSet
stack := stack.pop.push ⟨ci, some used⟩
stack := stack.push ⟨ci, some used⟩
for u in used do
if !c2m.contains u then
stack := stack.push ⟨← getConstInfo u, none⟩
| some used =>
let transitivelyUsed : NameSet := used.fold (init := used) (fun s u => s.union ((c2m.find? u).getD {}))
c2m := c2m.insert ci.name transitivelyUsed
stack := stack.pop
r := r.union ((c2m.find? n).getD {})
result := result.insert m r
return result
Expand Down

0 comments on commit 0c61e26

Please sign in to comment.