lolli_core/
formula.rs

1//! Linear logic formula representation.
2//!
3//! This module provides the [`Formula`] enum representing linear logic formulas
4//! with all standard connectives.
5
6/// A linear logic formula.
7///
8/// Linear logic has a rich set of connectives split into multiplicative and additive families,
9/// plus exponentials for controlled structural rules.
10#[derive(Clone, Debug, PartialEq, Eq, Hash)]
11pub enum Formula {
12    // Atoms
13    /// Atomic proposition
14    Atom(String),
15    /// Negated atomic proposition (A⊥)
16    NegAtom(String),
17
18    // Multiplicatives
19    /// Tensor product (A ⊗ B) - "both A and B independently"
20    Tensor(Box<Formula>, Box<Formula>),
21    /// Par (A ⅋ B) - "A or B, opponent chooses"
22    Par(Box<Formula>, Box<Formula>),
23    /// Multiplicative unit (1)
24    One,
25    /// Multiplicative false (⊥)
26    Bottom,
27
28    // Additives
29    /// With (A & B) - "both available, you choose one"
30    With(Box<Formula>, Box<Formula>),
31    /// Plus (A ⊕ B) - "one of them, I choose which"
32    Plus(Box<Formula>, Box<Formula>),
33    /// Additive truth (⊤)
34    Top,
35    /// Additive false (0)
36    Zero,
37
38    // Exponentials
39    /// Of course (!A) - "unlimited supply of A, can copy/discard"
40    OfCourse(Box<Formula>),
41    /// Why not (?A) - "demand for A"
42    WhyNot(Box<Formula>),
43
44    // Derived (syntactic sugar)
45    /// Linear implication (A ⊸ B) - sugar for A⊥ ⅋ B
46    Lolli(Box<Formula>, Box<Formula>),
47}
48
49impl Formula {
50    /// Compute the linear negation of a formula.
51    ///
52    /// Linear negation is involutive: (A⊥)⊥ = A
53    ///
54    /// De Morgan dualities:
55    /// - (A ⊗ B)⊥ = A⊥ ⅋ B⊥
56    /// - (A ⅋ B)⊥ = A⊥ ⊗ B⊥
57    /// - (A & B)⊥ = A⊥ ⊕ B⊥
58    /// - (A ⊕ B)⊥ = A⊥ & B⊥
59    /// - 1⊥ = ⊥
60    /// - ⊥⊥ = 1
61    /// - (!A)⊥ = ?(A⊥)
62    /// - (?A)⊥ = !(A⊥)
63    pub fn negate(&self) -> Formula {
64        match self {
65            Formula::Atom(a) => Formula::NegAtom(a.clone()),
66            Formula::NegAtom(a) => Formula::Atom(a.clone()),
67
68            Formula::Tensor(a, b) => {
69                Formula::Par(Box::new(a.negate()), Box::new(b.negate()))
70            }
71            Formula::Par(a, b) => {
72                Formula::Tensor(Box::new(a.negate()), Box::new(b.negate()))
73            }
74            Formula::One => Formula::Bottom,
75            Formula::Bottom => Formula::One,
76
77            Formula::With(a, b) => {
78                Formula::Plus(Box::new(a.negate()), Box::new(b.negate()))
79            }
80            Formula::Plus(a, b) => {
81                Formula::With(Box::new(a.negate()), Box::new(b.negate()))
82            }
83            Formula::Top => Formula::Zero,
84            Formula::Zero => Formula::Top,
85
86            Formula::OfCourse(a) => Formula::WhyNot(Box::new(a.negate())),
87            Formula::WhyNot(a) => Formula::OfCourse(Box::new(a.negate())),
88
89            Formula::Lolli(a, b) => {
90                // (A ⊸ B)⊥ = (A⊥ ⅋ B)⊥ = A ⊗ B⊥
91                Formula::Tensor(a.clone(), Box::new(b.negate()))
92            }
93        }
94    }
95
96    /// Desugar the formula by expanding A ⊸ B to A⊥ ⅋ B.
97    pub fn desugar(&self) -> Formula {
98        match self {
99            Formula::Lolli(a, b) => {
100                Formula::Par(Box::new(a.negate().desugar()), Box::new(b.desugar()))
101            }
102            Formula::Tensor(a, b) => {
103                Formula::Tensor(Box::new(a.desugar()), Box::new(b.desugar()))
104            }
105            Formula::Par(a, b) => {
106                Formula::Par(Box::new(a.desugar()), Box::new(b.desugar()))
107            }
108            Formula::With(a, b) => {
109                Formula::With(Box::new(a.desugar()), Box::new(b.desugar()))
110            }
111            Formula::Plus(a, b) => {
112                Formula::Plus(Box::new(a.desugar()), Box::new(b.desugar()))
113            }
114            Formula::OfCourse(a) => Formula::OfCourse(Box::new(a.desugar())),
115            Formula::WhyNot(a) => Formula::WhyNot(Box::new(a.desugar())),
116            _ => self.clone(),
117        }
118    }
119
120    /// Returns true if this formula is positive (async/eager).
121    ///
122    /// Positive formulas: ⊗, 1, ⊕, 0, !, atoms
123    pub fn is_positive(&self) -> bool {
124        matches!(
125            self,
126            Formula::Atom(_)
127                | Formula::Tensor(_, _)
128                | Formula::One
129                | Formula::Plus(_, _)
130                | Formula::Zero
131                | Formula::OfCourse(_)
132        )
133    }
134
135    /// Returns true if this formula is negative (sync/lazy).
136    ///
137    /// Negative formulas: ⅋, ⊥, &, ⊤, ?, negated atoms
138    pub fn is_negative(&self) -> bool {
139        !self.is_positive()
140    }
141
142    /// Pretty print the formula with Unicode symbols.
143    pub fn pretty(&self) -> String {
144        match self {
145            Formula::Atom(a) => a.clone(),
146            Formula::NegAtom(a) => format!("{}⊥", a),
147            Formula::Tensor(a, b) => format!("({} ⊗ {})", a.pretty(), b.pretty()),
148            Formula::Par(a, b) => format!("({} ⅋ {})", a.pretty(), b.pretty()),
149            Formula::Lolli(a, b) => format!("({} ⊸ {})", a.pretty(), b.pretty()),
150            Formula::With(a, b) => format!("({} & {})", a.pretty(), b.pretty()),
151            Formula::Plus(a, b) => format!("({} ⊕ {})", a.pretty(), b.pretty()),
152            Formula::OfCourse(a) => format!("!{}", a.pretty()),
153            Formula::WhyNot(a) => format!("?{}", a.pretty()),
154            Formula::One => "1".to_string(),
155            Formula::Bottom => "⊥".to_string(),
156            Formula::Top => "⊤".to_string(),
157            Formula::Zero => "0".to_string(),
158        }
159    }
160
161    /// Pretty print the formula with ASCII symbols.
162    pub fn pretty_ascii(&self) -> String {
163        match self {
164            Formula::Atom(a) => a.clone(),
165            Formula::NegAtom(a) => format!("{}^", a),
166            Formula::Tensor(a, b) => format!("({} * {})", a.pretty_ascii(), b.pretty_ascii()),
167            Formula::Par(a, b) => format!("({} | {})", a.pretty_ascii(), b.pretty_ascii()),
168            Formula::Lolli(a, b) => format!("({} -o {})", a.pretty_ascii(), b.pretty_ascii()),
169            Formula::With(a, b) => format!("({} & {})", a.pretty_ascii(), b.pretty_ascii()),
170            Formula::Plus(a, b) => format!("({} + {})", a.pretty_ascii(), b.pretty_ascii()),
171            Formula::OfCourse(a) => format!("!{}", a.pretty_ascii()),
172            Formula::WhyNot(a) => format!("?{}", a.pretty_ascii()),
173            Formula::One => "1".to_string(),
174            Formula::Bottom => "bot".to_string(),
175            Formula::Top => "top".to_string(),
176            Formula::Zero => "0".to_string(),
177        }
178    }
179}
180
181#[cfg(test)]
182mod tests {
183    use super::*;
184
185    #[test]
186    fn test_negation_involutive() {
187        let a = Formula::Atom("A".to_string());
188        assert_eq!(a.negate().negate(), a);
189
190        let complex = Formula::Tensor(
191            Box::new(Formula::Atom("A".to_string())),
192            Box::new(Formula::Atom("B".to_string())),
193        );
194        assert_eq!(complex.negate().negate(), complex);
195    }
196
197    #[test]
198    fn test_de_morgan() {
199        let a = Formula::Atom("A".to_string());
200        let b = Formula::Atom("B".to_string());
201
202        // (A ⊗ B)⊥ = A⊥ ⅋ B⊥
203        let tensor = Formula::Tensor(Box::new(a.clone()), Box::new(b.clone()));
204        let expected = Formula::Par(
205            Box::new(Formula::NegAtom("A".to_string())),
206            Box::new(Formula::NegAtom("B".to_string())),
207        );
208        assert_eq!(tensor.negate(), expected);
209    }
210
211    #[test]
212    fn test_polarity() {
213        assert!(Formula::Atom("A".to_string()).is_positive());
214        assert!(Formula::NegAtom("A".to_string()).is_negative());
215        assert!(Formula::Tensor(
216            Box::new(Formula::Atom("A".to_string())),
217            Box::new(Formula::Atom("B".to_string()))
218        )
219        .is_positive());
220        assert!(Formula::Par(
221            Box::new(Formula::Atom("A".to_string())),
222            Box::new(Formula::Atom("B".to_string()))
223        )
224        .is_negative());
225    }
226}