-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathset_reasoning.h
4859 lines (4422 loc) · 181 KB
/
set_reasoning.h
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
#ifndef SET_GRAPH_H_
#define SET_GRAPH_H_
#include <core/array.h>
#include <core/map.h>
#include <stdint.h>
#include <set>
using namespace core;
/* forward declarations */
template<typename BuiltInConstants, typename ProofCalculus, typename Canonicalizer> struct set_reasoning;
template<typename BuiltInConstants, typename ProofCalculus, typename Canonicalizer>
bool find_largest_disjoint_clique_with_set(
const set_reasoning<BuiltInConstants, ProofCalculus, Canonicalizer>&,
unsigned int, unsigned int*&, unsigned int&, unsigned int&, int);
template<typename BuiltInConstants, typename ProofCalculus, typename Canonicalizer>
bool find_largest_disjoint_clique_with_set(
const set_reasoning<BuiltInConstants, ProofCalculus, Canonicalizer>&,
unsigned int, unsigned int, unsigned int*&, unsigned int&, unsigned int&, int);
template<typename T, typename Stream>
bool print(const hash_set<T>& set, Stream&& out) {
if (!print('{', out)) return false;
bool first = true;
for (const T& element : set) {
if (first) {
first = false;
} else if (!print(", ", out)) {
return false;
}
if (!print(element, out)) return false;
}
return print('}', out);
}
struct intensional_set_vertex {
array<unsigned int> parents;
array<unsigned int> children;
static inline bool clone(const intensional_set_vertex& src, intensional_set_vertex& dst) {
if (!array_init(dst.parents, src.parents.capacity)) {
return false;
} else if (!array_init(dst.children, src.children.capacity)) {
core::free(dst.parents);
return false;
}
for (unsigned int parent : src.parents)
dst.parents[dst.parents.length++] = parent;
for (unsigned int child : src.children)
dst.children[dst.children.length++] = child;
return true;
}
static inline void free(intensional_set_vertex& vertex) {
core::free(vertex.parents);
core::free(vertex.children);
}
};
inline bool init(intensional_set_vertex& vertex) {
if (!array_init(vertex.parents, 4)) {
return false;
} else if (!array_init(vertex.children, 4)) {
core::free(vertex.parents);
return false;
}
return true;
}
template<typename Stream>
bool read(intensional_set_vertex& vertex, Stream& in)
{
if (!read(vertex.parents, in)) {
return false;
} else if (!read(vertex.children, in)) {
free(vertex.parents);
return false;
}
return true;
}
template<typename Stream>
bool write(const intensional_set_vertex& vertex, Stream& out)
{
return write(vertex.parents, out)
&& write(vertex.children, out);
}
template<typename ProofCalculus>
struct extensional_set_vertex
{
typedef typename ProofCalculus::Language Formula;
typedef typename ProofCalculus::Proof Proof;
array_map<unsigned int, array<Proof*>> parents;
array_map<unsigned int, array<Proof*>> children;
/* NOTE: we can't properly implement a `clone` function because this struct
only owns the memory of the proofs in `children` and not those in
`parents`, so to correctly clone an extensional graph, we first need to
clone all of the proofs in `children` across all vertices, and then
clone the proofs in `parents`. */
static inline bool clone_except_parents(
const extensional_set_vertex<ProofCalculus>& src,
extensional_set_vertex<ProofCalculus>& dst,
array_map<const Proof*, Proof*>& proof_map,
hash_map<const Formula*, Formula*>& formula_map)
{
if (!array_map_init(dst.parents, src.parents.capacity)) {
return false;
} else if (!array_map_init(dst.children, src.children.capacity)) {
core::free(dst.parents);
return false;
}
for (const auto& entry : src.children) {
dst.children.keys[dst.children.size] = entry.key;
array<Proof*>& dst_proofs = dst.children.values[dst.children.size];
if (!array_init(dst_proofs, entry.value.capacity)) {
core::free(dst);
return false;
}
dst.children.size++;
for (Proof* proof : entry.value) {
if (!Proof::clone(proof, dst_proofs[dst_proofs.length], proof_map, formula_map)) {
core::free(dst);
return false;
}
dst_proofs.length++;
}
}
return true;
}
static inline bool clone_only_parents(
const extensional_set_vertex<ProofCalculus>& src,
extensional_set_vertex<ProofCalculus>& dst,
array_map<const Proof*, Proof*>& proof_map)
{
for (const auto& entry : src.parents) {
dst.parents.keys[dst.parents.size] = entry.key;
array<Proof*>& dst_proofs = dst.parents.values[dst.parents.size];
if (!array_init(dst_proofs, entry.value.capacity))
return false;
dst.parents.size++;
for (Proof* proof : entry.value) {
unsigned int index = proof_map.index_of(proof);
#if !defined(NDEBUG)
if (index == proof_map.size)
fprintf(stderr, "extensional_set_vertex.clone_only_parents WARNING: Given proof does not exist in `proof_map`.\n");
#endif
dst_proofs[dst_proofs.length++] = proof_map.values[index];
}
}
return true;
}
static inline void free(extensional_set_vertex<ProofCalculus>& vertex) {
for (auto entry : vertex.parents)
core::free(entry.value);
for (auto entry : vertex.children) {
for (Proof* proof : entry.value) {
core::free(*proof); if (proof->reference_count == 0) core::free(proof);
}
core::free(entry.value);
}
core::free(vertex.parents);
core::free(vertex.children);
}
};
template<typename ProofCalculus>
inline bool init(extensional_set_vertex<ProofCalculus>& vertex) {
if (!array_map_init(vertex.parents, 4)) {
return false;
} else if (!array_map_init(vertex.children, 4)) {
core::free(vertex.parents);
return false;
}
return true;
}
template<typename ProofCalculus>
inline bool get_proof_map(
const extensional_set_vertex<ProofCalculus>& vertex,
hash_map<const typename ProofCalculus::Proof*, unsigned int>& proof_map,
hash_map<const typename ProofCalculus::Language*, unsigned int>& formula_map)
{
typedef typename ProofCalculus::Proof Proof;
for (const auto& entry : vertex.parents) {
for (const Proof* proof : entry.value) {
if (!get_proof_map(proof, proof_map, formula_map))
return false;
}
} for (const auto& entry : vertex.children) {
for (const Proof* proof : entry.value) {
if (!get_proof_map(proof, proof_map, formula_map))
return false;
}
}
return true;
}
template<typename ProofCalculus, typename Stream>
bool read(
extensional_set_vertex<ProofCalculus>& vertex,
Stream& in,
typename ProofCalculus::Proof** proofs)
{
decltype(vertex.parents.size) parent_count;
decltype(vertex.children.size) child_count;
if (!read(parent_count, in)
|| !read(child_count, in))
return false;
if (!array_map_init(vertex.parents, ((size_t) 1) << (core::log2(parent_count == 0 ? 1 : parent_count) + 1))) {
return false;
} else if (!array_map_init(vertex.children, ((size_t) 1) << (core::log2(child_count == 0 ? 1 : child_count) + 1))) {
free(vertex.parents);
return false;
}
for (unsigned int i = 0; i < parent_count; i++) {
size_t length;
if (!read(vertex.parents.keys[i], in)
|| !read(length, in)
|| !array_init(vertex.parents.values[i], ((size_t) 1) << (core::log2(length == 0 ? 1 : length) + 1)))
{
free(vertex);
return false;
}
vertex.parents.size++;
for (size_t j = 0; j < length; j++) {
unsigned int index;
if (!read(index, in)) {
free(vertex);
return false;
}
vertex.parents.values[i][j] = proofs[index];
vertex.parents.values[i].length++;
}
} for (unsigned int i = 0; i < child_count; i++) {
size_t length;
if (!read(vertex.children.keys[i], in)
|| !read(length, in)
|| !array_init(vertex.children.values[i], ((size_t) 1) << (core::log2(length == 0 ? 1 : length) + 1)))
{
free(vertex);
return false;
}
vertex.children.size++;
for (size_t j = 0; j < length; j++) {
unsigned int index;
if (!read(index, in)) {
free(vertex);
return false;
}
vertex.children.values[i][j] = proofs[index];
proofs[index]->reference_count++;
vertex.children.values[i].length++;
}
}
return true;
}
template<typename ProofCalculus, typename Stream>
bool write(
const extensional_set_vertex<ProofCalculus>& vertex, Stream& out,
const hash_map<const typename ProofCalculus::Proof*, unsigned int>& proof_map)
{
typedef typename ProofCalculus::Proof Proof;
if (!write(vertex.parents.size, out)
|| !write(vertex.children.size, out))
return false;
for (const auto& entry : vertex.parents) {
if (!write(entry.key, out)
|| !write(entry.value.length, out))
return false;
for (const Proof* proof : entry.value)
if (!write(proof_map.get(proof), out)) return false;
} for (const auto& entry : vertex.children) {
if (!write(entry.key, out)
|| !write(entry.value.length, out))
return false;
for (const Proof* proof : entry.value)
if (!write(proof_map.get(proof), out)) return false;
}
return true;
}
struct intensional_set_graph {
intensional_set_vertex* vertices;
intensional_set_graph(unsigned int initial_capacity) {
vertices = (intensional_set_vertex*) malloc(sizeof(intensional_set_vertex) * initial_capacity);
if (vertices == NULL) exit(EXIT_FAILURE);
}
~intensional_set_graph() {
core::free(vertices);
}
static inline void free(intensional_set_graph& graph) {
core::free(graph.vertices);
}
inline bool resize(unsigned int new_capacity) {
return core::resize(vertices, new_capacity);
}
inline bool new_set(unsigned int vertex_id) {
return init(vertices[vertex_id]);
}
template<bool CleanupEdges>
inline void free_set(unsigned int vertex_id) {
if (CleanupEdges) {
for (unsigned int parent : vertices[vertex_id].parents)
remove_edge(parent, vertex_id);
for (unsigned int child : vertices[vertex_id].children)
remove_edge(vertex_id, child);
}
core::free(vertices[vertex_id]);
}
inline bool add_edge(unsigned int parent, unsigned int child) {
if (!vertices[parent].children.add(child)) {
return false;
} else if (!vertices[child].parents.add(parent)) {
vertices[parent].children.length--;
return false;
}
return true;
}
inline void remove_edge(unsigned int parent, unsigned int child) {
unsigned int index = vertices[parent].children.index_of(child);
#if !defined(NDEBUG)
if (index == vertices[parent].children.length)
fprintf(stderr, "intensional_set_graph.remove_edge WARNING: The set %u is not in `vertices[%u].children`.\n", child, parent);
#endif
vertices[parent].children.remove(index);
index = vertices[child].parents.index_of(parent);
#if !defined(NDEBUG)
if (index == vertices[child].parents.length)
fprintf(stderr, "intensional_set_graph.remove_edge WARNING: The set %u is not in `vertices[%u].children`.\n", parent, child);
#endif
vertices[child].parents.remove(index);
}
};
template<typename ProofCalculus>
struct extensional_set_graph
{
typedef typename ProofCalculus::Proof Proof;
extensional_set_vertex<ProofCalculus>* vertices;
extensional_set_graph(unsigned int initial_capacity) {
vertices = (extensional_set_vertex<ProofCalculus>*) malloc(sizeof(extensional_set_vertex<ProofCalculus>) * initial_capacity);
if (vertices == NULL) exit(EXIT_FAILURE);
}
~extensional_set_graph() {
core::free(vertices);
}
static inline void free(extensional_set_graph<ProofCalculus>& graph) {
core::free(graph.vertices);
}
inline bool resize(unsigned int new_capacity) {
return core::resize(vertices, new_capacity);
}
inline bool new_set(unsigned int vertex_id) {
return init(vertices[vertex_id]);
}
template<bool CleanupEdges>
inline void free_set(unsigned int vertex_id) {
if (CleanupEdges) {
for (const auto& entry : vertices[vertex_id].parents) {
core::free(entry.value);
} for (const auto& entry : vertices[vertex_id].children) {
for (Proof* proof : entry.value)
core::free(*proof);
core::free(entry.value);
}
}
core::free(vertices[vertex_id]);
}
inline bool add_edge(unsigned int parent, unsigned int child, Proof* axiom)
{
if (!vertices[parent].children.ensure_capacity(vertices[parent].children.size + 1)
|| !vertices[child].parents.ensure_capacity(vertices[child].parents.size + 1))
return false;
unsigned int index = vertices[parent].children.index_of(child);
array<Proof*>& parent_axioms = vertices[parent].children.values[index];
if (index == vertices[parent].children.size) {
if (!array_init(parent_axioms, 4)) return false;
vertices[parent].children.keys[index] = child;
vertices[parent].children.size++;
} else if (!parent_axioms.ensure_capacity(parent_axioms.length + 1)) {
return false;
}
index = vertices[child].parents.index_of(parent);
array<Proof*>& child_axioms = vertices[child].parents.values[index];
if (index == vertices[child].parents.size) {
if (!array_init(child_axioms, 4)) return false;
vertices[child].parents.keys[index] = parent;
vertices[child].parents.size++;
} else if (!child_axioms.ensure_capacity(child_axioms.length + 1)) {
return false;
}
#if !defined(NDEBUG)
for (Proof* existing_axiom : parent_axioms) {
if (existing_axiom == axiom || *existing_axiom == *axiom) {
fprintf(stderr, "extensional_edge.add_edge WARNING: The given edge already exists.\n");
return false;
}
} for (Proof* existing_axiom : child_axioms) {
if (existing_axiom == axiom || *existing_axiom == *axiom) {
fprintf(stderr, "extensional_edge.add_edge WARNING: The given edge already exists.\n");
return false;
}
}
#endif
parent_axioms[parent_axioms.length++] = axiom;
child_axioms[child_axioms.length++] = axiom;
axiom->reference_count++;
return true;
}
template<typename Formula>
inline Proof* get_existing_edge(unsigned int parent, unsigned int child,
Formula* parent_formula, Formula* child_formula) const
{
typedef typename Formula::Type FormulaType;
array<Proof*>& axioms = vertices[parent].children.get(child);
for (Proof* axiom : axioms) {
Formula* formula = axiom->formula->quantifier.operand;
while (formula->type == FormulaType::FOR_ALL)
formula = formula->quantifier.operand;
if ((formula->binary.left == child_formula || *formula->binary.left == *child_formula)
&& (formula->binary.right == parent_formula || *formula->binary.right == *parent_formula))
{
return axiom;
}
}
return nullptr;
}
template<typename Formula>
inline Proof* get_edge(unsigned int parent, unsigned int child,
Formula* parent_formula, Formula* child_formula,
unsigned int arity, bool& new_edge)
{
typedef typename Formula::Type FormulaType;
if (!vertices[parent].children.ensure_capacity(vertices[parent].children.size + 1)
|| !vertices[child].parents.ensure_capacity(vertices[child].parents.size + 1))
return nullptr;
unsigned int index = vertices[parent].children.index_of(child);
array<Proof*>& parent_axioms = vertices[parent].children.values[index];
if (index == vertices[parent].children.size) {
if (!array_init(parent_axioms, 4)) return nullptr;
vertices[parent].children.keys[index] = child;
vertices[parent].children.size++;
} else if (!parent_axioms.ensure_capacity(parent_axioms.length + 1)) {
return nullptr;
}
index = vertices[child].parents.index_of(parent);
array<Proof*>& child_axioms = vertices[child].parents.values[index];
if (index == vertices[child].parents.size) {
if (!array_init(child_axioms, 4)) return nullptr;
vertices[child].parents.keys[index] = parent;
vertices[child].parents.size++;
} else if (!child_axioms.ensure_capacity(child_axioms.length + 1)) {
return nullptr;
}
for (Proof* existing_axiom : parent_axioms) {
Formula* formula = existing_axiom->formula->quantifier.operand;
while (formula->type == FormulaType::FOR_ALL)
formula = formula->quantifier.operand;
if ((formula->binary.left == child_formula || *formula->binary.left == *child_formula)
&& (formula->binary.right == parent_formula || *formula->binary.right == *parent_formula))
{
new_edge = false;
return existing_axiom;
}
}
new_edge = true;
Formula* formula = Formula::new_for_all(arity, Formula::new_if_then(child_formula, parent_formula));
if (formula == nullptr) return nullptr;
for (unsigned int i = arity - 1; i > 0; i--) {
Formula* new_formula = Formula::new_for_all(i, formula);
if (new_formula == nullptr) {
core::free(*formula); core::free(formula);
return nullptr;
}
formula = new_formula;
}
Proof* new_axiom = ProofCalculus::new_axiom(formula);
core::free(*formula); if (formula->reference_count == 0) core::free(formula);
if (new_axiom == nullptr) return nullptr;
child_formula->reference_count++;
parent_formula->reference_count++;
parent_axioms[parent_axioms.length++] = new_axiom;
child_axioms[child_axioms.length++] = new_axiom;
new_axiom->reference_count++;
return new_axiom;
}
template<typename Formula>
inline void remove_edge(unsigned int parent, unsigned int child,
Formula* parent_formula, Formula* child_formula)
{
typedef typename Formula::Type FormulaType;
unsigned int index = vertices[child].parents.index_of(parent);
#if !defined(NDEBUG)
if (index == vertices[child].parents.size)
fprintf(stderr, "extensional_set_graph.remove_edge WARNING: The set %u is not in `vertices[%u].children`.\n", child, parent);
#endif
array<Proof*>& child_axioms = vertices[child].parents.values[index];
for (unsigned int i = 0; i < child_axioms.length; i++) {
Proof* axiom = child_axioms[i];
Formula* formula = axiom->formula->quantifier.operand;
while (formula->type == FormulaType::FOR_ALL)
formula = formula->quantifier.operand;
if ((formula->binary.left == child_formula || *formula->binary.left == *child_formula)
&& (formula->binary.right == parent_formula || *formula->binary.right == *parent_formula))
{
child_axioms.remove(i);
break;
}
}
if (child_axioms.length == 0) {
core::free(child_axioms);
vertices[child].parents.remove_at(index);
}
index = vertices[parent].children.index_of(child);
#if !defined(NDEBUG)
if (index == vertices[parent].children.size)
fprintf(stderr, "extensional_set_graph.remove_edge WARNING: The set %u is not in `vertices[%u].children`.\n", parent, child);
#endif
array<Proof*>& parent_axioms = vertices[parent].children.values[index];
for (unsigned int i = 0; i < parent_axioms.length; i++) {
Proof* axiom = parent_axioms[i];
Formula* formula = axiom->formula->quantifier.operand;
while (formula->type == FormulaType::FOR_ALL)
formula = formula->quantifier.operand;
if ((formula->binary.left == child_formula || *formula->binary.left == *child_formula)
&& (formula->binary.right == parent_formula || *formula->binary.right == *parent_formula))
{
core::free(*axiom);
if (axiom->reference_count == 0)
core::free(axiom);
parent_axioms.remove(i);
break;
}
}
if (parent_axioms.length == 0) {
core::free(parent_axioms);
vertices[parent].children.remove_at(index);
}
}
};
bool get_ancestors(
const intensional_set_graph& graph, unsigned int vertex,
array_map<unsigned int, unsigned int>& ancestors)
{
if (!ancestors.ensure_capacity(ancestors.size + 1)) return false;
size_t index = ancestors.index_of(vertex);
if (index < ancestors.size) {
ancestors.values[index]++;
return true;
}
ancestors.keys[index] = vertex;
ancestors.values[index] = 0;
ancestors.size++;
array<unsigned int> stack(8);
stack.add(vertex);
while (stack.length > 0) {
unsigned int v = stack.pop();
for (unsigned int parent : graph.vertices[v].parents) {
if (!ancestors.ensure_capacity(ancestors.size + 1)) return false;
size_t index = ancestors.index_of(parent);
if (index < ancestors.size) {
ancestors.values[index]++;
} else {
ancestors.keys[index] = parent;
ancestors.values[index] = 1;
ancestors.size++;
if (!stack.add(parent)) return false;
}
}
}
return true;
}
bool get_descendants(
const intensional_set_graph& graph, unsigned int vertex,
array_map<unsigned int, unsigned int>& descendants)
{
if (!descendants.ensure_capacity(descendants.size + 1)) return false;
size_t index = descendants.index_of(vertex);
if (index < descendants.size) {
descendants.values[index]++;
return true;
}
descendants.keys[index] = vertex;
descendants.values[index] = 0;
descendants.size++;
array<unsigned int> stack(8);
stack.add(vertex);
while (stack.length > 0) {
unsigned int v = stack.pop();
for (unsigned int child : graph.vertices[v].children) {
if (!descendants.ensure_capacity(descendants.size + 1)) return false;
size_t index = descendants.index_of(child);
if (index < descendants.size) {
descendants.values[index]++;
} else {
descendants.keys[index] = child;
descendants.values[index] = 1;
descendants.size++;
if (!stack.add(child)) return false;
}
}
}
return true;
}
typedef uint_fast8_t tuple_element_type_specifier;
enum class tuple_element_type : tuple_element_type_specifier {
CONSTANT,
NUMBER,
STRING
};
struct tuple_element {
tuple_element_type type;
union {
unsigned int constant;
hol_number number;
string str;
};
static inline unsigned int hash(const tuple_element& key) {
/* TODO: precompute these statically */
unsigned int type_hash = default_hash(key.type);
switch (key.type) {
case tuple_element_type::CONSTANT:
return type_hash ^ default_hash(key.constant);
case tuple_element_type::NUMBER:
return type_hash ^ default_hash(key.number);
case tuple_element_type::STRING:
return type_hash ^ string::hash(key.str);
}
fprintf(stderr, "tuple_element.hash ERROR: Unrecognized `tuple_element_type`.\n");
exit(EXIT_FAILURE);
}
static inline void move(const tuple_element& src, tuple_element& dst) {
dst.type = src.type;
switch (src.type) {
case tuple_element_type::CONSTANT:
dst.constant = src.constant; return;
case tuple_element_type::NUMBER:
dst.number = src.number; return;
case tuple_element_type::STRING:
core::move(src.str, dst.str); return;
}
fprintf(stderr, "tuple_element.move ERROR: Unrecognized `tuple_element_type`.\n");
exit(EXIT_FAILURE);
}
static inline void free(tuple_element& element) {
if (element.type == tuple_element_type::STRING)
core::free(element.str);
}
private:
inline bool init_helper(const tuple_element& src) {
type = src.type;
switch (type) {
case tuple_element_type::CONSTANT:
constant = src.constant; return true;
case tuple_element_type::NUMBER:
number = src.number; return true;
case tuple_element_type::STRING:
return init(str, src.str);
}
fprintf(stderr, "tuple_element.init_helper ERROR: Unrecognized `tuple_element_type`.\n");
return false;
}
friend bool init(tuple_element&, const tuple_element&);
};
inline bool init(tuple_element& element, const tuple_element& src) {
return element.init_helper(src);
}
inline bool operator == (const tuple_element& first, const tuple_element& second) {
if (first.type != second.type)
return false;
switch (first.type) {
case tuple_element_type::CONSTANT:
return first.constant == second.constant;
case tuple_element_type::NUMBER:
return first.number == second.number;
case tuple_element_type::STRING:
return first.str == second.str;
}
fprintf(stderr, "operator == ERROR: Unrecognized `tuple_element_type`.\n");
return false;
}
inline bool operator != (const tuple_element& first, const tuple_element& second) {
if (first.type != second.type)
return true;
switch (first.type) {
case tuple_element_type::CONSTANT:
return first.constant != second.constant;
case tuple_element_type::NUMBER:
return first.number != second.number;
case tuple_element_type::STRING:
return first.str != second.str;
}
fprintf(stderr, "operator != ERROR: Unrecognized `tuple_element_type`.\n");
return false;
}
inline bool operator < (const tuple_element& first, const tuple_element& second) {
if (first.type < second.type) return true;
else if (first.type > second.type) return false;
switch (first.type) {
case tuple_element_type::CONSTANT:
return first.constant < second.constant;
case tuple_element_type::NUMBER:
return first.number < second.number;
case tuple_element_type::STRING:
return first.str < second.str;
}
fprintf(stderr, "operator < ERROR: Unrecognized `tuple_element_type`.\n");
return false;
}
template<typename Stream, typename... Printer>
inline bool print(const tuple_element& element, Stream& out, Printer&&... constant_printer) {
switch (element.type) {
case tuple_element_type::CONSTANT:
return print(element.constant, out, std::forward<Printer>(constant_printer)...);
case tuple_element_type::NUMBER:
return print(element.number, out);
case tuple_element_type::STRING:
return print('"', out) && print(element.str, out) && print('"', out);
}
fprintf(stderr, "print ERROR: Unrecognized `tuple_element_type`.\n");
return false;
}
template<typename Stream>
bool read(tuple_element& tup, Stream& in)
{
tuple_element_type_specifier type;
if (!read(type, in))
return false;
tup.type = (tuple_element_type) type;
switch (tup.type) {
case tuple_element_type::CONSTANT:
return read(tup.constant, in);
case tuple_element_type::NUMBER:
return read(tup.number, in);
case tuple_element_type::STRING:
return read(tup.str, in);
}
fprintf(stderr, "read ERROR: Unrecognized `tuple_element_type`.\n");
return false;
}
template<typename Stream>
bool write(const tuple_element& tup, Stream& out)
{
if (!write((tuple_element_type_specifier) tup.type, out))
return false;
switch (tup.type) {
case tuple_element_type::CONSTANT:
return write(tup.constant, out);
case tuple_element_type::NUMBER:
return write(tup.number, out);
case tuple_element_type::STRING:
return write(tup.str, out);
}
fprintf(stderr, "write ERROR: Unrecognized `tuple_element_type`.\n");
return false;
}
struct tuple {
tuple_element* elements;
unsigned int length;
inline bool operator = (const tuple& src) {
return init_helper(src.elements, src.length);
}
inline tuple_element& operator [] (unsigned int i) {
return elements[i];
}
inline const tuple_element& operator [] (unsigned int i) const {
return elements[i];
}
static inline bool is_empty(const tuple& src) {
return src.elements == nullptr;
}
static inline void set_empty(tuple& key) {
key.elements = nullptr;
}
static inline void move(const tuple& src, tuple& dst) {
dst.elements = src.elements;
dst.length = src.length;
}
static inline void swap(tuple& first, tuple& second) {
core::swap(first.elements, second.elements);
core::swap(first.length, second.length);
}
static inline unsigned int hash(const tuple& key) {
unsigned int value = 0;
for (unsigned int i = 0; i < key.length; i++)
value ^= tuple_element::hash(key.elements[i]);
return value;
}
static inline void free(tuple& tup) {
for (unsigned int i = 0; i < tup.length; i++)
core::free(tup.elements[i]);
core::free(tup.elements);
}
private:
inline bool init_helper(unsigned int src_length) {
length = src_length;
elements = (tuple_element*) malloc(sizeof(tuple_element) * length);
if (elements == nullptr) {
fprintf(stderr, "tuple.init_helper ERROR: Out of memory.\n");
return false;
}
return true;
}
inline bool init_helper(const tuple_element* src_elements, unsigned int src_length) {
if (!init_helper(src_length))
return false;
for (unsigned int i = 0; i < src_length; i++) {
if (!init(elements[i], src_elements[i])) {
for (unsigned int j = 0; j < i; j++)
core::free(elements[j]);
core::free(elements);
return false;
}
}
return true;
}
friend bool init(tuple&, unsigned int);
friend bool init(tuple&, const tuple&);
friend bool init(tuple&, const tuple_element*, unsigned int);
};
inline bool init(tuple& new_tuple, unsigned int length) {
return new_tuple.init_helper(length);
}
inline bool init(tuple& new_tuple, const tuple& src) {
return new_tuple.init_helper(src.elements, src.length);
}
inline bool init(tuple& new_tuple, const tuple_element* src_elements, unsigned int src_length) {
return new_tuple.init_helper(src_elements, src_length);
}
inline bool operator == (const tuple& first, const tuple& second) {
if (first.length != second.length)
return false;
/* `first` may be uninitialized */
if (first.elements == nullptr)
return false;
for (unsigned int i = 0; i < first.length; i++)
if (first.elements[i] != second.elements[i]) return false;
return true;
}
inline bool operator != (const tuple& first, const tuple& second) {
if (first.length != second.length)
return true;
/* `first` may be uninitialized */
if (first.elements == nullptr)
return true;
for (unsigned int i = 0; i < first.length; i++)
if (first.elements[i] != second.elements[i]) return true;
return false;
}
inline bool operator < (const tuple& first, const tuple& second) {
if (first.length < second.length) return true;
else if (first.length > second.length) return false;
for (unsigned int i = 0; i < first.length; i++) {
if (first.elements[i] < second.elements[i]) return true;
else if (second.elements[i] < first.elements[i]) return false;
}
return false;
}
inline bool operator >= (const tuple& first, const tuple& second) {
return !(first < second);
}
template<typename Stream, typename... Printer>
bool print(const tuple& tup, Stream& out, Printer&&... printer) {
if (tup.length == 0)
return print("()", out);
if (tup.length == 1)
return print(tup[0], out, std::forward<Printer>(printer)...);
if (!print('(', out) || !print(tup[0], out, std::forward<Printer>(printer)...))
return false;
for (unsigned int i = 1; i < tup.length; i++) {
if (!print(", ", out) || !print(tup[i], out, std::forward<Printer>(printer)...))
return false;
}
return print(')', out);
}
template<typename Stream>
bool read(tuple& tup, Stream& in)
{
if (!read(tup.length, in))
return false;
tup.elements = (tuple_element*) malloc(sizeof(tuple_element) * tup.length);
if (tup.elements == nullptr) {
fprintf(stderr, "read ERROR: Insufficient memory for `tuple.elements`.\n");
return false;
}
for (unsigned int i = 0; i < tup.length; i++) {
if (!read(tup.elements[i], in)) {
for (unsigned int j = 0; j < i; j++)
free(tup.elements[j]);
free(tup.elements);
return false;
}
}
return true;
}
template<typename Stream>
bool write(const tuple& tup, Stream& out) {
if (!write(tup.length, out))
return false;
for (unsigned int i = 0; i < tup.length; i++)
if (!write(tup.elements[i], out)) return false;
return true;
}
template<typename BuiltInConstants, typename ProofCalculus>
struct set_info
{
typedef typename ProofCalculus::Language Formula;