-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsatml.ml
2200 lines (1901 loc) · 74 KB
/
satml.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
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
(**************************************************************************)
(* *)
(* Alt-Ergo: The SMT Solver For Software Verification *)
(* Copyright (C) 2013-2023 --- OCamlPro SAS *)
(* *)
(* This file is distributed under the terms of OCamlPro *)
(* Non-Commercial Purpose License, version 1. *)
(* *)
(* As an exception, Alt-Ergo Club members at the Gold level can *)
(* use this file under the terms of the Apache Software License *)
(* version 2.0. *)
(* *)
(* --------------------------------------------------------------- *)
(* *)
(* The Alt-Ergo theorem prover *)
(* *)
(* Sylvain Conchon, Evelyne Contejean, Francois Bobot *)
(* Mohamed Iguernelala, Stephane Lescuyer, Alain Mebsout *)
(* *)
(* CNRS - INRIA - Universite Paris Sud *)
(* *)
(* Until 2013, some parts of this code were released under *)
(* the Apache Software License version 2.0. *)
(* *)
(* --------------------------------------------------------------- *)
(* *)
(* More details can be found in the directory licenses/ *)
(* *)
(**************************************************************************)
(** This module implements a CDCL solver, as well as some extensions based on a
combination of CDCL with ideas from Tableaux solver (the solver using
Tableaux extensions is called CDCL-Tableaux).
The CDCL part of the solver is heavily inspired by the MiniSat design:
http://www.decision-procedures.org/handouts/MiniSat.pdf
The Tableaux extensions to the CDCL solver mostly relate to the integration
of a lazy CNF conversion into the CDCL solver (part of this happens in the
[Satml_types] module). It is described in chapter 4 of Albin Coquereau's
PhD thesis (in French):
https://pastel.hal.science/tel-02504894
The lazy CNF is used in two ways:
- With the [get_cdcl_tableaux_inst ()] option, only atoms that are relevant
to the current state of the lazy CNF conversion are used for
instantiation. For instance, if the formula [a \/ (b /\ c)] has been
asserted, and we picked the left branch (i.e. [a]) in the formula, then
only the terms in [a] will be used for instantiation -- not the terms in
[b] or [c], even if they are decided to be [true] by the CDCL solver. Of
course, if [b] or [c] also appear in other formulas where they have been
"chosen", then they will be used for instantiation.contents.
In addition, there is an early stopping mechanism for satisfiable
problems: if all disjunctions have had one "side" successfully assigned
to [true], the solver will not try to assign truth values to the
remaining atoms ([b] and [c] in the example). This can lead to partial
boolean models.
- With the [get_cdcl_tableaux_th ()] option, atoms are only propagated to
the theories once they become relevant. For instance, in the above
scenario, only [a] (if it is a literal) will be propagated to the theory,
even if [b] or [c] are later asserted (again, unless they appear in other
formulas). *)
module E = Expr
module Atom = Satml_types.Atom
module FF = Satml_types.Flat_Formula
module Ex = Explanation
exception Sat
exception Unsat of Atom.clause list option
exception Last_UIP_reason of Atom.Set.t
exception Restart
exception Stopped
type conflict_origin =
| C_none
| C_bool of Atom.clause
| C_theory of Ex.t
(* not even the final one *)
let vraie_form = E.vrai
module type SAT_ML = sig
(*module Make (Dummy : sig end) : sig*)
type th
type t
val solve : t -> unit
val compute_concrete_model :
declared_ids:Id.typed list ->
t ->
Models.t Lazy.t * Objective.Model.t
val set_new_proxies : t -> FF.proxies -> unit
val new_vars :
t ->
nbv : int -> (* nb made vars *)
Atom.var list ->
Atom.atom list list -> Atom.atom list list ->
Atom.atom list list * Atom.atom list list
val assume :
t ->
Atom.atom list list -> Atom.atom list list -> E.t ->
cnumber : int ->
FF.Set.t -> dec_lvl:int ->
unit
val boolean_model : t -> Atom.atom list
val instantiation_context :
t -> FF.hcons_env -> Satml_types.Atom.Set.t
val current_tbox : t -> th
val set_current_tbox : t -> th -> unit
val create : Atom.hcons_env -> t
val assume_th_elt : t -> Expr.th_elt -> Explanation.t -> unit
val decision_level : t -> int
val cancel_until : t -> int -> unit
val exists_in_lazy_cnf : t -> FF.t -> bool
val known_lazy_formulas : t -> int FF.Map.t
val reason_of_deduction: Atom.atom -> Atom.Set.t
val assume_simple : t -> Atom.atom list list -> unit
val do_case_split : t -> Util.case_split_policy -> conflict_origin
val decide : t -> Atom.atom -> unit
val conflict_analyze_and_fix : t -> conflict_origin -> unit
val push : t -> Satml_types.Atom.atom -> unit
val pop : t -> unit
val optimize : t -> is_max:bool -> Expr.t -> unit
end
module MFF = FF.Map
module SFF = FF.Set
module Vheap = Heap.Make(struct
type t = Atom.var
let index (a : t) = a.hindex
let set_index (a : t) index = a.hindex <- index
(* Note: comparison is flipped because we want maximum weight first and
[Heap] is a min-heap. *)
let compare (a : t) (b : t) = Stdlib.compare b.weight a.weight
end)
let is_semantic (a : Atom.atom) =
match Shostak.Literal.view a.lit with
| LTerm _ -> false
| LSem _ -> true
module Make (Th : Theory.S) : SAT_ML with type th = Th.t = struct
module Matoms = Atom.Map
type th = Th.t
type t =
{
hcons_env : Atom.hcons_env;
(* si vrai, les contraintes sont deja fausses *)
mutable is_unsat : bool;
mutable unsat_core : Atom.clause list option;
(* clauses du probleme *)
mutable clauses : Atom.clause Vec.t;
(* clauses apprises *)
mutable learnts : Atom.clause Vec.t;
(* valeur de l'increment pour l'activite des clauses *)
mutable clause_inc : float;
(* valeur de l'increment pour l'activite des variables *)
mutable var_inc : float;
(* un vecteur des variables du probleme *)
mutable vars : Atom.var Vec.t;
(* la pile de decisions avec les faits impliques *)
mutable trail : Atom.atom Vec.t;
(* une pile qui pointe vers les niveaux de decision dans trail *)
mutable trail_lim : int Vec.t;
mutable nchoices : int ;
(** Number of semantic choices (case splits) that have been made. Semantic
literals are not counted as assignments but are still part of the
trail; we keep track of this number to properly compute the number of
assignments to boolean literals in [nb_assigns]. *)
mutable nchoices_stack : int Vec.t;
(** Stack for [nchoices] values. *)
(* Tete de la File des faits unitaires a propager.
C'est un index vers le trail *)
mutable qhead : int;
(* Nombre des assignements top-level depuis la derniere
execution de 'simplify()' *)
mutable simpDB_assigns : int;
(* Nombre restant de propagations a faire avant la prochaine
execution de 'simplify()' *)
mutable simpDB_props : int;
(* Un tas ordone en fonction de l'activite des variables *)
mutable order : Vheap.t;
(* estimation de progressions, mis a jour par 'search()' *)
mutable progress_estimate : float;
(* *)
remove_satisfied : bool;
(* inverse du facteur d'acitivte des variables, vaut 1/0.999 par defaut *)
var_decay : float;
(* inverse du facteur d'activite des clauses, vaut 1/0.95 par defaut *)
clause_decay : float;
(* la limite de restart initiale, vaut 100 par defaut *)
mutable restart_first : int;
(* facteur de multiplication de restart limite, vaut 1.5 par defaut*)
restart_inc : float;
(* limite initiale du nombre de clause apprises, vaut 1/3
des clauses originales par defaut *)
mutable learntsize_factor : float;
(* multiplier learntsize_factor par cette valeur a chaque restart,
vaut 1.1 par defaut *)
learntsize_inc : float;
(* controler la minimisation des clauses conflit, vaut true par defaut *)
expensive_ccmin : bool;
(* controle la polarite a choisir lors de la decision *)
polarity_mode : bool;
mutable starts : int;
mutable decisions : int;
mutable propagations : int;
mutable conflicts : int;
mutable clauses_literals : int;
mutable learnts_literals : int;
mutable max_literals : int;
mutable tot_literals : int;
mutable nb_init_clauses : int;
mutable tenv : Th.t;
mutable unit_tenv : Th.t;
mutable tenv_queue : Th.t Vec.t;
mutable tatoms_queue : Atom.atom Queue.t;
(** Queue of atoms that have been added to the [trail] through either
decision or boolean propagation, but have not been otherwise processed
yet (in particular, they have not been propagated to the theory).
In pure CDCL mode, all the atoms in [tatoms_queue] are propagated in
the theory.
In CDCL-Tableaux with the [get_cdcl_tableaux_th ()] option, the
[tatoms_queue] is only used for relevancy propagation (see
[relevancy_propagation]); instead, the [th_tableaux] field is used to
dictate theory propagations. *)
mutable th_tableaux : Atom.atom Queue.t;
(** Queue of atoms to propagate to the theory when using CDCL-Tableaux
with the [get_cdcl_tableaux_th ()] option.
The atoms in [th_tableaux] correspond to the asserted components of
the [lazy_cnf] formulas. In particular, they always correspond to
[UNIT] flat formulas that have been asserted (or selected in a
disjunction).
This ensures that atoms are propagated to the theory according to the
structure of the formula. *)
mutable cpt_current_propagations : int;
mutable proxies : FF.proxies;
(** Map from flat formulas to the proxies that represent them.
Note: the [Satml] module does not touch the [proxies] field itself; it
only uses it to find the atom associated with a flat formula.
Appropriate proxies that cover all the flat formulas to be encountered
need to be pre-emptively given using [set_new_proxies], which is
called from [Satml_frontend]. *)
mutable lazy_cnf :
(FF.t list MFF.t * FF.t) Matoms.t;
(** Map from atoms [a] to pairs [parents, ff].
The [lazy_cnf] field is only used by the CDCL-Tableaux solver. It is
used in relevancy propagation (see [relevancy_propagation]) when the
[get_cdcl_tableaux_th ()] option is enabled, and to stop decisions as
soon as the *relevant* parts of the assertions have been decided if
the [get_cdcl_tableaux_inst ()] option is enabled.
It satisfies the following invariants:
- [ff] is the flat formula that the atom [a] represents (either
directly if [ff] is an UNIT formula, or indirectly as a proxy -- in
which case, [a] is the proxy associated with [ff] in [proxies]).
- [parents] is the set of _disjunctive_ flat formulas that contain
[ff] ([parents] is a map whose entries are always of the form
[FF.OR l -> l], where [ff] appears in [l]).
- The formulas in [parents] represent the disjunctions that have been
asserted but not yet proven (i.e. none of their components is
[true]).
- The atoms that appear as keys in [lazy_cnf] are not [true] (they
are either undecided or forced to [false]). Once an atom in
[lazy_cnf] is forced to [true], it is removed from the [lazy_cnf]
and its parents are removed as parents of their other children (one
of their components is [true], so they no longer constrain the
problem). The corresponding flat formula is then added again, which
may introduce new entries to the [lazy_cnf] if it was itself a
disjunction or contained disjunctions (see
[relevancy_propagation]).
When [lazy_cnf] is empty, all the disjunctions have been decided, and
we have a (partial!) boolean model. If there are nested disjunctions
(for instance, if [a \/ (b /\ (c \/ d))] is asserted, and we decide or
propagate that [a] is true, [c] and [d] may stay undecided). *)
lazy_cnf_queue :
(FF.t list MFF.t * FF.t) Matoms.t Vec.t;
(** Checkpoint of the [lazy_cnf] field at each decision level. Only used
with the CDCL-Tableaux solver. *)
mutable relevants : SFF.t;
(** Set of relevant flat formulas. These are the formulas that have been
added to the lazy CNF (i.e. asserted), including sub-formulas that are
true.
For instance, when asserting [a \/ (b /\ c)], the formula
[a \/ (b /\ c)] is added to the [relevants] set; when deciding to pick
the second branch of the disjunction, the formulas [b /\ c], [b] and
[c] are added to the [relevants] set (see [add_form_to_lazy_cnf]).
Used to extract relevant terms for instantiation when only option
[get_cdcl_tableaux_inst ()] (but not [get_cdcl_tableaux_th ()]) is
enabled. *)
relevants_queue : SFF.t Vec.t;
(** Checkpoint of the [relevant] field at each decision level. Only used
with the CDCL-Tableaux solver. *)
mutable ff_lvl : int MFF.t;
(** Map from flat formulas to the (earliest) decision level at which they
were asserted. Only used with the CDCL-Tableaux solver. *)
mutable lvl_ff : SFF.t Util.MI.t;
(** Map from decision level to the set of flat formulas asserted at that
level. Set inverse of [ff_lvl]. Only used with the CDCL-Tableaux
solver. *)
mutable increm_guards : Atom.atom Vec.t;
mutable next_dec_guard : int;
mutable next_decisions : Atom.atom list;
(** Literals that must be decided on before the solver can answer [Sat].
These are added by the theory through calls to [acts_add_decision]. *)
mutable next_split : Atom.atom option;
(** Literal that should be decided on before the solver answers [Sat].
These are added by the theory through calls to [acts_add_split]. The
difference with [next_decisions] is that the [splits] are optional
(i.e. they can be dropped, and the solver is allowed to answer [Sat]
without deciding on the splits if it chooses so) whereas once added,
the [decisions] are guaranteed to be decided on at some point (unless
backtracking occurs). *)
mutable next_objective :
(Objective.Function.t * Objective.Value.t * Atom.atom) option;
(** Objective functions that must be optimized before the solver can
answer [Sat].
The provided [Atom.atom] correspond to a decision forcing the
[Function.t] to the corresponding [Value.t] (or nudging the model
towards "more optimal" values if the objective is not reachable); once
the decision is made by the solver, the optimized value is sent back
to the theory through [Th.add_objective]. *)
}
exception Conflict of Atom.clause
(*module Make (Dummy : sig end) = struct*)
let create hcons_env =
{
hcons_env;
is_unsat = false;
unsat_core = None;
clauses = Vec.make 0 ~dummy:Atom.dummy_clause;
(*sera mis a jour lors du parsing*)
learnts = Vec.make 0 ~dummy:Atom.dummy_clause;
(*sera mis a jour lors du parsing*)
clause_inc = 1.;
var_inc = 1.;
vars = Vec.make 0 ~dummy:Atom.dummy_var;
(*sera mis a jour lors du parsing*)
trail = Vec.make 601 ~dummy:Atom.dummy_atom;
trail_lim = Vec.make 601 ~dummy:(-105);
nchoices = 0;
nchoices_stack = Vec.make 100 ~dummy:(-105);
qhead = 0;
simpDB_assigns = -1;
simpDB_props = 0;
order = Vheap.make 0 Atom.dummy_var; (* sera mis a jour dans solve *)
progress_estimate = 0.;
remove_satisfied = true;
var_decay = 1. /. 0.95;
clause_decay = 1. /. 0.999;
restart_first = 100;
restart_inc = 1.5;
learntsize_factor = 1. /. 3. ;
learntsize_inc = 1.1;
expensive_ccmin = true;
polarity_mode = false;
starts = 0;
decisions = 0;
propagations = 0;
conflicts = 0;
clauses_literals = 0;
learnts_literals = 0;
max_literals = 0;
tot_literals = 0;
nb_init_clauses = 0;
tenv = Th.empty();
unit_tenv = Th.empty();
tenv_queue = Vec.make 100 ~dummy:(Th.empty());
tatoms_queue = Queue.create ();
th_tableaux = Queue.create ();
cpt_current_propagations = 0;
proxies = FF.empty_proxies;
lazy_cnf = Matoms.empty;
lazy_cnf_queue =
Vec.make 100
~dummy:(Matoms.singleton (Atom.faux_atom) (MFF.empty, FF.faux));
relevants = SFF.empty;
relevants_queue = Vec.make 100 ~dummy:(SFF.singleton (FF.faux));
ff_lvl = MFF.empty;
lvl_ff = Util.MI.empty;
increm_guards = Vec.make 1 ~dummy:Atom.dummy_atom;
next_dec_guard = 0;
next_decisions = [];
next_split = None;
next_objective = None;
}
let insert_var_order env (v : Atom.var) =
Vheap.insert env.order v
let var_decay_activity env = env.var_inc <- env.var_inc *. env.var_decay
let clause_decay_activity env =
env.clause_inc <- env.clause_inc *. env.clause_decay
let var_bump_activity env (v : Atom.var) =
v.weight <- v.weight +. env.var_inc;
if (Stdlib.compare v.weight 1e100) > 0 then begin
Vec.iter
(fun (v : Atom.var) ->
v.weight <- v.weight *. 1e-100
) env.vars;
env.var_inc <- env.var_inc *. 1e-100;
end;
if Vheap.in_heap v then
Vheap.decrease env.order v
let clause_bump_activity env (c : Atom.clause) =
c.activity <- c.activity +. env.clause_inc;
if (Stdlib.compare c.activity 1e20) > 0 then begin
Vec.iter (fun (clause : Atom.clause) ->
clause.activity <- clause.activity *. 1e-20
) env.learnts;
env.clause_inc <- env.clause_inc *. 1e-20
end
let decision_level env = Vec.size env.trail_lim
let nb_choices env = env.nchoices
let nb_assigns env = Vec.size env.trail - nb_choices env
let nb_clauses env = Vec.size env.clauses
(* unused -- let nb_learnts env = Vec.size env.learnts *)
let nb_vars env = Vec.size env.vars
let new_decision_level env =
env.decisions <- env.decisions + 1;
Vec.push env.trail_lim (Vec.size env.trail);
Vec.push env.nchoices_stack env.nchoices;
if Options.get_profiling() then
Profiling.decision (decision_level env) "<none>";
Vec.push env.tenv_queue env.tenv; (* save the current tenv *)
if Options.get_cdcl_tableaux () then begin
Vec.push env.lazy_cnf_queue env.lazy_cnf;
Vec.push env.relevants_queue env.relevants
end
let attach_clause env (c : Atom.clause) =
Vec.push (Vec.get c.atoms 0).neg.watched c;
Vec.push (Vec.get c.atoms 1).neg.watched c;
if c.learnt then
env.learnts_literals <- env.learnts_literals + Vec.size c.atoms
else
env.clauses_literals <- env.clauses_literals + Vec.size c.atoms
let detach_clause env (c : Atom.clause) =
c.removed <- true;
(*
Vec.remove (Vec.get c.atoms 0).neg.watched c;
Vec.remove (Vec.get c.atoms 1).neg.watched c;
*)
if c.learnt then
env.learnts_literals <- env.learnts_literals - Vec.size c.atoms
else
env.clauses_literals <- env.clauses_literals - Vec.size c.atoms
let remove_clause env c = detach_clause env c
let satisfied (c : Atom.clause) =
Vec.exists (fun (atom : Atom.atom) ->
atom.is_true && atom.var.level == 0
) c.atoms
let is_assigned (a : Atom.atom) =
a.is_true || a.neg.is_true
let unassign_atom (a : Atom.atom) =
a.is_true <- false;
a.neg.is_true <- false;
a.timp <- 0;
a.neg.timp <- 0;
a.var.level <- -1;
a.var.index <- -1;
a.var.reason <- None;
a.var.vpremise <- []
let enqueue_assigned env (a : Atom.atom) =
if Options.get_debug_sat () then
Printer.print_dbg "[satml] enqueue_assigned: %a@." Atom.pr_atom a;
if a.neg.is_guard then begin
(* if the negation of a is (still) a guard, it should be forced to true
during the first decisions.
If the SAT tries to deduce that a.neg is true (ie. a is false),
then we have detected an inconsistency. *)
assert (a.var.level <= env.next_dec_guard);
(* guards are necessarily decided/propagated before all other atoms *)
raise (Unsat None);
end;
assert (a.is_true || a.neg.is_true);
if a.timp = 1 then begin
a.timp <- -1;
a.neg.timp <- -1
end;
assert (a.var.level >= 0);
Vec.push env.trail a;
if is_semantic a then
env.nchoices <- env.nchoices + 1
let cancel_ff_lvls_until env lvl =
for i = decision_level env downto lvl + 1 do
try
let s = Util.MI.find i env.lvl_ff in
SFF.iter (fun f' -> env.ff_lvl <- MFF.remove f' env.ff_lvl) s;
env.lvl_ff <- Util.MI.remove i env.lvl_ff;
with Not_found -> ()
done
(* annule tout jusqu'a lvl *exclu* *)
let cancel_until env lvl =
if Options.get_debug_sat () then
Printer.print_dbg
"[satml] cancel until %d (current is %d)@." lvl (decision_level env);
cancel_ff_lvls_until env lvl;
let repush = ref [] in
if decision_level env > lvl then begin
env.qhead <- Vec.get env.trail_lim lvl;
for c = Vec.size env.trail - 1 downto env.qhead do
let a = Vec.get env.trail c in
if Options.get_minimal_bj () && a.var.level <= lvl then begin
assert (a.var.level = 0 || a.var.reason != None);
repush := a :: !repush
end
else begin
unassign_atom a;
if a.is_guard then
env.next_dec_guard <- env.next_dec_guard - 1;
if not (is_semantic a) then
insert_var_order env a.var
end
done;
Queue.clear env.tatoms_queue;
Queue.clear env.th_tableaux;
env.tenv <- Vec.get env.tenv_queue lvl; (* recover the right tenv *)
env.nchoices <- Vec.get env.nchoices_stack lvl;
if Options.get_cdcl_tableaux () then begin
env.lazy_cnf <- Vec.get env.lazy_cnf_queue lvl;
env.relevants <- Vec.get env.relevants_queue lvl;
end;
Vec.shrink env.trail env.qhead;
Vec.shrink env.trail_lim lvl;
Vec.shrink env.nchoices_stack lvl;
Vec.shrink env.tenv_queue lvl;
if Options.get_cdcl_tableaux () then begin
Vec.shrink
env.lazy_cnf_queue lvl;
Vec.shrink env.relevants_queue
lvl
[@ocaml.ppwarning "TODO: try to disable 'fill_with_dummy'"]
end;
(try
let last_dec =
if Vec.size env.trail_lim = 0 then 0 else Vec.last env.trail_lim in
env.cpt_current_propagations <- (Vec.size env.trail) - last_dec
with _ -> assert false
);
env.next_decisions <- [];
env.next_split <- None;
env.next_objective <- None
end;
if Options.get_profiling() then Profiling.reset_dlevel (decision_level env);
assert (Vec.size env.trail_lim = Vec.size env.tenv_queue);
assert (Vec.size env.trail_lim = Vec.size env.nchoices_stack);
assert (Options.get_minimal_bj () || (!repush == []));
List.iter (enqueue_assigned env) !repush
let debug_enqueue_level a lvl reason =
match reason with
| None -> ()
| Some (c : Atom.clause) ->
let maxi = ref min_int in
Vec.iter (fun (atom : Atom.atom) ->
if not (Atom.eq_atom a atom) then
maxi := max !maxi atom.var.level
) c.atoms;
assert (!maxi = lvl)
let max_level_in_clause (c : Atom.clause) =
let max_lvl = ref 0 in
Vec.iter (fun (a : Atom.atom) ->
max_lvl := max !max_lvl a.var.level) c.atoms;
!max_lvl
let enqueue env (a : Atom.atom) lvl reason =
assert (not a.is_true && not a.neg.is_true &&
a.var.level < 0 && a.var.reason == None && lvl >= 0);
if a.neg.is_guard then begin
(* if the negation of a is (still) a guard, it should be forced to true
during the first decisions.
If the SAT tries to deduce that a.neg is true (ie. a is false),
then we have detected an inconsistency. *)
assert (a.var.level <= env.next_dec_guard);
(* guards are necessarily decided/propagated before all other atoms *)
raise (Unsat None);
end;
(* Garder la reason car elle est utile pour les unsat-core *)
(*let reason = if lvl = 0 then None else reason in*)
a.is_true <- true;
a.var.level <- lvl;
a.var.reason <- reason;
if Options.get_debug_sat () then
Printer.print_dbg "[satml] enqueue: %a@." Atom.pr_atom a;
Vec.push env.trail a;
if is_semantic a then
env.nchoices <- env.nchoices + 1;
a.var.index <- Vec.size env.trail;
if Options.get_enable_assertions() then debug_enqueue_level a lvl reason
let progress_estimate env =
let prg = ref 0. in
let nbv = Atom.to_float (nb_vars env) in
let lvl = decision_level env in
let _F = 1. /. nbv in
for i = 0 to lvl do
let _beg = if i = 0 then 0 else Vec.get env.trail_lim (i-1) in
let _end =
if i=lvl then Vec.size env.trail
else Vec.get env.trail_lim i in
prg := !prg +. _F**(Atom.to_float i) *. (Atom.to_float (_end - _beg))
done;
!prg /. nbv
let check_levels propag_lvl current_lvl =
assert (propag_lvl <= current_lvl);
assert (propag_lvl == current_lvl || (Options.get_minimal_bj ()))
let best_propagation_level env c =
let mlvl =
if Options.get_minimal_bj () then max_level_in_clause c
else decision_level env
in
check_levels mlvl (decision_level env);
mlvl
let propagate_in_clause env (a : Atom.atom) (c : Atom.clause)
i watched new_sz =
let atoms = c.atoms in
let first = Vec.get atoms 0 in
if first == a.neg then begin (* le literal faux doit etre dans .(1) *)
Vec.set atoms 0 (Vec.get atoms 1);
Vec.set atoms 1 first
end;
let first = Vec.get atoms 0 in
if first.is_true then begin
(* clause vraie, la garder dans les watched *)
Vec.set watched !new_sz c;
incr new_sz;
if Options.get_profiling() then Profiling.elim true;
end
else
try (* chercher un nouveau watcher *)
for k = 2 to Vec.size atoms - 1 do
let ak = Vec.get atoms k in
if not (ak.neg.is_true) then begin
(* Watcher Trouve: mettre a jour et sortir *)
Vec.set atoms 1 ak;
Vec.set atoms k a.neg;
Vec.push ak.neg.watched c;
raise Exit
end
done;
(* Watcher NON Trouve *)
if first.neg.is_true then begin
(* la clause est fausse *)
env.qhead <- Vec.size env.trail;
for k = i to Vec.size watched - 1 do
Vec.set watched !new_sz (Vec.get watched k);
incr new_sz;
done;
if Options.get_profiling() then Profiling.bcp_conflict true true;
raise (Conflict c)
end
else begin
(* la clause est unitaire *)
Vec.set watched !new_sz c;
incr new_sz;
let mlvl = best_propagation_level env c in
enqueue env first mlvl (Some c);
if Options.get_profiling() then Profiling.red true;
end
with Exit -> ()
let propagate_atom env (a : Atom.atom) res =
let watched = a.watched in
let new_sz_w = ref 0 in
begin
try
Vec.iteri (fun i (clause : Atom.clause) ->
if not clause.removed then
propagate_in_clause env a clause i watched new_sz_w
) watched;
with Conflict c -> assert (!res == C_none); res := C_bool c
end;
Vec.shrink watched !new_sz_w
let acts_add_decision_lit env lit =
let atom, _ = Atom.add_atom env.hcons_env lit [] in
if atom.var.level < 0 then (
assert (not atom.is_true && not atom.neg.is_true);
env.next_decisions <- atom :: env.next_decisions
) else
assert (atom.is_true || atom.neg.is_true)
let acts_add_split env lit =
let atom, _ = Atom.add_atom env.hcons_env lit [] in
if atom.var.level < 0 then (
assert (not atom.is_true && not atom.neg.is_true);
env.next_split <- Some atom
) else
assert (atom.is_true || atom.neg.is_true)
let acts_add_objective env fn value lit =
(* Note: we must store the objective even if the atom is already true,
because we must send back the objective to the theory afterwards.
We can't update the theory inside this function, because it is called
from within the theory. *)
let atom, _ = Atom.add_atom env.hcons_env lit [] in
env.next_objective <- Some (fn, value, atom)
let[@inline] theory_slice env : _ Th_util.acts = {
acts_add_decision_lit = acts_add_decision_lit env ;
acts_add_split = acts_add_split env ;
acts_add_objective = acts_add_objective env ;
}
let do_case_split env origin =
try
let acts = theory_slice env in
let tenv, _terms = Th.do_case_split ~acts env.tenv origin in
(* TODO: terms not added to matching !!! *)
env.tenv <- tenv;
C_none
with Ex.Inconsistent (expl, _) ->
C_theory expl
module SA = Atom.Set
let get_atom_or_proxy f proxies =
let open FF in
match view f with
| UNIT a -> a
| _ ->
match get_proxy_of f proxies with
| Some a -> a
| None -> assert false
(* [add_form_to_lazy_cnf env lazy_cnf ff] updates [env] and [lazy_cnf] by
assuming the flat formula [ff].
More precisely:
- [ff] and any conjunctive component (i.e. if [ff = ff_1 /\ ff_2], [ff_1]
and [ff_2] are conjunctive components, recursively) are added to the
[relevant] field in [env];
- UNIT formulas (either [ff] or in a conjunctive component) are added to
the [th_tableaux] queue in [env] (NOTE: [th_tableaux] is only used if
the [get_cdcl_tableaux_th ()] option is enabled);
- Disjunctive formulas that contain a component already known to be true
are treated as that component (this is used by [relevancy_propagation]
which adds the decided children of disjunctive formulas -- it is also
necessary for soundness in case a child atom was propagated earlier than
expected e.g. due to clause learning);
- Disjunctive formulas that are still undecided are added to the
[lazy_cnf] (see documentation of the [lazy_cnf] field in [env]). *)
let add_form_to_lazy_cnf =
let open FF in
let add_disj env ma f_a l =
List.fold_left
(fun ma fchild ->
let child = get_atom_or_proxy fchild env.proxies in
let ctt =
try Matoms.find child ma |> fst with Not_found -> MFF.empty
in
Matoms.add child (MFF.add f_a l ctt, fchild) ma
)ma l
in
let rec add_aux env ma (f_a : t) =
if SFF.mem f_a env.relevants then ma
else
begin
env.relevants <- SFF.add f_a env.relevants;
match view f_a with
| UNIT a ->
Queue.push a env.th_tableaux;
ma
| AND l ->
List.fold_left (add_aux env) ma l
| OR l ->
match Stdcompat.List.find_opt (fun e ->
let p = get_atom_or_proxy e env.proxies in
p.is_true) l
with
| None -> add_disj env ma f_a l
| Some e -> add_aux env ma e
end
in
fun env ma f_a -> add_aux env ma f_a
(** [relevancy_propagation env lazy_cnf a] propagates the fact that atom [a]
is now true to [env] and the [lazy_cnf].
More precisely, if [a] was an atom in the [lazy_cnf] (i.e. if a formula of
the form [a_0 \/ ... \/ a \/ ... \/ a_n] must hold given the current
CNF unfolding), its parents are removed from the [lazy_cnf] and [a] is
added again to the [lazy_cnf]. This will either add [a] to the
[th_tableaux] field (if [a] corresponds to a [UNIT] flat formula that was
asserted), or otherwise perform one step of CNF unfolding and add the
formula that [a] was a proxy for to the [lazy_cnf] (see
[add_form_to_lazy_cnf]). *)
let relevancy_propagation env ma a =
match Shostak.Literal.view @@ Atom.literal a with
| LSem _ ->
(* Always propagate back semantic literals to the theory. *)
Queue.push a env.th_tableaux;
ma
| LTerm _ ->
try
let parents, f_a = Matoms.find a ma in
let ma = Matoms.remove a ma in
let ma =
MFF.fold
(fun fp lp ma ->
List.fold_left
(fun ma bf ->
let b = get_atom_or_proxy bf env.proxies in
if Atom.eq_atom a b then ma
else
let mf_b, fb =
try Matoms.find b ma with Not_found -> assert false in
assert (FF.equal bf fb);
let mf_b = MFF.remove fp mf_b in
if MFF.is_empty mf_b then Matoms.remove b ma
else Matoms.add b (mf_b, fb) ma
)ma lp
)parents ma
in
assert (let a = get_atom_or_proxy f_a env.proxies in a.is_true);
add_form_to_lazy_cnf env ma f_a
with Not_found -> ma
(* Note: although this seems to only update [env.lazy_cnf], the call to
[relevancy_propagation] in fact updates the [th_tableaux] queue, which is
the one used for theory propagation when [get_cdcl_tableaux_th ()] is
enabled.
When only [get_cdcl_tableaux_inst ()] is enabled, we still need to call
this, because the update of [lazy_cnf] is required for early stopping. *)