Zulip Chat Archive

Stream: Is there code for X?

Topic: transitive deps of a def?


Alok Singh (Dec 13 2024 at 06:51):

the idea is to extract the necessary code to make a self-contained runnable example.

Say we have code to sum a function like

def sum (xs: List Int) : Int := xs.foldl (. + . ) 0

Then running the desired code would give a file that exposes the definitions of foldl and maybe of List.

This is for verilib.org, to allow 'atomizing' code so people can pull out single verified definitions instead of a whole project, at the cost of a bunch of essentially copy-pasted code coming along for the ride.

Chris Bailey (Dec 13 2024 at 07:38):

https://leanprover-community.github.io/mathlib4_docs/Lean/Util/FoldConsts.html#Lean.Expr.getUsedConstants

Chris Bailey (Dec 13 2024 at 07:39):

The ones that work on an element of Lean.ConstantInfo are more specifically "of a def".

Chris Bailey (Dec 13 2024 at 08:05):

May be more helpful since it's not obvious how to get names to resolve:

import Lean
open Lean Meta Elab
syntax "#go" ident : command

elab "#go" n:ident : command => do
  match ( Lean.resolveGlobalConst n) with
  | [] => throwError s!"no declar found for {n}"
  | hd :: _ =>
  let constInfo  Lean.getConstInfo hd
  let deps := constInfo.getUsedConstantsAsSet
  logInfo s!"deps := {deps.toArray}"

#go Nat.mul

Adam Topaz (Dec 13 2024 at 11:01):

That’s not the transitive deps, just the immediate deps

Johan Commelin (Dec 13 2024 at 11:10):

The ImportGraph package has some helper functions for the transitive closure of deps

Adam Topaz (Dec 13 2024 at 12:32):

Here is the relevant function: https://github.com/leanprover-community/import-graph/blob/ed3b856bd8893ade75cafe13e8544d4c2660f377/ImportGraph/RequiredModules.lean#L65

Adam Topaz (Dec 13 2024 at 12:34):

or for a single name instead of a NameSet: https://github.com/leanprover-community/import-graph/blob/ed3b856bd8893ade75cafe13e8544d4c2660f377/ImportGraph/RequiredModules.lean#L81

Chris Bailey (Dec 13 2024 at 18:37):

Adam Topaz said:

That’s not the transitive deps, just the immediate deps

Touche; it should recursively map over the results. I wasn't aware there was an import graph library, that's good to know.

Alok Singh (Dec 16 2024 at 18:39):

Works well (thanks Johan and Adam), now I'm looking to get line/col numbers for a definition.

Alok Singh (Dec 18 2024 at 00:57):

Is there a function that does this? Given a declaration, extract the line/column/file name where it was defined?

Adam Topaz (Dec 18 2024 at 01:04):

This info will be present in the info trees.

Alok Singh (Dec 18 2024 at 01:19):

Thanks, digging into how to construct/extract such a tree now. I found https://github.com/adamtopaz/lean_extras (how appropriate) and am reading through it now.

Damiano Testa (Dec 19 2024 at 11:12):

There was also this previous thread.


Last updated: May 02 2025 at 03:31 UTC