Zulip Chat Archive

Stream: lean4

Topic: Ordering of modules: `moduleNames` vs `ModuleIdx`


Damiano Testa (Jan 22 2025 at 21:06):

The docs#Lean.EnvironmentHeader contains a moduleNames field that is an Array of Names containing all (transitively) imported module names of the current file.

Damiano Testa (Jan 22 2025 at 21:06):

For most functions, though, Lean refers to imported modules not by Name, but by docs#Lean.ModuleIdx, a type alias of Nat.

Damiano Testa (Jan 22 2025 at 21:06):

This means that it is easy to find the index of a module, but you then have to convert this index to a Name in some way.

Damiano Testa (Jan 22 2025 at 21:07):

Empirically, the order of the module names in moduleNames is exactly the ModuleIdx of the module.

Damiano Testa (Jan 22 2025 at 21:07):

Was I lucky? :smile:

Damiano Testa (Jan 22 2025 at 21:07):

Can I count on this?

Damiano Testa (Jan 22 2025 at 21:07):

Should this be documented?

Damiano Testa (Jan 22 2025 at 21:07):

(This question came from the comments to #20908 that exploits this "feature".)

Damiano Testa (Jan 22 2025 at 22:40):

Answered during office hours: the correspondence of ModuleIdx and "index in the array of module names" has been around for a long time and is also used by doc-gen.

Damiano Testa (Jan 22 2025 at 23:00):

lean4#6749

Damiano Testa (Jan 23 2025 at 15:54):

Kyle mentioned also docs#Lean.findModuleOf?, which was a perfect fit for my application.


Last updated: May 02 2025 at 03:31 UTC