Source file Internal.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
open Solver_types
module Fmt = CCFormat
type proof = Proof.t
exception Sat
exception Unsat
exception UndecidedLit of term
exception Restart
exception Out_of_time
exception Out_of_space
let error = 1
let warn = 3
let info = 5
let debug = 15
module H = Heap.Make(struct
type t = term
let[@inline] idx t = t.t_idx
let[@inline] set_idx t i = t.t_idx <- i
let[@inline] cmp i j = Term.weight j < Term.weight i
end)
type t = {
plugins: Plugin.t CCVector.vector;
services: Service.Registry.t;
clauses_hyps : clause Vec.t;
clauses_learnt : clause Vec.t;
clauses_temp : clause Vec.t;
clauses_root : clause Stack.t;
clauses_to_add : clause Stack.t;
actions: actions lazy_t;
mutable unsat_conflict : clause option;
mutable next_decision : atom option;
trail : term Vec.t;
decision_levels : level Vec.t;
backtrack_stack : (unit -> unit) Vec.t;
backtrack_levels: level Vec.t;
user_levels : level Vec.t;
mutable bcp_head : int;
mutable th_head : int;
term_heap : H.t;
var_decay : float;
clause_decay : float;
tmp_term_vec : term Vec.t;
mutable var_incr : float;
mutable clause_incr : float;
restart_inc : float;
mutable restart_first : int;
mutable learntsize_factor : float;
mutable learntsize_inc : float;
mutable starts : int;
mutable decisions : int;
mutable bool_decisions : int;
mutable propagations : int;
mutable conflicts : int;
mutable n_learnt : int;
mutable n_gc_c: int;
mutable n_gc_t: int;
mutable n_deleted_c: int;
mutable n_deleted_t: int;
mutable nb_init_clauses : int;
}
(** {2 Print} *)
let[@inline] get_plugin (env:t) (p_id:plugin_id) : Plugin.t =
try CCVector.get env.plugins p_id
with _ ->
Log.debugf error (fun k->k "cannot find plugin %d" p_id);
assert false
let[@inline] plugin_of_term (env:t) (t:term) : Plugin.t =
get_plugin env (Term.plugin_id t)
let[@inline] plugins t = t.plugins
let[@inline] actions t = Lazy.force t.actions
let[@inline] to_float i = float_of_int i
let[@inline] to_int f = int_of_float f
let[@inline] nb_clauses env = Vec.size env.clauses_hyps
let[@inline] decision_level env = Vec.size env.decision_levels
let[@inline] base_level env = Vec.size env.user_levels
let[@inline] services env = env.services
let[@inline] plugins env = CCVector.to_iter env.plugins
let[@inline] get_service env (k:_ Service.Key.t) =
Service.Registry.find env.services k
let[@inline] get_service_exn env (k:_ Service.Key.t) =
Service.Registry.find_exn env.services k
let[@inline] err_undecided_lit t = raise (UndecidedLit t)
let() = Printexc.register_printer
(function
| UndecidedLit t ->
Some (Error.err_sprintf "undecided_lit: %a" Term.debug t)
| Out_of_space -> Some "Unknown"
| Out_of_time -> Some "Timeout"
| _ -> None)
let add_plugin (env:t) (fcty:Plugin.Factory.t) : Plugin.t =
let id = CCVector.length env.plugins |> Term.Unsafe.mk_plugin_id in
let rec find_services
: type a. a Plugin.service_key_list -> a Plugin.service_list
= function
| Plugin.K_nil -> Plugin.S_nil
| Plugin.K_cons (k, tail) ->
begin match get_service env k with
| None ->
Error.errorf "could not find service `%s`" (Service.Key.name k)
| Some serv ->
Plugin.S_cons (k, serv, find_services tail)
end
in
let Plugin.Factory.Factory {requires; build; _} = fcty in
let serv_list = find_services requires in
let p = build id serv_list in
CCVector.push env.plugins p;
Log.debugf info (fun k->k "add plugin %s with ID %d" (Plugin.name p) id);
let (module P) = p in
List.iter
(fun (Service.Any (k,s)) -> Service.Registry.register env.services k s)
P.provided_services;
p
let[@inline] is_unsat t = match t.unsat_conflict with
| Some _ -> true
| None -> false
let[@inline] iter_terms (env:t) : term Iter.t =
CCVector.to_iter env.plugins
|> Iter.flat_map
(fun (module P : Plugin.S) -> P.iter_terms)
|> Iter.filter Term.has_var
let[@inline] term_init (env:t) (t:term) : unit =
t.t_tc.tct_init (actions env) t
let[@unrolled 1] rec add_term (env:t) (t:term): unit =
if Term.is_deleted t then (
Error.errorf "(@[trying to add deleted term@ `%a`@])" Term.debug t
) else if Term.is_added t then (
assert (Term.has_var t);
) else (
Log.debugf 15 (fun k->k"(@[solver.add_term %a@])" Term.debug t);
Term.field_set field_t_is_added t;
Term.setup_var t;
Term.iter_subterms t (add_term env);
begin match Term.eval t with
| Eval_into (value, []) ->
t.t_assign <- TA_assign {value;reason=Eval [];level=0};
| Eval_into (value, l) when List.for_all (fun t -> Term.level t=0) l ->
t.t_assign <- TA_assign {value;reason=Eval l;level=0};
| _ ->
H.insert env.term_heap t;
end;
term_init env t;
)
let[@inline] add_atom (env:t) (a:atom) : unit = add_term env (Atom.term a)
let[@inline] schedule_decision_term (env:t) (t:term): unit =
H.insert env.term_heap t
let var_decay_activity (env:t) =
env.var_incr <- env.var_incr *. env.var_decay
let clause_decay_activity (env:t) =
env.clause_incr <- env.clause_incr *. env.clause_decay
let decay_all_terms (env:t): unit =
iter_terms env
(fun t -> Term.set_weight t (Term.weight t *. 1e-100));
env.var_incr <- env.var_incr *. 1e-100;
()
let bump_term_activity_aux (env:t) (t:term): unit =
t.t_weight <- t.t_weight +. env.var_incr;
if t.t_weight > 1e100 then (
decay_all_terms env;
);
if H.in_heap t then (
H.decrease env.term_heap t
)
let[@inline] bump_term_activity env (t:term): unit =
bump_term_activity_aux env t;
Term.iter_subterms t (bump_term_activity_aux env)
let decay_all_learnt_clauses env : unit =
Vec.iter
(fun c -> c.c_activity <- c.c_activity *. 1e-20)
env.clauses_learnt;
env.clause_incr <- env.clause_incr *. 1e-20
let[@inline] bump_clause_activity (env:t) (c:clause) : unit =
c.c_activity <- c.c_activity +. env.clause_incr;
if c.c_activity > 1e20 then (
decay_all_learnt_clauses env;
)
let[@inline] decide_term (env:t) (t:term): value =
let ty = (Term.ty t) in
if ty == Bool then
env.bool_decisions <- env.bool_decisions + 1;
Type.decide ty (actions env) t
let[@inline] assign_term (env:t) (t:term) value reason level : unit =
assert (t.t_assign == TA_none);
t.t_assign <- TA_assign {value;reason;level};
Vec.push env.trail t
let create_real (actions:actions lazy_t) : t = {
unsat_conflict = None;
next_decision = None;
plugins = CCVector.create();
services = Service.Registry.create();
actions;
clauses_hyps = Vec.create();
clauses_learnt = Vec.create();
clauses_temp = Vec.create();
clauses_root = Stack.create ();
clauses_to_add = Stack.create ();
th_head = 0;
bcp_head = 0;
trail = Vec.create();
backtrack_stack = Vec.make 601 (fun () -> assert false);
backtrack_levels = Vec.make 10 (-1);
decision_levels = Vec.make 601 (-1);
user_levels = Vec.make 10 (-1);
tmp_term_vec = Vec.create();
term_heap = H.create();
var_incr = 1.;
clause_incr = 1.;
var_decay = 1. /. 0.95;
clause_decay = 1. /. 0.999;
restart_inc = 1.5;
restart_first = 100;
learntsize_factor = 1.5 ;
learntsize_inc = 1.5;
starts = 0;
decisions = 0;
bool_decisions = 0;
propagations = 0;
conflicts = 0;
n_learnt=0;
n_gc_c=0;
n_gc_t=0;
n_deleted_c=0;
n_deleted_t=0;
nb_init_clauses = 0;
}
exception Trivial
let[@inline] arr_to_list arr i : _ list =
if i >= Array.length arr then []
else Array.to_list (Array.sub arr i (Array.length arr - i))
let eliminate_duplicates_and_absurd (clause:clause) : clause * bool =
let duplicates = ref [] in
let removed_absurd = ref false in
let res = ref [] in
Array.iter
(fun a ->
if Atom.marked a then duplicates := a :: !duplicates
else if Atom.is_absurd a then removed_absurd := true
else (Atom.mark a; res := a :: !res))
(Clause.atoms clause);
let trivial =
List.exists (fun a -> Term.Bool.both_atoms_marked a.a_term) !res
in
Array.iter Atom.unmark clause.c_atoms;
if trivial then (
raise Trivial
) else if not !removed_absurd && !duplicates = [] then (
clause, false
) else (
Clause.make !res (Simplify clause), true
)
let simplify_clause (c:clause) : clause =
let c', has_simplified = eliminate_duplicates_and_absurd c in
if has_simplified then (
Log.debugf 15
(fun k -> k "(@[solver.simplify_clause@ :into %a@ :from %a@])"
Clause.debug c' Clause.debug c);
);
c'
let partition_atoms (atoms:atom array) : atom list * raw_premise_step list =
let rec partition_aux trues unassigned falses history i =
if i >= Array.length atoms then (
trues @ unassigned @ falses, history
) else (
let a = atoms.(i) in
if Atom.is_true a then (
let l = Atom.level a in
if l = 0 then (
raise Trivial
) else (
(a :: trues) @ unassigned @ falses @
(arr_to_list atoms (i + 1)), history
)
) else if Atom.is_false a then (
let l = Atom.level a in
if l = 0 then (
begin match Term.reason a.a_term with
| Some (Bcp cl | Bcp_lazy (lazy cl)) ->
partition_aux trues unassigned falses
(cl :: history) (i + 1)
| Some (Eval []) ->
partition_aux trues unassigned falses history (i + 1)
| Some (Eval l) when List.for_all (fun t->Term.level t=0) l ->
partition_aux trues unassigned falses history (i + 1)
| Some (Eval _) -> assert false
| None | Some Decision -> assert false
end
) else (
partition_aux trues unassigned (a::falses) history (i + 1)
)
) else (
partition_aux trues (a::unassigned) falses history (i + 1)
)
)
in
partition_aux [] [] [] [] 0
let[@inline] fully_propagated (env:t) : bool =
env.th_head = Vec.size env.trail &&
env.bcp_head = Vec.size env.trail
let new_decision_level (env:t) : unit =
assert (fully_propagated env);
Vec.push env.decision_levels (Vec.size env.trail);
Vec.push env.backtrack_levels (Vec.size env.backtrack_stack);
()
let attach_clause (_env:t) (c:clause): unit =
if not (Clause.attached c) then (
Log.debugf debug (fun k -> k "(@[solver.attach_clause@ %a@])" Clause.debug c);
Vec.push (Atom.neg c.c_atoms.(0)).a_watched c;
Vec.push (Atom.neg c.c_atoms.(1)).a_watched c;
Clause.set_attached c;
)
let cancel_until (env:t) (lvl:int) : unit =
assert (lvl >= base_level env);
if decision_level env <= lvl then (
Log.debugf debug (fun k -> k "Already at level <= %d" lvl)
) else (
Log.debugf info (fun k -> k "@{<Yellow>### Backtracking@} to lvl %d" lvl);
let top = Vec.get env.decision_levels lvl in
let backtrack_top = Vec.get env.backtrack_levels lvl in
env.th_head <- top;
let head = ref top in
for i = top to Vec.size env.trail - 1 do
let t = Vec.get env.trail i in
if Term.level t <= lvl then (
Vec.set env.trail !head t;
head := !head + 1;
) else (
Log.debugf 50 (fun k->k "pop term %a (in heap %B, deleted %B)"
Term.debug t (H.in_heap t) (Term.is_deleted t));
t.t_assign <- TA_none;
if not (Term.is_deleted t) then (
schedule_decision_term env t;
)
);
done;
env.bcp_head <- !head;
Vec.shrink env.trail !head;
Vec.shrink env.decision_levels lvl;
Vec.shrink env.backtrack_levels lvl;
while Vec.size env.backtrack_stack > backtrack_top do
let f = Vec.pop env.backtrack_stack in
f();
done;
);
assert (Vec.size env.decision_levels = Vec.size env.backtrack_levels);
()
exception Conflict of clause
let report_unsat (env:t) (confl:clause) : _ =
Log.debugf info
(fun k -> k "(@[@{<Yellow>solver.unsat_conflict@}:@ %a@])" Clause.debug confl);
env.unsat_conflict <- Some confl;
raise Unsat
let simpl_reason_level_0 : reason -> reason = function
| (Bcp cl | Bcp_lazy (lazy cl)) as r ->
let l, history = partition_atoms cl.c_atoms in
begin match l with
| [_] ->
if history = [] then r
else (
let c' =
Clause.make l (Premise.raw_steps (cl :: history))
in
Log.debugf debug
(fun k -> k "(@[simplified_reason@ :from %a@ :to %a@])"
Clause.debug cl Clause.debug c');
Bcp c'
)
| _ ->
Error.errorf
"(@[simpl_reason_level_0.fail@ :simp-atoms %a@ :bcp-from %a@])"
Clause.debug_atoms l Clause.debug cl
end
| r -> r
let enqueue_bool (env:t) (a:atom) ~level:level (reason:reason) : unit =
if Atom.is_false a then (
Error.errorf "(@[solver.enqueue_bool.atom_is_false@ %a@])" Atom.debug a
) else if Atom.is_true a then (
Log.debugf 15 (fun k->k "(@[solver.enqueue_bool.already_true@ %a@])" Atom.debug a);
) else (
assert (level >= 0);
let reason =
if level > 0 then reason
else simpl_reason_level_0 reason
in
let value = Value.of_bool (Atom.is_pos a) in
assign_term env a.a_term value reason level;
Log.debugf debug
(fun k->k "(@[solver.enqueue_bool (%d/%d)@ %a@ :reason %a@])"
(Vec.size env.trail)(decision_level env) Atom.debug a Reason.pp (level,reason));
()
)
let enqueue_semantic_bool_eval (env:t) (a:atom) (terms:term list) : unit =
if Atom.is_true a then ()
else if Atom.is_false a then (
begin match Term.reason_exn a.a_term with
| Bcp c | Bcp_lazy (lazy c) ->
raise (Conflict c)
| Decision -> assert false
| Eval _ -> assert false
end
) else (
assert (List.for_all Term.is_added terms);
let lvl =
List.fold_left
(fun acc t ->
let t_lvl = Term.level t in
assert (t_lvl >= 0); max acc t_lvl)
0 terms
in
env.propagations <- env.propagations + 1;
enqueue_bool env a ~level:lvl (Eval terms)
)
let enqueue_bool_theory_propagate (env:t) (a:atom)
~lvl (atoms:atom list) (lemma: lemma) : unit =
if Atom.is_true a then ()
else (
let c = Clause.make atoms (Lemma lemma) |> simplify_clause in
env.propagations <- env.propagations + 1;
enqueue_bool env a ~level:lvl (Bcp c)
)
let enqueue_assign (env:t) (t:term) (value:value) (reason:reason) ~(level:int) : unit =
if Term.has_some_value t then (
Log.debugf error
(fun k -> k "Trying to assign an already assigned literal: %a" Term.debug t);
assert false
);
assert (t.t_assign = TA_none);
assign_term env t value reason level;
Log.debugf debug
(fun k->k "(@[solver.enqueue_semantic (%d/%d)@ %a@ :reason %a@])"
(Vec.size env.trail) (decision_level env)
Term.debug t Reason.pp (level,reason));
()
let th_eval (env:t) (a:atom) : value option =
if Atom.is_true a || Atom.is_false a then None
else (
begin match Atom.eval a with
| Eval_unknown -> None
| Eval_into (b, l) ->
let atom =
if Value.is_true b then a
else (
assert (Value.is_false b);
Atom.neg a
)
in
enqueue_semantic_bool_eval env atom l;
Some b
end
)
let pp_subs out l : unit =
let pp_p out t =
Fmt.fprintf out "(@[%a@ @<1>→ %a@])" Term.debug t Value.pp (Term.value_exn t)
in
Fmt.fprintf out "(@[<v>%a@])" (Util.pp_list pp_p) l
let debug_eval out = function
| Eval_unknown -> Fmt.string out "unknown"
| Eval_into (v, subs) ->
Fmt.fprintf out "(@[<hv>%a@ :subs (@[<v>%a@])@])" Value.pp v pp_subs subs
let eval_atom_to_false ~save (env:t) (a:atom): unit =
if Atom.has_some_value a then (
Log.debugf debug (fun k->k "(@[atom_must_be_false@ %a@])" Atom.debug a);
assert (
let ok = Atom.is_false a || Atom.can_eval_to_false a in
if not ok then (
Log.debugf 0 (fun k->k "(@[<2>atom should be false:@ %a@])" Atom.debug a);
);
ok);
) else (
let v = Atom.eval a in
Log.debugf debug (fun k->k "(@[atom_must_be_false@ %a@ :eval_to %a@])"
Atom.debug a debug_eval v);
begin match v with
| Eval_into (V_false, subs) ->
if save then (
enqueue_semantic_bool_eval env (Atom.neg a) subs
)
| _ ->
Log.debugf 0 (fun k->k "(@[<2>atom should be false:@ %a@])" Atom.debug a);
assert false
end
)
let[@inline] put_high_level_atoms_first (arr:atom array) : unit =
Array.iteri
(fun i a ->
if i>0 && Atom.level a > Atom.level arr.(0) then (
if i=1 then (
Util.swap_arr arr 0 1;
) else (
let tmp = arr.(1) in
arr.(1) <- arr.(0);
arr.(0) <- arr.(i);
arr.(i) <- tmp;
);
) else if i>1 && Atom.level a > Atom.level arr.(1) then (
Util.swap_arr arr 1 i;
))
arr
let[@inline] level_subs (l:term list) : level =
List.fold_left (fun l t -> max l (Term.level t)) 0 l
let[@inline] atom_as_false (a:atom) : (reason * level) option =
if Atom.is_false a then Some (Term.reason_exn a.a_term, Atom.level a)
else match Atom.eval a with
| Eval_into (V_false, subs) -> Some (Eval subs, level_subs subs)
| _ -> None
(** {2 Conflict Analysis} *)
module Conflict = struct
type t = clause
let[@inline] pp out (c:t) = Clause.debug out c
let[@inline] level (c:t) : level =
Array.fold_left
(fun acc p -> max acc (Atom.level p)) 0 c.c_atoms
end
module Conflict_res = struct
type t = {
cr_backtrack_lvl : int;
cr_learnt: atom array;
cr_history: raw_premise_step list;
cr_is_uip: bool;
}
end
module Analyze : sig
val analyze : t -> clause -> Conflict_res.t
end = struct
type state = {
cs_conflict_level: int;
mutable cs_continue: bool;
mutable cs_n_to_analyze: int;
mutable cs_ptr_trail: int;
mutable cs_clause: clause option;
mutable cs_learnt: atom list;
mutable cs_history: raw_premise_step list;
}
(** terms seen so far, for cleanup *)
let[@inline] seen (env:t) = env.tmp_term_vec
let backtrack_lvl (env:t) (a:atom array) : int * bool =
if Array.length a <= 1 then (
0, true
) else (
assert (Atom.level a.(0) >= base_level env);
assert (Atom.level a.(1) >= base_level env);
if Atom.level a.(0) > Atom.level a.(1) then (
Atom.level a.(1), true
) else (
assert (Atom.level a.(0) >= Atom.level a.(1));
assert (Atom.level a.(0) >= base_level env);
max (Atom.level a.(0) - 1) (base_level env), false
)
)
let analyze_loop (env:t) (st:state) : unit =
while st.cs_continue do
begin match st.cs_clause with
| None ->
Log.debug debug "(analyze_conflict.skip_resolution)"
| Some clause ->
Log.debugf debug
(fun k->k "(@[analyze_conflict.resolving@ :clause %a@])" Clause.debug clause);
begin match clause.c_premise with
| P_raw_steps _ | P_steps _ -> bump_clause_activity env clause
| Hyp | Local | Simplify _ | Lemma _ -> ()
end;
st.cs_history <- clause :: st.cs_history;
for j = 0 to Array.length clause.c_atoms - 1 do
let q = clause.c_atoms.(j) in
assert (Atom.is_true q || Atom.is_false q && Atom.level q >= 0);
if Atom.level q <= 0 then (
assert (Atom.level q=0 && Atom.is_false q);
begin match Atom.reason_exn q with
| Bcp cl | Bcp_lazy (lazy cl) ->
st.cs_history <- cl :: st.cs_history
| Eval [] -> ()
| Eval l -> assert (List.for_all (fun t->Term.level t=0) l);
| _ -> assert false
end
);
if not (Term.marked q.a_term) then (
Term.mark q.a_term;
Vec.push (seen env) q.a_term;
if Atom.level q > 0 then (
bump_term_activity env q.a_term;
if Atom.level q >= st.cs_conflict_level then (
st.cs_n_to_analyze <- 1 + st.cs_n_to_analyze;
) else (
st.cs_learnt <- q :: st.cs_learnt;
)
)
)
done
end;
while
let t = Vec.get env.trail st.cs_ptr_trail in
Log.debugf 30 (fun k -> k "(@[conflict_analyze.at_trail_elt@ %a@])" Term.debug t);
begin match t.t_var with
| Var_none -> assert false
| Var_semantic _ -> true
| Var_bool _ ->
not (Term.marked t) || Term.level t < st.cs_conflict_level
end
do
st.cs_ptr_trail <- st.cs_ptr_trail - 1;
done;
let t = Vec.get env.trail st.cs_ptr_trail in
let p = Term.Bool.assigned_atom_exn t in
st.cs_n_to_analyze <- st.cs_n_to_analyze - 1;
st.cs_ptr_trail <- st.cs_ptr_trail - 1;
let reason = Term.reason_exn t in
Log.debugf 30
(fun k->k"(@[<hv>conflict_analyze.check_done:@ %a@ :n_to_analyze %d@ :reason %a@])"
Term.debug t st.cs_n_to_analyze Reason.pp (Term.level t,reason));
begin match st.cs_n_to_analyze, reason with
| 0, _ ->
st.cs_continue <- false;
st.cs_learnt <- Atom.neg p :: st.cs_learnt
| n, Eval _ ->
assert (n > 0);
st.cs_learnt <- Atom.neg p :: st.cs_learnt;
st.cs_clause <- None
| n, (Bcp cl | Bcp_lazy (lazy cl))->
assert (n > 0);
assert (Atom.level p >= st.cs_conflict_level);
st.cs_clause <- Some cl
| _ -> assert false
end
done;
()
let analyze (env:t) (c:clause) : Conflict_res.t =
assert (decision_level env > 0);
let conflict_level = Conflict.level c in
let st = {
cs_n_to_analyze=0;
cs_learnt=[];
cs_continue=true;
cs_clause=Some c;
cs_ptr_trail=(Vec.size env.trail - 1);
cs_history=[];
cs_conflict_level=conflict_level;
} in
Vec.clear (seen env);
Log.debugf 15
(fun k -> k "(@[analyze_conflict (%d/%d)@ :conflict %a@])"
conflict_level (decision_level env) Clause.debug c);
assert (conflict_level >= 0);
analyze_loop env st;
Vec.iter Term.unmark (seen env);
Vec.clear (seen env);
let learnt_a = Array.of_list st.cs_learnt in
put_high_level_atoms_first learnt_a;
Log.debugf debug
(fun k -> k "(@[analyze_conflict.learnt@ %a@])" Clause.debug_atoms_a learnt_a);
let level, is_uip = backtrack_lvl env learnt_a in
{Conflict_res.
cr_backtrack_lvl = level;
cr_learnt = learnt_a;
cr_history = List.rev st.cs_history;
cr_is_uip = is_uip;
}
end
let record_learnt_clause (env:t) (cr:Conflict_res.t): unit =
let open Conflict_res in
begin match cr.cr_learnt with
| [||] ->
let c = Clause.make_arr [||] (Premise.raw_steps_or_simplify cr.cr_history) in
report_unsat env c
| [|fuip|] ->
assert (cr.cr_backtrack_lvl = 0);
env.n_learnt <- env.n_learnt + 1;
let uclause =
Clause.make_arr cr.cr_learnt (Premise.raw_steps_or_simplify cr.cr_history)
|> simplify_clause
in
add_atom env fuip;
if Atom.is_false fuip then (
assert (Atom.level fuip = 0);
report_unsat env uclause
) else (
Vec.push env.clauses_learnt uclause;
Log.debugf debug (fun k->k "(@[learn_clause_0:@ %a@])" Clause.debug uclause);
env.propagations <- env.propagations + 1;
enqueue_bool env fuip ~level:0 (Bcp uclause)
)
| c_learnt ->
let fuip = c_learnt.(0) in
let premise = Premise.raw_steps_or_simplify cr.cr_history in
let lclause = Clause.make_arr c_learnt premise |> simplify_clause in
Vec.push env.clauses_learnt lclause;
Array.iter (add_atom env) lclause.c_atoms;
env.n_learnt <- env.n_learnt + 1;
Log.debugf debug
(fun k->k "(@[learn_clause:@ %a@ :backtrack-lvl %d@])"
Clause.debug lclause cr.cr_backtrack_lvl);
attach_clause env lclause;
bump_clause_activity env lclause;
if cr.cr_is_uip then (
env.propagations <- env.propagations + 1;
enqueue_bool env fuip ~level:cr.cr_backtrack_lvl (Bcp lclause)
) else (
env.next_decision <- Some (Atom.neg fuip)
)
end;
var_decay_activity env;
clause_decay_activity env
let add_conflict (env:t) (confl:clause): unit =
Log.debugf info (fun k -> k"@{<Yellow>## add_conflict@}: %a" Clause.debug confl);
env.next_decision <- None;
env.conflicts <- env.conflicts + 1;
assert (decision_level env >= base_level env);
if decision_level env = base_level env ||
CCArray.for_all
(fun a -> Atom.level a <= base_level env)
confl.c_atoms
then (
report_unsat env confl
);
let cr = Analyze.analyze env confl in
cancel_until env (max cr.Conflict_res.cr_backtrack_lvl (base_level env));
record_learnt_clause env cr
let[@unrolled 1] rec vec_to_insert_clause_into env c = match c.c_premise with
| Hyp -> env.clauses_hyps
| Local -> env.clauses_temp
| Simplify d -> vec_to_insert_clause_into env d
| Lemma _ | P_raw_steps _ | P_steps _ -> env.clauses_learnt
let add_clause (env:t) (c0:clause) : unit =
Log.debugf debug (fun k -> k "(@[solver.add_clause@ %a@])" Clause.debug c0);
let vec = vec_to_insert_clause_into env c0 in
try
Array.iter (add_atom env) c0.c_atoms;
let c = simplify_clause c0 in
let atoms, history = partition_atoms c.c_atoms in
let clause =
if history = []
then (
List.iteri (fun i a -> c.c_atoms.(i) <- a) atoms;
c
) else (
Clause.make atoms (Premise.raw_steps (c :: history))
)
in
Log.debugf info (fun k->k "(@{<green>solver.new_clause@}@ %a@])" Clause.debug clause);
begin match atoms with
| [] ->
Stack.push clause env.clauses_root;
report_unsat env clause
| [a] ->
cancel_until env (base_level env);
if Atom.is_false a then (
Log.debug debug "(solver.add_clause: unit_clause adding to clauses to add)";
Stack.push clause env.clauses_to_add;
report_unsat env clause
) else if Atom.is_true a then (
Log.debug debug "(solver.add_clause: unit clause, adding to root clauses)";
assert (0 < Atom.level a && Atom.level a <= base_level env);
Stack.push clause env.clauses_root;
()
) else (
Log.debugf debug
(fun k->k "(@[solver.add_clause: unit clause, propagating@ :atom %a@])" Atom.debug a);
Vec.push vec clause;
env.propagations <- env.propagations + 1;
enqueue_bool env a ~level:0 (Bcp clause)
)
| a::b::_ ->
Vec.push vec clause;
if Atom.is_false a then (
let ats = clause.c_atoms in
put_high_level_atoms_first ats;
assert(Atom.level ats.(0) >= Atom.level ats.(1));
attach_clause env clause;
add_conflict env clause
) else (
attach_clause env clause;
if Atom.is_false b && Atom.is_undef a then (
let lvl = List.fold_left (fun m a -> max m (Atom.level a)) 0 atoms in
cancel_until env (max lvl (base_level env));
env.propagations <- env.propagations + 1;
enqueue_bool env a ~level:lvl (Bcp clause)
)
)
end
with Trivial ->
Log.debugf info
(fun k->k "(@[solver.add_clause: trivial clause ignored@ :c %a@])" Clause.debug c0);
()
let flush_clauses (env:t) =
if not (Stack.is_empty env.clauses_to_add) then (
let nbc = env.nb_init_clauses + Stack.length env.clauses_to_add in
env.nb_init_clauses <- nbc;
while not (Stack.is_empty env.clauses_to_add) do
let c = Stack.pop env.clauses_to_add in
add_clause env c
done
)
let propagate_in_clause (env:t) (a:atom) (c:clause) : watch_res =
let atoms = c.c_atoms in
assert (Array.length c.c_atoms >= 2);
let first = Array.unsafe_get atoms 0 in
if first == Atom.neg a then (
Array.unsafe_set atoms 0 (Array.unsafe_get atoms 1);
Array.unsafe_set atoms 1 first;
) else (
assert (Atom.neg a == atoms.(1));
);
let first = Array.unsafe_get atoms 0 in
if Atom.is_true first
then Watch_keep
else (
try
for k = 2 to Array.length atoms - 1 do
let ak = Array.unsafe_get atoms k in
if not (Atom.is_false ak) then (
Array.unsafe_set atoms 1 ak;
Array.unsafe_set atoms k (Atom.neg a);
Vec.push (Atom.neg ak).a_watched c;
raise_notrace Exit
)
done;
if Atom.is_false first then (
env.bcp_head <- Vec.size env.trail;
raise_notrace (Conflict c)
) else (
begin match th_eval env first with
| None ->
env.propagations <- env.propagations + 1;
Log.debugf 30
(fun k->k
"(@[<hv>solver.propagate_in_clause.@{<yellow>propagate_bool@}@ %a@ :in-clause %a@])"
Atom.debug first Clause.debug c);
enqueue_bool env first ~level:(decision_level env) (Bcp c)
| Some V_true -> ()
| Some V_false ->
env.bcp_head <- Vec.size env.trail;
raise_notrace (Conflict c)
| Some _ -> assert false
end
);
Watch_keep
with Exit ->
Watch_remove
)
let propagate_atom (env:t) (a:atom) : unit =
let watched = a.a_watched in
let i = ref 0 in
while !i < Vec.size watched do
let c = Vec.get watched !i in
assert (Clause.attached c);
if Clause.deleted c
then Vec.fast_remove watched !i
else begin match propagate_in_clause env a c with
| Watch_keep -> incr i
| Watch_remove ->
Vec.fast_remove watched !i;
end
done
let[@inline] propagate_in_watching_term (env:t) (t:term) ~watch =
t.t_tc.tct_update_watches (actions env) t ~watch
let propagate_term_real (env:t) (t:term) watched: unit =
let i = ref 0 in
while !i < Vec.size watched do
let u = Vec.get watched !i in
assert (Term.is_added u);
if Term.is_deleted u
then Vec.fast_remove watched !i
else begin match propagate_in_watching_term env u ~watch:t with
| Watch_keep -> incr i
| Watch_remove ->
Vec.fast_remove watched !i;
end
done
let[@inline] propagate_term (env:t) (t:term) : unit =
if Vec.size t.t_watches > 0 then (
propagate_term_real env t t.t_watches
)
let rec theory_propagate (env:t) : clause option =
assert (env.bcp_head <= Vec.size env.trail);
if env.th_head = Vec.size env.trail then (
if env.bcp_head = Vec.size env.trail then (
None
) else (
propagate env
)
) else (
let t = Vec.get env.trail env.th_head in
env.th_head <- env.th_head + 1;
begin match propagate_term env t with
| () -> theory_propagate env
| exception (Conflict c) -> Some c
end
)
and bool_propagate (env:t) : clause option =
if env.bcp_head = Vec.size env.trail then (
theory_propagate env
) else (
let t = Vec.get env.trail env.bcp_head in
env.bcp_head <- env.bcp_head + 1;
begin match t.t_var with
| Var_none -> assert false
| Var_semantic _ -> bool_propagate env
| Var_bool _ ->
env.propagations <- env.propagations + 1;
let a = Term.Bool.assigned_atom_exn t in
begin match propagate_atom env a with
| () -> bool_propagate env
| exception Conflict c -> Some c
end
end
)
and propagate (env:t) : clause option =
flush_clauses env;
assert (env.bcp_head <= Vec.size env.trail);
bool_propagate env
module Actions : sig
val make : t -> actions
end = struct
let[@inline] on_backtrack (env:t) (f:unit->unit) : unit =
Vec.push env.backtrack_stack f
let raise_conflict (env:t) (atoms:atom list) (lemma:lemma) : 'a =
Log.debugf debug (fun k->k
"(@[<hv>@{<yellow>raise_conflict@}@ :clause %a@ :lemma %a@])"
Clause.debug_atoms atoms Lemma.pp lemma);
env.bcp_head <- Vec.size env.trail;
env.th_head <- Vec.size env.trail;
let atoms =
Atom.Set.of_list atoms
|> Atom.Set.to_list
|> List.filter (fun a -> not (Atom.is_absurd a))
in
List.iter
(fun a ->
add_atom env a;
eval_atom_to_false ~save:true env a)
atoms;
let c = Clause.make atoms (Lemma lemma) in
raise (Conflict c)
let propagate_bool_eval (env:t) t (b:bool) ~(subs:term list) : unit =
Log.debugf 5
(fun k->k
"(@[<hv>solver.@{<yellow>semantic_propagate_bool@}@ %a@ :val %B@ :subs %a@])"
Term.debug t b pp_subs subs);
let a = if b then Term.Bool.pa_unsafe t else Term.Bool.na_unsafe t in
enqueue_semantic_bool_eval env a subs
let propagate_bool_lemma (env:t) t (v:bool) atoms lemma : unit =
let a = if v then Term.Bool.pa_unsafe t else Term.Bool.na_unsafe t in
let lvl = List.fold_left
(fun lvl b ->
if not (Atom.equal a b) then (
add_atom env b;
eval_atom_to_false ~save:true env b;
max lvl (Atom.level b)
) else lvl)
0 atoms
in
Log.debugf 5
(fun k->k "(@[<hv>solver.@{<yellow>theory_propagate_bool@}@ %a@ :val %B@ :lvl %d@ :clause %a@])"
Term.debug t v lvl Clause.debug_atoms atoms);
enqueue_bool_theory_propagate env a ~lvl atoms lemma
let make (env:t) : actions =
let act_level (): level = decision_level env
and act_push_clause (c:clause) : unit =
Log.debugf debug
(fun k->k "(@[solver.@{<yellow>push_clause@}@ %a@])" Clause.debug c);
Stack.push c env.clauses_to_add
in
{ act_on_backtrack=on_backtrack env;
act_push_clause;
act_level;
act_raise_conflict=raise_conflict env;
act_propagate_bool_eval=propagate_bool_eval env;
act_propagate_bool_lemma=propagate_bool_lemma env;
}
end
let create () : t =
let rec env = lazy (create_real actions)
and actions = lazy (Actions.make (Lazy.force env)) in
let env = Lazy.force env in
ignore (add_plugin env Builtins.plugin);
env
let rec pick_branch_aux (env:t) (atom:atom) : unit =
let t = atom.a_term in
if Term.has_some_value t then (
assert (not (Atom.is_undef atom));
pick_branch_lit env
) else (
begin match Term.eval t with
| Eval_unknown ->
env.decisions <- env.decisions + 1;
env.bool_decisions <- env.bool_decisions + 1;
new_decision_level env;
Log.debugf debug (fun k->k "(@[solver.bool_decide@ %a@])" Atom.debug atom);
let current_level = decision_level env in
enqueue_bool env atom ~level:current_level Decision
| Eval_into (b, l) ->
let a =
if Value.is_true b then atom
else (
assert (Value.is_false b);
Atom.neg atom
)
in
enqueue_semantic_bool_eval env a l
end
)
and pick_branch_lit (env:t) : unit =
begin match env.next_decision with
| Some atom ->
env.next_decision <- None;
pick_branch_aux env atom
| None ->
if H.is_empty env.term_heap then (
raise Sat
) else (
let t = H.remove_min env.term_heap in
if Term.is_deleted t then pick_branch_lit env
else begin match t.t_var with
| Var_none -> assert false
| Var_bool {pa; _} ->
pick_branch_aux env pa
| Var_semantic _ ->
if Term.has_some_value t then (
pick_branch_lit env
) else (
let value = decide_term env t in
env.decisions <- env.decisions + 1;
new_decision_level env;
let current_level = decision_level env in
enqueue_assign env t value Decision ~level:current_level
)
end
)
end
let rec gc_mark_clause (c:clause) : unit =
if not (Clause.gc_marked c) then (
Log.debugf 15 (fun k->k "(@[gc_mark_clause@ %a@])" Clause.pp_name c);
Clause.gc_mark c;
Array.iter (gc_mark_atom ~mark_clause:true) c.c_atoms;
)
and gc_mark_term ~mark_clause (t:term) : unit =
if not (Term.gc_marked t) then (
Term.gc_mark t;
Term.iter_subterms t (gc_mark_term ~mark_clause);
if mark_clause then begin match Term.reason t with
| Some (Bcp c) ->
if Clause.attached c then (
gc_mark_clause c
)
| Some (Bcp_lazy c) when Lazy.is_val c ->
let lazy c = c in
if Clause.attached c then (
gc_mark_clause c
);
| _ -> ()
end;
)
and[@inline] gc_mark_atom ~mark_clause (a:atom) =
gc_mark_term ~mark_clause (Atom.term a)
let gc_clauses (env:t) ~down_to : unit =
Log.debugf 2 (fun k->k"@{<Yellow>## gc_clauses@}");
assert (Stack.is_empty env.clauses_to_add);
env.n_gc_c <- env.n_gc_c + 1;
let n_clauses = Vec.size env.clauses_learnt in
assert (down_to <= n_clauses);
Log.debugf 4
(fun k->k"(@[gc_clauses.remove_learnt@ :n_total %d@ :downto %d@])" n_clauses down_to);
Vec.iter (gc_mark_term ~mark_clause:true) env.trail;
Stack.iter gc_mark_clause env.clauses_root;
Vec.iter gc_mark_clause env.clauses_hyps;
Vec.iter gc_mark_clause env.clauses_temp;
Vec.sort env.clauses_learnt
(fun c1 c2 -> CCFloat.compare (Clause.activity c2)(Clause.activity c1));
let kept_clauses = Vec.create() in
while Vec.size env.clauses_learnt > 0 &&
Vec.size env.clauses_learnt + Vec.size kept_clauses > down_to do
let c = Vec.pop env.clauses_learnt in
if Clause.gc_marked c then (
Vec.push kept_clauses c;
) else (
Log.debugf 15 (fun k->k"(@[gc_clauses.remove_clause@ %a@ :activity %f@])"
Clause.debug c (Clause.activity c));
Clause.set_deleted c;
env.n_deleted_c <- env.n_deleted_c + 1;
)
done;
Vec.append env.clauses_learnt kept_clauses;
Vec.iter gc_mark_clause env.clauses_learnt;
CCVector.iter
(fun (module P : Plugin.S) ->
let n = P.gc_all() in
env.n_deleted_t <- env.n_deleted_t + n)
env.plugins;
Stack.iter Clause.gc_unmark env.clauses_root;
Vec.iter Clause.gc_unmark env.clauses_temp;
Vec.iter Clause.gc_unmark env.clauses_hyps;
Vec.iter Clause.gc_unmark env.clauses_learnt;
()
let gc_terms (env:t) : unit =
Log.debugf 2 (fun k->k"@{<Yellow>## gc_terms@}");
env.n_gc_t <- env.n_gc_t + 1;
assert (Stack.is_empty env.clauses_to_add);
Vec.iter (gc_mark_term ~mark_clause:false) env.trail;
let f_clause c = Array.iter (gc_mark_atom ~mark_clause:false) c.c_atoms in
Stack.iter f_clause env.clauses_root;
Stack.iter f_clause env.clauses_to_add;
Vec.iter f_clause env.clauses_hyps;
Vec.iter f_clause env.clauses_temp;
Vec.iter f_clause env.clauses_learnt;
CCVector.iter
(fun (module P : Plugin.S) ->
let n = P.gc_all() in
env.n_deleted_t <- env.n_deleted_t + n)
env.plugins;
()
let check_limits ~time ~memory () =
let t = Sys.time () in
let heap_size = (Gc.quick_stat ()).Gc.heap_words in
let s = float heap_size *. float Sys.word_size /. 8. in
if t > time then (
raise Out_of_time
) else if s > memory then (
raise Out_of_space
)
let pp_progress (env:t) : unit =
Printf.printf "\r\027[K[%.2fs] [start %d|confl %d|decision %d|props %d|\
gc_c %d|del_c %d|gc_t %d|del_t %d]%!"
(Sys.time ()) env.starts env.conflicts env.decisions env.propagations
env.n_gc_c env.n_deleted_c env.n_gc_t env.n_deleted_t
let search (env:t) ~gc ~time ~memory ~progress ?switch n_of_conflicts : unit =
Log.debugf 5
(fun k->k "(@[@{<yellow>solver.search@}@ :nconflicts %d@])" n_of_conflicts);
let conflictC = ref 0 in
env.starts <- env.starts + 1;
while true do
Log.debugf 50
(fun k->k "(@[cur-heap@ %a@])" (Util.pp_iter Term.pp) @@ H.to_iter env.term_heap);
begin match propagate env with
| Some confl ->
incr conflictC;
add_conflict env confl
| None ->
assert (fully_propagated env);
if H.is_empty env.term_heap then (
Log.debugf 3 (fun k->k"@{<yellow>found SAT@}");
raise Sat;
);
if n_of_conflicts > 0 && !conflictC >= n_of_conflicts then (
Log.debug info "Restarting…";
cancel_until env (base_level env);
raise Restart
);
if Util.Switch.activated_opt switch then raise Out_of_time;
if env.conflicts = ((env.conflicts lsr 8) lsl 8) then (
if progress then pp_progress env;
check_limits ~time ~memory ();
if gc &&
env.conflicts > 0 &&
env.conflicts = ((env.conflicts lsr 13) lsl 13) then (
gc_terms env;
)
);
pick_branch_lit env
end
done
let eval_level (a:atom) =
let lvl = Atom.level a in
if Atom.is_true a then true, lvl
else if Atom.is_false a then false, lvl
else (
begin match Atom.eval a with
| Eval_unknown -> err_undecided_lit a.a_term
| Eval_into(b, l) ->
let lvl = level_subs l in
Value.as_bool_exn b, lvl
end
)
let[@inline] eval a = fst (eval_level a)
let[@inline] unsat_conflict (env:t) = env.unsat_conflict
let model (env:t) : assignment_view list =
Vec.fold
(fun acc t -> match Term.value t with
| None -> assert false
| Some value ->
let asgt = match Value.as_bool value with
| Some b -> A_bool (t, b)
| None -> A_semantic (t, value)
in
asgt :: acc )
[] env.trail
type final_check_res =
| FC_sat
| FC_propagate
| FC_conflict of clause
let final_check (env:t) : final_check_res =
try
CCVector.iter
(fun (module P : Plugin.S) ->
begin match P.check_if_sat (actions env) with
| Sat -> ()
| Unsat (l,p) ->
List.iter (add_atom env) l;
let c = Clause.make l (Lemma p) in
raise (Conflict c)
end)
env.plugins;
if fully_propagated env && Stack.is_empty env.clauses_to_add
then FC_sat
else FC_propagate
with Conflict c ->
FC_conflict c
let solve
?(gc=true)
?(restarts=true)
?(time=max_float)
?(memory=max_float)
?(progress=false)
?switch
(env:t)
: unit =
Log.debugf 2 (fun k->k"@{<Green>#### Solve@}");
if is_unsat env then (
raise Unsat;
);
let _alarm_progress =
if progress then Some (Gc.create_alarm (fun () -> pp_progress env)) else None
in
let n_of_conflicts = ref (to_float env.restart_first) in
let n_of_learnts =
ref (CCFloat.max 50. ((to_float (nb_clauses env)) *. env.learntsize_factor))
in
let rec loop () =
if Util.Switch.activated_opt switch then raise Out_of_time
else (
match
let nconf = if restarts then to_int !n_of_conflicts else max_int in
search env ~gc ~time ~memory ~progress ?switch nconf
with
| () -> ()
| exception Restart ->
if gc &&
!n_of_learnts >= 0. &&
float(Vec.size env.clauses_learnt - Vec.size env.trail) >= !n_of_learnts
then (
let n = (to_int !n_of_learnts) + 1 in
gc_clauses env ~down_to:n;
);
n_of_conflicts := !n_of_conflicts *. env.restart_inc;
n_of_learnts := !n_of_learnts *. env.learntsize_inc;
env.learntsize_inc <- 1. +. (env.learntsize_inc -. 1.) /. 1.3 ;
loop()
| exception Sat -> check_sat ()
)
and check_sat () =
assert (fully_propagated env);
begin match final_check env with
| FC_sat -> ()
| FC_conflict c ->
Log.debugf info
(fun k -> k "(@[solver.theory_conflict_clause@ %a@])" Clause.debug c);
Stack.push c env.clauses_to_add;
loop()
| FC_propagate ->
loop()
end
in
CCFun.finally
~h:(fun () -> CCOpt.iter Gc.delete_alarm _alarm_progress)
~f:loop
let assume env ?tag (cnf:atom list list) =
List.iter
(fun l ->
let c = Clause.make ?tag l Hyp in
Log.debugf debug (fun k->k "(@[solver.assume_clause@ %a@])" Clause.debug c);
Stack.push c env.clauses_to_add)
cnf
let push (env:t) : unit =
Log.debug debug "(solver.push)";
cancel_until env (base_level env);
Log.debugf 30
(fun k->k"(@[solver.push.status@ :prop_head %d/%d@ :trail (@[<hv>%a@])@])"
env.bcp_head env.th_head (Vec.pp ~sep:"" Term.debug) env.trail);
begin match propagate env with
| Some c -> report_unsat env c
| None ->
Log.debugf 30
(fun k -> k "(@[<v>solver.current_trail@ (@[<hv>%a@])@])"
(Vec.pp ~sep:"" Term.debug) env.trail);
new_decision_level env;
Log.debugf info
(fun k->k"(@[<hv>solver.create_new_user_level@ :cur-level %d@])" (decision_level env));
Vec.push env.user_levels (Vec.size env.clauses_temp);
assert (decision_level env = base_level env)
end
let pop (env:t) : unit =
if base_level env = 0 then (
Log.debug warn "Cannot pop (already at level 0)";
) else (
Log.debug info "(solver.pop)";
assert (base_level env > 0);
env.unsat_conflict <- None;
let n = Vec.pop env.user_levels in
Stack.iter (fun c -> Stack.push c env.clauses_to_add) env.clauses_root;
Stack.clear env.clauses_root;
Vec.shrink env.clauses_temp n;
assert (Vec.for_all (fun c -> Array.length c.c_atoms = 1) env.clauses_temp);
assert (Vec.for_all (fun c -> Atom.level c.c_atoms.(0) <= base_level env) env.clauses_temp);
cancel_until env (base_level env)
)
let local (env:t) (l:atom list) : unit =
let aux a =
Log.debugf info (fun k-> k "Local assumption: @[%a@]" Atom.debug a);
assert (decision_level env = base_level env);
if Atom.is_true a then ()
else (
let c = Clause.make [a] Local in
Log.debugf debug (fun k -> k "Temp clause: @[%a@]" Clause.debug c);
Vec.push env.clauses_temp c;
if Atom.is_false a then (
report_unsat env c;
) else (
let level = decision_level env in
enqueue_bool env a ~level (Bcp c);
)
)
in
assert (base_level env >= 0);
if base_level env = 0 then (
invalid_arg "Solver.local: need to `push` before";
);
begin match env.unsat_conflict with
| None ->
Log.debug info "Adding local assumption";
cancel_until env (base_level env);
List.iter aux l
| Some _ ->
Log.debug warn "Cannot add local assumption (already unsat)"
end
exception Bad_model of string
let bad_model msg = raise (Bad_model msg)
let bad_modelf msg = Fmt.ksprintf ~f:bad_model msg
let check_clause (c:clause) : unit =
let res = CCArray.exists Atom.is_true c.c_atoms in
if not res then (
bad_modelf "Clause not satisfied: @[<hov>%a@]" Clause.debug c
)
let check_term (t:term) : unit = match Term.value t, Term.eval t with
| None, Eval_unknown ->
()
| None, Eval_into _ -> ()
| Some _, Eval_unknown -> ()
| Some v1, Eval_into (v2,_) ->
if not (Value.equal v1 v2) then (
bad_modelf "@[<hv>inconsistency: term `%a`@ :assigned-to %a@ :eval-to %a@]"
Term.debug t Value.pp v1 Value.pp v2
)
let[@inline] check_terms (env:t) = iter_terms env check_term
let[@inline] check_vec v = Vec.iter check_clause v
let[@inline] check_stack s = Stack.iter check_clause s
let check (env:t) : (unit,string) result =
try
assert (Stack.is_empty env.clauses_to_add);
check_stack env.clauses_root;
check_vec env.clauses_hyps;
check_vec env.clauses_learnt;
check_vec env.clauses_temp;
check_terms env;
Ok ()
with Bad_model msg -> Error msg
let hyps env = env.clauses_hyps
let history env = env.clauses_learnt
let temp env = env.clauses_temp
let trail env = env.trail
let pp_stats out (s:t): unit =
Fmt.fprintf out
"(@[stats@ :n_conflicts %d@ \
:n_decisions %d@ :n_bool_decisions %d@ :n_propagations %d@ :n_restarts %d@ \
:n_learnt %d@ :n_initial %d@ \
@[:gc_c %d@ :deleted_c %d@]@ \
@[:gc_t %d :deleted_t %d@]@])"
s.conflicts s.decisions s.bool_decisions s.propagations s.starts s.n_learnt
(Vec.size s.clauses_hyps) s.n_gc_c s.n_deleted_c s.n_gc_t s.n_deleted_t
let[@inline] clear_progress () = print_string "\r\027[K";