logicaffeine_language/ast/theorem.rs
1//! Theorem and proof block AST types.
2//!
3//! This module defines the AST for theorem blocks in the vernacular proof language:
4//!
5//! ```text
6//! ## Theorem: Socrates_Mortality
7//! Given: All men are mortal.
8//! Given: Socrates is a man.
9//! Prove: Socrates is mortal.
10//! Proof: Auto.
11//! ```
12//!
13//! # Key Types
14//!
15//! - **[`TheoremBlock`]**: Contains premises, goal, and proof strategy
16//! - **[`ProofStrategy`]**: How to prove (Auto, Manual, By lemmas)
17
18use super::logic::LogicExpr;
19
20/// A theorem block containing premises, goal, and proof strategy.
21#[derive(Debug, Clone)]
22pub struct TheoremBlock<'a> {
23 /// The name of the theorem (e.g., "Socrates_Mortality")
24 pub name: String,
25
26 /// Premises (Given statements) - logical expressions to assume true
27 pub premises: Vec<&'a LogicExpr<'a>>,
28
29 /// Optional names for the premises, parallel to `premises`. A `Given (h): …`
30 /// names that premise `h`, so a `Proof:` script can refer to it as `cases h`
31 /// rather than the positional `hp0`. `None` for an unnamed `Given:`.
32 pub premise_names: Vec<Option<String>>,
33
34 /// The goal to prove (Prove statement)
35 pub goal: &'a LogicExpr<'a>,
36
37 /// The proof strategy to use
38 pub strategy: ProofStrategy,
39}
40
41/// Proof strategies for theorem verification.
42#[derive(Debug, Clone, PartialEq)]
43pub enum ProofStrategy {
44 /// Automatic proof search using backward chaining.
45 /// The prover will try all available inference rules.
46 Auto,
47
48 /// Induction on a variable (for inductive types like Nat, List).
49 /// Example: `Proof: By induction on n.`
50 ///
51 /// Driven by lowering to the tactic script `induction <variable>`, so there is one
52 /// proof engine rather than a second, divergent one — and the bare
53 /// [`Script`](ProofStrategy::Script) spelling `Proof: Induction on n.` reaches exactly
54 /// the same proof by the same route. What the structured variant adds is that the METHOD
55 /// and its ARGUMENT are readable without re-parsing prose.
56 ///
57 /// Only the WHOLE line qualifies: `By induction on n. Automatically.` is a two-step
58 /// script and stays one.
59 Induction(String),
60
61 /// A one-word `Proof:` line: the proof is a SINGLE named step.
62 /// Example: `Proof: Assumption.`
63 ///
64 /// The classification is by SHAPE — one word is one step, more than one word is a
65 /// [`Script`](ProofStrategy::Script) — so it cannot drift against the inference-rule
66 /// enum or the tactic table. The vernacular has exactly ONE proof-step namespace (the
67 /// tactics), and the step runs through the same engine a script does.
68 ///
69 /// A word that names a kernel INFERENCE RULE rather than a tactic — `Proof:
70 /// ModusPonens.` — is reported as exactly that and is NOT applied here. A rule is not a
71 /// step, exactly as Lean answers `by modus_ponens` with "unknown tactic" and wants
72 /// `apply`; the diagnostic names the spelling that DOES apply it, `Proof: By ModusPonens.`,
73 /// which lowers to the `by_rule` tactic and is certified before it reports success.
74 ByRule(String),
75
76 /// An explicit tactic-script proof, written in the English-esque vernacular and run by
77 /// the tactic framework. Over the block
78 ///
79 /// ```text
80 /// ## Theorem: Or_Commutes
81 /// Given (h): Bob is happy or Bob is sad.
82 /// Prove: Bob is sad or Bob is happy.
83 /// Proof: By cases on h. Right, by assumption. Left, by assumption.
84 /// ```
85 ///
86 /// the script is `Proof: By cases on h. Right, by assumption. Left, by assumption.`
87 ///
88 /// The example names its BLOCK because a proof step is only meaningful against a goal:
89 /// the spelling taught here before, which opened with `Assume h.`, wanted a goal
90 /// that was BOTH an implication (for `Assume`) and a disjunction (for `cases`), a shape
91 /// this surface has no sentence for, so the one worked example of the one open-ended
92 /// variant could not be run on anything. That is the same defect as T12-005 one variant
93 /// over. Every `Proof:` spelling quoted in this file is now extracted from the file and
94 /// run against the surface by
95 /// `logicaffeine-tests::L_language_extra::every_proof_spelling_our_own_doc_comments_teach_is_checked_against_the_surface`,
96 /// so a doc comment cannot teach a spelling the surface does not accept.
97 Script(String),
98}
99
100impl Default for ProofStrategy {
101 fn default() -> Self {
102 ProofStrategy::Auto
103 }
104}