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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! Covered Clause Elimination (CCE)
//!
//! CCE is an advanced preprocessing technique that removes redundant clauses.
//! A clause C is "covered" by clause D if there exists a literal l in C such that
//! D is exactly C with l removed. In this case, C is redundant and can be eliminated.
//!
//! More formally: C is covered by D if ∃l ∈ C such that D = C \ {l}
//!
//! Example:
//! - C = (a ∨ b ∨ c)
//! - D = (a ∨ b)
//! - C is covered by D (by removing c)
//!
//! CCE is more powerful than subsumption and can lead to significant formula simplification.
use crate::clause::{ClauseDatabase, ClauseId};
use crate::literal::Lit;
#[allow(unused_imports)]
use crate::prelude::*;
use smallvec::SmallVec;
/// Covered Clause Elimination engine
///
/// Identifies and removes covered clauses from the clause database.
/// This is done by building an index of clause signatures and checking
/// for coverage relationships.
pub struct CoveredClauseElimination {
/// Clauses indexed by their literals (for fast lookup)
clause_index: FxHashMap<Lit, Vec<ClauseId>>,
/// Clauses marked for removal
to_remove: FxHashSet<ClauseId>,
/// Statistics
stats: CceStats,
}
/// Statistics for CCE
#[derive(Debug, Default, Clone)]
pub struct CceStats {
/// Number of covered clauses eliminated
pub eliminated: usize,
/// Number of coverage checks performed
pub checks: usize,
/// Number of clauses processed
pub processed: usize,
}
impl CoveredClauseElimination {
/// Create a new CCE engine
#[must_use]
pub fn new() -> Self {
Self {
clause_index: FxHashMap::default(),
to_remove: FxHashSet::default(),
stats: CceStats::default(),
}
}
/// Build index of clauses by their literals
fn build_index(&mut self, clauses: &ClauseDatabase) {
self.clause_index.clear();
for id in clauses.iter_ids() {
if let Some(clause) = clauses.get(id) {
for &lit in &clause.lits {
self.clause_index.entry(lit).or_default().push(id);
}
}
}
}
/// Check if clause c1 covers clause c2
///
/// c1 covers c2 if there exists a literal l in c2 such that c1 = c2 \ {l}
/// In other words, c1 is c2 with one literal removed.
fn is_covered_by(&mut self, c1_lits: &[Lit], c2_lits: &[Lit]) -> bool {
self.stats.checks += 1;
// c1 must be exactly one literal shorter than c2
if c1_lits.len() + 1 != c2_lits.len() {
return false;
}
// Check if c1 is a subset of c2
let c1_set: FxHashSet<Lit> = c1_lits.iter().copied().collect();
// Count how many literals from c2 are in c1
let common_count = c2_lits.iter().filter(|lit| c1_set.contains(*lit)).count();
// If all literals from c1 are in c2, then c1 covers c2
common_count == c1_lits.len()
}
/// Run CCE elimination on the clause database
///
/// Returns the number of clauses eliminated.
pub fn eliminate(&mut self, clauses: &mut ClauseDatabase) -> usize {
self.to_remove.clear();
self.build_index(clauses);
// Collect all clause IDs and their literals
let mut clause_list: Vec<(ClauseId, SmallVec<[Lit; 8]>)> = Vec::new();
for id in clauses.iter_ids() {
if let Some(clause) = clauses.get(id) {
let lits: SmallVec<[Lit; 8]> = clause.lits.iter().copied().collect();
clause_list.push((id, lits));
}
}
// Sort by clause length (shorter clauses first)
clause_list.sort_by_key(|(_, lits)| lits.len());
// For each clause, check if it's covered by any shorter clause
for i in 0..clause_list.len() {
let (c2_id, ref c2_lits) = clause_list[i];
if self.to_remove.contains(&c2_id) {
continue;
}
self.stats.processed += 1;
// Check against all shorter clauses
for (c1_id, c1_lits) in clause_list.iter().take(i) {
if self.to_remove.contains(c1_id) {
continue;
}
// Check if c2 is covered by c1
if self.is_covered_by(c1_lits, c2_lits) {
self.to_remove.insert(c2_id);
self.stats.eliminated += 1;
break;
}
}
}
// Remove covered clauses
for id in &self.to_remove {
clauses.remove(*id);
}
self.stats.eliminated
}
/// Get statistics
#[must_use]
pub fn stats(&self) -> &CceStats {
&self.stats
}
/// Reset statistics
pub fn reset_stats(&mut self) {
self.stats = CceStats::default();
}
}
impl Default for CoveredClauseElimination {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::clause::Clause;
use crate::literal::Var;
#[test]
fn test_cce_basic() {
let mut cce = CoveredClauseElimination::new();
let mut db = ClauseDatabase::new();
// Add clause: (a v b v c)
db.add(Clause::new(
vec![
Lit::pos(Var::new(0)),
Lit::pos(Var::new(1)),
Lit::pos(Var::new(2)),
],
false,
));
// Add clause: (a v b) - this covers the first clause
db.add(Clause::new(
vec![Lit::pos(Var::new(0)), Lit::pos(Var::new(1))],
false,
));
let eliminated = cce.eliminate(&mut db);
// The first clause should be eliminated
assert_eq!(eliminated, 1);
assert_eq!(db.len(), 1);
}
#[test]
fn test_cce_no_coverage() {
let mut cce = CoveredClauseElimination::new();
let mut db = ClauseDatabase::new();
// Add clause: (a v b)
db.add(Clause::new(
vec![Lit::pos(Var::new(0)), Lit::pos(Var::new(1))],
false,
));
// Add clause: (c v d) - no coverage relationship
db.add(Clause::new(
vec![Lit::pos(Var::new(2)), Lit::pos(Var::new(3))],
false,
));
let eliminated = cce.eliminate(&mut db);
// No clauses should be eliminated
assert_eq!(eliminated, 0);
assert_eq!(db.len(), 2);
}
#[test]
fn test_cce_multiple_covered() {
let mut cce = CoveredClauseElimination::new();
let mut db = ClauseDatabase::new();
// Add short clause: (a)
db.add(Clause::new(vec![Lit::pos(Var::new(0))], false));
// Add covered clauses
db.add(Clause::new(
vec![Lit::pos(Var::new(0)), Lit::pos(Var::new(1))],
false,
));
db.add(Clause::new(
vec![Lit::pos(Var::new(0)), Lit::pos(Var::new(2))],
false,
));
db.add(Clause::new(
vec![Lit::pos(Var::new(0)), Lit::pos(Var::new(3))],
false,
));
let eliminated = cce.eliminate(&mut db);
// Three clauses should be eliminated (all covered by (a))
assert_eq!(eliminated, 3);
assert_eq!(db.len(), 1);
}
#[test]
fn test_cce_stats() {
let mut cce = CoveredClauseElimination::new();
let mut db = ClauseDatabase::new();
db.add(Clause::new(
vec![Lit::pos(Var::new(0)), Lit::pos(Var::new(1))],
false,
));
db.add(Clause::new(
vec![
Lit::pos(Var::new(0)),
Lit::pos(Var::new(1)),
Lit::pos(Var::new(2)),
],
false,
));
cce.eliminate(&mut db);
let stats = cce.stats();
assert_eq!(stats.eliminated, 1);
assert!(stats.checks > 0);
assert!(stats.processed > 0);
}
#[test]
fn test_cce_is_covered_by() {
let mut cce = CoveredClauseElimination::new();
// (a v b) covers (a v b v c)
let c1 = vec![Lit::pos(Var::new(0)), Lit::pos(Var::new(1))];
let c2 = vec![
Lit::pos(Var::new(0)),
Lit::pos(Var::new(1)),
Lit::pos(Var::new(2)),
];
assert!(cce.is_covered_by(&c1, &c2));
// (a v b) does not cover (a v c)
let c3 = vec![Lit::pos(Var::new(0)), Lit::pos(Var::new(2))];
assert!(!cce.is_covered_by(&c1, &c3));
}
#[test]
fn test_cce_empty_db() {
let mut cce = CoveredClauseElimination::new();
let mut db = ClauseDatabase::new();
let eliminated = cce.eliminate(&mut db);
assert_eq!(eliminated, 0);
assert_eq!(db.len(), 0);
}
#[test]
fn test_cce_single_clause() {
let mut cce = CoveredClauseElimination::new();
let mut db = ClauseDatabase::new();
db.add(Clause::new(vec![Lit::pos(Var::new(0))], false));
let eliminated = cce.eliminate(&mut db);
assert_eq!(eliminated, 0);
assert_eq!(db.len(), 1);
}
}