summaryrefslogtreecommitdiff
path: root/src/prop/cnf_stream.cpp
blob: b4a0a297e799b16bb7cd1285b9835a9dfa7b527b (plain)
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
/*********************                                                        */
/** cnf_stream.cpp
 ** Original author: taking
 ** Major contributors: dejan
 ** Minor contributors (to current version): mdeters
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.
 **
 ** A CNF converter that takes in asserts and has the side effect
 ** of given an equisatisfiable stream of assertions to PropEngine.
 **/

#include "prop/cnf_stream.h"
#include "prop/prop_engine.h"
#include "expr/node.h"
#include "util/Assert.h"
#include "util/output.h"

#include <queue>

using namespace std;

namespace CVC4 {
namespace prop {

CnfStream::CnfStream(PropEngine *pe) :
  d_propEngine(pe) {
}

TseitinCnfStream::TseitinCnfStream(PropEngine *pe) :
  CnfStream(pe) {
}

void CnfStream::insertClauseIntoStream(SatClause& c) {
  Debug("cnf") << "Inserting into stream " << c << endl;
  d_propEngine->assertClause(c);
}

void CnfStream::insertClauseIntoStream(SatLiteral a) {
  SatClause clause(1);
  clause[0] = a;
  insertClauseIntoStream(clause);
}
void CnfStream::insertClauseIntoStream(SatLiteral a, SatLiteral b) {
  SatClause clause(2);
  clause[0] = a;
  clause[1] = b;
  insertClauseIntoStream(clause);
}
void CnfStream::insertClauseIntoStream(SatLiteral a, SatLiteral b, SatLiteral c) {
  SatClause clause(3);
  clause[0] = a;
  clause[1] = b;
  clause[2] = c;
  insertClauseIntoStream(clause);
}

bool CnfStream::isCached(const Node& n) const {
  return d_translationCache.find(n) != d_translationCache.end();
}

SatLiteral CnfStream::lookupInCache(const Node& n) const {
  Assert(isCached(n),
      "Node is not in cnf translation cache");
  return d_translationCache.find(n)->second;
}

void CnfStream::flushCache() {
  Debug("cnf") << "flushing the translation cache" << endl;
  d_translationCache.clear();
}

void CnfStream::cacheTranslation(const Node& node, SatLiteral lit) {
  Debug("cnf") << "caching translation " << node << " to " << lit << endl;
  d_translationCache.insert(make_pair(node, lit));
}

SatLiteral CnfStream::aquireAndRegister(const Node& node, bool atom) {
  SatVariable var = atom ? d_propEngine->registerAtom(node)
      : d_propEngine->requestFreshVar();
  SatLiteral lit(var);
  cacheTranslation(node, lit);
  return lit;
}

bool CnfStream::isAtomMapped(const Node& n) const {
  return d_propEngine->isAtomMapped(n);
}

SatVariable CnfStream::lookupAtom(const Node& n) const {
  return d_propEngine->lookupAtom(n);
}

/***********************************************/
/***********************************************/
/************ End of CnfStream *****************/
/***********************************************/
/***********************************************/

SatLiteral TseitinCnfStream::handleAtom(const Node& n) {
  Assert(n.isAtomic(), "handleAtom(n) expects n to be an atom");

  Debug("cnf") << "handling atom" << endl;

  SatLiteral l = aquireAndRegister(n, true);
  switch(n.getKind()) { /* TRUE and FALSE are handled specially. */
  case TRUE:
    insertClauseIntoStream(l);
    break;
  case FALSE:
    insertClauseIntoStream(~l);
    break;
  default: //Does nothing else
    break;
  }
  return l;
}

SatLiteral TseitinCnfStream::handleXor(const Node& n) {
  // n: a XOR b

  SatLiteral a = recTransform(n[0]);
  SatLiteral b = recTransform(n[1]);

  SatLiteral l = aquireAndRegister(n);

  insertClauseIntoStream(a, b, ~l);
  insertClauseIntoStream(a, ~b, l);
  insertClauseIntoStream(~a, b, l);
  insertClauseIntoStream(~a, ~b, ~l);

  return l;
}

/* For a small efficiency boost target needs to already be allocated to have
 size of the number of children of n.
 */
void TseitinCnfStream::mapRecTransformOverChildren(const Node& n,
                                                   SatClause& target) {
  Assert((unsigned)target.size() == n.getNumChildren(),
      "Size of the children must be the same the constructed clause");

  int i = 0;
  Node::iterator subExprIter = n.begin();

  while(subExprIter != n.end()) {
    SatLiteral equivalentLit = recTransform(*subExprIter);
    target[i] = equivalentLit;
    ++subExprIter;
    ++i;
  }
}

SatLiteral TseitinCnfStream::handleOr(const Node& n) {
  //child_literals
  SatClause lits(n.getNumChildren());
  mapRecTransformOverChildren(n, lits);

  SatLiteral e = aquireAndRegister(n);

  /* e <-> (a1 | a2 | a3 | ...)
   *: e -> (a1 | a2 | a3 | ...)
   * : ~e | (
   * : ~e | a1 | a2 | a3 | ...
   * e <- (a1 | a2 | a3 | ...)
   * : e <- (a1 | a2 | a3 |...)
   * : e | ~(a1 | a2 | a3 |...)
   * : 
   * : (e | ~a1) & (e |~a2) & (e & ~a3) & ...
   */

  SatClause c(1 + lits.size());

  for(int index = 0; index < lits.size(); ++index) {
    SatLiteral a = lits[index];
    c[index] = a;
    insertClauseIntoStream(e, ~a);
  }
  c[lits.size()] = ~e;
  insertClauseIntoStream(c);

  return e;
}

/* TODO: this only supports 2-ary <=> nodes atm.
 * Should this be changed?
 */
SatLiteral TseitinCnfStream::handleIff(const Node& n) {
  Assert(n.getKind() == IFF);
  Assert(n.getNumChildren() == 2);
  // n: a <=> b;

  SatLiteral a = recTransform(n[0]);
  SatLiteral b = recTransform(n[1]);

  SatLiteral l = aquireAndRegister(n);

  /* l <-> (a<->b)
   * : l -> ((a-> b) & (b->a))
   * : ~l | ((~a | b) & (~b |a))
   * : (~a | b | ~l ) & (~b | a | ~l)
   * 
   * : (a<->b) -> l
   * : ~((a & b) | (~a & ~b)) | l
   * : (~(a & b)) & (~(~a & ~b)) | l
   * : ((~a | ~b) & (a | b)) | l
   * : (~a | ~b | l) & (a | b | l)
   */

  insertClauseIntoStream(~a, b, ~l);
  insertClauseIntoStream(a, ~b, ~l);
  insertClauseIntoStream(~a, ~b, l);
  insertClauseIntoStream(a, b, l);

  return l;
}

SatLiteral TseitinCnfStream::handleAnd(const Node& n) {
  Assert(n.getKind() == AND);
  Assert(n.getNumChildren() >= 1);

  //TODO: we know the exact size of the this.
  //Dynamically allocated array would have less overhead no?
  SatClause lits(n.getNumChildren());
  mapRecTransformOverChildren(n, lits);

  SatLiteral e = aquireAndRegister(n);

  /* e <-> (a1 & a2 & a3 & ...)
   * : e -> (a1 & a2 & a3 & ...)
   * : ~e | (a1 & a2 & a3 & ...)
   * : (~e | a1) & (~e | a2) & ...
   * e <- (a1 & a2 & a3 & ...)
   * : e <- (a1 & a2 & a3 & ...)
   * : e | ~(a1 & a2 & a3 & ...)
   * : e | (~a1 | ~a2 | ~a3 | ...)
   * : e | ~a1 | ~a2 | ~a3 | ...
   */

  SatClause c(lits.size() + 1);
  for(int index = 0; index < lits.size(); ++index) {
    SatLiteral a = lits[index];
    c[index] = (~a);
    insertClauseIntoStream(~e, a);
  }
  c[lits.size()] = e;

  insertClauseIntoStream(c);

  return e;
}

SatLiteral TseitinCnfStream::handleImplies(const Node& n) {
  Assert(n.getKind() == IMPLIES);
  Assert(n.getNumChildren() == 2);
  // n: a => b;

  SatLiteral a = recTransform(n[0]);
  SatLiteral b = recTransform(n[1]);

  SatLiteral l = aquireAndRegister(n);

  /* l <-> (a->b)
   * (l -> (a->b)) & (l <- (a->b))
   *  : l -> (a -> b)
   *  : ~l | (~ a | b)
   *  : (~l | ~a | b)
   * (~l | ~a | b) & (l<- (a->b))
   *  : (a->b) -> l
   *  : ~(~a | b) | l
   *  : (a & ~b) | l
   *  : (a | l) & (~b | l)
   * (~l | ~a | b) & (a | l) & (~b | l)
   */

  insertClauseIntoStream(a, l);
  insertClauseIntoStream(~b, l);
  insertClauseIntoStream(~l, ~a, b);

  return l;
}

SatLiteral TseitinCnfStream::handleNot(const Node& n) {
  Assert(n.getKind() == NOT);
  Assert(n.getNumChildren() == 1);

  // n : NOT m
  Node m = n[0];
  SatLiteral equivM = recTransform(m);

  SatLiteral equivN = ~equivM;

  cacheTranslation(n, equivN);

  return equivN;
}

SatLiteral TseitinCnfStream::handleIte(const Node& n) {
  Assert(n.getKind() == ITE);
  Assert(n.getNumChildren() == 3);

  // n : IF c THEN t ELSE f ENDIF;
  SatLiteral c = recTransform(n[0]);
  SatLiteral t = recTransform(n[1]);
  SatLiteral f = recTransform(n[2]);

  // d <-> IF c THEN tB ELSE fb
  // : d -> (c & tB) | (~c & fB)
  // : ~d | ( (c & tB) | (~c & fB) )
  // :          x | (y & z) = (x | y) & (x | z)
  // : ~d | ( ((c & t) | ~c ) & ((c & t) | f ) )
  // : ~d | ( (((c | ~c) & (~c | t))) & ((c | f) & (f | t)) )
  // : ~d | ( (~c | t) & (c | f) & (f | t) )
  // : (~d | ~c | t) & (~d | c | f) & (~d | f | t)

  // : ~d | (c & t & f)
  // : (~d | c)  & (~d | t) & (~d | f)
  // ( IF c THEN tB ELSE fb) -> d
  // :~((c & tB) | (~c & fB)) | d
  // : ((~c | ~t)& ( c |~fb)) | d
  // : (~c | ~ t | d) & (c | ~f | d)

  SatLiteral d = aquireAndRegister(n);

  insertClauseIntoStream(~d, ~c, t);
  insertClauseIntoStream(~d, c, f);
  insertClauseIntoStream(~d, f, t);
  insertClauseIntoStream(~c, ~t, d);
  insertClauseIntoStream(c, ~f, d);

  return d;
}

//TODO: The following code assumes everything is either
// an atom or is a logical connective. This ignores quantifiers and lambdas.
SatLiteral TseitinCnfStream::recTransform(const Node& n) {
  if(isCached(n)) {
    return lookupInCache(n);
  }

  if(n.isAtomic()) {

    /* Unfortunately we need to potentially allow
     * for n to be to the Atom <-> Var map but not the
     * translation cache.
     * This is because the translation cache can be flushed.
     * It is really not clear where this check should go, but
     * it needs to be done.
     */
    if(isAtomMapped(n)) {
      /* Puts the atom in the translation cache after looking it up.
       * Avoids doing 2 map lookups for this atom in the future.
       */
      SatLiteral l(lookupAtom(n));
      cacheTranslation(n, l);
      return l;
    }
    return handleAtom(n);
  } else {
    //Assume n is a logical connective
    switch(n.getKind()) {
    case NOT:
      return handleNot(n);
    case XOR:
      return handleXor(n);
    case ITE:
      return handleIte(n);
    case IFF:
      return handleIff(n);
    case IMPLIES:
      return handleImplies(n);
    case OR:
      return handleOr(n);
    case AND:
      return handleAnd(n);
    default:
      Unreachable();
    }
  }
}

void TseitinCnfStream::convertAndAssert(const Node& n) {
  SatLiteral e = recTransform(n);
  insertClauseIntoStream(e);
}

}/* CVC4::prop namespace */
}/* CVC4 namespace */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback