Source file superposition.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
open Logtk
open Libzipperposition
module BV = CCBV
module T = Term
module O = Ordering
module S = Subst
module Lit = Literal
module Lits = Literals
module Comp = Comparison
module US = Unif_subst
let section = Util.Section.make ~parent:Const.section "sup"
let flag_simplified = SClause.new_flag()
module type S = Superposition_intf.S
let stat_basic_simplify_calls = Util.mk_stat "sup.basic_simplify calls"
let stat_basic_simplify = Util.mk_stat "sup.basic_simplify"
let stat_superposition_call = Util.mk_stat "sup.superposition calls"
let stat_equality_resolution_call = Util.mk_stat "sup.equality_resolution calls"
let stat_equality_factoring_call = Util.mk_stat "sup.equality_factoring calls"
let stat_subsumption_call = Util.mk_stat "sup.subsumption_calls"
let stat_eq_subsumption_call = Util.mk_stat "sup.equality_subsumption calls"
let stat_eq_subsumption_success = Util.mk_stat "sup.equality_subsumption success"
let stat_subsumed_in_active_set_call = Util.mk_stat "sup.subsumed_in_active_set calls"
let stat_subsumed_by_active_set_call = Util.mk_stat "sup.subsumed_by_active_set calls"
let stat_clauses_subsumed = Util.mk_stat "sup.num_clauses_subsumed"
let stat_demodulate_call = Util.mk_stat "sup.demodulate calls"
let stat_demodulate_step = Util.mk_stat "sup.demodulate steps"
let stat_semantic_tautology = Util.mk_stat "sup.semantic_tautologies"
let stat_condensation = Util.mk_stat "sup.condensation"
let stat_clc = Util.mk_stat "sup.clc"
let prof_demodulate = Util.mk_profiler "sup.demodulate"
let prof_back_demodulate = Util.mk_profiler "sup.backward_demodulate"
let prof_pos_simplify_reflect = Util.mk_profiler "sup.simplify_reflect+"
let prof_neg_simplify_reflect = Util.mk_profiler "sup.simplify_reflect-"
let prof_clc = Util.mk_profiler "sup.contextual_literal_cutting"
let prof_semantic_tautology = Util.mk_profiler "sup.semantic_tautology"
let prof_condensation = Util.mk_profiler "sup.condensation"
let prof_basic_simplify = Util.mk_profiler "sup.basic_simplify"
let prof_subsumption = Util.mk_profiler "sup.subsumption"
let prof_eq_subsumption = Util.mk_profiler "sup.equality_subsumption"
let prof_subsumption_set = Util.mk_profiler "sup.forward_subsumption"
let prof_subsumption_in_set = Util.mk_profiler "sup.backward_subsumption"
let prof_infer_active = Util.mk_profiler "sup.infer_active"
let prof_infer_passive = Util.mk_profiler "sup.infer_passive"
let prof_infer_equality_resolution = Util.mk_profiler "sup.infer_equality_resolution"
let prof_infer_equality_factoring = Util.mk_profiler "sup.infer_equality_factoring"
let _use_semantic_tauto = ref true
let _use_simultaneous_sup = ref true
let _dot_sup_into = ref None
let _dot_sup_from = ref None
let _dot_simpl = ref None
let _dont_simplify = ref false
let _sup_at_vars = ref false
let _restrict_hidden_sup_at_vars = ref false
let _dot_demod_into = ref None
module Make(Env : Env.S) : S with module Env = Env = struct
module Env = Env
module Ctx = Env.Ctx
module C = Env.C
module PS = Env.ProofState
module I = PS.TermIndex
module TermIndex = PS.TermIndex
module SubsumIdx = PS.SubsumptionIndex
module UnitIdx = PS.UnitIndex
(** {6 Index Management} *)
let _idx_sup_into = ref (TermIndex.empty ())
let _idx_sup_from = ref (TermIndex.empty ())
let _idx_back_demod = ref (TermIndex.empty ())
let _idx_fv = ref (SubsumIdx.empty ())
let _idx_simpl = ref (UnitIdx.empty ())
let idx_sup_into () = !_idx_sup_into
let idx_sup_from () = !_idx_sup_from
let idx_fv () = !_idx_fv
let _update_active f c =
let ord = Ctx.ord () in
_idx_sup_into :=
Lits.fold_terms ~vars:!_sup_at_vars ~ty_args:false ~ord ~which:`Max ~subterms:true
~eligible:(C.Eligible.res c) (C.lits c)
|> Iter.filter (fun (t, _) -> not (T.is_var t) || T.is_ho_var t)
|> Iter.fold
(fun tree (t, pos) ->
let with_pos = C.WithPos.({term=t; pos; clause=c;}) in
f tree t with_pos)
!_idx_sup_into;
_idx_sup_from :=
Lits.fold_eqn ~ord ~both:true ~sign:true
~eligible:(C.Eligible.param c) (C.lits c)
|> Iter.fold
(fun tree (l, _, sign, pos) ->
assert sign;
let with_pos = C.WithPos.({term=l; pos; clause=c;}) in
f tree l with_pos)
!_idx_sup_from ;
_idx_back_demod :=
Lits.fold_terms ~vars:false ~ty_args:false ~ord ~subterms:true ~which:`All
~eligible:C.Eligible.always (C.lits c)
|> Iter.fold
(fun tree (t, pos) ->
let with_pos = C.WithPos.( {term=t; pos; clause=c} ) in
f tree t with_pos)
!_idx_back_demod;
Signal.ContinueListening
let _update_simpl f c =
let ord = Ctx.ord () in
let idx = !_idx_simpl in
let idx' = match C.lits c with
| [| Lit.Equation (l,r,true) |] ->
begin match Ordering.compare ord l r with
| Comparison.Gt ->
f idx (l,r,true,c)
| Comparison.Lt ->
f idx (r,l,true,c)
| Comparison.Incomparable ->
let idx = f idx (l,r,true,c) in
f idx (r,l,true,c)
| Comparison.Eq -> idx
end
| [| Lit.Equation (l,r,false) |] ->
f idx (l,r,false,c)
| [| Lit.Prop (p, sign) |] ->
f idx (p,T.true_,sign,c)
| _ -> idx
in
_idx_simpl := idx';
Signal.ContinueListening
let () =
Signal.on PS.ActiveSet.on_add_clause
(fun c ->
_idx_fv := SubsumIdx.add !_idx_fv c;
_update_active TermIndex.add c);
Signal.on PS.ActiveSet.on_remove_clause
(fun c ->
_idx_fv := SubsumIdx.remove !_idx_fv c;
_update_active TermIndex.remove c);
Signal.on PS.SimplSet.on_add_clause
(_update_simpl UnitIdx.add);
Signal.on PS.SimplSet.on_remove_clause
(_update_simpl UnitIdx.remove);
()
(** {6 Inference Rules} *)
module SupInfo = struct
type t = {
active : C.t;
active_pos : Position.t;
scope_active : int;
s : T.t;
t : T.t;
passive : C.t;
passive_pos : Position.t;
passive_lit : Lit.t;
scope_passive : int;
u_p : T.t;
subst : US.t;
}
end
exception ExitSuperposition of string
let is_hidden_sup_at_var info =
let open SupInfo in
let active_idx = Lits.Pos.idx info.active_pos in
begin match T.view info.u_p with
| T.App (head, args) ->
begin match T.as_var head with
| Some _ ->
begin match T.view info.s, T.view info.t with
| T.App (f, ss), T.App (g, tt) ->
let s_args = Array.of_list ss in
let t_args = Array.of_list tt in
if
Array.length s_args >= List.length args
&& Array.length t_args >= List.length args
&& Array.sub s_args (Array.length s_args - List.length args) (List.length args) =
Array.sub t_args (Array.length t_args - List.length args) (List.length args)
&& CCList.(Array.length s_args - List.length args --^ Array.length s_args)
|> List.for_all (fun idx ->
match T.as_var (Array.get s_args idx) with
| Some v ->
not (CCArray.exists (T.var_occurs ~var:v) (Array.sub s_args 0 idx))
&& not (CCArray.exists (T.var_occurs ~var:v) (Array.sub t_args 0 (Array.length t_args - List.length args))
&& not (T.var_occurs ~var:v f)
&& not (T.var_occurs ~var:v g)
&& not (List.exists (Literal.var_occurs v) (CCArray.except_idx (C.lits info.active) active_idx)))
| None -> false
)
then
let t_prefix = T.app g (Array.to_list (Array.sub t_args 0 (Array.length t_args - List.length args))) in
Some (head, t_prefix)
else
None
| _ -> None
end
| None -> None
end
| _ -> None
end
let sup_at_var_condition info var replacement =
let open SupInfo in
let ord = Ctx.ord () in
let us = info.subst in
let subst = US.subst us in
let renaming = S.Renaming.create () in
let replacement' = S.FO.apply renaming subst (replacement, info.scope_active) in
let var' = S.FO.apply renaming subst (var, info.scope_passive) in
if (not (Type.is_fun (Term.ty var')) || not (O.might_flip ord var' replacement'))
then (
Util.debugf ~section 5
"Cannot flip: %a = %a"
(fun k->k T.pp var' T.pp replacement');
false
)
else (
let unique_args_of_var =
C.lits info.passive
|> Lits.fold_terms ~vars:true ~ty_args:false ~which:`All ~ord ~subterms:true ~eligible:(fun _ _ -> true)
|> Iter.fold_while
(fun unique_args (t,_) ->
if Head.term_to_head t == Head.term_to_head var
then (
if unique_args == Some (Head.term_to_args t)
then (unique_args, `Continue)
else (None, `Stop)
) else (unique_args, `Continue)
)
None
in
match unique_args_of_var with
| Some _ ->
Util.debugf ~section 5
"Variable %a has same args everywhere in %a"
(fun k->k T.pp var C.pp info.passive);
false
| None ->
let passive'_lits = Lits.apply_subst renaming subst (C.lits info.passive, info.scope_passive) in
let subst_t = Unif.FO.update subst (T.as_var_exn var, info.scope_passive) (replacement, info.scope_active) in
let passive_t'_lits = Lits.apply_subst renaming subst_t (C.lits info.passive, info.scope_passive) in
if Lits.compare_multiset ~ord passive'_lits passive_t'_lits = Comp.Gt
then (
Util.debugf ~section 5
"Sup at var condition is not fulfilled because: %a >= %a"
(fun k->k Lits.pp passive'_lits Lits.pp passive_t'_lits);
false
)
else true
)
let do_classic_superposition info acc =
let ord = Ctx.ord () in
let open SupInfo in
let module P = Position in
Util.incr_stat stat_superposition_call;
let sc_a = info.scope_active in
let sc_p = info.scope_passive in
Util.debugf ~section 3
"@[<2>sup@ (@[<2>%a[%d]@ @[s=%a@]@ @[t=%a@]@])@ \
(@[<2>%a[%d]@ @[passive_lit=%a@]@ @[p=%a@]@])@ with subst=@[%a@]@]"
(fun k->k C.pp info.active sc_a T.pp info.s T.pp info.t
C.pp info.passive sc_p Lit.pp info.passive_lit
Position.pp info.passive_pos US.pp info.subst);
assert (InnerTerm.DB.closed (info.s:>InnerTerm.t));
assert (InnerTerm.DB.closed (info.u_p:T.t:>InnerTerm.t));
assert (not(T.is_var info.u_p) || T.is_ho_var info.u_p);
let active_idx = Lits.Pos.idx info.active_pos in
let passive_idx, passive_lit_pos = Lits.Pos.cut info.passive_pos in
try
let renaming = S.Renaming.create () in
let us = info.subst in
let subst = US.subst us in
let t' = S.FO.apply renaming subst (info.t, sc_a) in
begin match info.passive_lit, info.passive_pos with
| Lit.Prop (_, true), P.Arg(_, P.Left P.Stop) ->
if T.equal t' T.true_
then raise (ExitSuperposition "will yield a bool tautology")
| Lit.Equation (_, v, true), P.Arg(_, P.Left P.Stop)
| Lit.Equation (v, _, true), P.Arg(_, P.Right P.Stop) ->
let v' = S.FO.apply renaming subst (v, sc_p) in
if T.equal t' v'
then raise (ExitSuperposition "will yield a tautology");
| _ -> ()
end;
let passive_lit' = Lit.apply_subst_no_simp renaming subst (info.passive_lit, sc_p) in
let new_trail = C.trail_l [info.active; info.passive] in
if Env.is_trivial_trail new_trail then raise (ExitSuperposition "trivial trail");
let s' = S.FO.apply renaming subst (info.s, sc_a) in
if (
O.compare ord s' t' = Comp.Lt ||
not (Lit.Pos.is_max_term ~ord passive_lit' passive_lit_pos) ||
not (BV.get (C.eligible_res (info.passive, sc_p) subst) passive_idx) ||
not (C.is_eligible_param (info.active, sc_a) subst ~idx:active_idx)
) then raise (ExitSuperposition "bad ordering conditions");
if not !_sup_at_vars then
assert (not (T.is_var info.u_p))
else if T.is_var info.u_p && not (sup_at_var_condition info info.u_p info.t) then
raise (ExitSuperposition "superposition at variable");
if !_restrict_hidden_sup_at_vars then (
match is_hidden_sup_at_var info with
| Some (var,replacement) when not (!_sup_at_vars && sup_at_var_condition info var replacement)
-> raise (ExitSuperposition "hidden superposition at variable")
| _ -> ()
);
let lits_a = CCArray.except_idx (C.lits info.active) active_idx in
let lits_p = CCArray.except_idx (C.lits info.passive) passive_idx in
let new_passive_lit =
Lit.Pos.replace passive_lit'
~at:passive_lit_pos ~by:t' in
let c_guard = Literal.of_unif_subst renaming us in
let tags = Unif_subst.tags us in
let new_lits =
new_passive_lit ::
c_guard @
Lit.apply_subst_list renaming subst (lits_a, sc_a) @
Lit.apply_subst_list renaming subst (lits_p, sc_p)
in
let rule =
let name = if Lit.sign passive_lit' then "sup+" else "sup-" in
Proof.Rule.mk name
in
let proof =
Proof.Step.inference ~rule ~tags
[C.proof_parent_subst renaming (info.active,sc_a) subst;
C.proof_parent_subst renaming (info.passive,sc_p) subst]
and penalty =
C.penalty info.active
+ C.penalty info.passive
+ (if T.is_var s' then 2 else 0)
in
let new_clause = C.create ~trail:new_trail ~penalty new_lits proof in
Util.debugf ~section 3 "@[... ok, conclusion@ @[%a@]@]" (fun k->k C.pp new_clause);
new_clause :: acc
with ExitSuperposition reason ->
Util.debugf ~section 3 "... cancel, %s" (fun k->k reason);
acc
let do_simultaneous_superposition info acc =
let ord = Ctx.ord () in
let open SupInfo in
let module P = Position in
Util.incr_stat stat_superposition_call;
let sc_a = info.scope_active in
let sc_p = info.scope_passive in
Util.debugf ~section 3
"@[<hv2>simultaneous sup@ \
@[<2>active@ %a[%d]@ s=@[%a@]@ t=@[%a@]@]@ \
@[<2>passive@ %a[%d]@ passive_lit=@[%a@]@ p=@[%a@]@]@ with subst=@[%a@]@]"
(fun k->k C.pp info.active sc_a T.pp info.s T.pp info.t
C.pp info.passive sc_p Lit.pp info.passive_lit
Position.pp info.passive_pos US.pp info.subst);
assert (InnerTerm.DB.closed (info.s:>InnerTerm.t));
assert (InnerTerm.DB.closed (info.u_p:T.t:>InnerTerm.t));
assert (not(T.is_var info.u_p) || T.is_ho_var info.u_p);
let active_idx = Lits.Pos.idx info.active_pos in
let passive_idx, passive_lit_pos = Lits.Pos.cut info.passive_pos in
try
let renaming = S.Renaming.create () in
let us = info.subst in
let subst = US.subst us in
let t' = S.FO.apply renaming subst (info.t, sc_a) in
begin match info.passive_lit, info.passive_pos with
| Lit.Prop (_, true), P.Arg(_, P.Left P.Stop) ->
if T.equal t' T.true_
then raise (ExitSuperposition "will yield a bool tautology")
| Lit.Equation (_, v, true), P.Arg(_, P.Left P.Stop)
| Lit.Equation (v, _, true), P.Arg(_, P.Right P.Stop) ->
let v' = S.FO.apply renaming subst (v, sc_p) in
if T.equal t' v'
then raise (ExitSuperposition "will yield a tautology");
| _ -> ()
end;
let passive_lit' =
Lit.apply_subst_no_simp renaming subst (info.passive_lit, sc_p)
in
let new_trail = C.trail_l [info.active; info.passive] in
if Env.is_trivial_trail new_trail then raise (ExitSuperposition "trivial trail");
let s' = S.FO.apply renaming subst (info.s, sc_a) in
if (
O.compare ord s' t' = Comp.Lt ||
not (Lit.Pos.is_max_term ~ord passive_lit' passive_lit_pos) ||
not (BV.get (C.eligible_res (info.passive, sc_p) subst) passive_idx) ||
not (C.is_eligible_param (info.active, sc_a) subst ~idx:active_idx)
) then raise (ExitSuperposition "bad ordering conditions");
if not !_sup_at_vars then
assert (not (T.is_var info.u_p))
else if T.is_var info.u_p && not (sup_at_var_condition info info.u_p info.t) then
raise (ExitSuperposition "superposition at variable");
match is_hidden_sup_at_var info with
| Some (var,replacement) when not (!_sup_at_vars && sup_at_var_condition info var replacement)
-> raise (ExitSuperposition "hidden superposition at variable")
| _ -> ();
let lits_a = CCArray.except_idx (C.lits info.active) active_idx in
let lits_a = Lit.apply_subst_list renaming subst (lits_a, sc_a) in
let u' = S.FO.apply renaming subst (info.u_p, sc_p) in
assert (Type.equal (T.ty u') (T.ty t'));
let lits_p = Array.to_list (C.lits info.passive) in
let lits_p = Lit.apply_subst_list renaming subst (lits_p, sc_p) in
let lits_p = List.map (Lit.map (fun t-> T.replace t ~old:u' ~by:t')) lits_p in
let c_guard = Literal.of_unif_subst renaming us in
let tags = Unif_subst.tags us in
let new_lits = c_guard @ lits_a @ lits_p in
let rule =
let name = if Lit.sign passive_lit' then "s_sup+" else "s_sup-" in
Proof.Rule.mk name
in
let proof =
Proof.Step.inference ~rule ~tags
[C.proof_parent_subst renaming (info.active,sc_a) subst;
C.proof_parent_subst renaming (info.passive,sc_p) subst]
and penalty =
C.penalty info.active
+ C.penalty info.passive
+ (if T.is_var s' then 2 else 0)
in
let new_clause = C.create ~trail:new_trail ~penalty new_lits proof in
Util.debugf ~section 3 "@[... ok, conclusion@ @[%a@]@]" (fun k->k C.pp new_clause);
new_clause :: acc
with ExitSuperposition reason ->
Util.debugf ~section 3 "@[... cancel, %s@]" (fun k->k reason);
acc
let do_superposition info acc=
let open SupInfo in
assert (Type.equal (T.ty info.s) (T.ty info.t));
assert (Unif.Ty.equal ~subst:(US.subst info.subst)
(T.ty info.s, info.scope_active) (T.ty info.u_p, info.scope_passive));
if !_use_simultaneous_sup
then do_simultaneous_superposition info acc
else do_classic_superposition info acc
let infer_active clause =
Util.enter_prof prof_infer_active;
let eligible = C.Eligible.param clause in
let new_clauses =
Lits.fold_eqn ~sign:true ~ord:(Ctx.ord ())
~both:true ~eligible (C.lits clause)
|> Iter.fold
(fun acc (s, t, _, s_pos) ->
I.retrieve_unifiables (!_idx_sup_into, 1) (s, 0)
|> Iter.filter (fun (u_p,_,_) -> T.DB.is_closed u_p)
|> Iter.fold
(fun acc (u_p, with_pos, subst) ->
let passive = with_pos.C.WithPos.clause in
let passive_pos = with_pos.C.WithPos.pos in
let passive_lit, _ = Lits.Pos.lit_at (C.lits passive) passive_pos in
let info = SupInfo.( {
s; t; active=clause; active_pos=s_pos; scope_active=0;
u_p; passive; passive_lit; passive_pos; scope_passive=1; subst;
}) in
do_superposition info acc)
acc)
[]
in
Util.exit_prof prof_infer_active;
new_clauses
let infer_passive clause =
Util.enter_prof prof_infer_passive;
let eligible = C.Eligible.(res clause) in
let new_clauses =
Lits.fold_terms ~vars:!_sup_at_vars ~subterms:true ~ord:(Ctx.ord ())
~which:`Max ~eligible ~ty_args:false (C.lits clause)
|> Iter.filter (fun (u_p, _) -> not (T.is_var u_p) || T.is_ho_var u_p)
|> Iter.filter (fun (u_p, _) -> T.DB.is_closed u_p)
|> Iter.fold
(fun acc (u_p, passive_pos) ->
let passive_lit, _ = Lits.Pos.lit_at (C.lits clause) passive_pos in
I.retrieve_unifiables (!_idx_sup_from, 1) (u_p,0)
|> Iter.fold
(fun acc (_, with_pos, subst) ->
let active = with_pos.C.WithPos.clause in
let s_pos = with_pos.C.WithPos.pos in
match Lits.View.get_eqn (C.lits active) s_pos with
| Some (s, t, true) ->
let info = SupInfo.({
s; t; active; active_pos=s_pos; scope_active=1; subst;
u_p; passive=clause; passive_lit; passive_pos; scope_passive=0;
}) in
do_superposition info acc
| _ -> acc)
acc)
[]
in
Util.exit_prof prof_infer_passive;
new_clauses
let infer_equality_resolution clause =
Util.enter_prof prof_infer_equality_resolution;
let eligible = C.Eligible.always in
let new_clauses =
Lits.fold_eqn ~sign:false ~ord:(Ctx.ord ())
~both:false ~eligible (C.lits clause)
|> Iter.filter_map
(fun (l, r, _, l_pos) ->
let pos = Lits.Pos.idx l_pos in
try
let us = Unif.FO.unify_full (l, 0) (r, 0) in
if BV.get (C.eligible_res_no_subst clause) pos
then (
Util.incr_stat stat_equality_resolution_call;
let renaming = Subst.Renaming.create () in
let subst = US.subst us in
let rule = Proof.Rule.mk "eq_res" in
let new_lits = CCArray.except_idx (C.lits clause) pos in
let new_lits = Lit.apply_subst_list renaming subst (new_lits,0) in
let c_guard = Literal.of_unif_subst renaming us in
let tags = Unif_subst.tags us in
let trail = C.trail clause and penalty = C.penalty clause in
let proof = Proof.Step.inference ~rule ~tags
[C.proof_parent_subst renaming (clause,0) subst] in
let new_clause = C.create ~trail ~penalty (c_guard@new_lits) proof in
Util.debugf ~section 3 "@[<hv2>equality resolution on@ @[%a@]@ yields @[%a@]@]"
(fun k->k C.pp clause C.pp new_clause);
Some new_clause
) else None
with Unif.Fail ->
None)
|> Iter.to_rev_list
in
Util.exit_prof prof_infer_equality_resolution;
new_clauses
module EqFactInfo = struct
type t = {
clause : C.t;
active_idx : int;
s : T.t;
t : T.t;
u : T.t;
v : T.t;
subst : US.t;
scope : int;
}
end
let do_eq_factoring info acc =
let open EqFactInfo in
let ord = Ctx.ord () in
let s = info.s and t = info.t and v = info.v in
let us = info.subst in
let renaming = S.Renaming.create () in
let subst = US.subst us in
if O.compare ord (S.FO.apply renaming subst (s, info.scope))
(S.FO.apply renaming subst (t, info.scope)) <> Comp.Lt
&&
C.is_eligible_param (info.clause,info.scope) subst ~idx:info.active_idx
then (
Util.incr_stat stat_equality_factoring_call;
let tags = Unif_subst.tags us in
let proof =
Proof.Step.inference
~rule:(Proof.Rule.mk"eq_fact") ~tags
[C.proof_parent_subst renaming (info.clause,0) subst]
and new_lits = CCArray.except_idx (C.lits info.clause) info.active_idx in
let new_lits = Lit.apply_subst_list renaming subst (new_lits,info.scope) in
let c_guard = Literal.of_unif_subst renaming us in
let lit' = Lit.mk_neq
(S.FO.apply renaming subst (t, info.scope))
(S.FO.apply renaming subst (v, info.scope))
in
let new_lits = lit' :: c_guard @ new_lits in
let new_clause =
C.create ~trail:(C.trail info.clause) ~penalty:(C.penalty info.clause)
new_lits proof
in
Util.debugf ~section 3 "@[<hv2>equality factoring on@ @[%a@]@ yields @[%a@]@]"
(fun k->k C.pp info.clause C.pp new_clause);
new_clause :: acc
) else
acc
let infer_equality_factoring clause =
Util.enter_prof prof_infer_equality_factoring;
let eligible = C.Eligible.(filter Lit.is_pos) in
let find_unifiable_lits idx s _s_pos k =
Array.iteri
(fun i lit ->
match lit with
| _ when i = idx -> ()
| Lit.Prop (p, true) ->
begin try
let subst = Unif.FO.unify_full (s,0) (p,0) in
k (p, T.true_, subst)
with Unif.Fail -> ()
end
| Lit.Equation (u, v, true) ->
begin try
let subst = Unif.FO.unify_full (s,0) (u,0) in
k (u, v, subst)
with Unif.Fail -> ()
end;
begin try
let subst = Unif.FO.unify_full (s,0) (v,0) in
k (v, u, subst)
with Unif.Fail -> ()
end;
| _ -> ()
) (C.lits clause)
in
let new_clauses =
Lits.fold_eqn ~sign:true ~ord:(Ctx.ord ())
~both:true ~eligible (C.lits clause)
|> Iter.fold
(fun acc (s, t, _, s_pos) ->
let active_idx = Lits.Pos.idx s_pos in
find_unifiable_lits active_idx s s_pos
|> Iter.fold
(fun acc (u,v,subst) ->
let info = EqFactInfo.({
clause; s; t; u; v; active_idx; subst; scope=0;
}) in
do_eq_factoring info acc)
acc)
[]
in
Util.exit_prof prof_infer_equality_factoring;
new_clauses
let lazy_false = Lazy.from_val false
type demod_state = {
mutable demod_clauses: (C.t * Subst.t * Scoped.scope) list;
mutable demod_sc: Scoped.scope;
}
(** Compute normal form of term w.r.t active set. Clauses used to
rewrite are added to the clauses hashset.
restrict is an option for restricting demodulation in positive maximal terms *)
let demod_nf ?(restrict=lazy_false) (st:demod_state) c t : T.t =
let ord = Ctx.ord () in
let rec reduce_at_root ~restrict t k =
let cur_sc = st.demod_sc in
assert (cur_sc > 0);
let step =
UnitIdx.retrieve ~sign:true (!_idx_simpl, cur_sc) (t, 0)
|> Iter.find_map
(fun (l, r, (_,_,sign,unit_clause), subst) ->
assert (C.is_unit_clause unit_clause);
if sign &&
(not (Lazy.force restrict) || not (S.is_renaming subst)) &&
C.trail_subsumes unit_clause c &&
(O.compare ord
(S.FO.apply Subst.Renaming.none subst (l,cur_sc))
(S.FO.apply Subst.Renaming.none subst (r,cur_sc)) = Comp.Gt)
then (
Util.debugf ~section 5
"@[<hv2>demod:@ @[<hv>t=%a[%d],@ l=%a[%d],@ r=%a[%d]@],@ subst=@[%a@]@]"
(fun k->k T.pp t 0 T.pp l cur_sc T.pp r cur_sc S.pp subst);
assert (Type.equal (T.ty l) (T.ty r));
assert (Unif.FO.equal ~subst (l,cur_sc) (t,0));
st.demod_clauses <-
(unit_clause,subst,cur_sc) :: st.demod_clauses;
st.demod_sc <- 1 + st.demod_sc;
Util.incr_stat stat_demodulate_step;
Some (r, subst, cur_sc)
) else None)
in
begin match step with
| None -> k t
| Some (rhs,subst,cur_sc) ->
assert (cur_sc < st.demod_sc);
Util.debugf ~section 5
"@[<2>demod:@ rewrite `@[%a@]`@ into `@[%a@]`@ using %a[%d]@]"
(fun k->k T.pp t T.pp rhs Subst.pp subst cur_sc);
let rhs = Subst.FO.apply Subst.Renaming.none subst (rhs,cur_sc) in
normal_form ~restrict rhs k
end
and normal_form ~restrict t k =
match T.view t with
| T.Const _ -> reduce_at_root ~restrict t k
| T.App (hd, l) ->
let rewrite_head =
(List.length l = 0 || not (T.is_type (List.hd l)))
&& not (Ordering.monotonic ord)
in
(if rewrite_head then normal_form ~restrict hd else (fun k -> k hd))
(fun hd' ->
normal_form_l l
(fun l' ->
let t' =
if T.equal hd hd' && T.same_l l l'
then t
else T.app hd' l'
in
reduce_at_root ~restrict t' k))
| T.Fun (ty_arg, body) ->
normal_form ~restrict:lazy_false body
(fun body' ->
let u = if T.equal body body' then t else T.fun_ ty_arg body' in
k u)
| T.Var _ | T.DB _ -> k t
| T.AppBuiltin (b, l) ->
normal_form_l l
(fun l' ->
let u =
if T.same_l l l' then t else T.app_builtin ~ty:(T.ty t) b l'
in
k u)
and normal_form_l l k = match l with
| [] -> k []
| t :: tail ->
normal_form ~restrict:lazy_false t
(fun t' ->
normal_form_l tail
(fun l' -> k (t' :: l')))
in
normal_form ~restrict t (fun t->t)
let[@inline] eq_c_subst (c1,s1,sc1)(c2,s2,sc2) =
C.equal c1 c2 && sc1=sc2 && Subst.equal s1 s2
let demodulate_ c =
Util.incr_stat stat_demodulate_call;
let ord = Ctx.ord () in
let st = {
demod_clauses=[];
demod_sc=1;
} in
let eligible_param = lazy (C.eligible_param (c,0) S.empty) in
let demod_lit i lit =
let strictly_max = lazy (
begin match lit with
| Lit.Equation (t1,t2,true) ->
begin match O.compare ord t1 t2 with
| Comp.Gt -> [t1] | Comp.Lt -> [t2] | _ -> []
end
| Lit.Prop (t,true) -> [t]
| _ -> []
end
) in
let restrict_term t = lazy (
Lit.is_pos lit &&
BV.get (Lazy.force eligible_param) i &&
CCList.mem ~eq:T.equal t (Lazy.force strictly_max)
) in
Lit.map_no_simp
(fun t -> demod_nf ~restrict:(restrict_term t) st c t)
lit
in
let lits = Array.mapi demod_lit (C.lits c) in
if CCList.is_empty st.demod_clauses then (
SimplM.return_same c
) else (
assert (not (Lits.equal_com lits (C.lits c)));
st.demod_clauses <- CCList.uniq ~eq:eq_c_subst st.demod_clauses;
let proof =
Proof.Step.simp
~rule:(Proof.Rule.mk "demod")
(C.proof_parent c ::
List.rev_map
(fun (c,subst,sc) ->
C.proof_parent_subst Subst.Renaming.none (c,sc) subst)
st.demod_clauses) in
let trail = C.trail c in
let new_c = C.create_a ~trail ~penalty:(C.penalty c) lits proof in
Util.debugf ~section 3 "@[<hv2>demodulate@ @[%a@]@ into @[%a@]@ using {@[<hv>%a@]}@]"
(fun k->
let pp_c_s out (c,s,sc) =
Format.fprintf out "(@[%a@ :subst %a[%d]@])" C.pp c Subst.pp s sc in
k C.pp c C.pp new_c (Util.pp_list pp_c_s) st.demod_clauses);
SimplM.return_new new_c
)
let demodulate c = Util.with_prof prof_demodulate demodulate_ c
(** Find clauses that [given] may demodulate, add them to set *)
let backward_demodulate set given =
Util.enter_prof prof_back_demodulate;
let ord = Ctx.ord () in
let renaming = Subst.Renaming.create () in
let recurse ~oriented set l r =
I.retrieve_specializations (!_idx_back_demod,1) (l,0)
|> Iter.fold
(fun set (_t',with_pos,subst) ->
let c = with_pos.C.WithPos.clause in
if ((oriented ||
O.compare ord
(S.FO.apply renaming subst (l,0))
(S.FO.apply renaming subst (r,0)) = Comp.Gt
) && C.trail_subsumes c given
)
then
C.ClauseSet.add c set
else set)
set
in
let set' = match C.lits given with
| [|Lit.Equation (l,r,true) |] ->
begin match Ordering.compare ord l r with
| Comp.Gt -> recurse ~oriented:true set l r
| Comp.Lt -> recurse ~oriented:true set r l
| _ ->
let set' = recurse ~oriented:false set l r in
recurse ~oriented:false set' r l
end
| _ -> set
in
Util.exit_prof prof_back_demodulate;
set'
let is_tautology c =
let is_tauto = Lits.is_trivial (C.lits c) || Trail.is_trivial (C.trail c) in
if is_tauto then Util.debugf ~section 3 "@[@[%a@]@ is a tautology@]" (fun k->k C.pp c);
is_tauto
let is_semantic_tautology_real (c:C.t) : bool =
let cc = Congruence.FO.create ~size:8 () in
let cc =
Array.fold_left
(fun cc lit -> match lit with
| Lit.Equation (l, r, false) ->
Congruence.FO.mk_eq cc l r
| Lit.Prop (p, false) ->
Congruence.FO.mk_eq cc p T.true_
| _ -> cc)
cc (C.lits c)
in
let res = CCArray.exists
(function
| Lit.Equation (l, r, true) ->
Congruence.FO.is_eq cc l r
| Lit.Prop (p, true) ->
Congruence.FO.is_eq cc p T.true_
| _ -> false)
(C.lits c)
in
if res then (
Util.incr_stat stat_semantic_tautology;
Util.debugf ~section 2 "@[@[%a@]@ is a semantic tautology@]" (fun k->k C.pp c);
);
res
let is_semantic_tautology_ c =
if Array.length (C.lits c) >= 2 &&
CCArray.exists Lit.is_neg (C.lits c) &&
CCArray.exists Lit.is_pos (C.lits c)
then is_semantic_tautology_real c
else false
let is_semantic_tautology c =
Util.with_prof prof_semantic_tautology is_semantic_tautology_ c
let var_in_subst_ us v sc =
S.mem (US.subst us) ((v:T.var:>InnerTerm.t HVar.t),sc)
let basic_simplify c =
if C.get_flag flag_simplified c
then SimplM.return_same c
else (
Util.enter_prof prof_basic_simplify;
Util.incr_stat stat_basic_simplify_calls;
let lits = C.lits c in
let has_changed = ref false in
let tags = ref [] in
let bv = BV.create ~size:(Array.length lits) true in
Array.iteri
(fun i lit ->
if Lit.is_absurd lit then (
has_changed := true;
tags := Lit.is_absurd_tags lit @ !tags;
BV.reset bv i
))
lits;
let us = ref US.empty in
let try_unif i t1 sc1 t2 sc2 =
try
let subst' = Unif.FO.unify_full ~subst:!us (t1,sc1) (t2,sc2) in
has_changed := true;
BV.reset bv i;
us := subst';
with Unif.Fail -> ()
in
Array.iteri
(fun i lit ->
let can_destr_eq_var v =
not (var_in_subst_ !us v 0) && not (Type.is_fun (HVar.ty v))
in
if BV.get bv i then match lit with
| Lit.Equation (l, r, false) ->
begin match T.view l, T.view r with
| T.Var v, _ when can_destr_eq_var v ->
try_unif i l 0 r 0
| _, T.Var v when can_destr_eq_var v ->
try_unif i r 0 l 0
| _ -> ()
end
| Lit.Equation (l, r, true) when Type.is_prop (T.ty l) ->
begin match T.view l, T.view r with
| ( T.AppBuiltin (Builtin.True, []), T.Var x
| T.Var x, T.AppBuiltin (Builtin.True, []))
when not (var_in_subst_ !us x 0) ->
begin
try
let subst' = US.FO.bind !us (x,0) (T.false_,0) in
has_changed := true;
BV.reset bv i;
us := subst';
with Unif.Fail -> ()
end
| _ -> ()
end
| _ -> ())
lits;
let new_lits = BV.select bv lits in
let new_lits =
if US.is_empty !us then new_lits
else (
assert !has_changed;
let subst = US.subst !us in
let tgs = US.tags !us in
tags := tgs @ !tags;
let c_guard = Literal.of_unif_subst Subst.Renaming.none !us in
c_guard @ Lit.apply_subst_list Subst.Renaming.none subst (new_lits,0)
)
in
let new_lits = CCList.uniq ~eq:Lit.equal_com new_lits in
if not !has_changed && List.length new_lits = Array.length lits then (
Util.exit_prof prof_basic_simplify;
C.set_flag flag_simplified c true;
SimplM.return_same c
) else (
let parent =
if Subst.is_empty (US.subst !us) then C.proof_parent c
else C.proof_parent_subst Subst.Renaming.none (c,0) (US.subst !us)
in
let proof =
Proof.Step.simp [parent]
~tags:!tags ~rule:(Proof.Rule.mk "simplify") in
let new_clause =
C.create ~trail:(C.trail c) ~penalty:(C.penalty c) new_lits proof
in
Util.debugf ~section 3
"@[<>@[%a@]@ @[<2>basic_simplifies into@ @[%a@]@]@ with @[%a@]@]"
(fun k->k C.pp c C.pp new_clause US.pp !us);
Util.incr_stat stat_basic_simplify;
Util.exit_prof prof_basic_simplify;
SimplM.return_new new_clause
)
)
let handle_distinct_constants lit =
match lit with
| Lit.Equation (l, r, sign) when T.is_const l && T.is_const r ->
let s1 = T.head_exn l and s2 = T.head_exn r in
if ID.is_distinct_object s1 && ID.is_distinct_object s2
then
if sign = (ID.equal s1 s2)
then Some (Lit.mk_tauto,[],[Proof.Tag.T_distinct])
else Some (Lit.mk_absurd,[],[Proof.Tag.T_distinct])
else None
| _ -> None
exception FoundMatch of T.t * C.t * S.t
let positive_simplify_reflect c =
Util.enter_prof prof_pos_simplify_reflect;
let rec iterate_lits acc lits clauses = match lits with
| [] -> List.rev acc, clauses
| (Lit.Equation (s, t, false) as lit)::lits' ->
begin match equatable_terms clauses s t with
| None ->
iterate_lits (lit::acc) lits' clauses
| Some new_clauses ->
iterate_lits acc lits' new_clauses
end
| lit::lits' -> iterate_lits (lit::acc) lits' clauses
and equatable_terms clauses t1 t2 =
match T.Classic.view t1, T.Classic.view t2 with
| _ when T.equal t1 t2 -> Some clauses
| T.Classic.App (f, ss), T.Classic.App (g, ts)
when ID.equal f g && List.length ss = List.length ts ->
begin match equate_root clauses t1 t2 with
| None ->
let ok, clauses = List.fold_left2
(fun (ok, clauses) t1' t2' ->
if ok
then match equatable_terms clauses t1' t2' with
| None -> false, []
| Some clauses -> true, clauses
else false, [])
(true, clauses) ss ts
in
if ok then Some clauses else None
| Some clauses -> Some clauses
end
| _ -> equate_root clauses t1 t2
and equate_root clauses t1 t2 =
try
UnitIdx.retrieve ~sign:true (!_idx_simpl,1)(t1,0)
|> Iter.iter
(fun (l,r,(_,_,_,c'),subst) ->
assert (Unif.FO.equal ~subst (l,1)(t1,0));
if Unif.FO.equal ~subst (r,1)(t2,0)
&& C.trail_subsumes c' c
then begin
Util.debugf ~section 4
"@[<2>equate @[%a@]@ and @[%a@]@ using @[%a@]@]"
(fun k->k T.pp t1 T.pp t2 C.pp c');
raise (FoundMatch (r, c', subst))
end
);
None
with FoundMatch (_r, c', subst) ->
Some (C.proof_parent_subst Subst.Renaming.none (c',1) subst :: clauses)
in
let lits, premises = iterate_lits [] (C.lits c |> Array.to_list) [] in
if List.length lits = Array.length (C.lits c)
then (
Util.exit_prof prof_pos_simplify_reflect;
SimplM.return_same c
) else (
let proof =
Proof.Step.simp ~rule:(Proof.Rule.mk "simplify_reflect+")
(C.proof_parent c::premises) in
let trail = C.trail c and penalty = C.penalty c in
let new_c = C.create ~trail ~penalty lits proof in
Util.debugf ~section 3 "@[@[%a@]@ pos_simplify_reflect into @[%a@]@]"
(fun k->k C.pp c C.pp new_c);
Util.exit_prof prof_pos_simplify_reflect;
SimplM.return_new new_c
)
let negative_simplify_reflect c =
Util.enter_prof prof_neg_simplify_reflect;
let rec iterate_lits acc lits clauses = match lits with
| [] -> List.rev acc, clauses
| (Lit.Equation (s, t, true) as lit)::lits' ->
begin match can_refute s t, can_refute t s with
| None, None ->
iterate_lits (lit::acc) lits' clauses
| Some new_clause, _ | _, Some new_clause ->
iterate_lits acc lits' (new_clause :: clauses)
end
| lit::lits' -> iterate_lits (lit::acc) lits' clauses
and can_refute s t =
try
UnitIdx.retrieve ~sign:false (!_idx_simpl,1) (s,0)
|> Iter.iter
(fun (l, r, (_,_,_,c'), subst) ->
assert (Unif.FO.equal ~subst (l, 1) (s, 0));
if Unif.FO.equal ~subst (r, 1) (t, 0)
&& C.trail_subsumes c' c
then begin
let subst = Unif.FO.matching ~subst ~pattern:(r, 1) (t, 0) in
Util.debugf ~section 3 "@[neg_reflect eliminates@ @[%a=%a@]@ with @[%a@]@]"
(fun k->k T.pp s T.pp t C.pp c');
raise (FoundMatch (r, c', subst))
end
);
None
with FoundMatch (_r, c', subst) ->
Some (C.proof_parent_subst Subst.Renaming.none (c',1) subst)
in
let lits, premises = iterate_lits [] (C.lits c |> Array.to_list) [] in
if List.length lits = Array.length (C.lits c)
then (
Util.exit_prof prof_neg_simplify_reflect;
SimplM.return_same c
) else (
let proof =
Proof.Step.simp
~rule:(Proof.Rule.mk "simplify_reflect-")
(C.proof_parent c :: premises) in
let new_c = C.create ~trail:(C.trail c) ~penalty:(C.penalty c) lits proof in
Util.debugf ~section 3 "@[@[%a@]@ neg_simplify_reflect into @[%a@]@]"
(fun k->k C.pp c C.pp new_c);
Util.exit_prof prof_neg_simplify_reflect;
SimplM.return_new new_c
)
(** raised when a subsuming substitution is found *)
exception SubsumptionFound of S.t
(** check that every literal in a matches at least one literal in b *)
let all_lits_match a sc_a b sc_b =
CCArray.for_all
(fun lita ->
CCArray.exists
(fun litb ->
not (Iter.is_empty (Lit.subsumes (lita, sc_a) (litb, sc_b))))
b)
a
(** Compare literals by subsumption difficulty
(see "towards efficient subsumption", Tammet).
We sort by increasing order, so non-ground, deep, heavy literals are
smaller (thus tested early) *)
let compare_literals_subsumption lita litb =
CCOrd.(
bool (Lit.is_ground lita) (Lit.is_ground litb)
<?> (map Lit.depth (opp int), lita, litb)
<?> (map Lit.weight (opp int), lita, litb)
)
(** Check whether [a] subsumes [b], and if it does, return the
corresponding substitution *)
let subsumes_with_ (a,sc_a) (b,sc_b) : _ option =
if Array.length a > Array.length b
|| not (all_lits_match a sc_a b sc_b)
then None
else (
let a = Array.copy a in
let tags = ref [] in
let rec try_permutations i subst bv =
if i = Array.length a
then raise (SubsumptionFound subst)
else
let lita = a.(i) in
find_matched lita i subst bv 0
and find_matched lita i subst bv j =
if j = Array.length b then ()
else if BV.get bv j then find_matched lita i subst bv (j+1)
else (
let litb = b.(j) in
BV.set bv j;
let n_subst = ref 0 in
Lit.subsumes ~subst (lita, sc_a) (litb, sc_b)
(fun (subst',tgs) ->
incr n_subst;
tags := tgs @ !tags;
try_permutations (i+1) subst' bv);
BV.reset bv j;
if !n_subst > 0 && not (check_vars lita (i+1))
then ()
else find_matched lita i subst bv (j+1)
)
and check_vars lit j =
let vars = Lit.vars lit in
if vars = []
then false
else
try
for k = j to Array.length a - 1 do
if List.exists (fun v -> Lit.var_occurs v a.(k)) vars
then raise Exit
done;
false
with Exit -> true
in
try
Array.sort compare_literals_subsumption a;
let bv = BV.empty () in
try_permutations 0 S.empty bv;
None
with (SubsumptionFound subst) ->
Util.debugf ~section 2 "(@[<hv>subsumes@ :c1 @[%a@]@ :c2 @[%a@]@ :subst %a%a@]"
(fun k->k Lits.pp a Lits.pp b Subst.pp subst Proof.pp_tags !tags);
Some (subst, !tags)
)
let subsumes_with a b =
Util.enter_prof prof_subsumption;
Util.incr_stat stat_subsumption_call;
let res = subsumes_with_ a b in
Util.exit_prof prof_subsumption;
res
let subsumes a b = match subsumes_with (a,0) (b,1) with
| None -> false
| Some _ -> true
let anti_unify (t:T.t)(u:T.t): (T.t * T.t) option =
match Unif.FO.anti_unify ~cut:1 t u with
| Some [pair] -> Some pair
| _ -> None
let eq_subsumes_with (a,sc_a) (b,sc_b) =
let rec equate_lit_with a b lit = match lit with
| Lit.Equation (u, v, true) when not (T.equal u v) -> equate_terms a b u v
| _ -> None
and equate_terms a b u v =
begin match anti_unify u v with
| None -> None
| Some (u', v') -> equate_root a b u' v'
end
and equate_root a b u v: Subst.t option =
let check_ a b u v =
try
let subst = Unif.FO.matching ~pattern:(a, sc_a) (u, sc_b) in
let subst = Unif.FO.matching ~subst ~pattern:(b, sc_a) (v, sc_b) in
Some subst
with Unif.Fail -> None
in
begin match check_ a b u v with
| Some _ as s -> s
| None -> check_ b a u v
end
in
Util.enter_prof prof_eq_subsumption;
Util.incr_stat stat_eq_subsumption_call;
let res = match a with
| [|Lit.Equation (s, t, true)|] ->
let res = CCArray.find (equate_lit_with s t) b in
begin match res with
| None -> None
| Some subst ->
Util.debugf ~section 3 "@[<2>@[%a@]@ eq-subsumes @[%a@]@ :subst %a@]"
(fun k->k Lits.pp a Lits.pp b Subst.pp subst);
Util.incr_stat stat_eq_subsumption_success;
Some subst
end
| _ -> None
in
Util.exit_prof prof_eq_subsumption;
res
let eq_subsumes a b = CCOpt.is_some (eq_subsumes_with (a,1) (b,0))
let subsumed_by_active_set c =
Util.enter_prof prof_subsumption_set;
Util.incr_stat stat_subsumed_by_active_set_call;
let try_eq_subsumption = CCArray.exists Lit.is_eqn (C.lits c) in
let res =
SubsumIdx.retrieve_subsuming_c !_idx_fv c
|> Iter.exists
(fun c' ->
C.trail_subsumes c' c
&&
( (try_eq_subsumption && eq_subsumes (C.lits c') (C.lits c))
||
subsumes (C.lits c') (C.lits c)
))
in
Util.exit_prof prof_subsumption_set;
if res then (
Util.debugf ~section 3 "@[<2>@[%a@]@ subsumed by active set@]" (fun k->k C.pp c);
Util.incr_stat stat_clauses_subsumed;
);
res
let subsumed_in_active_set acc c =
Util.enter_prof prof_subsumption_in_set;
Util.incr_stat stat_subsumed_in_active_set_call;
let try_eq_subsumption =
C.is_unit_clause c && Lit.is_pos (C.lits c).(0)
in
let res =
SubsumIdx.retrieve_subsumed_c !_idx_fv c
|> Iter.fold
(fun res c' ->
if C.trail_subsumes c c'
then
let redundant =
(try_eq_subsumption && eq_subsumes (C.lits c) (C.lits c'))
|| subsumes (C.lits c) (C.lits c')
in
if redundant then (
Util.incr_stat stat_clauses_subsumed;
C.ClauseSet.add c' res
) else res
else res)
acc
in
Util.exit_prof prof_subsumption_in_set;
res
let num_equational lits =
Array.fold_left
(fun acc lit -> match lit with
| Lit.Equation _ -> acc+1
| _ -> acc
) 0 lits
let rec contextual_literal_cutting_rec c =
let open SimplM.Infix in
if Array.length (C.lits c) <= 1
|| num_equational (C.lits c) > 3
|| Array.length (C.lits c) > 8
then SimplM.return_same c
else (
let try_eq_subsumption = CCArray.exists Lit.is_eqn (C.lits c) in
let remove_one_lit lits =
Iter.of_array_i lits
|> Iter.filter (fun (_,lit) -> not (Lit.is_constraint lit))
|> Iter.find_map
(fun (i,old_lit) ->
lits.(i) <- Lit.negate old_lit;
SubsumIdx.retrieve_subsuming !_idx_fv
(Lits.Seq.to_form lits) (C.trail c |> Trail.labels)
|> Iter.filter (fun c' -> C.trail_subsumes c' c)
|> Iter.find_map
(fun c' ->
let subst =
match
if try_eq_subsumption
then eq_subsumes_with (C.lits c',1) (lits,0)
else None
with
| Some s -> Some (s, [])
| None -> subsumes_with (C.lits c',1) (lits,0)
in
subst
|> CCOpt.map
(fun (subst,tags) ->
CCArray.except_idx lits i, i, c', subst, tags))
|> CCFun.tap
(fun _ ->
lits.(i) <- old_lit))
in
begin match remove_one_lit (Array.copy (C.lits c)) with
| None ->
SimplM.return_same c
| Some (new_lits, _, c',subst,tags) ->
assert (List.length new_lits + 1 = Array.length (C.lits c));
let proof =
Proof.Step.inference
~rule:(Proof.Rule.mk "clc") ~tags
[C.proof_parent c;
C.proof_parent_subst Subst.Renaming.none (c',1) subst] in
let new_c = C.create ~trail:(C.trail c) ~penalty:(C.penalty c) new_lits proof in
Util.debugf ~section 3
"@[<2>contextual literal cutting@ in @[%a@]@ using @[%a@]@ gives @[%a@]@]"
(fun k->k C.pp c C.pp c' C.pp new_c);
Util.incr_stat stat_clc;
SimplM.return_new new_c >>= contextual_literal_cutting_rec
end
)
let contextual_literal_cutting c =
Util.enter_prof prof_clc;
let res = contextual_literal_cutting_rec c in
Util.exit_prof prof_clc;
res
exception CondensedInto of Lit.t array * S.t * Subst.Renaming.t * Proof.tag list
(** performs condensation on the clause. It looks for two literals l1 and l2 of same
sign such that l1\sigma = l2, and hc\sigma \ {l2} subsumes hc. Then
hc is simplified into hc\sigma \ {l2}.
If there are too many equational literals, the simplification is disabled to
avoid pathologically expensive subsumption checks.
TODO remove this limitation after an efficient subsumption check is implemented. *)
let rec condensation_rec c =
let open SimplM.Infix in
if Array.length (C.lits c) <= 1
|| num_equational (C.lits c) > 3
|| Array.length (C.lits c) > 8
then SimplM.return_same c
else
let lits = C.lits c in
let n = Array.length lits in
try
for i = 0 to n - 1 do
let lit = lits.(i) in
for j = i+1 to n - 1 do
let lit' = lits.(j) in
let subst_remove_lit =
Lit.subsumes (lit, 0) (lit', 0)
|> Iter.map (fun s -> s, i)
and subst_remove_lit' =
Lit.subsumes (lit', 0) (lit, 0)
|> Iter.map (fun s -> s, j)
in
let substs = Iter.append subst_remove_lit subst_remove_lit' in
Iter.iter
(fun ((subst,tags),idx_to_remove) ->
let new_lits = Array.sub lits 0 (n - 1) in
if idx_to_remove <> n-1
then new_lits.(idx_to_remove) <- lits.(n-1);
let renaming = Subst.Renaming.create () in
let new_lits = Lits.apply_subst renaming subst (new_lits,0) in
if subsumes new_lits lits then (
raise (CondensedInto (new_lits, subst, renaming, tags))
))
substs
done;
done;
SimplM.return_same c
with CondensedInto (new_lits, subst, renaming, tags) ->
let proof =
Proof.Step.simp
~rule:(Proof.Rule.mk "condensation") ~tags
[C.proof_parent_subst renaming (c,0) subst] in
let c' = C.create_a ~trail:(C.trail c) ~penalty:(C.penalty c) new_lits proof in
Util.debugf ~section 3
"@[<2>condensation@ of @[%a@] (with @[%a@])@ gives @[%a@]@]"
(fun k->k C.pp c S.pp subst C.pp c');
Util.incr_stat stat_condensation;
SimplM.return_new c' >>= condensation_rec
let condensation c =
Util.with_prof prof_condensation condensation_rec c
(** {2 Registration} *)
let _print_idx ~f file idx =
CCIO.with_out file
(fun oc ->
let out = Format.formatter_of_out_channel oc in
Format.fprintf out "@[%a@]@." f idx;
flush oc)
let setup_dot_printers () =
let pp_leaf _ _ = () in
CCOpt.iter
(fun file ->
Signal.once Signals.on_dot_output
(fun () -> _print_idx ~f:(TermIndex.to_dot pp_leaf) file !_idx_sup_into))
!_dot_sup_into;
CCOpt.iter
(fun file ->
Signal.once Signals.on_dot_output
(fun () -> _print_idx ~f:(TermIndex.to_dot pp_leaf) file !_idx_sup_from))
!_dot_sup_from;
CCOpt.iter
(fun file ->
Signal.once Signals.on_dot_output
(fun () -> _print_idx ~f:UnitIdx.to_dot file !_idx_simpl))
!_dot_simpl;
CCOpt.iter
(fun file ->
Signal.once Signals.on_dot_output
(fun () -> _print_idx ~f:(TermIndex.to_dot pp_leaf) file !_idx_back_demod))
!_dot_demod_into;
()
let register () =
let open SimplM.Infix in
let rw_simplify c =
demodulate c
>>= basic_simplify
>>= positive_simplify_reflect
>>= negative_simplify_reflect
and active_simplify c =
condensation c
>>= contextual_literal_cutting
and backward_simplify c =
let set = C.ClauseSet.empty in
backward_demodulate set c
and redundant = subsumed_by_active_set
and backward_redundant = subsumed_in_active_set
and is_trivial = is_tautology in
Env.add_binary_inf "superposition_passive" infer_passive;
Env.add_binary_inf "superposition_active" infer_active;
Env.add_unary_inf "equality_factoring" infer_equality_factoring;
Env.add_unary_inf "equality_resolution" infer_equality_resolution;
if not (!_dont_simplify) then (
Env.add_rw_simplify rw_simplify;
Env.add_basic_simplify basic_simplify;
Env.add_active_simplify active_simplify;
Env.add_backward_simplify backward_simplify
);
Env.add_redundant redundant;
Env.add_backward_redundant backward_redundant;
if !_use_semantic_tauto
then Env.add_is_trivial is_semantic_tautology;
Env.add_is_trivial is_trivial;
Env.add_lit_rule "distinct_symbol" handle_distinct_constants;
setup_dot_printers ();
()
end
let key = Flex_state.create_key()
let register ~sup =
let module Sup = (val sup : S) in
let module E = Sup.Env in
E.update_flex_state (Flex_state.add key sup)
let extension =
let action env =
let module E = (val env : Env.S) in
let module Sup = Make(E) in
Sup.register();
register ~sup:(module Sup : S)
in
{ Extensions.default with Extensions.
name="superposition";
env_actions = [action];
}
let () =
Params.add_opts
[ "--semantic-tauto"
, Arg.Set _use_semantic_tauto
, " enable semantic tautology check"
; "--no-semantic-tauto"
, Arg.Clear _use_semantic_tauto
, " disable semantic tautology check"
; "--dot-sup-into"
, Arg.String (fun s -> _dot_sup_into := Some s)
, " print superposition-into index into file"
; "--dot-sup-from"
, Arg.String (fun s -> _dot_sup_from := Some s)
, " print superposition-from index into file"
; "--dot-demod"
, Arg.String (fun s -> _dot_simpl := Some s)
, " print forward rewriting index into file"
; "--dot-demod-into"
, Arg.String (fun s -> _dot_demod_into := Some s)
, " print backward rewriting index into file"
; "--simultaneous-sup"
, Arg.Bool (fun b -> _use_simultaneous_sup := b)
, " enable/disable simultaneous superposition"
; "--dont-simplify"
, Arg.Set _dont_simplify
, " disable simplification rules"
; "--sup-at-vars"
, Arg.Set _sup_at_vars
, " enable superposition at variables under certain ordering conditions"
; "--restrict-hidden-sup-at-vars"
, Arg.Set _restrict_hidden_sup_at_vars
, " perform hidden superposition at variables only under certain ordering conditions"
]