Repository navigation
Expand file tree
/
Copy pathSidekick_cc.ml
More file actions
1269 lines (1143 loc) · 43.5 KB
/
Copy pathSidekick_cc.ml
File metadata and controls
1269 lines (1143 loc) · 43.5 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
open Sidekick_core
module View = Sidekick_core.CC_view
type ('f, 't, 'ts) view = ('f, 't, 'ts) View.t =
| Bool of bool
| App_fun of 'f * 'ts
| App_ho of 't * 't
| If of 't * 't * 't
| Eq of 't * 't
| Not of 't
| Opaque of 't
(* do not enter *)
module type S = Sidekick_core.CC_S
module Make (A : CC_ARG) :
S
with module T = A.T
and module Lit = A.Lit
and type proof = A.proof
and type proof_step = A.proof_step
and module Actions = A.Actions = struct
module T = A.T
module Lit = A.Lit
module Actions = A.Actions
module P = Actions.P
type term = T.Term.t
type value = term
type term_store = T.Term.store
type lit = Lit.t
type fun_ = T.Fun.t
type proof = A.proof
type proof_step = A.proof_step
type actions = Actions.t
module Term = T.Term
module Fun = T.Fun
module Bits : sig
type t = private int
type field
type bitfield_gen
val empty : t
val equal : t -> t -> bool
val mk_field : bitfield_gen -> field
val mk_gen : unit -> bitfield_gen
val get : field -> t -> bool
val set : field -> bool -> t -> t
val merge : t -> t -> t
end = struct
type bitfield_gen = int ref
let max_width = Sys.word_size - 2
let mk_gen () = ref 0
type t = int
type field = int
let empty : t = 0
let mk_field (gen : bitfield_gen) : field =
let n = !gen in
if n > max_width then
Error.errorf "maximum number of CC bitfields reached";
incr gen;
1 lsl n
let[@inline] get field x = x land field <> 0
let[@inline] set field b x =
if b then
x lor field
else
x land lnot field
let merge = ( lor )
let equal : t -> t -> bool = CCEqual.poly
end
type node = {
n_term: term;
mutable n_sig0: signature option; (* initial signature *)
mutable n_bits: Bits.t; (* bitfield for various properties *)
mutable n_parents: node Bag.t; (* parent terms of this node *)
mutable n_root: node;
(* representative of congruence class (itself if a representative) *)
mutable n_next: node; (* pointer to next element of congruence class *)
mutable n_size: int; (* size of the class *)
mutable n_as_lit: lit option;
(* TODO: put into payload? and only in root? *)
mutable n_expl: explanation_forest_link;
(* the rooted forest for explanations *)
}
(** A node of the congruence closure.
An equivalence class is represented by its "root" element,
the representative. *)
and signature = (fun_, node, node list) view
and explanation_forest_link =
| FL_none
| FL_some of { next: node; expl: explanation }
(* atomic explanation in the congruence closure *)
and explanation =
| E_trivial (* by pure reduction, tautologically equal *)
| E_lit of lit (* because of this literal *)
| E_merge of node * node
| E_merge_t of term * term
| E_congruence of node * node (* caused by normal congruence *)
| E_and of explanation * explanation
| E_theory of
term * term * (term * term * explanation list) list * proof_step
| E_same_val of node * node
type repr = node
module N = struct
type t = node
let[@inline] equal (n1 : t) n2 = n1 == n2
let[@inline] hash n = Term.hash n.n_term
let[@inline] term n = n.n_term
let[@inline] pp out n = Term.pp out n.n_term
let[@inline] as_lit n = n.n_as_lit
let make (t : term) : t =
let rec n =
{
n_term = t;
n_sig0 = None;
n_bits = Bits.empty;
n_parents = Bag.empty;
n_as_lit = None;
(* TODO: provide a method to do it *)
n_root = n;
n_expl = FL_none;
n_next = n;
n_size = 1;
}
in
n
let[@inline] is_root (n : node) : bool = n.n_root == n
(* traverse the equivalence class of [n] *)
let iter_class_ (n : node) : node Iter.t =
fun yield ->
let rec aux u =
yield u;
if u.n_next != n then aux u.n_next
in
aux n
let[@inline] iter_class n =
assert (is_root n);
iter_class_ n
let[@inline] iter_parents (n : node) : node Iter.t =
assert (is_root n);
Bag.to_iter n.n_parents
type bitfield = Bits.field
let[@inline] get_field f t = Bits.get f t.n_bits
let[@inline] set_field f b t = t.n_bits <- Bits.set f b t.n_bits
end
(* non-recursive, inlinable function for [find] *)
let[@inline] find_ (n : node) : repr =
let n2 = n.n_root in
assert (N.is_root n2);
n2
let[@inline] same_class (n1 : node) (n2 : node) : bool =
N.equal (find_ n1) (find_ n2)
let[@inline] find _ n = find_ n
module Expl = struct
type t = explanation
let rec pp out (e : explanation) =
match e with
| E_trivial -> Fmt.string out "reduction"
| E_lit lit -> Lit.pp out lit
| E_congruence (n1, n2) ->
Fmt.fprintf out "(@[congruence@ %a@ %a@])" N.pp n1 N.pp n2
| E_merge (a, b) -> Fmt.fprintf out "(@[merge@ %a@ %a@])" N.pp a N.pp b
| E_merge_t (a, b) ->
Fmt.fprintf out "(@[<hv>merge@ @[:n1 %a@]@ @[:n2 %a@]@])" Term.pp a
Term.pp b
| E_theory (t, u, es, _) ->
Fmt.fprintf out "(@[th@ :t `%a`@ :u `%a`@ :expl_sets %a@])" Term.pp t
Term.pp u
(Util.pp_list @@ Fmt.Dump.triple Term.pp Term.pp (Fmt.Dump.list pp))
es
| E_and (a, b) -> Format.fprintf out "(@[<hv1>and@ %a@ %a@])" pp a pp b
| E_same_val (n1, n2) ->
Fmt.fprintf out "(@[same-value@ %a@ %a@])" N.pp n1 N.pp n2
let mk_trivial : t = E_trivial
let[@inline] mk_congruence n1 n2 : t = E_congruence (n1, n2)
let[@inline] mk_merge a b : t =
if N.equal a b then
mk_trivial
else
E_merge (a, b)
let[@inline] mk_merge_t a b : t =
if Term.equal a b then
mk_trivial
else
E_merge_t (a, b)
let[@inline] mk_lit l : t = E_lit l
let[@inline] mk_theory t u es pr = E_theory (t, u, es, pr)
let[@inline] mk_same_value t u =
if N.equal t u then
mk_trivial
else
E_same_val (t, u)
let rec mk_list l =
match l with
| [] -> mk_trivial
| [ x ] -> x
| E_trivial :: tl -> mk_list tl
| x :: y ->
(match mk_list y with
| E_trivial -> x
| y' -> E_and (x, y'))
end
module Resolved_expl = struct
type t = {
lits: lit list;
same_value: (N.t * N.t) list;
pr: proof -> proof_step;
}
let[@inline] is_semantic (self : t) : bool =
match self.same_value with
| [] -> false
| _ :: _ -> true
let pp out (self : t) =
if not (is_semantic self) then
Fmt.fprintf out "(@[resolved-expl@ %a@])" (Util.pp_list Lit.pp)
self.lits
else (
let { lits; same_value; pr = _ } = self in
Fmt.fprintf out "(@[resolved-expl@ (@[%a@])@ :same-val (@[%a@])@])"
(Util.pp_list Lit.pp) lits
(Util.pp_list @@ Fmt.Dump.pair N.pp N.pp)
same_value
)
end
(** A signature is a shallow term shape where immediate subterms
are representative *)
module Signature = struct
type t = signature
let equal (s1 : t) s2 : bool =
match s1, s2 with
| Bool b1, Bool b2 -> b1 = b2
| App_fun (f1, []), App_fun (f2, []) -> Fun.equal f1 f2
| App_fun (f1, l1), App_fun (f2, l2) ->
Fun.equal f1 f2 && CCList.equal N.equal l1 l2
| App_ho (f1, a1), App_ho (f2, a2) -> N.equal f1 f2 && N.equal a1 a2
| Not a, Not b -> N.equal a b
| If (a1, b1, c1), If (a2, b2, c2) ->
N.equal a1 a2 && N.equal b1 b2 && N.equal c1 c2
| Eq (a1, b1), Eq (a2, b2) -> N.equal a1 a2 && N.equal b1 b2
| Opaque u1, Opaque u2 -> N.equal u1 u2
| Bool _, _
| App_fun _, _
| App_ho _, _
| If _, _
| Eq _, _
| Opaque _, _
| Not _, _ ->
false
let hash (s : t) : int =
let module H = CCHash in
match s with
| Bool b -> H.combine2 10 (H.bool b)
| App_fun (f, l) -> H.combine3 20 (Fun.hash f) (H.list N.hash l)
| App_ho (f, a) -> H.combine3 30 (N.hash f) (N.hash a)
| Eq (a, b) -> H.combine3 40 (N.hash a) (N.hash b)
| Opaque u -> H.combine2 50 (N.hash u)
| If (a, b, c) -> H.combine4 60 (N.hash a) (N.hash b) (N.hash c)
| Not u -> H.combine2 70 (N.hash u)
let pp out = function
| Bool b -> Fmt.bool out b
| App_fun (f, []) -> Fun.pp out f
| App_fun (f, l) ->
Fmt.fprintf out "(@[%a@ %a@])" Fun.pp f (Util.pp_list N.pp) l
| App_ho (f, a) -> Fmt.fprintf out "(@[%a@ %a@])" N.pp f N.pp a
| Opaque t -> N.pp out t
| Not u -> Fmt.fprintf out "(@[not@ %a@])" N.pp u
| Eq (a, b) -> Fmt.fprintf out "(@[=@ %a@ %a@])" N.pp a N.pp b
| If (a, b, c) ->
Fmt.fprintf out "(@[ite@ %a@ %a@ %a@])" N.pp a N.pp b N.pp c
end
module Sig_tbl = CCHashtbl.Make (Signature)
module T_tbl = CCHashtbl.Make (Term)
module T_b_tbl = Backtrackable_tbl.Make (Term)
type combine_task =
| CT_merge of node * node * explanation
| CT_set_val of node * value
type t = {
tst: term_store;
proof: proof;
tbl: node T_tbl.t; (* internalization [term -> node] *)
signatures_tbl: node Sig_tbl.t;
(* map a signature to the corresponding node in some equivalence class.
A signature is a [term_cell] in which every immediate subterm
that participates in the congruence/evaluation relation
is normalized (i.e. is its own representative).
The critical property is that all members of an equivalence class
that have the same "shape" (including head symbol)
have the same signature *)
pending: node Vec.t;
combine: combine_task Vec.t;
t_to_val: (node * value) T_b_tbl.t;
(* [repr -> (t,val)] where [repr = t] and [t := val] in the model *)
val_to_t: node T_b_tbl.t; (* [val -> t] where [t := val] in the model *)
undo: (unit -> unit) Backtrack_stack.t;
bitgen: Bits.bitfield_gen;
field_marked_explain: Bits.field;
(* used to mark traversed nodes when looking for a common ancestor *)
true_: node lazy_t;
false_: node lazy_t;
mutable model_mode: bool;
mutable on_pre_merge: ev_on_pre_merge list;
mutable on_post_merge: ev_on_post_merge list;
mutable on_new_term: ev_on_new_term list;
mutable on_conflict: ev_on_conflict list;
mutable on_propagate: ev_on_propagate list;
mutable on_is_subterm: ev_on_is_subterm list;
count_conflict: int Stat.counter;
count_props: int Stat.counter;
count_merge: int Stat.counter;
count_semantic_conflict: int Stat.counter;
}
(* TODO: an additional union-find to keep track, for each term,
of the terms they are known to be equal to, according
to the current explanation. That allows not to prove some equality
several times.
See "fast congruence closure and extensions", Nieuwenhuis&al, page 14 *)
and ev_on_pre_merge = t -> actions -> N.t -> N.t -> Expl.t -> unit
and ev_on_post_merge = t -> actions -> N.t -> N.t -> unit
and ev_on_new_term = t -> N.t -> term -> unit
and ev_on_conflict = t -> th:bool -> lit list -> unit
and ev_on_propagate = t -> lit -> (unit -> lit list * proof_step) -> unit
and ev_on_is_subterm = N.t -> term -> unit
let[@inline] size_ (r : repr) = r.n_size
let[@inline] n_true cc = Lazy.force cc.true_
let[@inline] n_false cc = Lazy.force cc.false_
let n_bool cc b =
if b then
n_true cc
else
n_false cc
let[@inline] term_store cc = cc.tst
let[@inline] proof cc = cc.proof
let allocate_bitfield ~descr cc =
Log.debugf 5 (fun k -> k "(@[cc.allocate-bit-field@ :descr %s@])" descr);
Bits.mk_field cc.bitgen
let[@inline] on_backtrack cc f : unit =
Backtrack_stack.push_if_nonzero_level cc.undo f
let[@inline] get_bitfield _cc field n = N.get_field field n
let set_bitfield cc field b n =
let old = N.get_field field n in
if old <> b then (
on_backtrack cc (fun () -> N.set_field field old n);
N.set_field field b n
)
(* check if [t] is in the congruence closure.
Invariant: [in_cc t ∧ do_cc t => forall u subterm t, in_cc u] *)
let[@inline] mem (cc : t) (t : term) : bool = T_tbl.mem cc.tbl t
(* print full state *)
let pp_full out (cc : t) : unit =
let pp_next out n = Fmt.fprintf out "@ :next %a" N.pp n.n_next in
let pp_root out n =
if N.is_root n then
Fmt.string out " :is-root"
else
Fmt.fprintf out "@ :root %a" N.pp n.n_root
in
let pp_expl out n =
match n.n_expl with
| FL_none -> ()
| FL_some e ->
Fmt.fprintf out " (@[:forest %a :expl %a@])" N.pp e.next Expl.pp e.expl
in
let pp_n out n =
Fmt.fprintf out "(@[%a%a%a%a@])" Term.pp n.n_term pp_root n pp_next n
pp_expl n
and pp_sig_e out (s, n) =
Fmt.fprintf out "(@[<1>%a@ ~~> %a%a@])" Signature.pp s N.pp n pp_root n
in
Fmt.fprintf out
"(@[@{<yellow>cc.state@}@ (@[<hv>:nodes@ %a@])@ (@[<hv>:sig-tbl@ %a@])@])"
(Util.pp_iter ~sep:" " pp_n)
(T_tbl.values cc.tbl)
(Util.pp_iter ~sep:" " pp_sig_e)
(Sig_tbl.to_iter cc.signatures_tbl)
(* compute up-to-date signature *)
let update_sig (s : signature) : Signature.t =
View.map_view s ~f_f:(fun x -> x) ~f_t:find_ ~f_ts:(List.map find_)
(* find whether the given (parent) term corresponds to some signature
in [signatures_] *)
let[@inline] find_signature cc (s : signature) : repr option =
Sig_tbl.get cc.signatures_tbl s
(* add to signature table. Assume it's not present already *)
let add_signature cc (s : signature) (n : node) : unit =
assert (not @@ Sig_tbl.mem cc.signatures_tbl s);
Log.debugf 50 (fun k ->
k "(@[cc.add-sig@ %a@ ~~> %a@])" Signature.pp s N.pp n);
on_backtrack cc (fun () -> Sig_tbl.remove cc.signatures_tbl s);
Sig_tbl.add cc.signatures_tbl s n
let push_pending cc t : unit =
Log.debugf 50 (fun k -> k "(@[<hv1>cc.push-pending@ %a@])" N.pp t);
Vec.push cc.pending t
let merge_classes cc t u e : unit =
if t != u && not (same_class t u) then (
Log.debugf 50 (fun k ->
k "(@[<hv1>cc.push-combine@ %a ~@ %a@ :expl %a@])" N.pp t N.pp u
Expl.pp e);
Vec.push cc.combine @@ CT_merge (t, u, e)
)
(* re-root the explanation tree of the equivalence class of [n]
so that it points to [n].
postcondition: [n.n_expl = None] *)
let[@unroll 2] rec reroot_expl (cc : t) (n : node) : unit =
match n.n_expl with
| FL_none -> () (* already root *)
| FL_some { next = u; expl = e_n_u } ->
(* reroot to [u], then invert link between [u] and [n] *)
reroot_expl cc u;
u.n_expl <- FL_some { next = n; expl = e_n_u };
n.n_expl <- FL_none
let raise_conflict_ (cc : t) ~th (acts : actions) (e : lit list)
(p : proof_step) : _ =
Profile.instant "cc.conflict";
(* clear tasks queue *)
Vec.clear cc.pending;
Vec.clear cc.combine;
List.iter (fun f -> f cc ~th e) cc.on_conflict;
Stat.incr cc.count_conflict;
Actions.raise_conflict acts e p
let[@inline] all_classes cc : repr Iter.t =
T_tbl.values cc.tbl |> Iter.filter N.is_root
(* find the closest common ancestor of [a] and [b] in the proof forest.
Precond:
- [a] and [b] are in the same class
- no node has the flag [field_marked_explain] on
Invariants:
- if [n] is marked, then all the predecessors of [n]
from [a] or [b] are marked too.
*)
let find_common_ancestor cc (a : node) (b : node) : node =
(* catch up to the other node *)
let rec find1 a =
if N.get_field cc.field_marked_explain a then
a
else (
match a.n_expl with
| FL_none -> assert false
| FL_some r -> find1 r.next
)
in
let rec find2 a b =
if N.equal a b then
a
else if N.get_field cc.field_marked_explain a then
a
else if N.get_field cc.field_marked_explain b then
b
else (
N.set_field cc.field_marked_explain true a;
N.set_field cc.field_marked_explain true b;
match a.n_expl, b.n_expl with
| FL_some r1, FL_some r2 -> find2 r1.next r2.next
| FL_some r, FL_none -> find1 r.next
| FL_none, FL_some r -> find1 r.next
| FL_none, FL_none -> assert false
(* no common ancestor *)
)
in
(* cleanup tags on nodes traversed in [find2] *)
let rec cleanup_ n =
if N.get_field cc.field_marked_explain n then (
N.set_field cc.field_marked_explain false n;
match n.n_expl with
| FL_none -> ()
| FL_some { next; _ } -> cleanup_ next
)
in
let n = find2 a b in
cleanup_ a;
cleanup_ b;
n
module Expl_state = struct
type t = {
mutable lits: Lit.t list;
mutable same_val: (N.t * N.t) list;
mutable th_lemmas: (Lit.t * (Lit.t * Lit.t list) list * proof_step) list;
}
let create () : t = { lits = []; same_val = []; th_lemmas = [] }
let[@inline] copy self : t = { self with lits = self.lits }
let[@inline] add_lit (self : t) lit = self.lits <- lit :: self.lits
let[@inline] add_th (self : t) lit hyps pr : unit =
self.th_lemmas <- (lit, hyps, pr) :: self.th_lemmas
let[@inline] add_same_val (self : t) n1 n2 : unit =
self.same_val <- (n1, n2) :: self.same_val
(** Does this explanation contain at least one merge caused by
"same value"? *)
let[@inline] is_semantic (self : t) : bool = self.same_val <> []
let merge self other =
let { lits = o_lits; th_lemmas = o_lemmas; same_val = o_same_val } =
other
in
self.lits <- List.rev_append o_lits self.lits;
self.th_lemmas <- List.rev_append o_lemmas self.th_lemmas;
self.same_val <- List.rev_append o_same_val self.same_val;
()
(* proof of [\/_i ¬lits[i]] *)
let proof_of_th_lemmas (self : t) (proof : proof) : proof_step =
let p_lits1 = Iter.of_list self.lits |> Iter.map Lit.neg in
let p_lits2 =
Iter.of_list self.th_lemmas
|> Iter.map (fun (lit_t_u, _, _) -> Lit.neg lit_t_u)
in
let p_cc = P.lemma_cc (Iter.append p_lits1 p_lits2) proof in
let resolve_with_th_proof pr (lit_t_u, sub_proofs, pr_th) =
(* pr_th: [sub_proofs |- t=u].
now resolve away [sub_proofs] to get literals that were
asserted in the congruence closure *)
let pr_th =
List.fold_left
(fun pr_th (lit_i, hyps_i) ->
(* [hyps_i |- lit_i] *)
let lemma_i =
P.lemma_cc
Iter.(cons lit_i (of_list hyps_i |> map Lit.neg))
proof
in
(* resolve [lit_i] away. *)
P.proof_res ~pivot:(Lit.term lit_i) lemma_i pr_th proof)
pr_th sub_proofs
in
P.proof_res ~pivot:(Lit.term lit_t_u) pr_th pr proof
in
(* resolve with theory proofs responsible for some merges, if any. *)
List.fold_left resolve_with_th_proof p_cc self.th_lemmas
let to_resolved_expl (self : t) : Resolved_expl.t =
(* FIXME: package the th lemmas too *)
let { lits; same_val; th_lemmas = _ } = self in
let s2 = copy self in
let pr proof = proof_of_th_lemmas s2 proof in
{ Resolved_expl.lits; same_value = same_val; pr }
end
(* decompose explanation [e] into a list of literals added to [acc] *)
let rec explain_decompose_expl cc (st : Expl_state.t) (e : explanation) : unit
=
Log.debugf 5 (fun k -> k "(@[cc.decompose_expl@ %a@])" Expl.pp e);
match e with
| E_trivial -> ()
| E_congruence (n1, n2) ->
(match n1.n_sig0, n2.n_sig0 with
| Some (App_fun (f1, a1)), Some (App_fun (f2, a2)) ->
assert (Fun.equal f1 f2);
assert (List.length a1 = List.length a2);
List.iter2 (explain_equal_rec_ cc st) a1 a2
| Some (App_ho (f1, a1)), Some (App_ho (f2, a2)) ->
explain_equal_rec_ cc st f1 f2;
explain_equal_rec_ cc st a1 a2
| Some (If (a1, b1, c1)), Some (If (a2, b2, c2)) ->
explain_equal_rec_ cc st a1 a2;
explain_equal_rec_ cc st b1 b2;
explain_equal_rec_ cc st c1 c2
| _ -> assert false)
| E_lit lit -> Expl_state.add_lit st lit
| E_same_val (n1, n2) -> Expl_state.add_same_val st n1 n2
| E_theory (t, u, expl_sets, pr) ->
let sub_proofs =
List.map
(fun (t_i, u_i, expls_i) ->
let lit_i = A.mk_lit_eq cc.tst t_i u_i in
(* use a separate call to [explain_expls] for each set *)
let sub = explain_expls cc expls_i in
Expl_state.merge st sub;
lit_i, sub.lits)
expl_sets
in
let lit_t_u = A.mk_lit_eq cc.tst t u in
Expl_state.add_th st lit_t_u sub_proofs pr
| E_merge (a, b) -> explain_equal_rec_ cc st a b
| E_merge_t (a, b) ->
(* find nodes for [a] and [b] on the fly *)
(match T_tbl.find cc.tbl a, T_tbl.find cc.tbl b with
| a, b -> explain_equal_rec_ cc st a b
| exception Not_found ->
Error.errorf "expl: cannot find node(s) for %a, %a" Term.pp a Term.pp b)
| E_and (a, b) ->
explain_decompose_expl cc st a;
explain_decompose_expl cc st b
and explain_expls cc (es : explanation list) : Expl_state.t =
let st = Expl_state.create () in
List.iter (explain_decompose_expl cc st) es;
st
and explain_equal_rec_ (cc : t) (st : Expl_state.t) (a : node) (b : node) :
unit =
Log.debugf 5 (fun k ->
k "(@[cc.explain_loop.at@ %a@ =?= %a@])" N.pp a N.pp b);
assert (N.equal (find_ a) (find_ b));
let ancestor = find_common_ancestor cc a b in
explain_along_path cc st a ancestor;
explain_along_path cc st b ancestor
(* explain why [a = parent_a], where [a -> ... -> target] in the
proof forest *)
and explain_along_path cc (st : Expl_state.t) (a : node) (target : node) :
unit =
let rec aux n =
if n == target then
()
else (
match n.n_expl with
| FL_none -> assert false
| FL_some { next = next_n; expl } ->
explain_decompose_expl cc st expl;
(* now prove [next_n = target] *)
aux next_n
)
in
aux a
(* add a term *)
let[@inline] rec add_term_rec_ cc t : node =
try T_tbl.find cc.tbl t with Not_found -> add_new_term_ cc t
(* add [t] to [cc] when not present already *)
and add_new_term_ cc (t : term) : node =
assert (not @@ mem cc t);
Log.debugf 15 (fun k -> k "(@[cc.add-term@ %a@])" Term.pp t);
let n = N.make t in
(* register sub-terms, add [t] to their parent list, and return the
corresponding initial signature *)
let sig0 = compute_sig0 cc n in
n.n_sig0 <- sig0;
(* remove term when we backtrack *)
on_backtrack cc (fun () ->
Log.debugf 30 (fun k -> k "(@[cc.remove-term@ %a@])" Term.pp t);
T_tbl.remove cc.tbl t);
(* add term to the table *)
T_tbl.add cc.tbl t n;
if Option.is_some sig0 then
(* [n] might be merged with other equiv classes *)
push_pending cc n;
if not cc.model_mode then List.iter (fun f -> f cc n t) cc.on_new_term;
n
(* compute the initial signature of the given node *)
and compute_sig0 (self : t) (n : node) : Signature.t option =
(* add sub-term to [cc], and register [n] to its parents.
Note that we return the exact sub-term, to get proper
explanations, but we add to the sub-term's root's parent list. *)
let deref_sub (u : term) : node =
let sub = add_term_rec_ self u in
(* add [n] to [sub.root]'s parent list *)
(let sub_r = find_ sub in
let old_parents = sub_r.n_parents in
if Bag.is_empty old_parents && not self.model_mode then
(* first time it has parents: tell watchers that this is a subterm *)
List.iter (fun f -> f sub u) self.on_is_subterm;
on_backtrack self (fun () -> sub_r.n_parents <- old_parents);
sub_r.n_parents <- Bag.cons n sub_r.n_parents);
sub
in
let[@inline] return x = Some x in
match A.cc_view n.n_term with
| Bool _ | Opaque _ -> None
| Eq (a, b) ->
let a = deref_sub a in
let b = deref_sub b in
return @@ Eq (a, b)
| Not u -> return @@ Not (deref_sub u)
| App_fun (f, args) ->
let args = args |> Iter.map deref_sub |> Iter.to_list in
if args <> [] then
return @@ App_fun (f, args)
else
None
| App_ho (f, a) ->
let f = deref_sub f in
let a = deref_sub a in
return @@ App_ho (f, a)
| If (a, b, c) -> return @@ If (deref_sub a, deref_sub b, deref_sub c)
let[@inline] add_term cc t : node = add_term_rec_ cc t
let mem_term = mem
let set_as_lit cc (n : node) (lit : lit) : unit =
match n.n_as_lit with
| Some _ -> ()
| None ->
Log.debugf 15 (fun k -> k "(@[cc.set-as-lit@ %a@ %a@])" N.pp n Lit.pp lit);
on_backtrack cc (fun () -> n.n_as_lit <- None);
n.n_as_lit <- Some lit
(* is [n] true or false? *)
let n_is_bool_value (self : t) n : bool =
N.equal n (n_true self) || N.equal n (n_false self)
(* gather a pair [lits, pr], where [lits] is the set of
asserted literals needed in the explanation (which is useful for
the SAT solver), and [pr] is a proof, including sub-proofs for theory
merges. *)
let lits_and_proof_of_expl (self : t) (st : Expl_state.t) :
Lit.t list * proof_step =
let { Expl_state.lits; th_lemmas = _; same_val } = st in
assert (same_val = []);
let pr = Expl_state.proof_of_th_lemmas st self.proof in
lits, pr
(* main CC algo: add terms from [pending] to the signature table,
check for collisions *)
let rec update_tasks (cc : t) (acts : actions) : unit =
while not (Vec.is_empty cc.pending && Vec.is_empty cc.combine) do
while not @@ Vec.is_empty cc.pending do
task_pending_ cc (Vec.pop_exn cc.pending)
done;
while not @@ Vec.is_empty cc.combine do
task_combine_ cc acts (Vec.pop_exn cc.combine)
done
done
and task_pending_ cc (n : node) : unit =
(* check if some parent collided *)
match n.n_sig0 with
| None -> () (* no-op *)
| Some (Eq (a, b)) ->
(* if [a=b] is now true, merge [(a=b)] and [true] *)
if same_class a b then (
let expl = Expl.mk_merge a b in
Log.debugf 5 (fun k ->
k "(@[cc.pending.eq@ %a@ :r1 %a@ :r2 %a@])" N.pp n N.pp a N.pp b);
merge_classes cc n (n_true cc) expl
)
| Some (Not u) ->
(* [u = bool ==> not u = not bool] *)
let r_u = find_ u in
if N.equal r_u (n_true cc) then (
let expl = Expl.mk_merge u (n_true cc) in
merge_classes cc n (n_false cc) expl
) else if N.equal r_u (n_false cc) then (
let expl = Expl.mk_merge u (n_false cc) in
merge_classes cc n (n_true cc) expl
)
| Some s0 ->
(* update the signature by using [find] on each sub-node *)
let s = update_sig s0 in
(match find_signature cc s with
| None ->
(* add to the signature table [sig(n) --> n] *)
add_signature cc s n
| Some u when N.equal n u -> ()
| Some u ->
(* [t1] and [t2] must be applications of the same symbol to
arguments that are pairwise equal *)
assert (n != u);
let expl = Expl.mk_congruence n u in
merge_classes cc n u expl)
and[@inline] task_combine_ cc acts = function
| CT_merge (a, b, e_ab) -> task_merge_ cc acts a b e_ab
| CT_set_val (n, v) -> task_set_val_ cc acts n v
and task_set_val_ cc acts n v =
let repr_n = find_ n in
(* - if repr(n) has value [v], do nothing
- else if repr(n) has value [v'], semantic conflict
- else add [repr(n) -> (n,v)] to cc.t_to_val *)
(match T_b_tbl.get cc.t_to_val repr_n.n_term with
| Some (n', v') when not (Term.equal v v') ->
(* semantic conflict *)
let expl = [ Expl.mk_merge n n' ] in
let expl_st = explain_expls cc expl in
let lits = expl_st.lits in
let tuples =
List.rev_map (fun (t, u) -> true, t.n_term, u.n_term) expl_st.same_val
in
let tuples = (false, n.n_term, n'.n_term) :: tuples in
Log.debugf 5 (fun k ->
k
"(@[cc.semantic-conflict.set-val@ (@[set-val %a@ := %a@])@ \
(@[existing-val %a@ := %a@])@])"
N.pp n Term.pp v N.pp n' Term.pp v');
Stat.incr cc.count_semantic_conflict;
Actions.raise_semantic_conflict acts lits tuples
| Some _ -> ()
| None -> T_b_tbl.add cc.t_to_val repr_n.n_term (n, v));
(* now for the reverse map, look in self.val_to_t for [v].
- if present, push a merge command with Expl.mk_same_value
- if not, add [v -> n] *)
match T_b_tbl.get cc.val_to_t v with
| None -> T_b_tbl.add cc.val_to_t v n
| Some n' when not (same_class n n') ->
merge_classes cc n n' (Expl.mk_same_value n n')
| Some _ -> ()
(* main CC algo: merge equivalence classes in [st.combine].
@raise Exn_unsat if merge fails *)
and task_merge_ cc acts a b e_ab : unit =
let ra = find_ a in
let rb = find_ b in
if not @@ N.equal ra rb then (
assert (N.is_root ra);
assert (N.is_root rb);
Stat.incr cc.count_merge;
(* check we're not merging [true] and [false] *)
if
(N.equal ra (n_true cc) && N.equal rb (n_false cc))
|| (N.equal rb (n_true cc) && N.equal ra (n_false cc))
then (
Log.debugf 5 (fun k ->
k
"(@[<hv>cc.merge.true_false_conflict@ @[:r1 %a@ :t1 %a@]@ @[:r2 \
%a@ :t2 %a@]@ :e_ab %a@])"
N.pp ra N.pp a N.pp rb N.pp b Expl.pp e_ab);
let th = ref false in
(* TODO:
C1: P.true_neq_false
C2: lemma [lits |- true=false] (and resolve on theory proofs)
C3: r1 C1 C2
*)
let expl_st = Expl_state.create () in
explain_decompose_expl cc expl_st e_ab;
explain_equal_rec_ cc expl_st a ra;
explain_equal_rec_ cc expl_st b rb;
if Expl_state.is_semantic expl_st then (
(* conflict involving some semantic values *)
let lits = expl_st.lits in
let same_val =
expl_st.same_val
|> List.rev_map (fun (t, u) -> true, N.term t, N.term u)
in
assert (same_val <> []);
Stat.incr cc.count_semantic_conflict;
Actions.raise_semantic_conflict acts lits same_val
) else (
(* regular conflict *)
let lits, pr = lits_and_proof_of_expl cc expl_st in
raise_conflict_ cc ~th:!th acts (List.rev_map Lit.neg lits) pr
)
);
(* We will merge [r_from] into [r_into].
we try to ensure that [size ra <= size rb] in general, but always
keep values as representative *)
let r_from, r_into =
if n_is_bool_value cc ra then
rb, ra
else if n_is_bool_value cc rb then
ra, rb
else if size_ ra > size_ rb then
rb, ra
else
ra, rb
in
(* when merging terms with [true] or [false], possibly propagate them to SAT *)
let merge_bool r1 t1 r2 t2 =
if N.equal r1 (n_true cc) then
propagate_bools cc acts r2 t2 r1 t1 e_ab true
else if N.equal r1 (n_false cc) then
propagate_bools cc acts r2 t2 r1 t1 e_ab false
in
if not cc.model_mode then (
merge_bool ra a rb b;
merge_bool rb b ra a
);
(* perform [union r_from r_into] *)
Log.debugf 15 (fun k ->
k "(@[cc.merge@ :from %a@ :into %a@])" N.pp r_from N.pp r_into);
(* call [on_pre_merge] functions, and merge theory data items *)
if not cc.model_mode then (
(* explanation is [a=ra & e_ab & b=rb] *)
let expl =
Expl.mk_list [ e_ab; Expl.mk_merge a ra; Expl.mk_merge b rb ]
in
List.iter (fun f -> f cc acts r_into r_from expl) cc.on_pre_merge
);
((* parents might have a different signature, check for collisions *)
N.iter_parents r_from (fun parent -> push_pending cc parent);
(* for each node in [r_from]'s class, make it point to [r_into] *)
N.iter_class r_from (fun u ->
assert (u.n_root == r_from);
u.n_root <- r_into);
(* capture current state *)
let r_into_old_next = r_into.n_next in
let r_from_old_next = r_from.n_next in
let r_into_old_parents = r_into.n_parents in
let r_into_old_bits = r_into.n_bits in
(* swap [into.next] and [from.next], merging the classes *)
r_into.n_next <- r_from_old_next;
r_from.n_next <- r_into_old_next;
r_into.n_parents <- Bag.append r_into.n_parents r_from.n_parents;
r_into.n_size <- r_into.n_size + r_from.n_size;
r_into.n_bits <- Bits.merge r_into.n_bits r_from.n_bits;
(* on backtrack, unmerge classes and restore the pointers to [r_from] *)
on_backtrack cc (fun () ->
Log.debugf 30 (fun k ->
k "(@[cc.undo_merge@ :from %a@ :into %a@])" N.pp r_from N.pp
r_into);
r_into.n_bits <- r_into_old_bits;
r_into.n_next <- r_into_old_next;
r_from.n_next <- r_from_old_next;
r_into.n_parents <- r_into_old_parents;
(* NOTE: this must come after the restoration of [next] pointers,
otherwise we'd iterate on too big a class *)
N.iter_class_ r_from (fun u -> u.n_root <- r_from);
r_into.n_size <- r_into.n_size - r_from.n_size));
(* check for semantic values, update the one of [r_into]
if [r_from] has a value *)
(match T_b_tbl.get cc.t_to_val r_from.n_term with
| None -> ()
| Some (n_from, v_from) ->
(match T_b_tbl.get cc.t_to_val r_into.n_term with
| None -> T_b_tbl.add cc.t_to_val r_into.n_term (n_from, v_from)
| Some (n_into, v_into) when not (Term.equal v_from v_into) ->
(* semantic conflict, including [n_from != n_into] in model *)
let expl =
[ e_ab; Expl.mk_merge r_from n_from; Expl.mk_merge r_into n_into ]
in
let expl_st = explain_expls cc expl in
let lits = expl_st.lits in
let tuples =
List.rev_map
(fun (t, u) -> true, t.n_term, u.n_term)
expl_st.same_val
in
let tuples = (false, n_from.n_term, n_into.n_term) :: tuples in
Log.debugf 5 (fun k ->
k
"(@[cc.semantic-conflict.post-merge@ (@[n-from %a@ := %a@])@ \
(@[n-into %a@ := %a@])@])"
N.pp n_from Term.pp v_from N.pp n_into Term.pp v_into);