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
//! DRAT proof format for SAT proofs.
//!
//! DRAT (Deletion Resolution Asymmetric Tautology) is the standard format
//! for SAT solver proofs, checkable by tools like `drat-trim`.
//!
//! ## Format
//!
//! DRAT proofs consist of a sequence of lines, each representing either:
//! - A clause addition (RAT clause)
//! - A clause deletion (marked with 'd')
//!
//! The proof demonstrates that the original formula is unsatisfiable by
//! deriving the empty clause.
use std::io::{self, Write};
/// A literal in DRAT format (signed integer, 0 is terminator)
pub type Lit = i32;
/// A clause in DRAT format
pub type Clause = Vec<Lit>;
/// A DRAT proof step
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DratStep {
/// Add a clause (learned or derived)
Add(Clause),
/// Delete a clause
Delete(Clause),
}
/// DRAT proof writer
///
/// Records proof steps and can output in text or binary format.
#[derive(Debug, Default)]
pub struct DratProof {
/// Proof steps
steps: Vec<DratStep>,
/// Whether to use binary format
binary: bool,
}
impl DratProof {
/// Create a new DRAT proof writer (text format)
#[must_use]
pub fn new() -> Self {
Self {
steps: Vec::new(),
binary: false,
}
}
/// Create a new DRAT proof writer (binary format)
#[must_use]
pub fn binary() -> Self {
Self {
steps: Vec::new(),
binary: true,
}
}
/// Add a clause to the proof
pub fn add_clause(&mut self, clause: impl Into<Clause>) {
self.steps.push(DratStep::Add(clause.into()));
}
/// Delete a clause from the proof
pub fn delete_clause(&mut self, clause: impl Into<Clause>) {
self.steps.push(DratStep::Delete(clause.into()));
}
/// Get the number of proof steps
#[must_use]
pub fn len(&self) -> usize {
self.steps.len()
}
/// Check if the proof is empty
#[must_use]
pub fn is_empty(&self) -> bool {
self.steps.is_empty()
}
/// Get the proof steps
#[must_use]
pub fn steps(&self) -> &[DratStep] {
&self.steps
}
/// Clear all proof steps
pub fn clear(&mut self) {
self.steps.clear();
}
/// Write the proof in text format
pub fn write_text<W: Write>(&self, mut writer: W) -> io::Result<()> {
for step in &self.steps {
match step {
DratStep::Add(clause) => {
for &lit in clause {
write!(writer, "{} ", lit)?;
}
writeln!(writer, "0")?;
}
DratStep::Delete(clause) => {
write!(writer, "d ")?;
for &lit in clause {
write!(writer, "{} ", lit)?;
}
writeln!(writer, "0")?;
}
}
}
Ok(())
}
/// Write the proof in binary format
///
/// Binary DRAT format uses:
/// - 'a' (0x61) prefix for additions
/// - 'd' (0x64) prefix for deletions
/// - Variable-length encoding for literals (similar to LEB128)
pub fn write_binary<W: Write>(&self, mut writer: W) -> io::Result<()> {
for step in &self.steps {
match step {
DratStep::Add(clause) => {
writer.write_all(b"a")?;
for &lit in clause {
self.write_lit_binary(&mut writer, lit)?;
}
self.write_lit_binary(&mut writer, 0)?;
}
DratStep::Delete(clause) => {
writer.write_all(b"d")?;
for &lit in clause {
self.write_lit_binary(&mut writer, lit)?;
}
self.write_lit_binary(&mut writer, 0)?;
}
}
}
Ok(())
}
/// Write a literal in binary format (variable-length encoding)
fn write_lit_binary<W: Write>(&self, writer: &mut W, lit: Lit) -> io::Result<()> {
// Convert signed literal to unsigned
// lit > 0: 2*lit
// lit < 0: 2*(-lit) + 1
// lit = 0: 0 (terminator)
let value = if lit == 0 {
0u64
} else if lit > 0 {
(lit as u64) << 1
} else {
(((-lit) as u64) << 1) | 1
};
// Variable-length encoding
let mut val = value;
loop {
let byte = (val & 0x7f) as u8;
val >>= 7;
if val == 0 {
writer.write_all(&[byte])?;
break;
} else {
writer.write_all(&[byte | 0x80])?;
}
}
Ok(())
}
/// Write the proof in the configured format
pub fn write<W: Write>(&self, writer: W) -> io::Result<()> {
if self.binary {
self.write_binary(writer)
} else {
self.write_text(writer)
}
}
/// Convert to string (text format)
#[must_use]
#[allow(clippy::inherent_to_string)]
pub fn to_string(&self) -> String {
let mut buf = Vec::new();
self.write_text(&mut buf)
.expect("writing to Vec should not fail");
String::from_utf8(buf).expect("DRAT output is ASCII")
}
}
/// Trait for SAT solvers that can produce DRAT proofs
pub trait DratProofProducer {
/// Enable DRAT proof production
fn enable_proof(&mut self);
/// Disable DRAT proof production
fn disable_proof(&mut self);
/// Get the DRAT proof (if available)
fn get_proof(&self) -> Option<&DratProof>;
/// Take the DRAT proof, leaving None
fn take_proof(&mut self) -> Option<DratProof>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_drat_proof_add() {
let mut proof = DratProof::new();
proof.add_clause(vec![1, 2, -3]);
proof.add_clause(vec![4]);
proof.add_clause(vec![]); // Empty clause (contradiction)
let output = proof.to_string();
assert!(output.contains("1 2 -3 0"));
assert!(output.contains("4 0"));
assert!(output.contains("0")); // Empty clause
}
#[test]
fn test_drat_proof_delete() {
let mut proof = DratProof::new();
proof.add_clause(vec![1, 2]);
proof.delete_clause(vec![1, 2]);
let output = proof.to_string();
assert!(output.contains("1 2 0"));
assert!(output.contains("d 1 2 0"));
}
#[test]
fn test_drat_proof_binary() {
let mut proof = DratProof::binary();
proof.add_clause(vec![1, -2]);
proof.delete_clause(vec![3]);
let mut buf = Vec::new();
proof
.write(&mut buf)
.expect("test operation should succeed");
// Check binary format starts with 'a' and 'd'
assert_eq!(buf[0], b'a');
// Find the 'd' for deletion
assert!(buf.contains(&b'd'));
}
#[test]
fn test_drat_proof_clear() {
let mut proof = DratProof::new();
proof.add_clause(vec![1, 2]);
assert!(!proof.is_empty());
proof.clear();
assert!(proof.is_empty());
}
#[test]
fn test_drat_lit_encoding() {
let proof = DratProof::new();
let mut buf = Vec::new();
// Test encoding of small positive literal
proof
.write_lit_binary(&mut buf, 1)
.expect("test operation should succeed");
assert_eq!(buf, vec![2]); // 1 << 1 = 2
buf.clear();
proof
.write_lit_binary(&mut buf, -1)
.expect("test operation should succeed");
assert_eq!(buf, vec![3]); // (1 << 1) | 1 = 3
buf.clear();
proof
.write_lit_binary(&mut buf, 0)
.expect("test operation should succeed");
assert_eq!(buf, vec![0]); // Terminator
buf.clear();
proof
.write_lit_binary(&mut buf, 64)
.expect("test operation should succeed");
// 64 << 1 = 128, needs 2 bytes: 0x80 | 0, 0x01
assert_eq!(buf, vec![0x80, 0x01]);
}
#[test]
fn test_drat_proof_steps() {
let mut proof = DratProof::new();
proof.add_clause(vec![1, 2]);
proof.delete_clause(vec![1]);
proof.add_clause(vec![-3]);
assert_eq!(proof.len(), 3);
assert_eq!(proof.steps()[0], DratStep::Add(vec![1, 2]));
assert_eq!(proof.steps()[1], DratStep::Delete(vec![1]));
assert_eq!(proof.steps()[2], DratStep::Add(vec![-3]));
}
}