GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/quantifiers_rewriter.cpp Lines: 873 1084 80.5 %
Date: 2021-11-05 Branches: 1889 4417 42.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Morgan Deters, Haniel Barbosa
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 QuantifiersRewriter class.
14
 */
15
16
#include "theory/quantifiers/quantifiers_rewriter.h"
17
18
#include "expr/ascription_type.h"
19
#include "expr/bound_var_manager.h"
20
#include "expr/dtype.h"
21
#include "expr/dtype_cons.h"
22
#include "expr/node_algorithm.h"
23
#include "expr/skolem_manager.h"
24
#include "options/quantifiers_options.h"
25
#include "theory/arith/arith_msum.h"
26
#include "theory/datatypes/theory_datatypes_utils.h"
27
#include "theory/quantifiers/bv_inverter.h"
28
#include "theory/quantifiers/ematching/trigger.h"
29
#include "theory/quantifiers/extended_rewrite.h"
30
#include "theory/quantifiers/quantifiers_attributes.h"
31
#include "theory/quantifiers/skolemize.h"
32
#include "theory/quantifiers/term_database.h"
33
#include "theory/quantifiers/term_util.h"
34
#include "theory/rewriter.h"
35
#include "theory/strings/theory_strings_utils.h"
36
#include "util/rational.h"
37
38
using namespace std;
39
using namespace cvc5::kind;
40
using namespace cvc5::context;
41
42
namespace cvc5 {
43
namespace theory {
44
namespace quantifiers {
45
46
/**
47
 * Attributes used for constructing bound variables in a canonical way. These
48
 * are attributes that map to bound variable, introduced for the following
49
 * purposes:
50
 * - QRewPrenexAttribute: cached on (v, body) where we are prenexing bound
51
 * variable v in a nested quantified formula within the given body.
52
 * - QRewMiniscopeAttribute: cached on (v, q, i) where q is being miniscoped
53
 * for F_i in its body (and F_1 ... F_n), and v is one of the bound variables
54
 * that q binds.
55
 * - QRewDtExpandAttribute: cached on (F, lit, a) where lit is the tested
56
 * literal used for expanding a quantified datatype variable in quantified
57
 * formula with body F, and a is the rational corresponding to the argument
58
 * position of the variable, e.g. lit is ((_ is C) x) and x is
59
 * replaced by (C y1 ... yn), where the argument position of yi is i.
60
 */
61
struct QRewPrenexAttributeId
62
{
63
};
64
typedef expr::Attribute<QRewPrenexAttributeId, Node> QRewPrenexAttribute;
65
struct QRewMiniscopeAttributeId
66
{
67
};
68
typedef expr::Attribute<QRewMiniscopeAttributeId, Node> QRewMiniscopeAttribute;
69
struct QRewDtExpandAttributeId
70
{
71
};
72
typedef expr::Attribute<QRewDtExpandAttributeId, Node> QRewDtExpandAttribute;
73
74
std::ostream& operator<<(std::ostream& out, RewriteStep s)
75
{
76
  switch (s)
77
  {
78
    case COMPUTE_ELIM_SYMBOLS: out << "COMPUTE_ELIM_SYMBOLS"; break;
79
    case COMPUTE_MINISCOPING: out << "COMPUTE_MINISCOPING"; break;
80
    case COMPUTE_AGGRESSIVE_MINISCOPING:
81
      out << "COMPUTE_AGGRESSIVE_MINISCOPING";
82
      break;
83
    case COMPUTE_EXT_REWRITE: out << "COMPUTE_EXT_REWRITE"; break;
84
    case COMPUTE_PROCESS_TERMS: out << "COMPUTE_PROCESS_TERMS"; break;
85
    case COMPUTE_PRENEX: out << "COMPUTE_PRENEX"; break;
86
    case COMPUTE_VAR_ELIMINATION: out << "COMPUTE_VAR_ELIMINATION"; break;
87
    case COMPUTE_COND_SPLIT: out << "COMPUTE_COND_SPLIT"; break;
88
    default: out << "UnknownRewriteStep"; break;
89
  }
90
  return out;
91
}
92
93
15432
QuantifiersRewriter::QuantifiersRewriter(const Options& opts) : d_opts(opts) {}
94
95
15402
bool QuantifiersRewriter::isLiteral( Node n ){
96
15402
  switch( n.getKind() ){
97
  case NOT:
98
    return n[0].getKind()!=NOT && isLiteral( n[0] );
99
    break;
100
  case OR:
101
  case AND:
102
  case IMPLIES:
103
  case XOR:
104
  case ITE:
105
    return false;
106
    break;
107
9077
  case EQUAL:
108
    //for boolean terms
109
9077
    return !n[0].getType().isBoolean();
110
    break;
111
6325
  default:
112
6325
    break;
113
  }
114
6325
  return true;
115
}
116
117
5186838
void QuantifiersRewriter::computeArgs(const std::vector<Node>& args,
118
                                      std::map<Node, bool>& activeMap,
119
                                      Node n,
120
                                      std::map<Node, bool>& visited)
121
{
122
5186838
  if( visited.find( n )==visited.end() ){
123
3678130
    visited[n] = true;
124
3678130
    if( n.getKind()==BOUND_VARIABLE ){
125
611496
      if( std::find( args.begin(), args.end(), n )!=args.end() ){
126
502229
        activeMap[ n ] = true;
127
      }
128
    }else{
129
3066634
      if (n.hasOperator())
130
      {
131
1622910
        computeArgs(args, activeMap, n.getOperator(), visited);
132
      }
133
6329886
      for( int i=0; i<(int)n.getNumChildren(); i++ ){
134
3263252
        computeArgs( args, activeMap, n[i], visited );
135
      }
136
    }
137
  }
138
5186838
}
139
140
154896
void QuantifiersRewriter::computeArgVec(const std::vector<Node>& args,
141
                                        std::vector<Node>& activeArgs,
142
                                        Node n)
143
{
144
154896
  Assert(activeArgs.empty());
145
309792
  std::map< Node, bool > activeMap;
146
309792
  std::map< Node, bool > visited;
147
154896
  computeArgs( args, activeMap, n, visited );
148
154896
  if( !activeMap.empty() ){
149
2439881
    for( unsigned i=0; i<args.size(); i++ ){
150
2286645
      if( activeMap.find( args[i] )!=activeMap.end() ){
151
352369
        activeArgs.push_back( args[i] );
152
      }
153
    }
154
  }
155
154896
}
156
157
73015
void QuantifiersRewriter::computeArgVec2(const std::vector<Node>& args,
158
                                         std::vector<Node>& activeArgs,
159
                                         Node n,
160
                                         Node ipl)
161
{
162
73015
  Assert(activeArgs.empty());
163
146030
  std::map< Node, bool > activeMap;
164
146030
  std::map< Node, bool > visited;
165
73015
  computeArgs( args, activeMap, n, visited );
166
73015
  if( !activeMap.empty() ){
167
    //collect variables in inst pattern list only if we cannot eliminate quantifier
168
72765
    computeArgs( args, activeMap, ipl, visited );
169
223715
    for( unsigned i=0; i<args.size(); i++ ){
170
150950
      if( activeMap.find( args[i] )!=activeMap.end() ){
171
149860
        activeArgs.push_back( args[i] );
172
      }
173
    }
174
  }
175
73015
}
176
177
143119
RewriteResponse QuantifiersRewriter::preRewrite(TNode in) {
178
143119
  if( in.getKind()==kind::EXISTS || in.getKind()==kind::FORALL ){
179
84036
    Trace("quantifiers-rewrite-debug") << "pre-rewriting " << in << std::endl;
180
166283
    std::vector< Node > args;
181
166283
    Node body = in;
182
84036
    bool doRewrite = false;
183
98358
    while( body.getNumChildren()==2 && body.getKind()==body[1].getKind() ){
184
14381
      for( unsigned i=0; i<body[0].getNumChildren(); i++ ){
185
7220
        args.push_back( body[0][i] );
186
      }
187
7161
      body = body[1];
188
7161
      doRewrite = true;
189
    }
190
84036
    if( doRewrite ){
191
3578
      std::vector< Node > children;
192
3618
      for( unsigned i=0; i<body[0].getNumChildren(); i++ ){
193
1829
        args.push_back( body[0][i] );
194
      }
195
1789
      children.push_back( NodeManager::currentNM()->mkNode(kind::BOUND_VAR_LIST,args) );
196
1789
      children.push_back( body[1] );
197
1789
      if( body.getNumChildren()==3 ){
198
48
        children.push_back( body[2] );
199
      }
200
3578
      Node n = NodeManager::currentNM()->mkNode( in.getKind(), children );
201
1789
      if( in!=n ){
202
1789
        Trace("quantifiers-pre-rewrite") << "*** pre-rewrite " << in << std::endl;
203
1789
        Trace("quantifiers-pre-rewrite") << " to " << std::endl;
204
1789
        Trace("quantifiers-pre-rewrite") << n << std::endl;
205
      }
206
1789
      return RewriteResponse(REWRITE_DONE, n);
207
    }
208
  }
209
141330
  return RewriteResponse(REWRITE_DONE, in);
210
}
211
212
261097
RewriteResponse QuantifiersRewriter::postRewrite(TNode in) {
213
261097
  Trace("quantifiers-rewrite-debug") << "post-rewriting " << in << std::endl;
214
261097
  RewriteStatus status = REWRITE_DONE;
215
522194
  Node ret = in;
216
261097
  RewriteStep rew_op = COMPUTE_LAST;
217
  //get the body
218
261097
  if( in.getKind()==EXISTS ){
219
6458
    std::vector< Node > children;
220
3229
    children.push_back( in[0] );
221
3229
    children.push_back( in[1].negate() );
222
3229
    if( in.getNumChildren()==3 ){
223
45
      children.push_back( in[2] );
224
    }
225
3229
    ret = NodeManager::currentNM()->mkNode( FORALL, children );
226
3229
    ret = ret.negate();
227
3229
    status = REWRITE_AGAIN_FULL;
228
257868
  }else if( in.getKind()==FORALL ){
229
140082
    if( in[1].isConst() && in.getNumChildren()==2 ){
230
951
      return RewriteResponse( status, in[1] );
231
    }else{
232
      //compute attributes
233
278262
      QAttributes qa;
234
139131
      QuantAttributes::computeQuantAttributes( in, qa );
235
1118932
      for (unsigned i = 0; i < COMPUTE_LAST; ++i)
236
      {
237
999448
        RewriteStep op = static_cast<RewriteStep>(i);
238
999448
        if( doOperation( in, op, qa ) ){
239
735908
          ret = computeOperation( in, op, qa );
240
735908
          if( ret!=in ){
241
19647
            rew_op = op;
242
19647
            status = REWRITE_AGAIN_FULL;
243
19647
            break;
244
          }
245
        }
246
      }
247
    }
248
  }
249
  //print if changed
250
260146
  if( in!=ret ){
251
22876
    Trace("quantifiers-rewrite") << "*** rewrite (op=" << rew_op << ") " << in << std::endl;
252
22876
    Trace("quantifiers-rewrite") << " to " << std::endl;
253
22876
    Trace("quantifiers-rewrite") << ret << std::endl;
254
  }
255
260146
  return RewriteResponse( status, ret );
256
}
257
258
840471
bool QuantifiersRewriter::addCheckElimChild(std::vector<Node>& children,
259
                                            Node c,
260
                                            Kind k,
261
                                            std::map<Node, bool>& lit_pol,
262
                                            bool& childrenChanged) const
263
{
264
840471
  if ((k == OR || k == AND) && d_opts.quantifiers.elimTautQuant)
265
  {
266
1353627
    Node lit = c.getKind()==NOT ? c[0] : c;
267
676939
    bool pol = c.getKind()!=NOT;
268
676939
    std::map< Node, bool >::iterator it = lit_pol.find( lit );
269
676939
    if( it==lit_pol.end() ){
270
675975
      lit_pol[lit] = pol;
271
675975
      children.push_back( c );
272
    }else{
273
964
      childrenChanged = true;
274
964
      if( it->second!=pol ){
275
251
        return false;
276
      }
277
676688
    }
278
  }
279
  else
280
  {
281
163532
    children.push_back( c );
282
  }
283
840220
  return true;
284
}
285
286
964833
Node QuantifiersRewriter::computeElimSymbols(Node body) const
287
{
288
964833
  Kind ok = body.getKind();
289
964833
  Kind k = ok;
290
964833
  bool negAllCh = false;
291
964833
  bool negCh1 = false;
292
964833
  if( ok==IMPLIES ){
293
12029
    k = OR;
294
12029
    negCh1 = true;
295
952804
  }else if( ok==XOR ){
296
771
    k = EQUAL;
297
771
    negCh1 = true;
298
952033
  }else if( ok==NOT ){
299
388614
    if( body[0].getKind()==NOT ){
300
      return computeElimSymbols( body[0][0] );
301
388614
    }else if( body[0].getKind()==OR || body[0].getKind()==IMPLIES ){
302
1751
      k = AND;
303
1751
      negAllCh = true;
304
1751
      negCh1 = body[0].getKind()==IMPLIES;
305
1751
      body = body[0];
306
386863
    }else if( body[0].getKind()==AND ){
307
6143
      k = OR;
308
6143
      negAllCh = true;
309
6143
      body = body[0];
310
380720
    }else if( body[0].getKind()==XOR || ( body[0].getKind()==EQUAL && body[0][0].getType().isBoolean() ) ){
311
2834
      k = EQUAL;
312
2834
      negCh1 = ( body[0].getKind()==EQUAL );
313
2834
      body = body[0];
314
377886
    }else if( body[0].getKind()==ITE ){
315
95
      k = body[0].getKind();
316
95
      negAllCh = true;
317
95
      negCh1 = true;
318
95
      body = body[0];
319
    }else{
320
377791
      return body;
321
    }
322
563419
  }else if( ( ok!=EQUAL || !body[0].getType().isBoolean() ) && ok!=ITE && ok!=AND && ok!=OR ){
323
    //a literal
324
336565
    return body;
325
  }
326
250477
  bool childrenChanged = false;
327
500954
  std::vector< Node > children;
328
500954
  std::map< Node, bool > lit_pol;
329
1075928
  for( unsigned i=0; i<body.getNumChildren(); i++ ){
330
1651153
    Node c = computeElimSymbols( ( i==0 && negCh1 )!=negAllCh ? body[i].negate() : body[i] );
331
825702
    bool success = true;
332
825702
    if( c.getKind()==k && ( k==OR || k==AND ) ){
333
      //flatten
334
5734
      childrenChanged = true;
335
26236
      for( unsigned j=0; j<c.getNumChildren(); j++ ){
336
20503
        if( !addCheckElimChild( children, c[j], k, lit_pol, childrenChanged ) ){
337
1
          success = false;
338
1
          break;
339
        }
340
      }
341
    }else{
342
819968
      success = addCheckElimChild( children, c, k, lit_pol, childrenChanged );
343
    }
344
825702
    if( !success ){
345
      // tautology
346
251
      Assert(k == OR || k == AND);
347
251
      return NodeManager::currentNM()->mkConst( k==OR );
348
    }
349
825451
    childrenChanged = childrenChanged || c!=body[i];
350
  }
351
250226
  if( childrenChanged || k!=ok ){
352
27462
    return ( children.size()==1 && k!=NOT ) ? children[0] : NodeManager::currentNM()->mkNode( k, children );
353
  }else{
354
222764
    return body;
355
  }
356
}
357
358
void QuantifiersRewriter::computeDtTesterIteSplit( Node n, std::map< Node, Node >& pcons, std::map< Node, std::map< int, Node > >& ncons,
359
                                                   std::vector< Node >& conj ){
360
  if( n.getKind()==ITE && n[0].getKind()==APPLY_TESTER && n[1].getType().isBoolean() ){
361
    Trace("quantifiers-rewrite-ite-debug") << "Split tester condition : " << n << std::endl;
362
    Node x = n[0][0];
363
    std::map< Node, Node >::iterator itp = pcons.find( x );
364
    if( itp!=pcons.end() ){
365
      Trace("quantifiers-rewrite-ite-debug") << "...condition already set " << itp->second << std::endl;
366
      computeDtTesterIteSplit( n[ itp->second==n[0] ? 1 : 2 ], pcons, ncons, conj );
367
    }else{
368
      Node tester = n[0].getOperator();
369
      int index = datatypes::utils::indexOf(tester);
370
      std::map< int, Node >::iterator itn = ncons[x].find( index );
371
      if( itn!=ncons[x].end() ){
372
        Trace("quantifiers-rewrite-ite-debug") << "...condition negated " << itn->second << std::endl;
373
        computeDtTesterIteSplit( n[ 2 ], pcons, ncons, conj );
374
      }else{
375
        for( unsigned i=0; i<2; i++ ){
376
          if( i==0 ){
377
            pcons[x] = n[0];
378
          }else{
379
            pcons.erase( x );
380
            ncons[x][index] = n[0].negate();
381
          }
382
          computeDtTesterIteSplit( n[i+1], pcons, ncons, conj );
383
        }
384
        ncons[x].erase( index );
385
      }
386
    }
387
  }else{
388
    NodeManager* nm = NodeManager::currentNM();
389
    Trace("quantifiers-rewrite-ite-debug") << "Return value : " << n << std::endl;
390
    std::vector< Node > children;
391
    children.push_back( n );
392
    std::vector< Node > vars;
393
    //add all positive testers
394
    for( std::map< Node, Node >::iterator it = pcons.begin(); it != pcons.end(); ++it ){
395
      children.push_back( it->second.negate() );
396
      vars.push_back( it->first );
397
    }
398
    //add all negative testers
399
    for( std::map< Node, std::map< int, Node > >::iterator it = ncons.begin(); it != ncons.end(); ++it ){
400
      Node x = it->first;
401
      //only if we haven't settled on a positive tester
402
      if( std::find( vars.begin(), vars.end(), x )==vars.end() ){
403
        //check if we have exhausted all options but one
404
        const DType& dt = x.getType().getDType();
405
        std::vector< Node > nchildren;
406
        int pos_cons = -1;
407
        for( int i=0; i<(int)dt.getNumConstructors(); i++ ){
408
          std::map< int, Node >::iterator itt = it->second.find( i );
409
          if( itt==it->second.end() ){
410
            pos_cons = pos_cons==-1 ? i : -2;
411
          }else{
412
            nchildren.push_back( itt->second.negate() );
413
          }
414
        }
415
        if( pos_cons>=0 ){
416
          Node tester = dt[pos_cons].getTester();
417
          children.push_back(nm->mkNode(APPLY_TESTER, tester, x).negate());
418
        }else{
419
          children.insert( children.end(), nchildren.begin(), nchildren.end() );
420
        }
421
      }
422
    }
423
    //make condition/output pair
424
    Node c = children.size()==1 ? children[0] : NodeManager::currentNM()->mkNode( OR, children );
425
    conj.push_back( c );
426
  }
427
}
428
429
119426
Node QuantifiersRewriter::computeProcessTerms(Node body,
430
                                              std::vector<Node>& new_vars,
431
                                              std::vector<Node>& new_conds,
432
                                              Node q,
433
                                              QAttributes& qa) const
434
{
435
238852
  std::map< Node, Node > cache;
436
119426
  if( qa.isFunDef() ){
437
    Node h = QuantAttributes::getFunDefHead( q );
438
    Assert(!h.isNull());
439
    // if it is a function definition, rewrite the body independently
440
    Node fbody = QuantAttributes::getFunDefBody( q );
441
    Trace("quantifiers-rewrite-debug") << "Decompose " << h << " / " << fbody << " as function definition for " << q << "." << std::endl;
442
    if (!fbody.isNull())
443
    {
444
      Node r = computeProcessTerms2(fbody, cache, new_vars, new_conds);
445
      Assert(new_vars.size() == h.getNumChildren());
446
      return Rewriter::rewrite(NodeManager::currentNM()->mkNode(EQUAL, h, r));
447
    }
448
    // It can happen that we can't infer the shape of the function definition,
449
    // for example: forall xy. f( x, y ) = 1 + f( x, y ), this is rewritten to
450
    // forall xy. false.
451
  }
452
119426
  return computeProcessTerms2(body, cache, new_vars, new_conds);
453
}
454
455
3110193
Node QuantifiersRewriter::computeProcessTerms2(
456
    Node body,
457
    std::map<Node, Node>& cache,
458
    std::vector<Node>& new_vars,
459
    std::vector<Node>& new_conds) const
460
{
461
3110193
  NodeManager* nm = NodeManager::currentNM();
462
6220386
  Trace("quantifiers-rewrite-term-debug2")
463
3110193
      << "computeProcessTerms " << body << std::endl;
464
3110193
  std::map< Node, Node >::iterator iti = cache.find( body );
465
3110193
  if( iti!=cache.end() ){
466
1023312
    return iti->second;
467
  }
468
2086881
  bool changed = false;
469
4173762
  std::vector<Node> children;
470
5077648
  for (size_t i = 0; i < body.getNumChildren(); i++)
471
  {
472
    // do the recursive call on children
473
5981534
    Node nn = computeProcessTerms2(body[i], cache, new_vars, new_conds);
474
2990767
    children.push_back(nn);
475
2990767
    changed = changed || nn != body[i];
476
  }
477
478
  // make return value
479
4173762
  Node ret;
480
2086881
  if (changed)
481
  {
482
5784
    if (body.getMetaKind() == kind::metakind::PARAMETERIZED)
483
    {
484
197
      children.insert(children.begin(), body.getOperator());
485
    }
486
5784
    ret = nm->mkNode(body.getKind(), children);
487
  }
488
  else
489
  {
490
2081097
    ret = body;
491
  }
492
493
4173762
  Trace("quantifiers-rewrite-term-debug2")
494
2086881
      << "Returning " << ret << " for " << body << std::endl;
495
  // do context-independent rewriting
496
4173762
  if (ret.getKind() == EQUAL
497
2086881
      && d_opts.quantifiers.iteLiftQuant != options::IteLiftQuantMode::NONE)
498
  {
499
726549
    for (size_t i = 0; i < 2; i++)
500
    {
501
484785
      if (ret[i].getKind() == ITE)
502
      {
503
23777
        Node no = i == 0 ? ret[1] : ret[0];
504
12356
        if (no.getKind() != ITE)
505
        {
506
11020
          bool doRewrite =
507
11020
              d_opts.quantifiers.iteLiftQuant == options::IteLiftQuantMode::ALL;
508
21105
          std::vector<Node> childrenIte;
509
11020
          childrenIte.push_back(ret[i][0]);
510
33060
          for (size_t j = 1; j <= 2; j++)
511
          {
512
            // check if it rewrites to a constant
513
44080
            Node nn = nm->mkNode(EQUAL, no, ret[i][j]);
514
22040
            nn = Rewriter::rewrite(nn);
515
22040
            childrenIte.push_back(nn);
516
22040
            if (nn.isConst())
517
            {
518
1306
              doRewrite = true;
519
            }
520
          }
521
11020
          if (doRewrite)
522
          {
523
935
            ret = nm->mkNode(ITE, childrenIte);
524
935
            break;
525
          }
526
        }
527
      }
528
    }
529
  }
530
1844182
  else if (ret.getKind() == SELECT && ret[0].getKind() == STORE)
531
  {
532
192
    Node st = ret[0];
533
192
    Node index = ret[1];
534
192
    std::vector<Node> iconds;
535
192
    std::vector<Node> elements;
536
602
    while (st.getKind() == STORE)
537
    {
538
253
      iconds.push_back(index.eqNode(st[1]));
539
253
      elements.push_back(st[2]);
540
253
      st = st[0];
541
    }
542
96
    ret = nm->mkNode(SELECT, st, index);
543
    // conditions
544
349
    for (int i = (iconds.size() - 1); i >= 0; i--)
545
    {
546
253
      ret = nm->mkNode(ITE, iconds[i], elements[i], ret);
547
    }
548
  }
549
2086881
  cache[body] = ret;
550
2086881
  return ret;
551
}
552
553
25
Node QuantifiersRewriter::computeExtendedRewrite(Node q)
554
{
555
50
  Node body = q[1];
556
  // apply extended rewriter
557
50
  Node bodyr = Rewriter::callExtendedRewrite(body);
558
25
  if (body != bodyr)
559
  {
560
24
    std::vector<Node> children;
561
12
    children.push_back(q[0]);
562
12
    children.push_back(bodyr);
563
12
    if (q.getNumChildren() == 3)
564
    {
565
      children.push_back(q[2]);
566
    }
567
12
    return NodeManager::currentNM()->mkNode(FORALL, children);
568
  }
569
13
  return q;
570
}
571
572
119657
Node QuantifiersRewriter::computeCondSplit(Node body,
573
                                           const std::vector<Node>& args,
574
                                           QAttributes& qa) const
575
{
576
119657
  NodeManager* nm = NodeManager::currentNM();
577
119657
  Kind bk = body.getKind();
578
239674
  if (d_opts.quantifiers.iteDtTesterSplitQuant && bk == ITE
579
239322
      && body[0].getKind() == APPLY_TESTER)
580
  {
581
    Trace("quantifiers-rewrite-ite-debug") << "DTT split : " << body << std::endl;
582
    std::map< Node, Node > pcons;
583
    std::map< Node, std::map< int, Node > > ncons;
584
    std::vector< Node > conj;
585
    computeDtTesterIteSplit( body, pcons, ncons, conj );
586
    Assert(!conj.empty());
587
    if( conj.size()>1 ){
588
      Trace("quantifiers-rewrite-ite") << "*** Split ITE (datatype tester) " << body << " into : " << std::endl;
589
      for( unsigned i=0; i<conj.size(); i++ ){
590
        Trace("quantifiers-rewrite-ite") << "   " << conj[i] << std::endl;
591
      }
592
      return nm->mkNode(AND, conj);
593
    }
594
  }
595
119657
  if (!d_opts.quantifiers.condVarSplitQuant)
596
  {
597
    return body;
598
  }
599
239314
  Trace("cond-var-split-debug")
600
119657
      << "Conditional var elim split " << body << "?" << std::endl;
601
602
119657
  if (bk == ITE
603
119849
      || (bk == EQUAL && body[0].getType().isBoolean()
604
25048
          && d_opts.quantifiers.condVarSplitQuantAgg))
605
  {
606
192
    Assert(!qa.isFunDef());
607
192
    bool do_split = false;
608
192
    unsigned index_max = bk == ITE ? 0 : 1;
609
354
    std::vector<Node> tmpArgs = args;
610
354
    for (unsigned index = 0; index <= index_max; index++)
611
    {
612
576
      if (hasVarElim(body[index], true, tmpArgs)
613
576
          || hasVarElim(body[index], false, tmpArgs))
614
      {
615
30
        do_split = true;
616
30
        break;
617
      }
618
    }
619
192
    if (do_split)
620
    {
621
60
      Node pos;
622
60
      Node neg;
623
30
      if (bk == ITE)
624
      {
625
30
        pos = nm->mkNode(OR, body[0].negate(), body[1]);
626
30
        neg = nm->mkNode(OR, body[0], body[2]);
627
      }
628
      else
629
      {
630
        pos = nm->mkNode(OR, body[0].negate(), body[1]);
631
        neg = nm->mkNode(OR, body[0], body[1].negate());
632
      }
633
60
      Trace("cond-var-split-debug") << "*** Split (conditional variable eq) "
634
30
                                    << body << " into : " << std::endl;
635
30
      Trace("cond-var-split-debug") << "   " << pos << std::endl;
636
30
      Trace("cond-var-split-debug") << "   " << neg << std::endl;
637
30
      return nm->mkNode(AND, pos, neg);
638
    }
639
  }
640
641
119627
  if (bk == OR)
642
  {
643
47435
    unsigned size = body.getNumChildren();
644
47435
    bool do_split = false;
645
47435
    unsigned split_index = 0;
646
192901
    for (unsigned i = 0; i < size; i++)
647
    {
648
      // check if this child is a (conditional) variable elimination
649
291075
      Node b = body[i];
650
145609
      if (b.getKind() == AND)
651
      {
652
18050
        std::vector<Node> vars;
653
18050
        std::vector<Node> subs;
654
18050
        std::vector<Node> tmpArgs = args;
655
35674
        for (unsigned j = 0, bsize = b.getNumChildren(); j < bsize; j++)
656
        {
657
26792
          if (getVarElimLit(body, b[j], false, tmpArgs, vars, subs))
658
          {
659
5402
            Trace("cond-var-split-debug") << "Variable elimination in child #"
660
2701
                                          << j << " under " << i << std::endl;
661
            // Figure out if we should split
662
            // Currently we split if the aggressive option is set, or
663
            // if the top-level OR is binary.
664
2701
            if (d_opts.quantifiers.condVarSplitQuantAgg || size == 2)
665
            {
666
143
              do_split = true;
667
            }
668
            // other splitting criteria go here
669
670
2701
            if (do_split)
671
            {
672
143
              split_index = i;
673
143
              break;
674
            }
675
2558
            vars.clear();
676
2558
            subs.clear();
677
2558
            tmpArgs = args;
678
          }
679
        }
680
      }
681
145609
      if (do_split)
682
      {
683
143
        break;
684
      }
685
    }
686
47435
    if (do_split)
687
    {
688
286
      std::vector<Node> children;
689
429
      for (TNode bc : body)
690
      {
691
286
        children.push_back(bc);
692
      }
693
286
      std::vector<Node> split_children;
694
577
      for (TNode bci : body[split_index])
695
      {
696
434
        children[split_index] = bci;
697
434
        split_children.push_back(nm->mkNode(OR, children));
698
      }
699
      // split the AND child, for example:
700
      //  ( x!=a ^ P(x) ) V Q(x) ---> ( x!=a V Q(x) ) ^ ( P(x) V Q(x) )
701
143
      return nm->mkNode(AND, split_children);
702
    }
703
  }
704
705
119484
  return body;
706
}
707
708
7052
bool QuantifiersRewriter::isVarElim(Node v, Node s)
709
{
710
7052
  Assert(v.getKind() == BOUND_VARIABLE);
711
7052
  return !expr::hasSubterm(s, v) && s.getType().isSubtypeOf(v.getType());
712
}
713
714
38331
Node QuantifiersRewriter::getVarElimEq(Node lit,
715
                                       const std::vector<Node>& args,
716
                                       Node& var)
717
{
718
38331
  Assert(lit.getKind() == EQUAL);
719
38331
  Node slv;
720
76662
  TypeNode tt = lit[0].getType();
721
38331
  if (tt.isReal())
722
  {
723
15975
    slv = getVarElimEqReal(lit, args, var);
724
  }
725
22356
  else if (tt.isBitVector())
726
  {
727
751
    slv = getVarElimEqBv(lit, args, var);
728
  }
729
21605
  else if (tt.isStringLike())
730
  {
731
220
    slv = getVarElimEqString(lit, args, var);
732
  }
733
76662
  return slv;
734
}
735
736
15975
Node QuantifiersRewriter::getVarElimEqReal(Node lit,
737
                                           const std::vector<Node>& args,
738
                                           Node& var)
739
{
740
  // for arithmetic, solve the equality
741
31950
  std::map<Node, Node> msum;
742
15975
  if (!ArithMSum::getMonomialSumLit(lit, msum))
743
  {
744
    return Node::null();
745
  }
746
15975
  std::vector<Node>::const_iterator ita;
747
48505
  for (std::map<Node, Node>::iterator itm = msum.begin(); itm != msum.end();
748
       ++itm)
749
  {
750
32742
    if (itm->first.isNull())
751
    {
752
3335
      continue;
753
    }
754
29407
    ita = std::find(args.begin(), args.end(), itm->first);
755
29407
    if (ita != args.end())
756
    {
757
988
      Node veq_c;
758
988
      Node val;
759
600
      int ires = ArithMSum::isolate(itm->first, msum, veq_c, val, EQUAL);
760
600
      if (ires != 0 && veq_c.isNull() && isVarElim(itm->first, val))
761
      {
762
212
        var = itm->first;
763
212
        return val;
764
      }
765
    }
766
  }
767
15763
  return Node::null();
768
}
769
770
751
Node QuantifiersRewriter::getVarElimEqBv(Node lit,
771
                                         const std::vector<Node>& args,
772
                                         Node& var)
773
{
774
751
  if (Trace.isOn("quant-velim-bv"))
775
  {
776
    Trace("quant-velim-bv") << "Bv-Elim : " << lit << " varList = { ";
777
    for (const Node& v : args)
778
    {
779
      Trace("quant-velim-bv") << v << " ";
780
    }
781
    Trace("quant-velim-bv") << "} ?" << std::endl;
782
  }
783
751
  Assert(lit.getKind() == EQUAL);
784
  // TODO (#1494) : linearize the literal using utility
785
786
  // compute a subset active_args of the bound variables args that occur in lit
787
1502
  std::vector<Node> active_args;
788
751
  computeArgVec(args, active_args, lit);
789
790
1502
  BvInverter binv;
791
1594
  for (const Node& cvar : active_args)
792
  {
793
    // solve for the variable on this path using the inverter
794
1726
    std::vector<unsigned> path;
795
1726
    Node slit = binv.getPathToPv(lit, cvar, path);
796
883
    if (!slit.isNull())
797
    {
798
848
      Node slv = binv.solveBvLit(cvar, lit, path, nullptr);
799
444
      Trace("quant-velim-bv") << "...solution : " << slv << std::endl;
800
444
      if (!slv.isNull())
801
      {
802
76
        var = cvar;
803
        // if this is a proper variable elimination, that is, var = slv where
804
        // var is not in the free variables of slv, then we can return this
805
        // as the variable elimination for lit.
806
76
        if (isVarElim(var, slv))
807
        {
808
40
          return slv;
809
        }
810
      }
811
    }
812
    else
813
    {
814
439
      Trace("quant-velim-bv") << "...non-invertible path." << std::endl;
815
    }
816
  }
817
818
711
  return Node::null();
819
}
820
821
220
Node QuantifiersRewriter::getVarElimEqString(Node lit,
822
                                             const std::vector<Node>& args,
823
                                             Node& var)
824
{
825
220
  Assert(lit.getKind() == EQUAL);
826
220
  NodeManager* nm = NodeManager::currentNM();
827
656
  for (unsigned i = 0; i < 2; i++)
828
  {
829
440
    if (lit[i].getKind() == STRING_CONCAT)
830
    {
831
12
      TypeNode stype = lit[i].getType();
832
20
      for (unsigned j = 0, nchildren = lit[i].getNumChildren(); j < nchildren;
833
           j++)
834
      {
835
16
        if (std::find(args.begin(), args.end(), lit[i][j]) != args.end())
836
        {
837
12
          var = lit[i][j];
838
20
          Node slv = lit[1 - i];
839
20
          std::vector<Node> preL(lit[i].begin(), lit[i].begin() + j);
840
20
          std::vector<Node> postL(lit[i].begin() + j + 1, lit[i].end());
841
20
          Node tpre = strings::utils::mkConcat(preL, stype);
842
20
          Node tpost = strings::utils::mkConcat(postL, stype);
843
20
          Node slvL = nm->mkNode(STRING_LENGTH, slv);
844
20
          Node tpreL = nm->mkNode(STRING_LENGTH, tpre);
845
20
          Node tpostL = nm->mkNode(STRING_LENGTH, tpost);
846
12
          slv = nm->mkNode(
847
              STRING_SUBSTR,
848
              slv,
849
              tpreL,
850
24
              nm->mkNode(MINUS, slvL, nm->mkNode(PLUS, tpreL, tpostL)));
851
          // forall x. r ++ x ++ t = s => P( x )
852
          //   is equivalent to
853
          // r ++ s' ++ t = s => P( s' ) where
854
          // s' = substr( s, |r|, |s|-(|t|+|r|) ).
855
          // We apply this only if r,t,s do not contain free variables.
856
12
          if (!expr::hasFreeVar(slv))
857
          {
858
4
            return slv;
859
          }
860
        }
861
      }
862
    }
863
  }
864
865
216
  return Node::null();
866
}
867
868
252338
bool QuantifiersRewriter::getVarElimLit(Node body,
869
                                        Node lit,
870
                                        bool pol,
871
                                        std::vector<Node>& args,
872
                                        std::vector<Node>& vars,
873
                                        std::vector<Node>& subs) const
874
{
875
252338
  if (lit.getKind() == NOT)
876
  {
877
7141
    lit = lit[0];
878
7141
    pol = !pol;
879
7141
    Assert(lit.getKind() != NOT);
880
  }
881
252338
  NodeManager* nm = NodeManager::currentNM();
882
504676
  Trace("var-elim-quant-debug")
883
252338
      << "Eliminate : " << lit << ", pol = " << pol << "?" << std::endl;
884
505500
  if (lit.getKind() == APPLY_TESTER && pol && lit[0].getKind() == BOUND_VARIABLE
885
504724
      && d_opts.quantifiers.dtVarExpandQuant)
886
  {
887
96
    Trace("var-elim-dt") << "Expand datatype variable based on : " << lit
888
48
                         << std::endl;
889
    std::vector<Node>::iterator ita =
890
48
        std::find(args.begin(), args.end(), lit[0]);
891
48
    if (ita != args.end())
892
    {
893
48
      vars.push_back(lit[0]);
894
96
      Node tester = lit.getOperator();
895
48
      int index = datatypes::utils::indexOf(tester);
896
48
      const DType& dt = datatypes::utils::datatypeOf(tester);
897
48
      const DTypeConstructor& c = dt[index];
898
96
      std::vector<Node> newChildren;
899
96
      Node cons = c.getConstructor();
900
96
      TypeNode tspec;
901
      // take into account if parametric
902
48
      if (dt.isParametric())
903
      {
904
2
        tspec = c.getSpecializedConstructorType(lit[0].getType());
905
6
        cons = nm->mkNode(
906
4
            APPLY_TYPE_ASCRIPTION, nm->mkConst(AscriptionType(tspec)), cons);
907
      }
908
      else
909
      {
910
46
        tspec = cons.getType();
911
      }
912
48
      newChildren.push_back(cons);
913
96
      std::vector<Node> newVars;
914
48
      BoundVarManager* bvm = nm->getBoundVarManager();
915
117
      for (size_t j = 0, nargs = c.getNumArgs(); j < nargs; j++)
916
      {
917
138
        TypeNode tn = tspec[j];
918
138
        Node rn = nm->mkConst(Rational(j));
919
138
        Node cacheVal = BoundVarManager::getCacheValue(body, lit, rn);
920
138
        Node v = bvm->mkBoundVar<QRewDtExpandAttribute>(cacheVal, tn);
921
69
        newChildren.push_back(v);
922
69
        newVars.push_back(v);
923
      }
924
48
      subs.push_back(nm->mkNode(APPLY_CONSTRUCTOR, newChildren));
925
96
      Trace("var-elim-dt") << "...apply substitution " << subs[0] << "/"
926
48
                           << vars[0] << std::endl;
927
48
      args.erase(ita);
928
48
      args.insert(args.end(), newVars.begin(), newVars.end());
929
48
      return true;
930
    }
931
  }
932
  // all eliminations below guarded by varElimQuant()
933
252290
  if (!d_opts.quantifiers.varElimQuant)
934
  {
935
    return false;
936
  }
937
938
252290
  if (lit.getKind() == EQUAL)
939
  {
940
137639
    if (pol || lit[0].getType().isBoolean())
941
    {
942
201240
      for (unsigned i = 0; i < 2; i++)
943
      {
944
136589
        bool tpol = pol;
945
267087
        Node v_slv = lit[i];
946
136589
        if (v_slv.getKind() == NOT)
947
        {
948
5676
          v_slv = v_slv[0];
949
5676
          tpol = !tpol;
950
        }
951
        std::vector<Node>::iterator ita =
952
136589
            std::find(args.begin(), args.end(), v_slv);
953
136589
        if (ita != args.end())
954
        {
955
6596
          if (isVarElim(v_slv, lit[1 - i]))
956
          {
957
12182
            Node slv = lit[1 - i];
958
6091
            if (!tpol)
959
            {
960
660
              Assert(slv.getType().isBoolean());
961
660
              slv = slv.negate();
962
            }
963
12182
            Trace("var-elim-quant")
964
6091
                << "Variable eliminate based on equality : " << v_slv << " -> "
965
6091
                << slv << std::endl;
966
6091
            vars.push_back(v_slv);
967
6091
            subs.push_back(slv);
968
6091
            args.erase(ita);
969
6091
            return true;
970
          }
971
        }
972
      }
973
    }
974
  }
975
246199
  if (lit.getKind() == BOUND_VARIABLE)
976
  {
977
1564
    std::vector< Node >::iterator ita = std::find( args.begin(), args.end(), lit );
978
1564
    if( ita!=args.end() ){
979
1562
      Trace("var-elim-bool") << "Variable eliminate : " << lit << std::endl;
980
1562
      vars.push_back( lit );
981
1562
      subs.push_back( NodeManager::currentNM()->mkConst( pol ) );
982
1562
      args.erase( ita );
983
1562
      return true;
984
    }
985
  }
986
244637
  if (lit.getKind() == EQUAL && pol)
987
  {
988
76406
    Node var;
989
76406
    Node slv = getVarElimEq(lit, args, var);
990
38331
    if (!slv.isNull())
991
    {
992
256
      Assert(!var.isNull());
993
      std::vector<Node>::iterator ita =
994
256
          std::find(args.begin(), args.end(), var);
995
256
      Assert(ita != args.end());
996
512
      Trace("var-elim-quant")
997
256
          << "Variable eliminate based on theory-specific solving : " << var
998
256
          << " -> " << slv << std::endl;
999
256
      Assert(!expr::hasSubterm(slv, var));
1000
256
      Assert(slv.getType().isSubtypeOf(var.getType()));
1001
256
      vars.push_back(var);
1002
256
      subs.push_back(slv);
1003
256
      args.erase(ita);
1004
256
      return true;
1005
    }
1006
  }
1007
244381
  return false;
1008
}
1009
1010
123116
bool QuantifiersRewriter::getVarElim(Node body,
1011
                                     std::vector<Node>& args,
1012
                                     std::vector<Node>& vars,
1013
                                     std::vector<Node>& subs) const
1014
{
1015
123116
  return getVarElimInternal(body, body, false, args, vars, subs);
1016
}
1017
1018
277672
bool QuantifiersRewriter::getVarElimInternal(Node body,
1019
                                             Node n,
1020
                                             bool pol,
1021
                                             std::vector<Node>& args,
1022
                                             std::vector<Node>& vars,
1023
                                             std::vector<Node>& subs) const
1024
{
1025
277672
  Kind nk = n.getKind();
1026
277672
  if (nk == NOT)
1027
  {
1028
97246
    n = n[0];
1029
97246
    pol = !pol;
1030
97246
    nk = n.getKind();
1031
97246
    Assert(nk != NOT);
1032
  }
1033
277672
  if ((nk == AND && pol) || (nk == OR && !pol))
1034
  {
1035
201480
    for (const Node& cn : n)
1036
    {
1037
154202
      if (getVarElimInternal(body, cn, pol, args, vars, subs))
1038
      {
1039
4848
        return true;
1040
      }
1041
    }
1042
47278
    return false;
1043
  }
1044
225546
  return getVarElimLit(body, n, pol, args, vars, subs);
1045
}
1046
1047
354
bool QuantifiersRewriter::hasVarElim(Node n,
1048
                                     bool pol,
1049
                                     std::vector<Node>& args) const
1050
{
1051
708
  std::vector< Node > vars;
1052
708
  std::vector< Node > subs;
1053
708
  return getVarElimInternal(n, n, pol, args, vars, subs);
1054
}
1055
1056
117764
bool QuantifiersRewriter::getVarElimIneq(Node body,
1057
                                         std::vector<Node>& args,
1058
                                         std::vector<Node>& bounds,
1059
                                         std::vector<Node>& subs,
1060
                                         QAttributes& qa)
1061
{
1062
117764
  Trace("var-elim-quant-debug") << "getVarElimIneq " << body << std::endl;
1063
  // For each variable v, we compute a set of implied bounds in the body
1064
  // of the quantified formula.
1065
  //   num_bounds[x][-1] stores the entailed lower bounds for x
1066
  //   num_bounds[x][1] stores the entailed upper bounds for x
1067
  //   num_bounds[x][0] stores the entailed disequalities for x
1068
  // These bounds are stored in a map that maps the literal for the bound to
1069
  // its required polarity. For example, for quantified formula
1070
  //   (forall ((x Int)) (or (= x 0) (>= x a)))
1071
  // we have:
1072
  //   num_bounds[x][0] contains { x -> { (= x 0) -> false } }
1073
  //   num_bounds[x][-1] contains { x -> { (>= x a) -> false } }
1074
  // This method succeeds in eliminating x if its only occurrences are in
1075
  // entailed disequalities, and one kind of bound. This is the case for the
1076
  // above quantified formula, which can be rewritten to false. The reason
1077
  // is that we can always chose a value for x that is arbitrarily large (resp.
1078
  // small) to satisfy all disequalities and inequalities for x.
1079
235528
  std::map<Node, std::map<int, std::map<Node, bool>>> num_bounds;
1080
  // The set of variables that we know we can not eliminate
1081
235528
  std::unordered_set<Node> ineligVars;
1082
  // compute the entailed literals
1083
235528
  QuantPhaseReq qpr(body);
1084
  // map to track which literals we have already processed, and hence can be
1085
  // excluded from the free variables check in the latter half of this method.
1086
235528
  std::map<Node, int> processed;
1087
322705
  for (const std::pair<const Node, bool>& pr : qpr.d_phase_reqs)
1088
  {
1089
    // an inequality that is entailed with a given polarity
1090
250456
    Node lit = pr.first;
1091
204941
    bool pol = pr.second;
1092
409882
    Trace("var-elim-quant-debug") << "Process inequality bounds : " << lit
1093
204941
                                  << ", pol = " << pol << "..." << std::endl;
1094
    bool canSolve =
1095
204941
        lit.getKind() == GEQ
1096
409882
        || (lit.getKind() == EQUAL && lit[0].getType().isReal() && !pol);
1097
204941
    if (!canSolve)
1098
    {
1099
159426
      continue;
1100
    }
1101
    // solve the inequality
1102
91030
    std::map<Node, Node> msum;
1103
45515
    if (!ArithMSum::getMonomialSumLit(lit, msum))
1104
    {
1105
      // not an inequality, cannot use
1106
      continue;
1107
    }
1108
45515
    processed[lit] = pol ? -1 : 1;
1109
142027
    for (const std::pair<const Node, Node>& m : msum)
1110
    {
1111
96512
      if (!m.first.isNull() && ineligVars.find(m.first) == ineligVars.end())
1112
      {
1113
        std::vector<Node>::iterator ita =
1114
76664
            std::find(args.begin(), args.end(), m.first);
1115
76664
        if (ita != args.end())
1116
        {
1117
          // store that this literal is upper/lower bound for itm->first
1118
75088
          Node veq_c;
1119
75088
          Node val;
1120
          int ires =
1121
37544
              ArithMSum::isolate(m.first, msum, veq_c, val, lit.getKind());
1122
37544
          if (ires != 0 && veq_c.isNull())
1123
          {
1124
37074
            if (lit.getKind() == GEQ)
1125
            {
1126
25478
              bool is_upper = pol != (ires == 1);
1127
50956
              Trace("var-elim-ineq-debug")
1128
25478
                  << lit << " is a " << (is_upper ? "upper" : "lower")
1129
25478
                  << " bound for " << m.first << std::endl;
1130
50956
              Trace("var-elim-ineq-debug")
1131
25478
                  << "  pol/ires = " << pol << " " << ires << std::endl;
1132
25478
              num_bounds[m.first][is_upper ? 1 : -1][lit] = pol;
1133
            }
1134
            else
1135
            {
1136
23192
              Trace("var-elim-ineq-debug")
1137
11596
                  << lit << " is a disequality for " << m.first << std::endl;
1138
11596
              num_bounds[m.first][0][lit] = pol;
1139
            }
1140
          }
1141
          else
1142
          {
1143
940
            Trace("var-elim-ineq-debug")
1144
470
                << "...ineligible " << m.first
1145
470
                << " since it cannot be solved for (" << ires << ", " << veq_c
1146
470
                << ")." << std::endl;
1147
470
            num_bounds.erase(m.first);
1148
470
            ineligVars.insert(m.first);
1149
          }
1150
        }
1151
        else
1152
        {
1153
          // compute variables in itm->first, these are not eligible for
1154
          // elimination
1155
78240
          std::unordered_set<Node> fvs;
1156
39120
          expr::getFreeVariables(m.first, fvs);
1157
82351
          for (const Node& v : fvs)
1158
          {
1159
86462
            Trace("var-elim-ineq-debug")
1160
43231
                << "...ineligible " << v
1161
43231
                << " since it is contained in monomial." << std::endl;
1162
43231
            num_bounds.erase(v);
1163
43231
            ineligVars.insert(v);
1164
          }
1165
        }
1166
      }
1167
    }
1168
  }
1169
1170
  // collect all variables that have only upper/lower bounds
1171
235528
  std::map<Node, bool> elig_vars;
1172
17672
  for (const std::pair<const Node, std::map<int, std::map<Node, bool>>>& nb :
1173
117764
       num_bounds)
1174
  {
1175
17672
    if (nb.second.find(1) == nb.second.end())
1176
    {
1177
18024
      Trace("var-elim-ineq-debug")
1178
9012
          << "Variable " << nb.first << " has only lower bounds." << std::endl;
1179
9012
      elig_vars[nb.first] = false;
1180
    }
1181
8660
    else if (nb.second.find(-1) == nb.second.end())
1182
    {
1183
4846
      Trace("var-elim-ineq-debug")
1184
2423
          << "Variable " << nb.first << " has only upper bounds." << std::endl;
1185
2423
      elig_vars[nb.first] = true;
1186
    }
1187
  }
1188
117764
  if (elig_vars.empty())
1189
  {
1190
107973
    return false;
1191
  }
1192
19582
  std::vector<Node> inactive_vars;
1193
19582
  std::map<Node, std::map<int, bool> > visited;
1194
  // traverse the body, invalidate variables if they occur in places other than
1195
  // the bounds they occur in
1196
19582
  std::unordered_map<TNode, std::unordered_set<int>> evisited;
1197
19582
  std::vector<TNode> evisit;
1198
19582
  std::vector<int> evisit_pol;
1199
19582
  TNode ecur;
1200
  int ecur_pol;
1201
9791
  evisit.push_back(body);
1202
9791
  evisit_pol.push_back(1);
1203
9791
  if (!qa.d_ipl.isNull())
1204
  {
1205
    // do not eliminate variables that occur in the annotation
1206
1432
    evisit.push_back(qa.d_ipl);
1207
1432
    evisit_pol.push_back(0);
1208
  }
1209
68591
  do
1210
  {
1211
78382
    ecur = evisit.back();
1212
78382
    evisit.pop_back();
1213
78382
    ecur_pol = evisit_pol.back();
1214
78382
    evisit_pol.pop_back();
1215
78382
    std::unordered_set<int>& epp = evisited[ecur];
1216
78382
    if (epp.find(ecur_pol) == epp.end())
1217
    {
1218
76697
      epp.insert(ecur_pol);
1219
76697
      if (elig_vars.find(ecur) != elig_vars.end())
1220
      {
1221
        // variable contained in a place apart from bounds, no longer eligible
1222
        // for elimination
1223
10725
        elig_vars.erase(ecur);
1224
21450
        Trace("var-elim-ineq-debug") << "...found occurrence of " << ecur
1225
10725
                                     << ", mark ineligible" << std::endl;
1226
      }
1227
      else
1228
      {
1229
65972
        bool rec = true;
1230
65972
        bool pol = ecur_pol >= 0;
1231
65972
        bool hasPol = ecur_pol != 0;
1232
65972
        if (hasPol)
1233
        {
1234
35761
          std::map<Node, int>::iterator itx = processed.find(ecur);
1235
35761
          if (itx != processed.end() && itx->second == ecur_pol)
1236
          {
1237
            // already processed this literal as a bound
1238
6348
            rec = false;
1239
          }
1240
        }
1241
65972
        if (rec)
1242
        {
1243
155579
          for (unsigned j = 0, size = ecur.getNumChildren(); j < size; j++)
1244
          {
1245
            bool newHasPol;
1246
            bool newPol;
1247
95955
            QuantPhaseReq::getPolarity(ecur, j, hasPol, pol, newHasPol, newPol);
1248
95955
            evisit.push_back(ecur[j]);
1249
95955
            evisit_pol.push_back(newHasPol ? (newPol ? 1 : -1) : 0);
1250
          }
1251
        }
1252
      }
1253
    }
1254
78382
  } while (!evisit.empty() && !elig_vars.empty());
1255
1256
9791
  bool ret = false;
1257
9791
  NodeManager* nm = NodeManager::currentNM();
1258
10501
  for (const std::pair<const Node, bool>& ev : elig_vars)
1259
  {
1260
1420
    Node v = ev.first;
1261
1420
    Trace("var-elim-ineq-debug")
1262
710
        << v << " is eligible for elimination." << std::endl;
1263
    // do substitution corresponding to infinite projection, all literals
1264
    // involving unbounded variable go to true/false
1265
    // disequalities of eligible variables are also eliminated
1266
710
    std::map<int, std::map<Node, bool>>& nbv = num_bounds[v];
1267
2130
    for (size_t i = 0; i < 2; i++)
1268
    {
1269
1420
      size_t nindex = i == 0 ? (elig_vars[v] ? 1 : -1) : 0;
1270
2311
      for (const std::pair<const Node, bool>& nb : nbv[nindex])
1271
      {
1272
1782
        Trace("var-elim-ineq-debug")
1273
891
            << "  subs : " << nb.first << " -> " << nb.second << std::endl;
1274
891
        bounds.push_back(nb.first);
1275
891
        subs.push_back(nm->mkConst(nb.second));
1276
      }
1277
    }
1278
    // eliminate from args
1279
710
    std::vector<Node>::iterator ita = std::find(args.begin(), args.end(), v);
1280
710
    Assert(ita != args.end());
1281
710
    args.erase(ita);
1282
710
    ret = true;
1283
  }
1284
9791
  return ret;
1285
}
1286
1287
118005
Node QuantifiersRewriter::computeVarElimination(Node body,
1288
                                                std::vector<Node>& args,
1289
                                                QAttributes& qa) const
1290
{
1291
118005
  if (!d_opts.quantifiers.varElimQuant && !d_opts.quantifiers.dtVarExpandQuant
1292
      && !d_opts.quantifiers.varIneqElimQuant)
1293
  {
1294
    return body;
1295
  }
1296
236010
  Trace("var-elim-quant-debug")
1297
118005
      << "computeVarElimination " << body << std::endl;
1298
236010
  Node prev;
1299
363915
  while (prev != body && !args.empty())
1300
  {
1301
122955
    prev = body;
1302
1303
245910
    std::vector<Node> vars;
1304
245910
    std::vector<Node> subs;
1305
    // standard variable elimination
1306
122955
    if (d_opts.quantifiers.varElimQuant)
1307
    {
1308
122955
      getVarElim(body, args, vars, subs);
1309
    }
1310
    // variable elimination based on one-direction inequalities
1311
122955
    if (vars.empty() && d_opts.quantifiers.varIneqElimQuant)
1312
    {
1313
117764
      getVarElimIneq(body, args, vars, subs, qa);
1314
    }
1315
    // if we eliminated a variable, update body and reprocess
1316
122955
    if (!vars.empty())
1317
    {
1318
11596
      Trace("var-elim-quant-debug")
1319
5798
          << "VE " << vars.size() << "/" << args.size() << std::endl;
1320
5798
      Assert(vars.size() == subs.size());
1321
      // remake with eliminated nodes
1322
5798
      body =
1323
11596
          body.substitute(vars.begin(), vars.end(), subs.begin(), subs.end());
1324
5798
      body = Rewriter::rewrite(body);
1325
5798
      if (!qa.d_ipl.isNull())
1326
      {
1327
54
        qa.d_ipl = qa.d_ipl.substitute(
1328
27
            vars.begin(), vars.end(), subs.begin(), subs.end());
1329
      }
1330
5798
      Trace("var-elim-quant") << "Return " << body << std::endl;
1331
    }
1332
  }
1333
118005
  return body;
1334
}
1335
1336
489785
Node QuantifiersRewriter::computePrenex(Node q,
1337
                                        Node body,
1338
                                        std::unordered_set<Node>& args,
1339
                                        std::unordered_set<Node>& nargs,
1340
                                        bool pol,
1341
                                        bool prenexAgg) const
1342
{
1343
489785
  NodeManager* nm = NodeManager::currentNM();
1344
489785
  Kind k = body.getKind();
1345
489785
  if (k == FORALL)
1346
  {
1347
27623
    if ((pol || prenexAgg)
1348
16921
        && (d_opts.quantifiers.prenexQuantUser
1349
15772
            || !QuantAttributes::hasPattern(body)))
1350
    {
1351
2298
      std::vector< Node > terms;
1352
2298
      std::vector< Node > subs;
1353
1149
      BoundVarManager* bvm = nm->getBoundVarManager();
1354
      //for doing prenexing of same-signed quantifiers
1355
      //must rename each variable that already exists
1356
2931
      for (const Node& v : body[0])
1357
      {
1358
1782
        terms.push_back(v);
1359
3564
        TypeNode vt = v.getType();
1360
3564
        Node vv;
1361
1782
        if (!q.isNull())
1362
        {
1363
          // We cache based on the original quantified formula, the subformula
1364
          // that we are pulling variables from (body), and the variable v.
1365
          // The argument body is required since in rare cases, two subformulas
1366
          // may share the same variables. This is the case for define-fun
1367
          // or inferred substitutions that contain quantified formulas.
1368
3564
          Node cacheVal = BoundVarManager::getCacheValue(q, body, v);
1369
1782
          vv = bvm->mkBoundVar<QRewPrenexAttribute>(cacheVal, vt);
1370
        }
1371
        else
1372
        {
1373
          // not specific to a quantified formula, use normal
1374
          vv = nm->mkBoundVar(vt);
1375
        }
1376
1782
        subs.push_back(vv);
1377
      }
1378
1149
      if (pol)
1379
      {
1380
1149
        args.insert(subs.begin(), subs.end());
1381
      }
1382
      else
1383
      {
1384
        nargs.insert(subs.begin(), subs.end());
1385
      }
1386
2298
      Node newBody = body[1];
1387
1149
      newBody = newBody.substitute( terms.begin(), terms.end(), subs.begin(), subs.end() );
1388
1149
      return newBody;
1389
    }
1390
  //must remove structure
1391
  }
1392
475320
  else if (prenexAgg && k == ITE && body.getType().isBoolean())
1393
  {
1394
    Node nn = nm->mkNode(AND,
1395
                         nm->mkNode(OR, body[0].notNode(), body[1]),
1396
                         nm->mkNode(OR, body[0], body[2]));
1397
    return computePrenex(q, nn, args, nargs, pol, prenexAgg);
1398
  }
1399
475320
  else if (prenexAgg && k == EQUAL && body[0].getType().isBoolean())
1400
  {
1401
    Node nn = nm->mkNode(AND,
1402
                         nm->mkNode(OR, body[0].notNode(), body[1]),
1403
                         nm->mkNode(OR, body[0], body[1].notNode()));
1404
    return computePrenex(q, nn, args, nargs, pol, prenexAgg);
1405
475320
  }else if( body.getType().isBoolean() ){
1406
475320
    Assert(k != EXISTS);
1407
475320
    bool childrenChanged = false;
1408
949443
    std::vector< Node > newChildren;
1409
1451242
    for (size_t i = 0, nchild = body.getNumChildren(); i < nchild; i++)
1410
    {
1411
      bool newHasPol;
1412
      bool newPol;
1413
975922
      QuantPhaseReq::getPolarity( body, i, true, pol, newHasPol, newPol );
1414
1580262
      if (!newHasPol)
1415
      {
1416
604340
        newChildren.push_back( body[i] );
1417
604340
        continue;
1418
      }
1419
743164
      Node n = computePrenex(q, body[i], args, nargs, newPol, prenexAgg);
1420
371582
      newChildren.push_back(n);
1421
371582
      childrenChanged = n != body[i] || childrenChanged;
1422
    }
1423
475320
    if( childrenChanged ){
1424
1197
      if (k == NOT && newChildren[0].getKind() == NOT)
1425
      {
1426
        return newChildren[0][0];
1427
      }
1428
1197
      return nm->mkNode(k, newChildren);
1429
    }
1430
  }
1431
487439
  return body;
1432
}
1433
1434
47575
Node QuantifiersRewriter::computeSplit(std::vector<Node>& args,
1435
                                       Node body,
1436
                                       QAttributes& qa) const
1437
{
1438
47575
  Assert(body.getKind() == OR);
1439
47575
  size_t var_found_count = 0;
1440
47575
  size_t eqc_count = 0;
1441
47575
  size_t eqc_active = 0;
1442
95150
  std::map< Node, int > var_to_eqc;
1443
95150
  std::map< int, std::vector< Node > > eqc_to_var;
1444
95150
  std::map< int, std::vector< Node > > eqc_to_lit;
1445
1446
95150
  std::vector<Node> lits;
1447
1448
201720
  for( unsigned i=0; i<body.getNumChildren(); i++ ){
1449
    //get variables contained in the literal
1450
308290
    Node n = body[i];
1451
308290
    std::vector< Node > lit_args;
1452
154145
    computeArgVec( args, lit_args, n );
1453
154145
    if( lit_args.empty() ){
1454
1656
      lits.push_back( n );
1455
    }else {
1456
      //collect the equivalence classes this literal belongs to, and the new variables it contributes
1457
304978
      std::vector< int > eqcs;
1458
304978
      std::vector< Node > lit_new_args;
1459
      //for each variable in literal
1460
503957
      for( unsigned j=0; j<lit_args.size(); j++) {
1461
        //see if the variable has already been found
1462
351468
        if (var_to_eqc.find(lit_args[j])!=var_to_eqc.end()) {
1463
232795
          int eqc = var_to_eqc[lit_args[j]];
1464
232795
          if (std::find(eqcs.begin(), eqcs.end(), eqc)==eqcs.end()) {
1465
104623
            eqcs.push_back(eqc);
1466
          }
1467
        }else{
1468
118673
          var_found_count++;
1469
118673
          lit_new_args.push_back(lit_args[j]);
1470
        }
1471
      }
1472
152489
      if (eqcs.empty()) {
1473
54788
        eqcs.push_back(eqc_count);
1474
54788
        eqc_count++;
1475
54788
        eqc_active++;
1476
      }
1477
1478
152489
      int eqcz = eqcs[0];
1479
      //merge equivalence classes with eqcz
1480
159411
      for (unsigned j=1; j<eqcs.size(); j++) {
1481
6922
        int eqc = eqcs[j];
1482
        //move all variables from equivalence class
1483
32353
        for (unsigned k=0; k<eqc_to_var[eqc].size(); k++) {
1484
50862
          Node v = eqc_to_var[eqc][k];
1485
25431
          var_to_eqc[v] = eqcz;
1486
25431
          eqc_to_var[eqcz].push_back(v);
1487
        }
1488
6922
        eqc_to_var.erase(eqc);
1489
        //move all literals from equivalence class
1490
31076
        for (unsigned k=0; k<eqc_to_lit[eqc].size(); k++) {
1491
48308
          Node l = eqc_to_lit[eqc][k];
1492
24154
          eqc_to_lit[eqcz].push_back(l);
1493
        }
1494
6922
        eqc_to_lit.erase(eqc);
1495
6922
        eqc_active--;
1496
      }
1497
      //add variables to equivalence class
1498
271162
      for (unsigned j=0; j<lit_new_args.size(); j++) {
1499
118673
        var_to_eqc[lit_new_args[j]] = eqcz;
1500
118673
        eqc_to_var[eqcz].push_back(lit_new_args[j]);
1501
      }
1502
      //add literal to equivalence class
1503
152489
      eqc_to_lit[eqcz].push_back(n);
1504
    }
1505
  }
1506
47575
  if ( eqc_active>1 || !lits.empty() || var_to_eqc.size()!=args.size() ){
1507
696
    NodeManager* nm = NodeManager::currentNM();
1508
696
    Trace("clause-split-debug") << "Split quantified formula with body " << body << std::endl;
1509
696
    Trace("clause-split-debug") << "   Ground literals: " << std::endl;
1510
2352
    for( size_t i=0; i<lits.size(); i++) {
1511
1656
      Trace("clause-split-debug") << "      " << lits[i] << std::endl;
1512
    }
1513
696
    Trace("clause-split-debug") << std::endl;
1514
696
    Trace("clause-split-debug") << "Equivalence classes: " << std::endl;
1515
1683
    for (std::map< int, std::vector< Node > >::iterator it = eqc_to_lit.begin(); it != eqc_to_lit.end(); ++it ){
1516
987
      Trace("clause-split-debug") << "   Literals: " << std::endl;
1517
4657
      for (size_t i=0; i<it->second.size(); i++) {
1518
3670
        Trace("clause-split-debug") << "      " << it->second[i] << std::endl;
1519
      }
1520
987
      int eqc = it->first;
1521
987
      Trace("clause-split-debug") << "   Variables: " << std::endl;
1522
4140
      for (size_t i=0; i<eqc_to_var[eqc].size(); i++) {
1523
3153
        Trace("clause-split-debug") << "      " << eqc_to_var[eqc][i] << std::endl;
1524
      }
1525
987
      Trace("clause-split-debug") << std::endl;
1526
1974
      Node bvl = NodeManager::currentNM()->mkNode( BOUND_VAR_LIST, eqc_to_var[eqc]);
1527
      Node bd =
1528
1974
          it->second.size() == 1 ? it->second[0] : nm->mkNode(OR, it->second);
1529
1974
      Node fa = nm->mkNode(FORALL, bvl, bd);
1530
987
      lits.push_back(fa);
1531
    }
1532
696
    Assert(!lits.empty());
1533
1392
    Node nf = lits.size()==1 ? lits[0] : NodeManager::currentNM()->mkNode(OR,lits);
1534
696
    Trace("clause-split-debug") << "Made node : " << nf << std::endl;
1535
696
    return nf;
1536
  }else{
1537
46879
    return mkForAll( args, body, qa );
1538
  }
1539
}
1540
1541
119924
Node QuantifiersRewriter::mkForAll(const std::vector<Node>& args,
1542
                                   Node body,
1543
                                   QAttributes& qa)
1544
{
1545
119924
  if (args.empty())
1546
  {
1547
250
    return body;
1548
  }
1549
119674
  NodeManager* nm = NodeManager::currentNM();
1550
239348
  std::vector<Node> children;
1551
119674
  children.push_back(nm->mkNode(kind::BOUND_VAR_LIST, args));
1552
119674
  children.push_back(body);
1553
119674
  if (!qa.d_ipl.isNull())
1554
  {
1555
14919
    children.push_back(qa.d_ipl);
1556
  }
1557
119674
  return nm->mkNode(kind::FORALL, children);
1558
}
1559
1560
Node QuantifiersRewriter::mkForall(const std::vector<Node>& args,
1561
                                   Node body,
1562
                                   bool marked)
1563
{
1564
  std::vector< Node > iplc;
1565
  return mkForall( args, body, iplc, marked );
1566
}
1567
1568
Node QuantifiersRewriter::mkForall(const std::vector<Node>& args,
1569
                                   Node body,
1570
                                   std::vector<Node>& iplc,
1571
                                   bool marked)
1572
{
1573
  if (args.empty())
1574
  {
1575
    return body;
1576
  }
1577
  NodeManager* nm = NodeManager::currentNM();
1578
  std::vector<Node> children;
1579
  children.push_back(nm->mkNode(kind::BOUND_VAR_LIST, args));
1580
  children.push_back(body);
1581
  if (marked)
1582
  {
1583
    SkolemManager* sm = nm->getSkolemManager();
1584
    Node avar = sm->mkDummySkolem("id", nm->booleanType());
1585
    QuantIdNumAttribute ida;
1586
    avar.setAttribute(ida, 0);
1587
    iplc.push_back(nm->mkNode(kind::INST_ATTRIBUTE, avar));
1588
  }
1589
  if (!iplc.empty())
1590
  {
1591
    children.push_back(nm->mkNode(kind::INST_PATTERN_LIST, iplc));
1592
  }
1593
  return nm->mkNode(kind::FORALL, children);
1594
}
1595
1596
//computes miniscoping, also eliminates variables that do not occur free in body
1597
121447
Node QuantifiersRewriter::computeMiniscoping(Node q, QAttributes& qa) const
1598
{
1599
121447
  NodeManager* nm = NodeManager::currentNM();
1600
242894
  std::vector<Node> args(q[0].begin(), q[0].end());
1601
242894
  Node body = q[1];
1602
121447
  if( body.getKind()==FORALL ){
1603
    //combine prenex
1604
32
    std::vector< Node > newArgs;
1605
16
    newArgs.insert(newArgs.end(), q[0].begin(), q[0].end());
1606
33
    for( unsigned i=0; i<body[0].getNumChildren(); i++ ){
1607
17
      newArgs.push_back( body[0][i] );
1608
    }
1609
16
    return mkForAll( newArgs, body[1], qa );
1610
121431
  }else if( body.getKind()==AND ){
1611
    // aggressive miniscoping implies that structural miniscoping should
1612
    // be applied first
1613
2162
    if (d_opts.quantifiers.miniscopeQuant
1614
1323
        || d_opts.quantifiers.aggressiveMiniscopeQuant)
1615
    {
1616
841
      BoundVarManager* bvm = nm->getBoundVarManager();
1617
      // Break apart the quantifed formula
1618
      // forall x. P1 ^ ... ^ Pn ---> forall x. P1 ^ ... ^ forall x. Pn
1619
1682
      NodeBuilder t(kind::AND);
1620
1682
      std::vector<Node> argsc;
1621
4381
      for (size_t i = 0, nchild = body.getNumChildren(); i < nchild; i++)
1622
      {
1623
3540
        if (argsc.empty())
1624
        {
1625
          // If not done so, we must create fresh copy of args. This is to
1626
          // ensure that quantified formulas do not reuse variables.
1627
7965
          for (const Node& v : q[0])
1628
          {
1629
11910
            TypeNode vt = v.getType();
1630
11910
            Node cacheVal = BoundVarManager::getCacheValue(q, v, i);
1631
11910
            Node vv = bvm->mkBoundVar<QRewMiniscopeAttribute>(cacheVal, vt);
1632
5955
            argsc.push_back(vv);
1633
          }
1634
        }
1635
7080
        Node b = body[i];
1636
        Node bodyc =
1637
7080
            b.substitute(args.begin(), args.end(), argsc.begin(), argsc.end());
1638
3540
        if (b == bodyc)
1639
        {
1640
          // Did not contain variables in args, thus it is ground. Since we did
1641
          // not use them, we keep the variables argsc for the next child.
1642
1576
          t << b;
1643
        }
1644
        else
1645
        {
1646
          // make the miniscoped quantified formula
1647
3928
          Node cbvl = nm->mkNode(BOUND_VAR_LIST, argsc);
1648
3928
          Node qq = nm->mkNode(FORALL, cbvl, bodyc);
1649
1964
          t << qq;
1650
          // We used argsc, clear so we will construct a fresh copy above.
1651
1964
          argsc.clear();
1652
        }
1653
      }
1654
1682
      Node retVal = t;
1655
841
      return retVal;
1656
    }
1657
119269
  }else if( body.getKind()==OR ){
1658
49799
    if (d_opts.quantifiers.quantSplit)
1659
    {
1660
      //splitting subsumes free variable miniscoping, apply it with higher priority
1661
47575
      return computeSplit( args, body, qa );
1662
    }
1663
2224
    else if (d_opts.quantifiers.miniscopeQuantFreeVar
1664
2224
             || d_opts.quantifiers.aggressiveMiniscopeQuant)
1665
    {
1666
      // aggressive miniscoping implies that free variable miniscoping should
1667
      // be applied first
1668
4
      Node newBody = body;
1669
4
      NodeBuilder body_split(kind::OR);
1670
4
      NodeBuilder tb(kind::OR);
1671
12
      for (const Node& trm : body)
1672
      {
1673
8
        if (expr::hasSubterm(trm, args))
1674
        {
1675
4
          tb << trm;
1676
        }else{
1677
4
          body_split << trm;
1678
        }
1679
      }
1680
4
      if( tb.getNumChildren()==0 ){
1681
        return body_split;
1682
4
      }else if( body_split.getNumChildren()>0 ){
1683
4
        newBody = tb.getNumChildren()==1 ? tb.getChild( 0 ) : tb;
1684
8
        std::vector< Node > activeArgs;
1685
4
        computeArgVec2( args, activeArgs, newBody, qa.d_ipl );
1686
4
        body_split << mkForAll( activeArgs, newBody, qa );
1687
4
        return body_split.getNumChildren()==1 ? body_split.getChild( 0 ) : body_split;
1688
      }
1689
    }
1690
69470
  }else if( body.getKind()==NOT ){
1691
15402
    Assert(isLiteral(body[0]));
1692
  }
1693
  //remove variables that don't occur
1694
146022
  std::vector< Node > activeArgs;
1695
73011
  computeArgVec2( args, activeArgs, body, qa.d_ipl );
1696
73011
  return mkForAll( activeArgs, body, qa );
1697
}
1698
1699
14
Node QuantifiersRewriter::computeAggressiveMiniscoping(std::vector<Node>& args,
1700
                                                       Node body) const
1701
{
1702
28
  std::map<Node, std::vector<Node> > varLits;
1703
28
  std::map<Node, std::vector<Node> > litVars;
1704
14
  if (body.getKind() == OR) {
1705
    Trace("ag-miniscope") << "compute aggressive miniscoping on " << body << std::endl;
1706
    for (size_t i = 0; i < body.getNumChildren(); i++) {
1707
      std::vector<Node> activeArgs;
1708
      computeArgVec(args, activeArgs, body[i]);
1709
      for (unsigned j = 0; j < activeArgs.size(); j++) {
1710
        varLits[activeArgs[j]].push_back(body[i]);
1711
      }
1712
      std::vector<Node>& lit_body_i = litVars[body[i]];
1713
      std::vector<Node>::iterator lit_body_i_begin = lit_body_i.begin();
1714
      std::vector<Node>::const_iterator active_begin = activeArgs.begin();
1715
      std::vector<Node>::const_iterator active_end = activeArgs.end();
1716
      lit_body_i.insert(lit_body_i_begin, active_begin, active_end);
1717
    }
1718
    //find the variable in the least number of literals
1719
    Node bestVar;
1720
    for( std::map< Node, std::vector<Node> >::iterator it = varLits.begin(); it != varLits.end(); ++it ){
1721
      if( bestVar.isNull() || varLits[bestVar].size()>it->second.size() ){
1722
        bestVar = it->first;
1723
      }
1724
    }
1725
    Trace("ag-miniscope-debug") << "Best variable " << bestVar << " occurs in " << varLits[bestVar].size() << "/ " << body.getNumChildren() << " literals." << std::endl;
1726
    if( !bestVar.isNull() && varLits[bestVar].size()<body.getNumChildren() ){
1727
      //we can miniscope
1728
      Trace("ag-miniscope") << "Miniscope on " << bestVar << std::endl;
1729
      //make the bodies
1730
      std::vector<Node> qlit1;
1731
      qlit1.insert( qlit1.begin(), varLits[bestVar].begin(), varLits[bestVar].end() );
1732
      std::vector<Node> qlitt;
1733
      //for all literals not containing bestVar
1734
      for( size_t i=0; i<body.getNumChildren(); i++ ){
1735
        if( std::find( qlit1.begin(), qlit1.end(), body[i] )==qlit1.end() ){
1736
          qlitt.push_back( body[i] );
1737
        }
1738
      }
1739
      //make the variable lists
1740
      std::vector<Node> qvl1;
1741
      std::vector<Node> qvl2;
1742
      std::vector<Node> qvsh;
1743
      for( unsigned i=0; i<args.size(); i++ ){
1744
        bool found1 = false;
1745
        bool found2 = false;
1746
        for( size_t j=0; j<varLits[args[i]].size(); j++ ){
1747
          if( !found1 && std::find( qlit1.begin(), qlit1.end(), varLits[args[i]][j] )!=qlit1.end() ){
1748
            found1 = true;
1749
          }else if( !found2 && std::find( qlitt.begin(), qlitt.end(), varLits[args[i]][j] )!=qlitt.end() ){
1750
            found2 = true;
1751
          }
1752
          if( found1 && found2 ){
1753
            break;
1754
          }
1755
        }
1756
        if( found1 ){
1757
          if( found2 ){
1758
            qvsh.push_back( args[i] );
1759
          }else{
1760
            qvl1.push_back( args[i] );
1761
          }
1762
        }else{
1763
          Assert(found2);
1764
          qvl2.push_back( args[i] );
1765
        }
1766
      }
1767
      Assert(!qvl1.empty());
1768
      //check for literals that only contain shared variables
1769
      std::vector<Node> qlitsh;
1770
      std::vector<Node> qlit2;
1771
      for( size_t i=0; i<qlitt.size(); i++ ){
1772
        bool hasVar2 = false;
1773
        for( size_t j=0; j<litVars[qlitt[i]].size(); j++ ){
1774
          if( std::find( qvl2.begin(), qvl2.end(), litVars[qlitt[i]][j] )!=qvl2.end() ){
1775
            hasVar2 = true;
1776
            break;
1777
          }
1778
        }
1779
        if( hasVar2 ){
1780
          qlit2.push_back( qlitt[i] );
1781
        }else{
1782
          qlitsh.push_back( qlitt[i] );
1783
        }
1784
      }
1785
      varLits.clear();
1786
      litVars.clear();
1787
      Trace("ag-miniscope-debug") << "Split into literals : " << qlit1.size() << " / " << qlit2.size() << " / " << qlitsh.size();
1788
      Trace("ag-miniscope-debug") << ", variables : " << qvl1.size() << " / " << qvl2.size() << " / " << qvsh.size() << std::endl;
1789
      Node n1 = qlit1.size()==1 ? qlit1[0] : NodeManager::currentNM()->mkNode( OR, qlit1 );
1790
      n1 = computeAggressiveMiniscoping( qvl1, n1 );
1791
      qlitsh.push_back( n1 );
1792
      if( !qlit2.empty() ){
1793
        Node n2 = qlit2.size()==1 ? qlit2[0] : NodeManager::currentNM()->mkNode( OR, qlit2 );
1794
        n2 = computeAggressiveMiniscoping( qvl2, n2 );
1795
        qlitsh.push_back( n2 );
1796
      }
1797
      Node n = NodeManager::currentNM()->mkNode( OR, qlitsh );
1798
      if( !qvsh.empty() ){
1799
        Node bvl = NodeManager::currentNM()->mkNode(kind::BOUND_VAR_LIST, qvsh);
1800
        n = NodeManager::currentNM()->mkNode( FORALL, bvl, n );
1801
      }
1802
      Trace("ag-miniscope") << "Return " << n << " for " << body << std::endl;
1803
      return n;
1804
    }
1805
  }
1806
28
  QAttributes qa;
1807
14
  return mkForAll( args, body, qa );
1808
}
1809
1810
999448
bool QuantifiersRewriter::doOperation(Node q,
1811
                                      RewriteStep computeOption,
1812
                                      QAttributes& qa) const
1813
{
1814
999448
  bool is_strict_trigger =
1815
999448
      qa.d_hasPattern
1816
999448
      && d_opts.quantifiers.userPatternsQuant == options::UserPatMode::STRICT;
1817
999448
  bool is_std = qa.isStandard() && !is_strict_trigger;
1818
999448
  if (computeOption == COMPUTE_ELIM_SYMBOLS)
1819
  {
1820
139131
    return true;
1821
  }
1822
860317
  else if (computeOption == COMPUTE_MINISCOPING)
1823
  {
1824
125436
    return is_std;
1825
  }
1826
734881
  else if (computeOption == COMPUTE_AGGRESSIVE_MINISCOPING)
1827
  {
1828
123427
    return d_opts.quantifiers.aggressiveMiniscopeQuant && is_std;
1829
  }
1830
611454
  else if (computeOption == COMPUTE_EXT_REWRITE)
1831
  {
1832
123427
    return d_opts.quantifiers.extRewriteQuant;
1833
  }
1834
488027
  else if (computeOption == COMPUTE_PROCESS_TERMS)
1835
  {
1836
    return is_std
1837
123415
           && d_opts.quantifiers.iteLiftQuant
1838
123415
                  != options::IteLiftQuantMode::NONE;
1839
  }
1840
364612
  else if (computeOption == COMPUTE_COND_SPLIT)
1841
  {
1842
119657
    return (d_opts.quantifiers.iteDtTesterSplitQuant
1843
119297
            || d_opts.quantifiers.condVarSplitQuant)
1844
239314
           && !is_strict_trigger;
1845
  }
1846
244955
  else if (computeOption == COMPUTE_PRENEX)
1847
  {
1848
122961
    return d_opts.quantifiers.prenexQuant != options::PrenexQuantMode::NONE
1849
122961
           && !d_opts.quantifiers.aggressiveMiniscopeQuant && is_std;
1850
  }
1851
121994
  else if (computeOption == COMPUTE_VAR_ELIMINATION)
1852
  {
1853
121994
    return (d_opts.quantifiers.varElimQuant
1854
            || d_opts.quantifiers.dtVarExpandQuant)
1855
243988
           && is_std;
1856
  }
1857
  else
1858
  {
1859
    return false;
1860
  }
1861
}
1862
1863
//general method for computing various rewrites
1864
735908
Node QuantifiersRewriter::computeOperation(Node f,
1865
                                           RewriteStep computeOption,
1866
                                           QAttributes& qa) const
1867
{
1868
735908
  Trace("quantifiers-rewrite-debug") << "Compute operation " << computeOption << " on " << f << " " << qa.d_qid_num << std::endl;
1869
735908
  if (computeOption == COMPUTE_MINISCOPING)
1870
  {
1871
121447
    if (d_opts.quantifiers.prenexQuant == options::PrenexQuantMode::NORMAL)
1872
    {
1873
      if( !qa.d_qid_num.isNull() ){
1874
        //already processed this, return self
1875
        return f;
1876
      }
1877
    }
1878
    //return directly
1879
121447
    return computeMiniscoping(f, qa);
1880
  }
1881
1228922
  std::vector<Node> args(f[0].begin(), f[0].end());
1882
1228922
  Node n = f[1];
1883
614461
  if (computeOption == COMPUTE_ELIM_SYMBOLS)
1884
  {
1885
139131
    n = computeElimSymbols(n);
1886
475330
  }else if( computeOption==COMPUTE_AGGRESSIVE_MINISCOPING ){
1887
14
    return computeAggressiveMiniscoping( args, n );
1888
  }
1889
475316
  else if (computeOption == COMPUTE_EXT_REWRITE)
1890
  {
1891
25
    return computeExtendedRewrite(f);
1892
  }
1893
475291
  else if (computeOption == COMPUTE_PROCESS_TERMS)
1894
  {
1895
238852
    std::vector< Node > new_conds;
1896
119426
    n = computeProcessTerms( n, args, new_conds, f, qa );
1897
119426
    if( !new_conds.empty() ){
1898
      new_conds.push_back( n );
1899
      n = NodeManager::currentNM()->mkNode( OR, new_conds );
1900
    }
1901
  }
1902
355865
  else if (computeOption == COMPUTE_COND_SPLIT)
1903
  {
1904
119657
    n = computeCondSplit(n, args, qa);
1905
  }
1906
236208
  else if (computeOption == COMPUTE_PRENEX)
1907
  {
1908
118203
    if (d_opts.quantifiers.prenexQuant == options::PrenexQuantMode::NORMAL)
1909
    {
1910
      //will rewrite at preprocess time
1911
      return f;
1912
    }
1913
    else
1914
    {
1915
236406
      std::unordered_set<Node> argsSet, nargsSet;
1916
118203
      n = computePrenex(f, n, argsSet, nargsSet, true, false);
1917
118203
      Assert(nargsSet.empty());
1918
118203
      args.insert(args.end(), argsSet.begin(), argsSet.end());
1919
    }
1920
  }
1921
118005
  else if (computeOption == COMPUTE_VAR_ELIMINATION)
1922
  {
1923
118005
    n = computeVarElimination( n, args, qa );
1924
  }
1925
614422
  Trace("quantifiers-rewrite-debug") << "Compute Operation: return " << n << ", " << args.size() << std::endl;
1926
614422
  if( f[1]==n && args.size()==f[0].getNumChildren() ){
1927
596796
    return f;
1928
  }else{
1929
17626
    if( args.empty() ){
1930
848
      return n;
1931
    }else{
1932
33556
      std::vector< Node > children;
1933
16778
      children.push_back( NodeManager::currentNM()->mkNode(kind::BOUND_VAR_LIST, args ) );
1934
16778
      children.push_back( n );
1935
16778
      if( !qa.d_ipl.isNull() && args.size()==f[0].getNumChildren() ){
1936
2412
        children.push_back( qa.d_ipl );
1937
      }
1938
16778
      return NodeManager::currentNM()->mkNode(kind::FORALL, children );
1939
    }
1940
  }
1941
}
1942
1943
bool QuantifiersRewriter::isPrenexNormalForm( Node n ) {
1944
  if( n.getKind()==FORALL ){
1945
    return n[1].getKind()!=FORALL && isPrenexNormalForm( n[1] );
1946
  }else if( n.getKind()==NOT ){
1947
    return n[0].getKind()!=NOT && isPrenexNormalForm( n[0] );
1948
  }else{
1949
    return !expr::hasClosure(n);
1950
  }
1951
}
1952
1953
}  // namespace quantifiers
1954
}  // namespace theory
1955
31125
}  // namespace cvc5