GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/quantifiers_rewriter.cpp Lines: 866 1077 80.4 %
Date: 2021-09-29 Branches: 1873 4387 42.7 %

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