GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/relevant_domain.cpp Lines: 226 234 96.6 %
Date: 2021-09-29 Branches: 451 894 50.4 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Mathias Preiner, Aina Niemetz
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
 * Implementation of relevant domain class.
14
 */
15
16
#include "theory/quantifiers/relevant_domain.h"
17
18
#include "expr/term_context.h"
19
#include "expr/term_context_stack.h"
20
#include "theory/arith/arith_msum.h"
21
#include "theory/quantifiers/first_order_model.h"
22
#include "theory/quantifiers/quantifiers_registry.h"
23
#include "theory/quantifiers/quantifiers_state.h"
24
#include "theory/quantifiers/term_database.h"
25
#include "theory/quantifiers/term_registry.h"
26
#include "theory/quantifiers/term_util.h"
27
28
using namespace cvc5::kind;
29
30
namespace cvc5 {
31
namespace theory {
32
namespace quantifiers {
33
34
3985
void RelevantDomain::RDomain::merge( RDomain * r ) {
35
3985
  Assert(!d_parent);
36
3985
  Assert(!r->d_parent);
37
3985
  d_parent = r;
38
4171
  for( unsigned i=0; i<d_terms.size(); i++ ){
39
186
    r->addTerm( d_terms[i] );
40
  }
41
3985
  d_terms.clear();
42
3985
}
43
44
4228
void RelevantDomain::RDomain::addTerm( Node t ) {
45
4228
  if( std::find( d_terms.begin(), d_terms.end(), t )==d_terms.end() ){
46
717
    d_terms.push_back( t );
47
  }
48
4228
}
49
50
129367
RelevantDomain::RDomain * RelevantDomain::RDomain::getParent() {
51
129367
  if( !d_parent ){
52
64561
    return this;
53
  }else{
54
64806
    RDomain * p = d_parent->getParent();
55
64806
    d_parent = p;
56
64806
    return p;
57
  }
58
}
59
60
332
void RelevantDomain::RDomain::removeRedundantTerms(QuantifiersState& qs)
61
{
62
664
  std::map< Node, Node > rterms;
63
863
  for( unsigned i=0; i<d_terms.size(); i++ ){
64
1062
    Node r = d_terms[i];
65
531
    if( !TermUtil::hasInstConstAttr( d_terms[i] ) ){
66
531
      r = qs.getRepresentative(d_terms[i]);
67
    }
68
531
    if( rterms.find( r )==rterms.end() ){
69
473
      rterms[r] = d_terms[i];
70
    }
71
  }
72
332
  d_terms.clear();
73
805
  for( std::map< Node, Node >::iterator it = rterms.begin(); it != rterms.end(); ++it ){
74
473
    d_terms.push_back( it->second );
75
  }
76
332
}
77
78
31
RelevantDomain::RelevantDomain(Env& env,
79
                               QuantifiersState& qs,
80
                               QuantifiersRegistry& qr,
81
31
                               TermRegistry& tr)
82
31
    : QuantifiersUtil(env), d_qs(qs), d_qreg(qr), d_treg(tr)
83
{
84
31
  d_is_computed = false;
85
31
}
86
87
93
RelevantDomain::~RelevantDomain() {
88
245
  for (auto& r : d_rel_doms)
89
  {
90
1673
    for (auto& rr : r.second)
91
    {
92
1459
      RDomain* current = rr.second;
93
1459
      Assert(current != NULL);
94
1459
      delete current;
95
    }
96
  }
97
62
}
98
99
60109
RelevantDomain::RDomain* RelevantDomain::getRDomain(Node n,
100
                                                    size_t i,
101
                                                    bool getParent)
102
{
103
60109
  if( d_rel_doms.find( n )==d_rel_doms.end() || d_rel_doms[n].find( i )==d_rel_doms[n].end() ){
104
1459
    d_rel_doms[n][i] = new RDomain;
105
  }
106
60109
  return getParent ? d_rel_doms[n][i]->getParent() : d_rel_doms[n][i];
107
}
108
109
419
bool RelevantDomain::reset( Theory::Effort e ) {
110
419
  d_is_computed = false;
111
419
  return true;
112
}
113
114
1919
void RelevantDomain::registerQuantifier(Node q) {}
115
45
void RelevantDomain::compute(){
116
45
  if( !d_is_computed ){
117
45
    d_is_computed = true;
118
452
    for (auto& r : d_rel_doms)
119
    {
120
3284
      for (auto& rr : r.second)
121
      {
122
2877
        rr.second->reset();
123
      }
124
    }
125
45
    FirstOrderModel* fm = d_treg.getModel();
126
465
    for( unsigned i=0; i<fm->getNumAssertedQuantifiers(); i++ ){
127
840
      Node q = fm->getAssertedQuantifier( i );
128
840
      Node icf = d_qreg.getInstConstantBody(q);
129
420
      Trace("rel-dom-debug") << "compute relevant domain for " << icf << std::endl;
130
420
      computeRelevantDomain(q);
131
    }
132
133
45
    Trace("rel-dom-debug") << "account for ground terms" << std::endl;
134
45
    TermDb* db = d_treg.getTermDatabase();
135
234
    for (unsigned k = 0; k < db->getNumOperators(); k++)
136
    {
137
378
      Node op = db->getOperator(k);
138
189
      unsigned sz = db->getNumGroundTerms( op );
139
1242
      for( unsigned i=0; i<sz; i++ ){
140
2106
        Node n = db->getGroundTerm(op, i);
141
        //if it is a non-redundant term
142
1053
        if( db->isTermActive( n ) ){
143
4249
          for( unsigned j=0; j<n.getNumChildren(); j++ ){
144
3591
            RDomain * rf = getRDomain( op, j );
145
3591
            rf->addTerm( n[j] );
146
3591
            Trace("rel-dom-debug") << "...add ground term " << n[j] << " to rel dom " << op << "[" << j << "]" << std::endl;
147
          }
148
        }
149
      }
150
    }
151
    //print debug
152
658
    for (std::pair<const Node, std::map<size_t, RDomain*> >& d : d_rel_doms)
153
    {
154
1226
      Trace("rel-dom") << "Relevant domain for " << d.first << " : "
155
613
                       << std::endl;
156
4930
      for (std::pair<const size_t, RDomain*>& dd : d.second)
157
      {
158
4317
        Trace("rel-dom") << "   " << dd.first << " : ";
159
4317
        RDomain* r = dd.second;
160
4317
        RDomain * rp = r->getParent();
161
4317
        if( r==rp ){
162
332
          r->removeRedundantTerms(d_qs);
163
332
          Trace("rel-dom") << r->d_terms;
164
        }else{
165
3985
          Trace("rel-dom") << "Dom( " << d.first << ", " << dd.first << " ) ";
166
        }
167
4317
        Trace("rel-dom") << std::endl;
168
4317
        if (Configuration::isAssertionBuild())
169
        {
170
4317
          if (d.first.getKind() == FORALL)
171
          {
172
5930
            TypeNode expectedType = d.first[0][dd.first].getType();
173
3055
            for (const Node& t : r->d_terms)
174
            {
175
90
              if (!t.getType().isComparableTo(expectedType))
176
              {
177
                Unhandled() << "Relevant domain: bad type " << t.getType()
178
                            << ", expected " << expectedType;
179
              }
180
            }
181
          }
182
        }
183
      }
184
    }
185
  }
186
45
}
187
188
420
void RelevantDomain::computeRelevantDomain(Node q)
189
{
190
420
  Assert(q.getKind() == FORALL);
191
840
  Node n = d_qreg.getInstConstantBody(q);
192
  // we care about polarity in the traversal, so we use a polarity term context
193
840
  PolarityTermContext tc;
194
840
  TCtxStack ctx(&tc);
195
420
  ctx.pushInitial(n);
196
  std::unordered_set<std::pair<Node, uint32_t>,
197
                     PairHashFunction<Node, uint32_t, std::hash<Node> > >
198
840
      visited;
199
840
  std::pair<Node, uint32_t> curr;
200
840
  Node node;
201
  uint32_t nodeVal;
202
  std::unordered_set<
203
      std::pair<Node, uint32_t>,
204
420
      PairHashFunction<Node, uint32_t, std::hash<Node> > >::const_iterator itc;
205
  bool hasPol, pol;
206
63632
  while (!ctx.empty())
207
  {
208
31606
    curr = ctx.getCurrent();
209
31606
    itc = visited.find(curr);
210
31606
    ctx.pop();
211
31606
    if (itc == visited.end())
212
    {
213
10159
      visited.insert(curr);
214
10159
      node = curr.first;
215
      // if not a quantified formula
216
10159
      if (!node.isClosure())
217
      {
218
10104
        nodeVal = curr.second;
219
        // get the polarity of the current term and process it
220
10104
        PolarityTermContext::getFlags(nodeVal, hasPol, pol);
221
10104
        computeRelevantDomainNode(q, node, hasPol, pol);
222
        // traverse the children
223
10104
        ctx.pushChildren(node, nodeVal);
224
      }
225
    }
226
  }
227
420
}
228
229
10104
void RelevantDomain::computeRelevantDomainNode(Node q,
230
                                               Node n,
231
                                               bool hasPol,
232
                                               bool pol)
233
{
234
10104
  Trace("rel-dom-debug") << "Compute relevant domain " << n << "..." << std::endl;
235
20208
  Node op = d_treg.getTermDatabase()->getMatchOperator(n);
236
  // Relevant domain only makes sense for non-parametric operators, thus we
237
  // check op==n.getOperator() here. This otherwise would lead to bad types
238
  // for terms in the relevant domain.
239
10104
  if (!op.isNull() && op == n.getOperator())
240
  {
241
27318
    for (size_t i = 0, nchild = n.getNumChildren(); i < nchild; i++)
242
    {
243
24148
      RDomain * rf = getRDomain( op, i );
244
24148
      if( n[i].getKind()==ITE ){
245
        for( unsigned j=1; j<=2; j++ ){
246
          computeRelevantDomainOpCh( rf, n[i][j] );
247
        }
248
      }else{
249
24148
        computeRelevantDomainOpCh( rf, n[i] );
250
      }
251
    }
252
  }
253
254
10104
  if( ( ( n.getKind()==EQUAL && !n[0].getType().isBoolean() ) || n.getKind()==GEQ ) && TermUtil::hasInstConstAttr( n ) ){
255
    //compute the information for what this literal does
256
336
    computeRelevantDomainLit( q, hasPol, pol, n );
257
336
    RDomainLit& rdl = d_rel_dom_lit[hasPol][pol][n];
258
336
    if (rdl.d_merge)
259
    {
260
48
      Assert(rdl.d_rd[0] != nullptr && rdl.d_rd[1] != nullptr);
261
48
      RDomain* rd1 = rdl.d_rd[0]->getParent();
262
48
      RDomain* rd2 = rdl.d_rd[1]->getParent();
263
48
      if( rd1!=rd2 ){
264
39
        rd1->merge( rd2 );
265
      }
266
    }
267
    else
268
    {
269
288
      if (rdl.d_rd[0] != nullptr)
270
      {
271
120
        RDomain* rd = rdl.d_rd[0]->getParent();
272
279
        for (unsigned i = 0; i < rdl.d_val.size(); i++)
273
        {
274
159
          rd->addTerm(rdl.d_val[i]);
275
        }
276
      }
277
    }
278
  }
279
10104
  Trace("rel-dom-debug") << "...finished Compute relevant domain " << n << std::endl;
280
10104
}
281
282
24148
void RelevantDomain::computeRelevantDomainOpCh( RDomain * rf, Node n ) {
283
24148
  if( n.getKind()==INST_CONSTANT ){
284
47640
    Node q = TermUtil::getInstConstAttr(n);
285
    //merge the RDomains
286
23820
    size_t id = n.getAttribute(InstVarNumAttribute());
287
23820
    Assert(q[0][id].getType() == n.getType());
288
23820
    Trace("rel-dom-debug") << n << " is variable # " << id << " for " << q;
289
47640
    Trace("rel-dom-debug") << " with body : " << d_qreg.getInstConstantBody(q)
290
23820
                           << std::endl;
291
23820
    RDomain * rq = getRDomain( q, id );
292
23820
    if( rf!=rq ){
293
3946
      rq->merge( rf );
294
    }
295
328
  }else if( !TermUtil::hasInstConstAttr( n ) ){
296
292
    Trace("rel-dom-debug") << "...add ground term to rel dom " << n << std::endl;
297
    //term to add
298
292
    rf->addTerm( n );
299
  }
300
24148
}
301
302
336
void RelevantDomain::computeRelevantDomainLit( Node q, bool hasPol, bool pol, Node n ) {
303
336
  if( d_rel_dom_lit[hasPol][pol].find( n )==d_rel_dom_lit[hasPol][pol].end() ){
304
132
    RDomainLit& rdl = d_rel_dom_lit[hasPol][pol][n];
305
132
    rdl.d_merge = false;
306
132
    int varCount = 0;
307
132
    int varCh = -1;
308
396
    for( unsigned i=0; i<n.getNumChildren(); i++ ){
309
264
      if( n[i].getKind()==INST_CONSTANT ){
310
        // must get the quantified formula this belongs to, which may be
311
        // different from q
312
148
        Node qi = TermUtil::getInstConstAttr(n[i]);
313
74
        unsigned id = n[i].getAttribute(InstVarNumAttribute());
314
74
        rdl.d_rd[i] = getRDomain(qi, id, false);
315
74
        varCount++;
316
74
        varCh = i;
317
      }else{
318
190
        rdl.d_rd[i] = nullptr;
319
      }
320
    }
321
322
264
    Node r_add;
323
132
    bool varLhs = true;
324
132
    if( varCount==2 ){
325
6
      rdl.d_merge = true;
326
    }else{
327
126
      if( varCount==1 ){
328
62
        r_add = n[1-varCh];
329
62
        varLhs = (varCh==0);
330
62
        rdl.d_rd[0] = rdl.d_rd[varCh];
331
62
        rdl.d_rd[1] = nullptr;
332
      }else{
333
        //solve the inequality for one/two variables, if possible
334
64
        if( n[0].getType().isReal() ){
335
78
          std::map< Node, Node > msum;
336
39
          if (ArithMSum::getMonomialSumLit(n, msum))
337
          {
338
78
            Node var;
339
78
            Node var2;
340
39
            bool hasNonVar = false;
341
117
            for( std::map< Node, Node >::iterator it = msum.begin(); it != msum.end(); ++it ){
342
228
              if (!it->first.isNull() && it->first.getKind() == INST_CONSTANT
343
173
                  && TermUtil::getInstConstAttr(it->first) == q)
344
              {
345
17
                if( var.isNull() ){
346
10
                  var = it->first;
347
7
                }else if( var2.isNull() ){
348
7
                  var2 = it->first;
349
                }else{
350
                  hasNonVar = true;
351
                }
352
              }else{
353
61
                hasNonVar = true;
354
              }
355
            }
356
78
            Trace("rel-dom") << "Process lit " << n << ", var/var2=" << var
357
39
                             << "/" << var2 << std::endl;
358
39
            if( !var.isNull() ){
359
10
              Assert(var.hasAttribute(InstVarNumAttribute()));
360
10
              if( var2.isNull() ){
361
                //single variable solve
362
6
                Node veq_c;
363
6
                Node val;
364
                int ires =
365
3
                    ArithMSum::isolate(var, msum, veq_c, val, n.getKind());
366
3
                if( ires!=0 ){
367
3
                  if( veq_c.isNull() ){
368
1
                    r_add = val;
369
1
                    varLhs = (ires==1);
370
1
                    rdl.d_rd[0] = getRDomain(
371
1
                        q, var.getAttribute(InstVarNumAttribute()), false);
372
1
                    rdl.d_rd[1] = nullptr;
373
                  }
374
                }
375
7
              }else if( !hasNonVar ){
376
3
                Assert(var2.hasAttribute(InstVarNumAttribute()));
377
                //merge the domains
378
3
                rdl.d_rd[0] = getRDomain(
379
3
                    q, var.getAttribute(InstVarNumAttribute()), false);
380
3
                rdl.d_rd[1] = getRDomain(
381
3
                    q, var2.getAttribute(InstVarNumAttribute()), false);
382
3
                rdl.d_merge = true;
383
              }
384
            }
385
          }
386
        }
387
      }
388
    }
389
132
    if (rdl.d_merge)
390
    {
391
      //do not merge if constant negative polarity
392
9
      if( hasPol && !pol ){
393
        rdl.d_merge = false;
394
      }
395
    }
396
123
    else if (!r_add.isNull() && !TermUtil::hasInstConstAttr(r_add))
397
    {
398
58
      Trace("rel-dom-debug") << "...add term " << r_add << ", pol = " << pol << ", kind = " << n.getKind() << std::endl;
399
      //the negative occurrence adds the term to the domain
400
58
      if( !hasPol || !pol ){
401
58
        rdl.d_val.push_back(r_add);
402
      }
403
      //the positive occurence adds other terms
404
58
      if( ( !hasPol || pol ) && n[0].getType().isInteger() ){
405
38
        if( n.getKind()==EQUAL ){
406
          for( unsigned i=0; i<2; i++ ){
407
            rdl.d_val.push_back(ArithMSum::offset(r_add, i == 0 ? 1 : -1));
408
          }
409
38
        }else if( n.getKind()==GEQ ){
410
38
          rdl.d_val.push_back(ArithMSum::offset(r_add, varLhs ? 1 : -1));
411
        }
412
      }
413
    }
414
    else
415
    {
416
65
      rdl.d_rd[0] = nullptr;
417
65
      rdl.d_rd[1] = nullptr;
418
    }
419
  }
420
336
}
421
422
}  // namespace quantifiers
423
}  // namespace theory
424
22746
}  // namespace cvc5