-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfirst_order_logic.h
More file actions
4245 lines (3828 loc) · 143 KB
/
Copy pathfirst_order_logic.h
File metadata and controls
4245 lines (3828 loc) · 143 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* first_order_logic.h
*
* Created on: Jun 26, 2018
* Author: asaparov
*/
#ifndef FIRST_ORDER_LOGIC_H_
#define FIRST_ORDER_LOGIC_H_
#include <core/lex.h>
#include <cstdint>
using namespace core;
/* forward declarations */
struct fol_formula;
bool operator == (const fol_formula&, const fol_formula&);
bool operator != (const fol_formula&, const fol_formula&);
enum class fol_term_type {
NONE = 0,
VARIABLE,
CONSTANT,
PARAMETER
};
struct fol_term {
fol_term_type type;
unsigned int reference_count;
union {
unsigned int variable;
unsigned int constant;
unsigned int parameter;
};
static inline bool is_empty(const fol_term& key) {
return key.type == fol_term_type::NONE;
}
static inline void set_empty(fol_term& key) {
key.type = fol_term_type::NONE;
}
static inline bool copy(const fol_term& src, fol_term& dst) {
dst = src;
return true;
}
static inline void move(const fol_term& src, fol_term& dst) {
dst = src;
}
static inline void swap(fol_term& first, fol_term& second) {
fol_term temp = first;
first = second;
second = temp;
}
static inline unsigned int hash(const fol_term& key) {
switch (key.type) {
case fol_term_type::VARIABLE:
return 0 + 3 * default_hash(key.variable);
case fol_term_type::CONSTANT:
return 1 + 3 * default_hash(key.constant);
case fol_term_type::PARAMETER:
return 2 + 3 * default_hash(key.parameter);
case fol_term_type::NONE:
break;
}
fprintf(stderr, "fol_term.hash ERROR: Unrecognized fol_term_type.\n");
exit(EXIT_FAILURE);
}
static inline void free(fol_term& term) { }
};
inline bool operator == (const fol_term& first, const fol_term& second) {
if (first.type != second.type) return false;
switch (first.type) {
case fol_term_type::VARIABLE:
return first.variable == second.variable;
case fol_term_type::CONSTANT:
return first.constant == second.constant;
case fol_term_type::PARAMETER:
return first.parameter == second.parameter;
case fol_term_type::NONE:
return true;
}
fprintf(stderr, "operator == ERROR: Unrecognized fol_term_type.\n");
exit(EXIT_FAILURE);
}
inline bool operator != (const fol_term& first, const fol_term& second) {
return !(first == second);
}
inline bool operator < (const fol_term& first, const fol_term& second) {
if (first.type < second.type) return true;
else if (first.type > second.type) return false;
switch (first.type) {
case fol_term_type::VARIABLE:
return first.variable < second.variable;
case fol_term_type::CONSTANT:
return first.constant < second.constant;
case fol_term_type::PARAMETER:
return first.parameter < second.parameter;
case fol_term_type::NONE:
return false;
}
fprintf(stderr, "operator < ERROR: Unrecognized fol_term_type.\n");
exit(EXIT_FAILURE);
}
template<typename Stream>
inline bool print_subscript(unsigned int number, Stream& out) {
static const char* subscripts[] = { "₀", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉" };
while (number > 0) {
if (!print(subscripts[number % 10], out)) return false;
number /= 10;
}
return true;
}
enum class fol_formula_syntax {
TPTP,
CLASSIC
};
template<fol_formula_syntax Syntax, typename Stream>
inline bool print_variable(unsigned int variable, Stream& out) {
switch (Syntax) {
case fol_formula_syntax::TPTP:
return print('$', out) + print(variable, out);
case fol_formula_syntax::CLASSIC:
return print('x', out) && print_subscript(variable, out);
}
fprintf(stderr, "print_variable ERROR: Unrecognized fol_formula_syntax.\n");
return false;
}
template<fol_formula_syntax Syntax, typename Stream>
inline bool print_parameter(unsigned int parameter, Stream& out) {
switch (Syntax) {
case fol_formula_syntax::TPTP:
return print('#', out) + print(parameter, out);
case fol_formula_syntax::CLASSIC:
return print('a', out) && print_subscript(parameter, out);
}
fprintf(stderr, "print_parameter ERROR: Unrecognized fol_formula_syntax.\n");
return false;
}
template<fol_formula_syntax Syntax = fol_formula_syntax::CLASSIC, typename Stream, typename... Printer>
bool print(const fol_term& term, Stream& out, Printer&&... printer) {
switch (term.type) {
case fol_term_type::VARIABLE:
return print_variable<Syntax>(term.variable, out);
case fol_term_type::CONSTANT:
return print(term.constant, out, std::forward<Printer>(printer)...);
case fol_term_type::PARAMETER:
return print_parameter<Syntax>(term.parameter, out);
case fol_term_type::NONE:
break;
}
fprintf(stderr, "print ERROR: Unexpected fol_term_type.\n");
return false;
}
enum class fol_formula_type {
ATOM,
EQUALS,
AND,
OR,
IF_THEN,
IFF,
NOT,
FOR_ALL,
EXISTS,
TRUE,
FALSE /* our canonicalization code assumes that FALSE is the last element of this enum */
};
struct fol_atom {
unsigned int predicate;
fol_term arg1;
fol_term arg2;
static inline void move(const fol_atom& src, fol_atom& dst) {
dst.predicate = src.predicate;
dst.arg1 = src.arg1;
dst.arg2 = src.arg2;
}
static inline void free(fol_atom& formula) { }
};
inline bool operator == (const fol_atom& first, const fol_atom& second) {
return first.predicate == second.predicate
&& first.arg1 == second.arg1
&& first.arg2 == second.arg2;
}
struct fol_equals {
fol_term arg1;
fol_term arg2;
static inline void move(const fol_equals& src, fol_equals& dst) {
dst.arg1 = src.arg1;
dst.arg2 = src.arg2;
}
static inline void free(fol_equals& equals) { }
};
inline bool operator == (const fol_equals& first, const fol_equals& second) {
return first.arg1 == second.arg1
&& first.arg2 == second.arg2;
}
struct fol_unary_formula {
fol_formula* operand;
static inline void move(const fol_unary_formula& src, fol_unary_formula& dst);
static inline void free(fol_unary_formula& formula);
};
struct fol_binary_formula {
fol_formula* left;
fol_formula* right;
static inline void move(const fol_binary_formula& src, fol_binary_formula& dst);
static inline void free(fol_binary_formula& formula);
};
struct fol_array_formula {
fol_formula** operands;
unsigned int length;
static inline void move(const fol_array_formula& src, fol_array_formula& dst);
static inline void free(fol_array_formula& formula);
};
struct fol_quantifier {
unsigned int variable;
fol_formula* operand;
static inline void move(const fol_quantifier& src, fol_quantifier& dst);
static inline void free(fol_quantifier& formula);
};
struct fol_formula
{
typedef fol_formula_type Type;
typedef fol_term Term;
typedef fol_term_type TermType;
static constexpr fol_term EMPTY_TERM = {fol_term_type::NONE, 1, {0}};
fol_formula_type type;
unsigned int reference_count;
union {
fol_atom atom;
fol_equals equals;
fol_unary_formula unary;
fol_binary_formula binary;
fol_array_formula array;
fol_quantifier quantifier;
};
fol_formula(fol_formula_type type) : type(type), reference_count(1) { }
~fol_formula() { free_helper(); }
static fol_term* new_variable(unsigned int variable);
static fol_term* new_constant(unsigned int constant);
static fol_term* new_parameter(unsigned int parameter);
static fol_formula* new_atom(unsigned int predicate, fol_term* arg1, fol_term* arg2);
static inline fol_formula* new_atom(unsigned int predicate, fol_term* arg1);
static inline fol_formula* new_atom(unsigned int predicate);
static inline fol_formula* new_equals(fol_term arg1, fol_term arg2);
static fol_formula* new_true();
static fol_formula* new_false();
template<typename... Args> static inline fol_formula* new_and(Args&&... args);
template<typename... Args> static inline fol_formula* new_or(Args&&... args);
template<typename... Args> static inline fol_formula* new_iff(Args&&... args);
static fol_formula* new_if_then(fol_formula* first, fol_formula* second);
static fol_formula* new_not(fol_formula* operand);
static inline fol_formula* new_for_all(unsigned int variable, fol_formula* operand);
static inline fol_formula* new_exists(unsigned int variable, fol_formula* operand);
static inline void move(const fol_formula& src, fol_formula& dst);
static inline void free(fol_formula& formula) { formula.free_helper(); }
private:
void free_helper();
};
constexpr fol_term fol_formula::EMPTY_TERM;
thread_local fol_formula FOL_TRUE(fol_formula_type::TRUE);
thread_local fol_formula FOL_FALSE(fol_formula_type::FALSE);
inline bool operator == (const fol_unary_formula& first, const fol_unary_formula& second) {
return first.operand == second.operand
|| *first.operand == *second.operand;
}
inline bool operator == (const fol_binary_formula& first, const fol_binary_formula& second) {
return (first.left == second.left || *first.left == *second.left)
&& (first.right == second.right || *first.right == *second.right);
}
inline bool operator == (const fol_array_formula& first, const fol_array_formula& second) {
if (first.length != second.length) return false;
for (unsigned int i = 0; i < first.length; i++)
if (first.operands[i] != second.operands[i] && *first.operands[i] != *second.operands[i]) return false;
return true;
}
inline bool operator == (const fol_quantifier& first, const fol_quantifier& second) {
return first.variable == second.variable
&& (first.operand == second.operand || *first.operand == *second.operand);
}
bool operator == (const fol_formula& first, const fol_formula& second)
{
if (first.type != second.type) return false;
switch (first.type) {
case fol_formula_type::ATOM:
return first.atom == second.atom;
case fol_formula_type::EQUALS:
return first.equals == second.equals;
case fol_formula_type::NOT:
return first.unary == second.unary;
case fol_formula_type::IF_THEN:
return first.binary == second.binary;
case fol_formula_type::AND:
case fol_formula_type::OR:
case fol_formula_type::IFF:
return first.array == second.array;
case fol_formula_type::FOR_ALL:
case fol_formula_type::EXISTS:
return first.quantifier == second.quantifier;
case fol_formula_type::TRUE:
case fol_formula_type::FALSE:
return true;
}
fprintf(stderr, "operator == ERROR: Unrecognized fol_formula_type.\n");
exit(EXIT_FAILURE);
}
inline bool operator != (const fol_formula& first, const fol_formula& second) {
return !(first == second);
}
inline void fol_formula::move(const fol_formula& src, fol_formula& dst) {
dst.type = src.type;
dst.reference_count = src.reference_count;
switch (src.type) {
case fol_formula_type::ATOM:
core::move(src.atom, dst.atom); return;
case fol_formula_type::EQUALS:
core::move(src.equals, dst.equals); return;
case fol_formula_type::NOT:
core::move(src.unary, dst.unary); return;
case fol_formula_type::IF_THEN:
core::move(src.binary, dst.binary); return;
case fol_formula_type::AND:
case fol_formula_type::OR:
case fol_formula_type::IFF:
core::move(src.array, dst.array); return;
case fol_formula_type::FOR_ALL:
case fol_formula_type::EXISTS:
core::move(src.quantifier, dst.quantifier); return;
case fol_formula_type::TRUE:
case fol_formula_type::FALSE:
return;
}
fprintf(stderr, "fol_formula.move ERROR: Unrecognized fol_formula_type.\n");
}
inline void fol_unary_formula::move(const fol_unary_formula& src, fol_unary_formula& dst) {
dst.operand = src.operand;
}
inline void fol_binary_formula::move(const fol_binary_formula& src, fol_binary_formula& dst) {
dst.left = src.left;
dst.right = src.right;
}
inline void fol_array_formula::move(const fol_array_formula& src, fol_array_formula& dst) {
dst.length = src.length;
dst.operands = src.operands;
}
inline void fol_quantifier::move(const fol_quantifier& src, fol_quantifier& dst) {
dst.variable = src.variable;
dst.operand = src.operand;
}
inline void fol_formula::free_helper() {
reference_count--;
if (reference_count == 0) {
switch (type) {
case fol_formula_type::ATOM:
core::free(atom); return;
case fol_formula_type::EQUALS:
core::free(equals); return;
case fol_formula_type::NOT:
core::free(unary); return;
case fol_formula_type::IF_THEN:
core::free(binary); return;
case fol_formula_type::AND:
case fol_formula_type::OR:
case fol_formula_type::IFF:
core::free(array); return;
case fol_formula_type::FOR_ALL:
case fol_formula_type::EXISTS:
core::free(quantifier); return;
case fol_formula_type::TRUE:
case fol_formula_type::FALSE:
return;
}
fprintf(stderr, "fol_formula.free_helper ERROR: Unrecognized fol_formula_type.\n");
}
}
inline void fol_unary_formula::free(fol_unary_formula& formula) {
core::free(*formula.operand);
if (formula.operand->reference_count == 0)
core::free(formula.operand);
}
inline void fol_binary_formula::free(fol_binary_formula& formula) {
core::free(*formula.left);
if (formula.left->reference_count == 0)
core::free(formula.left);
core::free(*formula.right);
if (formula.right->reference_count == 0)
core::free(formula.right);
}
inline void fol_array_formula::free(fol_array_formula& formula) {
for (unsigned int i = 0; i < formula.length; i++) {
core::free(*formula.operands[i]);
if (formula.operands[i]->reference_count == 0)
core::free(formula.operands[i]);
}
core::free(formula.operands);
}
inline void fol_quantifier::free(fol_quantifier& formula) {
core::free(*formula.operand);
if (formula.operand->reference_count == 0)
core::free(formula.operand);
}
inline bool is_atomic(
const fol_formula& term, unsigned int& predicate,
fol_term const*& arg1, fol_term const*& arg2)
{
if (term.type != fol_formula_type::ATOM) return false;
predicate = term.atom.predicate;
arg1 = (term.atom.arg1.type == fol_term_type::NONE ? NULL : &term.atom.arg1);
arg2 = (term.atom.arg2.type == fol_term_type::NONE ? NULL : &term.atom.arg2);
return true;
}
inline bool is_atomic(const fol_formula& term,
fol_term const*& arg1, fol_term const*& arg2)
{
unsigned int predicate;
return is_atomic(term, predicate, arg1, arg2);
}
inline bool is_atomic(const fol_formula& term) {
unsigned int predicate;
fol_term const* arg1; fol_term const* arg2;
return is_atomic(term, predicate, arg1, arg2);
}
inline bool is_atomic(
fol_formula& term, unsigned int& predicate,
fol_term*& arg1, fol_term*& arg2)
{
if (term.type != fol_formula_type::ATOM) return false;
predicate = term.atom.predicate;
arg1 = (term.atom.arg1.type == fol_term_type::NONE ? NULL : &term.atom.arg1);
arg2 = (term.atom.arg2.type == fol_term_type::NONE ? NULL : &term.atom.arg2);
return true;
}
inline bool is_atomic(fol_formula& term,
fol_term*& arg1, fol_term*& arg2)
{
unsigned int predicate;
return is_atomic(term, predicate, arg1, arg2);
}
inline bool is_atomic(fol_formula& term) {
unsigned int predicate;
fol_term* arg1; fol_term* arg2;
return is_atomic(term, predicate, arg1, arg2);
}
template<fol_formula_syntax Syntax> struct and_symbol;
template<fol_formula_syntax Syntax> struct or_symbol;
template<fol_formula_syntax Syntax> struct if_then_symbol;
template<fol_formula_syntax Syntax> struct iff_symbol;
template<fol_formula_syntax Syntax> struct not_symbol;
template<fol_formula_syntax Syntax> struct equals_symbol;
template<fol_formula_syntax Syntax> struct true_symbol;
template<fol_formula_syntax Syntax> struct false_symbol;
template<> struct and_symbol<fol_formula_syntax::TPTP> { static const char symbol[]; };
template<> struct or_symbol<fol_formula_syntax::TPTP> { static const char symbol[]; };
template<> struct iff_symbol<fol_formula_syntax::TPTP> { static const char symbol[]; };
template<> struct if_then_symbol<fol_formula_syntax::TPTP> { static const char symbol[]; };
template<> struct not_symbol<fol_formula_syntax::TPTP> { static const char symbol; };
template<> struct equals_symbol<fol_formula_syntax::TPTP> { static const char symbol; };
template<> struct true_symbol<fol_formula_syntax::TPTP> { static const char symbol; };
template<> struct false_symbol<fol_formula_syntax::TPTP> { static const char symbol; };
template<> struct and_symbol<fol_formula_syntax::CLASSIC> { static const char symbol[]; };
template<> struct or_symbol<fol_formula_syntax::CLASSIC> { static const char symbol[]; };
template<> struct iff_symbol<fol_formula_syntax::CLASSIC> { static const char symbol[]; };
template<> struct if_then_symbol<fol_formula_syntax::CLASSIC> { static const char symbol[]; };
template<> struct not_symbol<fol_formula_syntax::CLASSIC> { static const char symbol[]; };
template<> struct equals_symbol<fol_formula_syntax::CLASSIC> { static const char symbol; };
template<> struct true_symbol<fol_formula_syntax::CLASSIC> { static const char symbol[]; };
template<> struct false_symbol<fol_formula_syntax::CLASSIC> { static const char symbol[]; };
const char and_symbol<fol_formula_syntax::TPTP>::symbol[] = " & ";
const char or_symbol<fol_formula_syntax::TPTP>::symbol[] = " | ";
const char iff_symbol<fol_formula_syntax::TPTP>::symbol[] = " <=> ";
const char if_then_symbol<fol_formula_syntax::TPTP>::symbol[] = " => ";
const char not_symbol<fol_formula_syntax::TPTP>::symbol = '~';
const char equals_symbol<fol_formula_syntax::TPTP>::symbol = '=';
const char true_symbol<fol_formula_syntax::TPTP>::symbol = 'T';
const char false_symbol<fol_formula_syntax::TPTP>::symbol = 'F';
const char and_symbol<fol_formula_syntax::CLASSIC>::symbol[] = " ∧ ";
const char or_symbol<fol_formula_syntax::CLASSIC>::symbol[] = " ∨ ";
const char iff_symbol<fol_formula_syntax::CLASSIC>::symbol[] = " ↔ ";
const char if_then_symbol<fol_formula_syntax::CLASSIC>::symbol[] = " → ";
const char not_symbol<fol_formula_syntax::CLASSIC>::symbol[] = "¬";
const char equals_symbol<fol_formula_syntax::CLASSIC>::symbol = '=';
const char true_symbol<fol_formula_syntax::CLASSIC>::symbol[] = "⊤";
const char false_symbol<fol_formula_syntax::CLASSIC>::symbol[] = "⊥";
const char left_parens[] = "(";
const char right_parens[] = ")";
template<fol_formula_syntax Syntax, typename Stream>
inline bool print_for_all(unsigned int quantified_variable, Stream& out) {
switch (Syntax) {
case fol_formula_syntax::TPTP:
return print("![", out) && print_variable<Syntax>(quantified_variable, out) && print("]:", out);
case fol_formula_syntax::CLASSIC:
return print("∀", out) && print_variable<Syntax>(quantified_variable, out);
}
fprintf(stderr, "print_for_all ERROR: Unrecognized fol_formula_syntax.\n");
return false;
}
template<fol_formula_syntax Syntax, typename Stream>
inline bool print_exists(unsigned int quantified_variable, Stream& out) {
switch (Syntax) {
case fol_formula_syntax::TPTP:
return print("?[", out) && print_variable<Syntax>(quantified_variable, out) && print("]:", out);
case fol_formula_syntax::CLASSIC:
return print("∃", out) && print_variable<Syntax>(quantified_variable, out);
}
fprintf(stderr, "print_exists ERROR: Unrecognized fol_formula_syntax.\n");
return false;
}
template<fol_formula_syntax Syntax = fol_formula_syntax::CLASSIC, typename Stream, typename... Printer>
bool print(const fol_formula& formula, Stream& out, Printer&&... printer)
{
switch (formula.type) {
case fol_formula_type::ATOM:
if (!print(formula.atom.predicate, out, std::forward<Printer>(printer)...) || !print('(', out))
return false;
if (formula.atom.arg1.type != fol_term_type::NONE) {
if (!print(formula.atom.arg1, out, std::forward<Printer>(printer)...))
return false;
if (formula.atom.arg2.type != fol_term_type::NONE) {
if (!print(',', out) || !print(formula.atom.arg2, out, std::forward<Printer>(printer)...))
return false;
}
}
return print(')', out);
case fol_formula_type::EQUALS:
return print(formula.equals.arg1, out, std::forward<Printer>(printer)...)
&& print(equals_symbol<Syntax>::symbol, out)
&& print(formula.equals.arg2, out, std::forward<Printer>(printer)...);
case fol_formula_type::TRUE:
return print(true_symbol<Syntax>::symbol, out);
case fol_formula_type::FALSE:
return print(false_symbol<Syntax>::symbol, out);
case fol_formula_type::NOT:
return print(not_symbol<Syntax>::symbol, out) && print(*formula.unary.operand, out, std::forward<Printer>(printer)...);
case fol_formula_type::AND:
return print<fol_formula*, left_parens, right_parens, and_symbol<Syntax>::symbol>(formula.array.operands, formula.array.length, out, pointer_scribe(), std::forward<Printer>(printer)...);
case fol_formula_type::OR:
return print<fol_formula*, left_parens, right_parens, or_symbol<Syntax>::symbol>(formula.array.operands, formula.array.length, out, pointer_scribe(), std::forward<Printer>(printer)...);
case fol_formula_type::IF_THEN:
return print('(', out) && print(*formula.binary.left, out, std::forward<Printer>(printer)...)
&& print(if_then_symbol<Syntax>::symbol, out) && print(*formula.binary.right, out, std::forward<Printer>(printer)...) && print(')', out);
case fol_formula_type::IFF:
return print<fol_formula*, left_parens, right_parens, iff_symbol<Syntax>::symbol>(formula.array.operands, formula.array.length, out, pointer_scribe(), std::forward<Printer>(printer)...);
case fol_formula_type::FOR_ALL:
return print_for_all<Syntax>(formula.quantifier.variable, out)
&& print(*formula.quantifier.operand, out, std::forward<Printer>(printer)...);
case fol_formula_type::EXISTS:
return print_exists<Syntax>(formula.quantifier.variable, out)
&& print(*formula.quantifier.operand, out, std::forward<Printer>(printer)...);
}
fprintf(stderr, "print ERROR: Unrecognized fol_formula_type.\n");
return false;
}
inline bool new_fol_formula(fol_formula*& new_formula) {
new_formula = (fol_formula*) malloc(sizeof(fol_formula));
if (new_formula == NULL) {
fprintf(stderr, "new_fol_formula ERROR: Out of memory.\n");
return false;
}
return true;
}
constexpr bool visit_constant(unsigned int constant) { return true; }
constexpr bool visit_equals(const fol_formula& formula) { return true; }
constexpr bool visit_variable(unsigned int variable) { return true; }
constexpr bool visit_parameter(unsigned int parameter) { return true; }
constexpr bool visit_true(const fol_formula& formula) { return true; }
constexpr bool visit_false(const fol_formula& formula) { return true; }
template<fol_formula_type Operator>
constexpr bool visit_operator(const fol_formula& formula) { return true; }
template<typename Term, typename... Visitor,
typename std::enable_if<std::is_same<typename std::remove_cv<typename std::remove_reference<Term>::type>::type, fol_term>::value>::type* = nullptr>
inline bool visit(Term&& term, Visitor&&... visitor) {
switch (term.type) {
case fol_term_type::CONSTANT:
return visit_constant(term.constant, std::forward<Visitor>(visitor)...);
case fol_term_type::VARIABLE:
return visit_variable(term.variable, std::forward<Visitor>(visitor)...);
case fol_term_type::PARAMETER:
return visit_parameter(term.parameter, std::forward<Visitor>(visitor)...);
case fol_term_type::NONE:
return true;
}
fprintf(stderr, "visit ERROR: Unrecognized fol_term_type.\n");
return false;
}
template<typename Atom, typename... Visitor,
typename std::enable_if<std::is_same<typename std::remove_cv<typename std::remove_reference<Atom>::type>::type, fol_atom>::value>::type* = nullptr>
inline bool visit(Atom&& atom, Visitor&&... visitor) {
return visit_constant(atom.predicate, std::forward<Visitor>(visitor)...)
&& visit(atom.arg1, std::forward<Visitor>(visitor)...)
&& visit(atom.arg2, std::forward<Visitor>(visitor)...);
}
template<typename Formula, typename... Visitor,
typename std::enable_if<std::is_same<typename std::remove_cv<typename std::remove_reference<Formula>::type>::type, fol_formula>::value>::type* = nullptr>
bool visit(Formula&& formula, Visitor&&... visitor)
{
switch (formula.type) {
case fol_formula_type::ATOM:
return visit(formula.atom, std::forward<Visitor>(visitor)...);
case fol_formula_type::EQUALS:
return visit_equals(formula, std::forward<Visitor>(visitor)...)
&& visit(formula.equals.arg1, std::forward<Visitor>(visitor)...)
&& visit(formula.equals.arg2, std::forward<Visitor>(visitor)...);
case fol_formula_type::NOT:
return visit_operator<fol_formula_type::NOT>(formula, std::forward<Visitor>(visitor)...)
&& visit(*formula.unary.operand, std::forward<Visitor>(visitor)...);
case fol_formula_type::AND:
if (!visit_operator<fol_formula_type::AND>(formula, std::forward<Visitor>(visitor)...)) return false;
for (unsigned int i = 0; i < formula.array.length; i++)
if (!visit(*formula.array.operands[i], std::forward<Visitor>(visitor)...)) return false;
return true;
case fol_formula_type::OR:
if (!visit_operator<fol_formula_type::OR>(formula, std::forward<Visitor>(visitor)...)) return false;
for (unsigned int i = 0; i < formula.array.length; i++)
if (!visit(*formula.array.operands[i], std::forward<Visitor>(visitor)...)) return false;
return true;
case fol_formula_type::IF_THEN:
return visit_operator<fol_formula_type::IF_THEN>(formula, std::forward<Visitor>(visitor)...)
&& visit(*formula.binary.left, std::forward<Visitor>(visitor)...)
&& visit(*formula.binary.right, std::forward<Visitor>(visitor)...);
case fol_formula_type::IFF:
if (!visit_operator<fol_formula_type::IFF>(formula, std::forward<Visitor>(visitor)...)) return false;
for (unsigned int i = 0; i < formula.array.length; i++)
if (!visit(*formula.array.operands[i], std::forward<Visitor>(visitor)...)) return false;
return true;
case fol_formula_type::FOR_ALL:
return visit_operator<fol_formula_type::FOR_ALL>(formula, std::forward<Visitor>(visitor)...)
&& visit_variable(formula.quantifier.variable, std::forward<Visitor>(visitor)...)
&& visit(*formula.quantifier.operand, std::forward<Visitor>(visitor)...);
case fol_formula_type::EXISTS:
return visit_operator<fol_formula_type::EXISTS>(formula, std::forward<Visitor>(visitor)...)
&& visit_variable(formula.quantifier.variable, std::forward<Visitor>(visitor)...)
&& visit(*formula.quantifier.operand, std::forward<Visitor>(visitor)...);
case fol_formula_type::TRUE:
return visit_true(formula, std::forward<Visitor>(visitor)...);
case fol_formula_type::FALSE:
return visit_false(formula, std::forward<Visitor>(visitor)...);
}
fprintf(stderr, "visit ERROR: Unrecognized fol_formula_type.\n");
return false;
}
struct parameter_comparator {
unsigned int parameter;
};
constexpr bool visit_constant(unsigned int constant, const parameter_comparator& visitor) { return true; }
constexpr bool visit_variable(unsigned int variable, const parameter_comparator& visitor) { return true; }
constexpr bool visit_equals(const fol_formula& formula, const parameter_comparator& visitor) { return true; }
constexpr bool visit_true(const fol_formula& formula, const parameter_comparator& visitor) { return true; }
constexpr bool visit_false(const fol_formula& formula, const parameter_comparator& visitor) { return true; }
inline bool visit_parameter(unsigned int parameter, const parameter_comparator& visitor) {
return visitor.parameter == parameter;
}
template<fol_formula_type Operator>
constexpr bool visit_operator(const fol_formula& formula, const parameter_comparator& visitor) { return true; }
inline bool contains_parameter(const fol_formula& src, unsigned int parameter) {
parameter_comparator visitor = {parameter};
return !visit(src, visitor);
}
struct parameter_collector {
array<unsigned int>& parameters;
};
constexpr bool visit_constant(unsigned int constant, const parameter_collector& visitor) { return true; }
constexpr bool visit_variable(unsigned int variable, const parameter_collector& visitor) { return true; }
constexpr bool visit_equals(const fol_formula& formula, const parameter_collector& visitor) { return true; }
constexpr bool visit_true(const fol_formula& formula, const parameter_collector& visitor) { return true; }
constexpr bool visit_false(const fol_formula& formula, const parameter_collector& visitor) { return true; }
inline bool visit_parameter(unsigned int parameter, const parameter_collector& visitor) {
return visitor.parameters.add(parameter);
}
template<fol_formula_type Operator>
constexpr bool visit_operator(const fol_formula& formula, const parameter_collector& visitor) { return true; }
inline bool get_parameters(const fol_formula& src, array<unsigned int>& parameters) {
parameter_collector visitor = {parameters};
return !visit(src, visitor);
}
inline bool clone_constant(unsigned int src_constant, unsigned int& dst_constant) {
dst_constant = src_constant;
return true;
}
inline bool clone_predicate(unsigned int src_predicate, unsigned int& dst_predicate) {
dst_predicate = src_predicate;
return true;
}
inline bool clone_variable(unsigned int src_variable, unsigned int& dst_variable) {
dst_variable = src_variable;
return true;
}
inline bool clone_parameter(unsigned int src_parameter, unsigned int& dst_parameter) {
dst_parameter = src_parameter;
return true;
}
template<typename... Cloner>
inline bool clone(const fol_term& src, fol_term& dst, Cloner&&... cloner) {
dst.type = src.type;
switch (src.type) {
case fol_term_type::CONSTANT:
return clone_constant(src.constant, dst.constant, std::forward<Cloner>(cloner)...);
case fol_term_type::VARIABLE:
return clone_variable(src.variable, dst.variable, std::forward<Cloner>(cloner)...);
case fol_term_type::PARAMETER:
return clone_parameter(src.parameter, dst.parameter, std::forward<Cloner>(cloner)...);
case fol_term_type::NONE:
return true;
}
fprintf(stderr, "clone ERROR: Unrecognized fol_term_type.\n");
return false;
}
template<typename... Cloner>
inline bool clone(const fol_atom& src, fol_atom& dst, Cloner&&... cloner) {
return clone_predicate(src.predicate, dst.predicate, std::forward<Cloner>(cloner)...)
&& clone(src.arg1, dst.arg1, std::forward<Cloner>(cloner)...)
&& clone(src.arg2, dst.arg2, std::forward<Cloner>(cloner)...);
}
template<typename... Cloner>
bool clone(const fol_formula& src, fol_formula& dst, Cloner&&... cloner)
{
dst.type = src.type;
dst.reference_count = 1;
switch (src.type) {
case fol_formula_type::ATOM:
return clone(src.atom, dst.atom, std::forward<Cloner>(cloner)...);
case fol_formula_type::EQUALS:
return clone(src.equals.arg1, dst.equals.arg1, std::forward<Cloner>(cloner)...)
&& clone(src.equals.arg2, dst.equals.arg2, std::forward<Cloner>(cloner)...);
case fol_formula_type::NOT:
if (!new_fol_formula(dst.unary.operand)) return false;
if (!clone(*src.unary.operand, *dst.unary.operand, std::forward<Cloner>(cloner)...)) {
free(dst.unary.operand);
return false;
}
return true;
case fol_formula_type::IF_THEN:
if (!new_fol_formula(dst.binary.left)) {
return false;
} else if (!new_fol_formula(dst.binary.right)) {
free(dst.binary.left); return false;
} else if (!clone(*src.binary.left, *dst.binary.left, std::forward<Cloner>(cloner)...)) {
free(dst.binary.left); free(dst.binary.right);
return false;
} else if (!clone(*src.binary.right, *dst.binary.right, std::forward<Cloner>(cloner)...)) {
free(*dst.binary.left); free(dst.binary.left);
free(dst.binary.right); return false;
}
return true;
case fol_formula_type::AND:
case fol_formula_type::OR:
case fol_formula_type::IFF:
dst.array.operands = (fol_formula**) malloc(sizeof(fol_formula*) * src.array.length);
if (dst.array.operands == NULL) return false;
for (unsigned int i = 0; i < src.array.length; i++) {
if (!new_fol_formula(dst.array.operands[i])
|| !clone(*src.array.operands[i], *dst.array.operands[i], std::forward<Cloner>(cloner)...)) {
for (unsigned int j = 0; j < i; j++) {
free(*dst.array.operands[j]); free(dst.array.operands[j]);
}
if (dst.array.operands[i] != NULL) free(dst.array.operands[i]);
free(dst.array.operands); return false;
}
}
dst.array.length = src.array.length;
return true;
case fol_formula_type::FOR_ALL:
case fol_formula_type::EXISTS:
if (!clone_variable(src.quantifier.variable, dst.quantifier.variable, std::forward<Cloner>(cloner)...)
|| !new_fol_formula(dst.quantifier.operand))
return false;
if (!clone(*src.quantifier.operand, *dst.quantifier.operand, std::forward<Cloner>(cloner)...)) {
free(dst.quantifier.operand); return false;
}
return true;
case fol_formula_type::TRUE:
case fol_formula_type::FALSE:
return true;
}
fprintf(stderr, "clone ERROR: Unrecognized fol_formula_type.\n");
return false;
}
template<typename... Cloner>
inline bool clone(const fol_formula* src, fol_formula* dst, Cloner&&... cloner)
{
if (!new_fol_formula(dst)) return false;
return clone(*src, *dst, std::forward<Cloner>(cloner)...);
}
template<typename... Function>
inline bool apply_to_terms(const fol_atom& src, fol_atom& dst, Function&&... function)
{
dst.predicate = src.predicate;
return apply(src.arg1, dst.arg1, std::forward<Function>(function)...)
&& apply(src.arg2, dst.arg2, std::forward<Function>(function)...);
}
template<typename... Function>
inline bool apply_to_terms(const fol_equals& src, fol_equals& dst, Function&&... function)
{
return apply(src.arg1, dst.arg1, std::forward<Function>(function)...)
&& apply(src.arg2, dst.arg2, std::forward<Function>(function)...);
}
template<typename... Function>
fol_formula* apply_to_terms(fol_formula& src, Function&&... function)
{
fol_formula* new_formula;
fol_formula** new_formulas;
fol_formula* left; fol_formula* right;
fol_atom atom; fol_equals equals;
bool changed;
switch (src.type) {
case fol_formula_type::ATOM:
if (!apply_to_terms(src.atom, atom, std::forward<Function>(function)...)) {
return NULL;
} else if (atom == src.atom) {
free(atom);
return &src;
} else {
if (!new_fol_formula(new_formula)) {
free(atom); return NULL;
}
new_formula->atom = atom;
new_formula->type = fol_formula_type::ATOM;
new_formula->reference_count = 1;
return new_formula;
}
case fol_formula_type::EQUALS:
if (!apply_to_terms(src.equals, equals, std::forward<Function>(function)...)) {
return NULL;
} else if (equals == src.equals) {
free(equals);
return &src;
} else {
if (!new_fol_formula(new_formula)) {
free(equals); return NULL;
}
new_formula->equals = equals;
new_formula->type = fol_formula_type::EQUALS;
new_formula->reference_count = 1;
return new_formula;
}
case fol_formula_type::NOT:
left = apply_to_terms(*src.unary.operand, std::forward<Function>(function)...);
if (left == NULL) {
return NULL;
} else if (left == src.unary.operand) {
return &src;
} else {
if (!new_fol_formula(new_formula)) {
free(*left); if (left->reference_count == 0) free(left);
return NULL;
}
new_formula->unary.operand = left;
new_formula->type = fol_formula_type::NOT;
new_formula->reference_count = 1;
return new_formula;
}
case fol_formula_type::IF_THEN:
left = apply_to_terms(*src.binary.left, std::forward<Function>(function)...);
if (left == NULL) return NULL;
right = apply_to_terms(*src.binary.right, std::forward<Function>(function)...);
if (right == NULL) {
if (left != src.binary.left) {
free(*left); if (left->reference_count == 0) free(left);
}
return NULL;
} else if (left == src.binary.left && right == src.binary.right) {
return &src;
} else {
if (!new_fol_formula(new_formula)) {
if (left != src.binary.left) {
free(*left); if (left->reference_count == 0) free(left);
} if (right != src.binary.right) {
free(*right); if (right->reference_count == 0) free(right);
}
return NULL;
}
new_formula->binary.left = left;
new_formula->binary.right = right;
if (left == src.binary.left) left->reference_count++;
if (right == src.binary.right) right->reference_count++;
new_formula->type = src.type;
new_formula->reference_count = 1;
return new_formula;
}
case fol_formula_type::AND:
case fol_formula_type::OR:
case fol_formula_type::IFF:
new_formulas = (fol_formula**) malloc(sizeof(fol_formula*) * src.array.length);
if (new_formulas == NULL) return NULL;