-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequent.hh
412 lines (334 loc) · 14 KB
/
sequent.hh
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#ifndef LOGICAL_SEQUENT_HH
#define LOGICAL_SEQUENT_HH
#include "collections.hh"
#include "errors.hh"
#include "formula.hh"
#include "logical.hh"
#include "unionfind.hh"
namespace Logical
{
using std::pair;
static inline float fabs(float x)
{
if(x >= 0)
return x;
else
return -x;
}
static inline Shadow<CompoundFormula> ShadowOfCompoundFormula(const Formula& formula)
{
return Shadow<CompoundFormula>(static_cast<const CompoundFormula&>(formula));
}
static inline Zip<CompoundFormula, CompoundFormula> ZipOfCompoundFormula(const Formula& first, const Formula& second)
{
return Zip<CompoundFormula, CompoundFormula>(static_cast<const CompoundFormula&>(first), static_cast<const CompoundFormula&>(second));
}
class Sequent
{
private:
class UnionFind;
UnionFind* unionfind;
bool toplevel;
Unfold<Formula> left;
Unfold<Formula> right;
template<typename LeftInitializer, typename RightInitializer>
Sequent(LeftInitializer&& l, RightInitializer&& r, UnionFind* uf)
: left(forward<LeftInitializer>(l))
, right(forward<RightInitializer>(r))
, unionfind(uf)
, toplevel(false)
{
}
protected:
float guide_positive(const Formula& formula)
{
return formula.total_size();
}
float guide_negative(const Formula& formula)
{
return formula.total_size();
}
float guide_equal(const Formula& first, const Formula& second)
{
return (first.total_size() + second.total_size()) * (1.0f + fabs(float(first.total_size()) - float(second.total_size())));
}
private:
template <typename LeftInitializer, typename RightInitializer>
static bool sub_prove(LeftInitializer&& l, RightInitializer&& r, UnionFind* uf)
{
return Sequent(forward<LeftInitializer>(l), forward<RightInitializer>(r), uf).prove();
}
bool breakdown(const Formula& formula)
{
//cerr << "breakdown: " << formula << endl;
if(left.count(formula))
{
const auto singleton_formula = Singleton<Formula>(formula);
const auto left_sans_formula = left - singleton_formula;
logical_assert(left.count(formula));
logical_assert(!left_sans_formula.count(formula));
logical_assert(left_sans_formula.size() == left.size() - 1);
logical_assert(Unfold<Formula>(left_sans_formula).size() == left_sans_formula.size());
switch(formula.get_symbol())
{
case True:
return sub_prove(left_sans_formula, right, unionfind);
case False:
return true;
case Not:
return sub_prove(left_sans_formula, right + Singleton<Formula>(formula[0]), unionfind);
case RImpl:
return ShadowOfCompoundFormula(formula).for_any([this, &left_sans_formula, &formula](auto& subformula) {
if(&subformula == &formula[0])
return sub_prove(left_sans_formula + Singleton<Formula>(formula[0]), right, unionfind);
else if(&subformula == &formula[1])
return sub_prove(left_sans_formula, right + Singleton<Formula>(formula[1]), unionfind);
else
throw RuntimeError("None of the implication subformulas identical to the formula provided.");
});
case Impl:
return ShadowOfCompoundFormula(formula).for_any([this, &left_sans_formula, &formula](auto& subformula) {
if(&subformula == &formula[1])
return sub_prove(left_sans_formula + Singleton<Formula>(formula[1]), right, unionfind);
else if(&subformula == &formula[0])
return sub_prove(left_sans_formula, right + Singleton<Formula>(formula[0]), unionfind);
else
throw RuntimeError("None of the implication subformulas identical to the formula provided.");
});
case NRImpl:
return sub_prove(left_sans_formula + Singleton<Formula>(formula[0]), right + Singleton<Formula>(formula[1]), unionfind);
case NImpl:
return sub_prove(left_sans_formula + Singleton<Formula>(formula[1]), right + Singleton<Formula>(formula[0]), unionfind);
case And:
return sub_prove(left_sans_formula + ShadowOfCompoundFormula(formula), right, unionfind);
case Or:
return ShadowOfCompoundFormula(formula)
.sort([this](const Formula& f) { return guide_negative(f); })
.for_all([this, &left_sans_formula, &formula](
auto& subformula) { return sub_prove(left_sans_formula + Singleton<Formula>(subformula), right, unionfind); });
case NOr:
return sub_prove(left_sans_formula, right + ShadowOfCompoundFormula(formula), unionfind);
case NAnd:
return ShadowOfCompoundFormula(formula)
.sort([this](const Formula& f) { return guide_positive(f); })
.for_all([this, &left_sans_formula, &formula](
auto& subformula) { return sub_prove(left_sans_formula, right + Singleton<Formula>(subformula), unionfind); });
default:
return false;
// throw UnsupportedConnectiveError("Unsupported connective.", formula.get_symbol());
}
throw RuntimeError("Should not be here.");
}
if(right.count(formula))
{
const auto singleton_formula = Singleton<Formula>(formula);
const auto right_sans_formula = right - singleton_formula;
switch(formula.get_symbol())
{
case False:
return sub_prove(left, right_sans_formula, unionfind);
case True:
return true;
case Not:
return sub_prove(left + Singleton<Formula>(formula[0]), right_sans_formula, unionfind);
case NRImpl:
return ShadowOfCompoundFormula(formula).for_any([this, &right_sans_formula, &formula](auto& subformula) {
if(&subformula == &formula[0])
return sub_prove(right_sans_formula + Singleton<Formula>(formula[0]), right, unionfind);
else if(&subformula == &formula[1])
return sub_prove(right_sans_formula, right + Singleton<Formula>(formula[1]), unionfind);
else
throw RuntimeError("None of the implication subformulas identical to the formula provided.");
});
case NImpl:
return ShadowOfCompoundFormula(formula).for_any([this, &right_sans_formula, &formula](auto& subformula) {
if(&subformula == &formula[1])
return sub_prove(right_sans_formula + Singleton<Formula>(formula[1]), right, unionfind);
else if(&subformula == &formula[0])
return sub_prove(right_sans_formula, right + Singleton<Formula>(formula[0]), unionfind);
else
throw RuntimeError("None of the implication subformulas identical to the formula provided.");
});
case Impl:
return sub_prove(left + Singleton<Formula>(formula[0]), right_sans_formula + Singleton<Formula>(formula[1]), unionfind);
case RImpl:
return sub_prove(left + Singleton<Formula>(formula[1]), right_sans_formula + Singleton<Formula>(formula[0]), unionfind);
case Or:
return sub_prove(left, right_sans_formula + ShadowOfCompoundFormula(formula), unionfind);
case And:
return ShadowOfCompoundFormula(formula)
.sort([this](const Formula& f) { return guide_positive(f); })
.for_all([this, &right_sans_formula, &formula](
auto& subformula) { return sub_prove(left, right_sans_formula + Singleton<Formula>(subformula), unionfind); });
case NAnd:
return sub_prove(left + ShadowOfCompoundFormula(formula), right_sans_formula, unionfind);
case NOr:
return ShadowOfCompoundFormula(formula)
.sort([this](const Formula& f) { return guide_negative(f); })
.for_all([this, &right_sans_formula, &formula](
auto& subformula) { return sub_prove(left + Singleton<Formula>(subformula), right_sans_formula, unionfind); });
default:
return false;
// throw UnsupportedConnectiveError("Unsupported connective.", formula.get_symbol());
}
throw RuntimeError("Should not be here.");
}
throw RuntimeError("Formula not found on left nor right side of the sequent.");
}
bool equal(const Formula& first, const Formula& second)
{
//cerr << "equal: " << first << " == " << second << endl;
if(unionfind)
return unionfind->equal(first, second);
else
return formulas_equal(first, second);
}
bool formulas_equal(const Formula& first, const Formula& second)
{
static const auto commutative_symbols = unordered_set<Symbol, SymbolHash>({And, Or, NAnd, NOr, Xor, NXor, Equiv, NEquiv});
static const auto idempotent_symbols = unordered_set<Symbol, SymbolHash>({And, Or, NAnd, NOr});
const auto& first_symbol = first.get_symbol();
const auto& second_symbol = second.get_symbol();
if(first_symbol != second_symbol)
return false;
else if(first == second)
return true;
else if(commutative_symbols.count(first_symbol))
{
if(!idempotent_symbols.count(first_symbol) && first.size() != second.size())
return false;
const bool first_in_second = ShadowOfCompoundFormula(first).for_all([this, &second](const auto& sub1)
{
auto& parent = *this;
return ShadowOfCompoundFormula(second)
.sort([&parent, &sub1](const auto& sub2) { return parent.guide_equal(sub1, sub2); })
.for_any([&parent, &sub1](const auto& sub2) { return parent.equal(sub1, sub2); });
});
const bool second_in_first = ShadowOfCompoundFormula(second).for_all([this, &first](const auto& sub2)
{
auto& parent = *this;
return ShadowOfCompoundFormula(first)
.sort([&parent, &sub2](const auto& sub1) { return parent.guide_equal(sub2, sub1); })
.for_any([&parent, &sub2](const auto& sub1) { return parent.equal(sub2, sub1); });
});
return first_in_second && second_in_first;
}
else if(!first_symbol.is_relation() && !first_symbol.is_quantifier())
{
if(first.size() != second.size())
return false;
return ZipOfCompoundFormula(first, second)
.sort([this](const auto& p) { return -guide_equal(p.first, p.second); })
.for_all([this](const auto& p) { return equal(p.first, p.second); });
}
else if(!first_symbol.is_relation() && first_symbol.is_quantifier())
{
throw RuntimeError("Not implemented."); // TODO
}
else if(first_symbol.is_relation())
{
throw RuntimeError("Not implemented."); // TODO
}
else
{
throw RuntimeError("Unsupported case.");
}
}
class UnionFind : public CompareCache<Formula>
{
private:
Sequent& sequent;
protected:
bool value_compare(const Formula& one, const Formula& two)
{
return sequent.formulas_equal(one, two);
}
public:
UnionFind(Sequent& s)
: sequent(s)
{
}
};
public:
template<typename LeftInitializer, typename RightInitializer>
Sequent(LeftInitializer&& l, RightInitializer&& r, bool usecache=true)
: left(forward<LeftInitializer>(l))
, right(forward<RightInitializer>(r))
, unionfind(usecache ? new UnionFind(*this) : nullptr)
, toplevel(true)
{
}
~Sequent(void)
{
if(unionfind && toplevel)
delete unionfind;
}
bool prove(void)
{
//cerr << "prove " << (&left) << ", " << (&right) << endl;
//cerr << left << " |- " << right << endl;
return (left.size() == 0 && right.size() == 0)
|| (left * right)
.sort([this](const pair<const Formula&, const Formula&>& p) { return guide_equal(p.first, p.second); })
.for_any([this](const pair<const Formula&, const Formula&>& p) { return equal(p.first, p.second); })
|| (left + right)
.sort([this](const Formula& f) { return (left.count(f) ? guide_negative(f) : 0) + (right.count(f) ? guide_positive(f) : 0); })
.for_any([this](const Formula& f) { return breakdown(f); });
}
};
inline bool prove(const initializer_list<Formula>& l, const initializer_list<Formula>& r)
{
return Sequent(l, r).prove();
}
} // namespace Logical
#ifdef DEBUG
namespace Logical
{
void sequent_test(void)
{
try
{
const auto a = ConnectiveSymbol("a");
const auto b = ConnectiveSymbol("b");
const auto c = ConnectiveSymbol("c");
const auto ab = vector<Formula>({a(), b()});
logical_assert(Unfold<Formula>(ab).sort([](const Formula& f) -> float { return f.total_size(); }).for_any([&a, &b](const Formula& f) -> bool { return f == b(); }));
logical_assert(prove({}, {}), "Empty sequent should succeed.");
logical_assert(prove({a()}, {a()}), "Sequent with the same symbol on both sides must succeed.");
logical_assert(!prove({a()}, {b()}), "Sequent should fail.");
logical_assert(prove({a()}, {b(), a()}), "Sequent should succeed.");
logical_assert(prove({a(), b()}, {a()}), "Sequent should succeed.");
logical_assert(!prove({}, {b()}), "Sequent should fail.");
logical_assert(!prove({}, {a()}), "Sequent should fail.");
logical_assert(!prove({Or(a(), b())}, {b()}), "Sequent should fail.");
logical_assert(prove({And(a(), b())}, {a()}), "Sequent should succeed.");
logical_assert(prove({}, {Or(a(), Not(a()))}), "Sequent should succeed.");
logical_assert(prove({False()}, {False()}), "Sequent should succeed.");
logical_assert(prove({}, {True()}), "Sequent should succeed.");
logical_assert(prove({a(), Impl(a(), b())}, {b()}), "Sequent should succeed.");
logical_assert(prove({Impl(a(), b())}, {Or(Not(a()), b())}), "Sequent should succeed.");
logical_assert(prove({a()}, {True()}), "Sequent should succeed.");
logical_assert(prove({a(), b()}, {a(), b()}), "Sequent should succeed.");
logical_assert(prove({a(), b()}, {b(), a()}), "Sequent should succeed.");
logical_assert(prove({a(), b()}, {And(a(), b())}), "Sequent should succeed.");
logical_assert(prove({Impl(a(), b()), Impl(Not(a()), b())}, {b()}), "Sequent should succeed.");
logical_assert(prove({Not(a()), a()}, {}), "Sequent should succeed.");
logical_assert(prove({a()}, {a(), b()}), "Sequent should succeed.");
logical_assert(prove({Impl(a(), b()), Impl(b(), c())}, {Impl(a(), c())}), "Sequent should succeed.");
logical_assert(prove({Impl(a(), b()), Impl(a(), c())}, {Impl(a(), And(b(), c()))}), "Sequent should succeed.");
logical_assert(!prove({Impl(a(), b())}, {Impl(b(), a())}), "Sequent of the form `a->b |- b->a` should fail.");
logical_assert(prove({a() | b(), ~a()}, {b()}), "Sequent should succeed.");
const auto x = Variable("x");
const auto y = Variable("y");
logical_assert(prove({Equal(x, x)}, {Equal(x, x)}));
//logical_assert(!prove({Equal(x, x)}, {Equal(y, y)}));
}
catch(const UnsupportedConnectiveError& error)
{
cout << "UnsupportedConnectiveError.symbol = " << error.symbol << endl;
throw error;
}
}
} // namespace Logical
#endif // DEBUG
#endif // LOGICAL_SEQUENT_HH