lolli_core/proof.rs
1//! Proof tree representation.
2//!
3//! This module provides data structures for representing proofs in sequent calculus.
4
5use crate::{Formula, Sequent};
6
7/// A proof in the sequent calculus.
8#[derive(Clone, Debug)]
9pub struct Proof {
10 /// The conclusion of this proof step
11 pub conclusion: Sequent,
12 /// The inference rule applied
13 pub rule: Rule,
14 /// The premises (sub-proofs)
15 pub premises: Vec<Proof>,
16}
17
18/// Inference rules for linear logic sequent calculus.
19#[derive(Clone, Debug, PartialEq, Eq)]
20pub enum Rule {
21 // Identity rules
22 /// Axiom: ⊢ A⊥, A
23 Axiom,
24 /// Cut: from ⊢ Γ, A and ⊢ Δ, A⊥ derive ⊢ Γ, Δ
25 Cut(Formula),
26
27 // Multiplicative rules
28 /// One introduction: ⊢ 1
29 OneIntro,
30 /// Bottom introduction: from ⊢ Γ derive ⊢ Γ, ⊥
31 BottomIntro,
32 /// Tensor introduction: from ⊢ Γ, A and ⊢ Δ, B derive ⊢ Γ, Δ, A ⊗ B
33 TensorIntro,
34 /// Par introduction: from ⊢ Γ, A, B derive ⊢ Γ, A ⅋ B
35 ParIntro,
36
37 // Additive rules
38 /// Top introduction: ⊢ Γ, ⊤ (always provable)
39 TopIntro,
40 /// With introduction: from ⊢ Γ, A and ⊢ Γ, B derive ⊢ Γ, A & B
41 WithIntro,
42 /// Plus introduction (left): from ⊢ Γ, A derive ⊢ Γ, A ⊕ B
43 PlusIntroLeft,
44 /// Plus introduction (right): from ⊢ Γ, B derive ⊢ Γ, A ⊕ B
45 PlusIntroRight,
46
47 // Exponential rules
48 /// Of course introduction: from ⊢ ?Γ, A derive ⊢ ?Γ, !A
49 OfCourseIntro,
50 /// Why not introduction: from ⊢ Γ, A derive ⊢ Γ, ?A
51 WhyNotIntro,
52 /// Weakening: from ⊢ Γ derive ⊢ Γ, ?A
53 Weakening,
54 /// Contraction: from ⊢ Γ, ?A, ?A derive ⊢ Γ, ?A
55 Contraction,
56 /// Dereliction: use of ?A as A
57 Dereliction,
58
59 // Focused proof search rules
60 /// Focus on a positive formula at index
61 FocusPositive(usize),
62 /// Focus on a negative formula at index
63 FocusNegative(usize),
64 /// Blur (unfocus)
65 Blur,
66}
67
68impl Proof {
69 /// Count the number of cut rules in this proof.
70 pub fn cut_count(&self) -> usize {
71 let self_cuts = if matches!(self.rule, Rule::Cut(_)) {
72 1
73 } else {
74 0
75 };
76 let premise_cuts: usize = self.premises.iter().map(|p| p.cut_count()).sum();
77 self_cuts + premise_cuts
78 }
79
80 /// Returns true if this proof is cut-free.
81 pub fn is_cut_free(&self) -> bool {
82 self.cut_count() == 0
83 }
84
85 /// Returns the depth of the proof tree.
86 pub fn depth(&self) -> usize {
87 if self.premises.is_empty() {
88 1
89 } else {
90 1 + self.premises.iter().map(|p| p.depth()).max().unwrap_or(0)
91 }
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn test_cut_count() {
101 let axiom = Proof {
102 conclusion: Sequent::new(vec![]),
103 rule: Rule::Axiom,
104 premises: vec![],
105 };
106 assert_eq!(axiom.cut_count(), 0);
107 assert!(axiom.is_cut_free());
108 }
109
110 #[test]
111 fn test_depth() {
112 let leaf = Proof {
113 conclusion: Sequent::new(vec![]),
114 rule: Rule::Axiom,
115 premises: vec![],
116 };
117 assert_eq!(leaf.depth(), 1);
118
119 let with_premise = Proof {
120 conclusion: Sequent::new(vec![]),
121 rule: Rule::BottomIntro,
122 premises: vec![leaf],
123 };
124 assert_eq!(with_premise.depth(), 2);
125 }
126}