Skip to main content

flm_core/stdlib/features/theorems/
diagnostics.rs

1//! This module defines the diagnostic conditions that the [`theorems`](super)
2//! feature reports (see [diagnostics](crate::guide::concepts_overview#diagnostics)).
3//!
4//! The conditions [`MissingProof`] and [`BadRelationRef`] concern the theorems
5//! written in a document. The check stage [`CHECK_PROOFS`](super::CHECK_PROOFS)
6//! reports the warning [`MissingProof`] for a theorem that has no proof
7//! although its kind expects one, and the error [`BadRelationRef`] for a title
8//! of a proof that begins with a star but is not a valid back-reference.
9//!
10//! The two other conditions indicate an error in the code of a feature, and a
11//! document cannot cause them. The lowering rules of the nodes of a theorem
12//! heading report [`PointerRefused`] for a node that does not refer to the
13//! title of a theorem. The check stage reports [`StageFailed`] when it fails to
14//! read a table.
15//!
16//! The references feature reports two other conditions that concern theorems,
17//! as it does for any label and reference: a back-reference to a label that is
18//! not defined is reported as `flm.refs.unresolved`, and a `\label` on a proof
19//! is reported as `flm.refs.label-type-not-accepted`.
20
21use techy::error::DiagnosticInfo;
22
23/// The warning that is reported when a theorem has no proof although its kind
24/// expects one (id `flm.theorems.missing-proof`).
25///
26/// A theorem kind expects a proof when its
27/// [`expects_proof`](super::TheoremKind::expects_proof) setting is `true`. In
28/// the default configuration, these kinds are `theorem`, `proposition`,
29/// `lemma` and `corollary`. A theorem has a proof when the title of a proof
30/// is a back-reference to a label of the theorem, as in
31/// `\begin{proof}[*thm:main]` or `\begin{proof}[**thm:main]`. The warning is
32/// not reported for a theorem that carries the field `\noproofref`, as in
33/// `\begin{theorem}\label{thm:main}\noproofref`.
34///
35/// Reported by the check stage [`CHECK_PROOFS`](super::CHECK_PROOFS) at the
36/// theorem, under every workflow (see [`check`](super::check)). The output of
37/// the document is not affected. The warning is not reported when the registry
38/// does not contain the references feature, which resolves back-references.
39///
40/// The message suggests three fixes: writing a proof whose title refers to the
41/// theorem, adding `\noproofref` to the theorem, or setting `expects_proof` to
42/// `false` for the kind of the theorem.
43#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
44#[diagnostic(id = "flm.theorems.missing-proof")]
45pub struct MissingProof {
46    /// The kind of the theorem, which is also the name of its environment,
47    /// e.g. `lemma`.
48    pub kind: String,
49    /// The first label of the theorem whose type the kind accepts, written as
50    /// `type:label`. The labels are taken in the order in which they are
51    /// written. `None` when the theorem has no such label. A label of another
52    /// type does not define anything, so a proof cannot refer to the theorem
53    /// through it.
54    pub label: Option<String>,
55    /// The label type that the message suggests when the theorem has no label
56    /// (`label` is `None`): the first label type that the kind accepts, which
57    /// is the first entry of
58    /// [`TheoremsConfig::label_types`](super::TheoremsConfig::label_types),
59    /// e.g. `thm`.
60    pub label_type: String,
61    /// The theorem kind that the message suggests for writing the proof, e.g.
62    /// `proof`. It is the first kind, in the alphabetical order of the kind
63    /// names, whose class reads back-references
64    /// ([`relation_ref`](super::TheoremClass::relation_ref)). `None` when no
65    /// class reads back-references; the message then does not suggest writing
66    /// a proof.
67    pub proof_kind: Option<String>,
68}
69
70/// Writes the message of the warning. The first suggested fix depends on
71/// whether the theorem has a label and whether a kind for proofs exists, so
72/// the message has four forms.
73impl std::fmt::Display for MissingProof {
74    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75        let kind = &self.kind;
76        let ty = &self.label_type;
77        match (&self.label, &self.proof_kind) {
78            (Some(label), Some(proof)) => write!(
79                f,
80                "Missing proof for {kind} ‘{label}’: write its proof as \
81                 ‘\\begin{{{proof}}}[*{label}]’; or add ‘\\noproofref’ to the {kind}; or set \
82                 ‘expects_proof’ to false for theorem kind ‘{kind}’ in the configuration of \
83                 feature ‘theorems’"
84            ),
85            (None, Some(proof)) => write!(
86                f,
87                "Missing proof for this {kind}: label the {kind} with ‘\\label{{{ty}:…}}’ and \
88                 write its proof as ‘\\begin{{{proof}}}[*{ty}:…]’; or add ‘\\noproofref’ to the \
89                 {kind}; or set ‘expects_proof’ to false for theorem kind ‘{kind}’ in the \
90                 configuration of feature ‘theorems’"
91            ),
92            (Some(label), None) => write!(
93                f,
94                "Missing proof for {kind} ‘{label}’: add ‘\\noproofref’ to the {kind}; or set \
95                 ‘expects_proof’ to false for theorem kind ‘{kind}’ in the configuration of \
96                 feature ‘theorems’"
97            ),
98            (None, None) => write!(
99                f,
100                "Missing proof for this {kind}: add ‘\\noproofref’ to the {kind}; or set \
101                 ‘expects_proof’ to false for theorem kind ‘{kind}’ in the configuration of \
102                 feature ‘theorems’"
103            ),
104        }
105    }
106}
107
108/// The error that is reported when the title of a proof begins with a star but
109/// is not a valid back-reference (id `flm.theorems.bad-relation-ref`).
110///
111/// A back-reference consists of one or two stars followed by a label, as in
112/// `\begin{proof}[*lem:x]` or `\begin{proof}[**lem:x]`. Back-references are
113/// read in the titles of the theorem kinds whose class has
114/// [`relation_ref`](super::TheoremClass::relation_ref) set. In the default
115/// configuration, `proof` is the only such kind.
116///
117/// Reported by the check stage [`CHECK_PROOFS`](super::CHECK_PROOFS) at the
118/// title, under every workflow. The title is written to the output as it is
119/// typed, and the proof does not refer to any theorem.
120#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
121#[diagnostic(
122    id = "flm.theorems.bad-relation-ref",
123    message = "Invalid back-reference in the title of ‘{environment}’: back-references \
124               must have the form ‘*type:label’ or ‘**type:label’"
125)]
126pub struct BadRelationRef {
127    /// The name of the environment, e.g. `proof`.
128    pub environment: String,
129}
130
131/// Error representing a node of a theorem heading whose content pointer does
132/// not refer to the title of a theorem (id `flm.theorems.pointer-refused`).
133///
134/// The lowering rule of a theorem writes the heading of the theorem from a
135/// template (see [`heading`](super::heading)). In this heading, the title is
136/// represented by a synthesized node of the element
137/// [`TITLE_POINTER`](super::TITLE_POINTER), or of the element
138/// [`RELATION`](super::RELATION) for a back-reference that a proof shows in
139/// its heading. This node contains a content pointer
140/// ([`ContentPointer`](crate::stdlib::backends::lowering::ContentPointer)),
141/// through which the lowering rule of the node reads the title. The content
142/// pointer must refer to the title argument of a theorem.
143///
144/// A document cannot cause this error: it is caused by the faulty code that
145/// created the node. Reported by the lowering rules
146/// [`TitlePointerRule`](super::TitlePointerRule) and
147/// [`RelationRule`](super::RelationRule). The node expands to nothing.
148#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
149#[diagnostic(
150    id = "flm.theorems.pointer-refused",
151    message = "Invalid content pointer to {region} of a node of ‘{element}’: content \
152               pointers in a theorem heading must refer to the title argument of a theorem"
153)]
154pub struct PointerRefused {
155    /// The name of the element of the node that the content pointer refers to.
156    pub element: String,
157    /// The region of this node that the content pointer refers to, as text,
158    /// e.g. `argument 0`.
159    pub region: String,
160}
161
162/// Error raised when the check stage of the theorems feature fails to read a
163/// table (id `flm.theorems.stage-failed`).
164///
165/// The check stage [`CHECK_PROOFS`](super::CHECK_PROOFS) reads the element
166/// index and, when the registry contains the references feature, the table of
167/// resolved references. A table read fails only when the code of a feature is
168/// faulty: when a stage reads a table that it did not declare as a read, or
169/// when the value type of the table differs from the declared one (see
170/// [`AccessError`](crate::core::data::store::AccessError)). A document cannot
171/// cause this error.
172///
173/// Reported with no position in the source. The check stage then stops, and
174/// reports no other condition for the document.
175#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
176#[diagnostic(id = "flm.theorems.stage-failed", message = "Stage ‘{stage}’ failed: {detail}")]
177pub struct StageFailed {
178    /// The name of the stage, `flm.theorems.check-proofs`.
179    pub stage: String,
180    /// The error message of the failed table read.
181    pub detail: String,
182}