1#[derive(Clone, Debug, PartialEq, Eq, Hash)]
11pub enum Formula {
12 Atom(String),
15 NegAtom(String),
17
18 Tensor(Box<Formula>, Box<Formula>),
21 Par(Box<Formula>, Box<Formula>),
23 One,
25 Bottom,
27
28 With(Box<Formula>, Box<Formula>),
31 Plus(Box<Formula>, Box<Formula>),
33 Top,
35 Zero,
37
38 OfCourse(Box<Formula>),
41 WhyNot(Box<Formula>),
43
44 Lolli(Box<Formula>, Box<Formula>),
47}
48
49impl Formula {
50 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 Formula::Tensor(a.clone(), Box::new(b.negate()))
92 }
93 }
94 }
95
96 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 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 pub fn is_negative(&self) -> bool {
139 !self.is_positive()
140 }
141
142 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 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 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}