GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/relevant_domain.cpp Lines: 215 222 96.8 %
Date: 2021-08-01 Branches: 492 954 51.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
13338
void RelevantDomain::RDomain::merge( RDomain * r ) {
35
13338
  Assert(!d_parent);
36
13338
  Assert(!r->d_parent);
37
13338
  d_parent = r;
38
13967
  for( unsigned i=0; i<d_terms.size(); i++ ){
39
629
    r->addTerm( d_terms[i] );
40
  }
41
13338
  d_terms.clear();
42
13338
}
43
44
12678
void RelevantDomain::RDomain::addTerm( Node t ) {
45
12678
  if( std::find( d_terms.begin(), d_terms.end(), t )==d_terms.end() ){
46
2022
    d_terms.push_back( t );
47
  }
48
12678
}
49
50
419825
RelevantDomain::RDomain * RelevantDomain::RDomain::getParent() {
51
419825
  if( !d_parent ){
52
208247
    return this;
53
  }else{
54
211578
    RDomain * p = d_parent->getParent();
55
211578
    d_parent = p;
56
211578
    return p;
57
  }
58
}
59
60
772
void RelevantDomain::RDomain::removeRedundantTerms(QuantifiersState& qs)
61
{
62
1544
  std::map< Node, Node > rterms;
63
2165
  for( unsigned i=0; i<d_terms.size(); i++ ){
64
2786
    Node r = d_terms[i];
65
1393
    if( !TermUtil::hasInstConstAttr( d_terms[i] ) ){
66
1393
      r = qs.getRepresentative(d_terms[i]);
67
    }
68
1393
    if( rterms.find( r )==rterms.end() ){
69
1274
      rterms[r] = d_terms[i];
70
    }
71
  }
72
772
  d_terms.clear();
73
2046
  for( std::map< Node, Node >::iterator it = rterms.begin(); it != rterms.end(); ++it ){
74
1274
    d_terms.push_back( it->second );
75
  }
76
772
}
77
78
98
RelevantDomain::RelevantDomain(QuantifiersState& qs,
79
                               QuantifiersRegistry& qr,
80
98
                               TermRegistry& tr)
81
98
    : d_qs(qs), d_qreg(qr), d_treg(tr)
82
{
83
98
  d_is_computed = false;
84
98
}
85
86
294
RelevantDomain::~RelevantDomain() {
87
830
  for( std::map< Node, std::map< int, RDomain * > >::iterator itr = d_rel_doms.begin(); itr != d_rel_doms.end(); ++itr ){
88
5891
    for( std::map< int, RDomain * >::iterator itr2 = itr->second.begin(); itr2 != itr->second.end(); ++itr2 ){
89
5159
      RDomain * current = (*itr2).second;
90
5159
      Assert(current != NULL);
91
5159
      delete current;
92
    }
93
  }
94
196
}
95
96
193910
RelevantDomain::RDomain * RelevantDomain::getRDomain( Node n, int i, bool getParent ) {
97
193910
  if( d_rel_doms.find( n )==d_rel_doms.end() || d_rel_doms[n].find( i )==d_rel_doms[n].end() ){
98
5159
    d_rel_doms[n][i] = new RDomain;
99
5159
    d_rn_map[d_rel_doms[n][i]] = n;
100
5159
    d_ri_map[d_rel_doms[n][i]] = i;
101
  }
102
193910
  return getParent ? d_rel_doms[n][i]->getParent() : d_rel_doms[n][i];
103
}
104
105
1292
bool RelevantDomain::reset( Theory::Effort e ) {
106
1292
  d_is_computed = false;
107
1292
  return true;
108
}
109
110
3867
void RelevantDomain::registerQuantifier(Node q) {}
111
112
void RelevantDomain::compute(){
112
112
  if( !d_is_computed ){
113
112
    d_is_computed = true;
114
1155
    for( std::map< Node, std::map< int, RDomain * > >::iterator it = d_rel_doms.begin(); it != d_rel_doms.end(); ++it ){
115
10037
      for( std::map< int, RDomain * >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2 ){
116
8994
        it2->second->reset();
117
      }
118
    }
119
112
    FirstOrderModel* fm = d_treg.getModel();
120
1186
    for( unsigned i=0; i<fm->getNumAssertedQuantifiers(); i++ ){
121
2148
      Node q = fm->getAssertedQuantifier( i );
122
2148
      Node icf = d_qreg.getInstConstantBody(q);
123
1074
      Trace("rel-dom-debug") << "compute relevant domain for " << icf << std::endl;
124
1074
      computeRelevantDomain(q);
125
    }
126
127
112
    Trace("rel-dom-debug") << "account for ground terms" << std::endl;
128
112
    TermDb* db = d_treg.getTermDatabase();
129
759
    for (unsigned k = 0; k < db->getNumOperators(); k++)
130
    {
131
1294
      Node op = db->getOperator(k);
132
647
      unsigned sz = db->getNumGroundTerms( op );
133
3293
      for( unsigned i=0; i<sz; i++ ){
134
5292
        Node n = db->getGroundTerm(op, i);
135
        //if it is a non-redundant term
136
2646
        if( db->isTermActive( n ) ){
137
13127
          for( unsigned j=0; j<n.getNumChildren(); j++ ){
138
11140
            RDomain * rf = getRDomain( op, j );
139
11140
            rf->addTerm( n[j] );
140
11140
            Trace("rel-dom-debug") << "...add ground term " << n[j] << " to rel dom " << op << "[" << j << "]" << std::endl;
141
          }
142
        }
143
      }
144
    }
145
    //print debug
146
1868
    for( std::map< Node, std::map< int, RDomain * > >::iterator it = d_rel_doms.begin(); it != d_rel_doms.end(); ++it ){
147
1756
      Trace("rel-dom") << "Relevant domain for " << it->first << " : " << std::endl;
148
15866
      for( std::map< int, RDomain * >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2 ){
149
14110
        Trace("rel-dom") << "   " << it2->first << " : ";
150
14110
        RDomain * r = it2->second;
151
14110
        RDomain * rp = r->getParent();
152
14110
        if( r==rp ){
153
772
          r->removeRedundantTerms(d_qs);
154
2046
          for( unsigned i=0; i<r->d_terms.size(); i++ ){
155
1274
            Trace("rel-dom") << r->d_terms[i] << " ";
156
          }
157
        }else{
158
13338
          Trace("rel-dom") << "Dom( " << d_rn_map[rp] << ", " << d_ri_map[rp] << " ) ";
159
        }
160
14110
        Trace("rel-dom") << std::endl;
161
      }
162
    }
163
  }
164
112
}
165
166
1074
void RelevantDomain::computeRelevantDomain(Node q)
167
{
168
1074
  Assert(q.getKind() == FORALL);
169
2148
  Node n = d_qreg.getInstConstantBody(q);
170
  // we care about polarity in the traversal, so we use a polarity term context
171
2148
  PolarityTermContext tc;
172
2148
  TCtxStack ctx(&tc);
173
1074
  ctx.pushInitial(n);
174
  std::unordered_set<std::pair<Node, uint32_t>,
175
                     PairHashFunction<Node, uint32_t, std::hash<Node> > >
176
2148
      visited;
177
2148
  std::pair<Node, uint32_t> curr;
178
2148
  Node node;
179
  uint32_t nodeVal;
180
  std::unordered_set<
181
      std::pair<Node, uint32_t>,
182
1074
      PairHashFunction<Node, uint32_t, std::hash<Node> > >::const_iterator itc;
183
  bool hasPol, pol;
184
202212
  while (!ctx.empty())
185
  {
186
100569
    curr = ctx.getCurrent();
187
100569
    itc = visited.find(curr);
188
100569
    ctx.pop();
189
100569
    if (itc == visited.end())
190
    {
191
31239
      visited.insert(curr);
192
31239
      node = curr.first;
193
      // if not a quantified formula
194
31239
      if (!node.isClosure())
195
      {
196
31139
        nodeVal = curr.second;
197
        // get the polarity of the current term and process it
198
31139
        PolarityTermContext::getFlags(nodeVal, hasPol, pol);
199
31139
        computeRelevantDomainNode(q, node, hasPol, pol);
200
        // traverse the children
201
31139
        ctx.pushChildren(node, nodeVal);
202
      }
203
    }
204
  }
205
1074
}
206
207
31139
void RelevantDomain::computeRelevantDomainNode(Node q,
208
                                               Node n,
209
                                               bool hasPol,
210
                                               bool pol)
211
{
212
31139
  Trace("rel-dom-debug") << "Compute relevant domain " << n << "..." << std::endl;
213
62278
  Node op = d_treg.getTermDatabase()->getMatchOperator(n);
214
31139
  if (!op.isNull())
215
  {
216
89036
    for (size_t i = 0, nchild = n.getNumChildren(); i < nchild; i++)
217
    {
218
78659
      RDomain * rf = getRDomain( op, i );
219
78659
      if( n[i].getKind()==ITE ){
220
        for( unsigned j=1; j<=2; j++ ){
221
          computeRelevantDomainOpCh( rf, n[i][j] );
222
        }
223
      }else{
224
78659
        computeRelevantDomainOpCh( rf, n[i] );
225
      }
226
    }
227
  }
228
229
31139
  if( ( ( n.getKind()==EQUAL && !n[0].getType().isBoolean() ) || n.getKind()==GEQ ) && TermUtil::hasInstConstAttr( n ) ){
230
    //compute the information for what this literal does
231
672
    computeRelevantDomainLit( q, hasPol, pol, n );
232
672
    if( d_rel_dom_lit[hasPol][pol][n].d_merge ){
233
112
      Assert(d_rel_dom_lit[hasPol][pol][n].d_rd[0] != NULL
234
             && d_rel_dom_lit[hasPol][pol][n].d_rd[1] != NULL);
235
112
      RDomain * rd1 = d_rel_dom_lit[hasPol][pol][n].d_rd[0]->getParent();
236
112
      RDomain * rd2 = d_rel_dom_lit[hasPol][pol][n].d_rd[1]->getParent();
237
112
      if( rd1!=rd2 ){
238
73
        rd1->merge( rd2 );
239
      }
240
    }else{
241
560
      if( d_rel_dom_lit[hasPol][pol][n].d_rd[0]!=NULL ){
242
232
        RDomain * rd = d_rel_dom_lit[hasPol][pol][n].d_rd[0]->getParent();
243
604
        for( unsigned i=0; i<d_rel_dom_lit[hasPol][pol][n].d_val.size(); i++ ){
244
372
          rd->addTerm( d_rel_dom_lit[hasPol][pol][n].d_val[i] );
245
        }
246
      }
247
    }
248
  }
249
31139
  Trace("rel-dom-debug") << "...finished Compute relevant domain " << n << std::endl;
250
31139
}
251
252
78659
void RelevantDomain::computeRelevantDomainOpCh( RDomain * rf, Node n ) {
253
78659
  if( n.getKind()==INST_CONSTANT ){
254
155566
    Node q = TermUtil::getInstConstAttr(n);
255
    //merge the RDomains
256
77783
    unsigned id = n.getAttribute(InstVarNumAttribute());
257
77783
    Assert(q[0][id].getType() == n.getType());
258
77783
    Trace("rel-dom-debug") << n << " is variable # " << id << " for " << q;
259
155566
    Trace("rel-dom-debug") << " with body : " << d_qreg.getInstConstantBody(q)
260
77783
                           << std::endl;
261
77783
    RDomain * rq = getRDomain( q, id );
262
77783
    if( rf!=rq ){
263
13265
      rq->merge( rf );
264
    }
265
876
  }else if( !TermUtil::hasInstConstAttr( n ) ){
266
537
    Trace("rel-dom-debug") << "...add ground term to rel dom " << n << std::endl;
267
    //term to add
268
537
    rf->addTerm( n );
269
  }
270
78659
}
271
272
672
void RelevantDomain::computeRelevantDomainLit( Node q, bool hasPol, bool pol, Node n ) {
273
672
  if( d_rel_dom_lit[hasPol][pol].find( n )==d_rel_dom_lit[hasPol][pol].end() ){
274
355
    d_rel_dom_lit[hasPol][pol][n].d_merge = false;
275
355
    int varCount = 0;
276
355
    int varCh = -1;
277
1065
    for( unsigned i=0; i<n.getNumChildren(); i++ ){
278
710
      if( n[i].getKind()==INST_CONSTANT ){
279
        // must get the quantified formula this belongs to, which may be
280
        // different from q
281
416
        Node qi = TermUtil::getInstConstAttr(n[i]);
282
208
        unsigned id = n[i].getAttribute(InstVarNumAttribute());
283
208
        d_rel_dom_lit[hasPol][pol][n].d_rd[i] = getRDomain(qi, id, false);
284
208
        varCount++;
285
208
        varCh = i;
286
      }else{
287
502
        d_rel_dom_lit[hasPol][pol][n].d_rd[i] = NULL;
288
      }
289
    }
290
291
710
    Node r_add;
292
355
    bool varLhs = true;
293
355
    if( varCount==2 ){
294
15
      d_rel_dom_lit[hasPol][pol][n].d_merge = true;
295
    }else{
296
340
      if( varCount==1 ){
297
178
        r_add = n[1-varCh];
298
178
        varLhs = (varCh==0);
299
178
        d_rel_dom_lit[hasPol][pol][n].d_rd[0] = d_rel_dom_lit[hasPol][pol][n].d_rd[varCh];
300
178
        d_rel_dom_lit[hasPol][pol][n].d_rd[1] = NULL;
301
      }else{
302
        //solve the inequality for one/two variables, if possible
303
162
        if( n[0].getType().isReal() ){
304
262
          std::map< Node, Node > msum;
305
131
          if (ArithMSum::getMonomialSumLit(n, msum))
306
          {
307
262
            Node var;
308
262
            Node var2;
309
131
            bool hasNonVar = false;
310
392
            for( std::map< Node, Node >::iterator it = msum.begin(); it != msum.end(); ++it ){
311
762
              if (!it->first.isNull() && it->first.getKind() == INST_CONSTANT
312
577
                  && TermUtil::getInstConstAttr(it->first) == q)
313
              {
314
55
                if( var.isNull() ){
315
33
                  var = it->first;
316
22
                }else if( var2.isNull() ){
317
22
                  var2 = it->first;
318
                }else{
319
                  hasNonVar = true;
320
                }
321
              }else{
322
206
                hasNonVar = true;
323
              }
324
            }
325
131
            if( !var.isNull() ){
326
33
              if( var2.isNull() ){
327
                //single variable solve
328
22
                Node veq_c;
329
22
                Node val;
330
                int ires =
331
11
                    ArithMSum::isolate(var, msum, veq_c, val, n.getKind());
332
11
                if( ires!=0 ){
333
11
                  if( veq_c.isNull() ){
334
3
                    r_add = val;
335
3
                    varLhs = (ires==1);
336
3
                    d_rel_dom_lit[hasPol][pol][n].d_rd[0] = getRDomain( q, var.getAttribute(InstVarNumAttribute()), false );
337
3
                    d_rel_dom_lit[hasPol][pol][n].d_rd[1] = NULL;
338
                  }
339
                }
340
22
              }else if( !hasNonVar ){
341
                //merge the domains
342
9
                d_rel_dom_lit[hasPol][pol][n].d_rd[0] = getRDomain( q, var.getAttribute(InstVarNumAttribute()), false );
343
9
                d_rel_dom_lit[hasPol][pol][n].d_rd[1] = getRDomain( q, var2.getAttribute(InstVarNumAttribute()), false );
344
9
                d_rel_dom_lit[hasPol][pol][n].d_merge = true;
345
              }
346
            }
347
          }
348
        }
349
      }
350
    }
351
355
    if( d_rel_dom_lit[hasPol][pol][n].d_merge ){
352
      //do not merge if constant negative polarity
353
24
      if( hasPol && !pol ){
354
        d_rel_dom_lit[hasPol][pol][n].d_merge = false;
355
      }
356
331
    }else if( !r_add.isNull() && !TermUtil::hasInstConstAttr( r_add ) ){
357
162
      Trace("rel-dom-debug") << "...add term " << r_add << ", pol = " << pol << ", kind = " << n.getKind() << std::endl;
358
      //the negative occurrence adds the term to the domain
359
162
      if( !hasPol || !pol ){
360
162
        d_rel_dom_lit[hasPol][pol][n].d_val.push_back( r_add );
361
      }
362
      //the positive occurence adds other terms
363
162
      if( ( !hasPol || pol ) && n[0].getType().isInteger() ){
364
136
        if( n.getKind()==EQUAL ){
365
          for( unsigned i=0; i<2; i++ ){
366
            d_rel_dom_lit[hasPol][pol][n].d_val.push_back(
367
                ArithMSum::offset(r_add, i == 0 ? 1 : -1));
368
          }
369
136
        }else if( n.getKind()==GEQ ){
370
272
          d_rel_dom_lit[hasPol][pol][n].d_val.push_back(
371
272
              ArithMSum::offset(r_add, varLhs ? 1 : -1));
372
        }
373
      }
374
    }else{
375
169
      d_rel_dom_lit[hasPol][pol][n].d_rd[0] = NULL;
376
169
      d_rel_dom_lit[hasPol][pol][n].d_rd[1] = NULL;
377
    }
378
  }
379
672
}
380
381
}  // namespace quantifiers
382
}  // namespace theory
383
29280
}  // namespace cvc5