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

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
13193
void RelevantDomain::RDomain::merge( RDomain * r ) {
35
13193
  Assert(!d_parent);
36
13193
  Assert(!r->d_parent);
37
13193
  d_parent = r;
38
13794
  for( unsigned i=0; i<d_terms.size(); i++ ){
39
601
    r->addTerm( d_terms[i] );
40
  }
41
13193
  d_terms.clear();
42
13193
}
43
44
12644
void RelevantDomain::RDomain::addTerm( Node t ) {
45
12644
  if( std::find( d_terms.begin(), d_terms.end(), t )==d_terms.end() ){
46
2236
    d_terms.push_back( t );
47
  }
48
12644
}
49
50
417370
RelevantDomain::RDomain * RelevantDomain::RDomain::getParent() {
51
417370
  if( !d_parent ){
52
207118
    return this;
53
  }else{
54
210252
    RDomain * p = d_parent->getParent();
55
210252
    d_parent = p;
56
210252
    return p;
57
  }
58
}
59
60
893
void RelevantDomain::RDomain::removeRedundantTerms(QuantifiersState& qs)
61
{
62
1786
  std::map< Node, Node > rterms;
63
2528
  for( unsigned i=0; i<d_terms.size(); i++ ){
64
3270
    Node r = d_terms[i];
65
1635
    if( !TermUtil::hasInstConstAttr( d_terms[i] ) ){
66
1635
      r = qs.getRepresentative(d_terms[i]);
67
    }
68
1635
    if( rterms.find( r )==rterms.end() ){
69
1523
      rterms[r] = d_terms[i];
70
    }
71
  }
72
893
  d_terms.clear();
73
2416
  for( std::map< Node, Node >::iterator it = rterms.begin(); it != rterms.end(); ++it ){
74
1523
    d_terms.push_back( it->second );
75
  }
76
893
}
77
78
98
RelevantDomain::RelevantDomain(Env& env,
79
                               QuantifiersState& qs,
80
                               QuantifiersRegistry& qr,
81
98
                               TermRegistry& tr)
82
98
    : QuantifiersUtil(env), d_qs(qs), d_qreg(qr), d_treg(tr)
83
{
84
98
  d_is_computed = false;
85
98
}
86
87
294
RelevantDomain::~RelevantDomain() {
88
820
  for (auto& r : d_rel_doms)
89
  {
90
5869
    for (auto& rr : r.second)
91
    {
92
5147
      RDomain* current = rr.second;
93
5147
      Assert(current != NULL);
94
5147
      delete current;
95
    }
96
  }
97
196
}
98
99
192812
RelevantDomain::RDomain* RelevantDomain::getRDomain(Node n,
100
                                                    size_t i,
101
                                                    bool getParent)
102
{
103
192812
  if( d_rel_doms.find( n )==d_rel_doms.end() || d_rel_doms[n].find( i )==d_rel_doms[n].end() ){
104
5147
    d_rel_doms[n][i] = new RDomain;
105
  }
106
192812
  return getParent ? d_rel_doms[n][i]->getParent() : d_rel_doms[n][i];
107
}
108
109
1214
bool RelevantDomain::reset( Theory::Effort e ) {
110
1214
  d_is_computed = false;
111
1214
  return true;
112
}
113
114
3732
void RelevantDomain::registerQuantifier(Node q) {}
115
115
void RelevantDomain::compute(){
116
115
  if( !d_is_computed ){
117
115
    d_is_computed = true;
118
1153
    for (auto& r : d_rel_doms)
119
    {
120
10027
      for (auto& rr : r.second)
121
      {
122
8989
        rr.second->reset();
123
      }
124
    }
125
115
    FirstOrderModel* fm = d_treg.getModel();
126
1190
    for( unsigned i=0; i<fm->getNumAssertedQuantifiers(); i++ ){
127
2150
      Node q = fm->getAssertedQuantifier( i );
128
2150
      Node icf = d_qreg.getInstConstantBody(q);
129
1075
      Trace("rel-dom-debug") << "compute relevant domain for " << icf << std::endl;
130
1075
      computeRelevantDomain(q);
131
    }
132
133
115
    Trace("rel-dom-debug") << "account for ground terms" << std::endl;
134
115
    TermDb* db = d_treg.getTermDatabase();
135
768
    for (unsigned k = 0; k < db->getNumOperators(); k++)
136
    {
137
1306
      Node op = db->getOperator(k);
138
653
      unsigned sz = db->getNumGroundTerms( op );
139
3340
      for( unsigned i=0; i<sz; i++ ){
140
5374
        Node n = db->getGroundTerm(op, i);
141
        //if it is a non-redundant term
142
2687
        if( db->isTermActive( n ) ){
143
13256
          for( unsigned j=0; j<n.getNumChildren(); j++ ){
144
11230
            RDomain * rf = getRDomain( op, j );
145
11230
            rf->addTerm( n[j] );
146
11230
            Trace("rel-dom-debug") << "...add ground term " << n[j] << " to rel dom " << op << "[" << j << "]" << std::endl;
147
          }
148
        }
149
      }
150
    }
151
    //print debug
152
1851
    for (std::pair<const Node, std::map<size_t, RDomain*> >& d : d_rel_doms)
153
    {
154
3472
      Trace("rel-dom") << "Relevant domain for " << d.first << " : "
155
1736
                       << std::endl;
156
15822
      for (std::pair<const size_t, RDomain*>& dd : d.second)
157
      {
158
14086
        Trace("rel-dom") << "   " << dd.first << " : ";
159
14086
        RDomain* r = dd.second;
160
14086
        RDomain * rp = r->getParent();
161
14086
        if( r==rp ){
162
893
          r->removeRedundantTerms(d_qs);
163
893
          Trace("rel-dom") << r->d_terms;
164
        }else{
165
13193
          Trace("rel-dom") << "Dom( " << d.first << ", " << dd.first << " ) ";
166
        }
167
14086
        Trace("rel-dom") << std::endl;
168
14086
        if (Configuration::isAssertionBuild())
169
        {
170
14086
          if (d.first.getKind() == FORALL)
171
          {
172
18704
            TypeNode expectedType = d.first[0][dd.first].getType();
173
9639
            for (const Node& t : r->d_terms)
174
            {
175
287
              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
115
}
187
188
1075
void RelevantDomain::computeRelevantDomain(Node q)
189
{
190
1075
  Assert(q.getKind() == FORALL);
191
2150
  Node n = d_qreg.getInstConstantBody(q);
192
  // we care about polarity in the traversal, so we use a polarity term context
193
2150
  PolarityTermContext tc;
194
2150
  TCtxStack ctx(&tc);
195
1075
  ctx.pushInitial(n);
196
  std::unordered_set<std::pair<Node, uint32_t>,
197
                     PairHashFunction<Node, uint32_t, std::hash<Node> > >
198
2150
      visited;
199
2150
  std::pair<Node, uint32_t> curr;
200
2150
  Node node;
201
  uint32_t nodeVal;
202
  std::unordered_set<
203
      std::pair<Node, uint32_t>,
204
1075
      PairHashFunction<Node, uint32_t, std::hash<Node> > >::const_iterator itc;
205
  bool hasPol, pol;
206
202183
  while (!ctx.empty())
207
  {
208
100554
    curr = ctx.getCurrent();
209
100554
    itc = visited.find(curr);
210
100554
    ctx.pop();
211
100554
    if (itc == visited.end())
212
    {
213
31240
      visited.insert(curr);
214
31240
      node = curr.first;
215
      // if not a quantified formula
216
31240
      if (!node.isClosure())
217
      {
218
31134
        nodeVal = curr.second;
219
        // get the polarity of the current term and process it
220
31134
        PolarityTermContext::getFlags(nodeVal, hasPol, pol);
221
31134
        computeRelevantDomainNode(q, node, hasPol, pol);
222
        // traverse the children
223
31134
        ctx.pushChildren(node, nodeVal);
224
      }
225
    }
226
  }
227
1075
}
228
229
31134
void RelevantDomain::computeRelevantDomainNode(Node q,
230
                                               Node n,
231
                                               bool hasPol,
232
                                               bool pol)
233
{
234
31134
  Trace("rel-dom-debug") << "Compute relevant domain " << n << "..." << std::endl;
235
62268
  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
31134
  if (!op.isNull() && op == n.getOperator())
240
  {
241
87973
    for (size_t i = 0, nchild = n.getNumChildren(); i < nchild; i++)
242
    {
243
77952
      RDomain * rf = getRDomain( op, i );
244
77952
      if( n[i].getKind()==ITE ){
245
        for( unsigned j=1; j<=2; j++ ){
246
          computeRelevantDomainOpCh( rf, n[i][j] );
247
        }
248
      }else{
249
77952
        computeRelevantDomainOpCh( rf, n[i] );
250
      }
251
    }
252
  }
253
254
31134
  if( ( ( n.getKind()==EQUAL && !n[0].getType().isBoolean() ) || n.getKind()==GEQ ) && TermUtil::hasInstConstAttr( n ) ){
255
    //compute the information for what this literal does
256
663
    computeRelevantDomainLit( q, hasPol, pol, n );
257
663
    RDomainLit& rdl = d_rel_dom_lit[hasPol][pol][n];
258
663
    if (rdl.d_merge)
259
    {
260
111
      Assert(rdl.d_rd[0] != nullptr && rdl.d_rd[1] != nullptr);
261
111
      RDomain* rd1 = rdl.d_rd[0]->getParent();
262
111
      RDomain* rd2 = rdl.d_rd[1]->getParent();
263
111
      if( rd1!=rd2 ){
264
75
        rd1->merge( rd2 );
265
      }
266
    }
267
    else
268
    {
269
552
      if (rdl.d_rd[0] != nullptr)
270
      {
271
228
        RDomain* rd = rdl.d_rd[0]->getParent();
272
598
        for (unsigned i = 0; i < rdl.d_val.size(); i++)
273
        {
274
370
          rd->addTerm(rdl.d_val[i]);
275
        }
276
      }
277
    }
278
  }
279
31134
  Trace("rel-dom-debug") << "...finished Compute relevant domain " << n << std::endl;
280
31134
}
281
282
77952
void RelevantDomain::computeRelevantDomainOpCh( RDomain * rf, Node n ) {
283
77952
  if( n.getKind()==INST_CONSTANT ){
284
154732
    Node q = TermUtil::getInstConstAttr(n);
285
    //merge the RDomains
286
77366
    size_t id = n.getAttribute(InstVarNumAttribute());
287
77366
    Assert(q[0][id].getType() == n.getType());
288
77366
    Trace("rel-dom-debug") << n << " is variable # " << id << " for " << q;
289
154732
    Trace("rel-dom-debug") << " with body : " << d_qreg.getInstConstantBody(q)
290
77366
                           << std::endl;
291
77366
    RDomain * rq = getRDomain( q, id );
292
77366
    if( rf!=rq ){
293
13118
      rq->merge( rf );
294
    }
295
586
  }else if( !TermUtil::hasInstConstAttr( n ) ){
296
443
    Trace("rel-dom-debug") << "...add ground term to rel dom " << n << std::endl;
297
    //term to add
298
443
    rf->addTerm( n );
299
  }
300
77952
}
301
302
663
void RelevantDomain::computeRelevantDomainLit( Node q, bool hasPol, bool pol, Node n ) {
303
663
  if( d_rel_dom_lit[hasPol][pol].find( n )==d_rel_dom_lit[hasPol][pol].end() ){
304
357
    RDomainLit& rdl = d_rel_dom_lit[hasPol][pol][n];
305
357
    rdl.d_merge = false;
306
357
    int varCount = 0;
307
357
    int varCh = -1;
308
1071
    for( unsigned i=0; i<n.getNumChildren(); i++ ){
309
714
      if( n[i].getKind()==INST_CONSTANT ){
310
        // must get the quantified formula this belongs to, which may be
311
        // different from q
312
418
        Node qi = TermUtil::getInstConstAttr(n[i]);
313
209
        unsigned id = n[i].getAttribute(InstVarNumAttribute());
314
209
        rdl.d_rd[i] = getRDomain(qi, id, false);
315
209
        varCount++;
316
209
        varCh = i;
317
      }else{
318
505
        rdl.d_rd[i] = nullptr;
319
      }
320
    }
321
322
714
    Node r_add;
323
357
    bool varLhs = true;
324
357
    if( varCount==2 ){
325
15
      rdl.d_merge = true;
326
    }else{
327
342
      if( varCount==1 ){
328
179
        r_add = n[1-varCh];
329
179
        varLhs = (varCh==0);
330
179
        rdl.d_rd[0] = rdl.d_rd[varCh];
331
179
        rdl.d_rd[1] = nullptr;
332
      }else{
333
        //solve the inequality for one/two variables, if possible
334
163
        if( n[0].getType().isReal() ){
335
262
          std::map< Node, Node > msum;
336
131
          if (ArithMSum::getMonomialSumLit(n, msum))
337
          {
338
262
            Node var;
339
262
            Node var2;
340
131
            bool hasNonVar = false;
341
392
            for( std::map< Node, Node >::iterator it = msum.begin(); it != msum.end(); ++it ){
342
762
              if (!it->first.isNull() && it->first.getKind() == INST_CONSTANT
343
577
                  && TermUtil::getInstConstAttr(it->first) == q)
344
              {
345
55
                if( var.isNull() ){
346
33
                  var = it->first;
347
22
                }else if( var2.isNull() ){
348
22
                  var2 = it->first;
349
                }else{
350
                  hasNonVar = true;
351
                }
352
              }else{
353
206
                hasNonVar = true;
354
              }
355
            }
356
262
            Trace("rel-dom") << "Process lit " << n << ", var/var2=" << var
357
131
                             << "/" << var2 << std::endl;
358
131
            if( !var.isNull() ){
359
33
              Assert(var.hasAttribute(InstVarNumAttribute()));
360
33
              if( var2.isNull() ){
361
                //single variable solve
362
22
                Node veq_c;
363
22
                Node val;
364
                int ires =
365
11
                    ArithMSum::isolate(var, msum, veq_c, val, n.getKind());
366
11
                if( ires!=0 ){
367
11
                  if( veq_c.isNull() ){
368
3
                    r_add = val;
369
3
                    varLhs = (ires==1);
370
3
                    rdl.d_rd[0] = getRDomain(
371
3
                        q, var.getAttribute(InstVarNumAttribute()), false);
372
3
                    rdl.d_rd[1] = nullptr;
373
                  }
374
                }
375
22
              }else if( !hasNonVar ){
376
9
                Assert(var2.hasAttribute(InstVarNumAttribute()));
377
                //merge the domains
378
9
                rdl.d_rd[0] = getRDomain(
379
9
                    q, var.getAttribute(InstVarNumAttribute()), false);
380
9
                rdl.d_rd[1] = getRDomain(
381
9
                    q, var2.getAttribute(InstVarNumAttribute()), false);
382
9
                rdl.d_merge = true;
383
              }
384
            }
385
          }
386
        }
387
      }
388
    }
389
357
    if (rdl.d_merge)
390
    {
391
      //do not merge if constant negative polarity
392
24
      if( hasPol && !pol ){
393
        rdl.d_merge = false;
394
      }
395
    }
396
333
    else if (!r_add.isNull() && !TermUtil::hasInstConstAttr(r_add))
397
    {
398
163
      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
163
      if( !hasPol || !pol ){
401
163
        rdl.d_val.push_back(r_add);
402
      }
403
      //the positive occurence adds other terms
404
163
      if( ( !hasPol || pol ) && n[0].getType().isInteger() ){
405
138
        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
138
        }else if( n.getKind()==GEQ ){
410
138
          rdl.d_val.push_back(ArithMSum::offset(r_add, varLhs ? 1 : -1));
411
        }
412
      }
413
    }
414
    else
415
    {
416
170
      rdl.d_rd[0] = nullptr;
417
170
      rdl.d_rd[1] = nullptr;
418
    }
419
  }
420
663
}
421
422
}  // namespace quantifiers
423
}  // namespace theory
424
29577
}  // namespace cvc5