-
Notifications
You must be signed in to change notification settings - Fork 48
/
FormulaTransformer.hpp
216 lines (158 loc) · 5.5 KB
/
FormulaTransformer.hpp
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
/**
* @file FormulaTransformer.hpp
* Defines class FormulaTransformer.
*/
#ifndef __FormulaTransformer__
#define __FormulaTransformer__
#include "Forwards.hpp"
#include "Inference.hpp"
#include "Sorts.hpp"
namespace Kernel {
/**
* A convenient base class for formula transforming classes.
*
* The default implementations of transformers for particular
* connectives calls the "apply" function recursively and then
* build a resulting formula, reusing old formula objects if
* the recursive calls did not change their arguments.
*
* It also does flattening of AND and OR formulas, as well as of negations.
*/
class FormulaTransformer {
public:
/**
* This function is to be called from outside of the class to
* transform formulas.
*/
virtual Formula* transform(Formula* f);
protected:
FormulaTransformer() {}
virtual ~FormulaTransformer() {}
Formula* apply(Formula* f);
TermList apply(TermList ts);
/** Return true if f should be traversed */
virtual bool preApply(Formula*& f) { return true; }
virtual void postApply(Formula* orig, Formula*& res) {}
virtual Formula* applyLiteral(Formula* f) { return f; }
virtual Formula* applyAnd(Formula* f) { return applyJunction(f); }
virtual Formula* applyOr(Formula* f) { return applyJunction(f); }
/** This function is called by applyAnd() and applyOr()
* in their default implementation. */
virtual Formula* applyJunction(Formula* f);
virtual Formula* applyNot(Formula* f);
virtual Formula* applyImp(Formula* f) { return applyBinary(f); }
virtual Formula* applyIff(Formula* f) { return applyBinary(f); }
virtual Formula* applyXor(Formula* f) { return applyBinary(f); }
/** This function is called by applyImp(), applyIff()
* and applyXor() in their default implementation. */
virtual Formula* applyBinary(Formula* f);
virtual Formula* applyForAll(Formula* f) { return applyQuantified(f); }
virtual Formula* applyExists(Formula* f) { return applyQuantified(f); }
/** This function is called by applyForAll() and applyExists()
* in their default implementation. */
virtual Formula* applyQuantified(Formula* f);
virtual Formula* applyTrueFalse(Formula* f) { return f; }
};
class TermTransformingFormulaTransformer : public FormulaTransformer
{
public:
TermTransformingFormulaTransformer(TermTransformer& termTransformer) : _termTransformer(termTransformer) {}
protected:
virtual Formula* applyLiteral(Formula* f);
TermTransformer& _termTransformer;
};
class TermTransformerTransformTransformedFormulaTransformer : public FormulaTransformer
{
public:
TermTransformerTransformTransformedFormulaTransformer(TermTransformerTransformTransformed& termTransformer)
: _termTransformer(termTransformer) {}
protected:
virtual Formula* applyLiteral(Formula* f);
TermTransformerTransformTransformed& _termTransformer;
};
class PolarityAwareFormulaTransformer : protected FormulaTransformer {
public:
~PolarityAwareFormulaTransformer();
virtual Formula* transformWithPolarity(Formula* f, int polarity=1);
protected:
PolarityAwareFormulaTransformer();
virtual Formula* applyNot(Formula* f);
virtual Formula* applyImp(Formula* f);
virtual Formula* applyBinary(Formula* f);
int polarity() const { return _polarity; }
unsigned getVarSort(unsigned var) const;
private:
DHMap<unsigned,unsigned>* _varSorts;
int _polarity;
};
class FormulaUnitTransformer
{
public:
virtual ~FormulaUnitTransformer() {}
virtual FormulaUnit* transform(FormulaUnit* unit) = 0;
void transform(UnitList*& units);
};
class LocalFormulaUnitTransformer : public FormulaUnitTransformer
{
public:
LocalFormulaUnitTransformer(Inference::Rule rule)
: _rule(rule) {}
using FormulaUnitTransformer::transform;
virtual Formula* transform(Formula* f) = 0;
virtual FormulaUnit* transform(FormulaUnit* unit);
private:
Inference::Rule _rule;
};
template<class FT>
class FTFormulaUnitTransformer : public LocalFormulaUnitTransformer
{
public:
FTFormulaUnitTransformer(Inference::Rule rule, FT& formulaTransformer)
: LocalFormulaUnitTransformer(rule), _formulaTransformer(formulaTransformer) {}
using LocalFormulaUnitTransformer::transform;
virtual Formula* transform(Formula* f)
{
CALL("FTFormulaUnitTransformer::transform(Formula*)");
return _formulaTransformer.transform(f);
}
private:
FT& _formulaTransformer;
};
class ScanAndApplyFormulaUnitTransformer {
public:
virtual ~ScanAndApplyFormulaUnitTransformer() {}
void apply(Problem& prb);
bool apply(UnitList*& units);
virtual void scan(UnitList* units) {}
bool apply(Unit* u, Unit*& res);
virtual UnitList* getIntroducedFormulas() { return 0; }
protected:
virtual bool apply(FormulaUnit* unit, Unit*& res) {
return false;
}
virtual bool apply(Clause* cl, Unit*& res) {
return false;
}
virtual void updateModifiedProblem(Problem& prb);
};
class ScanAndApplyLiteralTransformer : public ScanAndApplyFormulaUnitTransformer {
public:
using ScanAndApplyFormulaUnitTransformer::apply;
protected:
/**
* @param infRule the rule that will be used to derive modified units
*/
ScanAndApplyLiteralTransformer(Inference::Rule infRule) : _infRule(infRule) {}
/**
* @param l the literal
* @param premAcc premises of the transformation should be added on this stack
*/
virtual Literal* apply(Literal* l, UnitStack& premAcc) = 0;
virtual bool apply(FormulaUnit* unit, Unit*& res);
virtual bool apply(Clause* cl, Unit*& res);
private:
struct LitFormulaTransformer;
Inference::Rule _infRule;
};
}
#endif // __FormulaTransformer__