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 CNF.cpp
* Implements class CNF implementing CNF transformation.
* @since 19/01/2004 Manchester
* @since 27/12/2007 Manchester, changed completely to a new implementation
*/
#include "Kernel/Clause.hpp"
#include "Kernel/Formula.hpp"
#include "Kernel/Inference.hpp"
#include "Kernel/FormulaUnit.hpp"
#include "CNF.hpp"
using namespace Kernel;
using namespace Shell;
/**
* Initialise the CNF object.
* @since 27/12/2007 Manchester
*/
CNF::CNF()
: _literals(16),
_formulas(16)
{
} // CNF::CNF
/**
* Convert @b unit to CNF and push the resulting clauses on @b stack
* @pre @b unit must be a formula unit
* @since 27/12/2007 Manchester
*/
void CNF::clausify (Unit* unit,Stack<Clause*>& stack)
{
ASS(! unit->isClause());
_unit = static_cast<FormulaUnit*>(unit);
_result = &stack;
_literals.reset();
_formulas.reset();
Formula* f = _unit->formula();
switch (f->connective()) {
case TRUE:
return;
case FALSE:
{
stack.push(Clause::empty(FormulaClauseTransformation(InferenceRule::CLAUSIFY,unit)));
}
return;
default:
clausify(f);
}
} // CNF::clausify()
/**
* Clausify the formula f \/ F1 \/ ... \/ Fn \/ L1 \/ ... \/ Lm,
* where [F1,...,Fn] is the content of _formulas and [L1,...,Lm]
* is the content of _literals. After the clausification restore
* the stacks _formulas and _literals to their state before the call.
*
* @since 27/12/2007 Manchester
*/
/*
void CNF::clausify_rec (Formula* f)
{
switch (f->connective()) {
case LITERAL:
_literals.push(f->literal());
if (_formulas.isEmpty()) {
// collect the clause
int length = _literals.length();
Clause* clause = new(length) Clause(length,
FormulaClauseTransformation(InferenceRule::CLAUSIFY,_unit));
for (int i = length-1;i >= 0;i--) {
(*clause)[i] = _literals[i];
}
_result->push(clause);
}
else {
f = _formulas.pop();
clausify_rec(f);
_formulas.push(f);
}
_literals.pop();
return;
case AND:
{
FormulaList::Iterator fs(f->args());
while (fs.hasNext()) {
clausify_rec(fs.next());
}
}
return;
case OR:
{
int ln = _formulas.length();
FormulaList::Iterator fs(f->args());
while (fs.hasNext()) {
_formulas.push(fs.next());
}
clausify_rec(_formulas.pop());
_formulas.truncate(ln);
}
return;
case FORALL:
clausify_rec(f->qarg());
return;
default:
ASSERTION_VIOLATION;
}
}*/ // CNF::clausify
/**
* A recursion-free implementation of the above.
*/
void CNF::clausify(Formula* f)
{
enum TodoTag {
MAIN, // needs a Formula*
LIT_REST, // needs a Formula*
AND_REST, // needs a FormulaList*
OR_REST, // needs an int
};
union TodoVal {
Formula* aFla;
FormulaList* aFlist;
int anInt;
};
Stack<std::pair<TodoTag,TodoVal>> todo;
todo.push(std::make_pair<TodoTag,TodoVal>(MAIN,{.aFla = f}));
do {
ASS(todo.isNonEmpty());
auto task = todo.pop();
switch(task.first) {
case MAIN: {
Formula* f = task.second.aFla;
formula_reset:
switch (f->connective()) {
case LITERAL:
_literals.push(f->literal());
if (_formulas.isEmpty()) {
// collect the clause
_result->push(Clause::fromStack(_literals,
FormulaClauseTransformation(InferenceRule::CLAUSIFY,_unit)));
_literals.pop();
}
else {
f = _formulas.pop();
todo.push(std::make_pair<TodoTag,TodoVal>(LIT_REST,
{.aFla = f}));
todo.push(std::make_pair<TodoTag,TodoVal>(MAIN,
{.aFla = f}));
}
break;
case AND:
{
FormulaList* fl = f->args();
if (FormulaList::isNonEmpty(fl)) {
todo.push(std::make_pair<TodoTag,TodoVal>(AND_REST,
{.aFlist = fl->tail()}));
todo.push(std::make_pair<TodoTag,TodoVal>(MAIN,
{.aFla = fl->head()}));
}
}
break;
case OR:
{
int ln = _formulas.length();
FormulaList::Iterator fs(f->args());
while (fs.hasNext()) {
_formulas.push(fs.next());
}
todo.push(std::make_pair<TodoTag,TodoVal>(OR_REST,
{.anInt = ln}));
todo.push(std::make_pair<TodoTag,TodoVal>(MAIN,
{.aFla = _formulas.pop()}));
}
break;
case FORALL:
f = f->qarg();
goto formula_reset;
default:
ASSERTION_VIOLATION;
}
break;
}
case LIT_REST: {
Formula* f = task.second.aFla;
_formulas.push(f);
_literals.pop();
break;
}
case AND_REST: {
FormulaList* fl = task.second.aFlist;
if (FormulaList::isNonEmpty(fl)) {
todo.push(std::make_pair<TodoTag,TodoVal>(AND_REST,
{.aFlist = fl->tail()}));
todo.push(std::make_pair<TodoTag,TodoVal>(MAIN,
{.aFla = fl->head()}));
}
break;
}
case OR_REST: {
int ln = task.second.anInt;
_formulas.truncate(ln);
break;
}
}
} while(todo.isNonEmpty());
}