@@ -16,6 +16,7 @@ to entries in external mathematical databases:
1616
1717* `@[stacks TAG]` — [ Stacks Project ] (https://stacks.math.columbia.edu/tags)
1818* `@[kerodon TAG]` — [ Kerodon ] (https://kerodon.net/tag/)
19+ * `@[pibase ID]` — [ π-Base ] (https://topology.pi-base.org/)
1920* `@[wikidata QID]` — [ Wikidata ] (https://www.wikidata.org)
2021
2122 Each attribute records the cross-reference in an environment extension and appends
@@ -35,6 +36,7 @@ namespace Mathlib.CrossRef
3536/-- The supported external databases -/
3637inductive Database where
3738 | kerodon
39+ | pibase
3840 | stacks
3941 | wikidata
4042 deriving BEq, Hashable, Ord
@@ -44,18 +46,33 @@ namespace Database
4446/-- The base URL for an external database's tag pages. Always ends with `/`. -/
4547def url : Database → String
4648 | .kerodon => "https://kerodon.net/tag/"
49+ | .pibase => "https://topology.pi-base.org/"
4750 | .stacks => "https://stacks.math.columbia.edu/tag/"
4851 | .wikidata => "https://www.wikidata.org/wiki/"
4952
53+ /-- The URL for an external database entry. -/
54+ def entryUrl (db : Database) (id : String) : String :=
55+ match db with
56+ | .pibase =>
57+ let path := match id.toList with
58+ | 'P' :: _ => "properties/"
59+ | 'S' :: _ => "spaces/"
60+ | 'T' :: _ => "theorems/"
61+ | _ => ""
62+ db.url ++ path ++ id
63+ | _ => db.url ++ id
64+
5065/-- The display label used in docstring links and trace output. -/
5166def label : Database → String
5267 | .kerodon => "Kerodon Tag"
68+ | .pibase => "π-Base"
5369 | .stacks => "Stacks Tag"
5470 | .wikidata => "Wikidata"
5571
5672/-- A lowercase short name for the given database. Useful when exporting to JSON. -/
5773def shortName : Database → String
5874 | .kerodon => "kerodon"
75+ | .pibase => "pibase"
5976 | .stacks => "stacks"
6077 | .wikidata => "wikidata"
6178
@@ -92,7 +109,7 @@ This is the database-agnostic core of every cross-reference attribute's `add` ha
92109def addCrossRefDoc (db : Database) (decl : Name) (idStr comment : String) : CoreM Unit := do
93110 let oldDoc := (← findDocString? (← getEnv) decl).getD ""
94111 let commentInDoc := if comment.isEmpty then "" else s! " ({ comment} )"
95- let link := s! "[{ db.label} { idStr} ]({ db.url }{ idStr} ){ commentInDoc} "
112+ let link := s! "[{ db.label} { idStr} ]({ db.entryUrl idStr} ){ commentInDoc} "
96113 addDocStringCore decl <| "\n\n " .intercalate ([oldDoc, link].filter (· != "" ))
97114 addTagEntry decl db idStr comment
98115
@@ -169,6 +186,49 @@ def wikidataIdNoAntiquot : Parser := {
169186def wikidataIdParser : Parser :=
170187 withAntiquot (mkAntiquot "wikidataId" wikidataIdKind) wikidataIdNoAntiquot
171188
189+ /-! ### π-Base parser -/
190+
191+ /-- `pibaseId` is the node kind of π-Base identifiers: one of the letters `P`, `S`, or `T`,
192+ followed by exactly six digits. -/
193+ abbrev pibaseIdKind : SyntaxNodeKind := `pibaseId
194+
195+ /-- The main parser for π-Base identifiers: it accepts canonical property, space, and theorem
196+ identifiers, such as `P000001`, `S000023`, and `T000010`. -/
197+ def pibaseIdFn : ParserFn := fun c s =>
198+ let i := s.pos
199+ let s := takeWhileFn (fun c => c.isAlphanum) c s
200+ if s.hasError then
201+ s
202+ else if s.pos == i then
203+ ParserState.mkError s "π-Base id"
204+ else
205+ let id := c.extract i s.pos
206+ match id.toList with
207+ | kind :: rest =>
208+ if kind != 'P' && kind != 'S' && kind != 'T' then
209+ ParserState.mkUnexpectedError s
210+ "π-Base ids must start with P, S, or T."
211+ else if rest.length != 6 then
212+ ParserState.mkUnexpectedError s "π-Base ids must have exactly six digits after P/S/T."
213+ else if !rest.all Char.isDigit then
214+ ParserState.mkUnexpectedError s
215+ "π-Base ids must consist of P, S, or T followed by six digits."
216+ else
217+ mkNodeToken pibaseIdKind i true c s
218+ | _ =>
219+ ParserState.mkUnexpectedError s
220+ "π-Base ids must consist of P, S, or T followed by six digits."
221+
222+ @ [inherit_doc pibaseIdFn]
223+ def pibaseIdNoAntiquot : Parser := {
224+ fn := pibaseIdFn
225+ info := mkAtomicInfo "pibaseId"
226+ }
227+
228+ @ [inherit_doc pibaseIdFn]
229+ def pibaseIdParser : Parser :=
230+ withAntiquot (mkAntiquot "pibaseId" pibaseIdKind) pibaseIdNoAntiquot
231+
172232end Mathlib.CrossRef
173233
174234open Mathlib.CrossRef
@@ -183,6 +243,11 @@ def Lean.TSyntax.getWikidataId (stx : TSyntax wikidataIdKind) : CoreM String :=
183243 let some val := Syntax.isLit? wikidataIdKind stx | throwError "Malformed Wikidata id"
184244 return val
185245
246+ /-- Extract the underlying identifier as a string from a `pibaseId` node. -/
247+ def Lean.TSyntax.getPibaseId (stx : TSyntax pibaseIdKind) : CoreM String := do
248+ let some val := Syntax.isLit? pibaseIdKind stx | throwError "Malformed π-Base id"
249+ return val
250+
186251namespace Lean.PrettyPrinter
187252
188253namespace Formatter
@@ -195,6 +260,10 @@ namespace Formatter
195260@ [combinator_formatter wikidataIdNoAntiquot] def wikidataIdNoAntiquot.formatter :=
196261 visitAtom wikidataIdKind
197262
263+ /-- The formatter for π-Base identifier syntax. -/
264+ @ [combinator_formatter pibaseIdNoAntiquot] def pibaseIdNoAntiquot.formatter :=
265+ visitAtom pibaseIdKind
266+
198267end Formatter
199268
200269namespace Parenthesizer
@@ -205,6 +274,9 @@ namespace Parenthesizer
205274/-- The parenthesizer for Wikidata identifier syntax. -/
206275@ [combinator_parenthesizer wikidataIdNoAntiquot] def wikidataIdAntiquot.parenthesizer := visitToken
207276
277+ /-- The parenthesizer for π-Base identifier syntax. -/
278+ @ [combinator_parenthesizer pibaseIdNoAntiquot] def pibaseIdAntiquot.parenthesizer := visitToken
279+
208280end Lean.PrettyPrinter.Parenthesizer
209281
210282namespace Mathlib.CrossRef
@@ -265,6 +337,28 @@ initialize Lean.registerBuiltinAttribute {
265337 applicationTime := .beforeElaboration
266338}
267339
340+ /-! ### π-Base attribute -/
341+
342+ /-- The `pibase` attribute.
343+ Use it as `@[pibase P000001 "Optional comment"]` to associate a Mathlib declaration with
344+ the corresponding [ π-Base ] (https://topology.pi-base.org/) property, space, or theorem.
345+
346+ The identifier must start with `P`, `S`, or `T`, followed by exactly six digits.
347+ -/
348+ syntax (name := pibaseTag) "pibase" pibaseIdParser (ppSpace str)? : attr
349+
350+ initialize Lean.registerBuiltinAttribute {
351+ name := `pibaseTag
352+ descr := "Apply a π-Base identifier to a declaration."
353+ add := fun decl stx _attrKind => do
354+ let (id, comment) ← match stx with
355+ | `(attr| pibase $id $[$comment]?) => pure (id, comment)
356+ | _ => throwUnsupportedSyntax
357+ addCrossRefDoc .pibase decl (← id.getPibaseId) ((comment.map (·.getString)).getD "" )
358+ -- docstrings are immutable once an asynchronous elaboration task has been started
359+ applicationTime := .beforeElaboration
360+ }
361+
268362end Mathlib.CrossRef
269363
270364/-- Returns the array of `Tag`s in the environment, sorted alphabetically by tag. -/
@@ -291,7 +385,7 @@ def traceCrossRefs (db : Database) (verbose : Bool := false) :
291385 let (parL, parR) := if d.comment.isEmpty then ("" , "" ) else (" (" , ")" )
292386 let cmt := parL ++ d.comment ++ parR
293387 msgs := msgs.push
294- m! "[{ db.label} { d.tag} ]({ db.url ++ d.tag} ) \
388+ m! "[{ db.label} { d.tag} ]({ db.entryUrl d.tag} ) \
295389 corresponds to declaration '{ .ofConstName d.declName} '.{ cmt} "
296390 if verbose then
297391 let dType := ((env.find? d.declName).getD default).type
@@ -336,4 +430,16 @@ or declaration type (for definitions, structures, instances, etc.) after each su
336430elab (name := wikidataTags) "#wikidata_tags" tk:("!" )? : command =>
337431 traceCrossRefs .wikidata (tk.isSome)
338432
433+ /-- The `#pibase_tags` command retrieves all declarations that have the `pibase` attribute.
434+
435+ For each found declaration, it prints a line
436+ ```
437+ 'declaration_name' corresponds to tag 'declaration_tag'.
438+ ```
439+ The variant `#pibase_tags!` also adds the theorem statement (for theorems)
440+ or declaration type (for definitions, structures, instances, etc.) after each summary line.
441+ -/
442+ elab (name := pibaseTags) "#pibase_tags" tk:("!" )? : command =>
443+ traceCrossRefs .pibase (tk.isSome)
444+
339445end Mathlib.CrossRef
0 commit comments