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
/*
* 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 TautologyDeletionISE.cpp
* Implements class TautologyDeletionISE.
*/
#include "Lib/Random.hpp"
#include "Lib/Environment.hpp"
#include "Lib/DArray.hpp"
#include "Kernel/Term.hpp"
#include "Kernel/Clause.hpp"
#include "Kernel/EqHelper.hpp"
#include "Shell/Statistics.hpp"
#include "TautologyDeletionISE.hpp"
using namespace Lib;
using namespace Kernel;
using namespace Inferences;
Clause* TautologyDeletionISE::simplify(Clause* c)
{
static DArray<Literal*> plits(32); // array of positive literals
static DArray<Literal*> nlits(32); // array of negative literals
int pos = 0;
int neg = 0;
int length = c->length();
plits.ensure(length);
nlits.ensure(length);
for (int i = length-1; i >= 0;i--) {
Literal* l = (*c)[i];
if (l->isNegative()) {
nlits[neg++] = l;
continue;
}
// l is positive
if (EqHelper::isEqTautology(l)) {
// literal t = t
env.statistics->equationalTautologies++;
return 0;
}
plits[pos++] = l;
}
if (pos == 0 || neg == 0) {
return c;
}
if (pos > 1) {
sort(plits.array(),pos);
}
if (neg > 1) {
sort(nlits.array(),neg);
}
int p = 0;
int n = 0;
for (;;) {
switch (compare(plits[p],nlits[n]))
{
case -1:
p++;
if (p >= pos) {
return c;
}
break;
case 0:
env.statistics->simpleTautologies++;
return 0;
case 1:
n++;
if (n >= neg) {
return c;
}
break;
}
}
}
/**
* Compare two literals using the following order:
* <ol>
* <li>First, using their predicate symbol numbers.</li>
* <li>Second, using their arguments (as void pointers).</li>
* </ol>
* @since 05/01/2008 Torrevieja
*/
int TautologyDeletionISE::compare(Literal* l1,Literal* l2)
{
unsigned f1 = l1->functor();
unsigned f2 = l2->functor();
if (f1 < f2) {
return -1;
}
if (f1 > f2) {
return 1;
}
TermList* ts1 = l1->args();
TermList* ts2 = l2->args();
while (! ts1->isEmpty()) {
unsigned varOrId1;
unsigned varOrId2;
if (ts1->isVar()) {
if (ts2->isVar()) { // both variables, let's compare them
varOrId1 = ts1->var();
varOrId2 = ts2->var();
} else {
return -1;
}
} else {
if (ts2->isVar()) {
return 1;
} else { // neither is var, let's compare the ids
varOrId1 = ts1->term()->getId();
varOrId2 = ts2->term()->getId();
}
}
if (varOrId1 < varOrId2) {
return -1;
}
if (varOrId1 > varOrId2) {
return 1;
}
ts1 = ts1->next();
ts2 = ts2->next();
}
return 0;
} // Tautology::compare
/**
* Randomised quicksort of an array @b lits of literals between
* indexes 0 and to-1 using compare()
* @pre the array has no duplicate literals, that is, compare()
* never returns 0
* @since 05/01/2008 Torrevieja
*/
void TautologyDeletionISE::sort(Literal** lits,int to)
{
ASS(to > 1);
// array behaves as a stack of calls to quicksort
static DArray<int> ft(32);
to--;
ft.ensure(to);
int from = 0;
int p = 0; // pointer to the next element in ft
for (;;) {
// invariant: from < to
int m = from + Random::getInteger(to-from+1);
Literal* mid = lits[m];
int l = from;
int r = to;
while (l < m) {
switch (compare(lits[l],mid))
{
case -1:
l++;
break;
#if VDEBUG
case 0:
ASSERTION_VIOLATION;
#endif
case 1:
if (m == r) {
lits[m] = lits[l];
lits[l] = lits[m-1];
lits[m-1] = mid;
m--;
r--;
}
else {
ASS(m < r);
Literal* lit = lits[l];
lits[l] = lits[r];
lits[r] = lit;
r--;
}
break;
}
}
// l == m
// now literals in lits[from ... m-1] are smaller than lits[m]
// and literals in lits[r+1 ... to] are greater than lits[m]
while (m < r) {
switch (compare(mid,lits[m+1]))
{
case -1:
{
Literal* lit = lits[r];
lits[r] = lits[m+1];
lits[m+1] = lit;
r--;
}
break;
#if VDEBUG
case 0:
ASSERTION_VIOLATION;
#endif
case 1:
lits[m] = lits[m+1];
lits[m+1] = mid;
m++;
}
}
// now literals in lits[from ... m-1] are smaller than lits[m]
// and all literals in lits[m+1 ... to] are greater than lits[m]
if (m+1 < to) {
ft[p++] = m+1;
ft[p++] = to;
}
to = m-1;
if (from < to) {
continue;
}
if (p != 0) {
p -= 2;
ASS(p >= 0);
from = ft[p];
to = ft[p+1];
continue;
}
return;
}
} // Tautology::sort