Source file bce.ml
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
(** {1 Blocked Clause Elimination} *)
open Logtk
open Libzipperposition
let k_enabled = Flex_state.create_key ()
let k_check_at = Flex_state.create_key ()
let k_max_symbol_occ = Flex_state.create_key ()
let k_processing_kind = Flex_state.create_key ()
let k_fp_mode = Flex_state.create_key ()
let section = Util.Section.make ~parent:Const.section "bce"
let _enabled = ref false
let _processing_kind = ref `PreprocessingOnly
let _check_at = ref 10
let _max_symbol_occ = ref (-1)
module Avatar = Libzipperposition_avatar
module type S = sig
module Env : Env.S
(** {6 Registration} *)
val setup : ?in_fp_mode:bool -> unit -> unit
val begin_fixpoint : unit -> unit
val fixpoint_step : unit -> bool
val end_fixpoint : unit -> unit
end
module Make(E : Env.S) : S with module Env = E = struct
module Env = E
module C = Env.C
module L = Literal
module T = Term
module CC = Congruence.FO
module DEQ = CCDeque
module SymSignIdx = Map.Make (struct
type t = (ID.t * bool)
let compare = CCPair.compare ID.compare CCBool.compare
end)
let k_removed_active = Flex_state.create_key ()
let k_removed_passive = Flex_state.create_key ()
let k_bce_sat_tracked = Flex_state.create_key ()
type logic =
| NEqFO
| EqFO
| NonAppVarHo
| Unsupported
let log_to_int =
[(NEqFO, 0); (EqFO, 1); (NonAppVarHo, 2); (Unsupported, 3)]
let log_compare (l1:logic) (l2:logic) =
compare (List.assoc l1 log_to_int) (List.assoc l2 log_to_int)
exception UnsupportedLogic
type bce_check_task =
{
lit_idx : int;
clause : C.t;
cands : C.t CCDeque.t;
mutable heap_idx : int;
}
module TaskStore = Map.Make (struct
type t = int * C.t
let compare (idx_a, cl_a) (idx_b, cl_b) =
CCOrd.(<?>) (CCInt.compare (C.id cl_a) (C.id cl_b)) (compare, idx_a, idx_b)
end)
module TaskWrapper = struct
type t = bce_check_task
let idx task = task.heap_idx
let set_idx task idx =
task.heap_idx <- idx
let lt a b =
(DEQ.length a.cands < DEQ.length b.cands)
|| (DEQ.length a.cands = DEQ.length b.cands
&& CCInt.compare (C.id a.clause) (C.id b.clause) < 0)
|| (DEQ.length a.cands = DEQ.length b.cands
&& CCInt.compare (C.id a.clause) (C.id b.clause) = 0
&& a.lit_idx < b.lit_idx)
end
module TaskPriorityQueue = CCMutHeap.Make(TaskWrapper)
let init_heap_idx = -1
let ss_idx = ref SymSignIdx.empty
let clause_lock = ref Util.Int_map.empty
let task_store = ref TaskStore.empty
let task_queue = TaskPriorityQueue.create ()
let ignored_symbols = ref ID.Set.empty
let logic = ref NEqFO
let refine_logic new_val =
if log_compare new_val !logic > 0 then (
logic := new_val;
if (new_val == NonAppVarHo) then (
Env.Ctx.lost_completeness ()
);
)
let lit_to_term sign =
if sign then CCFun.id else T.Form.not_
let task_eq a b = a.lit_idx = b.lit_idx && C.equal a.clause b.clause
let symbol_occurrs_too_often sym_count =
Env.flex_get k_max_symbol_occ > 0 &&
(sym_count > Env.flex_get k_max_symbol_occ)
let add_lit_to_idx lit_lhs sign cl =
let sym = T.head_exn lit_lhs in
let sym_occs sym sign =
CCOpt.map_or ~default:0 C.ClauseSet.cardinal
(SymSignIdx.find_opt (sym,sign) !ss_idx)
in
let total_sym_occs = sym_occs sym true + sym_occs sym false + 1 in
if symbol_occurrs_too_often total_sym_occs then (
ss_idx := SymSignIdx.remove (sym, false) (SymSignIdx.remove (sym, true) !ss_idx);
ignored_symbols := ID.Set.add sym !ignored_symbols;
Util.debugf ~section 5 "ignoring symbol @[%a@]@." (fun k -> k ID.pp sym);
) else (
ss_idx := SymSignIdx.update (sym, sign) (fun old ->
Some (C.ClauseSet.add cl (CCOpt.get_or ~default:C.ClauseSet.empty old))
) !ss_idx;
)
let find_candindates lhs sign =
let hd = T.head_exn lhs in
C.ClauseSet.to_list
(CCOpt.get_or
~default:C.ClauseSet.empty
(SymSignIdx.find_opt (hd, not sign) !ss_idx))
let scan_cl_lits cl =
CCArray.iter (function
| L.Equation(lhs,rhs,_) as lit ->
let sign = L.is_positivoid lit in
let is_poly =
not (Type.VarSet.is_empty (T.ty_vars lhs))
|| not (Type.VarSet.is_empty (T.ty_vars rhs))
in
if not is_poly && not (Type.is_fun (T.ty lhs)) then (
if Type.is_prop (T.ty lhs) then (
if L.is_predicate_lit lit && CCOpt.is_some (T.head lhs) then (
if not (T.is_fo_term lhs) then (
refine_logic NonAppVarHo
);
let hd_sym = T.head_exn lhs in
if not (ID.Set.mem hd_sym !ignored_symbols)
then add_lit_to_idx lhs sign cl
) else (
Util.debugf ~section 1 "unsupported because of formula @[%a@]@." (fun k -> k L.pp lit);
logic := Unsupported;
raise UnsupportedLogic;
)
) else (
if T.is_fo_term lhs && T.is_fo_term rhs then refine_logic EqFO
else refine_logic NonAppVarHo)
) else (
logic := Unsupported;
Util.debugf ~section 1 "unsupported because of functional literal @[%a@]@." (fun k -> k L.pp lit);
raise UnsupportedLogic)
| _ -> ()
) (C.lits cl)
let add_candidates lit_idx cl cand_cls =
let t = TaskStore.find (lit_idx, cl) !task_store in
DEQ.add_iter_back t.cands (CCList.to_iter cand_cls);
if TaskPriorityQueue.in_heap t then (
TaskPriorityQueue.increase task_queue t;
)
let register_task ?(update_others=true) lit_idx clause =
let update_cand_lists hd sign clause cands =
List.iter (fun cand ->
if not (C.equal cand clause) then (
CCArray.iteri (fun lit_idx lit ->
match lit with
| L.Equation(lhs,_,_)
when L.is_predicate_lit lit &&
ID.equal (T.head_exn lhs) hd &&
sign != L.is_positivoid lit ->
add_candidates lit_idx cand [clause]
| _ -> ()
) (C.lits cand))
) cands;
in
match (C.lits clause).(lit_idx) with
| L.Equation (lhs, rhs, _) as lit
when L.is_predicate_lit lit
&& not (ID.Set.mem (T.head_exn lhs) !ignored_symbols) ->
let hd = T.head_exn lhs in
let sign = L.is_positivoid lit in
let cands = find_candindates lhs sign in
if update_others then (
update_cand_lists hd sign clause cands
);
let task = {lit_idx; clause; cands=DEQ.of_list cands;
heap_idx = init_heap_idx} in
task_store := TaskStore.add (lit_idx, clause) task !task_store;
TaskPriorityQueue.insert task_queue task
| _ -> ( )
let add_clause cl =
try
if !logic == Unsupported then raise UnsupportedLogic;
scan_cl_lits cl;
CCArray.iteri (fun lit_idx _ -> register_task lit_idx cl) (C.lits cl)
with UnsupportedLogic ->
refine_logic Unsupported;
TaskPriorityQueue.clear task_queue
let deregister_symbols cl =
CCArray.iteri (fun _ lit ->
match lit with
| L.Equation(lhs,_,_)
when L.is_predicate_lit lit ->
ss_idx :=
SymSignIdx.update (T.head_exn lhs, L.is_positivoid lit) (function
| Some old ->
let new_ = C.ClauseSet.remove cl old in
CCOpt.return_if (not (C.ClauseSet.is_empty new_)) new_
| None -> None ) !ss_idx;
| _ -> ()
) (C.lits cl)
let lock_clause locker locked_task =
assert(C.id locker == C.id (DEQ.peek_front locked_task.cands));
clause_lock := Util.Int_map.update (C.id locker) (fun old_val ->
let locked_tasks = CCOpt.get_or ~default:[] old_val in
Some (locked_task :: locked_tasks)
) !clause_lock
let release_locks clause =
Util.debugf ~section 3 "clearing locks: @[%a@]@." (fun k -> k C.pp clause);
try
List.iter (fun task ->
assert (not (TaskPriorityQueue.in_heap task));
assert (not (DEQ.is_empty task.cands));
let locking_cl = DEQ.take_front task.cands in
assert (C.id locking_cl = C.id clause);
Util.debugf ~section 3 " |@[%a@]|%d@." (fun k -> k C.pp task.clause task.lit_idx);
TaskPriorityQueue.insert task_queue task
) (Util.Int_map.find (C.id clause) !clause_lock);
clause_lock := Util.Int_map.remove (C.id clause) !clause_lock
with Not_found ->
()
let deregister_clause clause =
deregister_symbols clause;
release_locks clause
let remove_from_proof_state clause =
begin
try
if Env.is_active clause then (
C.Tbl.add (Env.flex_get k_removed_active) clause ();
) else if Env.is_passive clause then (
C.Tbl.add (Env.flex_get k_removed_passive) clause ();
)
with _ ->
C.mark_redundant clause
end;
if Env.flex_get k_processing_kind != `InprocessingSat then (
C.mark_redundant clause
);
Env.remove_active (Iter.singleton clause);
Env.remove_passive (Iter.singleton clause);
Env.remove_simpl (Iter.singleton clause)
let resolvent_is_valid_neq lit_idx orig_cl partner =
assert (lit_idx < C.length orig_cl);
let sc_orig, sc_partner = 0, 1 in
let split_partner lhs sign partner =
CCArray.foldi (fun (unifiable, others) idx lit ->
match lit with
| L.Equation(lhs', _, _)
when L.is_predicate_lit lit
&& L.is_positivoid lit != sign ->
begin try
let subst = Unif.FO.unify_syn (lhs, sc_orig) (lhs', sc_partner) in
(lhs', subst) :: unifiable, others
with Unif.Fail -> unifiable, lit :: others end
| _ -> unifiable, lit :: others
) ([], []) (C.lits partner)
in
let check_resolvents l_idx orig_cl (unifiable, nonunifiable) =
let orig_sign = L.is_positivoid ((C.lits orig_cl).(l_idx)) in
let for_tautology_checking =
List.filter (fun (lit, _) ->
L.is_positivoid lit = orig_sign
&& L.is_predicate_lit lit)
( (List.map (fun x -> x,sc_orig) (CCArray.except_idx (C.lits orig_cl) l_idx)) @
List.map (fun x -> x, sc_partner) nonunifiable )
in
if CCList.is_empty unifiable then true
else (
let rec check_lit lhs subst rest =
let is_valid =
List.exists (fun lit' ->
CCOpt.is_some (CCArray.find_map_i (fun idx lit ->
if idx != l_idx &&
L.are_opposite_subst ~subst (lit, sc_orig) (lit', sc_partner) then(
Some lit)
else None)
(C.lits orig_cl))
) nonunifiable in
Util.debugf ~section 30
"check: @. lit: @[%a@]@. unif:@[%a@]@. non_unif @[%a@]@. partner_cl: @[%a@]@."
(fun k -> k L.pp ((C.lits orig_cl).(l_idx)) (CCList.pp T.pp)
(List.map fst unifiable)
(CCList.pp L.pp) nonunifiable C.pp partner );
is_valid ||
(not (CCList.is_empty rest) && (
let contrasting, rest' =
CCList.partition (fun (lhs,_) ->
CCOpt.is_some (
List.find_opt (fun (lit, sc) ->
let lhs' = CCOpt.get_exn (L.View.get_lhs lit) in
Unif.FO.equal ~subst (lhs',sc) (lhs, sc_partner)
)
(for_tautology_checking)))
rest
in
if CCList.is_empty contrasting then false
else (
try
let subst =
List.fold_left (fun subst (lhs',_) ->
Unif.FO.unify_syn ~subst (lhs, sc_partner) (lhs', sc_partner)
) (subst) contrasting
in
check_lit lhs subst rest'
with Unif.Fail ->
true)
))
in
let rec check_l_resolvents rest = function
| (lhs, subst) as x :: xs ->
check_lit lhs subst (rest @ xs) &&
check_l_resolvents (x :: rest) xs
| [] -> true
in
check_l_resolvents [] unifiable
)
in
let lit = (C.lits orig_cl).(lit_idx) in
let lhs, sign =
match lit with
| L.Equation(lhs, _, _) when L.is_predicate_lit lit ->
(lhs, L.is_positivoid lit)
| _ -> assert false
in
check_resolvents lit_idx orig_cl (split_partner lhs sign partner)
let resolvent_is_valid_eq lit_idx orig_cl partner =
assert (lit_idx < C.length orig_cl);
let sc_orig, sc_partner = 0, 1 in
let renaming = Subst.Renaming.create () in
let orig_cl = C.apply_subst ~renaming (orig_cl, sc_orig) Subst.empty in
let partner = C.apply_subst ~renaming (partner, sc_partner) Subst.empty in
let split_partner lhs sign partner =
CCArray.foldi (fun (same_hds, others) idx lit ->
match lit with
| L.Equation(lhs', _, _)
when L.is_predicate_lit lit
&& L.is_positivoid lit != sign
&& ID.equal (T.head_exn lhs) (T.head_exn lhs') ->
lhs' :: same_hds, others
| _ -> same_hds, lit :: others
) ([], []) (C.lits partner)
in
let lit = (C.lits orig_cl).(lit_idx) in
let orig_lhs, orig_sign =
match lit with
| L.Equation(lhs, _, _) when L.is_predicate_lit lit ->
(lhs, L.is_positivoid lit)
| _ -> assert false
in
let check_resolvents l_idx orig_cl (same_hd_lits, diff_hd_lits) =
let orig_args = T.args @@ CCOpt.get_exn @@ (L.View.get_lhs (C.lits orig_cl).(l_idx)) in
let add_flat_resolvent ~cc new_args =
Util.debugf ~section 10 " adding resolvent <%a>, <%a>@."
(fun k -> k (CCList.pp T.pp) orig_args (CCList.pp T.pp) new_args);
List.fold_left (fun acc (lhs,rhs) -> CC.mk_eq acc lhs rhs)
cc (List.combine orig_args new_args)
in
let orig_pos, orig_neg =
CCList.partition L.is_positivoid (CCArray.except_idx (C.lits orig_cl) l_idx) in
let partner_pos, partner_neg =
CCList.partition L.is_positivoid (diff_hd_lits)
in
let all_pos = orig_pos @ partner_pos in
let all_neg = orig_neg @ partner_neg in
let for_congruence_testing =
CCList.filter_map (fun lit ->
if L.is_predicate_lit lit && L.is_positivoid lit = orig_sign then (
CCOpt.flat_map (fun t ->
CCOpt.return_if
(ID.equal (T.head_exn t) (T.head_exn orig_lhs))
t)
(L.View.get_lhs lit)
) else None) (if orig_sign then all_pos else all_neg)
in
let orig_cc =
List.fold_left (fun acc lit ->
assert (L.is_negativoid lit);
let lhs,rhs,_ = CCOpt.get_exn @@ L.View.as_eqn lit in
CC.mk_eq acc lhs rhs
) (CC.create ~size:16 ()) all_neg
in
if CCList.is_empty same_hd_lits then true
else (
let rec check_lit ~cc rest =
let is_valid = List.exists (fun lit ->
assert(L.is_positivoid lit);
match lit with
| L.Equation (lhs, rhs, _) -> CC.is_eq cc lhs rhs
| L.True -> true
| _ -> false
) all_pos in
is_valid ||
(
not (CCList.is_empty rest) && (
let congruent, rest = List.partition (fun lhs ->
List.exists (fun lhs' -> CC.is_eq cc lhs lhs') for_congruence_testing
) rest
in
if CCList.is_empty congruent then false
else (
let cc =
List.fold_left (fun acc lhs -> add_flat_resolvent ~cc (T.args lhs))
cc congruent
in
check_lit ~cc rest
)))
in
let rec check_l_resolvents others = function
| x :: xs ->
let cc_with_lhs = add_flat_resolvent ~cc:orig_cc (T.args x) in
check_lit ~cc:cc_with_lhs (others @ xs) &&
check_l_resolvents (x :: others) xs
| [] -> true
in
check_l_resolvents [] same_hd_lits
)
in
check_resolvents lit_idx orig_cl (split_partner orig_lhs orig_sign partner)
let get_validity_checker () =
assert (!logic != Unsupported);
if !logic != NEqFO then resolvent_is_valid_eq
else resolvent_is_valid_neq
let is_blocked cl =
let validity_checker = get_validity_checker () in
let blocked_lit_idx =
CCArray.find_map_i (fun idx lit ->
match lit with
| L.Equation(lhs,_,_) when L.is_predicate_lit lit ->
let sym = T.head_exn lhs in
(match SymSignIdx.find_opt (sym, not (L.is_positivoid lit)) !ss_idx with
| Some partners ->
if (C.ClauseSet.for_all (fun partner ->
C.equal cl partner || validity_checker idx cl partner
) partners)
then (Some idx)
else None
| None ->
if not (ID.Set.mem sym !ignored_symbols)
then Some idx
else None)
| _ -> None
) (C.lits cl)
in
let ans = CCOpt.is_some blocked_lit_idx in
if ans then (
Util.debugf ~section 3 "@[%a@] is blocked on @[%a@] @."
(fun k -> k C.pp cl L.pp (C.lits cl).(CCOpt.get_exn blocked_lit_idx));
Util.debugf ~section 3 "proof:@[%a@]@." (fun k -> k Proof.S.pp_tstp (C.proof cl));
);
ans
let do_eliminate_blocked_clauses () =
let removed_cnt = ref 0 in
let process_task task =
let cl = task.clause in
let lit_idx = task.lit_idx in
let hd_sym =
T.head_exn @@ CCOpt.get_exn @@ L.View.get_lhs (C.lits task.clause).(lit_idx)
in
let validity_checker = get_validity_checker () in
let rec task_is_blocked deq =
DEQ.is_empty deq || (
let partner = DEQ.take_front deq in
if C.equal cl partner || C.is_redundant partner || validity_checker lit_idx cl partner
then (
Util.debugf ~section 5 "valid-res(@[%a@], @[%a@](%b))@."
(fun k -> k C.pp cl C.pp partner (C.is_redundant partner));
task_is_blocked deq
) else (
Util.debugf ~section 5 "blocks(@[%a@], @[%a@])@."
(fun k -> k C.pp partner C.pp cl);
DEQ.push_front deq partner;
lock_clause partner task;
false))
in
if not (C.is_empty task.clause ||
C.is_redundant task.clause ||
ID.Set.mem hd_sym !ignored_symbols) then (
Util.debugf ~section 3 "checking blockedness" CCFun.id;
match task_is_blocked task.cands with
| true ->
Util.debugf ~section 2 "removed(%d): @[%a@]" (fun k -> k task.lit_idx C.pp cl);
deregister_clause cl;
remove_from_proof_state cl;
incr removed_cnt;
| false ->
assert (not (TaskPriorityQueue.in_heap task))
) else (
Util.debugf ~section 3 "ignoring %b %b %b"
(fun k -> k (C.is_empty task.clause)
(C.is_redundant task.clause)
(ID.Set.mem hd_sym !ignored_symbols) ););
in
let module Q = TaskPriorityQueue in
while not (Q.is_empty task_queue) do
process_task (Q.remove_min task_queue)
done;
Util.debugf ~section 2 "bce removed %d clauses@." (fun k -> k !removed_cnt);
!removed_cnt
let steps = ref 0
let eliminate_blocked_clauses () =
steps := (!steps + 1) mod (Env.flex_get k_check_at);
if !steps = 0 then (
let original_cls =
Iter.to_list (Iter.append (Env.get_active ()) (Env.get_passive ()))
in
let eliminated = do_eliminate_blocked_clauses () in
if eliminated != 0 then (
Util.debugf ~section 2 "original clause set:@.@[%a@]"
(fun k -> k (CCList.pp C.pp) original_cls);
);
)
let react_clause_addded cl =
add_clause cl
let react_clause_removed cl =
deregister_clause cl
let do_bce_sat () =
C.Tbl.clear (Env.flex_get k_removed_active);
C.Tbl.clear (Env.flex_get k_removed_passive);
Util.debugf ~section 1 "new BCE-SAT attempt" CCFun.id;
ignore @@ do_eliminate_blocked_clauses ();
if C.Tbl.length (Env.flex_get k_bce_sat_tracked) == 0 then (
CCFormat.printf "%% BCE inprocessing removed all clauses"
) else (
let removed_actives = C.Tbl.keys (Env.flex_get k_removed_active) in
let removed_passives = C.Tbl.keys (Env.flex_get k_removed_passive) in
Util.debugf ~section 1 "reinserting %d/%d clauses" (fun k ->
k (Iter.length removed_actives + Iter.length removed_passives)
(C.Tbl.length (Env.flex_get k_bce_sat_tracked)));
Env.add_active removed_actives;
Env.add_simpl removed_actives;
Env.add_passive removed_passives;
)
let eliminate_bce_sat () =
steps := (!steps + 1) mod (Env.flex_get k_check_at);
if !steps = 0 then do_bce_sat ()
let initialize_regular () =
let init_clauses =
C.ClauseSet.to_list (Env.ProofState.ActiveSet.clauses ())
@ C.ClauseSet.to_list (Env.ProofState.PassiveSet.clauses ())
in
begin try
Util.debugf ~section 3 "init_cl: @[%a@]@."
(fun k -> k (CCList.pp C.pp) init_clauses);
let init_clause_num = List.length init_clauses in
CCFormat.printf "%% BCE start: %d@." init_clause_num;
List.iter scan_cl_lits init_clauses;
Util.debugf ~section 1 "logic has%sequalities"
(fun k -> k (if !logic == EqFO then " " else " no "));
List.iter
(fun cl ->
CCArray.iteri (fun lit_idx _ ->
register_task ~update_others:false lit_idx cl)
(C.lits cl))
init_clauses;
ignore (do_eliminate_blocked_clauses ());
let clause_diff =
init_clause_num -
(Iter.length (Env.get_active ()) + Iter.length (Env.get_passive ())) in
CCFormat.printf "%% BCE eliminated: %d@." clause_diff;
if Env.flex_get k_processing_kind != `PreprocessingOnly || Env.flex_get k_fp_mode then (
if Env.flex_get k_processing_kind == `InprocessingFull then (
Env.Ctx.lost_completeness ()
);
if Env.flex_get k_processing_kind == `InprocessingSat then (
Env.flex_add k_removed_active (C.Tbl.create 256);
Env.flex_add k_removed_passive (C.Tbl.create 256);
Env.flex_add k_bce_sat_tracked (C.Tbl.create 256);
let add_cl_sat cl =
C.Tbl.add (Env.flex_get k_bce_sat_tracked) cl ();
react_clause_addded cl
in
let remove_cl_sat cl =
C.Tbl.remove (Env.flex_get k_bce_sat_tracked) cl;
react_clause_removed cl
in
Env.ProofState.PassiveSet.clauses ()
|> C.ClauseSet.to_iter
|> Iter.iter (fun cl -> C.Tbl.add (Env.flex_get k_bce_sat_tracked) cl ());
Signal.on_every Env.ProofState.PassiveSet.on_add_clause (fun cl ->
if C.proof_depth cl = 0 then add_cl_sat cl
);
Signal.on_every Env.ProofState.PassiveSet.on_remove_clause remove_cl_sat;
Signal.on_every Env.on_forward_simplified (fun (_,state) ->
CCOpt.iter (add_cl_sat) state);
Signal.on_every Env.ProofState.ActiveSet.on_remove_clause remove_cl_sat;
) else (
Signal.on_every Env.ProofState.PassiveSet.on_add_clause react_clause_addded;
Signal.on_every Env.ProofState.ActiveSet.on_remove_clause react_clause_removed;
Signal.on_every Env.on_forward_simplified (fun (c, new_state) ->
match new_state with
| Some c' ->
if not (C.equal c c') then (
react_clause_removed c;
react_clause_addded c'
)
| _ -> react_clause_removed c; );
);
if not @@ Env.flex_get k_fp_mode then (
if Env.flex_get k_processing_kind = `InprocessingFull then(
Env.add_clause_elimination_rule ~priority:1 "BCE" eliminate_blocked_clauses
)else (Env.add_clause_elimination_rule ~priority:1 "BCE_SAT" eliminate_bce_sat)
)
) else ( raise UnsupportedLogic )
with UnsupportedLogic ->
Util.debugf ~section 1 "logic is unsupported" CCFun.id;
ss_idx := SymSignIdx.empty;
clause_lock := Util.Int_map.empty;
task_store := TaskStore.empty;
TaskPriorityQueue.clear task_queue;
end;
Signal.StopListening
let fixpoint_active = ref false
let begin_fixpoint () =
E.flex_add k_max_symbol_occ !_max_symbol_occ;
let init_clauses =
C.ClauseSet.to_list (Env.ProofState.ActiveSet.clauses ())
@ C.ClauseSet.to_list (Env.ProofState.PassiveSet.clauses ())
in
begin try
fixpoint_active := true;
List.iter scan_cl_lits init_clauses;
List.iter
(fun cl ->
CCArray.iteri (fun lit_idx _ ->
register_task ~update_others:false lit_idx cl)
(C.lits cl))
init_clauses;
let num_eliminated = do_eliminate_blocked_clauses () in
Util.debugf ~section 2"Step eliminates %d clauses" (fun k -> k num_eliminated);
CCFormat.printf "%% BCE start fixpoint: @[%d@]@." num_eliminated;
Signal.on Env.ProofState.PassiveSet.on_add_clause (fun c ->
if !fixpoint_active then (react_clause_addded c; Signal.ContinueListening)
else Signal.StopListening
);
Signal.on Env.ProofState.PassiveSet.on_remove_clause (fun c ->
if !fixpoint_active then (react_clause_removed c; Signal.ContinueListening)
else Signal.StopListening
);
with UnsupportedLogic ->
Util.debugf ~section 2 "logic is unsupported" CCFun.id;
ss_idx := SymSignIdx.empty;
clause_lock := Util.Int_map.empty;
task_store := TaskStore.empty;
TaskPriorityQueue.clear task_queue;
fixpoint_active := false
end
let fixpoint_step () =
let num_eliminated = do_eliminate_blocked_clauses () in
Util.debugf ~section 1 "Step eliminates %d clauses" (fun k -> k num_eliminated);
if num_eliminated != 0 then (
CCFormat.printf "%% BCE fixpoint: %d@." num_eliminated
);
num_eliminated != 0
let end_fixpoint () =
ss_idx := SymSignIdx.empty;
clause_lock := Util.Int_map.empty;
task_store := TaskStore.empty;
TaskPriorityQueue.clear task_queue;
fixpoint_active := false
let register () =
Signal.on Env.on_start initialize_regular
let setup ?(in_fp_mode=false) () =
if Env.flex_get k_enabled then (
Env.flex_add k_fp_mode in_fp_mode;
if not (Env.flex_get Avatar.k_avatar_enabled) then (register ())
else (
CCFormat.printf "AVATAR is not yet compatible with BCE@."
)
)
end
let extension =
let action env =
let module E = (val env : Env.S) in
let module BCE = Make(E) in
E.flex_add k_enabled !_enabled;
E.flex_add k_max_symbol_occ !_max_symbol_occ;
E.flex_add k_check_at !_check_at;
E.flex_add k_processing_kind !_processing_kind;
BCE.setup ()
in
{ Extensions.default with Extensions.
name="bce";
prio = 40;
env_actions=[action];
}
let () =
Options.add_opts [
"--bce", Arg.Bool ((:=) _enabled), " scan clauses for AC definitions";
"--bce-processing-kind", Arg.Symbol (["preprocessing";"inprocessing-full";"inprocessing-sat"], (function
| "preprocessing" -> _processing_kind := `PreprocessingOnly
| "inprocessing-full" -> _processing_kind := `InprocessingFull
| "inprocessing-sat" -> _processing_kind := `InprocessingSat
| _ -> assert false)),
" scan clauses for AC definitions";
"--bce-check-every", Arg.Int ((:=) _check_at), " check BCE every n steps of saturation algorithm";
"--bce-max-symbol-occurences", Arg.Int ((:=) _max_symbol_occ), " limit a given symbol to n occurences only";
]