1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use l_group_formulas::free_group_term::FreeGroupTerm;
use l_group_formulas::l_group_term::LGroupTerm;
use l_group_formulas::Reducable;
use std::collections::BTreeSet;
/// Represents a meet of joins of free group terms.
///
/// Can be constructed from LGroupTerms as follows.
/// ```
/// use l_group_formulas::l_group_term::LGroupTerm;
/// use l_group_cnf::normal_cnf::CNF;
/// let l_group_term = LGroupTerm::from("((x^y)v(x^z))(-(x^(yvz)))");
/// println!("The CNF of {} is {}", l_group_term.to_string(), CNF::from(l_group_term).to_string());
/// ```
pub struct CNF {
pub meetands: BTreeSet<BTreeSet<FreeGroupTerm>>
}
impl ToString for CNF {
fn to_string(&self) -> String {
let mut string = String::new();
for meetand in &self.meetands {
string.push('(');
for joinand in meetand {
string.push_str(joinand.to_string().as_str());
string.push_str(" v ");
}
string = string[0 .. string.len() - 3].to_string();
string.push_str(") ^ ");
}
if string.len() == 0 {
return String::from("(())")
}
string[0..string.len() - 3].to_string()
}
}
impl From<LGroupTerm> for CNF {
fn from(term: LGroupTerm) -> CNF {
let cnf_term = to_cnf(term);
let mut meetands = BTreeSet::new();
match cnf_term {
LGroupTerm::Meet(xs) => {
for x in xs {
match x {
LGroupTerm::Meet(_) | LGroupTerm::Prod(_) => panic!("CNF failed"),
LGroupTerm::Join(ys) => {
// should be ys, but freegroupterms instead of lgroupterms
let mut new_meetand = BTreeSet::new();
for y in ys {
match y {
LGroupTerm::Atom(t) => new_meetand.insert(t),
_ => panic!("CNF failed")
};
}
meetands.insert(new_meetand);
},
LGroupTerm::Atom(y) => {
let mut single_term = BTreeSet::new();
single_term.insert(y);
meetands.insert(single_term);
}
};
}
},
LGroupTerm::Join(xs) => {
let mut single_meetand = BTreeSet::new();
for x in xs {
match x {
LGroupTerm::Atom(t) => single_meetand.insert(t),
_ => panic!("CNF failed")
};
};
meetands.insert(single_meetand);
},
LGroupTerm::Atom(x) => {
let mut single_meetand = BTreeSet::new();
single_meetand.insert(x);
meetands.insert(single_meetand);
}
LGroupTerm::Prod(_) => panic!("CNF failed")
};
CNF { meetands: meetands }
}
}
impl CNF {
/// Constructs a new CNF from a set of sets of free group terms
///
/// # Examples
/// Basic usage:
/// ```
/// use l_group_formulas::literal::Literal;
/// use l_group_formulas::free_group_term::FreeGroupTerm;
/// use std::collections::BTreeSet;
/// use l_group_cnf::normal_cnf::CNF;
/// let mut meetands = BTreeSet::new();
/// let mut first_meetand = BTreeSet::new();
/// let mut second_meetand = BTreeSet::new();
/// let first_joinand = FreeGroupTerm::from(Literal::from('x'));
/// let second_joinand = FreeGroupTerm::from(Literal::from('y'));
/// let third_joinand = FreeGroupTerm::from(Literal::from('z'));
/// let fourth_joinand = FreeGroupTerm::from(Literal::from('w'));
///
/// first_meetand.insert(first_joinand);
/// first_meetand.insert(second_joinand);
///
/// second_meetand.insert(third_joinand);
/// second_meetand.insert(fourth_joinand);
///
/// meetands.insert(first_meetand);
/// meetands.insert(second_meetand);
///
/// assert_eq!(String::from("(w v z) ^ (x v y)"), CNF::new(meetands).to_string());
/// ```
pub fn new(meetands: BTreeSet<BTreeSet<FreeGroupTerm>>) -> CNF {
CNF { meetands: meetands }
}
}
fn to_cnf(term: LGroupTerm) -> LGroupTerm {
if is_in_cnf(&term) {
return term;
}
match term {
LGroupTerm::Atom(_) => { return term }
LGroupTerm::Meet(xs) => {
let mut meetands = BTreeSet::new();
for x in xs {
meetands.insert(to_cnf(x));
}
return LGroupTerm::Meet(meetands).reduced();
},
LGroupTerm::Join(xs) => {
let mut rest = BTreeSet::new();
for x in xs.clone() {
match &x {
LGroupTerm::Meet(meetands) => {
for y in xs {
if x != y { rest.insert(y); }
}
// term = join(meet(meetands), rest)
let mut new_meetands = BTreeSet::new();
for meetand in meetands {
let mut set = rest.clone();
set.insert(meetand.clone());
new_meetands.insert(to_cnf(LGroupTerm::Join(set)).reduced());
}
return LGroupTerm::Meet(new_meetands).reduced();
},
_ => {
rest.insert(x);
}
};
}
// term doesn't contain any meets
let mut new_joinands = BTreeSet::new();
for x in xs { new_joinands.insert(to_cnf(x)); }
return LGroupTerm::Join(new_joinands).reduced();
},
LGroupTerm::Prod(xs) => {
let mut rest_left = Vec::new();
for x in xs.clone() {
match &x {
LGroupTerm::Meet(meetands) => {
let mut rest_right = Vec::new();
enum Position { Left, Right }
let mut pos = Position::Left;
for y in xs {
match pos {
Position::Left => {
if x == y { pos = Position::Right; }
}
Position::Right => {
rest_right.push(y);
}
}
}
// term = prod(rest_left, meet(meetands), rest_right)
let mut new_meetands = BTreeSet::new();
for meetand in meetands {
let vec = vec![LGroupTerm::Prod(rest_left.clone()), meetand.clone(), LGroupTerm::Prod(rest_right.clone())];
new_meetands.insert(to_cnf(LGroupTerm::Prod(vec).reduced()));
}
return to_cnf(LGroupTerm::Meet(new_meetands));
},
LGroupTerm::Join(joinands) => {
let mut rest_right = Vec::new();
enum Position { Left, Right }
let mut pos = Position::Left;
for y in xs {
match pos {
Position::Left => {
if x == y { pos = Position::Right; }
}
Position::Right => {
rest_right.push(y);
}
}
}
// term = prod(rest_left, join(joinands), rest_right)
let mut new_joinands = BTreeSet::new();
for joinand in joinands {
let vec = vec![LGroupTerm::Prod(rest_left.clone()), joinand.clone(), LGroupTerm::Prod(rest_right.clone())];
new_joinands.insert(to_cnf(LGroupTerm::Prod(vec).reduced()));
}
return to_cnf(LGroupTerm::Join(new_joinands));
},
_ => {
rest_left.push(x);
}
};
}
// term doesn't contain any meets
let mut new_joinands = BTreeSet::new();
for x in xs { new_joinands.insert(to_cnf(x)); }
return LGroupTerm::Join(new_joinands).reduced();
}
};
}
fn is_in_cnf(term: &LGroupTerm) -> bool {
match term {
LGroupTerm::Prod(_) => return false,
LGroupTerm::Atom(_) => return true,
LGroupTerm::Join(xs) => {
for x in xs {
match x {
LGroupTerm::Atom(_) => {},
_ => return false
};
}
}
LGroupTerm::Meet(xs) => {
for x in xs {
match x {
LGroupTerm::Meet(_) | LGroupTerm::Prod(_) => return false,
LGroupTerm::Atom(_) | LGroupTerm::Join(_) => {
if !is_in_cnf(x) { return false };
}
}
}
}
};
return true;
}
#[cfg(test)]
mod tests {
use super::*;
use l_group_formulas::literal::Literal;
#[test]
fn test_to_string() {
let mut meetands = BTreeSet::new();
let mut first_meetand = BTreeSet::new();
let mut second_meetand = BTreeSet::new();
let first_joinand = FreeGroupTerm::from(Literal::from('x'));
let second_joinand = FreeGroupTerm::from(Literal::from('y'));
let third_joinand = FreeGroupTerm::from(Literal::from('z'));
let fourth_joinand = FreeGroupTerm::from(Literal::from('w'));
first_meetand.insert(first_joinand);
first_meetand.insert(second_joinand);
second_meetand.insert(third_joinand);
second_meetand.insert(fourth_joinand);
meetands.insert(first_meetand);
meetands.insert(second_meetand);
assert_eq!(String::from("(w v z) ^ (x v y)"), CNF::new(meetands).to_string());
}
}