-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProof.C
240 lines (193 loc) · 6.27 KB
/
Proof.C
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
/*****************************************************************************************[Proof.C]
MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************/
#include "Proof.h"
#include "Sort.h"
//=================================================================================================
// Temporary files handling:
class TempFiles {
vec<cchar*> files; // For clean-up purposed on abnormal exit.
public:
~TempFiles()
{
for (int i = 0; i < files.size(); i++)
remove(files[i]);
//printf("Didn't delete:\n %s\n", files[i]);
}
// Returns a read-only string with the filename of the temporary file. The pointer can be used to
// remove the file (which is otherwise done automatically upon program exit).
//
char* open(File& fp)
{
char* name;
for(;;){
name = tempnam(NULL, NULL); // (gcc complains about this... stupid gcc...)
assert(name != NULL);
fp.open(name, "wx+");
if (fp.null())
xfree(name);
else{
files.push(name);
return name;
}
}
}
};
static TempFiles temp_files; // (should be singleton)
//=================================================================================================
// Proof logging:
Proof::Proof()
{
fp_name = temp_files.open(fp);
id_counter = 0;
trav = NULL;
}
Proof::Proof(ProofTraverser& t)
{
id_counter = 0;
trav = &t;
}
ClauseId Proof::addRoot(vec<Lit>& cl)
{
cl.copyTo(clause);
sortUnique(clause);
if (trav != NULL)
trav->root(clause);
if (!fp.null()){
if (clause.size() > 0) { // PATCH for the case of an empty root clause
putUInt(fp, index(clause[0]) << 1);
for (int i = 1; i < clause.size(); i++)
putUInt(fp, index(clause[i]) - index(clause[i-1]));
}
putUInt(fp, 0); // (0 is safe terminator since we removed duplicates)
}
return id_counter++;
}
void Proof::beginChain(ClauseId start)
{
assert(start != ClauseId_NULL);
chain_id .clear();
chain_var.clear();
chain_id.push(start);
}
void Proof::resolve(ClauseId next, Var x)
{
assert(next != ClauseId_NULL);
chain_id .push(next);
chain_var.push(x);
}
ClauseId Proof::endChain()
{
assert(chain_id.size() == chain_var.size() + 1);
if (chain_id.size() == 1)
return chain_id[0];
else{
if (trav != NULL)
trav->chain(chain_id, chain_var);
if (!fp.null()){
putUInt(fp, ((id_counter - chain_id[0]) << 1) | 1);
for (int i = 0; i < chain_var.size(); i++)
putUInt(fp, chain_var[i] + 1),
putUInt(fp, id_counter - chain_id[i+1]);
putUInt(fp, 0);
}
return id_counter++;
}
}
void Proof::deleted(ClauseId gone)
{
if (trav != NULL)
trav->deleted(gone);
if (!fp.null()){
putUInt(fp, ((id_counter - gone) << 1) | 1);
putUInt(fp, 0);
}
}
//=================================================================================================
// Read-back methods:
void Proof::compress(Proof& dst, ClauseId goal)
{
assert(!fp.null());
assert(false); // Not yet!
}
bool Proof::save(cchar* filename)
{
assert(!fp.null());
// Switch to read mode:
fp.setMode(READ);
fp.seek(0);
// Copy file:
File out(filename, "wo"); // ORIGINAL flags "wox"; "wo" allows writing to existing files
if (out.null())
return false;
while (!fp.eof())
out.putChar(fp.getChar());
// Restore write (proof-logging) mode:
fp.seek(0, SEEK_END);
fp.setMode(WRITE);
return true;
}
void Proof::traverse(ProofTraverser& trav, ClauseId goal)
{
assert(!fp.null());
// Switch to read mode:
fp.setMode(READ);
fp.seek(0);
// Traverse proof:
if (goal == ClauseId_NULL)
goal = last();
//std::cout << "about to start traversal with goal " << goal << std::endl;
uint64 tmp;
int idx;
for(ClauseId id = 0; id <= goal; id++){
tmp = getUInt(fp);
if ((tmp & 1) == 0){
// Root clause:
clause.clear();
if (id < goal) { // PATCH for the case of an empty root clause being the goal clause
idx = tmp >> 1;
clause.push(toLit(idx));
for(;;){
tmp = getUInt(fp);
if (tmp == 0) break;
idx += tmp;
clause.push(toLit(idx));
}
}
trav.root(clause);
}else{
// Derivation or Deletion:
chain_id .clear();
chain_var.clear();
chain_id.push(id - (tmp >> 1));
for(;;){
tmp = getUInt(fp);
if (tmp == 0) break;
chain_var.push(tmp - 1);
tmp = getUInt(fp);
chain_id.push(id - tmp);
}
if (chain_var.size() == 0)
id--, // (no new clause introduced)
trav.deleted(chain_id[0]);
else
trav.chain(chain_id, chain_var);
}
}
trav.done();
// Restore write (proof-logging) mode:
fp.seek(0, SEEK_END);
fp.setMode(WRITE);
}