16#define size_buffer (1u << 20)
19 unsigned char chars[size_buffer];
23typedef struct write_buffer write_buffer;
40#if !defined(KISSAT_NDEBUG) || defined(LOGGING)
46#define LOGPREFIX "PROOF"
48#define LOGIMPORTED3(...) \
49 LOGLITS3 (SIZE_STACK (proof->imported), BEGIN_STACK (proof->imported), \
52#define LOGLINE3(...) \
53 LOGINTS3 (SIZE_STACK (proof->line), BEGIN_STACK (proof->line), \
60 proof->binary = binary;
64 LOG (
"starting to trace %s proof", binary ?
"binary" :
"non-binary");
67static void flush_buffer (proof *proof) {
68 size_t bytes = proof->buffer.pos;
71 size_t written = kissat_write (proof->file, proof->buffer.chars, bytes);
73 kissat_fatal (
"flushing %zu bytes in proof write-buffer failed", bytes);
74 proof->buffer.pos = 0;
78 proof *proof =
solver->proof;
80 LOG (
"stopping to trace proof");
82 kissat_flush (proof->file);
87#if !defined(KISSAT_NDEBUG) || defined(LOGGING)
98#define PERCENT_LINES(NAME) kissat_percent (proof->NAME, proof->lines)
100void kissat_print_proof_statistics (
kissat *
solver,
bool verbose) {
101 proof *proof =
solver->proof;
102 PRINT_STAT (
"proof_added", proof->added, PERCENT_LINES (added),
"%",
104 PRINT_STAT (
"proof_bytes", proof->file->bytes,
105 proof->file->bytes / (
double) (1 << 20),
"MB",
"");
106 PRINT_STAT (
"proof_deleted", proof->deleted, PERCENT_LINES (deleted),
"%",
109 PRINT_STAT (
"proof_lines", proof->lines, 100,
"%",
"");
111 PRINT_STAT (
"proof_literals", proof->literals,
112 kissat_average (proof->literals, proof->lines),
"",
120static inline void write_char (proof *,
unsigned char)
123static inline void import_external_proof_literal (
kissat *, proof *,
int)
127import_internal_proof_literal (
kissat *, proof *,
unsigned)
132static inline void write_char (proof *proof,
unsigned char ch) {
133 write_buffer *buffer = &proof->buffer;
134 if (buffer->pos == size_buffer)
135 flush_buffer (proof);
136 buffer->chars[buffer->pos++] = ch;
139static inline void import_internal_proof_literal (
kissat *
solver,
142 int elit = kissat_export_literal (
solver, ilit);
146#if !defined(KISSAT_NDEBUG) || defined(LOGGING)
151static inline void import_external_proof_literal (
kissat *
solver,
152 proof *proof,
int elit) {
161static void import_internal_proof_binary (
kissat *
solver, proof *proof,
162 unsigned a,
unsigned b) {
164 import_internal_proof_literal (
solver, proof, a);
165 import_internal_proof_literal (
solver, proof, b);
168static void import_internal_proof_literals (
kissat *
solver, proof *proof,
170 const unsigned *ilits) {
173 for (
size_t i = 0; i <
size; i++)
174 import_internal_proof_literal (
solver, proof, ilits[i]);
177static void import_external_proof_literals (
kissat *
solver, proof *proof,
178 size_t size,
const int *elits) {
181 for (
size_t i = 0; i <
size; i++)
182 import_external_proof_literal (
solver, proof, elits[i]);
185static void import_proof_clause (
kissat *
solver, proof *proof,
190static void print_binary_proof_line (proof *proof) {
192 for (
all_stack (
int, elit, proof->line)) {
193 unsigned x = 2u *
ABS (elit) + (elit < 0);
196 ch = (x & 0x7f) | 0x80;
197 write_char (proof, ch);
200 write_char (proof, x);
202 write_char (proof, 0);
205static void print_non_binary_proof_line (proof *proof) {
208 char *end_of_buffer = buffer +
sizeof buffer;
209 *--end_of_buffer = 0;
210 for (
all_stack (
int, elit, proof->line)) {
211 char *
p = end_of_buffer;
217 write_char (proof,
'-');
221 for (
unsigned tmp = eidx; tmp; tmp /= 10)
222 *--
p =
'0' + (tmp % 10);
223 while (
p != end_of_buffer)
224 write_char (proof, *
p++);
225 write_char (proof,
' ');
227 write_char (proof,
'0');
228 write_char (proof,
'\n');
231static void print_proof_line (proof *proof) {
234 print_binary_proof_line (proof);
236 print_non_binary_proof_line (proof);
238#if !defined(KISSAT_NDEBUG) || defined(LOGGING)
241#ifndef KISSAT_NOPTIONS
245 flush_buffer (proof);
246 kissat_flush (proof->file);
252static unsigned external_to_proof_literal (
int elit) {
255 return 2u * (abs (elit) - 1) + (elit < 0);
258static void resize_proof_units (proof *proof,
unsigned plit) {
260 const size_t old_size = proof->size_units;
261 size_t new_size = old_size ? old_size : 2;
262 while (new_size <= plit)
266 memcpy (new_units, proof->units, old_size);
268 proof->units = new_units;
269 proof->size_units = new_size;
272static void check_repeated_proof_lines (proof *proof) {
277 }
else if (size == 1) {
278 const int eunit =
PEEK_STACK (proof->line, 0);
279 const unsigned punit = external_to_proof_literal (eunit);
281 if (!proof->size_units || proof->size_units <= punit)
282 resize_proof_units (proof, punit);
283 proof->units[punit] = 1;
289static void print_added_proof_line (proof *proof) {
294 LOGIMPORTED3 (
"added proof line");
295 LOGLINE3 (
"added proof line");
298 check_repeated_proof_lines (proof);
301 write_char (proof,
'a');
302 print_proof_line (proof);
305static void print_delete_proof_line (proof *proof) {
310 LOGIMPORTED3 (
"deleted internal proof line");
311 LOGLINE3 (
"deleted external proof line");
313 write_char (proof,
'd');
315 write_char (proof,
' ');
316 print_proof_line (proof);
319void kissat_add_binary_to_proof (
kissat *
solver,
unsigned a,
unsigned b) {
320 proof *proof =
solver->proof;
322 import_internal_proof_binary (
solver, proof, a, b);
323 print_added_proof_line (proof);
327 proof *proof =
solver->proof;
329 import_proof_clause (
solver, proof, c);
330 print_added_proof_line (proof);
334 proof *proof =
solver->proof;
337 print_added_proof_line (proof);
341 const unsigned *ilits) {
342 proof *proof =
solver->proof;
344 import_internal_proof_literals (
solver, proof,
size, ilits);
345 print_added_proof_line (proof);
348void kissat_add_unit_to_proof (
kissat *
solver,
unsigned ilit) {
349 proof *proof =
solver->proof;
352 import_internal_proof_literal (
solver, proof, ilit);
353 print_added_proof_line (proof);
357 unsigned remove,
unsigned keep) {
358 proof *proof =
solver->proof;
361 const unsigned *ilits = c->
lits;
363 for (
unsigned i = 0; i !=
size; i++) {
364 const unsigned ilit = ilits[i];
367 if (ilit != keep &&
values[ilit] < 0 && !
LEVEL (ilit))
369 import_internal_proof_literal (
solver, proof, ilit);
371 print_added_proof_line (proof);
372 import_proof_clause (
solver, proof, c);
373 print_delete_proof_line (proof);
376void kissat_delete_binary_from_proof (
kissat *
solver,
unsigned a,
378 proof *proof =
solver->proof;
380 import_internal_proof_binary (
solver, proof, a, b);
381 print_delete_proof_line (proof);
385 proof *proof =
solver->proof;
387 import_proof_clause (
solver, proof, c);
388 print_delete_proof_line (proof);
393 proof *proof =
solver->proof;
396 import_external_proof_literals (
solver, proof,
size, elits);
397 print_delete_proof_line (proof);
401 const unsigned *ilits) {
402 proof *proof =
solver->proof;
404 import_internal_proof_literals (
solver, proof,
size, ilits);
405 print_delete_proof_line (proof);
void * kissat_calloc(kissat *solver, size_t n, size_t size)
void kissat_free(kissat *solver, void *ptr, size_t bytes)
void kissat_dealloc(kissat *solver, void *ptr, size_t n, size_t size)
#define ATTRIBUTE_ALWAYS_INLINE
#define all_stack(T, E, S)
ABC_NAMESPACE_IMPL_START typedef signed char value
void kissat_fatal(const char *fmt,...)
#define KISSAT_assert(ignore)
typedefABC_NAMESPACE_HEADER_START struct kissat kissat
int kissat_proof_dummy_to_avoid_warning