1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! The `theorems` feature: the theorem-like environments of mathematical
//! writing. These are numbered, titled block environments such as `theorem`,
//! `lemma` and `definition`, and the `proof` environment, whose title can refer
//! back to the statement it proves. Every environment is declared from the
//! feature's configuration ([`TheoremsConfig`]), so a host can add environments
//! of its own (an `exercise`) or remove shipped ones.
//!
//! # What an author writes
//!
//! ```text
//! \begin{lemma}[Key lemma]\label{lem:key}
//! Every bounded monotone sequence converges.
//! \end{lemma}
//!
//! \begin{proof}[*lem:key]
//! By completeness.
//! \end{proof}
//! ```
//!
//! prints, on the text backend,
//!
//! ```text
//! Lemma 1 (Key lemma). Every bounded monotone sequence converges.
//!
//! Proof (Lemma 1). By completeness. □
//! ```
//!
//! Each environment is one **theorem kind** (`lemma` above). It takes an
//! optional title in brackets, then two fields in any order, `\label{…}`
//! (repeatable) and `\noproofref` (which says that no proof of the statement
//! is coming), and a body laid out as blocks ([`elements`]). A proof's title
//! written `[*lem:key]` is a **back-reference**: it refers to the statement
//! labeled `lem:key` and shows that statement's word and number in the proof's
//! heading; `[**lem:key]` refers to the same statement and shows nothing in
//! the heading. The
//! language guide describes these environments for authors under
//! [Theorems](crate::guide::language_syntax#theorems).
//!
//! # Theorem kinds and theorem classes
//!
//! Every theorem kind belongs to one **theorem class** ([`TheoremClass`]),
//! which holds what a family of kinds shares: whether its kinds are numbered
//! and in which counter, the heading template, the closing mark, and whether a
//! title beginning with `*` is read as a back-reference. A kind
//! ([`TheoremKind`]) adds its words (`Lemma`, `Lemmas`, and variants such as
//! the abbreviation `Lem.`) and whether each of its statements expects a
//! proof. The default configuration ([`TheoremsConfig::new`]) declares:
//!
//! - `theorem`, `proposition`, `lemma`, `corollary` and `conjecture` in the
//! class `theoremlike`, and `definition` and `remark` in the class
//! `definitionlike`, all numbered in one shared counter;
//! - `proof` in the class `prooflike`: unnumbered, closed by the mark `□`,
//! its title read as a back-reference.
//!
//! The feature's code refers to no kind and no class by name: a host's
//! `exercise` is an environment in its own right, declared in the
//! configuration. The configuration is fixed when the registry is built
//! (see [settings, options and
//! configuration](crate::guide::concepts_overview#settings-options-and-configuration)).
//!
//! # Numbers, labels and back-references
//!
//! Every theorem is one [reference point](crate::model::aspects::RefPoint) of
//! the reference kind `theorem` (see [reference points, labels and
//! anchors](crate::guide::concepts_overview#reference-points-labels-and-anchors)).
//! A theorem of a numbered kind is numbered by the
//! [`numbering`](crate::stdlib::features::numbering) feature in its class's
//! [counter](crate::stdlib::features::numbering::CounterDecl) and shown in a
//! [counter style](crate::stdlib::features::numbering::CounterStyleDecl) of
//! its kind, whose words are the kind's ([`numbers`]): `\ref{lem:key}` reads
//! `Lemma~1`; in a document that numbers theorems within sections it reads
//! `Lemma~2.3` on every output except the recomposed LaTeX, where LaTeX
//! numbers the theorems (see [the
//! limitation](latex#limitation-theorems-numbered-within-sections)). Its anchor is the
//! environment's name and its number, `lemma-1`. A theorem of an unnumbered
//! kind accepts no label and has no anchor.
//!
//! A back-reference is a [reference site](crate::model::aspects::RefSite),
//! which the [references](crate::stdlib::features::refs) feature resolves as
//! it resolves `\ref`. [`title_reading`] is the function that decides whether
//! a title is a back-reference. On a class that reads back-references, a title
//! that begins with a star is kept as typed: its reader places it under an
//! opaque wrapper, so that its label meets the label of a `\label` as written,
//! and `[*lem:a--b]` refers to `\label{lem:a--b}`. Every other title is content
//! (see [a starred title is kept as
//! typed](elements#a-starred-title-is-kept-as-typed)).
//!
//! # The check of proofs
//!
//! A statement of a kind that [expects a proof](TheoremKind::expects_proof)
//! (by default `theorem`, `proposition`, `lemma` and `corollary`) should have
//! a proof whose back-reference refers to it, unless it carries `\noproofref`.
//! The feature's check stage ([`check`]) reports a statement that has neither
//! as the Warning `flm.theorems.missing-proof`, and a title that begins with a
//! star but is not a well-formed back-reference as the Error
//! `flm.theorems.bad-relation-ref`. The stage is a member of the
//! [`flm.validated`](crate::stdlib::VALIDATED) collection, which every
//! standard workflow demands, so it runs, with the same reports, under the
//! `check` workflow and under every render. It reads the document tree: a
//! theorem inside a user definition that the plan keeps unexpanded is not
//! checked (see [`check`]).
//!
//! # What the feature provides to the pipeline
//!
//! - **Elements.** [`THEOREM`] (`flm.theorems.theorem`), with one element
//! value per configured kind, available in text mode. Three synthesized
//! elements, [`NAME_ELEMENT`], [`TITLE_POINTER`] and [`RELATION`], hold the
//! parts of a theorem's heading ([`heading`]); no document can write them.
//! - **The general lowering rules**, each the default on every backend:
//! [`THEOREM_RULE`] and the rules of the three heading elements,
//! [`NAME_RULE`], [`TITLE_POINTER_RULE`] and [`RELATION_RULE`]. A theorem
//! becomes a `flm.pres.block` of role `flm.theorems.theorem` whose kind
//! tokens are the class's name and the kind's (`theoremlike lemma`), with
//! its heading as a run-in decoration at the head of its first paragraph
//! and, for a class with a closing mark, the mark as a final decoration at
//! the end of its last paragraph ([`rules`]; see
//! [decorations](crate::stdlib::backends::pres::Decoration) and [roles and
//! kinds](crate::stdlib::backends::pres::payload)). The
//! heading is the class's heading template, with the kind's word and number
//! in a span of role `flm.theorems.name` and the title in a span of role
//! `flm.theorems.title`; the title is lowered where the heading shows it,
//! never copied ([`heading`]). The text and html backends write a stop
//! after the heading (their render option `run_in_stop`, a period by
//! default; see
//! [`TextRenderOptions::run_in_stop`](crate::stdlib::backends::text::stages::TextRenderOptions::run_in_stop)
//! and
//! [`HtmlRenderOptions::run_in_stop`](crate::stdlib::backends::html::feature::HtmlRenderOptions::run_in_stop))
//! unless the heading already ends in punctuation; the text backend writes a
//! non-breaking space before the closing mark.
//! At every theorem the general rule requires the feature's style
//! identifier `flm.theorems.style` ([`html::STYLE`]).
//! - **On the `html` backend**, the stylesheet `flm.theorems.style`
//! ([`html`]), which the requirement brings into a page holding a theorem
//! and which sets the kind's word and number apart from the title.
//! - **On the `latex-rendered` backend**, the definition piece
//! `flm.latex.def.flmx.theorems` ([`latex_rendered`]), which the same
//! requirement brings into the style file and which registers the look of
//! each class of theorem.
//! - **On the `latex-recomposed` backend**, rules of their own ([`latex`]): a
//! theorem is written back as the environment the author wrote, and the
//! style file declares each kind the document uses. A proof's
//! back-reference is written in one of two forms, which the backend's render
//! option `resolved_proof_refs` chooses: as the author wrote it, for the
//! `phfthm` LaTeX package to read (the default), or resolved into the
//! proof's title, over the `amsthm` package alone. The rendering guide
//! describes this output under [Theorems in the recomposed
//! LaTeX](crate::guide::rendering#theorems-in-the-recomposed-latex).
//! - **Counters and counter styles** ([`numbers`]), contributed to the
//! numbering feature's service slots.
//! - **A check stage**, [`CHECK_PROOFS`] (`flm.theorems.check-proofs`),
//! providing the capability of the same name, a member of the
//! [`flm.validated`](crate::stdlib::VALIDATED) collection ([`check`]).
//! - **A configuration**, [`TheoremsConfig`], whose fields are the feature's
//! settings keys. A build hook ([`LateInit`](crate::core::registry::LateInit))
//! compiles the heading templates, the closing marks and the kinds' words
//! once the language is built; a text that does not compile fails the
//! registry build.
//!
//! # Interactions with other features
//!
//! - **The numbering feature.** Without it, no theorem is numbered: a heading
//! shows the kind's word alone (`Theorem`), and a theorem's anchor comes from
//! its first label.
//! - **The references feature.** It resolves labels and back-references and
//! computes anchors. Without it, no label resolves, a theorem has no anchor,
//! a back-reference shows its label's text, and the check reports no missing
//! proof.
//! - The feature reads the tables of these two features optionally, and
//! declares them through peer declarations
//! ([`PeerName`](crate::core::registry::PeerName)); it declares the same
//! way the argument role that the references feature gives the argument of
//! a theorem's `\label` field
//! ([`DEFINED_LABEL_ARG_ROLE`](crate::stdlib::features::refs::DEFINED_LABEL_ARG_ROLE)).
//! It therefore builds and renders in a registry that holds neither. The rules of the `latex-recomposed`
//! backend are registered under peer declarations of their backend and of
//! the LaTeX family's elements; in a registry without them, those rules are
//! set aside (excluded from rule selection; see
//! [`PeerName`](crate::core::registry::PeerName)).
//! - **User definitions.** A theorem written in a user definition's template is
//! checked and numbered where the template is expanded in the tree. The
//! title's reader sees a star written in the template (`[*#1]`), and keeps
//! that title as typed. It does not see a star that arrives through a
//! placeholder (a template `[#1]` used as `\pf{*lem:a--b}`): that title is
//! laid out as content, and a label holding `--`, quotes, an apostrophe or
//! `...` does not resolve through it. A
//! theorem in the template of a kept definition (a user definition whose use
//! sites the plan leaves in the tree; see [kept
//! definitions](crate::stdlib::features::userdefs::kept)) whose title holds
//! a placeholder where a back-reference's star could be is expanded at each
//! use site instead ([`TitleReading::Unknown`]).
//!
//! # Map of the items
//!
//! - The feature: [`Theorems`], [`feature`], [`FEATURE`].
//! - The configuration: [`TheoremsConfig`], [`TheoremClass`], [`TheoremKind`]
//! and their constants ([`DEFAULT_HEADING`], [`DEFAULT_CLOSING_MARK`],
//! [`DEFAULT_LABEL_TYPES`], [`LOWERCASE_VARIANT`], [`CAPITAL_VARIANT`],
//! [`ABBREVIATED_VARIANT`]).
//! - The element and its reading: [`elements`] ([`THEOREM`],
//! [`TheoremElement`], [`title_reading`], [`TitleReading`],
//! [`has_noproofref`], [`theorem_args`]).
//! - The heading: [`heading`]. The general rule of a theorem: [`rules`].
//! - The counters and counter styles: [`numbers`].
//! - The check of proofs: [`check`] ([`CHECK_PROOFS`]).
//! - The conditions: [`MissingProof`], [`BadRelationRef`], [`PointerRefused`],
//! [`StageFailed`].
//! - The stylesheet: [`html`]. The `latex-recomposed` backend's own rules:
//! [`latex`].
use Arc;
use crateSettingsDecl;
use crate;
use crate;
use crate;
use crateVALIDATED;
use craterefs;
pub use ;
pub use CHECK_PROOFS;
pub use ;
pub use ;
pub use ;
pub use ;
// Theorem kinds are values of one element type, so that a kind the configuration
// adds is an environment in its own right with no code written for it; the kinds
// are grouped into classes so that what describes a kind's shape is stated once
// per class.
/// The feature's registered name, `theorems`.
pub const FEATURE: &str = "theorems";
/// The declaration of the feature's settings: the fields of [`TheoremsConfig`] are its keys, and
/// the texts a kind's words, a class's heading template and closing mark, and the number format
/// hold are FLM markup. See [`crate::core::settings`].
/// The `theorems` feature ([`FlmFeature`]): it registers one element value of
/// [`THEOREM`] per kind of its [`TheoremsConfig`], each written as the
/// environment of the kind's name, together with the heading elements, the
/// lowering rules, the check stage, and the counters and counter styles of the
/// numbered kinds (contributed to the numbering feature). See the [module
/// documentation](self).
///
/// Construct it with [`Theorems::new`], or use [`feature`] for the default
/// configuration. Registering it fails with [`BuildError::Feature`] when the
/// configuration is refused by [`TheoremsConfig::validate`], and the registry
/// build fails when a heading template, a closing mark or a word does not
/// compile.
/// The [`Theorems`] feature under the default configuration (the eight kinds
/// of [`TheoremsConfig::new`]), ready to be listed in a package entry
/// ([`PackageEntry`](crate::core::registry::PackageEntry)).
///
/// Use `Arc::new(Theorems::new(config))` to list the feature under a
/// [`TheoremsConfig`] of your own.