GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/datatypes/theory_datatypes.cpp Lines: 977 1139 85.8 %
Date: 2021-08-17 Branches: 2226 5134 43.4 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Morgan Deters, Mathias Preiner
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 the theory of datatypes.
14
 */
15
#include "theory/datatypes/theory_datatypes.h"
16
17
#include <map>
18
#include <sstream>
19
20
#include "base/check.h"
21
#include "expr/dtype.h"
22
#include "expr/dtype_cons.h"
23
#include "expr/kind.h"
24
#include "expr/skolem_manager.h"
25
#include "options/datatypes_options.h"
26
#include "options/quantifiers_options.h"
27
#include "options/smt_options.h"
28
#include "options/theory_options.h"
29
#include "proof/proof_node_manager.h"
30
#include "smt/logic_exception.h"
31
#include "theory/datatypes/datatypes_rewriter.h"
32
#include "theory/datatypes/theory_datatypes_type_rules.h"
33
#include "theory/datatypes/theory_datatypes_utils.h"
34
#include "theory/logic_info.h"
35
#include "theory/quantifiers_engine.h"
36
#include "theory/rewriter.h"
37
#include "theory/theory_model.h"
38
#include "theory/theory_state.h"
39
#include "theory/type_enumerator.h"
40
#include "theory/valuation.h"
41
42
using namespace std;
43
using namespace cvc5::kind;
44
using namespace cvc5::context;
45
46
namespace cvc5 {
47
namespace theory {
48
namespace datatypes {
49
50
9850
TheoryDatatypes::TheoryDatatypes(Env& env,
51
                                 OutputChannel& out,
52
9850
                                 Valuation valuation)
53
    : Theory(THEORY_DATATYPES, env, out, valuation),
54
9850
      d_term_sk(getUserContext()),
55
      d_labels(getSatContext()),
56
      d_selector_apps(getSatContext()),
57
      d_collectTermsCache(getSatContext()),
58
9850
      d_collectTermsCacheU(getUserContext()),
59
      d_functionTerms(getSatContext()),
60
9850
      d_singleton_eq(getUserContext()),
61
      d_sygusExtension(nullptr),
62
      d_state(env, valuation),
63
      d_im(*this, d_state, d_pnm),
64
39400
      d_notify(d_im, *this)
65
{
66
67
9850
  d_true = NodeManager::currentNM()->mkConst( true );
68
9850
  d_zero = NodeManager::currentNM()->mkConst( Rational(0) );
69
9850
  d_dtfCounter = 0;
70
71
  // indicate we are using the default theory state object
72
9850
  d_theoryState = &d_state;
73
9850
  d_inferManager = &d_im;
74
9850
}
75
76
29550
TheoryDatatypes::~TheoryDatatypes() {
77
35917
  for(std::map< Node, EqcInfo* >::iterator i = d_eqc_info.begin(), iend = d_eqc_info.end();
78
35917
      i != iend; ++i){
79
26067
    EqcInfo* current = (*i).second;
80
26067
    Assert(current != NULL);
81
26067
    delete current;
82
  }
83
19700
}
84
85
9850
TheoryRewriter* TheoryDatatypes::getTheoryRewriter() { return &d_rewriter; }
86
87
3766
ProofRuleChecker* TheoryDatatypes::getProofChecker() { return &d_checker; }
88
89
9850
bool TheoryDatatypes::needsEqualityEngine(EeSetupInfo& esi)
90
{
91
9850
  esi.d_notify = &d_notify;
92
9850
  esi.d_name = "theory::datatypes::ee";
93
  // need notifications on new constructors, merging datatype eqcs
94
9850
  esi.d_notifyNewClass = true;
95
9850
  esi.d_notifyMerge = true;
96
9850
  return true;
97
}
98
99
9850
void TheoryDatatypes::finishInit()
100
{
101
9850
  Assert(d_equalityEngine != nullptr);
102
  // The kinds we are treating as function application in congruence
103
9850
  d_equalityEngine->addFunctionKind(kind::APPLY_CONSTRUCTOR);
104
9850
  d_equalityEngine->addFunctionKind(kind::APPLY_SELECTOR_TOTAL);
105
9850
  d_equalityEngine->addFunctionKind(kind::APPLY_TESTER);
106
  // We could but don't do congruence for DT_SIZE and DT_HEIGHT_BOUND here.
107
  // It also could make sense in practice to do congruence for APPLY_UF, but
108
  // this is not done.
109
9850
  if (getQuantifiersEngine() && options::sygus())
110
  {
111
    quantifiers::TermDbSygus* tds =
112
1150
        getQuantifiersEngine()->getTermDatabaseSygus();
113
1150
    d_sygusExtension.reset(new SygusExtension(d_state, d_im, tds));
114
    // do congruence on evaluation functions
115
1150
    d_equalityEngine->addFunctionKind(kind::DT_SYGUS_EVAL);
116
  }
117
  // testers are not relevant for model building
118
9850
  d_valuation.setIrrelevantKind(APPLY_TESTER);
119
9850
}
120
121
2169476
TheoryDatatypes::EqcInfo* TheoryDatatypes::getOrMakeEqcInfo( TNode n, bool doMake ){
122
2169476
  if( !hasEqcInfo( n ) ){
123
477178
    if( doMake ){
124
      //add to labels
125
152928
      d_labels[ n ] = 0;
126
127
152928
      std::map< Node, EqcInfo* >::iterator eqc_i = d_eqc_info.find( n );
128
      EqcInfo* ei;
129
152928
      if( eqc_i != d_eqc_info.end() ){
130
126861
        ei = eqc_i->second;
131
      }else{
132
26067
        ei = new EqcInfo( getSatContext() );
133
26067
        d_eqc_info[n] = ei;
134
      }
135
152928
      if( n.getKind()==APPLY_CONSTRUCTOR ){
136
108455
        ei->d_constructor = n;
137
      }
138
139
      //add to selectors
140
152928
      d_selector_apps[n] = 0;
141
142
152928
      return ei;
143
    }else{
144
324250
      return NULL;
145
    }
146
  }else{
147
1692298
    std::map< Node, EqcInfo* >::iterator eqc_i = d_eqc_info.find( n );
148
1692298
    return (*eqc_i).second;
149
  }
150
}
151
152
573582
TNode TheoryDatatypes::getEqcConstructor( TNode r ) {
153
573582
  if( r.getKind()==APPLY_CONSTRUCTOR ){
154
357166
    return r;
155
  }else{
156
216416
    EqcInfo * ei = getOrMakeEqcInfo( r, false );
157
216416
    if( ei && !ei->d_constructor.get().isNull() ){
158
65430
      return ei->d_constructor.get();
159
    }else{
160
150986
      return r;
161
    }
162
  }
163
}
164
165
505584
bool TheoryDatatypes::preCheck(Effort level)
166
{
167
1011168
  Trace("datatypes-check") << "TheoryDatatypes::preCheck: " << level
168
505584
                           << std::endl;
169
505584
  d_im.process();
170
505584
  d_im.reset();
171
505584
  return false;
172
}
173
174
505584
void TheoryDatatypes::postCheck(Effort level)
175
{
176
1011168
  Trace("datatypes-check") << "TheoryDatatypes::postCheck: " << level
177
505584
                           << std::endl;
178
  // Apply any last pending inferences, which may occur if the last processed
179
  // fact was an internal one and triggered further internal inferences.
180
505584
  d_im.process();
181
505584
  if (level == EFFORT_LAST_CALL)
182
  {
183
5540
    Assert(d_sygusExtension != nullptr);
184
5540
    d_sygusExtension->check();
185
  }
186
1032166
  else if (level == EFFORT_FULL && !d_state.isInConflict()
187
531838
           && !d_im.hasSentLemma() && !d_valuation.needCheck())
188
  {
189
    //check for cycles
190
28111
    Assert(!d_im.hasPendingFact());
191
4
    do {
192
28115
      d_im.reset();
193
28115
      Trace("datatypes-proc") << "Check cycles..." << std::endl;
194
28115
      checkCycles();
195
28115
      Trace("datatypes-proc") << "...finish check cycles" << std::endl;
196
28115
      d_im.process();
197
28115
      if (d_state.isInConflict() || d_im.hasSentLemma())
198
      {
199
143
        return;
200
      }
201
27972
    } while (d_im.hasSentFact());
202
203
    //check for splits
204
27968
    Trace("datatypes-debug") << "Check for splits " << endl;
205
1484
    do {
206
29452
      d_im.reset();
207
58904
      std::map< TypeNode, Node > rec_singletons;
208
29452
      eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(d_equalityEngine);
209
1021380
      while( !eqcs_i.isFinished() ){
210
993221
        Node n = (*eqcs_i);
211
        //TODO : avoid irrelevant (pre-registered but not asserted) terms here?
212
993221
        TypeNode tn = n.getType();
213
497257
        if( tn.isDatatype() ){
214
171948
          Trace("datatypes-debug") << "Process equivalence class " << n << std::endl;
215
171948
          EqcInfo* eqc = getOrMakeEqcInfo( n );
216
          //if there are more than 1 possible constructors for eqc
217
171948
          if( !hasLabel( eqc, n ) ){
218
39948
            Trace("datatypes-debug") << "No constructor..." << std::endl;
219
78603
            TypeNode tt = tn;
220
39948
            const DType& dt = tt.getDType();
221
79896
            Trace("datatypes-debug")
222
79896
                << "Datatype " << dt.getName() << " is "
223
79896
                << dt.getCardinalityClass(tt) << " "
224
39948
                << dt.isRecursiveSingleton(tt) << std::endl;
225
39948
            bool continueProc = true;
226
39948
            if( dt.isRecursiveSingleton( tt ) ){
227
8
              Trace("datatypes-debug") << "Check recursive singleton..." << std::endl;
228
              //handle recursive singleton case
229
8
              std::map< TypeNode, Node >::iterator itrs = rec_singletons.find( tn );
230
8
              if( itrs!=rec_singletons.end() ){
231
8
                Node eq = n.eqNode( itrs->second );
232
4
                if( d_singleton_eq.find( eq )==d_singleton_eq.end() ){
233
4
                  d_singleton_eq[eq] = true;
234
                  // get assumptions
235
4
                  bool success = true;
236
8
                  std::vector< Node > assumptions;
237
                  //if there is at least one uninterpreted sort occurring within the datatype and the logic is not quantified, add lemmas ensuring cardinality is more than one,
238
                  //  do not infer the equality if at least one sort was processed.
239
                  //otherwise, if the logic is quantified, under the assumption that all uninterpreted sorts have cardinality one,
240
                  //  infer the equality.
241
4
                  for( unsigned i=0; i<dt.getNumRecursiveSingletonArgTypes( tt ); i++ ){
242
                    TypeNode type = dt.getRecursiveSingletonArgType(tt, i);
243
                    if( getQuantifiersEngine() ){
244
                      // under the assumption that the cardinality of this type is one
245
                      Node a = getSingletonLemma(type, true);
246
                      assumptions.push_back( a.negate() );
247
                    }else{
248
                      success = false;
249
                      // assert that the cardinality of this type is more than one
250
                      getSingletonLemma(type, false);
251
                    }
252
                  }
253
4
                  if( success ){
254
8
                    Node assumption = n.eqNode(itrs->second);
255
4
                    assumptions.push_back(assumption);
256
8
                    Node lemma = assumptions.size()==1 ? assumptions[0] : NodeManager::currentNM()->mkNode( OR, assumptions );
257
4
                    Trace("dt-singleton") << "*************Singleton equality lemma " << lemma << std::endl;
258
4
                    d_im.lemma(lemma, InferenceId::DATATYPES_REC_SINGLETON_EQ);
259
                  }
260
                }
261
              }else{
262
4
                rec_singletons[tn] = n;
263
              }
264
              //do splitting for quantified logics (incomplete anyways)
265
8
              continueProc = ( getQuantifiersEngine()!=NULL );
266
            }
267
39948
            if( continueProc ){
268
39948
              Trace("datatypes-debug") << "Get possible cons..." << std::endl;
269
              //all other cases
270
78603
              std::vector< bool > pcons;
271
39948
              getPossibleCons( eqc, n, pcons );
272
              //check if we do not need to resolve the constructor type for this equivalence class.
273
              // this is if there are no selectors for this equivalence class, and its possible values are infinite,
274
              //  then do not split.
275
39948
              int consIndex = -1;
276
39948
              int fconsIndex = -1;
277
39948
              bool needSplit = true;
278
145071
              for (size_t j = 0, psize = pcons.size(); j < psize; j++)
279
              {
280
105123
                if( pcons[j] ) {
281
103881
                  if( consIndex==-1 ){
282
39842
                    consIndex = j;
283
                  }
284
207762
                  Trace("datatypes-debug") << j << " compute finite..."
285
103881
                                           << std::endl;
286
                  // Notice that we split here on all datatypes except the
287
                  // truly infinite ones. It is possible to also not split
288
                  // on those that are interpreted-finite when finite model
289
                  // finding is disabled, but as a heuristic we choose to split
290
                  // on those too.
291
207762
                  bool ifin = dt[j].getCardinalityClass(tt)
292
103881
                              != CardinalityClass::INFINITE;
293
207762
                  Trace("datatypes-debug") << "...returned " << ifin
294
103881
                                           << std::endl;
295
103881
                  if (!ifin)
296
                  {
297
62881
                    if( !eqc || !eqc->d_selectors ){
298
57406
                      needSplit = false;
299
                    }
300
                  }else{
301
41000
                    if( fconsIndex==-1 ){
302
31202
                      fconsIndex = j;
303
                    }
304
                  }
305
                }
306
              }
307
              //if we want to force an assignment of constructors to all ground eqc
308
              //d_dtfCounter++;
309
39948
              if( !needSplit && options::dtForceAssignment() && d_dtfCounter%2==0 ){
310
                Trace("datatypes-force-assign") << "Force assignment for " << n << std::endl;
311
                needSplit = true;
312
                consIndex = fconsIndex!=-1 ? fconsIndex : consIndex;
313
              }
314
315
39948
              if( needSplit ) {
316
19149
                if( dt.getNumConstructors()==1 ){
317
                  //this may not be necessary?
318
                  //if only one constructor, then this term must be this constructor
319
35712
                  Node t = utils::mkTester(n, 0, dt);
320
17856
                  d_im.addPendingInference(
321
                      t, InferenceId::DATATYPES_SPLIT, d_true);
322
17856
                  Trace("datatypes-infer") << "DtInfer : 1-cons (full) : " << t << std::endl;
323
                }else{
324
1293
                  Assert(consIndex != -1 || dt.isSygus());
325
1293
                  if( options::dtBinarySplit() && consIndex!=-1 ){
326
                    Node test = utils::mkTester(n, consIndex, dt);
327
                    Trace("dt-split") << "*************Split for possible constructor " << dt[consIndex] << " for " << n << endl;
328
                    test = Rewriter::rewrite( test );
329
                    NodeBuilder nb(kind::OR);
330
                    nb << test << test.notNode();
331
                    Node lemma = nb;
332
                    d_im.lemma(lemma, InferenceId::DATATYPES_BINARY_SPLIT);
333
                    d_im.requirePhase(test, true);
334
                  }else{
335
1293
                    Trace("dt-split") << "*************Split for constructors on " << n <<  endl;
336
2586
                    Node lemma = utils::mkSplit(n, dt);
337
1293
                    Trace("dt-split-debug") << "Split lemma is : " << lemma << std::endl;
338
1293
                    d_im.sendDtLemma(lemma,
339
                                     InferenceId::DATATYPES_SPLIT,
340
                                     LemmaProperty::SEND_ATOMS);
341
                  }
342
1293
                  if( !options::dtBlastSplits() ){
343
1293
                    break;
344
                  }
345
                }
346
              }else{
347
20799
                Trace("dt-split-debug") << "Do not split constructor for " << n << " : " << n.getType() << " " << dt.getNumConstructors() << std::endl;
348
              }
349
            }
350
          }else{
351
132000
            Trace("datatypes-debug") << "Has constructor " << eqc->d_constructor.get() << std::endl;
352
          }
353
        }
354
495964
        ++eqcs_i;
355
      }
356
29452
      if (d_im.hasSentLemma())
357
      {
358
        // clear pending facts: we added a lemma, so internal inferences are
359
        // no longer necessary
360
1250
        d_im.clearPendingFacts();
361
      }
362
      else
363
      {
364
        // we did not add a lemma, process internal inferences. This loop
365
        // will repeat.
366
28202
        Trace("datatypes-debug") << "Flush pending facts..." << std::endl;
367
28202
        d_im.process();
368
      }
369
57829
    } while (!d_state.isInConflict() && !d_im.hasSentLemma()
370
56285
             && d_im.hasSentFact());
371
55936
    Trace("datatypes-debug")
372
55936
        << "Finished, conflict=" << d_state.isInConflict()
373
27968
        << ", lemmas=" << d_im.hasSentLemma() << std::endl;
374
27968
    if (!d_state.isInConflict())
375
    {
376
26893
      Trace("dt-model-debug") << std::endl;
377
26893
      printModelDebug("dt-model-debug");
378
    }
379
  }
380
381
505441
  Trace("datatypes-check") << "Finished check effort " << level << std::endl;
382
505441
  if( Debug.isOn("datatypes") || Debug.isOn("datatypes-split") ) {
383
    Notice() << "TheoryDatatypes::check(): done" << endl;
384
  }
385
}
386
387
11041
bool TheoryDatatypes::needsCheckLastEffort() {
388
11041
  return d_sygusExtension != nullptr;
389
}
390
391
1787110
void TheoryDatatypes::notifyFact(TNode atom,
392
                                 bool polarity,
393
                                 TNode fact,
394
                                 bool isInternal)
395
{
396
3574220
  Trace("datatypes-debug") << "TheoryDatatypes::assertFact : " << fact
397
1787110
                           << ", isInternal = " << isInternal << std::endl;
398
  // could be sygus-specific
399
1787110
  if (d_sygusExtension)
400
  {
401
1399051
    d_sygusExtension->assertFact(atom, polarity);
402
  }
403
  //add to tester if applicable
404
3574220
  Node t_arg;
405
1787110
  int tindex = utils::isTester(atom, t_arg);
406
1787110
  if (tindex >= 0)
407
  {
408
766326
    Trace("dt-tester") << "Assert tester : " << atom << " for " << t_arg << std::endl;
409
1532652
    Node rep = getRepresentative( t_arg );
410
766326
    EqcInfo* eqc = getOrMakeEqcInfo( rep, true );
411
    Node tst =
412
1532652
        isInternal ? (polarity ? Node(atom) : atom.notNode()) : Node(fact);
413
766326
    addTester(tindex, tst, eqc, rep, t_arg);
414
766326
    Trace("dt-tester") << "Done assert tester." << std::endl;
415
766326
    Trace("dt-tester") << "Done pending merges." << std::endl;
416
766326
    if (!d_state.isInConflict() && polarity)
417
    {
418
222769
      if (d_sygusExtension)
419
      {
420
170890
        Trace("dt-tester") << "Assert tester to sygus : " << atom << std::endl;
421
170890
        d_sygusExtension->assertTester(tindex, t_arg, atom);
422
170890
        Trace("dt-tester") << "Done assert tester to sygus." << std::endl;
423
      }
424
    }
425
  }else{
426
1020784
    Trace("dt-tester-debug") << "Assert (non-tester) : " << atom << std::endl;
427
  }
428
1787110
  Trace("datatypes-debug") << "TheoryDatatypes::assertFact : finished " << fact << std::endl;
429
  // now, flush pending facts if this wasn't an internal call
430
1787110
  if (!isInternal)
431
  {
432
1494765
    d_im.process();
433
  }
434
1787110
}
435
436
180174
void TheoryDatatypes::preRegisterTerm(TNode n)
437
{
438
360348
  Trace("datatypes-prereg")
439
180174
      << "TheoryDatatypes::preRegisterTerm() " << n << endl;
440
  // external selectors should be preprocessed away by now
441
180174
  Assert(n.getKind() != APPLY_SELECTOR);
442
  // must ensure the type is well founded and has no nested recursion if
443
  // the option dtNestedRec is not set to true.
444
360348
  TypeNode tn = n.getType();
445
180174
  if (tn.isDatatype())
446
  {
447
72395
    const DType& dt = tn.getDType();
448
72395
    Trace("dt-expand") << "Check properties of " << dt.getName() << std::endl;
449
72395
    if (!dt.isWellFounded())
450
    {
451
      std::stringstream ss;
452
      ss << "Cannot handle non-well-founded datatype " << dt.getName();
453
      throw LogicException(ss.str());
454
    }
455
72395
    Trace("dt-expand") << "...well-founded ok" << std::endl;
456
72395
    if (!options::dtNestedRec())
457
    {
458
72167
      if (dt.hasNestedRecursion())
459
      {
460
2
        std::stringstream ss;
461
1
        ss << "Cannot handle nested-recursive datatype " << dt.getName();
462
1
        throw LogicException(ss.str());
463
      }
464
72166
      Trace("dt-expand") << "...nested recursion ok" << std::endl;
465
    }
466
  }
467
180173
  collectTerms( n );
468
180173
  switch (n.getKind()) {
469
81528
  case kind::EQUAL:
470
  case kind::APPLY_TESTER:
471
    // add predicate trigger for testers and equalities
472
    // Get triggered for both equal and dis-equal
473
81528
    d_equalityEngine->addTriggerPredicate(n);
474
81528
    break;
475
98645
  default:
476
    // Function applications/predicates
477
98645
    d_equalityEngine->addTerm(n);
478
98645
    if (d_sygusExtension)
479
    {
480
21388
      d_sygusExtension->preRegisterTerm(n);
481
    }
482
98645
    break;
483
  }
484
180173
  d_im.process();
485
180173
}
486
487
83309
TrustNode TheoryDatatypes::ppRewrite(TNode in, std::vector<SkolemLemma>& lems)
488
{
489
83309
  Debug("tuprec") << "TheoryDatatypes::ppRewrite(" << in << ")" << endl;
490
  // first, see if we need to expand definitions
491
166618
  TrustNode texp = d_rewriter.expandDefinition(in);
492
83309
  if (!texp.isNull())
493
  {
494
4502
    return texp;
495
  }
496
78807
  if( in.getKind()==EQUAL ){
497
8284
    Node nn;
498
8284
    std::vector< Node > rew;
499
4188
    if (utils::checkClash(in[0], in[1], rew))
500
    {
501
      nn = NodeManager::currentNM()->mkConst(false);
502
    }
503
    else
504
    {
505
12492
      nn = rew.size()==0 ? d_true :
506
8304
                ( rew.size()==1 ? rew[0] : NodeManager::currentNM()->mkNode( kind::AND, rew ) );
507
    }
508
4188
    if (in != nn)
509
    {
510
92
      return TrustNode::mkTrustRewrite(in, nn, nullptr);
511
    }
512
  }
513
514
  // nothing to do
515
78715
  return TrustNode::null();
516
}
517
518
49051
TrustNode TheoryDatatypes::explain(TNode literal)
519
{
520
49051
  return d_im.explainLit(literal);
521
}
522
523
/** called when a new equivalance class is created */
524
310070
void TheoryDatatypes::eqNotifyNewClass(TNode t){
525
310070
  if( t.getKind()==APPLY_CONSTRUCTOR ){
526
108455
    getOrMakeEqcInfo( t, true );
527
  }
528
310070
}
529
530
/** called when two equivalance classes have merged */
531
1995943
void TheoryDatatypes::eqNotifyMerge(TNode t1, TNode t2)
532
{
533
1995943
  if( t1.getType().isDatatype() ){
534
950298
    Trace("datatypes-merge")
535
475149
        << "NotifyMerge : " << t1 << " " << t2 << std::endl;
536
475149
    merge(t1, t2);
537
  }
538
1995943
}
539
540
475149
void TheoryDatatypes::merge( Node t1, Node t2 ){
541
475149
  if (d_state.isInConflict())
542
  {
543
134998
    return;
544
  }
545
475006
  Trace("datatypes-merge") << "Merge " << t1 << " " << t2 << std::endl;
546
475006
  Assert(areEqual(t1, t2));
547
815300
  TNode trep1 = t1;
548
815300
  TNode trep2 = t2;
549
475006
  EqcInfo* eqc2 = getOrMakeEqcInfo(t2);
550
475006
  if (eqc2 == nullptr)
551
  {
552
133281
    return;
553
  }
554
341725
  bool checkInst = false;
555
341725
  if (!eqc2->d_constructor.get().isNull())
556
  {
557
111217
    trep2 = eqc2->d_constructor.get();
558
  }
559
341725
  EqcInfo* eqc1 = getOrMakeEqcInfo(t1);
560
341725
  if (eqc1)
561
  {
562
657166
    Trace("datatypes-debug")
563
328583
        << "  merge eqc info " << eqc2 << " into " << eqc1 << std::endl;
564
328583
    if (!eqc1->d_constructor.get().isNull())
565
    {
566
271833
      trep1 = eqc1->d_constructor.get();
567
    }
568
    // check for clash
569
656126
    TNode cons1 = eqc1->d_constructor.get();
570
656126
    TNode cons2 = eqc2->d_constructor.get();
571
    // if both have constructor, then either clash or unification
572
328583
    if (!cons1.isNull() && !cons2.isNull())
573
    {
574
129816
      Trace("datatypes-debug")
575
64908
          << "  constructors : " << cons1 << " " << cons2 << std::endl;
576
128781
      Node unifEq = cons1.eqNode(cons2);
577
128781
      std::vector<Node> rew;
578
64908
      if (utils::checkClash(cons1, cons2, rew))
579
      {
580
2070
        std::vector<Node> conf;
581
1035
        conf.push_back(unifEq);
582
2070
        Trace("dt-conflict")
583
1035
            << "CONFLICT: Clash conflict : " << conf << std::endl;
584
1035
        d_im.sendDtConflict(conf, InferenceId::DATATYPES_CLASH_CONFLICT);
585
1035
        return;
586
      }
587
      else
588
      {
589
63873
        Assert(areEqual(cons1, cons2));
590
        // do unification
591
186523
        for (size_t i = 0, nchild = cons1.getNumChildren(); i < nchild; i++)
592
        {
593
122650
          if (!areEqual(cons1[i], cons2[i]))
594
          {
595
69174
            Node eq = cons1[i].eqNode(cons2[i]);
596
34587
            d_im.addPendingInference(eq, InferenceId::DATATYPES_UNIF, unifEq);
597
69174
            Trace("datatypes-infer") << "DtInfer : cons-inj : " << eq << " by "
598
34587
                                     << unifEq << std::endl;
599
          }
600
        }
601
      }
602
    }
603
655096
    Trace("datatypes-debug") << "  instantiated : " << eqc1->d_inst << " "
604
327548
                             << eqc2->d_inst << std::endl;
605
327548
    eqc1->d_inst = eqc1->d_inst || eqc2->d_inst;
606
327548
    if (!cons2.isNull())
607
    {
608
102969
      if (cons1.isNull())
609
      {
610
78192
        Trace("datatypes-debug")
611
39096
            << "  must check if it is okay to set the constructor."
612
39096
            << std::endl;
613
39096
        checkInst = true;
614
39096
        addConstructor(eqc2->d_constructor.get(), eqc1, t1);
615
39096
        if (d_state.isInConflict())
616
        {
617
5
          return;
618
        }
619
      }
620
    }
621
  }
622
  else
623
  {
624
26284
    Trace("datatypes-debug")
625
13142
        << "  no eqc info for " << t1 << ", must create" << std::endl;
626
    // just copy the equivalence class information
627
13142
    eqc1 = getOrMakeEqcInfo(t1, true);
628
13142
    eqc1->d_inst.set(eqc2->d_inst);
629
13142
    eqc1->d_constructor.set(eqc2->d_constructor);
630
13142
    eqc1->d_selectors.set(eqc2->d_selectors);
631
  }
632
633
  // merge labels
634
340685
  NodeUIntMap::iterator lbl_i = d_labels.find(t2);
635
340685
  if (lbl_i != d_labels.end())
636
  {
637
681370
    Trace("datatypes-debug")
638
340685
        << "  merge labels from " << eqc2 << " " << t2 << std::endl;
639
340685
    size_t n_label = (*lbl_i).second;
640
588503
    for (size_t i = 0; i < n_label; i++)
641
    {
642
248209
      Assert(i < d_labels_data[t2].size());
643
496027
      Node t = d_labels_data[t2][i];
644
496027
      Node t_arg = d_labels_args[t2][i];
645
248209
      unsigned tindex = d_labels_tindex[t2][i];
646
248209
      addTester(tindex, t, eqc1, t1, t_arg);
647
248209
      if (d_state.isInConflict())
648
      {
649
391
        Trace("datatypes-debug") << "  conflict!" << std::endl;
650
391
        return;
651
      }
652
    }
653
  }
654
  // merge selectors
655
340294
  if (!eqc1->d_selectors && eqc2->d_selectors)
656
  {
657
51754
    eqc1->d_selectors = true;
658
51754
    checkInst = true;
659
  }
660
340294
  NodeUIntMap::iterator sel_i = d_selector_apps.find(t2);
661
340294
  if (sel_i != d_selector_apps.end())
662
  {
663
680588
    Trace("datatypes-debug")
664
340294
        << "  merge selectors from " << eqc2 << " " << t2 << std::endl;
665
340294
    size_t n_sel = (*sel_i).second;
666
766017
    for (size_t j = 0; j < n_sel; j++)
667
    {
668
425723
      addSelector(d_selector_apps_data[t2][j],
669
                  eqc1,
670
                  t1,
671
425723
                  eqc2->d_constructor.get().isNull());
672
    }
673
  }
674
340294
  if (checkInst)
675
  {
676
90845
    Trace("datatypes-debug") << "  checking instantiate" << std::endl;
677
90845
    instantiate(eqc1, t1);
678
  }
679
340294
  Trace("datatypes-debug") << "Finished Merge " << t1 << " " << t2 << std::endl;
680
}
681
682
26067
TheoryDatatypes::EqcInfo::EqcInfo(context::Context* c)
683
    : d_inst(c, false),
684
52134
      d_constructor(c, Node::null()),
685
78201
      d_selectors(c, false)
686
26067
{}
687
688
171948
bool TheoryDatatypes::hasLabel( EqcInfo* eqc, Node n ){
689
171948
  return ( eqc && !eqc->d_constructor.get().isNull() ) || !getLabel( n ).isNull();
690
}
691
692
671039
Node TheoryDatatypes::getLabel( Node n ) {
693
671039
  NodeUIntMap::iterator lbl_i = d_labels.find(n);
694
671039
  if( lbl_i != d_labels.end() ){
695
607987
    size_t n_lbl = (*lbl_i).second;
696
607987
    if( n_lbl>0 && d_labels_data[n][ n_lbl-1 ].getKind()!=kind::NOT ){
697
209800
      return d_labels_data[n][ n_lbl-1 ];
698
    }
699
  }
700
461239
  return Node::null();
701
}
702
703
1287234
int TheoryDatatypes::getLabelIndex( EqcInfo* eqc, Node n ){
704
1287234
  if( eqc && !eqc->d_constructor.get().isNull() ){
705
760368
    return utils::indexOf(eqc->d_constructor.get().getOperator());
706
  }else{
707
1053732
    Node lbl = getLabel( n );
708
526866
    if( lbl.isNull() ){
709
421291
      return -1;
710
    }else{
711
105575
      int tindex = utils::isTester(lbl);
712
211150
      Trace("datatypes-debug") << "Label of " << n << " is " << lbl
713
105575
                               << " with tindex " << tindex << std::endl;
714
105575
      Assert(tindex != -1);
715
105575
      return tindex;
716
    }
717
  }
718
}
719
720
8469
bool TheoryDatatypes::hasTester( Node n ) {
721
8469
  NodeUIntMap::iterator lbl_i = d_labels.find(n);
722
8469
  if( lbl_i != d_labels.end() ){
723
    return (*lbl_i).second>0;
724
  }else{
725
8469
    return false;
726
  }
727
}
728
729
78096
void TheoryDatatypes::getPossibleCons( EqcInfo* eqc, Node n, std::vector< bool >& pcons ){
730
156192
  TypeNode tn = n.getType();
731
78096
  const DType& dt = tn.getDType();
732
78096
  int lindex = getLabelIndex( eqc, n );
733
78096
  pcons.resize( dt.getNumConstructors(), lindex==-1 );
734
78096
  if( lindex!=-1 ){
735
    pcons[ lindex ] = true;
736
  }else{
737
78096
    NodeUIntMap::iterator lbl_i = d_labels.find(n);
738
78096
    if( lbl_i != d_labels.end() ){
739
46570
      size_t n_lbl = (*lbl_i).second;
740
229048
      for (size_t i = 0; i < n_lbl; i++)
741
      {
742
182478
        Assert(d_labels_data[n][i].getKind() == NOT);
743
182478
        unsigned tindex = d_labels_tindex[n][i];
744
182478
        pcons[ tindex ] = false;
745
      }
746
    }
747
  }
748
78096
}
749
750
118177
Node TheoryDatatypes::getTermSkolemFor( Node n ) {
751
118177
  if( n.getKind()==APPLY_CONSTRUCTOR ){
752
14419
    NodeMap::const_iterator it = d_term_sk.find( n );
753
14419
    if( it==d_term_sk.end() ){
754
1343
      NodeManager* nm = NodeManager::currentNM();
755
1343
      SkolemManager* sm = nm->getSkolemManager();
756
      //add purification unit lemma ( k = n )
757
2686
      Node k = sm->mkPurifySkolem(n, "kdt");
758
1343
      d_term_sk[n] = k;
759
2686
      Node eq = k.eqNode( n );
760
1343
      Trace("datatypes-infer") << "DtInfer : ref : " << eq << std::endl;
761
1343
      d_im.addPendingLemma(eq, InferenceId::DATATYPES_PURIFY);
762
1343
      return k;
763
    }else{
764
13076
      return (*it).second;
765
    }
766
  }else{
767
103758
    return n;
768
  }
769
}
770
771
1014535
void TheoryDatatypes::addTester(
772
    unsigned ttindex, Node t, EqcInfo* eqc, Node n, Node t_arg)
773
{
774
1014535
  Trace("datatypes-debug") << "Add tester : " << t << " to eqc(" << n << ")" << std::endl;
775
1014535
  Debug("datatypes-labels") << "Add tester " << t << " " << n << " " << eqc << std::endl;
776
1014535
  bool tpolarity = t.getKind()!=NOT;
777
1014535
  Assert((tpolarity ? t : t[0]).getKind() == APPLY_TESTER);
778
1301234
  Node j, jt;
779
1014535
  bool makeConflict = false;
780
1014535
  int prevTIndex = getLabelIndex(eqc, n);
781
1014535
  if (prevTIndex >= 0)
782
  {
783
671519
    unsigned ptu = static_cast<unsigned>(prevTIndex);
784
    //if we already know the constructor type, check whether it is in conflict or redundant
785
671519
    if ((ptu == ttindex) != tpolarity)
786
    {
787
1268
      if( !eqc->d_constructor.get().isNull() ){
788
        //conflict because equivalence class contains a constructor
789
2536
        std::vector<Node> conf;
790
1268
        conf.push_back(t);
791
1268
        conf.push_back(t_arg.eqNode(eqc->d_constructor.get()));
792
2536
        Trace("dt-conflict")
793
1268
            << "CONFLICT: Tester eq conflict " << conf << std::endl;
794
1268
        d_im.sendDtConflict(conf, InferenceId::DATATYPES_TESTER_CONFLICT);
795
1268
        return;
796
      }else{
797
        makeConflict = true;
798
        //conflict because the existing label is contradictory
799
        j = getLabel( n );
800
        jt = j;
801
      }
802
    }else{
803
670251
      return;
804
    }
805
  }else{
806
    //otherwise, scan list of labels
807
343016
    NodeUIntMap::iterator lbl_i = d_labels.find(n);
808
343016
    Assert(lbl_i != d_labels.end());
809
343016
    size_t n_lbl = (*lbl_i).second;
810
629715
    std::map< int, bool > neg_testers;
811
1267852
    for (size_t i = 0; i < n_lbl; i++)
812
    {
813
943113
      Assert(d_labels_data[n][i].getKind() == NOT);
814
943113
      unsigned jtindex = d_labels_tindex[n][i];
815
943113
      if( jtindex==ttindex ){
816
18277
        if( tpolarity ){  //we are in conflict
817
108
          j = d_labels_data[n][i];
818
108
          jt = j[0];
819
108
          makeConflict = true;
820
108
          break;
821
        }else{            //it is redundant
822
18169
          return;
823
        }
824
      }else{
825
924836
        neg_testers[jtindex] = true;
826
      }
827
    }
828
324847
    if( !makeConflict ){
829
324739
      Debug("datatypes-labels") << "Add to labels " << t << std::endl;
830
324739
      d_labels[n] = n_lbl + 1;
831
324739
      if (n_lbl < d_labels_data[n].size())
832
      {
833
        // reuse spot in the vector
834
313764
        d_labels_data[n][n_lbl] = t;
835
313764
        d_labels_args[n][n_lbl] = t_arg;
836
313764
        d_labels_tindex[n][n_lbl] = ttindex;
837
      }else{
838
10975
        d_labels_data[n].push_back(t);
839
10975
        d_labels_args[n].push_back(t_arg);
840
10975
        d_labels_tindex[n].push_back(ttindex);
841
      }
842
324739
      n_lbl++;
843
844
324739
      const DType& dt = t_arg.getType().getDType();
845
324739
      Debug("datatypes-labels") << "Labels at " << n_lbl << " / " << dt.getNumConstructors() << std::endl;
846
324739
      if( tpolarity ){
847
103758
        instantiate(eqc, n);
848
        // We could propagate is-C1(x) => not is-C2(x) here for all other
849
        // constructors, but empirically this hurts performance.
850
      }else{
851
        //check if we have reached the maximum number of testers
852
        // in this case, add the positive tester
853
220981
        if (n_lbl == dt.getNumConstructors() - 1)
854
        {
855
76296
          std::vector< bool > pcons;
856
38148
          getPossibleCons( eqc, n, pcons );
857
38148
          int testerIndex = -1;
858
122325
          for( unsigned i=0; i<pcons.size(); i++ ) {
859
122325
            if( pcons[i] ){
860
38148
              testerIndex = i;
861
38148
              break;
862
            }
863
          }
864
38148
          Assert(testerIndex != -1);
865
          //we must explain why each term in the set of testers for this equivalence class is equal
866
76296
          std::vector< Node > eq_terms;
867
76296
          NodeBuilder nb(kind::AND);
868
219384
          for (unsigned i = 0; i < n_lbl; i++)
869
          {
870
362472
            Node ti = d_labels_data[n][i];
871
181236
            nb << ti;
872
181236
            Assert(ti.getKind() == NOT);
873
362472
            Node t_arg2 = d_labels_args[n][i];
874
181236
            if( std::find( eq_terms.begin(), eq_terms.end(), t_arg2 )==eq_terms.end() ){
875
43161
              eq_terms.push_back( t_arg2 );
876
43161
              if( t_arg2!=t_arg ){
877
5013
                nb << t_arg2.eqNode( t_arg );
878
              }
879
            }
880
          }
881
          Node t_concl = testerIndex == -1
882
                             ? NodeManager::currentNM()->mkConst(false)
883
76296
                             : utils::mkTester(t_arg, testerIndex, dt);
884
76296
          Node t_concl_exp = ( nb.getNumChildren() == 1 ) ? nb.getChild( 0 ) : nb;
885
38148
          d_im.addPendingInference(
886
              t_concl, InferenceId::DATATYPES_LABEL_EXH, t_concl_exp);
887
38148
          Trace("datatypes-infer") << "DtInfer : label : " << t_concl << " by " << t_concl_exp << std::endl;
888
38148
          return;
889
        }
890
      }
891
    }
892
  }
893
286699
  if( makeConflict ){
894
108
    Debug("datatypes-labels") << "Explain " << j << " " << t << std::endl;
895
216
    std::vector<Node> conf;
896
108
    conf.push_back(j);
897
108
    conf.push_back(t);
898
108
    conf.push_back(jt[0].eqNode(t_arg));
899
108
    Trace("dt-conflict") << "CONFLICT: Tester conflict : " << conf << std::endl;
900
108
    d_im.sendDtConflict(conf, InferenceId::DATATYPES_TESTER_MERGE_CONFLICT);
901
  }
902
}
903
904
449069
void TheoryDatatypes::addSelector( Node s, EqcInfo* eqc, Node n, bool assertFacts ) {
905
449069
  Trace("dt-collapse-sel") << "Add selector : " << s << " to eqc(" << n << ")" << std::endl;
906
  //check to see if it is redundant
907
449069
  NodeUIntMap::iterator sel_i = d_selector_apps.find(n);
908
449069
  Assert(sel_i != d_selector_apps.end());
909
449069
  if( sel_i != d_selector_apps.end() ){
910
449069
    size_t n_sel = (*sel_i).second;
911
812419
    for (size_t j = 0; j < n_sel; j++)
912
    {
913
1004424
      Node ss = d_selector_apps_data[n][j];
914
641074
      if( s.getOperator()==ss.getOperator() && ( s.getKind()!=DT_HEIGHT_BOUND || s[1]==ss[1] ) ){
915
277724
        Trace("dt-collapse-sel") << "...redundant." << std::endl;
916
277724
        return;
917
      }
918
    }
919
    //add it to the vector
920
    //sel->push_back( s );
921
171345
    d_selector_apps[n] = n_sel + 1;
922
171345
    if (n_sel < d_selector_apps_data[n].size())
923
    {
924
154048
      d_selector_apps_data[n][n_sel] = s;
925
    }else{
926
17297
      d_selector_apps_data[n].push_back( s );
927
    }
928
929
171345
    eqc->d_selectors = true;
930
  }
931
171345
  if( assertFacts && !eqc->d_constructor.get().isNull() ){
932
    //conclude the collapsed merge
933
128205
    collapseSelector( s, eqc->d_constructor.get() );
934
  }
935
}
936
937
39096
void TheoryDatatypes::addConstructor( Node c, EqcInfo* eqc, Node n ){
938
39096
  Trace("datatypes-debug") << "Add constructor : " << c << " to eqc(" << n << ")" << std::endl;
939
39096
  Assert(eqc->d_constructor.get().isNull());
940
  //check labels
941
39096
  NodeUIntMap::iterator lbl_i = d_labels.find(n);
942
39096
  if( lbl_i != d_labels.end() ){
943
39096
    size_t constructorIndex = utils::indexOf(c.getOperator());
944
39096
    size_t n_lbl = (*lbl_i).second;
945
171046
    for (size_t i = 0; i < n_lbl; i++)
946
    {
947
263905
      Node t = d_labels_data[n][i];
948
131955
      if (d_labels_data[n][i].getKind() == NOT)
949
      {
950
95065
        unsigned tindex = d_labels_tindex[n][i];
951
95065
        if (tindex == constructorIndex)
952
        {
953
10
          std::vector<Node> conf;
954
5
          conf.push_back(t);
955
5
          conf.push_back(t[0][0].eqNode(c));
956
10
          Trace("dt-conflict")
957
5
              << "CONFLICT: Tester merge eq conflict : " << conf << std::endl;
958
5
          d_im.sendDtConflict(conf, InferenceId::DATATYPES_TESTER_CONFLICT);
959
5
          return;
960
        }
961
      }
962
    }
963
  }
964
  //check selectors
965
39091
  NodeUIntMap::iterator sel_i = d_selector_apps.find(n);
966
39091
  if( sel_i != d_selector_apps.end() ){
967
39091
    size_t n_sel = (*sel_i).second;
968
147526
    for (size_t j = 0; j < n_sel; j++)
969
    {
970
216870
      Node s = d_selector_apps_data[n][j];
971
      //collapse the selector
972
108435
      collapseSelector( s, c );
973
    }
974
  }
975
39091
  eqc->d_constructor.set( c );
976
}
977
978
236640
void TheoryDatatypes::collapseSelector( Node s, Node c ) {
979
236640
  Assert(c.getKind() == APPLY_CONSTRUCTOR);
980
236640
  Trace("dt-collapse-sel") << "collapse selector : " << s << " " << c << std::endl;
981
473280
  Node r;
982
236640
  bool wrong = false;
983
473280
  Node eq_exp = s[0].eqNode(c);
984
236640
  if( s.getKind()==kind::APPLY_SELECTOR_TOTAL ){
985
354366
    Node selector = s.getOperator();
986
177183
    size_t constructorIndex = utils::indexOf(c.getOperator());
987
177183
    const DType& dt = utils::datatypeOf(selector);
988
177183
    const DTypeConstructor& dtc = dt[constructorIndex];
989
177183
    int selectorIndex = dtc.getSelectorIndexInternal(selector);
990
177183
    wrong = selectorIndex<0;
991
177183
    r = NodeManager::currentNM()->mkNode( kind::APPLY_SELECTOR_TOTAL, s.getOperator(), c );
992
  }
993
236640
  if( !r.isNull() ){
994
354366
    Node rrs;
995
177183
    if (wrong)
996
    {
997
      // Must use make ground term here instead of the rewriter, since we
998
      // do not want to introduce arbitrary values. This is important so that
999
      // we avoid constants for types that are not "closed enumerable", e.g.
1000
      // uninterpreted sorts and arrays, where the solver does not fully
1001
      // handle values of the sort. The call to mkGroundTerm does not introduce
1002
      // values for these sorts.
1003
81935
      rrs = r.getType().mkGroundTerm();
1004
163870
      Trace("datatypes-wrong-sel")
1005
81935
          << "Bad apply " << r << " term = " << rrs
1006
81935
          << ", value = " << r.getType().mkGroundValue() << std::endl;
1007
    }
1008
    else
1009
    {
1010
95248
      rrs = Rewriter::rewrite(r);
1011
    }
1012
177183
    if (s != rrs)
1013
    {
1014
226716
      Node eq = s.eqNode(rrs);
1015
      // Since collapsing selectors may generate new terms, we must send
1016
      // this out as a lemma if it is of an external type, or otherwise we
1017
      // may ask for the equality status of terms that only datatypes knows
1018
      // about, see issue #5344.
1019
113358
      bool forceLemma = !s.getType().isDatatype();
1020
113358
      Trace("datatypes-infer") << "DtInfer : collapse sel";
1021
113358
      Trace("datatypes-infer") << " : " << eq << " by " << eq_exp << std::endl;
1022
113358
      d_im.addPendingInference(
1023
          eq, InferenceId::DATATYPES_COLLAPSE_SEL, eq_exp, forceLemma);
1024
    }
1025
  }
1026
236640
}
1027
1028
133463
EqualityStatus TheoryDatatypes::getEqualityStatus(TNode a, TNode b){
1029
133463
  Assert(d_equalityEngine->hasTerm(a) && d_equalityEngine->hasTerm(b));
1030
133463
  if (d_equalityEngine->areEqual(a, b))
1031
  {
1032
    // The terms are implied to be equal
1033
4783
    return EQUALITY_TRUE;
1034
  }
1035
128680
  if (d_equalityEngine->areDisequal(a, b, false))
1036
  {
1037
    // The terms are implied to be dis-equal
1038
    return EQUALITY_FALSE;
1039
  }
1040
128680
  return EQUALITY_FALSE_IN_MODEL;
1041
}
1042
1043
88320
void TheoryDatatypes::addCarePairs(TNodeTrie* t1,
1044
                                   TNodeTrie* t2,
1045
                                   unsigned arity,
1046
                                   unsigned depth,
1047
                                   unsigned& n_pairs)
1048
{
1049
88320
  if( depth==arity ){
1050
10489
    if( t2!=NULL ){
1051
20978
      Node f1 = t1->getData();
1052
20978
      Node f2 = t2->getData();
1053
10489
      if( !areEqual( f1, f2 ) ){
1054
10489
        Trace("dt-cg") << "Check " << f1 << " and " << f2 << std::endl;
1055
20978
        vector< pair<TNode, TNode> > currentPairs;
1056
31625
        for (unsigned k = 0; k < f1.getNumChildren(); ++ k) {
1057
42272
          TNode x = f1[k];
1058
42272
          TNode y = f2[k];
1059
21136
          Assert(d_equalityEngine->hasTerm(x));
1060
21136
          Assert(d_equalityEngine->hasTerm(y));
1061
21136
          Assert(!areDisequal(x, y));
1062
21136
          Assert(!areCareDisequal(x, y));
1063
21136
          if (!d_equalityEngine->areEqual(x, y))
1064
          {
1065
11898
            Trace("dt-cg") << "Arg #" << k << " is " << x << " " << y << std::endl;
1066
35694
            if (d_equalityEngine->isTriggerTerm(x, THEORY_DATATYPES)
1067
35694
                && d_equalityEngine->isTriggerTerm(y, THEORY_DATATYPES))
1068
            {
1069
2012
              TNode x_shared = d_equalityEngine->getTriggerTermRepresentative(
1070
4024
                  x, THEORY_DATATYPES);
1071
2012
              TNode y_shared = d_equalityEngine->getTriggerTermRepresentative(
1072
4024
                  y, THEORY_DATATYPES);
1073
2012
              currentPairs.push_back(make_pair(x_shared, y_shared));
1074
            }
1075
          }
1076
        }
1077
12501
        for (unsigned c = 0; c < currentPairs.size(); ++ c) {
1078
2012
          Trace("dt-cg-pair") << "Pair : " << currentPairs[c].first << " " << currentPairs[c].second << std::endl;
1079
2012
          addCarePair(currentPairs[c].first, currentPairs[c].second);
1080
2012
          n_pairs++;
1081
        }
1082
      }
1083
    }
1084
  }else{
1085
77831
    if( t2==NULL ){
1086
69120
      if( depth<(arity-1) ){
1087
        //add care pairs internal to each child
1088
30851
        for (std::pair<const TNode, TNodeTrie>& tt : t1->d_data)
1089
        {
1090
18597
          addCarePairs(&tt.second, nullptr, arity, depth + 1, n_pairs);
1091
        }
1092
      }
1093
      //add care pairs based on each pair of non-disequal arguments
1094
185756
      for (std::map<TNode, TNodeTrie>::iterator it = t1->d_data.begin();
1095
185756
           it != t1->d_data.end();
1096
           ++it)
1097
      {
1098
116636
        std::map<TNode, TNodeTrie>::iterator it2 = it;
1099
116636
        ++it2;
1100
357090
        for( ; it2 != t1->d_data.end(); ++it2 ){
1101
120227
          if (!d_equalityEngine->areDisequal(it->first, it2->first, false))
1102
          {
1103
53968
            if( !areCareDisequal(it->first, it2->first) ){
1104
11948
              addCarePairs( &it->second, &it2->second, arity, depth+1, n_pairs );
1105
            }
1106
          }
1107
        }
1108
      }
1109
    }else{
1110
      //add care pairs based on product of indices, non-disequal arguments
1111
27307
      for (std::pair<const TNode, TNodeTrie>& tt1 : t1->d_data)
1112
      {
1113
43852
        for (std::pair<const TNode, TNodeTrie>& tt2 : t2->d_data)
1114
        {
1115
25256
          if (!d_equalityEngine->areDisequal(tt1.first, tt2.first, false))
1116
          {
1117
16795
            if (!areCareDisequal(tt1.first, tt2.first))
1118
            {
1119
7252
              addCarePairs(&tt1.second, &tt2.second, arity, depth + 1, n_pairs);
1120
            }
1121
          }
1122
        }
1123
      }
1124
    }
1125
  }
1126
88320
}
1127
1128
11788
void TheoryDatatypes::computeCareGraph(){
1129
11788
  unsigned n_pairs = 0;
1130
11788
  Trace("dt-cg-summary") << "Compute graph for dt..." << d_functionTerms.size() << " " << d_sharedTerms.size() << std::endl;
1131
11788
  Trace("dt-cg") << "Build indices..." << std::endl;
1132
23576
  std::map<TypeNode, std::map<Node, TNodeTrie> > index;
1133
23576
  std::map< Node, unsigned > arity;
1134
  //populate indices
1135
11788
  unsigned functionTerms = d_functionTerms.size();
1136
251017
  for( unsigned i=0; i<functionTerms; i++ ){
1137
478458
    TNode f1 = d_functionTerms[i];
1138
239229
    Assert(d_equalityEngine->hasTerm(f1));
1139
239229
    Trace("dt-cg-debug") << "...build for " << f1 << std::endl;
1140
    //break into index based on operator, and type of first argument (since some operators are parametric)
1141
478458
    Node op = f1.getOperator();
1142
478458
    TypeNode tn = f1[0].getType();
1143
478458
    std::vector< TNode > reps;
1144
239229
    bool has_trigger_arg = false;
1145
516999
    for( unsigned j=0; j<f1.getNumChildren(); j++ ){
1146
277770
      reps.push_back(d_equalityEngine->getRepresentative(f1[j]));
1147
277770
      if (d_equalityEngine->isTriggerTerm(f1[j], THEORY_DATATYPES))
1148
      {
1149
230789
        has_trigger_arg = true;
1150
      }
1151
    }
1152
    //only may contribute to care pairs if has at least one trigger argument
1153
239229
    if( has_trigger_arg ){
1154
201442
      index[tn][op].addTerm( f1, reps );
1155
201442
      arity[op] = reps.size();
1156
    }
1157
  }
1158
  //for each index
1159
29578
  for (std::pair<const TypeNode, std::map<Node, TNodeTrie> >& tt : index)
1160
  {
1161
68313
    for (std::pair<const Node, TNodeTrie>& t : tt.second)
1162
    {
1163
101046
      Trace("dt-cg") << "Process index " << tt.first << ", " << t.first << "..."
1164
50523
                     << std::endl;
1165
50523
      addCarePairs(&t.second, nullptr, arity[t.first], 0, n_pairs);
1166
    }
1167
  }
1168
11788
  Trace("dt-cg-summary") << "...done, # pairs = " << n_pairs << std::endl;
1169
11788
}
1170
1171
9867
bool TheoryDatatypes::collectModelValues(TheoryModel* m,
1172
                                         const std::set<Node>& termSet)
1173
{
1174
19734
  Trace("dt-cmi") << "Datatypes : Collect model values "
1175
9867
                  << d_equalityEngine->consistent() << std::endl;
1176
9867
  Trace("dt-model") << std::endl;
1177
9867
  printModelDebug( "dt-model" );
1178
9867
  Trace("dt-model") << std::endl;
1179
1180
  //get all constructors
1181
9867
  eq::EqClassesIterator eqccs_i = eq::EqClassesIterator(d_equalityEngine);
1182
19734
  std::vector< Node > cons;
1183
19734
  std::vector< Node > nodes;
1184
19734
  std::map< Node, Node > eqc_cons;
1185
529703
  while( !eqccs_i.isFinished() ){
1186
519836
    Node eqc = (*eqccs_i);
1187
    //for all equivalence classes that are datatypes
1188
    //if( termSet.find( eqc )==termSet.end() ){
1189
    //  Trace("dt-cmi-debug") << "Irrelevant eqc : " << eqc << std::endl;
1190
    //}
1191
259918
    if( eqc.getType().isDatatype() ){
1192
53112
      EqcInfo* ei = getOrMakeEqcInfo( eqc );
1193
53112
      if( ei && !ei->d_constructor.get().isNull() ){
1194
89286
        Node c = ei->d_constructor.get();
1195
44643
        cons.push_back( c );
1196
44643
        eqc_cons[ eqc ] = c;
1197
      }else{
1198
        //if eqc contains a symbol known to datatypes (a selector), then we must assign
1199
        //should assign constructors to EQC if they have a selector or a tester
1200
8469
        bool shouldConsider = ( ei && ei->d_selectors ) || hasTester( eqc );
1201
8469
        if( shouldConsider ){
1202
          nodes.push_back( eqc );
1203
        }
1204
      }
1205
    }
1206
    //}
1207
259918
    ++eqccs_i;
1208
  }
1209
1210
  //unsigned orig_size = nodes.size();
1211
19734
  std::map< TypeNode, int > typ_enum_map;
1212
19734
  std::vector< TypeEnumerator > typ_enum;
1213
9867
  unsigned index = 0;
1214
9867
  while( index<nodes.size() ){
1215
    Node eqc = nodes[index];
1216
    Node neqc;
1217
    bool addCons = false;
1218
    TypeNode tt = eqc.getType();
1219
    const DType& dt = tt.getDType();
1220
    if (!d_equalityEngine->hasTerm(eqc))
1221
    {
1222
      Assert(false);
1223
    }else{
1224
      Trace("dt-cmi") << "NOTICE : Datatypes: no constructor in equivalence class " << eqc << std::endl;
1225
      Trace("dt-cmi") << "   Type : " << eqc.getType() << std::endl;
1226
      EqcInfo* ei = getOrMakeEqcInfo( eqc );
1227
      std::vector< bool > pcons;
1228
      getPossibleCons( ei, eqc, pcons );
1229
      Trace("dt-cmi") << "Possible constructors : ";
1230
      for( unsigned i=0; i<pcons.size(); i++ ){
1231
        Trace("dt-cmi") << pcons[i] << " ";
1232
      }
1233
      Trace("dt-cmi") << std::endl;
1234
      for( unsigned r=0; r<2; r++ ){
1235
        if( neqc.isNull() ){
1236
          for( unsigned i=0; i<pcons.size(); i++ ){
1237
            // must try the infinite ones first
1238
            bool cfinite =
1239
                d_state.isFiniteType(dt[i].getSpecializedConstructorType(tt));
1240
            if( pcons[i] && (r==1)==cfinite ){
1241
              neqc = utils::getInstCons(eqc, dt, i);
1242
              break;
1243
            }
1244
          }
1245
        }
1246
      }
1247
      addCons = true;
1248
    }
1249
    if( !neqc.isNull() ){
1250
      Trace("dt-cmi") << "Assign : " << neqc << std::endl;
1251
      if (!m->assertEquality(eqc, neqc, true))
1252
      {
1253
        return false;
1254
      }
1255
      eqc_cons[ eqc ] = neqc;
1256
    }
1257
    if( addCons ){
1258
      cons.push_back( neqc );
1259
    }
1260
    ++index;
1261
  }
1262
1263
54526
  for( std::map< Node, Node >::iterator it = eqc_cons.begin(); it != eqc_cons.end(); ++it ){
1264
89318
    Node eqc = it->first;
1265
44659
    if( eqc.getType().isCodatatype() ){
1266
      //until models are implemented for codatatypes
1267
      //throw Exception("Models for codatatypes are not supported in this version.");
1268
      //must proactive expand to avoid looping behavior in model builder
1269
75
      if( !it->second.isNull() ){
1270
124
        std::map< Node, int > vmap;
1271
124
        Node v = getCodatatypesValue( it->first, eqc_cons, vmap, 0 );
1272
62
        Trace("dt-cmi") << "  EQC(" << it->first << "), constructor is " << it->second << ", value is " << v << ", const = " << v.isConst() << std::endl;
1273
62
        if (!m->assertEquality(eqc, v, true))
1274
        {
1275
          return false;
1276
        }
1277
62
        m->assertSkeleton(v);
1278
      }
1279
    }else{
1280
44584
      Trace("dt-cmi") << "Datatypes : assert representative " << it->second << " for " << it->first << std::endl;
1281
44584
      m->assertSkeleton(it->second);
1282
    }
1283
  }
1284
9867
  return true;
1285
}
1286
1287
1288
199
Node TheoryDatatypes::getCodatatypesValue( Node n, std::map< Node, Node >& eqc_cons, std::map< Node, int >& vmap, int depth ){
1289
199
  std::map< Node, int >::iterator itv = vmap.find( n );
1290
199
  if( itv!=vmap.end() ){
1291
6
    int debruijn = depth - 1 - itv->second;
1292
    return NodeManager::currentNM()->mkConst(
1293
6
        UninterpretedConstant(n.getType(), debruijn));
1294
193
  }else if( n.getType().isDatatype() ){
1295
173
    Node nc = eqc_cons[n];
1296
139
    if( !nc.isNull() ){
1297
105
      vmap[n] = depth;
1298
105
      Trace("dt-cmi-cdt-debug") << "    map " << n << " -> " << depth << std::endl;
1299
105
      Assert(nc.getKind() == APPLY_CONSTRUCTOR);
1300
210
      std::vector< Node > children;
1301
105
      children.push_back( nc.getOperator() );
1302
242
      for( unsigned i=0; i<nc.getNumChildren(); i++ ){
1303
274
        Node r = getRepresentative( nc[i] );
1304
274
        Node rv = getCodatatypesValue( r, eqc_cons, vmap, depth+1 );
1305
137
        children.push_back( rv );
1306
      }
1307
105
      vmap.erase( n );
1308
105
      return NodeManager::currentNM()->mkNode( APPLY_CONSTRUCTOR, children );
1309
    }
1310
  }
1311
88
  return n;
1312
}
1313
1314
Node TheoryDatatypes::getSingletonLemma( TypeNode tn, bool pol ) {
1315
  NodeManager* nm = NodeManager::currentNM();
1316
  SkolemManager* sm = nm->getSkolemManager();
1317
  int index = pol ? 0 : 1;
1318
  std::map< TypeNode, Node >::iterator it = d_singleton_lemma[index].find( tn );
1319
  if( it==d_singleton_lemma[index].end() ){
1320
    Node a;
1321
    if( pol ){
1322
      Node v1 = nm->mkBoundVar(tn);
1323
      Node v2 = nm->mkBoundVar(tn);
1324
      a = nm->mkNode(FORALL, nm->mkNode(BOUND_VAR_LIST, v1, v2), v1.eqNode(v2));
1325
    }else{
1326
      Node v1 = sm->mkDummySkolem("k1", tn);
1327
      Node v2 = sm->mkDummySkolem("k2", tn);
1328
      a = v1.eqNode( v2 ).negate();
1329
      //send out immediately as lemma
1330
      d_im.lemma(a, InferenceId::DATATYPES_REC_SINGLETON_FORCE_DEQ);
1331
      Trace("dt-singleton") << "******** assert " << a << " to avoid singleton cardinality for type " << tn << std::endl;
1332
    }
1333
    d_singleton_lemma[index][tn] = a;
1334
    return a;
1335
  }else{
1336
    return it->second;
1337
  }
1338
}
1339
1340
298350
void TheoryDatatypes::collectTerms( Node n ) {
1341
298350
  if (d_collectTermsCache.find(n) != d_collectTermsCache.end())
1342
  {
1343
    // already processed
1344
39229
    return;
1345
  }
1346
259121
  d_collectTermsCache[n] = true;
1347
259121
  Kind nk = n.getKind();
1348
259121
  if (nk == APPLY_CONSTRUCTOR)
1349
  {
1350
101985
    Debug("datatypes") << "  Found constructor " << n << endl;
1351
101985
    if (n.getNumChildren() > 0)
1352
    {
1353
74269
      d_functionTerms.push_back(n);
1354
    }
1355
101985
    return;
1356
  }
1357
157136
  if (nk == APPLY_SELECTOR_TOTAL || nk == DT_SIZE || nk == DT_HEIGHT_BOUND)
1358
  {
1359
23346
    d_functionTerms.push_back(n);
1360
    // we must also record which selectors exist
1361
23346
    Trace("dt-collapse-sel") << "  Found selector " << n << endl;
1362
46692
    Node rep = getRepresentative(n[0]);
1363
    // record it in the selectors
1364
23346
    EqcInfo* eqc = getOrMakeEqcInfo(rep, true);
1365
    // add it to the eqc info
1366
23346
    addSelector(n, eqc, rep);
1367
  }
1368
1369
  // now, do user-context-dependent lemmas
1370
157136
  if (nk != DT_SIZE && nk != DT_HEIGHT_BOUND)
1371
  {
1372
    // if not one of these kinds, there are no lemmas
1373
152652
    return;
1374
  }
1375
4484
  if (d_collectTermsCacheU.find(n) != d_collectTermsCacheU.end())
1376
  {
1377
2158
    return;
1378
  }
1379
2326
  d_collectTermsCacheU[n] = true;
1380
1381
2326
  NodeManager* nm = NodeManager::currentNM();
1382
1383
2326
  if (nk == DT_SIZE)
1384
  {
1385
4652
    Node lem = nm->mkNode(LEQ, d_zero, n);
1386
4652
    Trace("datatypes-infer")
1387
2326
        << "DtInfer : size geq zero : " << lem << std::endl;
1388
2326
    d_im.addPendingLemma(lem, InferenceId::DATATYPES_SIZE_POS);
1389
  }
1390
  else if (nk == DT_HEIGHT_BOUND && n[1].getConst<Rational>().isZero())
1391
  {
1392
    std::vector<Node> children;
1393
    const DType& dt = n[0].getType().getDType();
1394
    for (unsigned i = 0, ncons = dt.getNumConstructors(); i < ncons; i++)
1395
    {
1396
      if (utils::isNullaryConstructor(dt[i]))
1397
      {
1398
        Node test = utils::mkTester(n[0], i, dt);
1399
        children.push_back(test);
1400
      }
1401
    }
1402
    Node lem;
1403
    if (children.empty())
1404
    {
1405
      lem = n.negate();
1406
    }
1407
    else
1408
    {
1409
      lem = n.eqNode(children.size() == 1 ? children[0]
1410
                                          : nm->mkNode(OR, children));
1411
    }
1412
    Trace("datatypes-infer") << "DtInfer : zero height : " << lem << std::endl;
1413
    d_im.addPendingLemma(lem, InferenceId::DATATYPES_HEIGHT_ZERO);
1414
  }
1415
}
1416
1417
128711
Node TheoryDatatypes::getInstantiateCons(Node n, const DType& dt, int index)
1418
{
1419
128711
  if( n.getKind()==APPLY_CONSTRUCTOR && n.getNumChildren()==0 ){
1420
10534
    return n;
1421
  }
1422
  //add constructor to equivalence class
1423
236354
  Node k = getTermSkolemFor( n );
1424
236354
  Node n_ic = utils::getInstCons(k, dt, index);
1425
118177
  n_ic = Rewriter::rewrite( n_ic );
1426
  // it may be a new term, so we collect terms and add it to the equality engine
1427
118177
  collectTerms( n_ic );
1428
118177
  d_equalityEngine->addTerm(n_ic);
1429
118177
  Debug("dt-enum") << "Made instantiate cons " << n_ic << std::endl;
1430
118177
  return n_ic;
1431
}
1432
1433
194603
bool TheoryDatatypes::instantiate(EqcInfo* eqc, Node n)
1434
{
1435
194603
  Trace("datatypes-debug") << "Instantiate: " << n << std::endl;
1436
  //add constructor to equivalence class if not done so already
1437
194603
  int index = getLabelIndex( eqc, n );
1438
194603
  if (index == -1 || eqc->d_inst)
1439
  {
1440
65892
    return false;
1441
  }
1442
257422
  Node exp;
1443
257422
  Node tt;
1444
128711
  if (!eqc->d_constructor.get().isNull())
1445
  {
1446
24953
    exp = d_true;
1447
24953
    tt = eqc->d_constructor;
1448
  }
1449
  else
1450
  {
1451
103758
    exp = getLabel(n);
1452
103758
    tt = exp[0];
1453
  }
1454
257422
  TypeNode ttn = tt.getType();
1455
128711
  const DType& dt = ttn.getDType();
1456
  // instantiate this equivalence class
1457
128711
  eqc->d_inst = true;
1458
257422
  Node tt_cons = getInstantiateCons(tt, dt, index);
1459
257422
  Node eq;
1460
128711
  if (tt == tt_cons)
1461
  {
1462
    // not necessary
1463
10534
    return false;
1464
  }
1465
118177
  eq = tt.eqNode(tt_cons);
1466
  // Determine if the equality must be sent out as a lemma. Notice that
1467
  // we  keep new equalities from the instantiate rule internal
1468
  // as long as they are for datatype constructors that have no arguments that
1469
  // have finite external type, which corresponds to:
1470
  //   forceLemma = dt[index].hasFiniteExternalArgType(ttn);
1471
  // Such equalities must be sent because they introduce selector terms that
1472
  // may contribute to conflicts due to cardinality (good examples of this are
1473
  // regress0/datatypes/dt-param-card4-bool-sat.smt2 and
1474
  // regress0/datatypes/list-bool.smt2).
1475
  bool forceLemma;
1476
118177
  if (options::dtPoliteOptimize())
1477
  {
1478
118177
    forceLemma = dt[index].hasFiniteExternalArgType(ttn);
1479
  }
1480
  else
1481
  {
1482
    forceLemma = dt.involvesExternalType();
1483
  }
1484
236354
  Trace("datatypes-infer-debug") << "DtInstantiate : " << eqc << " " << eq
1485
118177
                                 << " forceLemma = " << forceLemma << std::endl;
1486
236354
  Trace("datatypes-infer") << "DtInfer : instantiate : " << eq << " by " << exp
1487
118177
                           << std::endl;
1488
118177
  d_im.addPendingInference(eq, InferenceId::DATATYPES_INST, exp, forceLemma);
1489
118177
  return true;
1490
}
1491
1492
28115
void TheoryDatatypes::checkCycles() {
1493
28115
  Trace("datatypes-cycle-check") << "Check acyclicity" << std::endl;
1494
56093
  std::vector< Node > cdt_eqc;
1495
28115
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(d_equalityEngine);
1496
1019231
  while( !eqcs_i.isFinished() ){
1497
991253
    Node eqc = (*eqcs_i);
1498
991253
    TypeNode tn = eqc.getType();
1499
495695
    if( tn.isDatatype() ) {
1500
168961
      if( !tn.isCodatatype() ){
1501
167787
        if( options::dtCyclic() ){
1502
          //do cycle checks
1503
335437
          std::map< TNode, bool > visited;
1504
335437
          std::map< TNode, bool > proc;
1505
335437
          std::vector<Node> expl;
1506
167787
          Trace("datatypes-cycle-check") << "...search for cycle starting at " << eqc << std::endl;
1507
335437
          Node cn = searchForCycle( eqc, eqc, visited, proc, expl );
1508
167787
          Trace("datatypes-cycle-check") << "...finish." << std::endl;
1509
          //if we discovered a different cycle while searching this one
1510
167787
          if( !cn.isNull() && cn!=eqc ){
1511
19
            visited.clear();
1512
19
            proc.clear();
1513
19
            expl.clear();
1514
38
            Node prev = cn;
1515
19
            cn = searchForCycle( cn, cn, visited, proc, expl );
1516
19
            Assert(prev == cn);
1517
          }
1518
1519
167787
          if( !cn.isNull() ) {
1520
137
            Assert(expl.size() > 0);
1521
274
            Trace("dt-conflict")
1522
137
                << "CONFLICT: Cycle conflict : " << expl << std::endl;
1523
137
            d_im.sendDtConflict(expl, InferenceId::DATATYPES_CYCLE);
1524
137
            return;
1525
          }
1526
        }
1527
      }else{
1528
        //indexing
1529
1174
        cdt_eqc.push_back( eqc );
1530
      }
1531
    }
1532
495558
    ++eqcs_i;
1533
  }
1534
27978
  Trace("datatypes-cycle-check") << "Check uniqueness" << std::endl;
1535
  //process codatatypes
1536
27978
  if( cdt_eqc.size()>1 && options::cdtBisimilar() ){
1537
90
    printModelDebug("dt-cdt-debug");
1538
90
    Trace("dt-cdt-debug") << "Process " << cdt_eqc.size() << " co-datatypes" << std::endl;
1539
180
    std::vector< std::vector< Node > > part_out;
1540
180
    std::vector<Node> exp;
1541
180
    std::map< Node, Node > cn;
1542
180
    std::map< Node, std::map< Node, int > > dni;
1543
1260
    for( unsigned i=0; i<cdt_eqc.size(); i++ ){
1544
1170
      cn[cdt_eqc[i]] = cdt_eqc[i];
1545
    }
1546
90
    separateBisimilar( cdt_eqc, part_out, exp, cn, dni, 0, false );
1547
90
    Trace("dt-cdt-debug") << "Done separate bisimilar." << std::endl;
1548
90
    if( !part_out.empty() ){
1549
10
      Trace("dt-cdt-debug") << "Process partition size " << part_out.size() << std::endl;
1550
20
      for( unsigned i=0; i<part_out.size(); i++ ){
1551
20
        std::vector< Node > part;
1552
10
        part.push_back( part_out[i][0] );
1553
20
        for( unsigned j=1; j<part_out[i].size(); j++ ){
1554
10
          Trace("dt-cdt") << "Codatatypes : " << part_out[i][0] << " and " << part_out[i][j] << " must be equal!!" << std::endl;
1555
10
          part.push_back( part_out[i][j] );
1556
20
          std::vector< std::vector< Node > > tpart_out;
1557
10
          exp.clear();
1558
10
          cn.clear();
1559
10
          cn[part_out[i][0]] = part_out[i][0];
1560
10
          cn[part_out[i][j]] = part_out[i][j];
1561
10
          dni.clear();
1562
10
          separateBisimilar( part, tpart_out, exp, cn, dni, 0, true );
1563
10
          Assert(tpart_out.size() == 1 && tpart_out[0].size() == 2);
1564
10
          part.pop_back();
1565
          //merge based on explanation
1566
10
          Trace("dt-cdt") << "  exp is : ";
1567
38
          for( unsigned k=0; k<exp.size(); k++ ){
1568
28
            Trace("dt-cdt") << exp[k] << " ";
1569
          }
1570
10
          Trace("dt-cdt") << std::endl;
1571
20
          Node eq = part_out[i][0].eqNode( part_out[i][j] );
1572
20
          Node eqExp = NodeManager::currentNM()->mkAnd(exp);
1573
10
          d_im.addPendingInference(eq, InferenceId::DATATYPES_BISIMILAR, eqExp);
1574
10
          Trace("datatypes-infer") << "DtInfer : cdt-bisimilar : " << eq << " by " << eqExp << std::endl;
1575
        }
1576
      }
1577
    }
1578
  }
1579
}
1580
1581
//everything is in terms of representatives
1582
278
void TheoryDatatypes::separateBisimilar(
1583
    std::vector<Node>& part,
1584
    std::vector<std::vector<Node> >& part_out,
1585
    std::vector<Node>& exp,
1586
    std::map<Node, Node>& cn,
1587
    std::map<Node, std::map<Node, int> >& dni,
1588
    int dniLvl,
1589
    bool mkExp)
1590
{
1591
278
  if( !mkExp ){
1592
240
    Trace("dt-cdt-debug") << "Separate bisimilar : " << std::endl;
1593
1818
    for( unsigned i=0; i<part.size(); i++ ){
1594
1578
      Trace("dt-cdt-debug") << "   " << part[i] << ", current = " << cn[part[i]] << std::endl;
1595
    }
1596
  }
1597
278
  Assert(part.size() > 1);
1598
556
  std::map< Node, std::vector< Node > > new_part;
1599
556
  std::map< Node, std::vector< Node > > new_part_c;
1600
556
  std::map< int, std::vector< Node > > new_part_rec;
1601
1602
556
  std::map< Node, Node > cn_cons;
1603
1932
  for( unsigned j=0; j<part.size(); j++ ){
1604
3308
    Node c = cn[part[j]];
1605
1654
    std::map< Node, int >::iterator it_rec = dni[part[j]].find( c );
1606
1654
    if( it_rec!=dni[part[j]].end() ){
1607
      //looped
1608
46
      if( !mkExp ){ Trace("dt-cdt-debug") << "  - " << part[j] << " is looping at index " << it_rec->second << std::endl; }
1609
46
      new_part_rec[ it_rec->second ].push_back( part[j] );
1610
    }else{
1611
1608
      if( c.getType().isDatatype() ){
1612
2704
        Node ncons = getEqcConstructor( c );
1613
1352
        if( ncons.getKind()==APPLY_CONSTRUCTOR ) {
1614
810
          Node cc = ncons.getOperator();
1615
405
          cn_cons[part[j]] = ncons;
1616
405
          if (mkExp && c != ncons)
1617
          {
1618
20
            exp.push_back(c.eqNode(ncons));
1619
          }
1620
405
          new_part[cc].push_back( part[j] );
1621
405
          if( !mkExp ){ Trace("dt-cdt-debug") << "  - " << part[j] << " is datatype " << ncons << "." << std::endl; }
1622
        }else{
1623
947
          new_part_c[c].push_back( part[j] );
1624
947
          if( !mkExp ){ Trace("dt-cdt-debug") << "  - " << part[j] << " is unspecified datatype." << std::endl; }
1625
        }
1626
      }else{
1627
        //add equivalences
1628
256
        if( !mkExp ){ Trace("dt-cdt-debug") << "  - " << part[j] << " is term " << c << "." << std::endl; }
1629
256
        new_part_c[c].push_back( part[j] );
1630
      }
1631
    }
1632
  }
1633
  //direct add for constants
1634
1376
  for( std::map< Node, std::vector< Node > >::iterator it = new_part_c.begin(); it != new_part_c.end(); ++it ){
1635
1098
    if( it->second.size()>1 ){
1636
186
      std::vector< Node > vec;
1637
93
      vec.insert( vec.begin(), it->second.begin(), it->second.end() );
1638
93
      part_out.push_back( vec );
1639
    }
1640
  }
1641
  //direct add for recursive
1642
304
  for( std::map< int, std::vector< Node > >::iterator it = new_part_rec.begin(); it != new_part_rec.end(); ++it ){
1643
26
    if( it->second.size()>1 ){
1644
40
      std::vector< Node > vec;
1645
20
      vec.insert( vec.begin(), it->second.begin(), it->second.end() );
1646
20
      part_out.push_back( vec );
1647
    }else{
1648
      //add back : could match a datatype?
1649
    }
1650
  }
1651
  //recurse for the datatypes
1652
502
  for( std::map< Node, std::vector< Node > >::iterator it = new_part.begin(); it != new_part.end(); ++it ){
1653
224
    if( it->second.size()>1 ){
1654
      //set dni to check for loops
1655
170
      std::map< Node, Node > dni_rem;
1656
351
      for( unsigned i=0; i<it->second.size(); i++ ){
1657
532
        Node n = it->second[i];
1658
266
        dni[n][cn[n]] = dniLvl;
1659
266
        dni_rem[n] = cn[n];
1660
      }
1661
1662
      //we will split based on the arguments of the datatype
1663
170
      std::vector< std::vector< Node > > split_new_part;
1664
85
      split_new_part.push_back( it->second );
1665
1666
85
      unsigned nChildren = cn_cons[it->second[0]].getNumChildren();
1667
      //for each child of constructor
1668
85
      unsigned cindex = 0;
1669
405
      while( cindex<nChildren && !split_new_part.empty() ){
1670
160
        if( !mkExp ){ Trace("dt-cdt-debug") << "Split argument #" << cindex << " of " << it->first << "..." << std::endl; }
1671
320
        std::vector< std::vector< Node > > next_split_new_part;
1672
338
        for( unsigned j=0; j<split_new_part.size(); j++ ){
1673
          //set current node
1674
642
          for( unsigned k=0; k<split_new_part[j].size(); k++ ){
1675
928
            Node n = split_new_part[j][k];
1676
928
            Node cnc = cn_cons[n][cindex];
1677
928
            Node nr = getRepresentative(cnc);
1678
464
            cn[n] = nr;
1679
464
            if (mkExp && cnc != nr)
1680
            {
1681
8
              exp.push_back(nr.eqNode(cnc));
1682
            }
1683
          }
1684
356
          std::vector< std::vector< Node > > c_part_out;
1685
178
          separateBisimilar( split_new_part[j], c_part_out, exp, cn, dni, dniLvl+1, mkExp );
1686
178
          next_split_new_part.insert( next_split_new_part.end(), c_part_out.begin(), c_part_out.end() );
1687
        }
1688
160
        split_new_part.clear();
1689
160
        split_new_part.insert( split_new_part.end(), next_split_new_part.begin(), next_split_new_part.end() );
1690
160
        cindex++;
1691
      }
1692
85
      part_out.insert( part_out.end(), split_new_part.begin(), split_new_part.end() );
1693
1694
351
      for( std::map< Node, Node >::iterator it2 = dni_rem.begin(); it2 != dni_rem.end(); ++it2 ){
1695
266
        dni[it2->first].erase( it2->second );
1696
      }
1697
    }
1698
  }
1699
278
}
1700
1701
//postcondition: if cycle detected, explanation is why n is a subterm of on
1702
713980
Node TheoryDatatypes::searchForCycle(TNode n,
1703
                                     TNode on,
1704
                                     std::map<TNode, bool>& visited,
1705
                                     std::map<TNode, bool>& proc,
1706
                                     std::vector<Node>& explanation,
1707
                                     bool firstTime)
1708
{
1709
713980
  Trace("datatypes-cycle-check2") << "Search for cycle " << n << " " << on << endl;
1710
1427960
  TNode ncons;
1711
1427960
  TNode nn;
1712
713980
  if( !firstTime ){
1713
546174
    nn = getRepresentative( n );
1714
546174
    if( nn==on ){
1715
137
      if (n != nn)
1716
      {
1717
98
        explanation.push_back(n.eqNode(nn));
1718
      }
1719
137
      return on;
1720
    }
1721
  }else{
1722
167806
    nn = getRepresentative( n );
1723
  }
1724
713843
  if( proc.find( nn )!=proc.end() ){
1725
141594
    return Node::null();
1726
  }
1727
572249
  Trace("datatypes-cycle-check2") << "...representative : " << nn << " " << ( visited.find( nn ) == visited.end() ) << " " << visited.size() << std::endl;
1728
572249
  if( visited.find( nn ) == visited.end() ) {
1729
572230
    Trace("datatypes-cycle-check2") << "  visit : " << nn << std::endl;
1730
572230
    visited[nn] = true;
1731
1144460
    TNode nncons = getEqcConstructor(nn);
1732
572230
    if (nncons.getKind() == APPLY_CONSTRUCTOR)
1733
    {
1734
967404
      for (unsigned i = 0; i < nncons.getNumChildren(); i++)
1735
      {
1736
        TNode cn =
1737
1091387
            searchForCycle(nncons[i], on, visited, proc, explanation, false);
1738
546174
        if( cn==on ) {
1739
          //add explanation for why the constructor is connected
1740
850
          if (n != nncons)
1741
          {
1742
465
            explanation.push_back(n.eqNode(nncons));
1743
          }
1744
850
          return on;
1745
545324
        }else if( !cn.isNull() ){
1746
111
          return cn;
1747
        }
1748
      }
1749
    }
1750
571269
    Trace("datatypes-cycle-check2") << "  unvisit : " << nn << std::endl;
1751
571269
    proc[nn] = true;
1752
571269
    visited.erase( nn );
1753
571269
    return Node::null();
1754
  }else{
1755
38
    TypeNode tn = nn.getType();
1756
19
    if( tn.isDatatype() ) {
1757
19
      if( !tn.isCodatatype() ){
1758
19
        return nn;
1759
      }
1760
    }
1761
    return Node::null();
1762
  }
1763
}
1764
1765
2861789
bool TheoryDatatypes::hasTerm(TNode a) { return d_equalityEngine->hasTerm(a); }
1766
1767
672018
bool TheoryDatatypes::areEqual( TNode a, TNode b ){
1768
672018
  if( a==b ){
1769
9784
    return true;
1770
662234
  }else if( hasTerm( a ) && hasTerm( b ) ){
1771
662234
    return d_equalityEngine->areEqual(a, b);
1772
  }else{
1773
    return false;
1774
  }
1775
}
1776
1777
21136
bool TheoryDatatypes::areDisequal( TNode a, TNode b ){
1778
21136
  if( a==b ){
1779
4602
    return false;
1780
16534
  }else if( hasTerm( a ) && hasTerm( b ) ){
1781
16534
    return d_equalityEngine->areDisequal(a, b, false);
1782
  }else{
1783
    //TODO : constants here?
1784
    return false;
1785
  }
1786
}
1787
1788
91899
bool TheoryDatatypes::areCareDisequal( TNode x, TNode y ) {
1789
91899
  Trace("datatypes-cg") << "areCareDisequal: " << x << " " << y << std::endl;
1790
91899
  Assert(d_equalityEngine->hasTerm(x));
1791
91899
  Assert(d_equalityEngine->hasTerm(y));
1792
275697
  if (d_equalityEngine->isTriggerTerm(x, THEORY_DATATYPES)
1793
275697
      && d_equalityEngine->isTriggerTerm(y, THEORY_DATATYPES))
1794
  {
1795
    TNode x_shared =
1796
88821
        d_equalityEngine->getTriggerTermRepresentative(x, THEORY_DATATYPES);
1797
    TNode y_shared =
1798
88821
        d_equalityEngine->getTriggerTermRepresentative(y, THEORY_DATATYPES);
1799
70192
    EqualityStatus eqStatus = d_valuation.getEqualityStatus(x_shared, y_shared);
1800
70192
    if( eqStatus==EQUALITY_FALSE_AND_PROPAGATED || eqStatus==EQUALITY_FALSE || eqStatus==EQUALITY_FALSE_IN_MODEL ){
1801
51563
      return true;
1802
    }
1803
  }
1804
40336
  return false;
1805
}
1806
1807
1504253
TNode TheoryDatatypes::getRepresentative( TNode a ){
1808
1504253
  if( hasTerm( a ) ){
1809
1504253
    return d_equalityEngine->getRepresentative(a);
1810
  }else{
1811
    return a;
1812
  }
1813
}
1814
1815
36850
void TheoryDatatypes::printModelDebug( const char* c ){
1816
36850
  if(! (Trace.isOn(c))) {
1817
36850
    return;
1818
  }
1819
1820
  Trace( c ) << "Datatypes model : " << std::endl;
1821
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(d_equalityEngine);
1822
  while( !eqcs_i.isFinished() ){
1823
    Node eqc = (*eqcs_i);
1824
    //if( !eqc.getType().isBoolean() ){
1825
      if( eqc.getType().isDatatype() ){
1826
        Trace( c ) << "DATATYPE : ";
1827
      }
1828
      Trace( c ) << eqc << " : " << eqc.getType() << " : " << std::endl;
1829
      Trace( c ) << "   { ";
1830
      //add terms to model
1831
      eq::EqClassIterator eqc_i = eq::EqClassIterator(eqc, d_equalityEngine);
1832
      while( !eqc_i.isFinished() ){
1833
        if( (*eqc_i)!=eqc ){
1834
          Trace( c ) << (*eqc_i) << " ";
1835
        }
1836
        ++eqc_i;
1837
      }
1838
      Trace( c ) << "}" << std::endl;
1839
      if( eqc.getType().isDatatype() ){
1840
        EqcInfo* ei = getOrMakeEqcInfo( eqc );
1841
        if( ei ){
1842
          Trace( c ) << "   Instantiated : " << ei->d_inst.get() << std::endl;
1843
          Trace( c ) << "   Constructor : ";
1844
          if( !ei->d_constructor.get().isNull() ){
1845
            Trace( c )<< ei->d_constructor.get();
1846
          }
1847
          Trace( c ) << std::endl << "   Labels : ";
1848
          if( hasLabel( ei, eqc ) ){
1849
            Trace( c ) << getLabel( eqc );
1850
          }else{
1851
            NodeUIntMap::iterator lbl_i = d_labels.find(eqc);
1852
            if( lbl_i != d_labels.end() ){
1853
              for (size_t j = 0; j < (*lbl_i).second; j++)
1854
              {
1855
                Trace( c ) << d_labels_data[eqc][j] << " ";
1856
              }
1857
            }
1858
          }
1859
          Trace( c ) << std::endl;
1860
          Trace( c ) << "   Selectors : " << ( ei->d_selectors ? "yes, " : "no " );
1861
          NodeUIntMap::iterator sel_i = d_selector_apps.find(eqc);
1862
          if( sel_i != d_selector_apps.end() ){
1863
            for (size_t j = 0; j < (*sel_i).second; j++)
1864
            {
1865
              Trace( c ) << d_selector_apps_data[eqc][j] << " ";
1866
            }
1867
          }
1868
          Trace( c ) << std::endl;
1869
        }
1870
      }
1871
    //}
1872
    ++eqcs_i;
1873
  }
1874
}
1875
1876
9867
void TheoryDatatypes::computeRelevantTerms(std::set<Node>& termSet)
1877
{
1878
19734
  Trace("dt-cmi") << "Have " << termSet.size() << " relevant terms..."
1879
9867
                  << std::endl;
1880
1881
  //also include non-singleton dt equivalence classes  TODO : revisit this
1882
9867
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(d_equalityEngine);
1883
529703
  while( !eqcs_i.isFinished() ){
1884
519836
    TNode r = (*eqcs_i);
1885
259918
    if (r.getType().isDatatype())
1886
    {
1887
53112
      eq::EqClassIterator eqc_i = eq::EqClassIterator(r, d_equalityEngine);
1888
444368
      while (!eqc_i.isFinished())
1889
      {
1890
195628
        termSet.insert(*eqc_i);
1891
195628
        ++eqc_i;
1892
      }
1893
    }
1894
259918
    ++eqcs_i;
1895
  }
1896
9867
}
1897
1898
std::pair<bool, Node> TheoryDatatypes::entailmentCheck(TNode lit)
1899
{
1900
  Trace("dt-entail") << "Check entailed : " << lit << std::endl;
1901
  Node atom = lit.getKind()==NOT ? lit[0] : lit;
1902
  bool pol = lit.getKind()!=NOT;
1903
  if( atom.getKind()==APPLY_TESTER ){
1904
    Node n = atom[0];
1905
    if( hasTerm( n ) ){
1906
      Node r = d_equalityEngine->getRepresentative(n);
1907
      EqcInfo * ei = getOrMakeEqcInfo( r, false );
1908
      int l_index = getLabelIndex( ei, r );
1909
      int t_index = static_cast<int>(utils::indexOf(atom.getOperator()));
1910
      Trace("dt-entail") << "  Tester indices are " << t_index << " and " << l_index << std::endl;
1911
      if( l_index!=-1 && (l_index==t_index)==pol ){
1912
        std::vector< TNode > exp_c;
1913
        Node eqToExplain;
1914
        if( ei && !ei->d_constructor.get().isNull() ){
1915
          eqToExplain = n.eqNode(ei->d_constructor.get());
1916
        }else{
1917
          Node lbl = getLabel( n );
1918
          Assert(!lbl.isNull());
1919
          exp_c.push_back( lbl );
1920
          Assert(areEqual(n, lbl[0]));
1921
          eqToExplain = n.eqNode(lbl[0]);
1922
        }
1923
        d_equalityEngine->explainLit(eqToExplain, exp_c);
1924
        Node exp = NodeManager::currentNM()->mkAnd(exp_c);
1925
        Trace("dt-entail") << "  entailed, explanation is " << exp << std::endl;
1926
        return make_pair(true, exp);
1927
      }
1928
    }
1929
  }
1930
  return make_pair(false, Node::null());
1931
}
1932
1933
}  // namespace datatypes
1934
}  // namespace theory
1935
29337
}  // namespace cvc5