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
/*
* 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 Shuffling.cpp
* Implementing the normalisation inference rule.
* @since 25/12/2003 Manchester
*/
#include "Lib/Environment.hpp"
#include "Lib/DArray.hpp"
#include "Kernel/Clause.hpp"
#include "Kernel/FormulaUnit.hpp"
#include "Kernel/Inference.hpp"
#include "Kernel/Problem.hpp"
#include "Kernel/Term.hpp"
#include "Kernel/Signature.hpp"
#include "Kernel/Unit.hpp"
#include "Shuffling.hpp"
using namespace std;
using namespace Kernel;
using namespace Shell;
/**
* Consistently flip predicate polarities in the given CNF Problem.
*/
void Shuffling::polarityFlip(Problem& prb)
{
DArray<bool> flippage(env.signature->predicates());
for (unsigned p = 0; p < flippage.size(); p++) {
auto pSymb = env.signature->getPredicate(p);
if (!pSymb->protectedSymbol() && !pSymb->termAlgebraDest() && !pSymb->termAlgebraDiscriminator()) {
// don't try to flip interpreted or otherwise protected predicates
// (this includes term algebra destructors which may go to bool and thus eventually become first-order predicates,
// as well as term algebra discriminators, which are to_bool to begin with)
ASS(p); // the equality predicate (at index 0) is protected
flippage[p] = Random::getBit();
if (flippage[p]) {
pSymb->markFlipped();
prb.addFlippedPredicate(p);
}
} else {
flippage[p] = false;
}
}
Stack<Literal*> newLits;
UnitList::RefIterator us(prb.units());
while (us.hasNext()) {
Unit* &u = us.next(); ASS(u->isClause()); Clause* cl = u->asClause();
// cout << "Before: " << cl->toString() << std::endl;
newLits.reset();
bool modified = false;
for (unsigned i = 0; i < cl->length(); i++) {
Literal* l = (*cl)[i];
// cout << " bef: " << l->toString() << std::endl;
if (flippage[l->functor()]) {
l = Literal::complementaryLiteral(l);
modified = true;
}
newLits.push(l);
// cout << " aft: " << l->toString() << std::endl;
}
// cout << "After: " << cl->toString() << std::endl;
if (modified) {
Clause* nc = Clause::fromStack(newLits,
NonspecificInferenceMany(InferenceRule::POLARITY_FLIPPING,UnitList::singleton(cl)));
u = nc; // replace the original in the Problem's list
}
}
}
/**
* Shuffle a problem
*/
void Shuffling::shuffle(Problem& prb)
{
shuffle(prb.units());
}
/**
* Shuffle a list of units by shuffling every unit in it and
* also shuffling the list itself.
* @since 09/06/2021 Prague
*/
void Shuffling::shuffle (UnitList*& units)
{
UnitList::Iterator us(units);
while (us.hasNext()) {
shuffle(us.next());
}
shuffleList(units);
} // Shuffling::shuffle (UnitList*)
void Shuffling::shuffle(Unit* unit)
{
// cout << "Bef: " << unit->toString() << std::endl;
if (unit->isClause()) {
shuffle(unit->asClause());
} else {
shuffle(static_cast<FormulaUnit*>(unit)->formula());
}
// cout << "Aft: " << unit->toString() << std::endl;
}
void Shuffling::shuffle(Clause* clause)
{
unsigned s = clause->numSelected();
// don't shuffle between selected and non-selected literals
shuffleArray(clause->literals(),s);
shuffleArray(clause->literals()+s,clause->length()-s);
// literals must be shared in typical clauses (I think), so let's not even try shuffling them
clause->notifyLiteralReorder();
}
// iterative implementation of shuffling Formula* / Literal* / TermList
void Shuffling::shuffleIter(Shufflable sh) {
static Stack<Shufflable> todo;
ASS(todo.isEmpty());
todo.push(sh);
do {
ASS(todo.isNonEmpty());
sh = todo.pop();
sh_updated:
if (sh.is<Formula*>()) {
Formula* fla = sh.unwrap<Formula*>();
fla_updated:
switch (fla->connective() ) {
case LITERAL:
{
sh = Shufflable(fla->literal());
goto sh_updated;
break; // I know, unreachable
}
case AND:
case OR:
{
// multi-ary; shuffling subformula list
shuffleList(*fla->argsPtr());
// recurse to subformulas
FormulaList::Iterator fs(fla->args());
while (fs.hasNext()) {
todo.push(Shufflable(fs.next()));
}
break;
}
case IFF:
case XOR:
{
// binary; can swap
if (Random::getBit()) {
fla->leftRightSwap();
}
// intentional fall-through
case IMP:
// binary; cannot simply swap
// in any case, recurse to subformulas
todo.push(Shufflable(fla->left()));
fla = fla->right();
goto fla_updated;
break; // I know, unreachable
}
case NOT:
{
// unary; just bubble through
fla = fla->uarg();
goto fla_updated;
break; // I know, unreachable
}
case FORALL:
case EXISTS:
{
//cout << "Shuffling FORALL/EXISTS: " << fla->toString() << std::endl;
// can't naively shuffle variables in the polymorphic case
// as we require type variables to come before term variable in the list
if(!env.getMainProblem()->hasPolymorphicSym()){
// can even shuffle the variables in the quantifier!
if (fla->sorts()) { // need to shuffle sorts in sync with vars, if they are there
shuffleTwoList(*fla->varsPtr(),*fla->sortsPtr());
} else {
shuffleList(*fla->varsPtr());
}
}
//cout << "getting: " << fla->toString() << std::endl;
fla = fla->qarg();
goto fla_updated;
break; // I know, unreachable;
}
case BOOL_TERM:
{
sh = Shufflable(fla->getBooleanTerm());
goto sh_updated;
break; // I know, unreachable;
}
case TRUE:
case FALSE:
{
// bottom reached; nothing to be done
break;
}
case NAME: // NamedFormula is only used by AVATAR proof documentation; so ignored here
default:
ASSERTION_VIOLATION;
}
} else if (sh.is<Literal*>()) {
Literal* lit = sh.unwrap<Literal*>();
if (!lit->shared()) {
if (lit->isEquality() && Random::getBit()) {
lit->argSwap();
}
Term::Iterator it(lit);
while (it.hasNext()) {
todo.push(Shufflable(it.next()));
}
}
} else {
ASS(sh.is<TermList>());
TermList tl = sh.unwrap<TermList>();
tl_updated:
if (tl.isTerm()) {
Term* t = tl.term();
if (!t->shared()) {
if (t->isSpecial()) {
Term::SpecialTermData* sd = t->getSpecialData();
switch (sd->specialFunctor()) {
case SpecialFunctor::ITE:
todo.push(Shufflable(sd->getITECondition()));
todo.push(Shufflable(*t->nthArgument(0)));
tl = *t->nthArgument(1);
goto tl_updated;
break; // I know, unreachable;
case SpecialFunctor::FORMULA:
todo.push(Shufflable(sd->getFormula()));
break;
case SpecialFunctor::LET:
todo.push(Shufflable(sd->getLetBinding()));
tl = *t->nthArgument(0);
goto tl_updated;
break; // I know, unreachable;
case SpecialFunctor::LAMBDA:
tl = sd->getLambdaExp();
goto tl_updated;
break; // I know, unreachable;
case SpecialFunctor::MATCH:
{
// treat as non-special (and don't shuffle the MATCH specifics)
Term::Iterator it(t);
while (it.hasNext()) {
todo.push(Shufflable(it.next()));
}
}
break;
default:
ASSERTION_VIOLATION_REP(tl.toString());
}
} else {
Term::Iterator it(t);
while (it.hasNext()) {
todo.push(Shufflable(it.next()));
}
}
}
}
}
} while (todo.isNonEmpty());
}