Zulip Chat Archive

Stream: mathlib4

Topic: attributes in docs


Kevin Buzzard (Dec 04 2022 at 22:17):

I just noticed that docs4#CategoryTheory.Iso.symm doesn't indicate that the declaration is tagged with @[symm]. Is this expected? Note that later declarations on that web page are correctly shown with the @[simp] attribute attached.

Scott Morrison (Dec 05 2022 at 23:24):

I suspect this is an aspect of the whole "there is no uniform mechanism for asking which attributes are on a declaration" problem. :-(

Kevin Buzzard (Dec 06 2022 at 07:19):

I guess that it's not the end of the world if we can't see these in the docs. I've got used to them from mathlib3 but at the end of the day I'm not sure I'm particularly interested in seeing whether something is tagged symm apart from when I'm trying to debug teething troubles with symm.

Henrik Böving (Dec 06 2022 at 10:19):

The issue is that doc-gen4 needs to know about the environment extension and its types etc. In order to access this information. But since symm is not in core (I thibk?) That would require it to pull that in as a dependency cycle :/

Scott Morrison (Dec 06 2022 at 10:20):

It's increasingly clear we need a centralised store of which declarations have which attributes, no?

Henrik Böving (Dec 06 2022 at 10:21):

Thinking further though there might be a way for a package to provide extractor functions to doc-gen via some meta programming mechanism but I'm not sure.

And I agree that that would be the simpler solution

Filippo A. E. Nuccio (Dec 11 2024 at 10:33):

I am reviving this old topic to ask whether there is

  1. A list of all mathlib attributes?
  2. A reference about "what" an attribute is and how it works. I have tried looking at the doc for the ext one and it looks daunting. Is there perhaps an easier example to understand what lies behind the scene?

Yury G. Kudryashov (Dec 11 2024 at 11:15):

AFAIR, currently, an attribute is a syntax for calling any metaprogram. It is responsible for maintaining its data, and there is no uniform way to query all attributes. If you want to support specific attributes in your code, then you have to look at each of them and see how it stores its data (probably, in some env extension).

Filippo A. E. Nuccio (Dec 11 2024 at 11:19):

Thanks! As a matter of fact mine was more of a pedagogical question, aimed at understanding what is there under the hood rather than adding something to my code.

Filippo A. E. Nuccio (Dec 11 2024 at 11:19):

I'll try to keep on digging.

Edward van de Meent (Dec 11 2024 at 21:04):

Yury G. Kudryashov said:

and there is no uniform way to query all attributes.

doesn't #help attribute do this?

Kyle Miller (Dec 11 2024 at 21:06):

I think Yury was indirectly answering your question about "what" an attribute is @Filippo A. E. Nuccio, since the point of view of the metaprogrammer is as under the hood as you can get.

In short: if you write @[my_attr] before a declaration, then this causes a metaprogram to run at some stage of elaboration (configuration: docs#Lean.AttributeApplicationTime), and it can do anything.

A common sort of attribute is one that has an associated environment extension. The environment extension is often a table that keeps track of some collection of declarations, maybe with some associated data, and the data is saved to disk (in olean files) and loaded again by import.

Here are the key parts of the @[ext] system.

Then, the ext tactic reads this environment extension when trying to find an applicable ext lemma. If there were no ext tactic, there would still be a case for having a @[ext] attribute: we could delete the environment extension, and then @[ext] would just be responsible for realizing ext theorems for structures and for realizing ext_iff theorems from ext theorems. In that case, there would be no way to know for sure whether @[ext] was ever applied (all we can see is that there are the ext and ext_iff theorems available, which may have been written by hand).

Kyle Miller (Dec 11 2024 at 21:07):

@Edward van de Meent Yury means "there's no uniform way to query the data of attributes". You are right that you can query Lean for the list of all attributes that exist.

Kyle Miller (Dec 11 2024 at 21:08):

There's a core PR in progress for "attribute delaborators" to give an interface where attributes can read their own data and represent themselves as syntax, on a best-effort basis. lean4#6115

Filippo A. E. Nuccio (Dec 12 2024 at 12:36):

Wow, thanks a lot for the detailed explanation. It'll take time to digest, in the meanwhile I am really grateful.


Last updated: May 02 2025 at 03:31 UTC