rustsat-cadical 0.7.5

Interface to the SAT solver CaDiCaL for the RustSAT library.
Documentation
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "internal.hpp"

namespace CaDiCaL {

/*------------------------------------------------------------------------*/

VeripbTracer::VeripbTracer (Internal *i, File *f, bool b, bool a, bool c)
    : internal (i), file (f), with_antecedents (a), checked_deletions (c),
      num_clauses (0), size_clauses (0), clauses (0), last_hash (0),
      last_id (0), last_clause (0)
#ifndef QUIET
      ,
      added (0), deleted (0)
#endif
{
  (void) internal;

  // Initialize random number table for hash function.
  //
  Random random (42);
  for (unsigned n = 0; n < num_nonces; n++) {
    uint64_t nonce = random.next ();
    if (!(nonce & 1))
      nonce++;
    assert (nonce), assert (nonce & 1);
    nonces[n] = nonce;
  }
#ifndef NDEBUG
  binary = b;
#else
  (void) b;
#endif
}

void VeripbTracer::connect_internal (Internal *i) {
  internal = i;
  file->connect_internal (internal);
  LOG ("VERIPB TRACER connected to internal");
}

VeripbTracer::~VeripbTracer () {
  LOG ("VERIPB TRACER delete");
  delete file;
  for (size_t i = 0; i < size_clauses; i++)
    for (HashId *c = clauses[i], *next; c; c = next)
      next = c->next, delete_clause (c);
  delete[] clauses;
}

/*------------------------------------------------------------------------*/

void VeripbTracer::enlarge_clauses () {
  assert (num_clauses == size_clauses);
  const uint64_t new_size_clauses = size_clauses ? 2 * size_clauses : 1;
  LOG ("VeriPB Tracer enlarging clauses from %" PRIu64 " to %" PRIu64,
       (uint64_t) size_clauses, (uint64_t) new_size_clauses);
  HashId **new_clauses;
  new_clauses = new HashId *[new_size_clauses];
  clear_n (new_clauses, new_size_clauses);
  for (uint64_t i = 0; i < size_clauses; i++) {
    for (HashId *c = clauses[i], *next; c; c = next) {
      next = c->next;
      const uint64_t h = reduce_hash (c->hash, new_size_clauses);
      c->next = new_clauses[h];
      new_clauses[h] = c;
    }
  }
  delete[] clauses;
  clauses = new_clauses;
  size_clauses = new_size_clauses;
}

HashId *VeripbTracer::new_clause () {
  HashId *res = new HashId ();
  res->next = 0;
  res->hash = last_hash;
  res->id = last_id;
  last_clause = res;
  num_clauses++;
  return res;
}

void VeripbTracer::delete_clause (HashId *c) {
  assert (c);
  num_clauses--;
  delete c;
}

uint64_t VeripbTracer::reduce_hash (uint64_t hash, uint64_t size) {
  assert (size > 0);
  unsigned shift = 32;
  uint64_t res = hash;
  while ((((uint64_t) 1) << shift) > size) {
    res ^= res >> shift;
    shift >>= 1;
  }
  res &= size - 1;
  assert (res < size);
  return res;
}

uint64_t VeripbTracer::compute_hash (const int64_t id) {
  assert (id > 0);
  unsigned j = id % num_nonces;             // Dont know if this is a good
  uint64_t tmp = nonces[j] * (uint64_t) id; // hash funktion or even better
  return last_hash = tmp;                   // than just using id.
}

bool VeripbTracer::find_and_delete (const int64_t id) {
  if (!num_clauses)
    return false;
  /*
  if (last_clause && last_clause->id == id) {
    const uint64_t h = reduce_hash (last_clause->hash, size_clauses);
    clauses[h] = last_clause->next;
    delete last_clause;
    return true;
  }
  */
  HashId **res = 0, *c;
  const uint64_t hash = compute_hash (id);
  const uint64_t h = reduce_hash (hash, size_clauses);
  for (res = clauses + h; (c = *res); res = &c->next) {
    if (c->hash == hash && c->id == id) {
      break;
    }
    if (!c->next)
      return false;
  }
  if (!c)
    return false;
  assert (c && res);
  *res = c->next;
  delete_clause (c);
  return true;
}

void VeripbTracer::insert () {
  if (num_clauses == size_clauses)
    enlarge_clauses ();
  const uint64_t h = reduce_hash (compute_hash (last_id), size_clauses);
  HashId *c = new_clause ();
  c->next = clauses[h];
  clauses[h] = c;
}

/*------------------------------------------------------------------------*/

inline void VeripbTracer::put_binary_zero () {
  assert (binary);
  assert (file);
  file->put ((unsigned char) 0);
}

inline void VeripbTracer::put_binary_lit (int lit) {
  assert (binary);
  assert (file);
  assert (lit != INT_MIN);
  unsigned x = 2 * abs (lit) + (lit < 0);
  unsigned char ch;
  while (x & ~0x7f) {
    ch = (x & 0x7f) | 0x80;
    file->put (ch);
    x >>= 7;
  }
  ch = x;
  file->put (ch);
}

inline void VeripbTracer::put_binary_id (int64_t id, bool can_be_negative) {
  assert (binary);
  assert (file);
  uint64_t x = abs (id);
  if (can_be_negative) {
    x = 2 * x + (id < 0);
  }
  unsigned char ch;
  while (x & ~0x7f) {
    ch = (x & 0x7f) | 0x80;
    file->put (ch);
    x >>= 7;
  }
  ch = x;
  file->put (ch);
}

/*------------------------------------------------------------------------*/

void VeripbTracer::veripb_add_derived_clause (int64_t id, bool redundant,
                                              int witness,
                                              const vector<int> &clause) {
  assert (witness == clause[0]);
  file->put ("red ");
  for (const auto &external_lit : clause) {
    file->put ("1 ");
    if (external_lit < 0)
      file->put ('~');
    file->put ('x');
    file->put (abs (external_lit));
    file->put (' ');
  }
  file->put (">= 1 : ");
  file->put ('x');
  file->put (abs (witness));
  file->put (" -> ");
  if (witness < 0)
    file->put ("0");
  else
    file->put ("1");
  file->put (";\n");
  if (!redundant && checked_deletions) {
    file->put ("core id ");
    file->put (id);
    file->put (";\n");
  }
}

void VeripbTracer::veripb_add_derived_clause (
    int64_t id, bool redundant, const vector<int> &clause,
    const vector<int64_t> &chain) {
  file->put ("pol ");
  bool first = true;
  assert (!chain.empty ());
  for (auto p = chain.rbegin (); p != chain.rend (); p++) {
    auto cid = *p;
    if (first) {
      first = false;
      file->put (cid);
    } else {
      file->put (' ');
      file->put (cid);
      file->put (" + s");
    }
  }
  file->put (";\n");
  file->put ("e ");
  for (const auto &external_lit : clause) {
    file->put ("1 ");
    if (external_lit < 0)
      file->put ('~');
    file->put ('x');
    file->put (abs (external_lit));
    file->put (' ');
  }
  file->put (">= 1 : ");
  file->put (id);
  file->put (";\n");
  if (!redundant && checked_deletions) {
    file->put ("core id ");
    file->put (id);
    file->put (";\n");
  }
}

void VeripbTracer::veripb_add_derived_clause (int64_t id, bool redundant,
                                              const vector<int> &clause) {
  file->put ("rup ");
  for (const auto &external_lit : clause) {
    file->put ("1 ");
    if (external_lit < 0)
      file->put ('~');
    file->put ('x');
    file->put (abs (external_lit));
    file->put (' ');
  }
  file->put (">= 1;\n");
  if (!redundant && checked_deletions) {
    file->put ("core id ");
    file->put (id);
    file->put (";\n");
  }
}

void VeripbTracer::veripb_begin_proof (int64_t reserved_ids) {
  file->put ("pseudo-Boolean proof version 3.0\n");
  file->put ("f ");
  file->put (reserved_ids);
  file->put (";\n");
}

void VeripbTracer::veripb_delete_clause (int64_t id, bool redundant) {
  if (!redundant && checked_deletions && find_and_delete (id))
    return;
  if (redundant || !checked_deletions)
    file->put ("del id ");
  else {
    file->put ("delc ");
  }
  file->put (id);
  file->put (";\n");
}

void VeripbTracer::veripb_report_status (bool unsat, int64_t conflict_id) {
  file->put ("output NONE;\n");
  if (unsat) {
    file->put ("conclusion UNSAT : ");
    file->put (conflict_id);
    file->put (";\n");
  } else
    file->put ("conclusion NONE;\n");
  file->put ("end pseudo-Boolean proof;\n");
}

void VeripbTracer::veripb_strengthen (int64_t id) {
  if (!checked_deletions)
    return;
  file->put ("core id ");
  file->put (id);
  file->put (";\n");
}

/*------------------------------------------------------------------------*/

void VeripbTracer::begin_proof (int64_t id) {
  if (file->closed ())
    return;
  LOG ("VERIPB TRACER tracing start of proof with %" PRId64
       "original clauses",
       id);
  veripb_begin_proof (id);
}

void VeripbTracer::add_derived_clause (int64_t id, bool redundant,
                                       int witness,
                                       const vector<int> &clause,
                                       const vector<int64_t> &chain) {
  if (file->closed ())
    return;
  LOG ("VERIPB TRACER tracing addition of derived clause[%" PRId64 "]", id);
  if (witness)
    veripb_add_derived_clause (id, redundant, witness, clause);
  else if (with_antecedents)
    veripb_add_derived_clause (id, redundant, clause, chain);
  else
    veripb_add_derived_clause (id, redundant, clause);
#ifndef QUIET
  added++;
#endif
}

void VeripbTracer::delete_clause (int64_t id, bool redundant,
                                  const vector<int> &) {
  if (file->closed ())
    return;
  LOG ("VERIPB TRACER tracing deletion of clause[%" PRId64 "]", id);
  veripb_delete_clause (id, redundant);
#ifndef QUIET
  deleted++;
#endif
}

void VeripbTracer::report_status (int status, int64_t conflict_id) {
  if (file->closed ())
    return;
#ifdef LOGGING
  if (conflict_id)
    LOG ("VERIPB TRACER tracing finalization of proof with empty "
         "clause[%" PRId64 "]",
         conflict_id);
#endif
  veripb_report_status (status == UNSATISFIABLE, conflict_id);
}

void VeripbTracer::weaken_minus (int64_t id, const vector<int> &) {
  if (!checked_deletions)
    return;
  if (file->closed ())
    return;
  LOG ("VERIPB TRACER tracing weaken minus of clause[%" PRId64 "]", id);
  last_id = id;
  insert ();
}

void VeripbTracer::strengthen (int64_t id) {
  if (file->closed ())
    return;
  LOG ("VERIPB TRACER tracing strengthen of clause[%" PRId64 "]", id);
  veripb_strengthen (id);
}

/*------------------------------------------------------------------------*/

bool VeripbTracer::closed () { return file->closed (); }

#ifndef QUIET

void VeripbTracer::print_statistics () {
  // TODO complete
  uint64_t bytes = file->bytes ();
  uint64_t total = added + deleted;
  MSG ("VeriPB %" PRId64 " added clauses %.2f%%", added,
       percent (added, total));
  MSG ("VeriPB %" PRId64 " deleted clauses %.2f%%", deleted,
       percent (deleted, total));
  MSG ("VeriPB %" PRId64 " bytes (%.2f MB)", bytes,
       bytes / (double) (1 << 20));
}

#endif

void VeripbTracer::close (bool print) {
  assert (!closed ());
  file->close ();
#ifndef QUIET
  if (print) {
    MSG ("VeriPB proof file '%s' closed", file->name ());
    print_statistics ();
  }
#else
  (void) print;
#endif
}

void VeripbTracer::flush (bool print) {
  assert (!closed ());
  file->flush ();
#ifndef QUIET
  if (print) {
    MSG ("VeriPB proof file '%s' flushed", file->name ());
    print_statistics ();
  }
#else
  (void) print;
#endif
}

} // namespace CaDiCaL