vampire-sys 0.5.2

Low-level FFI bindings to the Vampire theorem prover (use the 'vampire' crate instead)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
/*
 * This file is part of the source code of the software program
 * Vampire. It is protected by applicable
 * copyright laws.
 *
 * This source code is distributed under the licence found here
 * https://vprover.github.io/license.html
 * and in the source directory
 */
/**
 * @file SimpleCongruenceClosure.cpp
 * Implements class SimpleCongruenceClosure.
 */

#include <sstream>

#include "SimpleCongruenceClosure.hpp"

#include "Lib/ArrayMap.hpp"

#include "Lib/IntUnionFind.hpp"
#include "Lib/DynamicHeap.hpp"

#include "Kernel/SortHelper.hpp"

#include "Shell/DistinctProcessor.hpp"

namespace DP
{

using namespace std;

const unsigned SimpleCongruenceClosure::NO_SIG_SYMBOL = 0xFFFFFFFF;

std::string SimpleCongruenceClosure::CEq::toString() const
{
  std::ostringstream res;
  res << c1<<"="<<c2<<" implied by ";
  if(foOrigin) {
    if(foPremise) {
      res << (*foPremise);
    }
    else {
      res << "built-in true!=false";
    }
  }
  else {
    res << "congruence";
  }
  return res.str();
}

std::string SimpleCongruenceClosure::CEq::toString(SimpleCongruenceClosure& parent) const
{
  std::ostringstream res;
  res << c1<<"="<<c2<<" implied by ";
  if(foOrigin) {
    if(foPremise) {
      res << (*foPremise);
    }
    else {
      res << "built-in true!=false";
    }
  }
  else {
    CPair p1= parent._cInfos[c1].namedPair;
    CPair p2= parent._cInfos[c2].namedPair;
    res << "congruence of ("<<p1.first<<","<<p1.second<<") and ("<<p2.first<<","<<p2.second<<")";
  }
  return res.str();
}

void SimpleCongruenceClosure::ConstInfo::init() {
  sigSymbol = NO_SIG_SYMBOL;
  term = TermList::empty();
  lit = 0;
  namedPair = CPair(0,0);
  reprConst = 0;
  proofPredecessor = 0;
  predecessorPremise = CEq(0,0);
  classList.reset();
  useList.reset();
}

void SimpleCongruenceClosure::ConstInfo::resetEquivalences(SimpleCongruenceClosure& parent, unsigned selfIndex) {
  reprConst = 0;
  proofPredecessor = 0;
  predecessorPremise = CEq(0,0);
  classList.reset();

  static ArraySet seen;
  seen.ensure(parent.getMaxConst()+1);
  seen.reset();

  Stack<unsigned>::DelIterator ulit(useList);
  // Martin: keep only those namedPairs in useList for which selfIndex
  // is one of the arguments, and keep each such exactly once
  while(ulit.hasNext()) {
    unsigned p = ulit.next();
    ConstInfo& pInfo = parent._cInfos[p];
    if(pInfo.namedPair.first!=selfIndex && pInfo.namedPair.second!=selfIndex) {
      ulit.del();
      continue;
    }
    if(seen.find(p)) {
      ulit.del();
      continue;
    }
    seen.insert(p);
  }
}

#if VDEBUG

void SimpleCongruenceClosure::ConstInfo::assertValid(SimpleCongruenceClosure& parent, unsigned selfIndex) const
{
  if(reprConst==0) {
    Stack<unsigned>::ConstIterator ulit(useList);
    while(ulit.hasNext()) {
      unsigned p = ulit.next();
      ConstInfo& pInfo = parent._cInfos[p];
      CPair pPair = pInfo.namedPair;
      ASS_NEQ(pPair.first,0);
      ASS_NEQ(pPair.second,0);
      CPair derefPair = parent.deref(pPair);
      ASS(derefPair.first==selfIndex || derefPair.second==selfIndex);
    }
  }
}

#endif


SimpleCongruenceClosure::SimpleCongruenceClosure(Ordering* ord) :
  _ord(ord)
{
  _cInfos.ensure(1); //this ensures constants will start numbered from 1, not 0

  _posLitConst = getFreshConst();
  _negLitConst = getFreshConst();
  _negEqualities.push(CEq(_posLitConst, _negLitConst, 0));

  _hadPropagated = false;
}

void SimpleCongruenceClosure::reset()
{
  //do reset that keeps the data for converting terms to constants
  unsigned maxConst = getMaxConst();
  for(unsigned i=1; i<=maxConst; i++) {
    _cInfos[i].resetEquivalences(*this, i);
  }

  PairMap::DelIterator pmit(_pairNames);
  while(pmit.hasNext()) {
    CPair namePair;
    unsigned nameConst;
    pmit.next(namePair, nameConst);

    if(_cInfos[nameConst].namedPair!=namePair) {
      pmit.del();
    }
  }

  //this leaves us just with the true!=false non-equality
  _negEqualities.truncate(1);
  ASS_EQ(_negEqualities.top().c1,_posLitConst);
  ASS_EQ(_negEqualities.top().c2,_negLitConst);

  //no unsat non-equality
  _unsatEqs.reset();

  _pendingEqualities.reset();
  _distinctConstraints.reset();
  _negDistinctConstraints.reset();

  _hadPropagated = false;
}

/** Introduce fresh congruence closure constant */
unsigned SimpleCongruenceClosure::getFreshConst()
{
  unsigned res = _cInfos.size();
  _cInfos.expand(res+1);
  _cInfos[res].init();
  return res;
}

/** Get congruence closure constant corresponding to a signature symbol number @c symbol */
unsigned SimpleCongruenceClosure::getSignatureConst(unsigned symbol, SignatureKind kind)
{
  unsigned* pRes;
  if(!_sigConsts.getValuePtr(make_pair(symbol, kind), pRes)) {
    return *pRes;
  }
  unsigned res = getFreshConst();
  _cInfos[res].sigSymbol = symbol;
  _cInfos[res].sigSymKind = kind;
  *pRes = res;

  return res;
}

unsigned SimpleCongruenceClosure::getPairName(CPair p)
{
  unsigned* pRes;
  if(!_pairNames.getValuePtr(p, pRes)) {
    return *pRes;
  }
  unsigned res = getFreshConst();
  _cInfos[res].namedPair = p;
  *pRes = res;

  _cInfos[p.first].useList.push(res);
  if(_cInfos[p.first].reprConst!=0) {
    // Martin: if we are here, the above insertion was not needed now,
    // but will become necessary after reset(); see resetEquivalences
    unsigned fRepr = _cInfos[p.first].reprConst;
    _cInfos[fRepr].useList.push(res);
  }
  _cInfos[p.second].useList.push(res);
  if(_cInfos[p.second].reprConst!=0) {
    // Martin: if we are here, the above insertion was not needed now,
    // but will become necessary after reset(); see resetEquivalences
    unsigned sRepr = _cInfos[p.second].reprConst;
    _cInfos[sRepr].useList.push(res);
  }

  return res;
}

// memoising structure used in convertFO below
// TODO: should this be provided as part of the BUE machinery?
struct Memo {
  Option<unsigned> get(TermList t) {
    unsigned cached;
    return termNames.find(t, cached) ? some(cached) : none<unsigned>();
  }

  template<class Init> unsigned getOrInit(TermList orig, Init init) {
    unsigned cached;
    if(termNames.find(orig, cached))
      return cached;
    unsigned result = init();
    termNames.insert(orig, result);
    return result;
  }
  DHMap<TermList, unsigned> &termNames;
};

/**
 * Convert a first-order term into a canonical constant
 * by traversing the term structure converting each subterm, results are
 * cached in _termNames for consistency
 *
 * (see FOConversionWorker)
 */
unsigned SimpleCongruenceClosure::convertFO(TermList trm)
{
  unsigned cached;
  if(_termNames.find(trm, cached))
    return cached;

  return BottomUpEvaluation<TermList, unsigned>()
    .context(TermListContext {.ignoreTypeArgs = false})
    .function([&](TermList t, unsigned *children) {
        unsigned res;
        if(t.isVar()) {
          res = getSignatureConst(t.var(), SignatureKind::VARIABLE);
        }
        else {
          ASS(t.isTerm());
          Term *trm = t.term();
          SignatureKind sk = trm->isSort() ? SignatureKind::TYPECON : SignatureKind::FUNCTION;
          res = getSignatureConst(trm->functor(), sk);
          for(size_t i = 0; i < trm->arity(); i++) {
            res = getPairName(CPair(res, children[i]));
          }
        }
        _cInfos[res].term = t;
        return res;
    })
    .memo(Memo {_termNames})
    .apply(trm);
}

/**
 * Convert a non-equality term into a canonical constant
 *
 */
unsigned SimpleCongruenceClosure::convertFONonEquality(Literal* lit)
{
  ASS(!lit->isEquality());

  unsigned res;
  if(_litNames.find(lit, res)) {
    return res;
  }  
  if(_litNames.find(Literal::complementaryLiteral(lit), res)) {
    _litNames.insert(lit, res);
    return res;
  }
  // Martin: in any case, the logical negation is encoded by the caller;
  // notice that we ignore the polarity below:

  res = getSignatureConst(lit->functor(), SignatureKind::PREDICATE);
  Term::Iterator ait(lit);
  while(ait.hasNext()) {
    TermList a = ait.next();
    unsigned argConst = convertFO(a);
    res = getPairName(CPair(res, argConst));
    //Martin: same way to currify like in convertFO
  }
  _cInfos[res].lit = lit;

  _litNames.insert(lit, res);
  return res;
}

/**
 * Uses Shell::DistinctProcessor to check for $distinct predicate from TFF language in TPTP
 */
bool SimpleCongruenceClosure::isDistinctPred(Literal* l)
{
  return Shell::DistinctProcessor::isDistinctPred(l);
}

/**
 * Records distinct arguments (will be used to check satisfiability)
 */
void SimpleCongruenceClosure::readDistinct(Literal* lit)
{
  bool pos = lit->isPositive();
  DistinctStack& tgtDStack = pos ? _distinctConstraints : _negDistinctConstraints;
  tgtDStack.push(DistinctEntry(lit));
  Stack<unsigned>& tgtStack = tgtDStack.top()._consts;
  Literal::Iterator ait(lit);
  while(ait.hasNext()) {
    TermList arg = ait.next();
    unsigned cNum = convertFO(arg);
    tgtStack.push(cNum);
  }
}

/**
 * Convert a equality literal into a CEq
 * by first converting each side into a representative constant (see convertFO)
 */
SimpleCongruenceClosure::CEq SimpleCongruenceClosure::convertFOEquality(Literal* equality)
{
  ASS(equality->isEquality());

  unsigned arg1 = convertFO(*equality->nthArgument(0));
  unsigned arg2 = convertFO(*equality->nthArgument(1));
  return CEq(arg1, arg2, equality);
}

/**
 * Add literals based on their structure
 * - if non-ground ignore
 *
 * - if equality convert each side to a representative integer and place in a CEq
 *     if positive equality add eq as a pending equality
 *     otherwise add eq to _negEqualities
 *
 * - if distinct predicate (a special predicate in the TFF language of TPTP that
 *     guarantees that its arguments are distinct)
 *     add to distinctConstraints or negDistinctConstraints appropriately
 *
 * - otherwise build an integer representative of the term and
 *    if the literal is positive equate it with a positive constant
 *    otherwise, equate it with a negative constant
 *
 */
void SimpleCongruenceClosure::addLiterals(LiteralIterator lits, bool onlyEqualites)
{
  ASS(!_hadPropagated);

  while(lits.hasNext()) {
    Literal* l = lits.next();
    if(!l->ground()) {
      //for now we ignore non-ground literals
      continue;
      // update: we do handle them, but let's not change the bahavoir of this interface
    }

    if (!onlyEqualites || (l->isEquality() && l->isPositive())) {
      addLiteral(l);
    }
  }
}

void SimpleCongruenceClosure::addLiteral(Literal* lit)
{
  if (lit->isEquality()) {
    CEq eq = convertFOEquality(lit);

    if (lit->isPositive()) {
      addPendingEquality(eq);
    } else {
      _negEqualities.push(eq);
    }
  } else if(isDistinctPred(lit)) {
    readDistinct(lit);
  } else {
    unsigned predConst = convertFONonEquality(lit);
    CEq eq;
    if(lit->isPositive()) {
      eq = CEq(predConst, _posLitConst, lit);
    }
    else {
      eq = CEq(predConst, _negLitConst, lit);
    }
    addPendingEquality(eq);
  }
}

void SimpleCongruenceClosure::addPendingEquality(CEq eq) {
  ASS_G(eq.c1,0);
  ASS_G(eq.c2,0);
  _pendingEqualities.push_back(eq);
}

/**
 * Invert some edges in the proof tree, so that c becomes the proof root
 */
void SimpleCongruenceClosure::makeProofRepresentant(unsigned c)
{
  if(_cInfos[c].proofPredecessor==0) {
    return;
  }

  //this kind of traversal may possibly cause quadratic complexity!
  
  //Martin: it shouldn't, because we always reorder a path in the smaller proof tree
  
  CEq transfPrem; //initialized as invalid
  unsigned prevC = 0;

  do{
    unsigned newC = _cInfos[c].proofPredecessor;
    _cInfos[c].proofPredecessor = prevC;
    swap(_cInfos[c].predecessorPremise, transfPrem);
    prevC = c;
    c = newC;
  } while(c!=0);
  ASS(transfPrem.isInvalid()); //the proof tree root has invalid equality as a premise
}

/**
 * Propagate any pending equalities
 *
 *
 */
void SimpleCongruenceClosure::propagate()
{
  _hadPropagated = true;

  while(_pendingEqualities.isNonEmpty()) {
    CEq curr0 = _pendingEqualities.pop_back();
    CPair curr = deref(curr0);
    if(curr.first==curr.second) {
      continue;
    }
    // Ensure the first constant has a smaller class size
    if(getClassSize(curr.first)>getClassSize(curr.second)) {
      std::swap(curr0.c1, curr0.c2);
      std::swap(curr.first, curr.second);
    }

    {
      //proof updating
      unsigned aProofRep = curr0.c1;
      unsigned bProofRep = curr0.c2;
      makeProofRepresentant(aProofRep);
      ConstInfo& aProofInfo = _cInfos[aProofRep];
      ASS_EQ(aProofInfo.proofPredecessor,0);
      aProofInfo.proofPredecessor = bProofRep;
      aProofInfo.predecessorPremise = curr0;
    }

    // Get the class representatives
    unsigned aRep = curr.first;
    unsigned bRep = curr.second;

    ConstInfo& aInfo = _cInfos[aRep];
    ConstInfo& bInfo = _cInfos[bRep];
    ASS_EQ(aInfo.reprConst,0); // ensure that they are their own representatives
    ASS_EQ(bInfo.reprConst,0);

    DEBUG_CODE( aInfo.assertValid(*this, aRep); );
    DEBUG_CODE( bInfo.assertValid(*this, bRep); );

    // Merge first class into second (which is why we wanted the first to be smaller)
    // To do this we update the representative for all constants in
    // the class of aRep to be bRep
    aInfo.reprConst = bRep;
    bInfo.classList.push(aRep);
    Stack<unsigned>::Iterator aChildIt(aInfo.classList);
    while(aChildIt.hasNext()) {
      unsigned aChild = aChildIt.next();
      bInfo.classList.push(aChild);
      _cInfos[aChild].reprConst = bRep;
    }
    // Now update all places where aRep has been used as a
    // representative of one of the arguments of a pair
    Stack<unsigned>::Iterator aUseIt(aInfo.useList);
    while(aUseIt.hasNext()) {
      unsigned usePairConst = aUseIt.next();
      CPair usedPair = _cInfos[usePairConst].namedPair;
      ASS(usedPair!=CPair(0,0)); //the constant must be a name of a pair
      CPair derefPair = deref(usedPair);
      ASS(usedPair!=derefPair); // Martin: (at least) one of the arguments was aRep, now is bRep

      unsigned* pDerefPairName;
      if(!_pairNames.getValuePtr(derefPair, pDerefPairName)) {
	addPendingEquality(CEq(*pDerefPairName, usePairConst));
      }
      else {
	*pDerefPairName = usePairConst;
	bInfo.useList.push(usePairConst);
      }
    }
  }
}

bool SimpleCongruenceClosure::checkPositiveDistincts(bool retrieveMultipleCores)
{
  //map from a representative to constant in its class present in the current distinct group
  static ArrayMap<unsigned> reprs;
  reprs.ensure(getMaxConst()+1);

  bool foundConflict = false;

  DistinctStack::BottomFirstIterator distIt(_distinctConstraints);
  while(distIt.hasNext()) {
    const DistinctEntry& grp = distIt.next();

    reprs.reset();
    Stack<unsigned>::ConstIterator git(grp._consts);
    while(git.hasNext()) {
      unsigned c = git.next();
      unsigned rep = deref(c);
      unsigned c2;
      if(reprs.find(rep, c2)) {
	//we're unsat, two equal constants in a distinct group
	_unsatEqs.push(CEq(c, c2, grp._lit));
	if(!retrieveMultipleCores) {
	  return false;
	}
	foundConflict = true;
      }
      else {
	reprs.insert(rep, c);
      }
    }
  }
  return !foundConflict;
}

DecisionProcedure::Status SimpleCongruenceClosure::checkNegativeDistincts(bool retrieveMultipleCores)
{
  //map from a representative to constant in its class present in the current distinct group
  static ArrayMap<unsigned> reprs;
  reprs.ensure(getMaxConst()+1);

  DistinctStack::BottomFirstIterator distIt(_negDistinctConstraints);
  while(distIt.hasNext()) {
    const DistinctEntry& grp = distIt.next();

    reprs.reset();
    bool isFalse = false;
    Stack<unsigned>::ConstIterator git(grp._consts);
    while(git.hasNext()) {
      unsigned c = git.next();
      unsigned rep = deref(c);
      if(reprs.find(rep)) {
	//distinct constraint is false, we're happy
	isFalse = true;
	continue; // Martin: why not break?
      }
      reprs.insert(rep, c);
    }
    if(!isFalse) {
      //The distinct predicate wasn't falsified by the current rewriting system.
      //It might still be possible to extend the system to make the distinct
      //falsified (hence the result UNKNOWN), but we do not implement this.
      return DecisionProcedure::UNKNOWN;
    }
  }
  return DecisionProcedure::SATISFIABLE;
}

/**
 * Decide whether the added literals are satisfiable. The @c retrieveMultipleCores parameter
 * indicates whether, on unsat, we should compute all unsat cores or only the first one
 *
 */
DecisionProcedure::Status SimpleCongruenceClosure::getStatus(bool retrieveMultipleCores)
{
  // Propagate any pending equalities
  propagate();

  // Check classes satisfy distincts (inbuilt predicate stating inequality)
  // Straightforward to check positive distincts against congruence classes
  if(!checkPositiveDistincts(retrieveMultipleCores)) {
    if(!retrieveMultipleCores) {
      return DecisionProcedure::UNSATISFIABLE;
    }
    ASS(_unsatEqs.isNonEmpty());
    // Martin: else continue collecting other potential reasons for unsat    
  }

  //The intuition is that we want to fail on as early equalities as possible
  //(to improve back-jumping), so we have a BottomUpIterator here.
  //A possible problem with this is that the non-equality true!=false is the
  //first one, so whenever we have a contradiction with non-equality predicates,
  //it will be discovered first.
  //
  // Giles: not sure why 'early' equalities are better as the order of added literals
  //        is based on order added to SAT solver, which is not necessarily indicative
  //        of ordering in SAT solver splitting tree
  //        Cannot see why true!=false would be first non-equality
  // Martin: agree with first, can explain the second (see the constructor and reset())
  Stack<CEq>::BottomFirstIterator neqIt(_negEqualities);
  while(neqIt.hasNext()) {
    CEq neq = neqIt.next();
    // derNEq is neq with each half converted to its class representative
    CPair derNEq = deref(neq);
    if(derNEq.first==derNEq.second) {
      _unsatEqs.push(neq);
      if(!retrieveMultipleCores) {
	return DecisionProcedure::UNSATISFIABLE;
      }
    }
  }

  // check negative distincts, but might return UNKNOWN as if we don't falsify the
  // distinctness it doesn't mean we can't
  DecisionProcedure::Status ndStatus = checkNegativeDistincts(retrieveMultipleCores);

  if(_unsatEqs.isNonEmpty()) {
    return DecisionProcedure::UNSATISFIABLE;
  }
  if(ndStatus==DecisionProcedure::UNKNOWN) {
    return DecisionProcedure::UNKNOWN;
  }
  return DecisionProcedure::SATISFIABLE;
}

/**
 * Return the depth of constant @c c in the tree determined
 * by ConstInfo::proofPredecessor. 0 means @c c is the root.
 */
unsigned SimpleCongruenceClosure::getProofDepth(unsigned c)
{
  unsigned res = 0;
  while(_cInfos[c].proofPredecessor!=0) {
    c = _cInfos[c].proofPredecessor;
    res++;
  }
  return res;
}

/**
 * Into @c path put constants so that their predecessorPremise objects
 * imply equality of c1 and c2.
 */
void SimpleCongruenceClosure::collectUnifyingPath(unsigned c1, unsigned c2, Stack<unsigned>& path)
{
  ASS_EQ(deref(c1), deref(c2));

  //this function could be probably made more efficient if we use some time-stamping of the ConstInfo object
  
  // Martin: paper says, proof can be found in O(k), where k is size of the proof
  // My idea: go from both in parallel and start marking the paths, when one hits
  // a marked node, the other should go back here too and then they both return home, "printing"
  
  // Martin: this is probably not the whole story; some parts of these proofs might have been output already
  // (see the paper for more details) [probably only if this is shown to be a bottleneck]

  unsigned depth1 = getProofDepth(c1);
  unsigned depth2 = getProofDepth(c2);

  if(depth1<depth2) {
    swap(c1,c2);
    swap(depth1,depth2);
  }

  while(depth1>depth2) {
    path.push(c1);
    c1 = _cInfos[c1].proofPredecessor;
    depth1--;
 }

#if VDEBUG
  unsigned depth=depth1;
#endif
  while(c1!=c2) {
#if VDEBUG
    ASS_G(depth,0);
    depth--;
#endif
    path.push(c1);
    c1 = _cInfos[c1].proofPredecessor;
    path.push(c2);
    c2 = _cInfos[c2].proofPredecessor;
  }
}

/**
 * Retrieve unsatcore @c coreIndex
 * This uses the @c coreIndex violated non-equality to extract a set of literals to explain
 * the violation.
 */
void SimpleCongruenceClosure::getUnsatCore(LiteralStack& res, unsigned coreIndex)
{
  ASS(res.isEmpty());
  ASS_L(coreIndex,_unsatEqs.size());

  CEq unsatEq = _unsatEqs[coreIndex];

  ASS(unsatEq.foOrigin);
  if(unsatEq.foPremise) {
    res.push(unsatEq.foPremise);
  }

  static Stack<CPair> toExplain;
  toExplain.push(CPair(unsatEq.c1, unsatEq.c2));
  ASS_EQ(deref(toExplain.top().first), deref(toExplain.top().second));

  IntUnionFind explained(getMaxConst()+1);
  static Stack<unsigned> pathStack;

  while(toExplain.isNonEmpty()) {
    CPair curr = toExplain.pop();
    ASS_EQ(deref(curr.first), deref(curr.second));

    if(explained.root(curr.first)==explained.root(curr.second)) {
      //we've already explained this equality
      continue;
    }
    pathStack.reset();
    // put into pathStack constants whose premises imply the equality
    collectUnifyingPath(curr.first, curr.second, pathStack);
    while(pathStack.isNonEmpty()) {
      unsigned proofStepConst = pathStack.pop();
      CEq& prem = _cInfos[proofStepConst].predecessorPremise;
      if(explained.root(prem.c1)==explained.root(prem.c2)) {
        //we've already explained this equality
        continue;
      }
      if(prem.foOrigin) {
	if(prem.foPremise) {
	  res.push(prem.foPremise);
	} // Martin: this should be only null for the "built-in" dis-equality (_posLitConst != _negLitConst)
      }
      else {
	//the equality was implied by congruence between the following two pairs:
	CPair cp1 = _cInfos[prem.c1].namedPair;
	CPair cp2 = _cInfos[prem.c2].namedPair;
	ASS_NEQ(cp1.first,0);
	ASS_NEQ(cp1.second,0);
	ASS_NEQ(cp2.first,0);
	ASS_NEQ(cp2.second,0);
	toExplain.push(CPair(cp1.first, cp2.first));
	ASS_EQ(deref(toExplain.top().first), deref(toExplain.top().second));
	toExplain.push(CPair(cp1.second, cp2.second));
	ASS_EQ(deref(toExplain.top().first), deref(toExplain.top().second));
      }
      explained.doUnion(prem.c1, prem.c2);
    }
  }
}

struct SimpleCongruenceClosure::ConstOrderingComparator {
  ConstOrderingComparator(DArray<ConstInfo>& cInfos, Ordering& ord) 
    : _cInfos(cInfos), _ord(ord) {}
  
  Comparison compare(unsigned c1, unsigned c2)
  {
    TermList c1NF = _cInfos[c1].normalForm;
    TermList c2NF = _cInfos[c2].normalForm;
    
    // we don't care about the order of partial applications 
    // as long as they are smaller than the proper terms
    
    if (c1NF.isEmpty()) {
      if (c2NF.isEmpty()) {
        return EQUAL;
      } else {
        return LESS;
      }
    } else {
      if (c2NF.isEmpty()) {
        return GREATER;
      } else {
        // We should not be comparing sorts with terms via the ordering
        if(c1NF.term()->isSort() && !c2NF.term()->isSort()){
          return LESS;
        } else if(c2NF.term()->isSort() && !c1NF.term()->isSort()) {
          return GREATER;
        }
        // two proper terms
        switch(_ord.compare(c1NF,c2NF)) {
          case Ordering::Result::GREATER:
            return GREATER;    
          case Ordering::Result::LESS:
            return LESS;
          case Ordering::Result::EQUAL:
            return EQUAL;    
          default:
            ASSERTION_VIOLATION;
        }
      }
    }
  }
  
  DArray<ConstInfo>& _cInfos;
  Ordering& _ord;
};

/**
 * Const @c c is a namedPair and both of its arguments are normalized.
 * 
 * We can compute normal form for @c c, provided it names a term at all.
 */
void SimpleCongruenceClosure::computeConstsNormalForm(unsigned c, NFMap& normalForms) 
{
  ConstInfo& cInfo = _cInfos[c];
  if (!cInfo.term.isEmpty() && cInfo.normalForm.isEmpty()) {
    Term* t = cInfo.term.term();
    unsigned idx = t->arity();
    unsigned d = c;
    
    static DArray<TermList> args(8);
    args.ensure(idx);
    while (idx-->0) {
      CPair pair = _cInfos[d].namedPair;
      ASS(pair !=CPair(0,0)); 
      args[idx] = normalForms.get(deref(pair.second));
      ASS(args[idx].isTerm());
      d = pair.first;
    }
    ASS_EQ(_cInfos[d].sigSymbol,t->functor());
    if(t->isSort()){
      cInfo.normalForm = TermList(AtomicSort::create(static_cast<AtomicSort*>(t),args.array()));
    } else {
      cInfo.normalForm = TermList(Term::create(t,args.array()));
    }
  }
}

/**
 * Return a reduced rewrite system compatible with the current ordering
 * equivalent to the previously given positive equations
 * in the form of a set of equational literals.
 * 
 * Based on the algorithm suggested by Wayne Snyder (1993),
 * A Fast Algorithm for Generating Reduced Ground Rewriting Systems from a Set of Ground Equations.
 */
void SimpleCongruenceClosure::getModel(LiteralStack& model)
{
  // a heap of candidate constants to process
  static DynamicHeap<unsigned, ConstOrderingComparator, ArrayMap<unsigned> >
    candidates(ConstOrderingComparator(_cInfos,*_ord));
  ASS(candidates.isEmpty());
  
  // a map of already computed normal forms, indexed by class representatives
  static NFMap normalForms;
  normalForms.reset();
  
  unsigned maxConst = getMaxConst();
  candidates.elMap().expand(maxConst+1);
  
  // we process even predicates/literals but they will not show up in the result
  // (because they are not in a class associated with a term)
  
  // moreover, it is encouraged that a separate copy of the SimpleCongruenceClosure
  // is used for the purposes of calling getModel, a copy which never receives predicates or even dis-equalities

  // initialize the info associated with constants
  // and the heap of potential candidates    
  for (unsigned c = 3; // skip 0=unused, 1=_posLitConst and 2=_negLitConst
                c <= maxConst; c++) {
    ConstInfo& cInfo = _cInfos[c];
    
    // cout << "init for " << c;
    
    cInfo.processed = false;    
    cInfo.half_normalized = false;
    cInfo.normalForm = TermList::empty();
    
    if (cInfo.sigSymbol != NO_SIG_SYMBOL) { // either a symbol ...
      // cout << " is sigsym ";
      
      // this copies the term for constants and an empty termlist for function symbols of arity > 0
      cInfo.normalForm = cInfo.term;
      
      //cout << " and term " << cInfo.term.toString() << endl;      
      //if (cInfo.lit) {
      //  cout << " and a lit " << cInfo.lit->toString() << endl;
      //}
      
      // it is important that the normal form has already been assigned, 
      // since the insertion into the heap starts calling compare
      candidates.insert(c);
    } else { // ... or a named pair      
    	ASS_NEQ(cInfo.namedPair.first,0);
      ASS_NEQ(cInfo.namedPair.second,0);
      
      //cout << " names a pair (" << cInfo.namedPair.first << ", " 
      //                          << cInfo.namedPair.second << ")";
      //cout << " and term " << cInfo.term.toString();      
      //if (cInfo.lit) {
      //  cout << " and a lit " << cInfo.lit->toString();
      //}
      
      unsigned lRep = deref(cInfo.namedPair.first);
      unsigned rRep = deref(cInfo.namedPair.second);
      ASS_NEQ(lRep,c); // the left child is necessarily in a different class
      _cInfos[lRep].upEdges.push(c);
      
      //cout << " adds a left edge to " << lRep;
      
      if (rRep != deref(c)) { // for the right child, don't store upEdges within a class
        _cInfos[rRep].upEdges.push(c);
        
        //cout << " adds a right edge to " << rRep;
      }
      //cout << endl;      
    }
  }
  
  // cout << "processing" << endl;
  
  while (!candidates.isEmpty()) {
    unsigned c = candidates.pop();
    unsigned r = deref(c);
    ConstInfo& rInfo = _cInfos[r];
    
    //cout << "Picking candidate " << c << " with repr " << r << endl;
    
    if (!rInfo.processed) {
      //cout << "Class not processed yet." << endl;
      ConstInfo& cInfo = _cInfos[c];

      rInfo.processed = true;
      if (!cInfo.normalForm.isEmpty()) { // Are we considering a class with true terms (not partial applications)?
        // c was the smallest candidate from this class,
        // we will now keep c's normal form under the index r for everybody "above" to access
        ALWAYS(normalForms.insert(r,cInfo.normalForm));
      }

      Stack<unsigned>::Iterator rUseIt(rInfo.upEdges);
      while(rUseIt.hasNext()) {
        unsigned s = rUseIt.next(); // a super-term refers to a term in our class

        // Giles suggested an optimization: don't consider terms from already normalized classes as candidates
        if (_cInfos[deref(s)].processed) {
          continue;
        }

        ConstInfo& sInfo = _cInfos[s];
        ASS(sInfo.namedPair!=CPair(0,0)); //must be a name of a pair
        
        // cout << "superterm " << s;
        
        if (sInfo.half_normalized) {
          // now becomes fully
          computeConstsNormalForm(s,normalForms);

          //cout << " gets its normal form " << sInfo.normalForm.toString();

          // and can be inserted among candidates
          candidates.insert(s);
        } else {
          //cout << " becoming half_normalized";

          sInfo.half_normalized = true;
        }
        
        //cout << endl;
      }
      // upEdges must be kept clean between the calls to this function
      rInfo.upEdges.reset();
    }
  }
  
  // start preparing the result
  // avoid generating trivial equalities and duplicates (by hashing the pairs)
  NFMap::Iterator nfIt(normalForms);
  while(nfIt.hasNext()) {
    unsigned r;
    TermList nf;
    nfIt.next(r,nf);
    
    //cout << "Outputting for class " << r << " with nf " << nf.toString() << endl;
    
    static DHSet<TermList> seen;
    seen.reset();
    seen.insert(nf); // this way we avoid generating the identity equality
    
    // for the representative
    computeConstsNormalForm(r,normalForms); // maybe calling for a second time, but that's cheap    
    if (!seen.contains(_cInfos[r].normalForm)) {
      //cout << _cInfos[r].normalForm << " -> " << nf << endl;
      model.push(Literal::createEquality(true,_cInfos[r].normalForm,nf,SortHelper::getResultSort(nf.term())));
      seen.insert(_cInfos[r].normalForm);
    }
    
    // and for all other members of the class
    Stack<unsigned>::Iterator classIt(_cInfos[r].classList);
    while (classIt.hasNext()) {
      unsigned c = classIt.next();
      
      computeConstsNormalForm(c,normalForms); // maybe calling for a second time, but that's cheap
      if (!seen.contains(_cInfos[c].normalForm)) {
        //cout << _cInfos[c].normalForm << " -> " << nf << endl;
        model.push(Literal::createEquality(true,_cInfos[c].normalForm,nf,SortHelper::getResultSort(nf.term())));
        seen.insert(_cInfos[c].normalForm);
      }
    }
  }
  
  DEBUG_CODE( assertModelInfoClean(); );  
}

#if VDEBUG
void SimpleCongruenceClosure::assertModelInfoClean() const
{
  unsigned maxConst = getMaxConst();
  for (unsigned c = 0; c <= maxConst; c++) {
    const ConstInfo& cInfo = _cInfos[c];
    ASS(cInfo.upEdges.isEmpty());
  }
}
#endif 

}