/
equality_graph.c
2301 lines (1946 loc) · 73 KB
/
equality_graph.c
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
/*
* This file is part of the Yices SMT Solver.
* Copyright (C) 2017 SRI International.
*
* Yices is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Yices is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Yices. If not, see <http://www.gnu.org/licenses/>.
*/
#include "equality_graph.h"
#include "utils/memalloc.h"
#include "mcsat/tracing.h"
#include "mcsat/variable_db.h"
#include "mcsat/trail.h"
#include <inttypes.h>
#include <assert.h>
/* Default initial size and max size */
#define BFS_VECTOR_DEFAULT_SIZE 10
#define BFS_VECTOR_MAX_SIZE (UINT32_MAX/sizeof(bfs_entry_t))
static inline
void bfs_vector_construct(bfs_vector_t *v, uint32_t n) {
if (n >= BFS_VECTOR_MAX_SIZE) {
out_of_memory();
}
v->capacity = n;
v->size = 0;
v->data = NULL;
if (n > 0) {
v->data = (bfs_entry_t*) safe_malloc(n * sizeof(bfs_entry_t));
}
}
static inline
void bfs_vector_destruct(bfs_vector_t* v) {
safe_free(v->data);
v->data = NULL;
}
static inline
void bfs_vector_extend(bfs_vector_t *v) {
uint32_t n;
n = v->capacity;
if (n == 0) {
n = BFS_VECTOR_DEFAULT_SIZE;
} else {
n ++;
n += n >> 1;
if (n >= BFS_VECTOR_MAX_SIZE) {
out_of_memory();
}
}
v->data = (bfs_entry_t*) safe_realloc(v->data, n * sizeof(bfs_entry_t));
v->capacity = n;
}
static inline
void bfs_vector_shrink(bfs_vector_t *v, uint32_t n) {
assert(n <= v->size);
v->size = n;
}
static inline
void bfs_vector_push(bfs_vector_t* v, eq_node_id_t n, uint32_t prev, eq_edge_id_t e) {
uint32_t i;
i = v->size;
if (i >= v->capacity) {
bfs_vector_extend(v);
}
v->data[i].n = n;
v->data[i].prev = prev;
v->data[i].e = e;
v->size = i+1;
}
static
void eq_graph_propagate(eq_graph_t* eq);
static inline
const char* eq_graph_reason_to_string(eq_reason_type_t reason) {
switch (reason) {
case REASON_IS_FUNCTION_DEF: return "function definition";
case REASON_IS_CONSTANT_DEF: return "constant definition";
case REASON_IS_CONGRUENCE: return "congruence";
case REASON_IS_CONGRUENCE_EQ_SYM: return "eq sym congruence";
case REASON_IS_TRUE_EQUALITY: return "equality = true";
case REASON_IS_REFLEXIVITY: return "reflexivity";
case REASON_IS_EVALUATION: return "eq evaluation";
case REASON_IS_IN_TRAIL: return "assigned in trail";
case REASON_IS_USER: return "user asserted";
default:
assert(false);
}
return "unknown";
}
static inline
const char* eq_graph_reason_to_short_string(eq_reason_type_t reason) {
switch (reason) {
case REASON_IS_FUNCTION_DEF: return "f-def";
case REASON_IS_CONSTANT_DEF: return "c-def";
case REASON_IS_CONGRUENCE: return "cc";
case REASON_IS_CONGRUENCE_EQ_SYM: return "e-cc";
case REASON_IS_TRUE_EQUALITY: return "eq";
case REASON_IS_REFLEXIVITY: return "refl";
case REASON_IS_EVALUATION: return "eval";
case REASON_IS_IN_TRAIL: return "trail";
case REASON_IS_USER: return "user";
default:
assert(false);
}
return "unknown";
}
static
void eq_graph_eq_assigned_to_value(eq_graph_t* eq, eq_node_id_t eq_id, eq_node_id_t v_id);
static
void eq_graph_eq_args_updated(eq_graph_t* eq, eq_node_id_t eq_id);
/** Get the id of the node */
static inline
eq_node_id_t eq_graph_get_node_id(const eq_graph_t* eq, const eq_node_t* n) {
return n - eq->nodes;
}
/** Get the node given id */
static inline
eq_node_t* eq_graph_get_node(eq_graph_t* eq, eq_node_id_t id) {
assert (id >= 0 && id < eq->nodes_size);
return eq->nodes + id;
}
/** Get the node given id */
static inline
const eq_node_t* eq_graph_get_node_const(const eq_graph_t* eq, eq_node_id_t id) {
assert (id < eq->nodes_size);
return eq->nodes + id;
}
static inline
const eq_node_id_t* eq_graph_get_children(const eq_graph_t* eq, eq_node_id_t id) {
assert (id < eq->nodes_size);
int_hmap_pair_t* find = int_hmap_find((int_hmap_t*) &eq->node_to_children, id);
if (find != NULL) {
return (const eq_node_id_t*) eq->children_list.data + find->val;
} else {
return NULL;
}
}
#ifndef NDEBUG
static inline
bool eq_graph_has_children(const eq_graph_t* eq, eq_node_id_t id) {
return eq_graph_get_children(eq, id) != NULL;
}
static inline
bool eq_graph_is_term(const eq_graph_t* eq, eq_node_id_t n_id) {
const eq_node_t* n = eq_graph_get_node_const(eq, n_id);
return n->type == EQ_NODE_TERM;
}
static inline
bool eq_graph_is_value(const eq_graph_t* eq, eq_node_id_t n_id) {
const eq_node_t* n = eq_graph_get_node_const(eq, n_id);
return n->type == EQ_NODE_VALUE;
}
static inline
bool eq_graph_is_pair(const eq_graph_t* eq, eq_node_id_t n_id) {
const eq_node_t* n = eq_graph_get_node_const(eq, n_id);
return n->type == EQ_NODE_PAIR || n->type == EQ_NODE_EQ_PAIR;
}
#endif
/** Add a value node */
eq_node_id_t eq_graph_add_value(eq_graph_t* eq, const mcsat_value_t* v);
/** Is this value registered yet? */
bool eq_graph_has_value(const eq_graph_t* eq, const mcsat_value_t* v);
/** Return the id of a value */
eq_node_id_t eq_graph_value_id(const eq_graph_t* eq, const mcsat_value_t* v);
void eq_graph_construct(eq_graph_t* eq, plugin_context_t* ctx, const char* name) {
eq->ctx = ctx;
eq->nodes_capacity = 0;
eq->nodes_size = 0;
eq->nodes = NULL;
eq->edges_capacity = 0;
eq->edges_size = 0;
eq->edges = NULL;
eq->uselist_nodes_capacity = 0;
eq->uselist_nodes_size = 0;
eq->uselist_nodes = NULL;
eq->name = name;
eq->in_conflict = false;
eq->conflict_lhs = eq_node_null;
eq->conflict_rhs = eq_node_null;
eq->in_propagate = false;
eq->trail_i = 0;
init_int_hmap(&eq->kind_to_id, 0);
init_int_hmap(&eq->term_to_id, 0);
init_value_hmap(&eq->value_to_id, 0);
init_pmap2(&eq->pair_to_id);
init_pmap2(&eq->eq_pair_to_id);
init_ivector(&eq->kind_list, 0);
init_ivector(&eq->terms_list, 0);
init_value_vector(&eq->values_list, 0);
init_ivector(&eq->pair_list, 0);
scope_holder_construct(&eq->scope_holder);
init_ivector(&eq->graph, 0);
init_pmap2(&eq->pair_to_rep);
init_pmap2(&eq->eq_pair_to_rep);
init_merge_queue(&eq->merge_queue, 0);
init_ivector(&eq->merges, 0);
init_ivector(&eq->term_value_merges, 0);
init_ivector(&eq->uselist, 0);
init_ivector(&eq->uselist_updates, 0);
init_ivector(&eq->children_list, 0);
init_int_hmap(&eq->node_to_children, 0);
// Add true/false
eq->true_node_id = eq_graph_add_value(eq, &mcsat_value_true);
eq->false_node_id = eq_graph_add_value(eq, &mcsat_value_false);
eq->graph_out = 0;
bfs_vector_construct(&eq->bfs_queue, 0);
init_ivector(&eq->explain_cache_list, 0);
// init_pmap2(&eq->explain_cache_map): initialized on every call
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "eq_graph_construct[%s]()\n", eq->name);
}
}
void eq_graph_destruct(eq_graph_t* eq) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "eq_graph_destruct[%s]()\n", eq->name);
}
safe_free(eq->nodes);
safe_free(eq->edges);
delete_int_hmap(&eq->kind_to_id);
delete_int_hmap(&eq->term_to_id);
delete_value_hmap(&eq->value_to_id);
delete_pmap2(&eq->pair_to_id);
delete_pmap2(&eq->eq_pair_to_id);
delete_ivector(&eq->kind_list);
delete_ivector(&eq->terms_list);
delete_value_vector(&eq->values_list);
delete_ivector(&eq->pair_list);
scope_holder_destruct(&eq->scope_holder);
delete_ivector(&eq->graph);
delete_pmap2(&eq->pair_to_rep);
delete_pmap2(&eq->eq_pair_to_rep);
delete_merge_queue(&eq->merge_queue);
delete_ivector(&eq->merges);
delete_ivector(&eq->term_value_merges);
safe_free(eq->uselist_nodes);
delete_ivector(&eq->uselist);
delete_ivector(&eq->uselist_updates);
delete_ivector(&eq->children_list);
delete_int_hmap(&eq->node_to_children);
bfs_vector_destruct(&eq->bfs_queue);
delete_ivector(&eq->explain_cache_list);
// delete_pmap2(&eq->explain_cache_map): deleted when done
}
// Default initial size and max size
#define DEFAULT_GRAPH_SIZE 10
#define MAX_GRAPH_SIZE (UINT32_MAX/sizeof(eq_node_t))
#define DEFAULT_EDGES_SIZE 10
#define MAX_EDGES_SIZE (UINT32_MAX/sizeof(eq_edge_t))
#define DEFAULT_USELIST_NODES_SIZE 10
#define MAX_USELIST_NODES_SIZE (UINT32_MAX/sizeof(eq_uselist_t))
static
eq_uselist_id_t eq_graph_new_uselist_node(eq_graph_t* eq, eq_node_id_t node, eq_uselist_id_t next) {
uint32_t n = eq->uselist_nodes_size;
eq_uselist_id_t id = eq->uselist_nodes_size;
// Check if we need to resize
if (n == eq->uselist_nodes_capacity) {
// Compute new size
if (n == 0) {
n = DEFAULT_USELIST_NODES_SIZE;
} else {
n ++;
n += n >> 1;
if (n >= MAX_USELIST_NODES_SIZE) {
out_of_memory();
}
}
// Resize
eq->uselist_nodes = (eq_uselist_t*) safe_realloc(eq->uselist_nodes, n * sizeof(eq_uselist_t));
eq->uselist_nodes_capacity = n;
}
// Construct the new node
eq_uselist_t* new_node = eq->uselist_nodes + id;
new_node->node = node;
new_node->next = next;
// More nodes
eq->uselist_nodes_size ++;
// Return the new element
return id;
}
static
eq_node_id_t eq_graph_new_node(eq_graph_t* eq, eq_node_type_t type, uint32_t index) {
uint32_t n = eq->nodes_size;
eq_node_id_t id = eq->nodes_size;
// Check if we need to resize
if (n == eq->nodes_capacity) {
// Compute new size
if (n == 0) {
n = DEFAULT_GRAPH_SIZE;
} else {
n ++;
n += n >> 1;
if (n >= MAX_GRAPH_SIZE) {
out_of_memory();
}
}
// Resize
eq->nodes = (eq_node_t*) safe_realloc(eq->nodes, n * sizeof(eq_node_t));
eq->nodes_capacity = n;
}
// Construct the new node
eq_node_t* new_node = eq->nodes + eq->nodes_size;
new_node->find = id;
new_node->next = id;
new_node->size = 1;
new_node->type = type;
new_node->index = index;
new_node->uselist = eq_uselist_null;
// More nodes
eq->nodes_size ++;
// Add empty edge
ivector_push(&eq->graph, eq_edge_null);
// Add empty uselist
ivector_push(&eq->uselist, eq_uselist_null);
// Return the new element
return id;
}
static
eq_node_id_t eq_graph_add_kind(eq_graph_t* eq, term_kind_t kind) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "eq_graph_add_kind[%s](): %s\n", eq->name, kind_to_string(kind));
}
// Check if already there
int_hmap_pair_t* find = int_hmap_get(&eq->kind_to_id, kind);
if (find->val >= 0) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "already there: %"PRIi32"\n", find->val);
}
return find->val;
}
// Index where we put the kind
uint32_t index = eq->kind_list.size;
ivector_push(&eq->kind_list, kind);
// Setup the new node
eq_node_id_t id = eq_graph_new_node(eq, EQ_NODE_KIND, index);
find->val = id;
assert(eq->nodes_size == eq->graph.size);
assert(eq->kind_list.size + eq->terms_list.size + eq->values_list.size + eq->pair_list.size / 2 == eq->nodes_size);
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "id: %"PRIi32"\n", id);
}
// Added, done
return id;
}
eq_node_id_t eq_graph_add_term_internal(eq_graph_t* eq, term_t t) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "eq_graph_add_term[%s](): ", eq->name);
ctx_trace_term(eq->ctx, t);
}
// Check if already there
int_hmap_pair_t* find = int_hmap_get(&eq->term_to_id, t);
if (find->val >= 0) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "already there: %"PRIi32"\n", find->val);
}
return find->val;
}
// Index where we put the term
uint32_t index = eq->terms_list.size;
ivector_push(&eq->terms_list, t);
// Setup the new node
eq_node_id_t id = eq_graph_new_node(eq, EQ_NODE_TERM, index);
find->val = id;
assert(eq->nodes_size == eq->graph.size);
assert(eq->kind_list.size + eq->terms_list.size + eq->values_list.size + eq->pair_list.size / 2 == eq->nodes_size);
// If the node is a constant, we also create a value for it
bool is_const = is_const_term(eq->ctx->terms, t);
if (is_const) {
mcsat_value_t t_value;
mcsat_value_construct_from_constant_term(&t_value, eq->ctx->terms, t);
eq_node_id_t v_id = eq_graph_add_value(eq, &t_value);
mcsat_value_destruct(&t_value);
merge_queue_push_init(&eq->merge_queue, id, v_id, REASON_IS_CONSTANT_DEF, 0);
}
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "id: %"PRIi32"\n", id);
}
// Added, done
return id;
}
uint32_t eq_graph_term_size(const eq_graph_t* eq) {
return eq->terms_list.size;
}
eq_node_id_t eq_graph_add_term(eq_graph_t* eq, term_t t) {
eq_node_id_t id = eq_graph_add_term_internal(eq, t);
eq_graph_propagate(eq);
return id;
}
eq_node_id_t eq_graph_add_value(eq_graph_t* eq, const mcsat_value_t* v) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "eq_graph_add_value[%s](): ", eq->name);
mcsat_value_print(v, trace_out(eq->ctx->tracer));
ctx_trace_printf(eq->ctx, "\n");
}
// Check if already there
value_hmap_pair_t* find = value_hmap_get(&eq->value_to_id, v);
if (find->val >= 0) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "already there: %"PRIi32"\n", find->val);
}
return find->val;
}
// Index where we put the value
uint32_t index = eq->values_list.size;
mcsat_value_t* v_copy = value_vector_push(&eq->values_list);
mcsat_value_assign(v_copy, v);
// Setup the new node
eq_node_id_t id = eq_graph_new_node(eq, EQ_NODE_VALUE, index);
find->val = id;
assert(eq->kind_list.size + eq->terms_list.size + eq->values_list.size + eq->pair_list.size / 2 == eq->nodes_size);
assert(eq->nodes_size == eq->graph.size);
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "id: %"PRIi32"\n", id);
}
// Added, done
return id;
}
static inline
void eq_graph_add_to_uselist(eq_graph_t* eq, eq_node_id_t n_id, eq_node_id_t parent_id) {
assert(n_id < eq->uselist.size);
eq_uselist_id_t n_uselist = eq->uselist.data[n_id];
eq->uselist.data[n_id] = eq_graph_new_uselist_node(eq, parent_id, n_uselist);
ivector_push(&eq->uselist_updates, n_id);
}
/**
* Adds a pair. If n_children > 0 it will associate the children with the pair
* in fun_children_array. If the pair is already there it will pop the children
* of the eq->children array
*/
eq_node_id_t eq_graph_add_pair(eq_graph_t* eq, eq_node_type_t type, eq_node_id_t p1, eq_node_id_t p2, uint32_t children_size, uint32_t children_start) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "eq_graph_add_pair[%s]()\n", eq->name);
}
assert(type == EQ_NODE_PAIR || type == EQ_NODE_EQ_PAIR);
assert(type != EQ_NODE_EQ_PAIR || children_size > 0);
// Check if already there
pmap2_t* cache = type == EQ_NODE_PAIR ? &eq->pair_to_id : &eq->eq_pair_to_id;
pmap2_rec_t* find = pmap2_get(cache, p1, p2);
if (find->val >= 0) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "already there: %"PRIi32"\n", find->val);
}
// Remove from children array
if (children_size > 0) {
assert(eq->children_list.size == children_start + children_size + 1); // + 1 for null
ivector_shrink(&eq->children_list, children_start);
}
return find->val;
}
// Index where we put the value
uint32_t index = eq->pair_list.size;
ivector_push(&eq->pair_list, p1);
ivector_push(&eq->pair_list, p2);
// Setup the new node
eq_node_id_t id = eq_graph_new_node(eq, type, index);
find->val = id;
// Remember the children
if (children_size > 0) {
int_hmap_add(&eq->node_to_children, id, children_start);
}
assert(eq->kind_list.size + eq->terms_list.size + eq->values_list.size + eq->pair_list.size / 2 == eq->nodes_size);
assert(eq->nodes_size == eq->graph.size);
// Add to uselists: p1 is used in id, p2 is used in id
eq_graph_add_to_uselist(eq, p1, id);
eq_graph_add_to_uselist(eq, p2, id);
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "id: %"PRIi32"\n", id);
}
// Added, done
return id;
}
void eq_graph_update_pair_hash(eq_graph_t* eq, eq_node_id_t pair_id) {
// n = (n1, n2)
const eq_node_t* n = eq_graph_get_node_const(eq, pair_id);
assert(n->type == EQ_NODE_PAIR || n->type == EQ_NODE_EQ_PAIR);
// n1
eq_node_id_t p1 = eq->pair_list.data[n->index];
const eq_node_t* n1 = eq_graph_get_node_const(eq, p1);
// n2
eq_node_id_t p2 = eq->pair_list.data[n->index + 1];
const eq_node_t* n2 = eq_graph_get_node_const(eq, p2);
// The cache we're using
pmap2_t* rep_cache = n->type == EQ_NODE_PAIR ? &eq->pair_to_rep : &eq->eq_pair_to_rep;
// Store normalized pair or merge if someone is already there
pmap2_rec_t* find = pmap2_get(rep_cache, n1->find, n2->find);
if (find->val < 0) {
// New representative
find->val = pair_id;
} else {
// Merge with existing representative
if (find->val != pair_id) {
merge_queue_push_init(&eq->merge_queue, pair_id, find->val, REASON_IS_CONGRUENCE, 0);
}
}
// If equality we check for symmetry and other
if (n->type == EQ_NODE_EQ_PAIR) {
// Check for reflexivity and evaluation
eq_graph_eq_args_updated(eq, pair_id);
// Check for symmetry
if (n1->find != n2->find) {
find = pmap2_find(rep_cache, n2->find, n1->find);
if (find != NULL && find->val != pair_id) {
merge_queue_push_init(&eq->merge_queue, pair_id, find->val, REASON_IS_CONGRUENCE_EQ_SYM, 0);
}
}
}
}
/** Generic function add */
static
eq_node_id_t eq_graph_add_fun_term(eq_graph_t* eq, term_t t, term_t f_term, term_kind_t f_kind, uint32_t n, const term_t* c_terms) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "eq_graph_add_fun_term[%s](): ", eq->name);
ctx_trace_term(eq->ctx, t);
}
assert(n >= 1);
assert(f_term == NULL_TERM || f_kind == UNINTERPRETED_TERM);
// Add the term f
eq_node_id_t f_id = eq_graph_add_term_internal(eq, t);
// We add the function term f(x_1, ..., x_n) as a sequence of pair nodes:
//
// n_1 = (x_n-1, x_n)
// n_2 = (x_n-2, n_1)
// ...
// n_n = (f, n_n-1)
//
// These nodes we do congruence over.
// Where we put the children
uint32_t children_start = eq->children_list.size;
uint32_t children_size = 0;
// Add the function itself
eq_node_type_t final_pair_type;
if (f_kind == UNINTERPRETED_TERM) {
assert(f_term != NULL_TERM);
eq_node_id_t c = eq_graph_add_term(eq, f_term);
ivector_push(&eq->children_list, c);
children_size ++;
final_pair_type = EQ_NODE_PAIR;
} else {
assert(f_term == NULL_TERM);
if (f_kind == EQ_TERM) {
final_pair_type = EQ_NODE_EQ_PAIR;
} else {
eq_node_id_t c = eq_graph_add_kind(eq, f_kind);
ivector_push(&eq->children_list, c);
children_size ++;
final_pair_type = EQ_NODE_PAIR;
}
}
// Add the real children
uint32_t i = 0;
for (i = 0; i < n; ++ i) {
eq_node_id_t c = eq_graph_add_term(eq, c_terms[i]);
ivector_push(&eq->children_list, c);
children_size ++;
}
ivector_push(&eq->children_list, eq_node_null);
const eq_node_id_t* c_nodes = (const eq_node_id_t*) eq->children_list.data + children_start;
// Add the pairs for children
assert(children_size >= 2);
i = children_size - 1;
eq_node_id_t p2 = c_nodes[i];
for (-- i; i > 0; -- i) {
eq_node_id_t p1 = c_nodes[i];
// Add the graph node (p1, p2) with children if root
p2 = eq_graph_add_pair(eq, EQ_NODE_PAIR, p1, p2, 0, 0);
// Store in the hash table
eq_graph_update_pair_hash(eq, p2);
}
// Add the final one for the whole function (NOTE!!! if already there, it will pop children NOTE!!!)
p2 = eq_graph_add_pair(eq, final_pair_type, c_nodes[0], p2, children_size, children_start);
// Store in the hash table
eq_graph_update_pair_hash(eq, p2);
// Add the equality f = p2
merge_queue_push_init(&eq->merge_queue, f_id, p2, REASON_IS_FUNCTION_DEF, 0);
// We added lots of stuff, maybe there were merges
eq_graph_propagate(eq);
return f_id;
}
eq_node_id_t eq_graph_add_ufun_term(eq_graph_t* eq, term_t t, term_t f, uint32_t n, const term_t* children) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "eq_graph_ufun_term[%s](): ", eq->name);
ctx_trace_term(eq->ctx, t);
}
return eq_graph_add_fun_term(eq, t, f, UNINTERPRETED_TERM, n, children);
}
eq_node_id_t eq_graph_add_ifun_term(eq_graph_t* eq, term_t t, term_kind_t f, uint32_t n, const term_t* children) {
if (ctx_trace_enabled(eq->ctx, "mcsat::eq")) {
ctx_trace_printf(eq->ctx, "eq_graph_ifun_term[%s](): ", eq->name);
ctx_trace_term(eq->ctx, t);
}
return eq_graph_add_fun_term(eq, t, NULL_TERM, f, n, children);
}
eq_node_id_t eq_graph_term_id(const eq_graph_t* eq, term_t t) {
int_hmap_pair_t* find = int_hmap_find((int_hmap_t*) &eq->term_to_id, t);
assert(find != NULL);
return find->val;
}
eq_node_id_t eq_graph_term_id_if_exists(const eq_graph_t* eq, term_t t) {
int_hmap_pair_t* find = int_hmap_find((int_hmap_t*) &eq->term_to_id, t);
if (find != NULL) {
return find->val;
} else {
return eq_node_null;
}
}
bool eq_graph_term_is_rep(const eq_graph_t* eq, term_t t) {
eq_node_id_t id = eq_graph_term_id(eq, t);
const eq_node_t* n = eq_graph_get_node_const(eq, id);
return n->find == id;
}
eq_node_id_t eq_graph_value_id(const eq_graph_t* eq, const mcsat_value_t* v) {
value_hmap_pair_t* find = value_hmap_find(&eq->value_to_id, v);
assert(find != NULL);
return find->val;
}
bool eq_graph_has_term(const eq_graph_t* eq, term_t t) {
return int_hmap_find((int_hmap_t*) &eq->term_to_id, t) != NULL;
}
bool eq_graph_has_value(const eq_graph_t* eq, const mcsat_value_t* v) {
return value_hmap_find(&eq->value_to_id, v) != NULL;
}
bool eq_graph_are_equal(const eq_graph_t* eq, term_t t1, term_t t2){
assert(eq_graph_has_term(eq, t1));
assert(eq_graph_has_term(eq, t2));
eq_node_id_t t_id1 = eq_graph_term_id(eq, t1);
eq_node_id_t t_id2 = eq_graph_term_id(eq, t2);
const eq_node_t* n1 = eq_graph_get_node_const(eq, t_id1);
const eq_node_t* n2 = eq_graph_get_node_const(eq, t_id2);
return (n1->find == n2->find);
}
bool eq_graph_term_has_value(const eq_graph_t* eq, term_t t){
assert(eq_graph_has_term(eq, t));
eq_node_id_t t_id = eq_graph_term_id(eq, t);
const eq_node_t* n = eq_graph_get_node_const(eq, t_id);
eq_node_id_t n_find_id = n->find;
const eq_node_t* n_find = eq_graph_get_node_const(eq, n_find_id);
return(n_find->type == EQ_NODE_VALUE);
}
void eq_graph_print_node(const eq_graph_t* eq, const eq_node_t* n, FILE* out, bool print_extra) {
eq_node_id_t n_id = eq_graph_get_node_id(eq, n);
switch (n->type) {
case EQ_NODE_KIND: {
term_kind_t kind = eq->kind_list.data[n->index];
fprintf(out, "%s", kind_to_string(kind));
if (print_extra) {
fprintf(out, "(id=%"PRIu32", idx=%"PRIu32")", n_id, n->index);
}
break;
}
case EQ_NODE_TERM: {
term_t t = eq->terms_list.data[n->index];
term_print_to_file(out, eq->ctx->terms, t);
if (print_extra) {
fprintf(out, " (id=%"PRIu32", idx=%"PRIu32")", n_id, n->index);
}
break;
}
case EQ_NODE_VALUE: {
const mcsat_value_t* v = eq->values_list.data + n->index;
mcsat_value_print(v, out);
if (print_extra) {
fprintf(out, " (id=%"PRIu32", idx=%"PRIu32")", n_id, n->index);
}
break;
}
case EQ_NODE_EQ_PAIR:
fprintf(out, "[= ");
eq_node_id_t p1 = eq->pair_list.data[n->index];
eq_graph_print_node(eq, eq_graph_get_node_const(eq, p1), out, false);
fprintf(out, " ");
eq_node_id_t p2 = eq->pair_list.data[n->index + 1];
eq_graph_print_node(eq, eq_graph_get_node_const(eq, p2), out, false);
fprintf(out, "]");
if (print_extra) {
fprintf(out, " (id=%"PRIu32", idx=%"PRIu32")", n_id, n->index);
}
break;
case EQ_NODE_PAIR: {
fprintf(out, "[");
eq_node_id_t p1 = eq->pair_list.data[n->index];
eq_graph_print_node(eq, eq_graph_get_node_const(eq, p1), out, false);
fprintf(out, ", ");
eq_node_id_t p2 = eq->pair_list.data[n->index + 1];
eq_graph_print_node(eq, eq_graph_get_node_const(eq, p2), out, false);
fprintf(out, "]");
if (print_extra) {
fprintf(out, " (id=%"PRIu32", idx=%"PRIu32")", n_id, n->index);
}
break;
}
}
if (print_extra) {
const eq_node_id_t* children = eq_graph_get_children(eq, n_id);
if (children != NULL) {
fprintf(out, " {");
const eq_node_id_t* it = children;
for (; *it != eq_node_null; ++it) {
if (it != children) {
fprintf(out, ", ");
}
const eq_node_t* n = eq_graph_get_node_const(eq, *it);
eq_graph_print_node(eq, n, out, false);
}
fprintf(out, "}");
}
}
}
void eq_graph_print_class(const eq_graph_t* eq, eq_node_id_t start_node_id, FILE* out) {
const eq_node_t* n = eq_graph_get_node_const(eq, start_node_id);
// eq_node_id_t n_id = start_node_id; // BD: dead store reported by infer
eq_node_id_t n_id;
bool first = true;
do {
if (!first) { fprintf(out, ", "); }
eq_graph_print_node(eq, n, out, true);
n = eq->nodes + n->next;
n_id = eq_graph_get_node_id(eq, n);
first = false;
} while (n_id != start_node_id);
}
void eq_graph_print(const eq_graph_t* eq, FILE* out) {
uint32_t i;
fprintf(out, "eq_graph[%s]:\n", eq->name);
fprintf(out, "nodes:\n");
for (i = 0; i < eq->nodes_size; ++ i) {
const eq_node_t* n = eq_graph_get_node_const(eq, i);
// Only print representatives
if (n->find == i) {
fprintf(out, " ");
eq_graph_print_node(eq, n, out, true);
fprintf(out, ": ");
eq_graph_print_class(eq, n->find, out);
fprintf(out, "\n");
}
}
}
void eq_graph_to_gv_init(const eq_graph_t* eq, const char* filename) {
assert(eq->graph_out == NULL);
// Open the file
eq_graph_t* eq_nonconst = (eq_graph_t *) eq;
eq_nonconst->graph_out = fopen(filename, "w");
// Header
fprintf(eq->graph_out, "graph G1 {\n\n");
fprintf(eq->graph_out, " node [shape=record, style=filled];\n\n");
// All the nodes
uint32_t i;
for (i = 0; i < eq->nodes_size; ++ i) {
const eq_node_t* n = eq_graph_get_node_const(eq, i);
fprintf(eq->graph_out, " n%"PRIu32" [label=\"", i);
eq_graph_print_node(eq, n, eq->graph_out, false);
fprintf(eq->graph_out, "\"];\n");
}
// All the edges (they come in pairs)
fprintf(eq->graph_out, "\n");
for (i = 0; i < eq->edges_size; i += 2) {
const eq_edge_t* e = eq->edges + i;
fprintf(eq->graph_out, " n%"PRIu32" -- n%"PRIu32" [label=\"%s\"]\n", e->u, e->v, eq_graph_reason_to_short_string(e->reason.type));
}
}
void eq_graph_to_gv_edge(const eq_graph_t* eq, const eq_edge_t* e, uint32_t id) {
if (eq->graph_out != NULL) {
fprintf(eq->graph_out, " n%"PRIu32" -- n%"PRIu32" [label=\"%d\"]\n", e->u, e->v, id);
}
}
void eq_graph_to_gv_mark_node(const eq_graph_t* eq, eq_node_id_t n_id) {
if (eq->graph_out != NULL) {
fprintf(eq->graph_out, "\n n%"PRIu32" [color=red, fillcolor=lightgray];\n", n_id);
}
}
void eq_graph_to_gv_done(const eq_graph_t* eq) {
assert(eq->graph_out != NULL);
// Footer
fprintf(eq->graph_out, "}\n");
// Close the file
eq_graph_t* eq_nonconst = (eq_graph_t *) eq;
fclose(eq_nonconst->graph_out);
eq_nonconst->graph_out = NULL;
}
static
void eq_graph_update_find(eq_graph_t* eq, eq_node_id_t n_id, eq_node_id_t find) {
// Update the find in n_id's class
eq_node_t* it = eq_graph_get_node(eq, n_id);
assert(it->find != find);
do {
assert(it->type != EQ_NODE_VALUE);
it->find = find;
it = eq_graph_get_node(eq, it->next);
} while (it->find != find);
}
/** Merge node n2 into n1 */
static
void eq_graph_merge_nodes(eq_graph_t* eq, eq_node_id_t n_into_id, eq_node_id_t n_from_id) {
eq_node_t* n_into = eq_graph_get_node(eq, n_into_id);
eq_node_t* n_from = eq_graph_get_node(eq, n_from_id);
assert(n_into->type != EQ_NODE_VALUE || n_from->type != EQ_NODE_VALUE);
assert(n_into->find == n_into_id);
assert(n_from->find == n_into_id); // Nodes have been updated already
assert(n_into_id != n_from_id);
// Finally merge the lists (circular lists)
eq_node_id_t tmp = n_into->next;
n_into->next = n_from->next;
n_from->next = tmp;
// Update the size
n_into->size += n_from->size;
}
/** Un-merge node n2 from n1 */
static
void eq_graph_unmerge_nodes(eq_graph_t* eq, eq_node_id_t n_into_id, eq_node_id_t n_from_id) {
eq_node_t* n_into = eq_graph_get_node(eq, n_into_id);
eq_node_t* n_from = eq_graph_get_node(eq, n_from_id);
assert(n_into->find == n_into_id);
assert(n_from->find == n_into_id);
assert(n_into_id != n_from_id);
// Update the size