GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/arith_ite_utils.cpp Lines: 1 278 0.4 %
Date: 2021-11-06 Branches: 2 1334 0.1 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Tim King, Aina Niemetz, Piotr Trojanek
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * [[ Add one-line brief description here ]]
14
 *
15
 * [[ Add lengthier description here ]]
16
 * \todo document this file
17
 */
18
19
#include "theory/arith/arith_ite_utils.h"
20
21
#include <ostream>
22
23
#include "base/output.h"
24
#include "expr/skolem_manager.h"
25
#include "options/base_options.h"
26
#include "preprocessing/util/ite_utilities.h"
27
#include "smt/env.h"
28
#include "theory/arith/arith_utilities.h"
29
#include "theory/arith/normal_form.h"
30
#include "theory/rewriter.h"
31
#include "theory/substitutions.h"
32
#include "theory/theory_model.h"
33
34
using namespace std;
35
36
namespace cvc5 {
37
namespace theory {
38
namespace arith {
39
40
Node ArithIteUtils::applyReduceVariablesInItes(Node n){
41
  NodeBuilder nb(n.getKind());
42
  if(n.getMetaKind() == kind::metakind::PARAMETERIZED) {
43
    nb << (n.getOperator());
44
  }
45
  for(Node::iterator it = n.begin(), end = n.end(); it != end; ++it){
46
    nb << reduceVariablesInItes(*it);
47
  }
48
  Node res = nb;
49
  return res;
50
}
51
52
Node ArithIteUtils::reduceVariablesInItes(Node n){
53
  using namespace cvc5::kind;
54
  if(d_reduceVar.find(n) != d_reduceVar.end()){
55
    Node res = d_reduceVar[n];
56
    return res.isNull() ? n : res;
57
  }
58
59
  switch(n.getKind()){
60
  case ITE:{
61
    Node c = n[0], t = n[1], e = n[2];
62
    if(n.getType().isReal()){
63
      Node rc = reduceVariablesInItes(c);
64
      Node rt = reduceVariablesInItes(t);
65
      Node re = reduceVariablesInItes(e);
66
67
      Node vt = d_varParts[t];
68
      Node ve = d_varParts[e];
69
      Node vpite = (vt == ve) ? vt : Node::null();
70
71
      if(vpite.isNull()){
72
        Node rite = rc.iteNode(rt, re);
73
        // do not apply
74
        d_reduceVar[n] = rite;
75
        d_constants[n] = mkRationalNode(Rational(0));
76
        d_varParts[n] = rite; // treat the ite as a variable
77
        return rite;
78
      }else{
79
        NodeManager* nm = NodeManager::currentNM();
80
        Node constantite = rc.iteNode(d_constants[t], d_constants[e]);
81
        Node sum = nm->mkNode(kind::PLUS, vpite, constantite);
82
        d_reduceVar[n] = sum;
83
        d_constants[n] = constantite;
84
        d_varParts[n] = vpite;
85
        return sum;
86
      }
87
    }else{ // non-arith ite
88
      if(!d_contains.containsTermITE(n)){
89
        // don't bother adding to d_reduceVar
90
        return n;
91
      }else{
92
        Node newIte = applyReduceVariablesInItes(n);
93
        d_reduceVar[n] = (n == newIte) ? Node::null(): newIte;
94
        return newIte;
95
      }
96
    }
97
  }break;
98
  default:
99
    if(n.getType().isReal() && Polynomial::isMember(n)){
100
      Node newn = Node::null();
101
      if(!d_contains.containsTermITE(n)){
102
        newn = n;
103
      }else if(n.getNumChildren() > 0){
104
        newn = applyReduceVariablesInItes(n);
105
        newn = rewrite(newn);
106
        Assert(Polynomial::isMember(newn));
107
      }else{
108
        newn = n;
109
      }
110
111
      Polynomial p = Polynomial::parsePolynomial(newn);
112
      if(p.isConstant()){
113
        d_constants[n] = newn;
114
        d_varParts[n] = mkRationalNode(Rational(0));
115
        // don't bother adding to d_reduceVar
116
        return newn;
117
      }else if(!p.containsConstant()){
118
        d_constants[n] = mkRationalNode(Rational(0));
119
        d_varParts[n] = newn;
120
        d_reduceVar[n] = p.getNode();
121
        return p.getNode();
122
      }else{
123
        Monomial mc = p.getHead();
124
        d_constants[n] = mc.getConstant().getNode();
125
        d_varParts[n] = p.getTail().getNode();
126
        d_reduceVar[n] = newn;
127
        return newn;
128
      }
129
    }else{
130
      if(!d_contains.containsTermITE(n)){
131
        return n;
132
      }
133
      if(n.getNumChildren() > 0){
134
        Node res = applyReduceVariablesInItes(n);
135
        d_reduceVar[n] = res;
136
        return res;
137
      }else{
138
        return n;
139
      }
140
    }
141
    break;
142
  }
143
  Unreachable();
144
}
145
146
ArithIteUtils::ArithIteUtils(
147
    Env& env,
148
    preprocessing::util::ContainsTermITEVisitor& contains,
149
    SubstitutionMap& subs)
150
    : EnvObj(env),
151
      d_contains(contains),
152
      d_subs(subs),
153
      d_one(1),
154
      d_subcount(userContext(), 0),
155
      d_skolems(userContext()),
156
      d_implies(),
157
      d_orBinEqs()
158
{
159
}
160
161
ArithIteUtils::~ArithIteUtils(){
162
}
163
164
void ArithIteUtils::clear(){
165
  d_reduceVar.clear();
166
  d_constants.clear();
167
  d_varParts.clear();
168
}
169
170
const Integer& ArithIteUtils::gcdIte(Node n){
171
  if(d_gcds.find(n) != d_gcds.end()){
172
    return d_gcds[n];
173
  }
174
  if(n.getKind() == kind::CONST_RATIONAL){
175
    const Rational& q = n.getConst<Rational>();
176
    if(q.isIntegral()){
177
      d_gcds[n] = q.getNumerator();
178
      return d_gcds[n];
179
    }else{
180
      return d_one;
181
    }
182
  }else if(n.getKind() == kind::ITE && n.getType().isReal()){
183
    const Integer& tgcd = gcdIte(n[1]);
184
    if(tgcd.isOne()){
185
      d_gcds[n] = d_one;
186
      return d_one;
187
    }else{
188
      const Integer& egcd = gcdIte(n[2]);
189
      Integer ite_gcd = tgcd.gcd(egcd);
190
      d_gcds[n] = ite_gcd;
191
      return d_gcds[n];
192
    }
193
  }
194
  return d_one;
195
}
196
197
Node ArithIteUtils::reduceIteConstantIteByGCD_rec(Node n, const Rational& q){
198
  if(n.isConst()){
199
    Assert(n.getKind() == kind::CONST_RATIONAL);
200
    return mkRationalNode(n.getConst<Rational>() * q);
201
  }else{
202
    Assert(n.getKind() == kind::ITE);
203
    Assert(n.getType().isInteger());
204
    Node rc = reduceConstantIteByGCD(n[0]);
205
    Node rt = reduceIteConstantIteByGCD_rec(n[1], q);
206
    Node re = reduceIteConstantIteByGCD_rec(n[2], q);
207
    return rc.iteNode(rt, re);
208
  }
209
}
210
211
Node ArithIteUtils::reduceIteConstantIteByGCD(Node n){
212
  Assert(n.getKind() == kind::ITE);
213
  Assert(n.getType().isReal());
214
  const Integer& gcd = gcdIte(n);
215
  if(gcd.isOne()){
216
    Node newIte = reduceConstantIteByGCD(n[0]).iteNode(n[1],n[2]);
217
    d_reduceGcd[n] = newIte;
218
    return newIte;
219
  }else if(gcd.isZero()){
220
    Node zeroNode = mkRationalNode(Rational(0));
221
    d_reduceGcd[n] = zeroNode;
222
    return zeroNode;
223
  }else{
224
    Rational divBy(Integer(1), gcd);
225
    Node redite = reduceIteConstantIteByGCD_rec(n, divBy);
226
    Node gcdNode = mkRationalNode(Rational(gcd));
227
    Node multIte = NodeManager::currentNM()->mkNode(kind::MULT, gcdNode, redite);
228
    d_reduceGcd[n] = multIte;
229
    return multIte;
230
  }
231
}
232
233
Node ArithIteUtils::reduceConstantIteByGCD(Node n){
234
  if(d_reduceGcd.find(n) != d_reduceGcd.end()){
235
    return d_reduceGcd[n];
236
  }
237
  if(n.getKind() == kind::ITE && n.getType().isReal()){
238
    return reduceIteConstantIteByGCD(n);
239
  }
240
241
  if(n.getNumChildren() > 0){
242
    NodeBuilder nb(n.getKind());
243
    if(n.getMetaKind() == kind::metakind::PARAMETERIZED) {
244
      nb << (n.getOperator());
245
    }
246
    bool anychange = false;
247
    for(Node::iterator it = n.begin(), end = n.end(); it != end; ++it){
248
      Node child = *it;
249
      Node redchild = reduceConstantIteByGCD(child);
250
      anychange = anychange || (child != redchild);
251
      nb << redchild;
252
    }
253
    if(anychange){
254
      Node res = nb;
255
      d_reduceGcd[n] = res;
256
      return res;
257
    }else{
258
      d_reduceGcd[n] = n;
259
      return n;
260
    }
261
  }else{
262
    return n;
263
  }
264
}
265
266
unsigned ArithIteUtils::getSubCount() const{
267
  return d_subcount;
268
}
269
270
void ArithIteUtils::addSubstitution(TNode f, TNode t){
271
  Debug("arith::ite") << "adding " << f << " -> " << t << endl;
272
  d_subcount = d_subcount + 1;
273
  d_subs.addSubstitution(f, t);
274
}
275
276
Node ArithIteUtils::applySubstitutions(TNode f){
277
  AlwaysAssert(!options().base.incrementalSolving);
278
  return d_subs.apply(f);
279
}
280
281
Node ArithIteUtils::selectForCmp(Node n) const{
282
  if(n.getKind() == kind::ITE){
283
    if(d_skolems.find(n[0]) != d_skolems.end()){
284
      return selectForCmp(n[1]);
285
    }
286
  }
287
  return n;
288
}
289
290
void ArithIteUtils::learnSubstitutions(const std::vector<Node>& assertions){
291
  AlwaysAssert(!options().base.incrementalSolving);
292
  for(size_t i=0, N=assertions.size(); i < N; ++i){
293
    collectAssertions(assertions[i]);
294
  }
295
  bool solvedSomething;
296
  do{
297
    solvedSomething = false;
298
    size_t readPos = 0, writePos = 0, N = d_orBinEqs.size();
299
    for(; readPos < N; readPos++){
300
      Node curr = d_orBinEqs[readPos];
301
      bool solved = solveBinOr(curr);
302
      if(solved){
303
        solvedSomething = true;
304
      }else{
305
        // didn't solve, push back
306
        d_orBinEqs[writePos] = curr;
307
        writePos++;
308
      }
309
    }
310
    Assert(writePos <= N);
311
    d_orBinEqs.resize(writePos);
312
  }while(solvedSomething);
313
314
  d_implies.clear();
315
  d_orBinEqs.clear();
316
}
317
318
void ArithIteUtils::addImplications(Node x, Node y){
319
  // (or x y)
320
  // (=> (not x) y)
321
  // (=> (not y) x)
322
323
  Node xneg = x.negate();
324
  Node yneg = y.negate();
325
  d_implies[xneg].insert(y);
326
  d_implies[yneg].insert(x);
327
}
328
329
void ArithIteUtils::collectAssertions(TNode assertion){
330
  if(assertion.getKind() == kind::OR){
331
    if(assertion.getNumChildren() == 2){
332
      TNode left = assertion[0], right = assertion[1];
333
      addImplications(left, right);
334
      if(left.getKind() == kind::EQUAL && right.getKind() == kind::EQUAL){
335
        if(left[0].getType().isInteger() && right[0].getType().isInteger()){
336
          d_orBinEqs.push_back(assertion);
337
        }
338
      }
339
    }
340
  }else if(assertion.getKind() == kind::AND){
341
    for(unsigned i=0, N=assertion.getNumChildren(); i < N; ++i){
342
      collectAssertions(assertion[i]);
343
    }
344
  }
345
}
346
347
Node ArithIteUtils::findIteCnd(TNode tb, TNode fb) const{
348
  Node negtb = tb.negate();
349
  Node negfb = fb.negate();
350
  ImpMap::const_iterator ti = d_implies.find(negtb);
351
  ImpMap::const_iterator fi = d_implies.find(negfb);
352
353
  if(ti != d_implies.end() && fi != d_implies.end()){
354
    const std::set<Node>& negtimp = ti->second;
355
    const std::set<Node>& negfimp = fi->second;
356
357
    // (or (not x) y)
358
    // (or x z)
359
    // (or y z)
360
    // ---
361
    // (ite x y z) return x
362
    // ---
363
    // (not y) => (not x)
364
    // (not z) => x
365
    std::set<Node>::const_iterator ci = negtimp.begin(), cend = negtimp.end();
366
    for (; ci != cend; ++ci)
367
    {
368
      Node impliedByNotTB = *ci;
369
      Node impliedByNotTBNeg = impliedByNotTB.negate();
370
      if(negfimp.find(impliedByNotTBNeg) != negfimp.end()){
371
        return impliedByNotTBNeg; // implies tb
372
      }
373
    }
374
  }
375
376
  return Node::null();
377
}
378
379
bool ArithIteUtils::solveBinOr(TNode binor){
380
  Assert(binor.getKind() == kind::OR);
381
  Assert(binor.getNumChildren() == 2);
382
  Assert(binor[0].getKind() == kind::EQUAL);
383
  Assert(binor[1].getKind() == kind::EQUAL);
384
385
  //Node n =
386
  Node n = applySubstitutions(binor);
387
  if(n != binor){
388
    n = rewrite(n);
389
390
    if(!(n.getKind() == kind::OR &&
391
	 n.getNumChildren() == 2 &&
392
	 n[0].getKind() ==  kind::EQUAL &&
393
	 n[1].getKind() ==  kind::EQUAL)){
394
      return false;
395
    }
396
  }
397
398
  Assert(n.getKind() == kind::OR);
399
  Assert(n.getNumChildren() == 2);
400
  TNode l = n[0];
401
  TNode r = n[1];
402
403
  Assert(l.getKind() == kind::EQUAL);
404
  Assert(r.getKind() == kind::EQUAL);
405
406
  Debug("arith::ite") << "bin or " << n << endl;
407
408
  bool lArithEq = l.getKind() == kind::EQUAL && l[0].getType().isInteger();
409
  bool rArithEq = r.getKind() == kind::EQUAL && r[0].getType().isInteger();
410
411
  if(lArithEq && rArithEq){
412
    TNode sel = Node::null();
413
    TNode otherL = Node::null();
414
    TNode otherR = Node::null();
415
    if(l[0] == r[0]) {
416
      sel = l[0]; otherL = l[1]; otherR = r[1];
417
    }else if(l[0] == r[1]){
418
      sel = l[0]; otherL = l[1]; otherR = r[0];
419
    }else if(l[1] == r[0]){
420
      sel = l[1]; otherL = l[0]; otherR = r[1];
421
    }else if(l[1] == r[1]){
422
      sel = l[1]; otherL = l[0]; otherR = r[0];
423
    }
424
    Debug("arith::ite") << "selected " << sel << endl;
425
    if(sel.isVar() && sel.getKind() != kind::SKOLEM){
426
427
      Debug("arith::ite") << "others l:" << otherL << " r " << otherR << endl;
428
      Node useForCmpL = selectForCmp(otherL);
429
      Node useForCmpR = selectForCmp(otherR);
430
431
      Assert(Polynomial::isMember(sel));
432
      Assert(Polynomial::isMember(useForCmpL));
433
      Assert(Polynomial::isMember(useForCmpR));
434
      Polynomial lside = Polynomial::parsePolynomial( useForCmpL );
435
      Polynomial rside = Polynomial::parsePolynomial( useForCmpR );
436
      Polynomial diff = lside-rside;
437
438
      Debug("arith::ite") << "diff: " << diff.getNode() << endl;
439
      if(diff.isConstant()){
440
        // a: (sel = otherL) or (sel = otherR), otherL-otherR = c
441
442
        NodeManager* nm = NodeManager::currentNM();
443
        SkolemManager* sm = nm->getSkolemManager();
444
445
        Node cnd = findIteCnd(binor[0], binor[1]);
446
447
        Node sk = sm->mkDummySkolem("deor", nm->booleanType());
448
        Node ite = sk.iteNode(otherL, otherR);
449
        d_skolems.insert(sk, cnd);
450
        addSubstitution(sel, ite);
451
        return true;
452
      }
453
    }
454
  }
455
  return false;
456
}
457
458
}  // namespace arith
459
}  // namespace theory
460
31137
}  // namespace cvc5