GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/datatypes/theory_datatypes.cpp Lines: 999 1130 88.4 %
Date: 2021-05-21 Branches: 2313 5110 45.3 %

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/proof_node_manager.h"
25
#include "expr/skolem_manager.h"
26
#include "options/datatypes_options.h"
27
#include "options/quantifiers_options.h"
28
#include "options/smt_options.h"
29
#include "options/theory_options.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
8954
TheoryDatatypes::TheoryDatatypes(Context* c,
51
                                 UserContext* u,
52
                                 OutputChannel& out,
53
                                 Valuation valuation,
54
                                 const LogicInfo& logicInfo,
55
8954
                                 ProofNodeManager* pnm)
56
    : Theory(THEORY_DATATYPES, c, u, out, valuation, logicInfo, pnm),
57
      d_term_sk(u),
58
      d_labels(c),
59
      d_selector_apps(c),
60
      d_collectTermsCache(c),
61
      d_collectTermsCacheU(u),
62
      d_functionTerms(c),
63
      d_singleton_eq(u),
64
      d_lemmas_produced_c(u),
65
      d_sygusExtension(nullptr),
66
      d_state(c, u, valuation),
67
      d_im(*this, d_state, pnm),
68
8954
      d_notify(d_im, *this)
69
{
70
71
8954
  d_true = NodeManager::currentNM()->mkConst( true );
72
8954
  d_zero = NodeManager::currentNM()->mkConst( Rational(0) );
73
8954
  d_dtfCounter = 0;
74
75
  // indicate we are using the default theory state object
76
8954
  d_theoryState = &d_state;
77
8954
  d_inferManager = &d_im;
78
8954
}
79
80
26862
TheoryDatatypes::~TheoryDatatypes() {
81
30837
  for(std::map< Node, EqcInfo* >::iterator i = d_eqc_info.begin(), iend = d_eqc_info.end();
82
30837
      i != iend; ++i){
83
21883
    EqcInfo* current = (*i).second;
84
21883
    Assert(current != NULL);
85
21883
    delete current;
86
  }
87
17908
}
88
89
8954
TheoryRewriter* TheoryDatatypes::getTheoryRewriter() { return &d_rewriter; }
90
91
3117
ProofRuleChecker* TheoryDatatypes::getProofChecker() { return &d_checker; }
92
93
8954
bool TheoryDatatypes::needsEqualityEngine(EeSetupInfo& esi)
94
{
95
8954
  esi.d_notify = &d_notify;
96
8954
  esi.d_name = "theory::datatypes::ee";
97
8954
  return true;
98
}
99
100
8954
void TheoryDatatypes::finishInit()
101
{
102
8954
  Assert(d_equalityEngine != nullptr);
103
  // The kinds we are treating as function application in congruence
104
8954
  d_equalityEngine->addFunctionKind(kind::APPLY_CONSTRUCTOR);
105
8954
  d_equalityEngine->addFunctionKind(kind::APPLY_SELECTOR_TOTAL);
106
8954
  d_equalityEngine->addFunctionKind(kind::APPLY_TESTER);
107
  // We could but don't do congruence for DT_SIZE and DT_HEIGHT_BOUND here.
108
  // It also could make sense in practice to do congruence for APPLY_UF, but
109
  // this is not done.
110
8954
  if (getQuantifiersEngine() && options::sygus())
111
  {
112
    quantifiers::TermDbSygus* tds =
113
1543
        getQuantifiersEngine()->getTermDatabaseSygus();
114
1543
    d_sygusExtension.reset(new SygusExtension(d_state, d_im, tds));
115
    // do congruence on evaluation functions
116
1543
    d_equalityEngine->addFunctionKind(kind::DT_SYGUS_EVAL);
117
  }
118
  // testers are not relevant for model building
119
8954
  d_valuation.setIrrelevantKind(APPLY_TESTER);
120
8954
}
121
122
2261019
TheoryDatatypes::EqcInfo* TheoryDatatypes::getOrMakeEqcInfo( TNode n, bool doMake ){
123
2261019
  if( !hasEqcInfo( n ) ){
124
496253
    if( doMake ){
125
      //add to labels
126
129316
      d_labels[ n ] = 0;
127
128
129316
      std::map< Node, EqcInfo* >::iterator eqc_i = d_eqc_info.find( n );
129
      EqcInfo* ei;
130
129316
      if( eqc_i != d_eqc_info.end() ){
131
107433
        ei = eqc_i->second;
132
      }else{
133
21883
        ei = new EqcInfo( getSatContext() );
134
21883
        d_eqc_info[n] = ei;
135
      }
136
129316
      if( n.getKind()==APPLY_CONSTRUCTOR ){
137
98068
        ei->d_constructor = n;
138
      }
139
140
      //add to selectors
141
129316
      d_selector_apps[n] = 0;
142
143
129316
      return ei;
144
    }else{
145
366937
      return NULL;
146
    }
147
  }else{
148
1764766
    std::map< Node, EqcInfo* >::iterator eqc_i = d_eqc_info.find( n );
149
1764766
    return (*eqc_i).second;
150
  }
151
}
152
153
614307
TNode TheoryDatatypes::getEqcConstructor( TNode r ) {
154
614307
  if( r.getKind()==APPLY_CONSTRUCTOR ){
155
386132
    return r;
156
  }else{
157
228175
    EqcInfo * ei = getOrMakeEqcInfo( r, false );
158
228175
    if( ei && !ei->d_constructor.get().isNull() ){
159
54813
      return ei->d_constructor.get();
160
    }else{
161
173362
      return r;
162
    }
163
  }
164
}
165
166
480105
bool TheoryDatatypes::preCheck(Effort level)
167
{
168
480105
  d_im.reset();
169
480105
  d_im.clearPending();
170
480105
  return false;
171
}
172
173
480105
void TheoryDatatypes::postCheck(Effort level)
174
{
175
  // Apply any last pending inferences, which may occur if the last processed
176
  // fact was an internal one and triggered further internal inferences.
177
480105
  d_im.process();
178
480105
  if (level == EFFORT_LAST_CALL)
179
  {
180
5486
    Assert(d_sygusExtension != nullptr);
181
5486
    d_sygusExtension->check();
182
5486
    return;
183
  }
184
976264
  else if (level == EFFORT_FULL && !d_state.isInConflict()
185
501529
           && !d_im.hasSentLemma() && !d_valuation.needCheck())
186
  {
187
    //check for cycles
188
23313
    Assert(!d_im.hasPendingFact());
189
4
    do {
190
23317
      d_im.reset();
191
23317
      Trace("datatypes-proc") << "Check cycles..." << std::endl;
192
23317
      checkCycles();
193
23317
      Trace("datatypes-proc") << "...finish check cycles" << std::endl;
194
23317
      d_im.process();
195
23317
      if (d_state.isInConflict() || d_im.hasSentLemma())
196
      {
197
418
        return;
198
      }
199
22899
    } while (d_im.hasSentFact());
200
201
    //check for splits
202
22895
    Trace("datatypes-debug") << "Check for splits " << endl;
203
925
    do {
204
23820
      d_im.reset();
205
47640
      std::map< TypeNode, Node > rec_singletons;
206
23820
      eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(d_equalityEngine);
207
874382
      while( !eqcs_i.isFinished() ){
208
851718
        Node n = (*eqcs_i);
209
        //TODO : avoid irrelevant (pre-registered but not asserted) terms here?
210
851718
        TypeNode tn = n.getType();
211
426437
        if( tn.isDatatype() ){
212
134795
          Trace("datatypes-debug") << "Process equivalence class " << n << std::endl;
213
134795
          EqcInfo* eqc = getOrMakeEqcInfo( n );
214
          //if there are more than 1 possible constructors for eqc
215
134795
          if( !hasLabel( eqc, n ) ){
216
36150
            Trace("datatypes-debug") << "No constructor..." << std::endl;
217
71144
            TypeNode tt = tn;
218
36150
            const DType& dt = tt.getDType();
219
72300
            Trace("datatypes-debug")
220
72300
                << "Datatype " << dt.getName() << " is "
221
72300
                << dt.getCardinalityClass(tt) << " "
222
36150
                << dt.isRecursiveSingleton(tt) << std::endl;
223
36150
            bool continueProc = true;
224
36150
            if( dt.isRecursiveSingleton( tt ) ){
225
8
              Trace("datatypes-debug") << "Check recursive singleton..." << std::endl;
226
              //handle recursive singleton case
227
8
              std::map< TypeNode, Node >::iterator itrs = rec_singletons.find( tn );
228
8
              if( itrs!=rec_singletons.end() ){
229
8
                Node eq = n.eqNode( itrs->second );
230
4
                if( d_singleton_eq.find( eq )==d_singleton_eq.end() ){
231
4
                  d_singleton_eq[eq] = true;
232
                  // get assumptions
233
4
                  bool success = true;
234
8
                  std::vector< Node > assumptions;
235
                  //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,
236
                  //  do not infer the equality if at least one sort was processed.
237
                  //otherwise, if the logic is quantified, under the assumption that all uninterpreted sorts have cardinality one,
238
                  //  infer the equality.
239
4
                  for( unsigned i=0; i<dt.getNumRecursiveSingletonArgTypes( tt ); i++ ){
240
                    TypeNode type = dt.getRecursiveSingletonArgType(tt, i);
241
                    if( getQuantifiersEngine() ){
242
                      // under the assumption that the cardinality of this type is one
243
                      Node a = getSingletonLemma(type, true);
244
                      assumptions.push_back( a.negate() );
245
                    }else{
246
                      success = false;
247
                      // assert that the cardinality of this type is more than one
248
                      getSingletonLemma(type, false);
249
                    }
250
                  }
251
4
                  if( success ){
252
8
                    Node assumption = n.eqNode(itrs->second);
253
4
                    assumptions.push_back(assumption);
254
8
                    Node lemma = assumptions.size()==1 ? assumptions[0] : NodeManager::currentNM()->mkNode( OR, assumptions );
255
4
                    Trace("dt-singleton") << "*************Singleton equality lemma " << lemma << std::endl;
256
4
                    d_im.lemma(lemma, InferenceId::DATATYPES_REC_SINGLETON_EQ);
257
                  }
258
                }
259
              }else{
260
4
                rec_singletons[tn] = n;
261
              }
262
              //do splitting for quantified logics (incomplete anyways)
263
8
              continueProc = ( getQuantifiersEngine()!=NULL );
264
            }
265
36150
            if( continueProc ){
266
36150
              Trace("datatypes-debug") << "Get possible cons..." << std::endl;
267
              //all other cases
268
71144
              std::vector< bool > pcons;
269
36150
              getPossibleCons( eqc, n, pcons );
270
              //check if we do not need to resolve the constructor type for this equivalence class.
271
              // this is if there are no selectors for this equivalence class, and its possible values are infinite,
272
              //  then do not split.
273
36150
              int consIndex = -1;
274
36150
              int fconsIndex = -1;
275
36150
              bool needSplit = true;
276
203361
              for (size_t j = 0, psize = pcons.size(); j < psize; j++)
277
              {
278
167211
                if( pcons[j] ) {
279
166066
                  if( consIndex==-1 ){
280
36056
                    consIndex = j;
281
                  }
282
332132
                  Trace("datatypes-debug") << j << " compute finite..."
283
166066
                                           << std::endl;
284
                  // Notice that we split here on all datatypes except the
285
                  // truly infinite ones. It is possible to also not split
286
                  // on those that are interpreted-finite when finite model
287
                  // finding is disabled, but as a heuristic we choose to split
288
                  // on those too.
289
332132
                  bool ifin = dt[j].getCardinalityClass(tt)
290
166066
                              != CardinalityClass::INFINITE;
291
332132
                  Trace("datatypes-debug") << "...returned " << ifin
292
166066
                                           << std::endl;
293
166066
                  if (!ifin)
294
                  {
295
113886
                    if( !eqc || !eqc->d_selectors ){
296
111103
                      needSplit = false;
297
                    }
298
                  }else{
299
52180
                    if( fconsIndex==-1 ){
300
33454
                      fconsIndex = j;
301
                    }
302
                  }
303
                }
304
              }
305
              //if we want to force an assignment of constructors to all ground eqc
306
              //d_dtfCounter++;
307
62310
              if( !needSplit && options::dtForceAssignment() && d_dtfCounter%2==0 ){
308
                Trace("datatypes-force-assign") << "Force assignment for " << n << std::endl;
309
                needSplit = true;
310
                consIndex = fconsIndex!=-1 ? fconsIndex : consIndex;
311
              }
312
313
36150
              if( needSplit ) {
314
9990
                if( dt.getNumConstructors()==1 ){
315
                  //this may not be necessary?
316
                  //if only one constructor, then this term must be this constructor
317
17668
                  Node t = utils::mkTester(n, 0, dt);
318
8834
                  d_im.addPendingInference(
319
                      t, InferenceId::DATATYPES_SPLIT, d_true);
320
8834
                  Trace("datatypes-infer") << "DtInfer : 1-cons (full) : " << t << std::endl;
321
                }else{
322
1156
                  Assert(consIndex != -1 || dt.isSygus());
323
2312
                  if( options::dtBinarySplit() && consIndex!=-1 ){
324
                    Node test = utils::mkTester(n, consIndex, dt);
325
                    Trace("dt-split") << "*************Split for possible constructor " << dt[consIndex] << " for " << n << endl;
326
                    test = Rewriter::rewrite( test );
327
                    NodeBuilder nb(kind::OR);
328
                    nb << test << test.notNode();
329
                    Node lemma = nb;
330
                    d_im.lemma(lemma, InferenceId::DATATYPES_BINARY_SPLIT);
331
                    d_im.requirePhase(test, true);
332
                  }else{
333
1156
                    Trace("dt-split") << "*************Split for constructors on " << n <<  endl;
334
2312
                    Node lemma = utils::mkSplit(n, dt);
335
1156
                    Trace("dt-split-debug") << "Split lemma is : " << lemma << std::endl;
336
1156
                    d_im.sendDtLemma(lemma,
337
                                     InferenceId::DATATYPES_SPLIT,
338
                                     LemmaProperty::SEND_ATOMS);
339
                  }
340
2312
                  if( !options::dtBlastSplits() ){
341
1156
                    break;
342
                  }
343
                }
344
              }else{
345
26160
                Trace("dt-split-debug") << "Do not split constructor for " << n << " : " << n.getType() << " " << dt.getNumConstructors() << std::endl;
346
              }
347
            }
348
          }else{
349
98645
            Trace("datatypes-debug") << "Has constructor " << eqc->d_constructor.get() << std::endl;
350
          }
351
        }
352
425281
        ++eqcs_i;
353
      }
354
23820
      if (d_im.hasSentLemma())
355
      {
356
        // clear pending facts: we added a lemma, so internal inferences are
357
        // no longer necessary
358
1160
        d_im.clearPendingFacts();
359
      }
360
      else
361
      {
362
        // we did not add a lemma, process internal inferences. This loop
363
        // will repeat.
364
22660
        Trace("datatypes-debug") << "Flush pending facts..." << std::endl;
365
22660
        d_im.process();
366
      }
367
47149
    } while (!d_state.isInConflict() && !d_im.hasSentLemma()
368
45838
             && d_im.hasSentFact());
369
45790
    Trace("datatypes-debug")
370
45790
        << "Finished, conflict=" << d_state.isInConflict()
371
22895
        << ", lemmas=" << d_im.hasSentLemma() << std::endl;
372
22895
    if (!d_state.isInConflict())
373
    {
374
22404
      Trace("dt-model-debug") << std::endl;
375
22404
      printModelDebug("dt-model-debug");
376
    }
377
  }
378
379
474201
  Trace("datatypes-check") << "Finished check effort " << level << std::endl;
380
474201
  if( Debug.isOn("datatypes") || Debug.isOn("datatypes-split") ) {
381
    Notice() << "TheoryDatatypes::check(): done" << endl;
382
  }
383
}
384
385
10065
bool TheoryDatatypes::needsCheckLastEffort() {
386
10065
  return d_sygusExtension != nullptr;
387
}
388
389
1870094
void TheoryDatatypes::notifyFact(TNode atom,
390
                                 bool polarity,
391
                                 TNode fact,
392
                                 bool isInternal)
393
{
394
3740188
  Trace("datatypes-debug") << "TheoryDatatypes::assertFact : " << fact
395
1870094
                           << ", isInternal = " << isInternal << std::endl;
396
  // could be sygus-specific
397
1870094
  if (d_sygusExtension)
398
  {
399
1549861
    d_sygusExtension->assertFact(atom, polarity);
400
  }
401
  //add to tester if applicable
402
3740188
  Node t_arg;
403
1870094
  int tindex = utils::isTester(atom, t_arg);
404
1870094
  if (tindex >= 0)
405
  {
406
890892
    Trace("dt-tester") << "Assert tester : " << atom << " for " << t_arg << std::endl;
407
1781784
    Node rep = getRepresentative( t_arg );
408
890892
    EqcInfo* eqc = getOrMakeEqcInfo( rep, true );
409
    Node tst =
410
1781784
        isInternal ? (polarity ? Node(atom) : atom.notNode()) : Node(fact);
411
890892
    addTester(tindex, tst, eqc, rep, t_arg);
412
890892
    Trace("dt-tester") << "Done assert tester." << std::endl;
413
890892
    Trace("dt-tester") << "Done pending merges." << std::endl;
414
890892
    if (!d_state.isInConflict() && polarity)
415
    {
416
242890
      if (d_sygusExtension)
417
      {
418
197210
        Trace("dt-tester") << "Assert tester to sygus : " << atom << std::endl;
419
197210
        d_sygusExtension->assertTester(tindex, t_arg, atom);
420
197210
        Trace("dt-tester") << "Done assert tester to sygus." << std::endl;
421
      }
422
    }
423
  }else{
424
979202
    Trace("dt-tester-debug") << "Assert (non-tester) : " << atom << std::endl;
425
  }
426
1870094
  Trace("datatypes-debug") << "TheoryDatatypes::assertFact : finished " << fact << std::endl;
427
  // now, flush pending facts if this wasn't an internal call
428
1870094
  if (!isInternal)
429
  {
430
1596122
    d_im.process();
431
  }
432
1870094
}
433
434
138394
void TheoryDatatypes::preRegisterTerm(TNode n)
435
{
436
276788
  Trace("datatypes-prereg")
437
138394
      << "TheoryDatatypes::preRegisterTerm() " << n << endl;
438
  // external selectors should be preprocessed away by now
439
138394
  Assert(n.getKind() != APPLY_SELECTOR);
440
  // must ensure the type is well founded and has no nested recursion if
441
  // the option dtNestedRec is not set to true.
442
276788
  TypeNode tn = n.getType();
443
138394
  if (tn.isDatatype())
444
  {
445
52594
    const DType& dt = tn.getDType();
446
52594
    Trace("dt-expand") << "Check properties of " << dt.getName() << std::endl;
447
52594
    if (!dt.isWellFounded())
448
    {
449
      std::stringstream ss;
450
      ss << "Cannot handle non-well-founded datatype " << dt.getName();
451
      throw LogicException(ss.str());
452
    }
453
52594
    Trace("dt-expand") << "...well-founded ok" << std::endl;
454
105198
    if (!options::dtNestedRec())
455
    {
456
52366
      if (dt.hasNestedRecursion())
457
      {
458
2
        std::stringstream ss;
459
1
        ss << "Cannot handle nested-recursive datatype " << dt.getName();
460
1
        throw LogicException(ss.str());
461
      }
462
52365
      Trace("dt-expand") << "...nested recursion ok" << std::endl;
463
    }
464
  }
465
138393
  collectTerms( n );
466
138393
  switch (n.getKind()) {
467
63787
  case kind::EQUAL:
468
  case kind::APPLY_TESTER:
469
    // add predicate trigger for testers and equalities
470
    // Get triggered for both equal and dis-equal
471
63787
    d_equalityEngine->addTriggerPredicate(n);
472
63787
    break;
473
74606
  default:
474
    // Function applications/predicates
475
74606
    d_equalityEngine->addTerm(n);
476
74606
    if (d_sygusExtension)
477
    {
478
21100
      d_sygusExtension->preRegisterTerm(n);
479
    }
480
74606
    break;
481
  }
482
138393
  d_im.process();
483
138393
}
484
485
66348
TrustNode TheoryDatatypes::ppRewrite(TNode in, std::vector<SkolemLemma>& lems)
486
{
487
66348
  Debug("tuprec") << "TheoryDatatypes::ppRewrite(" << in << ")" << endl;
488
  // first, see if we need to expand definitions
489
132696
  TrustNode texp = d_rewriter.expandDefinition(in);
490
66348
  if (!texp.isNull())
491
  {
492
3004
    return texp;
493
  }
494
63344
  if( in.getKind()==EQUAL ){
495
7884
    Node nn;
496
7884
    std::vector< Node > rew;
497
3988
    if (utils::checkClash(in[0], in[1], rew))
498
    {
499
      nn = NodeManager::currentNM()->mkConst(false);
500
    }
501
    else
502
    {
503
11892
      nn = rew.size()==0 ? d_true :
504
7904
                ( rew.size()==1 ? rew[0] : NodeManager::currentNM()->mkNode( kind::AND, rew ) );
505
    }
506
3988
    if (in != nn)
507
    {
508
92
      return TrustNode::mkTrustRewrite(in, nn, nullptr);
509
    }
510
  }
511
512
  // nothing to do
513
63252
  return TrustNode::null();
514
}
515
516
50906
TrustNode TheoryDatatypes::explain(TNode literal)
517
{
518
50906
  return d_im.explainLit(literal);
519
}
520
521
/** called when a new equivalance class is created */
522
255478
void TheoryDatatypes::eqNotifyNewClass(TNode t){
523
255478
  if( t.getKind()==APPLY_CONSTRUCTOR ){
524
98068
    getOrMakeEqcInfo( t, true );
525
  }
526
255478
}
527
528
/** called when two equivalance classes have merged */
529
2089984
void TheoryDatatypes::eqNotifyMerge(TNode t1, TNode t2)
530
{
531
2089984
  if( t1.getType().isDatatype() ){
532
957214
    Trace("datatypes-debug")
533
478607
        << "NotifyMerge : " << t1 << " " << t2 << std::endl;
534
478607
    merge(t1,t2);
535
  }
536
2089984
}
537
538
478607
void TheoryDatatypes::merge( Node t1, Node t2 ){
539
478607
  if (!d_state.isInConflict())
540
  {
541
952407
    TNode trep1 = t1;
542
952407
    TNode trep2 = t2;
543
477448
    Trace("datatypes-debug") << "Merge " << t1 << " " << t2 << std::endl;
544
477448
    EqcInfo* eqc2 = getOrMakeEqcInfo( t2 );
545
477448
    if( eqc2 ){
546
338708
      bool checkInst = false;
547
338708
      if( !eqc2->d_constructor.get().isNull() ){
548
88854
        trep2 = eqc2->d_constructor.get();
549
      }
550
338708
      EqcInfo* eqc1 = getOrMakeEqcInfo( t1 );
551
338708
      if( eqc1 ){
552
328681
        Trace("datatypes-debug") << "  merge eqc info " << eqc2 << " into " << eqc1 << std::endl;
553
328681
        if( !eqc1->d_constructor.get().isNull() ){
554
274182
          trep1 = eqc1->d_constructor.get();
555
        }
556
        //check for clash
557
655195
        TNode cons1 = eqc1->d_constructor.get();
558
655195
        TNode cons2 = eqc2->d_constructor.get();
559
        //if both have constructor, then either clash or unification
560
328681
        if( !cons1.isNull() && !cons2.isNull() ){
561
50152
          Trace("datatypes-debug") << "  constructors : " << cons1 << " " << cons2 << std::endl;
562
98141
          Node unifEq = cons1.eqNode( cons2 );
563
98141
          std::vector< Node > rew;
564
50152
          if (utils::checkClash(cons1, cons2, rew))
565
          {
566
4326
            std::vector<Node> conf;
567
2163
            conf.push_back(unifEq);
568
4326
            Trace("dt-conflict")
569
2163
                << "CONFLICT: Clash conflict : " << conf << std::endl;
570
2163
            d_im.sendDtConflict(conf, InferenceId::DATATYPES_CLASH_CONFLICT);
571
2163
            return;
572
          }
573
          else
574
          {
575
            //do unification
576
140360
            for( int i=0; i<(int)cons1.getNumChildren(); i++ ) {
577
92371
              if( !areEqual( cons1[i], cons2[i] ) ){
578
59088
                Node eq = cons1[i].eqNode( cons2[i] );
579
29544
                d_im.addPendingInference(
580
                    eq, InferenceId::DATATYPES_UNIF, unifEq);
581
29544
                Trace("datatypes-infer") << "DtInfer : cons-inj : " << eq << " by " << unifEq << std::endl;
582
              }
583
            }
584
          }
585
        }
586
326518
        Trace("datatypes-debug") << "  instantiated : " << eqc1->d_inst << " " << eqc2->d_inst << std::endl;
587
326518
        eqc1->d_inst = eqc1->d_inst || eqc2->d_inst;
588
326518
        if( !cons2.isNull() ){
589
82567
          if( cons1.isNull() ){
590
34578
            Trace("datatypes-debug") << "  must check if it is okay to set the constructor." << std::endl;
591
34578
            checkInst = true;
592
34578
            addConstructor( eqc2->d_constructor.get(), eqc1, t1 );
593
34578
            if (d_state.isInConflict())
594
            {
595
4
              return;
596
            }
597
          }
598
        }
599
      }else{
600
10027
        Trace("datatypes-debug") << "  no eqc info for " << t1 << ", must create" << std::endl;
601
        //just copy the equivalence class information
602
10027
        eqc1 = getOrMakeEqcInfo( t1, true );
603
10027
        eqc1->d_inst.set( eqc2->d_inst );
604
10027
        eqc1->d_constructor.set( eqc2->d_constructor );
605
10027
        eqc1->d_selectors.set( eqc2->d_selectors );
606
      }
607
608
609
      //merge labels
610
336541
      NodeUIntMap::iterator lbl_i = d_labels.find(t2);
611
336541
      if( lbl_i != d_labels.end() ){
612
336541
        Trace("datatypes-debug") << "  merge labels from " << eqc2 << " " << t2 << std::endl;
613
336541
        size_t n_label = (*lbl_i).second;
614
586012
        for (size_t i = 0; i < n_label; i++)
615
        {
616
249793
          Assert(i < d_labels_data[t2].size());
617
499264
          Node t = d_labels_data[ t2 ][i];
618
499264
          Node t_arg = d_labels_args[t2][i];
619
249793
          unsigned tindex = d_labels_tindex[t2][i];
620
249793
          addTester( tindex, t, eqc1, t1, t_arg );
621
249793
          if (d_state.isInConflict())
622
          {
623
322
            Trace("datatypes-debug") << "  conflict!" << std::endl;
624
322
            return;
625
          }
626
        }
627
628
      }
629
      //merge selectors
630
336219
      if( !eqc1->d_selectors && eqc2->d_selectors ){
631
50932
        eqc1->d_selectors = true;
632
50932
        checkInst = true;
633
      }
634
336219
      NodeUIntMap::iterator sel_i = d_selector_apps.find(t2);
635
336219
      if( sel_i != d_selector_apps.end() ){
636
336219
        Trace("datatypes-debug") << "  merge selectors from " << eqc2 << " " << t2 << std::endl;
637
336219
        size_t n_sel = (*sel_i).second;
638
817428
        for (size_t j = 0; j < n_sel; j++)
639
        {
640
481209
          addSelector( d_selector_apps_data[t2][j], eqc1, t1, eqc2->d_constructor.get().isNull() );
641
        }
642
      }
643
336219
      if( checkInst ){
644
85506
        Trace("datatypes-debug") << "  checking instantiate" << std::endl;
645
85506
        instantiate( eqc1, t1 );
646
85506
        if (d_state.isInConflict())
647
        {
648
          return;
649
        }
650
      }
651
    }
652
474959
    Trace("datatypes-debug") << "Finished Merge " << t1 << " " << t2 << std::endl;
653
  }
654
}
655
656
21883
TheoryDatatypes::EqcInfo::EqcInfo( context::Context* c )
657
    : d_inst( c, false )
658
43766
    , d_constructor( c, Node::null() )
659
65649
    , d_selectors( c, false )
660
21883
{}
661
662
134795
bool TheoryDatatypes::hasLabel( EqcInfo* eqc, Node n ){
663
134795
  return ( eqc && !eqc->d_constructor.get().isNull() ) || !getLabel( n ).isNull();
664
}
665
666
648635
Node TheoryDatatypes::getLabel( Node n ) {
667
648635
  NodeUIntMap::iterator lbl_i = d_labels.find(n);
668
648635
  if( lbl_i != d_labels.end() ){
669
584501
    size_t n_lbl = (*lbl_i).second;
670
584501
    if( n_lbl>0 && d_labels_data[n][ n_lbl-1 ].getKind()!=kind::NOT ){
671
187160
      return d_labels_data[n][ n_lbl-1 ];
672
    }
673
  }
674
461475
  return Node::null();
675
}
676
677
1395015
int TheoryDatatypes::getLabelIndex( EqcInfo* eqc, Node n ){
678
1395015
  if( eqc && !eqc->d_constructor.get().isNull() ){
679
875278
    return utils::indexOf(eqc->d_constructor.get().getOperator());
680
  }else{
681
1039474
    Node lbl = getLabel( n );
682
519737
    if( lbl.isNull() ){
683
425325
      return -1;
684
    }else{
685
94412
      int tindex = utils::isTester(lbl);
686
188824
      Trace("datatypes-debug") << "Label of " << n << " is " << lbl
687
94412
                               << " with tindex " << tindex << std::endl;
688
94412
      Assert(tindex != -1);
689
94412
      return tindex;
690
    }
691
  }
692
}
693
694
20965
bool TheoryDatatypes::hasTester( Node n ) {
695
20965
  NodeUIntMap::iterator lbl_i = d_labels.find(n);
696
20965
  if( lbl_i != d_labels.end() ){
697
1
    return (*lbl_i).second>0;
698
  }else{
699
20964
    return false;
700
  }
701
}
702
703
76424
void TheoryDatatypes::getPossibleCons( EqcInfo* eqc, Node n, std::vector< bool >& pcons ){
704
152848
  TypeNode tn = n.getType();
705
76424
  const DType& dt = tn.getDType();
706
76424
  int lindex = getLabelIndex( eqc, n );
707
76424
  pcons.resize( dt.getNumConstructors(), lindex==-1 );
708
76424
  if( lindex!=-1 ){
709
    pcons[ lindex ] = true;
710
  }else{
711
76424
    NodeUIntMap::iterator lbl_i = d_labels.find(n);
712
76424
    if( lbl_i != d_labels.end() ){
713
44357
      size_t n_lbl = (*lbl_i).second;
714
233421
      for (size_t i = 0; i < n_lbl; i++)
715
      {
716
189064
        Assert(d_labels_data[n][i].getKind() == NOT);
717
189064
        unsigned tindex = d_labels_tindex[n][i];
718
189064
        pcons[ tindex ] = false;
719
      }
720
    }
721
  }
722
76424
}
723
724
103673
Node TheoryDatatypes::getTermSkolemFor( Node n ) {
725
103673
  if( n.getKind()==APPLY_CONSTRUCTOR ){
726
11273
    NodeMap::const_iterator it = d_term_sk.find( n );
727
11273
    if( it==d_term_sk.end() ){
728
989
      NodeManager* nm = NodeManager::currentNM();
729
989
      SkolemManager* sm = nm->getSkolemManager();
730
      //add purification unit lemma ( k = n )
731
1978
      Node k = sm->mkPurifySkolem(n, "kdt");
732
989
      d_term_sk[n] = k;
733
1978
      Node eq = k.eqNode( n );
734
989
      Trace("datatypes-infer") << "DtInfer : ref : " << eq << std::endl;
735
989
      d_im.addPendingLemma(eq, InferenceId::DATATYPES_PURIFY);
736
989
      return k;
737
    }else{
738
10284
      return (*it).second;
739
    }
740
  }else{
741
92400
    return n;
742
  }
743
}
744
745
1140685
void TheoryDatatypes::addTester(
746
    unsigned ttindex, Node t, EqcInfo* eqc, Node n, Node t_arg)
747
{
748
1140685
  Trace("datatypes-debug") << "Add tester : " << t << " to eqc(" << n << ")" << std::endl;
749
1140685
  Debug("datatypes-labels") << "Add tester " << t << " " << n << " " << eqc << std::endl;
750
1140685
  bool tpolarity = t.getKind()!=NOT;
751
1140685
  Assert((tpolarity ? t : t[0]).getKind() == APPLY_TESTER);
752
1425332
  Node j, jt;
753
1140685
  bool makeConflict = false;
754
1140685
  int prevTIndex = getLabelIndex(eqc, n);
755
1140685
  if (prevTIndex >= 0)
756
  {
757
791931
    unsigned ptu = static_cast<unsigned>(prevTIndex);
758
    //if we already know the constructor type, check whether it is in conflict or redundant
759
791931
    if ((ptu == ttindex) != tpolarity)
760
    {
761
1325
      if( !eqc->d_constructor.get().isNull() ){
762
        //conflict because equivalence class contains a constructor
763
2650
        std::vector<Node> conf;
764
1325
        conf.push_back(t);
765
1325
        conf.push_back(t_arg.eqNode(eqc->d_constructor.get()));
766
2650
        Trace("dt-conflict")
767
1325
            << "CONFLICT: Tester eq conflict " << conf << std::endl;
768
1325
        d_im.sendDtConflict(conf, InferenceId::DATATYPES_TESTER_CONFLICT);
769
1325
        return;
770
      }else{
771
        makeConflict = true;
772
        //conflict because the existing label is contradictory
773
        j = getLabel( n );
774
        jt = j;
775
      }
776
    }else{
777
790606
      return;
778
    }
779
  }else{
780
    //otherwise, scan list of labels
781
348754
    NodeUIntMap::iterator lbl_i = d_labels.find(n);
782
348754
    Assert(lbl_i != d_labels.end());
783
348754
    size_t n_lbl = (*lbl_i).second;
784
633401
    std::map< int, bool > neg_testers;
785
1321608
    for (size_t i = 0; i < n_lbl; i++)
786
    {
787
996774
      Assert(d_labels_data[n][i].getKind() == NOT);
788
996774
      unsigned jtindex = d_labels_tindex[n][i];
789
996774
      if( jtindex==ttindex ){
790
23920
        if( tpolarity ){  //we are in conflict
791
86
          j = d_labels_data[n][i];
792
86
          jt = j[0];
793
86
          makeConflict = true;
794
86
          break;
795
        }else{            //it is redundant
796
23834
          return;
797
        }
798
      }else{
799
972854
        neg_testers[jtindex] = true;
800
      }
801
    }
802
324920
    if( !makeConflict ){
803
324834
      Debug("datatypes-labels") << "Add to labels " << t << std::endl;
804
324834
      d_labels[n] = n_lbl + 1;
805
324834
      if (n_lbl < d_labels_data[n].size())
806
      {
807
        // reuse spot in the vector
808
315270
        d_labels_data[n][n_lbl] = t;
809
315270
        d_labels_args[n][n_lbl] = t_arg;
810
315270
        d_labels_tindex[n][n_lbl] = ttindex;
811
      }else{
812
9564
        d_labels_data[n].push_back(t);
813
9564
        d_labels_args[n].push_back(t_arg);
814
9564
        d_labels_tindex[n].push_back(ttindex);
815
      }
816
324834
      n_lbl++;
817
818
324834
      const DType& dt = t_arg.getType().getDType();
819
324834
      Debug("datatypes-labels") << "Labels at " << n_lbl << " / " << dt.getNumConstructors() << std::endl;
820
324834
      if( tpolarity ){
821
92400
        instantiate( eqc, n );
822
        // We could propagate is-C1(x) => not is-C2(x) here for all other
823
        // constructors, but empirically this hurts performance.
824
      }else{
825
        //check if we have reached the maximum number of testers
826
        // in this case, add the positive tester
827
232434
        if (n_lbl == dt.getNumConstructors() - 1)
828
        {
829
80546
          std::vector< bool > pcons;
830
40273
          getPossibleCons( eqc, n, pcons );
831
40273
          int testerIndex = -1;
832
122502
          for( unsigned i=0; i<pcons.size(); i++ ) {
833
122502
            if( pcons[i] ){
834
40273
              testerIndex = i;
835
40273
              break;
836
            }
837
          }
838
40273
          Assert(testerIndex != -1);
839
          //we must explain why each term in the set of testers for this equivalence class is equal
840
80546
          std::vector< Node > eq_terms;
841
80546
          NodeBuilder nb(kind::AND);
842
228189
          for (unsigned i = 0; i < n_lbl; i++)
843
          {
844
375832
            Node ti = d_labels_data[n][i];
845
187916
            nb << ti;
846
187916
            Assert(ti.getKind() == NOT);
847
375832
            Node t_arg2 = d_labels_args[n][i];
848
187916
            if( std::find( eq_terms.begin(), eq_terms.end(), t_arg2 )==eq_terms.end() ){
849
44968
              eq_terms.push_back( t_arg2 );
850
44968
              if( t_arg2!=t_arg ){
851
4695
                nb << t_arg2.eqNode( t_arg );
852
              }
853
            }
854
          }
855
          Node t_concl = testerIndex == -1
856
                             ? NodeManager::currentNM()->mkConst(false)
857
80546
                             : utils::mkTester(t_arg, testerIndex, dt);
858
80546
          Node t_concl_exp = ( nb.getNumChildren() == 1 ) ? nb.getChild( 0 ) : nb;
859
40273
          d_im.addPendingInference(
860
              t_concl, InferenceId::DATATYPES_LABEL_EXH, t_concl_exp);
861
40273
          Trace("datatypes-infer") << "DtInfer : label : " << t_concl << " by " << t_concl_exp << std::endl;
862
40273
          return;
863
        }
864
      }
865
    }
866
  }
867
284647
  if( makeConflict ){
868
86
    Debug("datatypes-labels") << "Explain " << j << " " << t << std::endl;
869
172
    std::vector<Node> conf;
870
86
    conf.push_back(j);
871
86
    conf.push_back(t);
872
86
    conf.push_back(jt[0].eqNode(t_arg));
873
86
    Trace("dt-conflict") << "CONFLICT: Tester conflict : " << conf << std::endl;
874
86
    d_im.sendDtConflict(conf, InferenceId::DATATYPES_TESTER_MERGE_CONFLICT);
875
  }
876
}
877
878
499730
void TheoryDatatypes::addSelector( Node s, EqcInfo* eqc, Node n, bool assertFacts ) {
879
499730
  Trace("dt-collapse-sel") << "Add selector : " << s << " to eqc(" << n << ")" << std::endl;
880
  //check to see if it is redundant
881
499730
  NodeUIntMap::iterator sel_i = d_selector_apps.find(n);
882
499730
  Assert(sel_i != d_selector_apps.end());
883
499730
  if( sel_i != d_selector_apps.end() ){
884
499730
    size_t n_sel = (*sel_i).second;
885
937547
    for (size_t j = 0; j < n_sel; j++)
886
    {
887
1203891
      Node ss = d_selector_apps_data[n][j];
888
766074
      if( s.getOperator()==ss.getOperator() && ( s.getKind()!=DT_HEIGHT_BOUND || s[1]==ss[1] ) ){
889
328257
        Trace("dt-collapse-sel") << "...redundant." << std::endl;
890
328257
        return;
891
      }
892
    }
893
    //add it to the vector
894
    //sel->push_back( s );
895
171473
    d_selector_apps[n] = n_sel + 1;
896
171473
    if (n_sel < d_selector_apps_data[n].size())
897
    {
898
157129
      d_selector_apps_data[n][n_sel] = s;
899
    }else{
900
14344
      d_selector_apps_data[n].push_back( s );
901
    }
902
903
171473
    eqc->d_selectors = true;
904
  }
905
171473
  if( assertFacts && !eqc->d_constructor.get().isNull() ){
906
    //conclude the collapsed merge
907
130763
    collapseSelector( s, eqc->d_constructor.get() );
908
  }
909
}
910
911
34578
void TheoryDatatypes::addConstructor( Node c, EqcInfo* eqc, Node n ){
912
34578
  Trace("datatypes-debug") << "Add constructor : " << c << " to eqc(" << n << ")" << std::endl;
913
34578
  Assert(eqc->d_constructor.get().isNull());
914
  //check labels
915
34578
  NodeUIntMap::iterator lbl_i = d_labels.find(n);
916
34578
  if( lbl_i != d_labels.end() ){
917
34578
    size_t constructorIndex = utils::indexOf(c.getOperator());
918
34578
    size_t n_lbl = (*lbl_i).second;
919
167572
    for (size_t i = 0; i < n_lbl; i++)
920
    {
921
265992
      Node t = d_labels_data[n][i];
922
132998
      if (d_labels_data[n][i].getKind() == NOT)
923
      {
924
99697
        unsigned tindex = d_labels_tindex[n][i];
925
99697
        if (tindex == constructorIndex)
926
        {
927
8
          std::vector<Node> conf;
928
4
          conf.push_back(t);
929
4
          conf.push_back(t[0][0].eqNode(c));
930
8
          Trace("dt-conflict")
931
4
              << "CONFLICT: Tester merge eq conflict : " << conf << std::endl;
932
4
          d_im.sendDtConflict(conf, InferenceId::DATATYPES_TESTER_CONFLICT);
933
4
          return;
934
        }
935
      }
936
    }
937
  }
938
  //check selectors
939
34574
  NodeUIntMap::iterator sel_i = d_selector_apps.find(n);
940
34574
  if( sel_i != d_selector_apps.end() ){
941
34574
    size_t n_sel = (*sel_i).second;
942
141691
    for (size_t j = 0; j < n_sel; j++)
943
    {
944
214234
      Node s = d_selector_apps_data[n][j];
945
      //collapse the selector
946
107117
      collapseSelector( s, c );
947
    }
948
  }
949
34574
  eqc->d_constructor.set( c );
950
}
951
952
237880
void TheoryDatatypes::collapseSelector( Node s, Node c ) {
953
237880
  Assert(c.getKind() == APPLY_CONSTRUCTOR);
954
237880
  Trace("dt-collapse-sel") << "collapse selector : " << s << " " << c << std::endl;
955
475760
  Node r;
956
237880
  bool wrong = false;
957
475760
  Node eq_exp = s[0].eqNode(c);
958
237880
  if( s.getKind()==kind::APPLY_SELECTOR_TOTAL ){
959
349382
    Node selector = s.getOperator();
960
174691
    size_t constructorIndex = utils::indexOf(c.getOperator());
961
174691
    const DType& dt = utils::datatypeOf(selector);
962
174691
    const DTypeConstructor& dtc = dt[constructorIndex];
963
174691
    int selectorIndex = dtc.getSelectorIndexInternal(selector);
964
174691
    wrong = selectorIndex<0;
965
174691
    r = NodeManager::currentNM()->mkNode( kind::APPLY_SELECTOR_TOTAL, s.getOperator(), c );
966
  }
967
237880
  if( !r.isNull() ){
968
349382
    Node rrs;
969
174691
    if (wrong)
970
    {
971
      // Must use make ground term here instead of the rewriter, since we
972
      // do not want to introduce arbitrary values. This is important so that
973
      // we avoid constants for types that are not "closed enumerable", e.g.
974
      // uninterpreted sorts and arrays, where the solver does not fully
975
      // handle values of the sort. The call to mkGroundTerm does not introduce
976
      // values for these sorts.
977
90004
      rrs = r.getType().mkGroundTerm();
978
180008
      Trace("datatypes-wrong-sel")
979
90004
          << "Bad apply " << r << " term = " << rrs
980
90004
          << ", value = " << r.getType().mkGroundValue() << std::endl;
981
    }
982
    else
983
    {
984
84687
      rrs = Rewriter::rewrite(r);
985
    }
986
174691
    if (s != rrs)
987
    {
988
228588
      Node eq = s.eqNode(rrs);
989
      // Since collapsing selectors may generate new terms, we must send
990
      // this out as a lemma if it is of an external type, or otherwise we
991
      // may ask for the equality status of terms that only datatypes knows
992
      // about, see issue #5344.
993
114294
      bool forceLemma = !s.getType().isDatatype();
994
114294
      Trace("datatypes-infer") << "DtInfer : collapse sel";
995
114294
      Trace("datatypes-infer") << " : " << eq << " by " << eq_exp << std::endl;
996
114294
      d_im.addPendingInference(
997
          eq, InferenceId::DATATYPES_COLLAPSE_SEL, eq_exp, forceLemma);
998
    }
999
  }
1000
237880
}
1001
1002
105478
EqualityStatus TheoryDatatypes::getEqualityStatus(TNode a, TNode b){
1003
105478
  Assert(d_equalityEngine->hasTerm(a) && d_equalityEngine->hasTerm(b));
1004
105478
  if (d_equalityEngine->areEqual(a, b))
1005
  {
1006
    // The terms are implied to be equal
1007
8211
    return EQUALITY_TRUE;
1008
  }
1009
97267
  if (d_equalityEngine->areDisequal(a, b, false))
1010
  {
1011
    // The terms are implied to be dis-equal
1012
    return EQUALITY_FALSE;
1013
  }
1014
97267
  return EQUALITY_FALSE_IN_MODEL;
1015
}
1016
1017
82670
void TheoryDatatypes::addCarePairs(TNodeTrie* t1,
1018
                                   TNodeTrie* t2,
1019
                                   unsigned arity,
1020
                                   unsigned depth,
1021
                                   unsigned& n_pairs)
1022
{
1023
82670
  if( depth==arity ){
1024
9967
    if( t2!=NULL ){
1025
19934
      Node f1 = t1->getData();
1026
19934
      Node f2 = t2->getData();
1027
9967
      if( !areEqual( f1, f2 ) ){
1028
9967
        Trace("dt-cg") << "Check " << f1 << " and " << f2 << std::endl;
1029
19934
        vector< pair<TNode, TNode> > currentPairs;
1030
30024
        for (unsigned k = 0; k < f1.getNumChildren(); ++ k) {
1031
40114
          TNode x = f1[k];
1032
40114
          TNode y = f2[k];
1033
20057
          Assert(d_equalityEngine->hasTerm(x));
1034
20057
          Assert(d_equalityEngine->hasTerm(y));
1035
20057
          Assert(!areDisequal(x, y));
1036
20057
          Assert(!areCareDisequal(x, y));
1037
20057
          if (!d_equalityEngine->areEqual(x, y))
1038
          {
1039
11532
            Trace("dt-cg") << "Arg #" << k << " is " << x << " " << y << std::endl;
1040
34596
            if (d_equalityEngine->isTriggerTerm(x, THEORY_DATATYPES)
1041
34596
                && d_equalityEngine->isTriggerTerm(y, THEORY_DATATYPES))
1042
            {
1043
1658
              TNode x_shared = d_equalityEngine->getTriggerTermRepresentative(
1044
3316
                  x, THEORY_DATATYPES);
1045
1658
              TNode y_shared = d_equalityEngine->getTriggerTermRepresentative(
1046
3316
                  y, THEORY_DATATYPES);
1047
1658
              currentPairs.push_back(make_pair(x_shared, y_shared));
1048
            }
1049
          }
1050
        }
1051
11625
        for (unsigned c = 0; c < currentPairs.size(); ++ c) {
1052
1658
          Trace("dt-cg-pair") << "Pair : " << currentPairs[c].first << " " << currentPairs[c].second << std::endl;
1053
1658
          addCarePair(currentPairs[c].first, currentPairs[c].second);
1054
1658
          n_pairs++;
1055
        }
1056
      }
1057
    }
1058
  }else{
1059
72703
    if( t2==NULL ){
1060
65796
      if( depth<(arity-1) ){
1061
        //add care pairs internal to each child
1062
27975
        for (std::pair<const TNode, TNodeTrie>& tt : t1->d_data)
1063
        {
1064
16679
          addCarePairs(&tt.second, nullptr, arity, depth + 1, n_pairs);
1065
        }
1066
      }
1067
      //add care pairs based on each pair of non-disequal arguments
1068
173201
      for (std::map<TNode, TNodeTrie>::iterator it = t1->d_data.begin();
1069
173201
           it != t1->d_data.end();
1070
           ++it)
1071
      {
1072
107405
        std::map<TNode, TNodeTrie>::iterator it2 = it;
1073
107405
        ++it2;
1074
298021
        for( ; it2 != t1->d_data.end(); ++it2 ){
1075
95308
          if (!d_equalityEngine->areDisequal(it->first, it2->first, false))
1076
          {
1077
45422
            if( !areCareDisequal(it->first, it2->first) ){
1078
10243
              addCarePairs( &it->second, &it2->second, arity, depth+1, n_pairs );
1079
            }
1080
          }
1081
        }
1082
      }
1083
    }else{
1084
      //add care pairs based on product of indices, non-disequal arguments
1085
19731
      for (std::pair<const TNode, TNodeTrie>& tt1 : t1->d_data)
1086
      {
1087
29463
        for (std::pair<const TNode, TNodeTrie>& tt2 : t2->d_data)
1088
        {
1089
16639
          if (!d_equalityEngine->areDisequal(tt1.first, tt2.first, false))
1090
          {
1091
13658
            if (!areCareDisequal(tt1.first, tt2.first))
1092
            {
1093
6631
              addCarePairs(&tt1.second, &tt2.second, arity, depth + 1, n_pairs);
1094
            }
1095
          }
1096
        }
1097
      }
1098
    }
1099
  }
1100
82670
}
1101
1102
10643
void TheoryDatatypes::computeCareGraph(){
1103
10643
  unsigned n_pairs = 0;
1104
10643
  Trace("dt-cg-summary") << "Compute graph for dt..." << d_functionTerms.size() << " " << d_sharedTerms.size() << std::endl;
1105
10643
  Trace("dt-cg") << "Build indices..." << std::endl;
1106
21286
  std::map<TypeNode, std::map<Node, TNodeTrie> > index;
1107
21286
  std::map< Node, unsigned > arity;
1108
  //populate indices
1109
10643
  unsigned functionTerms = d_functionTerms.size();
1110
236659
  for( unsigned i=0; i<functionTerms; i++ ){
1111
452032
    TNode f1 = d_functionTerms[i];
1112
226016
    Assert(d_equalityEngine->hasTerm(f1));
1113
226016
    Trace("dt-cg-debug") << "...build for " << f1 << std::endl;
1114
    //break into index based on operator, and type of first argument (since some operators are parametric)
1115
452032
    Node op = f1.getOperator();
1116
452032
    TypeNode tn = f1[0].getType();
1117
452032
    std::vector< TNode > reps;
1118
226016
    bool has_trigger_arg = false;
1119
484424
    for( unsigned j=0; j<f1.getNumChildren(); j++ ){
1120
258408
      reps.push_back(d_equalityEngine->getRepresentative(f1[j]));
1121
258408
      if (d_equalityEngine->isTriggerTerm(f1[j], THEORY_DATATYPES))
1122
      {
1123
216755
        has_trigger_arg = true;
1124
      }
1125
    }
1126
    //only may contribute to care pairs if has at least one trigger argument
1127
226016
    if( has_trigger_arg ){
1128
193744
      index[tn][op].addTerm( f1, reps );
1129
193744
      arity[op] = reps.size();
1130
    }
1131
  }
1132
  //for each index
1133
27052
  for (std::pair<const TypeNode, std::map<Node, TNodeTrie> >& tt : index)
1134
  {
1135
65526
    for (std::pair<const Node, TNodeTrie>& t : tt.second)
1136
    {
1137
98234
      Trace("dt-cg") << "Process index " << tt.first << ", " << t.first << "..."
1138
49117
                     << std::endl;
1139
49117
      addCarePairs(&t.second, nullptr, arity[t.first], 0, n_pairs);
1140
    }
1141
  }
1142
10643
  Trace("dt-cg-summary") << "...done, # pairs = " << n_pairs << std::endl;
1143
10643
}
1144
1145
9089
bool TheoryDatatypes::collectModelValues(TheoryModel* m,
1146
                                         const std::set<Node>& termSet)
1147
{
1148
18178
  Trace("dt-cmi") << "Datatypes : Collect model values "
1149
9089
                  << d_equalityEngine->consistent() << std::endl;
1150
9089
  Trace("dt-model") << std::endl;
1151
9089
  printModelDebug( "dt-model" );
1152
9089
  Trace("dt-model") << std::endl;
1153
1154
  //get all constructors
1155
9089
  eq::EqClassesIterator eqccs_i = eq::EqClassesIterator(d_equalityEngine);
1156
18178
  std::vector< Node > cons;
1157
18178
  std::vector< Node > nodes;
1158
18178
  std::map< Node, Node > eqc_cons;
1159
573967
  while( !eqccs_i.isFinished() ){
1160
564878
    Node eqc = (*eqccs_i);
1161
    //for all equivalence classes that are datatypes
1162
    //if( termSet.find( eqc )==termSet.end() ){
1163
    //  Trace("dt-cmi-debug") << "Irrelevant eqc : " << eqc << std::endl;
1164
    //}
1165
282439
    if( eqc.getType().isDatatype() ){
1166
64384
      EqcInfo* ei = getOrMakeEqcInfo( eqc );
1167
64384
      if( ei && !ei->d_constructor.get().isNull() ){
1168
86838
        Node c = ei->d_constructor.get();
1169
43419
        cons.push_back( c );
1170
43419
        eqc_cons[ eqc ] = c;
1171
      }else{
1172
        //if eqc contains a symbol known to datatypes (a selector), then we must assign
1173
        //should assign constructors to EQC if they have a selector or a tester
1174
20965
        bool shouldConsider = ( ei && ei->d_selectors ) || hasTester( eqc );
1175
20965
        if( shouldConsider ){
1176
1
          nodes.push_back( eqc );
1177
        }
1178
      }
1179
    }
1180
    //}
1181
282439
    ++eqccs_i;
1182
  }
1183
1184
  //unsigned orig_size = nodes.size();
1185
18178
  std::map< TypeNode, int > typ_enum_map;
1186
18178
  std::vector< TypeEnumerator > typ_enum;
1187
9089
  unsigned index = 0;
1188
9091
  while( index<nodes.size() ){
1189
2
    Node eqc = nodes[index];
1190
2
    Node neqc;
1191
1
    bool addCons = false;
1192
2
    TypeNode tt = eqc.getType();
1193
1
    const DType& dt = tt.getDType();
1194
1
    if (!d_equalityEngine->hasTerm(eqc))
1195
    {
1196
      Assert(false);
1197
    }else{
1198
1
      Trace("dt-cmi") << "NOTICE : Datatypes: no constructor in equivalence class " << eqc << std::endl;
1199
1
      Trace("dt-cmi") << "   Type : " << eqc.getType() << std::endl;
1200
1
      EqcInfo* ei = getOrMakeEqcInfo( eqc );
1201
2
      std::vector< bool > pcons;
1202
1
      getPossibleCons( ei, eqc, pcons );
1203
1
      Trace("dt-cmi") << "Possible constructors : ";
1204
8
      for( unsigned i=0; i<pcons.size(); i++ ){
1205
7
        Trace("dt-cmi") << pcons[i] << " ";
1206
      }
1207
1
      Trace("dt-cmi") << std::endl;
1208
3
      for( unsigned r=0; r<2; r++ ){
1209
2
        if( neqc.isNull() ){
1210
5
          for( unsigned i=0; i<pcons.size(); i++ ){
1211
            // must try the infinite ones first
1212
            bool cfinite =
1213
5
                d_state.isFiniteType(dt[i].getSpecializedConstructorType(tt));
1214
5
            if( pcons[i] && (r==1)==cfinite ){
1215
1
              neqc = utils::getInstCons(eqc, dt, i);
1216
1
              break;
1217
            }
1218
          }
1219
        }
1220
      }
1221
1
      addCons = true;
1222
    }
1223
1
    if( !neqc.isNull() ){
1224
1
      Trace("dt-cmi") << "Assign : " << neqc << std::endl;
1225
1
      if (!m->assertEquality(eqc, neqc, true))
1226
      {
1227
        return false;
1228
      }
1229
1
      eqc_cons[ eqc ] = neqc;
1230
    }
1231
1
    if( addCons ){
1232
1
      cons.push_back( neqc );
1233
    }
1234
1
    ++index;
1235
  }
1236
1237
52525
  for( std::map< Node, Node >::iterator it = eqc_cons.begin(); it != eqc_cons.end(); ++it ){
1238
86872
    Node eqc = it->first;
1239
43436
    if( eqc.getType().isCodatatype() ){
1240
      //until models are implemented for codatatypes
1241
      //throw Exception("Models for codatatypes are not supported in this version.");
1242
      //must proactive expand to avoid looping behavior in model builder
1243
75
      if( !it->second.isNull() ){
1244
124
        std::map< Node, int > vmap;
1245
124
        Node v = getCodatatypesValue( it->first, eqc_cons, vmap, 0 );
1246
62
        Trace("dt-cmi") << "  EQC(" << it->first << "), constructor is " << it->second << ", value is " << v << ", const = " << v.isConst() << std::endl;
1247
62
        if (!m->assertEquality(eqc, v, true))
1248
        {
1249
          return false;
1250
        }
1251
62
        m->assertSkeleton(v);
1252
      }
1253
    }else{
1254
43361
      Trace("dt-cmi") << "Datatypes : assert representative " << it->second << " for " << it->first << std::endl;
1255
43361
      m->assertSkeleton(it->second);
1256
    }
1257
  }
1258
9089
  return true;
1259
}
1260
1261
1262
199
Node TheoryDatatypes::getCodatatypesValue( Node n, std::map< Node, Node >& eqc_cons, std::map< Node, int >& vmap, int depth ){
1263
199
  std::map< Node, int >::iterator itv = vmap.find( n );
1264
199
  if( itv!=vmap.end() ){
1265
6
    int debruijn = depth - 1 - itv->second;
1266
    return NodeManager::currentNM()->mkConst(
1267
6
        UninterpretedConstant(n.getType(), debruijn));
1268
193
  }else if( n.getType().isDatatype() ){
1269
173
    Node nc = eqc_cons[n];
1270
139
    if( !nc.isNull() ){
1271
105
      vmap[n] = depth;
1272
105
      Trace("dt-cmi-cdt-debug") << "    map " << n << " -> " << depth << std::endl;
1273
105
      Assert(nc.getKind() == APPLY_CONSTRUCTOR);
1274
210
      std::vector< Node > children;
1275
105
      children.push_back( nc.getOperator() );
1276
242
      for( unsigned i=0; i<nc.getNumChildren(); i++ ){
1277
274
        Node r = getRepresentative( nc[i] );
1278
274
        Node rv = getCodatatypesValue( r, eqc_cons, vmap, depth+1 );
1279
137
        children.push_back( rv );
1280
      }
1281
105
      vmap.erase( n );
1282
105
      return NodeManager::currentNM()->mkNode( APPLY_CONSTRUCTOR, children );
1283
    }
1284
  }
1285
88
  return n;
1286
}
1287
1288
Node TheoryDatatypes::getSingletonLemma( TypeNode tn, bool pol ) {
1289
  NodeManager* nm = NodeManager::currentNM();
1290
  SkolemManager* sm = nm->getSkolemManager();
1291
  int index = pol ? 0 : 1;
1292
  std::map< TypeNode, Node >::iterator it = d_singleton_lemma[index].find( tn );
1293
  if( it==d_singleton_lemma[index].end() ){
1294
    Node a;
1295
    if( pol ){
1296
      Node v1 = nm->mkBoundVar(tn);
1297
      Node v2 = nm->mkBoundVar(tn);
1298
      a = nm->mkNode(FORALL, nm->mkNode(BOUND_VAR_LIST, v1, v2), v1.eqNode(v2));
1299
    }else{
1300
      Node v1 = sm->mkDummySkolem("k1", tn);
1301
      Node v2 = sm->mkDummySkolem("k2", tn);
1302
      a = v1.eqNode( v2 ).negate();
1303
      //send out immediately as lemma
1304
      d_im.lemma(a, InferenceId::DATATYPES_REC_SINGLETON_FORCE_DEQ);
1305
      Trace("dt-singleton") << "******** assert " << a << " to avoid singleton cardinality for type " << tn << std::endl;
1306
    }
1307
    d_singleton_lemma[index][tn] = a;
1308
    return a;
1309
  }else{
1310
    return it->second;
1311
  }
1312
}
1313
1314
242066
void TheoryDatatypes::collectTerms( Node n ) {
1315
242066
  if (d_collectTermsCache.find(n) != d_collectTermsCache.end())
1316
  {
1317
    // already processed
1318
31940
    return;
1319
  }
1320
210126
  d_collectTermsCache[n] = true;
1321
210126
  Kind nk = n.getKind();
1322
210126
  if (nk == APPLY_CONSTRUCTOR)
1323
  {
1324
91356
    Debug("datatypes") << "  Found constructor " << n << endl;
1325
91356
    if (n.getNumChildren() > 0)
1326
    {
1327
62705
      d_functionTerms.push_back(n);
1328
    }
1329
91356
    return;
1330
  }
1331
118770
  if (nk == APPLY_SELECTOR_TOTAL || nk == DT_SIZE || nk == DT_HEIGHT_BOUND)
1332
  {
1333
18521
    d_functionTerms.push_back(n);
1334
    // we must also record which selectors exist
1335
18521
    Trace("dt-collapse-sel") << "  Found selector " << n << endl;
1336
37042
    Node rep = getRepresentative(n[0]);
1337
    // record it in the selectors
1338
18521
    EqcInfo* eqc = getOrMakeEqcInfo(rep, true);
1339
    // add it to the eqc info
1340
18521
    addSelector(n, eqc, rep);
1341
  }
1342
1343
  // now, do user-context-dependent lemmas
1344
118770
  if (nk != DT_SIZE && nk != DT_HEIGHT_BOUND)
1345
  {
1346
    // if not one of these kinds, there are no lemmas
1347
114333
    return;
1348
  }
1349
4437
  if (d_collectTermsCacheU.find(n) != d_collectTermsCacheU.end())
1350
  {
1351
2115
    return;
1352
  }
1353
2322
  d_collectTermsCacheU[n] = true;
1354
1355
2322
  NodeManager* nm = NodeManager::currentNM();
1356
1357
2322
  if (nk == DT_SIZE)
1358
  {
1359
4644
    Node lem = nm->mkNode(LEQ, d_zero, n);
1360
4644
    Trace("datatypes-infer")
1361
2322
        << "DtInfer : size geq zero : " << lem << std::endl;
1362
2322
    d_im.addPendingLemma(lem, InferenceId::DATATYPES_SIZE_POS);
1363
  }
1364
  else if (nk == DT_HEIGHT_BOUND && n[1].getConst<Rational>().isZero())
1365
  {
1366
    std::vector<Node> children;
1367
    const DType& dt = n[0].getType().getDType();
1368
    for (unsigned i = 0, ncons = dt.getNumConstructors(); i < ncons; i++)
1369
    {
1370
      if (utils::isNullaryConstructor(dt[i]))
1371
      {
1372
        Node test = utils::mkTester(n[0], i, dt);
1373
        children.push_back(test);
1374
      }
1375
    }
1376
    Node lem;
1377
    if (children.empty())
1378
    {
1379
      lem = n.negate();
1380
    }
1381
    else
1382
    {
1383
      lem = n.eqNode(children.size() == 1 ? children[0]
1384
                                          : nm->mkNode(OR, children));
1385
    }
1386
    Trace("datatypes-infer") << "DtInfer : zero height : " << lem << std::endl;
1387
    d_im.addPendingLemma(lem, InferenceId::DATATYPES_HEIGHT_ZERO);
1388
  }
1389
}
1390
1391
115293
Node TheoryDatatypes::getInstantiateCons(Node n, const DType& dt, int index)
1392
{
1393
115293
  if( n.getKind()==APPLY_CONSTRUCTOR && n.getNumChildren()==0 ){
1394
11620
    return n;
1395
  }
1396
  //add constructor to equivalence class
1397
207346
  Node k = getTermSkolemFor( n );
1398
207346
  Node n_ic = utils::getInstCons(k, dt, index);
1399
103673
  n_ic = Rewriter::rewrite( n_ic );
1400
  // it may be a new term, so we collect terms and add it to the equality engine
1401
103673
  collectTerms( n_ic );
1402
103673
  d_equalityEngine->addTerm(n_ic);
1403
103673
  Debug("dt-enum") << "Made instantiate cons " << n_ic << std::endl;
1404
103673
  return n_ic;
1405
}
1406
1407
177906
void TheoryDatatypes::instantiate( EqcInfo* eqc, Node n ){
1408
177906
  Trace("datatypes-debug") << "Instantiate: " << n << std::endl;
1409
  //add constructor to equivalence class if not done so already
1410
177906
  int index = getLabelIndex( eqc, n );
1411
177906
  if (index == -1 || eqc->d_inst)
1412
  {
1413
136846
    return;
1414
  }
1415
218966
  Node exp;
1416
218966
  Node tt;
1417
115293
  if (!eqc->d_constructor.get().isNull())
1418
  {
1419
22893
    exp = d_true;
1420
22893
    tt = eqc->d_constructor;
1421
  }
1422
  else
1423
  {
1424
92400
    exp = getLabel(n);
1425
92400
    tt = exp[0];
1426
  }
1427
218966
  TypeNode ttn = tt.getType();
1428
115293
  const DType& dt = ttn.getDType();
1429
  // instantiate this equivalence class
1430
115293
  eqc->d_inst = true;
1431
218966
  Node tt_cons = getInstantiateCons(tt, dt, index);
1432
218966
  Node eq;
1433
115293
  if (tt == tt_cons)
1434
  {
1435
11620
    return;
1436
  }
1437
103673
  eq = tt.eqNode(tt_cons);
1438
  // Determine if the equality must be sent out as a lemma. Notice that
1439
  // we  keep new equalities from the instantiate rule internal
1440
  // as long as they are for datatype constructors that have no arguments that
1441
  // have finite external type, which corresponds to:
1442
  //   forceLemma = dt[index].hasFiniteExternalArgType(ttn);
1443
  // Such equalities must be sent because they introduce selector terms that
1444
  // may contribute to conflicts due to cardinality (good examples of this are
1445
  // regress0/datatypes/dt-param-card4-bool-sat.smt2 and
1446
  // regress0/datatypes/list-bool.smt2).
1447
  bool forceLemma;
1448
207346
  if (options::dtPoliteOptimize())
1449
  {
1450
103673
    forceLemma = dt[index].hasFiniteExternalArgType(ttn);
1451
  }
1452
  else
1453
  {
1454
    forceLemma = dt.involvesExternalType();
1455
  }
1456
207346
  Trace("datatypes-infer-debug") << "DtInstantiate : " << eqc << " " << eq
1457
103673
                                 << " forceLemma = " << forceLemma << std::endl;
1458
103673
  d_im.addPendingInference(eq, InferenceId::DATATYPES_INST, exp, forceLemma);
1459
207346
  Trace("datatypes-infer") << "DtInfer : instantiate : " << eq << " by " << exp
1460
103673
                           << std::endl;
1461
}
1462
1463
23317
void TheoryDatatypes::checkCycles() {
1464
23317
  Trace("datatypes-cycle-check") << "Check acyclicity" << std::endl;
1465
46222
  std::vector< Node > cdt_eqc;
1466
23317
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(d_equalityEngine);
1467
1090747
  while( !eqcs_i.isFinished() ){
1468
1067842
    Node eqc = (*eqcs_i);
1469
1067842
    TypeNode tn = eqc.getType();
1470
534127
    if( tn.isDatatype() ) {
1471
191538
      if( !tn.isCodatatype() ){
1472
380442
        if( options::dtCyclic() ){
1473
          //do cycle checks
1474
380030
          std::map< TNode, bool > visited;
1475
380030
          std::map< TNode, bool > proc;
1476
380030
          std::vector<Node> expl;
1477
190221
          Trace("datatypes-cycle-check") << "...search for cycle starting at " << eqc << std::endl;
1478
380030
          Node cn = searchForCycle( eqc, eqc, visited, proc, expl );
1479
190221
          Trace("datatypes-cycle-check") << "...finish." << std::endl;
1480
          //if we discovered a different cycle while searching this one
1481
190221
          if( !cn.isNull() && cn!=eqc ){
1482
22
            visited.clear();
1483
22
            proc.clear();
1484
22
            expl.clear();
1485
44
            Node prev = cn;
1486
22
            cn = searchForCycle( cn, cn, visited, proc, expl );
1487
22
            Assert(prev == cn);
1488
          }
1489
1490
190221
          if( !cn.isNull() ) {
1491
412
            Assert(expl.size() > 0);
1492
824
            Trace("dt-conflict")
1493
412
                << "CONFLICT: Cycle conflict : " << expl << std::endl;
1494
412
            d_im.sendDtConflict(expl, InferenceId::DATATYPES_CYCLE);
1495
412
            return;
1496
          }
1497
        }
1498
      }else{
1499
        //indexing
1500
1317
        cdt_eqc.push_back( eqc );
1501
      }
1502
    }
1503
533715
    ++eqcs_i;
1504
  }
1505
22905
  Trace("datatypes-cycle-check") << "Check uniqueness" << std::endl;
1506
  //process codatatypes
1507
22991
  if( cdt_eqc.size()>1 && options::cdtBisimilar() ){
1508
86
    printModelDebug("dt-cdt-debug");
1509
86
    Trace("dt-cdt-debug") << "Process " << cdt_eqc.size() << " co-datatypes" << std::endl;
1510
172
    std::vector< std::vector< Node > > part_out;
1511
172
    std::vector<Node> exp;
1512
172
    std::map< Node, Node > cn;
1513
172
    std::map< Node, std::map< Node, int > > dni;
1514
1399
    for( unsigned i=0; i<cdt_eqc.size(); i++ ){
1515
1313
      cn[cdt_eqc[i]] = cdt_eqc[i];
1516
    }
1517
86
    separateBisimilar( cdt_eqc, part_out, exp, cn, dni, 0, false );
1518
86
    Trace("dt-cdt-debug") << "Done separate bisimilar." << std::endl;
1519
86
    if( !part_out.empty() ){
1520
10
      Trace("dt-cdt-debug") << "Process partition size " << part_out.size() << std::endl;
1521
20
      for( unsigned i=0; i<part_out.size(); i++ ){
1522
20
        std::vector< Node > part;
1523
10
        part.push_back( part_out[i][0] );
1524
20
        for( unsigned j=1; j<part_out[i].size(); j++ ){
1525
10
          Trace("dt-cdt") << "Codatatypes : " << part_out[i][0] << " and " << part_out[i][j] << " must be equal!!" << std::endl;
1526
10
          part.push_back( part_out[i][j] );
1527
20
          std::vector< std::vector< Node > > tpart_out;
1528
10
          exp.clear();
1529
10
          cn.clear();
1530
10
          cn[part_out[i][0]] = part_out[i][0];
1531
10
          cn[part_out[i][j]] = part_out[i][j];
1532
10
          dni.clear();
1533
10
          separateBisimilar( part, tpart_out, exp, cn, dni, 0, true );
1534
10
          Assert(tpart_out.size() == 1 && tpart_out[0].size() == 2);
1535
10
          part.pop_back();
1536
          //merge based on explanation
1537
10
          Trace("dt-cdt") << "  exp is : ";
1538
38
          for( unsigned k=0; k<exp.size(); k++ ){
1539
28
            Trace("dt-cdt") << exp[k] << " ";
1540
          }
1541
10
          Trace("dt-cdt") << std::endl;
1542
20
          Node eq = part_out[i][0].eqNode( part_out[i][j] );
1543
20
          Node eqExp = NodeManager::currentNM()->mkAnd(exp);
1544
10
          d_im.addPendingInference(eq, InferenceId::DATATYPES_BISIMILAR, eqExp);
1545
10
          Trace("datatypes-infer") << "DtInfer : cdt-bisimilar : " << eq << " by " << eqExp << std::endl;
1546
        }
1547
      }
1548
    }
1549
  }
1550
}
1551
1552
//everything is in terms of representatives
1553
289
void TheoryDatatypes::separateBisimilar(
1554
    std::vector<Node>& part,
1555
    std::vector<std::vector<Node> >& part_out,
1556
    std::vector<Node>& exp,
1557
    std::map<Node, Node>& cn,
1558
    std::map<Node, std::map<Node, int> >& dni,
1559
    int dniLvl,
1560
    bool mkExp)
1561
{
1562
289
  if( !mkExp ){
1563
251
    Trace("dt-cdt-debug") << "Separate bisimilar : " << std::endl;
1564
2051
    for( unsigned i=0; i<part.size(); i++ ){
1565
1800
      Trace("dt-cdt-debug") << "   " << part[i] << ", current = " << cn[part[i]] << std::endl;
1566
    }
1567
  }
1568
289
  Assert(part.size() > 1);
1569
578
  std::map< Node, std::vector< Node > > new_part;
1570
578
  std::map< Node, std::vector< Node > > new_part_c;
1571
578
  std::map< int, std::vector< Node > > new_part_rec;
1572
1573
578
  std::map< Node, Node > cn_cons;
1574
2165
  for( unsigned j=0; j<part.size(); j++ ){
1575
3752
    Node c = cn[part[j]];
1576
1876
    std::map< Node, int >::iterator it_rec = dni[part[j]].find( c );
1577
1876
    if( it_rec!=dni[part[j]].end() ){
1578
      //looped
1579
46
      if( !mkExp ){ Trace("dt-cdt-debug") << "  - " << part[j] << " is looping at index " << it_rec->second << std::endl; }
1580
46
      new_part_rec[ it_rec->second ].push_back( part[j] );
1581
    }else{
1582
1830
      if( c.getType().isDatatype() ){
1583
3058
        Node ncons = getEqcConstructor( c );
1584
1529
        if( ncons.getKind()==APPLY_CONSTRUCTOR ) {
1585
914
          Node cc = ncons.getOperator();
1586
457
          cn_cons[part[j]] = ncons;
1587
457
          if (mkExp && c != ncons)
1588
          {
1589
20
            exp.push_back(c.eqNode(ncons));
1590
          }
1591
457
          new_part[cc].push_back( part[j] );
1592
457
          if( !mkExp ){ Trace("dt-cdt-debug") << "  - " << part[j] << " is datatype " << ncons << "." << std::endl; }
1593
        }else{
1594
1072
          new_part_c[c].push_back( part[j] );
1595
1072
          if( !mkExp ){ Trace("dt-cdt-debug") << "  - " << part[j] << " is unspecified datatype." << std::endl; }
1596
        }
1597
      }else{
1598
        //add equivalences
1599
301
        if( !mkExp ){ Trace("dt-cdt-debug") << "  - " << part[j] << " is term " << c << "." << std::endl; }
1600
301
        new_part_c[c].push_back( part[j] );
1601
      }
1602
    }
1603
  }
1604
  //direct add for constants
1605
1540
  for( std::map< Node, std::vector< Node > >::iterator it = new_part_c.begin(); it != new_part_c.end(); ++it ){
1606
1251
    if( it->second.size()>1 ){
1607
220
      std::vector< Node > vec;
1608
110
      vec.insert( vec.begin(), it->second.begin(), it->second.end() );
1609
110
      part_out.push_back( vec );
1610
    }
1611
  }
1612
  //direct add for recursive
1613
315
  for( std::map< int, std::vector< Node > >::iterator it = new_part_rec.begin(); it != new_part_rec.end(); ++it ){
1614
26
    if( it->second.size()>1 ){
1615
40
      std::vector< Node > vec;
1616
20
      vec.insert( vec.begin(), it->second.begin(), it->second.end() );
1617
20
      part_out.push_back( vec );
1618
    }else{
1619
      //add back : could match a datatype?
1620
    }
1621
  }
1622
  //recurse for the datatypes
1623
518
  for( std::map< Node, std::vector< Node > >::iterator it = new_part.begin(); it != new_part.end(); ++it ){
1624
229
    if( it->second.size()>1 ){
1625
      //set dni to check for loops
1626
166
      std::map< Node, Node > dni_rem;
1627
394
      for( unsigned i=0; i<it->second.size(); i++ ){
1628
622
        Node n = it->second[i];
1629
311
        dni[n][cn[n]] = dniLvl;
1630
311
        dni_rem[n] = cn[n];
1631
      }
1632
1633
      //we will split based on the arguments of the datatype
1634
166
      std::vector< std::vector< Node > > split_new_part;
1635
83
      split_new_part.push_back( it->second );
1636
1637
83
      unsigned nChildren = cn_cons[it->second[0]].getNumChildren();
1638
      //for each child of constructor
1639
83
      unsigned cindex = 0;
1640
389
      while( cindex<nChildren && !split_new_part.empty() ){
1641
153
        if( !mkExp ){ Trace("dt-cdt-debug") << "Split argument #" << cindex << " of " << it->first << "..." << std::endl; }
1642
306
        std::vector< std::vector< Node > > next_split_new_part;
1643
346
        for( unsigned j=0; j<split_new_part.size(); j++ ){
1644
          //set current node
1645
736
          for( unsigned k=0; k<split_new_part[j].size(); k++ ){
1646
1086
            Node n = split_new_part[j][k];
1647
1086
            Node cnc = cn_cons[n][cindex];
1648
1086
            Node nr = getRepresentative(cnc);
1649
543
            cn[n] = nr;
1650
543
            if (mkExp && cnc != nr)
1651
            {
1652
8
              exp.push_back(nr.eqNode(cnc));
1653
            }
1654
          }
1655
386
          std::vector< std::vector< Node > > c_part_out;
1656
193
          separateBisimilar( split_new_part[j], c_part_out, exp, cn, dni, dniLvl+1, mkExp );
1657
193
          next_split_new_part.insert( next_split_new_part.end(), c_part_out.begin(), c_part_out.end() );
1658
        }
1659
153
        split_new_part.clear();
1660
153
        split_new_part.insert( split_new_part.end(), next_split_new_part.begin(), next_split_new_part.end() );
1661
153
        cindex++;
1662
      }
1663
83
      part_out.insert( part_out.end(), split_new_part.begin(), split_new_part.end() );
1664
1665
394
      for( std::map< Node, Node >::iterator it2 = dni_rem.begin(); it2 != dni_rem.end(); ++it2 ){
1666
311
        dni[it2->first].erase( it2->second );
1667
      }
1668
    }
1669
  }
1670
289
}
1671
1672
//postcondition: if cycle detected, explanation is why n is a subterm of on
1673
761041
Node TheoryDatatypes::searchForCycle(TNode n,
1674
                                     TNode on,
1675
                                     std::map<TNode, bool>& visited,
1676
                                     std::map<TNode, bool>& proc,
1677
                                     std::vector<Node>& explanation,
1678
                                     bool firstTime)
1679
{
1680
761041
  Trace("datatypes-cycle-check2") << "Search for cycle " << n << " " << on << endl;
1681
1522082
  TNode ncons;
1682
1522082
  TNode nn;
1683
761041
  if( !firstTime ){
1684
570798
    nn = getRepresentative( n );
1685
570798
    if( nn==on ){
1686
412
      if (n != nn)
1687
      {
1688
340
        explanation.push_back(n.eqNode(nn));
1689
      }
1690
412
      return on;
1691
    }
1692
  }else{
1693
190243
    nn = getRepresentative( n );
1694
  }
1695
760629
  if( proc.find( nn )!=proc.end() ){
1696
147829
    return Node::null();
1697
  }
1698
612800
  Trace("datatypes-cycle-check2") << "...representative : " << nn << " " << ( visited.find( nn ) == visited.end() ) << " " << visited.size() << std::endl;
1699
612800
  if( visited.find( nn ) == visited.end() ) {
1700
612778
    Trace("datatypes-cycle-check2") << "  visit : " << nn << std::endl;
1701
612778
    visited[nn] = true;
1702
1225556
    TNode nncons = getEqcConstructor(nn);
1703
612778
    if (nncons.getKind() == APPLY_CONSTRUCTOR)
1704
    {
1705
1009883
      for (unsigned i = 0; i < nncons.getNumChildren(); i++)
1706
      {
1707
        TNode cn =
1708
1140193
            searchForCycle(nncons[i], on, visited, proc, explanation, false);
1709
570798
        if( cn==on ) {
1710
          //add explanation for why the constructor is connected
1711
1275
          if (n != nncons)
1712
          {
1713
876
            explanation.push_back(n.eqNode(nncons));
1714
          }
1715
1275
          return on;
1716
569523
        }else if( !cn.isNull() ){
1717
128
          return cn;
1718
        }
1719
      }
1720
    }
1721
611375
    Trace("datatypes-cycle-check2") << "  unvisit : " << nn << std::endl;
1722
611375
    proc[nn] = true;
1723
611375
    visited.erase( nn );
1724
611375
    return Node::null();
1725
  }else{
1726
44
    TypeNode tn = nn.getType();
1727
22
    if( tn.isDatatype() ) {
1728
22
      if( !tn.isCodatatype() ){
1729
22
        return nn;
1730
      }
1731
    }
1732
    return Node::null();
1733
  }
1734
}
1735
1736
1893132
bool TheoryDatatypes::hasTerm(TNode a) { return d_equalityEngine->hasTerm(a); }
1737
1738
102338
bool TheoryDatatypes::areEqual( TNode a, TNode b ){
1739
102338
  if( a==b ){
1740
5230
    return true;
1741
97108
  }else if( hasTerm( a ) && hasTerm( b ) ){
1742
97108
    return d_equalityEngine->areEqual(a, b);
1743
  }else{
1744
    return false;
1745
  }
1746
}
1747
1748
20057
bool TheoryDatatypes::areDisequal( TNode a, TNode b ){
1749
20057
  if( a==b ){
1750
6166
    return false;
1751
13891
  }else if( hasTerm( a ) && hasTerm( b ) ){
1752
13891
    return d_equalityEngine->areDisequal(a, b, false);
1753
  }else{
1754
    //TODO : constants here?
1755
    return false;
1756
  }
1757
}
1758
1759
79137
bool TheoryDatatypes::areCareDisequal( TNode x, TNode y ) {
1760
79137
  Trace("datatypes-cg") << "areCareDisequal: " << x << " " << y << std::endl;
1761
79137
  Assert(d_equalityEngine->hasTerm(x));
1762
79137
  Assert(d_equalityEngine->hasTerm(y));
1763
237411
  if (d_equalityEngine->isTriggerTerm(x, THEORY_DATATYPES)
1764
237411
      && d_equalityEngine->isTriggerTerm(y, THEORY_DATATYPES))
1765
  {
1766
    TNode x_shared =
1767
75130
        d_equalityEngine->getTriggerTermRepresentative(x, THEORY_DATATYPES);
1768
    TNode y_shared =
1769
75130
        d_equalityEngine->getTriggerTermRepresentative(y, THEORY_DATATYPES);
1770
58668
    EqualityStatus eqStatus = d_valuation.getEqualityStatus(x_shared, y_shared);
1771
58668
    if( eqStatus==EQUALITY_FALSE_AND_PROPAGATED || eqStatus==EQUALITY_FALSE || eqStatus==EQUALITY_FALSE_IN_MODEL ){
1772
42206
      return true;
1773
    }
1774
  }
1775
36931
  return false;
1776
}
1777
1778
1671134
TNode TheoryDatatypes::getRepresentative( TNode a ){
1779
1671134
  if( hasTerm( a ) ){
1780
1671134
    return d_equalityEngine->getRepresentative(a);
1781
  }else{
1782
    return a;
1783
  }
1784
}
1785
1786
31579
void TheoryDatatypes::printModelDebug( const char* c ){
1787
31579
  if(! (Trace.isOn(c))) {
1788
31579
    return;
1789
  }
1790
1791
  Trace( c ) << "Datatypes model : " << std::endl;
1792
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(d_equalityEngine);
1793
  while( !eqcs_i.isFinished() ){
1794
    Node eqc = (*eqcs_i);
1795
    //if( !eqc.getType().isBoolean() ){
1796
      if( eqc.getType().isDatatype() ){
1797
        Trace( c ) << "DATATYPE : ";
1798
      }
1799
      Trace( c ) << eqc << " : " << eqc.getType() << " : " << std::endl;
1800
      Trace( c ) << "   { ";
1801
      //add terms to model
1802
      eq::EqClassIterator eqc_i = eq::EqClassIterator(eqc, d_equalityEngine);
1803
      while( !eqc_i.isFinished() ){
1804
        if( (*eqc_i)!=eqc ){
1805
          Trace( c ) << (*eqc_i) << " ";
1806
        }
1807
        ++eqc_i;
1808
      }
1809
      Trace( c ) << "}" << std::endl;
1810
      if( eqc.getType().isDatatype() ){
1811
        EqcInfo* ei = getOrMakeEqcInfo( eqc );
1812
        if( ei ){
1813
          Trace( c ) << "   Instantiated : " << ei->d_inst.get() << std::endl;
1814
          Trace( c ) << "   Constructor : ";
1815
          if( !ei->d_constructor.get().isNull() ){
1816
            Trace( c )<< ei->d_constructor.get();
1817
          }
1818
          Trace( c ) << std::endl << "   Labels : ";
1819
          if( hasLabel( ei, eqc ) ){
1820
            Trace( c ) << getLabel( eqc );
1821
          }else{
1822
            NodeUIntMap::iterator lbl_i = d_labels.find(eqc);
1823
            if( lbl_i != d_labels.end() ){
1824
              for (size_t j = 0; j < (*lbl_i).second; j++)
1825
              {
1826
                Trace( c ) << d_labels_data[eqc][j] << " ";
1827
              }
1828
            }
1829
          }
1830
          Trace( c ) << std::endl;
1831
          Trace( c ) << "   Selectors : " << ( ei->d_selectors ? "yes, " : "no " );
1832
          NodeUIntMap::iterator sel_i = d_selector_apps.find(eqc);
1833
          if( sel_i != d_selector_apps.end() ){
1834
            for (size_t j = 0; j < (*sel_i).second; j++)
1835
            {
1836
              Trace( c ) << d_selector_apps_data[eqc][j] << " ";
1837
            }
1838
          }
1839
          Trace( c ) << std::endl;
1840
        }
1841
      }
1842
    //}
1843
    ++eqcs_i;
1844
  }
1845
}
1846
1847
9089
void TheoryDatatypes::computeRelevantTerms(std::set<Node>& termSet)
1848
{
1849
18178
  Trace("dt-cmi") << "Have " << termSet.size() << " relevant terms..."
1850
9089
                  << std::endl;
1851
1852
  //also include non-singleton equivalence classes  TODO : revisit this
1853
9089
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(d_equalityEngine);
1854
573967
  while( !eqcs_i.isFinished() ){
1855
564878
    TNode r = (*eqcs_i);
1856
282439
    bool addedFirst = false;
1857
564878
    Node first;
1858
564878
    TypeNode rtn = r.getType();
1859
282439
    if (!rtn.isBoolean())
1860
    {
1861
207251
      eq::EqClassIterator eqc_i = eq::EqClassIterator(r, d_equalityEngine);
1862
1058497
      while (!eqc_i.isFinished())
1863
      {
1864
851246
        TNode n = (*eqc_i);
1865
425623
        if (first.isNull())
1866
        {
1867
207251
          first = n;
1868
          // always include all datatypes
1869
207251
          if (rtn.isDatatype())
1870
          {
1871
64384
            addedFirst = true;
1872
64384
            termSet.insert(n);
1873
          }
1874
        }
1875
        else
1876
        {
1877
218372
          if (!addedFirst)
1878
          {
1879
22895
            addedFirst = true;
1880
22895
            termSet.insert(first);
1881
          }
1882
218372
          termSet.insert(n);
1883
        }
1884
425623
        ++eqc_i;
1885
      }
1886
    }
1887
282439
    ++eqcs_i;
1888
  }
1889
9089
}
1890
1891
std::pair<bool, Node> TheoryDatatypes::entailmentCheck(TNode lit)
1892
{
1893
  Trace("dt-entail") << "Check entailed : " << lit << std::endl;
1894
  Node atom = lit.getKind()==NOT ? lit[0] : lit;
1895
  bool pol = lit.getKind()!=NOT;
1896
  if( atom.getKind()==APPLY_TESTER ){
1897
    Node n = atom[0];
1898
    if( hasTerm( n ) ){
1899
      Node r = d_equalityEngine->getRepresentative(n);
1900
      EqcInfo * ei = getOrMakeEqcInfo( r, false );
1901
      int l_index = getLabelIndex( ei, r );
1902
      int t_index = static_cast<int>(utils::indexOf(atom.getOperator()));
1903
      Trace("dt-entail") << "  Tester indices are " << t_index << " and " << l_index << std::endl;
1904
      if( l_index!=-1 && (l_index==t_index)==pol ){
1905
        std::vector< TNode > exp_c;
1906
        Node eqToExplain;
1907
        if( ei && !ei->d_constructor.get().isNull() ){
1908
          eqToExplain = n.eqNode(ei->d_constructor.get());
1909
        }else{
1910
          Node lbl = getLabel( n );
1911
          Assert(!lbl.isNull());
1912
          exp_c.push_back( lbl );
1913
          Assert(areEqual(n, lbl[0]));
1914
          eqToExplain = n.eqNode(lbl[0]);
1915
        }
1916
        d_equalityEngine->explainLit(eqToExplain, exp_c);
1917
        Node exp = NodeManager::currentNM()->mkAnd(exp_c);
1918
        Trace("dt-entail") << "  entailed, explanation is " << exp << std::endl;
1919
        return make_pair(true, exp);
1920
      }
1921
    }
1922
  }
1923
  return make_pair(false, Node::null());
1924
}
1925
1926
}  // namespace datatypes
1927
}  // namespace theory
1928
27735
}  // namespace cvc5