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
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
/*
 * 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 AnswerLiteralManager.cpp
 * Implements class AnswerLiteralManager.
 */

#include <set>

#include "Lib/DArray.hpp"
#include "Lib/Environment.hpp"
#include "Lib/Stack.hpp"
#include "Lib/StringUtils.hpp"

#include "Kernel/Signature.hpp"
#include "Kernel/Clause.hpp"
#include "Kernel/Formula.hpp"
#include "Kernel/FormulaUnit.hpp"
#include "Kernel/Inference.hpp"
#include "Kernel/MainLoop.hpp"
#include "Kernel/Problem.hpp"
#include "Kernel/SortHelper.hpp"
#include "Kernel/SubstHelper.hpp"
#include "Kernel/TermIterators.hpp"
#include "Kernel/OperatorType.hpp"
#include "Kernel/InterpretedLiteralEvaluator.hpp"
#include "Kernel/TermIterators.hpp"

#include "Shell/Options.hpp"

#include "Parse/TPTP.hpp"

#include "AnswerLiteralManager.hpp"

static bool isProperAnswerClause(Clause* cl) {
  static bool ground_only = (env.options->questionAnswering() == Options::QuestionAnsweringMode::PLAIN) && (env.options->questionAnsweringGroundOnly());

  return !cl->isEmpty() && forAll(cl->iterLits(),[] (Literal* l) { return l->isAnswerLiteral() && (!ground_only || l->ground()); } );
}

namespace Inferences
{

Clause* AnswerLiteralResolver::simplify(Clause* cl)
{
  if (isProperAnswerClause(cl)) {
    return AnswerLiteralManager::getInstance()->getRefutation(cl);
  }
  return cl;
}

}


namespace Shell
{

using namespace std;
typedef List<pair<unsigned,pair<Clause*, Literal*>>> AnsList;

///////////////////////
// AnswerLiteralManager
//

AnswerLiteralManager* AnswerLiteralManager::getInstance()
{
  static AnswerLiteralManager* instance =
    (env.options->questionAnswering() == Options::QuestionAnsweringMode::PLAIN) ?
      static_cast<AnswerLiteralManager*>(new PlainALManager()) :
      ((env.options->questionAnswering() == Options::QuestionAnsweringMode::SYNTHESIS) ?
        static_cast<AnswerLiteralManager*>(new SynthesisALManager()) :
        nullptr);

  return instance;
}

void AnswerLiteralManager::addAnswerLiterals(Problem& prb)
{
  if(addAnswerLiterals(prb.units())) {
    prb.invalidateProperty();
  }
}

/**
 * Attempt adding answer literals into questions among the units
 * in the list @c units. Return true if some answer literal was added.
 */
bool AnswerLiteralManager::addAnswerLiterals(UnitList*& units)
{
  bool someAdded = false;
  UnitList::DelIterator uit(units);
  while(uit.hasNext()) {
    Unit* u = uit.next();
    Unit* newU = tryAddingAnswerLiteral(u);
    if(u!=newU) {
      someAdded = true;
      uit.replace(newU);
    }
  }
  return someAdded;
}

Unit* AnswerLiteralManager::tryAddingAnswerLiteral(Unit* unit)
{
  if (unit->isClause() || (unit->inputType()!=UnitInputType::CONJECTURE && unit->inputType()!=UnitInputType::NEGATED_CONJECTURE)) {
    return unit; // do nothing
  }

  Formula* topF = static_cast<FormulaUnit*>(unit)->formula();

  // it must start with a not
  if(topF->connective()!=NOT) {
    return unit; // do nothing
  }

  Formula* subNot = topF->uarg();

  bool skolemise = false;
  Formula* eQuant;
  if (subNot->connective() == EXISTS) {
    eQuant = subNot;
  } else if (subNot->connective() == FORALL) {
    skolemise = true;
    Formula* subForall = subNot->qarg();
    if (subForall->connective() == EXISTS) {
      eQuant = subForall;
    } else {
      return unit; // do nothing
    }
  } else {
    return unit; // do nothing
  }

  VList* eVars = eQuant->vars();
  SList* eSrts = eQuant->sorts();
  ASS(eVars);

  FormulaList* conjArgs = 0;
  FormulaList::push(eQuant->qarg(), conjArgs);
  Literal* ansLit = getAnswerLiteral(eVars,eSrts,eQuant);
  _originUnitsAndInjectedLiterals.insert(ansLit->functor(),make_pair(unit,ansLit));
  FormulaList::push(new AtomicFormula(ansLit), conjArgs);

  Formula* out = new NegatedFormula(new QuantifiedFormula(EXISTS, eVars, eSrts, new JunctionFormula(AND, conjArgs)));

  if (skolemise) {
    Map<unsigned,std::string>* questionVars = Parse::TPTP::findQuestionVars(unit->number());

    VList* fVars = subNot->vars();
    SList* fSrts = subNot->sorts();
    Substitution subst;
    while (VList::isNonEmpty(fVars)) {
      unsigned var = fVars->head();
      fVars = fVars->tail();
      unsigned skFun = env.signature->addSkolemFunction(/*arity=*/0, /*suffix=*/"in");
      Signature::Symbol* skSym = env.signature->getFunction(skFun);
      if ((env.options->questionAnswering() == Options::QuestionAnsweringMode::SYNTHESIS)) {
        ALWAYS(static_cast<Shell::SynthesisALManager*>(Shell::SynthesisALManager::getInstance())->addIntroducedComputableSymbol(make_pair(skFun, /*isPredicate=*/false)));
      }
      TermList sort;
      if (SList::isNonEmpty(fSrts)) {
        sort = fSrts->head();
        fSrts = fSrts->tail();
      } else if (!SortHelper::tryGetVariableSort(var, subNot, sort)) {
        sort = AtomicSort::defaultSort();
      }
      OperatorType* ot = OperatorType::getConstantsType(sort);
      skSym->setType(ot);
      Term* skTerm = Term::create(skFun, /*arity=*/0, /*args=*/nullptr);
      subst.bindUnbound(var, skTerm);
      recordSkolemBinding(skTerm, var, questionVars ? questionVars->get(var) : TermList(var,false).toString() );
    }
    out = SubstHelper::apply(out, subst);
  }

  return new FormulaUnit(out, FormulaClauseTransformation(skolemise ? InferenceRule::ANSWER_LITERAL_INPUT_SKOLEMISATION : InferenceRule::ANSWER_LITERAL_INJECTION, unit));
}

TermList AnswerLiteralManager::possiblyEvaluateAnswerTerm(TermList aT)
{
  if(aT.isTerm() && !aT.term()->isSpecial()){
    InterpretedLiteralEvaluator eval;
    unsigned p = env.signature->addFreshPredicate(1,"p");
    TermList sort = SortHelper::getResultSort(aT.term());
    OperatorType* type = OperatorType::getPredicateType({sort});
    env.signature->getPredicate(p)->setType(type);
    Literal* l = Literal::create1(p,true,aT);
    Literal* res =0;
    bool constant, constTrue;
    bool litMod = eval.evaluate(l,constant,res,constTrue);
    if(litMod && res){
      aT.setTerm(res->nthArgument(0)->term());
    }
  }
  return aT;
}

void AnswerLiteralManager::tryOutputAnswer(Clause* refutation, std::ostream& out)
{
  Stack<Clause*> answer;

  if (!tryGetAnswer(refutation, answer)) {
    return;
  }

  DHSet<unsigned> seenSkolems;

  out << "% SZS answers Tuple [";

  std::stringstream vss;
  optionalAnswerPrefix(vss);
  if (answer.size() > 1) {
    vss << "(";
  }
  Stack<Clause*>::BottomFirstIterator aIt(answer);
  while(aIt.hasNext()) {
    Clause* aCl = aIt.next();
    if (closeFreeVariablesForPrinting()) {
      auto varIt = aCl->getVariableIterator();
      if (varIt.hasNext()) {
        vss << "∀";
        while(varIt.hasNext()) {
          vss << TermList(varIt.next(),false).toString();
          if (varIt.hasNext()) {
            vss << ",";
          }
        }
        vss << ".";
      }
    }
    if (aCl->size() > 1) {
      vss << "(";
    }
    auto lIt = aCl->iterLits();
    while (lIt.hasNext()) {
      Literal* aLit = lIt.next();
      vss << "[";
      unsigned arity = aLit->arity();

      Map<unsigned,std::string>* questionVars = 0;
      std::pair<Unit*,Literal*> unitAndLiteral;
      if (_originUnitsAndInjectedLiterals.find(aLit->functor(),unitAndLiteral)) {
        questionVars = Parse::TPTP::findQuestionVars(unitAndLiteral.first->number());
      }

      for(unsigned i=0; i<arity; i++) {
        if(i > 0) {
          vss << ',';
        }
        if (questionVars) {
          vss << questionVars->get(unitAndLiteral.second->nthArgument(i)->var()) << "->";
        }
        TermList evalauted = possiblyEvaluateAnswerTerm(*aLit->nthArgument(i));
        if (evalauted.isTerm()){ // just check which Skolems we might have used
          NonVariableNonTypeIterator it(evalauted.term(),/*includeSelf=*/true);
          while (it.hasNext()) {
            unsigned f = it.next()->functor();
            std::pair<unsigned,Unit*> dummy;
            if (_skolemsOrigin.find(f,dummy)) {
              seenSkolems.insert(f);
            }
          }
        }
        vss << evalauted;
      }
      vss << "]";
      if(lIt.hasNext()) {
        vss << "|";
      }
    }
    if (aCl->size() > 1) {
      vss << ")";
    }
    if(aIt.hasNext()) {
      vss << "|";
    }
  }
  if (answer.size() > 1) {
    vss << ")";
  }
  out << postprocessAnswerString(vss.str());
  out << "|_] for " << env.options->problemName() << endl;

  // recall what the skolems mean:
  DHSet<unsigned>::Iterator it(seenSkolems);
  while (it.hasNext()) {
    unsigned f = it.next();
    const std::pair<unsigned,Unit*>& origin = _skolemsOrigin.get(f);
    out << "%    " << env.signature->getFunction(f)->name() << " introduced for " << TermList(origin.first,false).toString() << " in " << origin.second->toString() << endl;
  }
}

static bool pushFirstPremiseToAnswerIfFromResolver(Inference& inf, Stack<Clause*>& answer)
{
  if (inf.rule() == InferenceRule::UNIT_RESULTING_RESOLUTION) {
    auto it = inf.iterator();
    ASS(inf.hasNext(it));
    Clause* firstParent = inf.next(it)->asClause();
    // cout << firstParent->toNiceString() << endl;
    if (isProperAnswerClause(firstParent)) {
      answer.push(firstParent);
      return true;
    }
  }
  return false;
}

bool AnswerLiteralManager::tryGetAnswer(Clause* refutation, Stack<Clause*>& answer)
{
  ASS(refutation->isEmpty());

  Inference& inf = refutation->inference();
  if (pushFirstPremiseToAnswerIfFromResolver(inf,answer)) {
    return true;
  } else if (inf.rule() == InferenceRule::AVATAR_REFUTATION) {
    bool added = false;
    auto it = inf.iterator();
    while (inf.hasNext(it)) {
      Unit* prem = inf.next(it);
      Inference& inf2 = prem->inference();
      if (inf2.rule() == InferenceRule::AVATAR_CONTRADICTION_CLAUSE) {
        auto it2 = inf2.iterator();
        Unit* anEmpty = inf2.next(it2);
        ASS(!inf2.hasNext(it2));
        Inference& inf3 = anEmpty->inference();
        added |= pushFirstPremiseToAnswerIfFromResolver(inf3,answer);
      }
    }
    return added;
  }
  // currently does nothing for AVATAR_REFUTATION_SMT (which does not get minimized)

  return false;
}

Literal* AnswerLiteralManager::getAnswerLiteral(VList* vars,SList* srts,Formula* f)
{
  static Stack<TermList> litArgs;
  litArgs.reset();
  TermStack sorts;
  while(VList::isNonEmpty(vars)) {
    unsigned var = vars->head();
    vars = vars->tail();
    TermList sort;
    if (SList::isNonEmpty(srts)) {
      sort = srts->head();
      srts = srts->tail();
    } else if(!SortHelper::tryGetVariableSort(var, f, sort)) {
      sort = AtomicSort::defaultSort();
    }
    litArgs.push(TermList(var, false));
    sorts.push(sort);
  }

  unsigned vcnt = litArgs.size();
  unsigned pred = env.signature->addFreshPredicate(vcnt,"ans");
  Signature::Symbol* predSym = env.signature->getPredicate(pred);
  predSym->setType(OperatorType::getPredicateType(sorts.size(), sorts.begin()));
  predSym->markAnswerPredicate();
  // don't need equality proxy for answer literals
  predSym->markSkipCongruence();
  if ((env.options->questionAnswering() == Options::QuestionAnsweringMode::SYNTHESIS)) {
    ALWAYS(static_cast<Shell::SynthesisALManager*>(Shell::SynthesisALManager::getInstance())->addIntroducedComputableSymbol(make_pair(pred, /*isPredicate=*/true)));
  }
  return Literal::create(pred, vcnt, true, litArgs.begin());
}

Clause* AnswerLiteralManager::getResolverClause(unsigned pred)
{
  Clause* res;
  if(_resolverClauses.find(pred, res)) {
    return res;
  }

  static Stack<TermList> args;
  args.reset();

  Signature::Symbol* predSym = env.signature->getPredicate(pred);
  ASS(predSym->answerPredicate());
  unsigned arity = predSym->arity();

  for(unsigned i=0; i<arity; i++) {
    args.push(TermList(i, false));
  }
  Literal* lit = Literal::create(pred, arity, true, args.begin());
  res = Clause::fromIterator(getSingletonIterator(lit),NonspecificInference0(UnitInputType::AXIOM,InferenceRule::ANSWER_LITERAL_RESOLVER));

  _resolverClauses.insert(pred, res);
  return res;
}

Clause* AnswerLiteralManager::getRefutation(Clause* answer)
{
  unsigned clen = answer->length();
  UnitList* premises = 0;

  for(unsigned i=0; i<clen; i++) {
    Clause* resolvingPrem = getResolverClause((*answer)[i]->functor());
    UnitList::push(resolvingPrem, premises);
  }

  // finally, put in the actual answer clause (for an easier retrieval later)
  UnitList::push(answer, premises);

  Clause* refutation = Clause::fromIterator(LiteralIterator::getEmpty(),
      GeneratingInferenceMany(InferenceRule::UNIT_RESULTING_RESOLUTION, premises));
  return refutation;
}

///////////////////////
// PlainALManager
//

void PlainALManager::recordSkolemBinding(Term* skT,unsigned var,std::string vName)
{
  _skolemNames.push(std::make_pair(skT,vName));
}

void PlainALManager::optionalAnswerPrefix(std::ostream& out)
{
  if(!_skolemNames.isEmpty()) {
    out << "λ";
    auto it =  _skolemNames.iterFifo();
    while (it.hasNext()) {
      out << it.next().first->toString();
      if (it.hasNext())
        out << ",";
    }
    out << ".";
  }
}

std::string PlainALManager::postprocessAnswerString(std::string answer)
{
  /** string replacement is not ideal:
   * - substrings elsewhere could be rewritten (not just the intended skolems)
   * - also, if the users chooses a unfortunate variable names
   *   (with the "question" role, the TPTP parser is storing the original names for us)
   *   new clashes might arise
   *
   * However, creating a symbol which looks like a variable
   * (but is not, since vampire's variables are just unsigneds n, printed as "X<n>")
   * and starts with an uppercase character (not allowed for anything else than variables in TPTP)
   * sounded like too much pain.
  */

  auto it =  _skolemNames.iter();
  while (it.hasNext()) {
    auto [skT,vName] = it.next();
    std::string to;
    Lib::StringUtils::replaceAll(answer,skT->toString(),vName);
  }
  return answer;
}

///////////////////////
// SynthesisALManager
//

void SynthesisALManager::getNeededUnits(Clause* refutation, ClauseStack& premiseClauses, Stack<Unit*>& conjectures, DHSet<Unit*>& allProofUnits)
{
  Stack<Unit*> toDo;
  toDo.push(refutation);

  while(toDo.isNonEmpty()) {
    Unit* curr = toDo.pop();
    if(!allProofUnits.insert(curr)) {
      continue;
    }
    Inference& inf = curr->inference();
    InferenceRule infRule = inf.rule();
    if(infRule==InferenceRule::NEGATED_CONJECTURE) {
      conjectures.push(curr);
    }
    if(infRule==InferenceRule::CLAUSIFY ||
	    (curr->isClause() && (infRule==InferenceRule::INPUT || infRule==InferenceRule::NEGATED_CONJECTURE )) ){
      ASS(curr->isClause());
      premiseClauses.push(curr->asClause());
    }
    auto it = inf.iterator();
    while(inf.hasNext(it)) {
      Unit* premise = inf.next(it);
      toDo.push(premise);
    }
  }
}

void SynthesisALManager::recordSkolemBinding(Term* skTerm, unsigned var, std::string)
{
  // TODO(hzzv): use the var name
  _skolemReplacement.bindSkolemToTermList(skTerm, TermList(var, false));
}

bool SynthesisALManager::tryGetAnswer(Clause* refutation, Stack<Clause*>& answer)
{
  if (!_lastAnsLit && AnsList::isEmpty(_answerPairs)) {
    return false;
  }
  if (_lastAnsLit) {
    AnsList::push(make_pair(0, make_pair(nullptr, _lastAnsLit)), _answerPairs);
  }

  _skolemReplacement.associateRecMappings(&_recursionMappings, &_functionHeads);

  ClauseStack premiseClauses;
  Stack<Unit*> conjectures;
  DHSet<Unit*> proofUnits;
  getNeededUnits(refutation, premiseClauses, conjectures, proofUnits);
  DHSet<unsigned> proofNums;
  DHSet<Unit*>::Iterator puit(proofUnits);
  while (puit.hasNext()) proofNums.insert(puit.next()->number());

  // We iterate through the stored _answerPairs. An answer pair p is relevant if:
  // - either it is the _lastAnsLit (i.e., has p.first==0)
  // - or it corresponds to a clause contained in the proof (i.e., proofNums.contains(p.first))
  // We construct an answer by nesting if-then-elses, using as the then-branch the current
  // answer pair, and as the else-branch the program constructed so far.
  AnsList::Iterator it(_answerPairs);
  ALWAYS(it.hasNext());
  pair<unsigned, pair<Clause*, Literal*>> p = it.next();
  while (p.first > 0 && !proofNums.contains(p.first) && it.hasNext()) p = it.next();
  ASS(p.first == 0 || proofNums.contains(p.first));
  // The first relevant answer literal:
  Literal* origLit = p.second.second;
  unsigned arity = origLit->arity();
  Stack<TermList> answerArgs(arity);
  Stack<TermList> sorts(arity);
  // Initialization: each answer is set to the answer from origLit.
  for (unsigned i = 0; i < arity; i++) {
    sorts.push(env.signature->getPredicate(origLit->functor())->predType()->arg(i));
    answerArgs.push(_skolemReplacement.transformTermList(*origLit->nthArgument(i), sorts[i]));
  }
  // Go through all other answer pairs and use the relevant ones.
  while(it.hasNext()) {
    p = it.next();
    ASS(p.second.first != nullptr);
    if (!proofNums.contains(p.first)) {
      continue;
    }
    ASS_EQ(p.first, p.second.first->number());
    // Create the condition for an if-then-else by negating the clause
    Formula* condition = getConditionFromClause(p.second.first);
    for (unsigned i = 0; i < arity; i++) {
      ASS_EQ(sorts[i], env.signature->getPredicate(p.second.second->functor())->predType()->arg(i));
      // Construct the answer using if-then-else
      answerArgs[i] = TermList(Term::createITE(condition, _skolemReplacement.transformTermList(*p.second.second->nthArgument(i), sorts[i]), answerArgs[i], sorts[i]));
    }
  }
  // just a single literal answer
  answer.push(Clause::fromLiterals({Literal::create(origLit,answerArgs.begin())}, NonspecificInference0(UnitInputType::AXIOM,InferenceRule::INPUT)));

  outputRecursiveFunctions();

  return true;
}

void SynthesisALManager::onNewClause(Clause* cl)
{
  if(!cl->noSplits() || cl->isEmpty() || (cl->length() != 1) || !(*cl)[0]->isAnswerLiteral()) {
    return;
  }

  ASS(cl->hasAnswerLiteral())

  Literal* lit = cl->getAnswerLiteral();
  if (!isComputableOrVar(lit))
    return;
  _lastAnsLit = lit;

  Clause* refutation = getRefutation(cl);
  throw MainLoop::RefutationFoundException(refutation);
}

Clause* SynthesisALManager::recordAnswerAndReduce(Clause* cl) {
  if (!cl->noSplits() || !cl->hasAnswerLiteral() || !isComputable(cl)) {
    return nullptr;
  }
  // Check if the answer literal has only distinct variables as arguments.
  // If yes, we do not need to record the clause, because the answer literal
  // represents any answer.
  bool removeDefaultAnsLit = true;
  Literal* ansLit = cl->getAnswerLiteral();
  Set<unsigned> vars;
  for (unsigned i = 0; i < ansLit->numTermArguments(); ++i) {
    TermList* tl = ansLit->nthArgument(i);
    if (!tl->isVar()) {
      removeDefaultAnsLit = false;
      break;
    }
    vars.insert(tl->var());
  }
  if (vars.size() != ansLit->numTermArguments()) {
    removeDefaultAnsLit = false;
  }

  RStack<Literal*> resLits;
  for (Literal* curr : cl->iterLits()) {
    if (curr != ansLit) {
      resLits->push(curr);
    }
  }
  auto newCl = Clause::fromStack(*resLits,
      Inference(SimplifyingInference1(InferenceRule::ANSWER_LITERAL_REMOVAL, cl)));
  if (!removeDefaultAnsLit) {
    AnsList::push(make_pair(newCl->number(), make_pair(newCl, ansLit)), _answerPairs);
  } else {
    _lastAnsLit = ansLit;
  }
  return newCl;
}

Literal* SynthesisALManager::makeITEAnswerLiteral(Literal* condition, Literal* thenLit, Literal* elseLit) {
  ASS(Literal::headersMatch(thenLit, elseLit, /*complementary=*/false));

  Signature::Symbol* predSym = env.signature->getPredicate(thenLit->functor());
  Stack<TermList> litArgs;
  Term* condTerm = translateToSynthesisConditionTerm(condition);
  for (unsigned i = 0; i < thenLit->arity(); ++i) {
    TermList* ttl = thenLit->nthArgument(i);
    TermList* etl = elseLit->nthArgument(i);
    if (ttl == etl) {
      litArgs.push(*ttl);
    } else {
      litArgs.push(TermList(createRegularITE(condTerm, *ttl, *etl, predSym->predType()->arg(i))));
    }
  }
  return Literal::create(thenLit->functor(), thenLit->arity(), thenLit->polarity(), litArgs.begin());
}

void SynthesisALManager::pushEqualityConstraints(LiteralStack* ls, Literal* thenLit, Literal* elseLit) {
  ASS_EQ(thenLit->functor(), elseLit->functor());
  for (unsigned i = 0; i < thenLit->arity(); ++i) {
    TermList& t = *thenLit->nthArgument(i);
    TermList& e = *elseLit->nthArgument(i);
    if (t != e) {
      ls->push(Literal::createEquality(false, t, e, env.signature->getPredicate(thenLit->functor())->predType()->arg(i)));
    }
  }
}

Formula* SynthesisALManager::getConditionFromClause(Clause* cl) {
  FormulaList* fl = FormulaList::empty();
  for (unsigned i = 0; i < cl->length(); ++i) {
    Literal* newLit = Literal::complementaryLiteral(_skolemReplacement.transformLiteral((*cl)[i]));
    FormulaList::push(new AtomicFormula(newLit), fl);
  }
  return JunctionFormula::generalJunction(Connective::AND, fl);
}

/** Create a new complex term, with its top-level function symbol
 *  created as a dummy symbol representing the predicate of @b l, and copy
 *  from the array @b args its arguments. Insert it into the sharing
 *  structure if all arguments are shared.
 */
Term* SynthesisALManager::translateToSynthesisConditionTerm(Literal* l)
{
  ASS_EQ(l->getPreDataSize(), 0);
  ASS(!l->isSpecial());

  unsigned arity = l->arity();
  std::string fnName = "cond_";
  if (l->isNegative()) {
    fnName.append("not_");
  }
  fnName.append(l->predicateName());
  if (l->isEquality()) {
    fnName.append(SortHelper::getEqualityArgumentSort(l).toString());
  }
  bool added = false;
  unsigned fn = env.signature->addFunction(fnName, arity, added);
  // Store the mapping between the function and predicate symbols
  _skolemReplacement.addCondPair(fn, l->functor());
  if (added) {
    Signature::Symbol* sym = env.signature->getFunction(fn);
    Stack<TermList> argSorts;
    if (l->isEquality()) {
      TermList as = SortHelper::getEqualityArgumentSort(l);
      argSorts.push(as);
      argSorts.push(as);
    } else {
      OperatorType* ot = env.signature->getPredicate(l->functor())->predType();
      for (unsigned i = 0; i < arity; ++i) {
        argSorts.push(ot->arg(i));
      }
      if (isPredicateComputable(l->functor())) {
        ALWAYS(_introducedComputable.insert(make_pair(fn, /*isPredicate=*/false)));
      }
    }
    sym->setType(OperatorType::getFunctionType(arity, argSorts.begin(), AtomicSort::defaultSort()));
  }
  
  Stack<TermList> args;
  for (unsigned i = 0; i < arity; ++i) {
    args.push(*(l->nthArgument(i)));
  }
  return Term::create(fn, arity, args.begin());
}

/**
 * Create a (condition ? thenBranch : elseBranch) expression
 * and return the resulting term
 */
Term* SynthesisALManager::createRegularITE(Term* condition, TermList thenBranch, TermList elseBranch, TermList branchSort)
{
  unsigned itefn = getITEFunctionSymbol(branchSort);
  return Term::create(itefn, {TermList(condition), thenBranch, elseBranch});
}

void SynthesisALManager::ConjectureSkolemReplacement::bindSkolemToTermList(Term* t, TermList&& tl) {
  ASS(!_skolemToTermList.find(t));
  if (static_cast<SynthesisALManager*>(SynthesisALManager::getInstance())->isFunctionComputable(t->functor())) {
    ++_numInputSkolems;
  }
  _skolemToTermList.insert(t, std::move(tl));
}

TermList getConstantForVariable(TermList sort) {
  static TermList zero(theory->representConstant(IntegerConstantType(0)));
  if (sort == AtomicSort::intSort()) {
    return zero;
  } else {
    std::string name = "cz_" + sort.toString();
    unsigned czfn;
    if (!env.signature->tryGetFunctionNumber(name, 0, czfn)) {
      czfn = env.signature->addFreshFunction(0, name.c_str());
      env.signature->getFunction(czfn)->setType(OperatorType::getConstantsType(sort));
    } 
    return TermList(Term::createConstant(czfn));
  }
}

TermList SynthesisALManager::ConjectureSkolemReplacement::transformTermList(TermList tl, TermList sort) {
  // First replace free variables by 0-like constants
  if (tl.isVar() || (tl.isTerm() && !tl.term()->ground())) {
    if (tl.isVar()) {
      return getConstantForVariable(sort);
    } else {
      Substitution s;
      std::set<unsigned> done;
      Kernel::VariableWithSortIterator vit(tl.term());
      while (vit.hasNext()) {
        pair<TermList, TermList> p = vit.next();
        unsigned v = p.first.var();
        TermList& vsort = p.second;
        if (done.count(v) == 0) {
          done.insert(v);
          s.bindUnbound(v, getConstantForVariable(vsort));
        }
      }
      tl = TermList(tl.term()->apply(s));
    }
  }
  // Then replace skolems by variables
  return transformSubterm(transform(tl));
}

TermList SynthesisALManager::ConjectureSkolemReplacement::transformSubterm(TermList trm) {
  if (trm.isTerm()) {
    TermList* res = _skolemToTermList.findPtr(trm.term());
    if (res) {
      return *res;
    }
    Term* t = trm.term();
    unsigned functor = t->functor();
    if (static_cast<SynthesisALManager*>(SynthesisALManager::getInstance())->isRecTerm(t)) {
      // Construct a new recursive function corresponding to 'trm'.
      ASS(_recursionMappings->find(functor));
      Function* recf = new Function(functor, this);
      SimpleSkolemReplacement ssr(&recf->_skolemToTermList);
      Term* transformed = ssr.transform(t);
      recf->addCases(transformed);
      // If the cases of the recursive function contain other recursive functions,
      // their definitions might need skolem replacement corresponding to this function.
      for (unsigned i = 0; i < transformed->arity()-1; ++i) {
        // Iterate over cases and replace only the associated skolems in each.
        TermList* narg = transformed->nthArgument(i);
        DHMap<Term*, TermList>* m = recf->_skolemToTermListForCase.findPtr(i);
        if (narg->isTerm() && m) {
          ssr.setMap(m);
          NonVariableIterator it(narg->term());
          while (it.hasNext()) {
            TermList tl = it.next();
            ASS(tl.isTerm());
            Function** fptr = _functions.findPtr(tl.term()->functor());
            if (fptr) {
              for (unsigned j = 0; j < (*fptr)->_cases.size(); ++j) {
                TermList& c = (*fptr)->_cases[j];
                if (c.isTerm()) {
                  (*fptr)->_cases[j] = TermList(ssr.transform(c.term()));
                }
              }
            }
          }
        }
      }
      unsigned rfunctor = recf->_functor;
      _functions.insert(rfunctor, recf);
      // Replace 'trm' by the function called on the last argument of this 'trm'.
      return TermList(Term::create(rfunctor, {*t->nthArgument(t->arity()-1)}));
    } else if ((t->arity() == 3) && t->nthArgument(0)->isTerm()) {
      TermList sort = env.signature->getFunction(functor)->fnType()->arg(1);
      if (t->functor() == static_cast<SynthesisALManager*>(SynthesisALManager::getInstance())->getITEFunctionSymbol(sort)) {
        // Build condition
        Term* tcond = t->nthArgument(0)->term();
        std::string condName = tcond->functionName();
        unsigned pred = _condFnToPred.get(tcond->functor());
        Literal* newCond;
        if (env.signature->isEqualityPredicate(pred)) {
          newCond = Literal::createEquality(/*polarity=*/true, *tcond->nthArgument(0), *tcond->nthArgument(1), sort);
        } else {
          newCond = Literal::createFromIter(pred, /*polarity=*/true, anyArgIter(tcond));
        }
        // Build the whole ITE term
        return TermList(Term::createITE(new AtomicFormula(newCond), *(t->nthArgument(1)), *(t->nthArgument(2)), sort));
      }
    }
  }
  return trm;
}

void SynthesisALManager::ConjectureSkolemReplacement::outputRecursiveFunctions() {
  VirtualIterator<Function*> it = _functions.range();
  if (it.hasNext()) {
    cout << "% Recursive function definitions:" << endl;
    do {
      cout << it.next()->toString();
    } while (it.hasNext());
  }
}

SynthesisALManager::ConjectureSkolemReplacement::Function::Function(unsigned recFunctor, ConjectureSkolemReplacement* replacement) {
  // Store the heads of each case of the new function
  _caseHeads = replacement->_functionHeads->findPtr(recFunctor);
  ASS(_caseHeads);
  _cases.ensure(_caseHeads->size());
  // Add the new function to signature
  OperatorType* ot = env.signature->getFunction(recFunctor)->fnType();
  TermList in = ot->arg(ot->arity()-1);
  TermList out = ot->arg(0);
  ASS_EQ(env.signature->getTermAlgebraOfSort(in)->nConstructors(), _caseHeads->size());
  _functor = env.signature->addFreshFunction(/*arity=*/1, "rf");
  Signature::Symbol* f = env.signature->getFunction(_functor);
  f->setType(OperatorType::getFunctionType({in}, out));
  // Process SkolemTrackers corresponding to this function:
  // populate the maps mapping skolems to terms they represent.
  DHMap<Term*, TermList>* caseMap;
  const DHMap<unsigned, SkolemTracker>& mapping = replacement->_recursionMappings->get(recFunctor);
  DHMap<unsigned, SkolemTracker>::Iterator it(mapping);
  while (it.hasNext()) {
    unsigned var;
    SkolemTracker& st = it.nextRef(var);
    ASS_EQ(var, st.binding.first);
    ASS(!_skolemToTermList.find(st.binding.second));
    TermList tl(st.recursiveCall ? TermList(Term::create1(_functor, *(*_caseHeads)[st.constructorId]->nthArgument(st.indexInConstructor))) : TermList(var, false));
    _skolemToTermList.insert(st.binding.second, tl);
    _skolemToTermListForCase.getValuePtr(st.constructorId, caseMap);
    caseMap->insert(st.binding.second, tl);
  }
}

void SynthesisALManager::ConjectureSkolemReplacement::Function::addCases(Term* t) {
  ASS(_cases.size() == t->arity()-1);
  for (unsigned i = 0; i < t->arity()-1; ++i) {
    _cases[i] = *t->nthArgument(i);
  }
}

void SynthesisALManager::printSkolemTrackers() {
  cout << "Skolem mappings:" << endl;
  DHMap<unsigned, SkolemTracker*>::Iterator it(_skolemTrackers);
  while (it.hasNext()) {
    SkolemTracker* st = it.next();
    cout << st->toString() << endl;
  }
}

void SynthesisALManager::printRecursionMappings() {
  cout << "Recursion mappings:" << endl;
  RecursionMappings::Iterator rit(_recursionMappings);
  unsigned v;
  while (rit.hasNext()) {
    unsigned recFn;
    auto& m = rit.nextRef(recFn);
    cout << "  recFn " << recFn << ":" << endl; 
    DHMap<unsigned, SkolemTracker>::Iterator mit(m);
    while (mit.hasNext()) {
      SkolemTracker& s = mit.nextRef(v);
      cout << v << ": " << s.toString() << endl;
    }
  }
}

void SynthesisALManager::registerSkolemSymbols(Term* recTerm, const Substitution& subst, const std::vector<Term*>& functionHeadsByConstruction, vector<SkolemTracker>& incompleteTrackers, const VList* us) {
  unsigned recFnId = recTerm->functor();
  unsigned ctorNumber = recTerm->arity()-1;
  ASS_EQ(ctorNumber, VList::length(us));
  ASS_EQ(ctorNumber, functionHeadsByConstruction.size());
  // Find out what is the order of arguments in `recTerm`, and
  // store the function heads in the correct indices in `_functionHeads`.
  // Each of the first `ctorNumber` argumentsi of the `recTerm` should be one of `us`.
  // The order of `us` is reverse to both the order of elements in `functionHeadsByConstruction`,
  // and to the `constructorId` of the SkolemTrackers.
  DArray<unsigned> ctorOrder(ctorNumber);
  vector<Term*> functionHeads(ctorNumber);
  VList::Iterator vit(us);
  unsigned i = 0;
  while (vit.hasNext()) {
    unsigned v = vit.next();
    DEBUG_CODE(bool found = false;)
    for (unsigned j = 0; j < ctorNumber; ++j) {
      TermList& arg = *(recTerm->nthArgument(j));
      ASS(arg.isVar());
      if (arg.var() == v) {
        ctorOrder[ctorNumber-i-1] = j;
        functionHeads[j] = functionHeadsByConstruction[ctorNumber-i-1];
        ++i;
        DEBUG_CODE(found = true;)
        break;
      }
    }
    ASS(found);
  }
  ALWAYS(_functionHeads.insert(recFnId, std::move(functionHeads)));

  // Finalize SkolemTrackers and store them.
  DHMap<unsigned, SkolemTracker>* mapping;
  ALWAYS(_recursionMappings.getValuePtr(recFnId, mapping));
  for (SkolemTracker& st : incompleteTrackers) {
    ASS_EQ(st.binding.second, nullptr);
    ASS_EQ(st.recFnId, 0);
    const unsigned var = st.binding.first;
    st.binding.second = subst.apply(var).term();
    st.recFnId = recFnId;
    st.constructorId = ctorOrder[st.constructorId];
    SkolemTracker* stp;
    ALWAYS(mapping->getValuePtr(var, stp, st));
    _skolemTrackers.insert(st.binding.second->functor(), stp);
  }
}

bool SynthesisALManager::isRecTerm(const Term* t) const {
  return (_recursionMappings.findPtr(t->functor()) != nullptr);
}

const SkolemTracker* SynthesisALManager::getSkolemTracker(unsigned skolemFunctor) const {
  return _skolemTrackers.get(skolemFunctor, nullptr);
}

bool SynthesisALManager::hasRecTerm(Literal* lit) {
  NonVariableIterator it(lit);
  while (it.hasNext()) {
    TermList tl = it.next();
    ASS(tl.isTerm());
    if (isRecTerm(tl.term())) {
      return true;
    }
  }
  return false;
}

bool SynthesisALManager::isFunctionComputable(unsigned functor) const {
  Signature::Symbol* s = env.signature->getFunction(functor);
  return (s->introduced() && _introducedComputable.contains(make_pair(functor, false))) ||
    (!s->introduced() && !_annotatedUncomputable.contains(make_pair(functor, false)));
}

bool SynthesisALManager::isPredicateComputable(unsigned functor) const {
  Signature::Symbol* s = env.signature->getPredicate(functor);
  return (s->introduced() && _introducedComputable.contains(make_pair(functor, true))) ||
    (!s->introduced() && !_annotatedUncomputable.contains(make_pair(functor, true)));
}

bool SynthesisALManager::computableOrVarHelper(const Term* t, DHMap<unsigned, unsigned>* recAncestors) const {
  ASS(t);
  unsigned f = t->functor();
  Signature::Symbol* symbol = env.signature->getFunction(f);

  if (!isFunctionComputable(f)) {
    // either an uncomputable symbol from the input, or an introduced symbol
    if (!symbol->skolem()) { // computability of skolems depends on the context, all else is really uncomputable
      return false;
    }
    if (!isRecTerm(t)) { // non-rec skolem terms need to be specifically allowed
      const Shell::SkolemTracker* st = getSkolemTracker(f);
      if (st == nullptr) {
         return false;
      }
      unsigned idx = 0;
      if (!recAncestors->find(st->recFnId, idx) || (idx != st->constructorId)) {
        return false;
      }
      return true;
    }
  }

  // Top functor is computable, now recurse.
  unsigned* idx = nullptr;
  if (isRecTerm(t)) {
    if (!recAncestors->getValuePtr(f, idx, -1)) {
      // TODO: investigate if we need to deal with answer literal in which
      // nested rec-term (of the same rec-function) occurs in some special way.
      // E.g., maybe we can replace "ans(rec(X0,rec(X1,sK2,X2),X3))"
      // by "X0 != X1 | sK2 != rec(X1,sK2,X2) | ans(rec(X0,sK2,X3))"
      return false;
    }
  }
  for (unsigned i = 0; i < t->numTermArguments(); i++) {
    const TermList tl = t->termArg(i);
    if (tl.isTerm()) { // else we have a variable, which needs no check
      if (idx) {
        *idx = i;
      }
      if (!computableOrVarHelper(tl.term(), recAncestors)) {
        return false;
      }
    }
  }
  if (idx) {
    recAncestors->remove(f);
  }

  return true;
}

bool SynthesisALManager::isComputableOrVar(const Term* t) const {
  DHMap<unsigned, unsigned> recAncestors;
  return computableOrVarHelper(t, &recAncestors);
}

bool SynthesisALManager::isComputableOrVar(const Literal* l) const {
  if (!isPredicateComputable(l->functor())) {
    return false;
  }
  for (unsigned i = 0; i < l->arity(); ++i) {
    const TermList* t = l->nthArgument(i);
    if (t->isTerm() && !isComputableOrVar(t->term())) {
      return false;
    }
  }
  return true;
}

bool SynthesisALManager::isComputable(const Clause* c) const {
  for (const Literal* l : c->iterLits()) {
    if (l->isAnswerLiteral()) {
      continue;
    }
    if (!isComputable(l)) {
      return false;
    }
  }
  return true;
}

}