deduxit 0.5.0

A collection of logic suites: decides, checks, proves step by step and exports sequents of linear logic and of classical, intuitionistic and minimal propositional logic, with checked proofs, Kripke countermodels and Rocq certificates.
Documentation
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
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
// deduxit © Fabian Lukas Grubmüller 2026
// Licensed under the EUPL

//! Why a sequent is unprovable. A [`Refutation`] is the reason a search
//! or a test of the sequent gives. A [`Disproof`] holds it with the
//! sequent, the goal and the mode it is about.

use crate::linear::fragment::Mode;
use crate::linear::occurrences::{Member, OccId, Sign};
use crate::linear::sequents::{Atom, Sequent, Symbol, Term};
use std::fmt::{Display, Formatter, Result as FmtResult};

/// Why a sequent is unprovable.
///
/// Most refutations rest on a search that tried every way to a proof.
/// Others rest on the sequent alone: its literals or its connectives rule
/// out a proof, and the refutation says which. The focused engine tests
/// these on every sequent it searches. The Horn engine may refute a Horn
/// program by its state equation, whatever its search found.
///
/// A refutation means something only with its sequent, goal and mode,
/// which a [`Disproof`] holds. Its [`Display`] writes atoms by number
/// (`#0`); a disproof's writes them by name. For a goal other than the
/// sequent's roots, the "one-sided sequent" a count speaks of is the goal.
/// Its members are counted as written, even where they lie below an
/// exponential of the sequent. A disproof of a classical sequent written
/// with formulas left of `⊢` speaks of its sides instead: `a` counts
/// right of `⊢` and `~a` left of it, as in `a on the left has no a on the
/// right to meet in an axiom`.
///
/// In JSON (feature `serialize`), a refutation is tagged by `kind`. It is
/// the `refutation` of an unprovable outcome and of a disproof:
///
/// - `{"kind": "exhausted"}`;
/// - `{"kind": "unbalanced", "atom", "least", "most"}`, the atom by its
///   index into the sequent beside it;
/// - `{"kind": "one_sided", "atom", "least", "most", "copied"}`, with
///   `copied` the literal `"atom"` or `"dual"`;
/// - `{"kind": "equation", "formulas", "needed", "tensors", "pars",
///   "ones", "bottoms", "mix"}`;
/// - `{"kind": "state_equation", "atoms": [[atom, weight], …], "clauses":
///   [[occurrence, weight], …], "dropped": [occurrence, …]}`;
/// - `{"kind": "classical", "assignment": [true, false, …]}`, a value per
///   atom of the sequent, by index;
/// - `{"kind": "saturated", "positive": ["atom", "dual", …], "sequents":
///   [{"linear": [occurrence, …], "unrestricted": [occurrence, …],
///   "absorbs": false}, …]}`;
/// - `{"kind": "unbalanced_symbol", "symbol", "least", "most"}` and
///   `{"kind": "one_sided_symbol", "symbol", "least", "most", "copied"}`,
///   the predicate by its index into the sequent's symbols;
/// - `{"kind": "basis", "body": "atom" | "dual", "classes": [[occurrence,
///   …], …], "markings": [{"atoms": [[atom, count], …], "tickets":
///   [[class, count], …]}, …]}`.
///
/// It reads back the same way. A reader refuses a kind it does not know:
/// read as another kind, it would claim a certificate it does not hold.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Refutation {
    /// Every way to prove the sequent was tried and failed.
    Exhausted,
    /// The literals of an atom cannot all meet their duals in axioms.
    Unbalanced(Unbalanced),
    /// The literals of an atom cannot all meet their duals in axioms, even
    /// though copies may add some, all of one sign.
    OneSided(OneSided),
    /// The count equation of the multiplicatives fails.
    Equation(Equation),
    /// The goal is a Horn program whose state equation has no solution.
    StateEquation(StateEquation),
    /// The classical reading of the goal is false under an assignment.
    Classical(Classical),
    /// A forward search saturated. Its sequents are closed under the
    /// rules, and the goal needs one they do not give.
    Saturated(Saturated),
    /// The literals of a predicate cannot all meet their duals in axioms,
    /// counted over all its atoms. One of its atoms has a bound variable.
    UnbalancedSymbol(UnbalancedSymbol),
    /// The literals of a predicate cannot all meet their duals in axioms,
    /// even though copies may add some, all of one sign.
    OneSidedSymbol(OneSidedSymbol),
    /// The goal is a Horn program, and a backward search from its target
    /// saturated: the markings from which a firing sequence covers the
    /// target are those at least one of a finite basis, and the initial
    /// marking is not one of them.
    Basis(Basis),
}

/// The literals of a predicate, which cannot all meet their duals in
/// axioms. They are counted as [`Unbalanced`] counts an atom's, but over
/// every atom of the predicate together.
///
/// One of the predicate's atoms has a bound variable, so an instance of
/// it may be any atom of the predicate. An axiom joins only literals of
/// one predicate.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnbalancedSymbol {
    /// The predicate, whose name the sequent has.
    pub symbol: Symbol,
    /// The least excess of positive over negative literals.
    pub least: i32,
    /// The greatest excess.
    pub most: i32,
}

/// The literals of a predicate, counted as [`UnbalancedSymbol`] counts
/// them, some of which lie below a `?` or `!`. Copies may add any number
/// of those, all of one sign, as [`OneSided`] says of an atom.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct OneSidedSymbol {
    /// The predicate, whose name the sequent has.
    pub symbol: Symbol,
    /// The least excess of positive over negative literals outside the
    /// exponentials.
    pub least: i32,
    /// The greatest excess.
    pub most: i32,
    /// The sign of every literal of the predicate below an exponential.
    pub copied: Sign,
}

/// The literals of an atom, which cannot all meet their duals in axioms.
///
/// Whichever additive alternatives a proof takes, the positive literal
/// occurs between `least` and `most` times more often than the negative
/// one (less often, where these are negative), and never equally often.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Unbalanced {
    /// The atom, whose name the sequent has.
    pub atom: Atom,
    /// The least excess of positive over negative literals.
    pub least: i32,
    /// The greatest excess.
    pub most: i32,
}

/// The literals of an atom, which cannot all meet their duals in axioms
/// although some lie below a `?` or `!`, where copies may add any number
/// of them.
///
/// Every literal of the atom below an exponential has the sign `copied`,
/// so copies add only that literal. Outside the exponentials, that
/// literal already outnumbers the other one. Its excess, counted as
/// [`Unbalanced`] counts it, lies between `least` and `most`: `least` is
/// above zero where `copied` is the atom, and `most` is below zero where
/// it is the negation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct OneSided {
    /// The atom, whose name the sequent has.
    pub atom: Atom,
    /// The least excess of positive over negative literals outside the
    /// exponentials.
    pub least: i32,
    /// The greatest excess.
    pub most: i32,
    /// The sign of every literal of the atom below an exponential.
    pub copied: Sign,
}

/// The count equation of the multiplicatives, which the sequent fails.
///
/// A provable one-sided sequent of MLL with units has exactly `tensors −
/// pars − ones + bottoms + 2` formulas, and at least that many with Mix.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Equation {
    /// How many formulas the sequent has.
    pub formulas: u64,
    /// How many formulas the equation asks for. It is below zero where the
    /// `⅋` and the `1` outnumber the rest.
    pub needed: i64,
    /// How many `⊗` the sequent has.
    pub tensors: u64,
    /// How many `⅋` it has.
    pub pars: u64,
    /// How many `1` it has.
    pub ones: u64,
    /// How many `⊥` it has.
    pub bottoms: u64,
    /// Whether the mode allows Mix.
    pub mix: bool,
}

/// Weights under which a Horn program's state equation fails, so that no
/// sequence of firings reaches the goal.
///
/// The weights are for the program's Petri net, every place included.
/// Weigh the tokens of each atom, and the ticket of each clause used
/// once, as given, zero where not listed. Then no clause that can fire
/// raises the weighted count, while the goal asks it raised (Farkas'
/// lemma). In affine mode, with weights that are not negative, no
/// sequence of firings covers the goal either. A clause with an input
/// that no marking reachable from the start holds can never fire, and is
/// dropped from the inequalities.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct StateEquation {
    /// The atoms whose tokens weigh something, in the order of the
    /// sequent's atoms, with their weights.
    pub atoms: Vec<(Atom, i64)>,
    /// The clauses used once whose ticket weighs something, by
    /// occurrence, with their weights.
    pub clauses: Vec<(OccId, i64)>,
    /// The clauses that can never fire, by occurrence.
    pub dropped: Vec<OccId>,
}

/// An assignment of truth values to the atoms under which the classical
/// reading of the goal is false.
///
/// The classical reading erases `!` and `?`, reads `⊗` and `&` as "and",
/// `⅋` and `⊕` as "or", `1` and `⊤` as true, `⊥` and `0` as false, `~a`
/// as "not a", and the goal as the disjunction of its formulas. In every
/// mode, a provable sequent's reading is true under every assignment.
/// Each rule, Mix and weakening included, derives a tautology from
/// tautologies, and an intuitionistic sequent's one-sided form reads as
/// its implication. So the assignment proves the goal unprovable.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Classical {
    /// The value of each atom of the sequent, by index. An atom the goal
    /// does not contain has one too.
    pub assignment: Vec<bool>,
}

/// The sequents a forward search kept when it saturated, which show that
/// the goal has no proof.
///
/// The sequents are stable ([`Stable`]) and closed under the rules of the
/// focused calculus. They leave underived a stable sequent of the goal's
/// asynchronous phase, the part of a proof where only invertible rules
/// apply. A rule is a focus on a positive formula, or a copy of a `?`
/// formula's subformula, with the sides of its `⊕`, the partners of its
/// axioms and the asynchronous phases of what it releases. Applied to any
/// of the sequents, every rule derives a sequent that a sequent of the set
/// stands for, or one that no proof holds: a member twice, or two members
/// that a `⊗`, `&` or `⊕` separates, outside the subformulas of `?`. Mix
/// does the same where the mode has it. So, by induction, every stable
/// sequent of a focused proof is one that a sequent of the set stands for,
/// and some stable sequent of the goal's phase is one that none stands
/// for.
///
/// The rules are those of the polarity `positive` names. Focusing is as
/// complete under it as under any other. The certificate is as large as
/// the set, which the check reads whole and applies every rule to.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Saturated {
    /// The positive literal of each atom of the sequent, by index:
    /// [`Sign::Atom`] where the atom is positive, [`Sign::Dual`] where its
    /// negation is.
    pub positive: Vec<Sign>,
    /// The sequents.
    pub sequents: Vec<Stable>,
}

/// A stable sequent of a saturated set, as occurrences of the sequent's
/// forest. Its linear zone holds positive formulas and negative literals,
/// and its unrestricted zone holds subformulas of `?`.
///
/// It stands for the sequents with exactly this linear zone and an
/// unrestricted zone that holds this one. Where it `absorbs` (a `⊤`
/// above), or in affine mode, it also stands for those with a larger
/// linear zone. A member in output position is then absorbed only where
/// the zone has none, and never weakened in. Every sequent of a proof
/// holds the subformulas of the `?` the goal reaches through `⅋` alone,
/// so they may be left out of the unrestricted zone.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Stable {
    /// The linear zone, sorted, with repeats.
    pub linear: Vec<OccId>,
    /// The unrestricted zone, sorted, without repeats.
    pub unrestricted: Vec<OccId>,
    /// Whether a `⊤` above absorbs any further linear member.
    pub absorbs: bool,
}

impl Stable {
    /// Returns the stable sequent of the zones given.
    pub fn new(linear: Vec<OccId>, unrestricted: Vec<OccId>, absorbs: bool) -> Self {
        Self {
            linear,
            unrestricted,
            absorbs,
        }
    }
}

impl Saturated {
    /// Returns the saturated set of `sequents` under the polarity
    /// `positive`.
    pub fn new(positive: Vec<Sign>, sequents: Vec<Stable>) -> Self {
        Self { positive, sequents }
    }
}

/// The basis of the markings from which a Horn program's target can be
/// covered, as a backward search left it, which shows the goal
/// unprovable.
///
/// The goal is read as a Petri net. A place is an atom, or a class of
/// interchangeable clauses used once, which holds a ticket per unused
/// clause. A clause is a transition: it takes its body's literals (and a
/// ticket of its class, when it is used once) and gives its head's. The
/// initial marking is the goal's markings with every ticket; the target
/// is the goal's own body. A proof of the goal fires a sequence of
/// clauses from the initial marking to a marking at least the target, in
/// every mode: weakening only drops tokens.
///
/// The markings at least one of the basis hold every marking at least the
/// target, and with a marking `m` every marking from which a clause leads
/// to `m`. So they hold every marking of such a sequence, the initial one
/// included, unless none is at most it. The basis is all the backward
/// search kept, smaller markings and the ones they made redundant.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Basis {
    /// The sign of the bodies' literals and the goal's: [`Sign::Atom`]
    /// where they are atoms, [`Sign::Dual`] where they are negated. Heads
    /// and markings have the other.
    pub body: Sign,
    /// The goal's clauses used once, by occurrence, in classes of clauses
    /// with the same body and head. A marking counts each class's
    /// tickets.
    pub classes: Vec<Vec<OccId>>,
    /// The markings of the basis.
    pub markings: Vec<Marking>,
}

/// A marking of a [`Basis`]: tokens on atoms and tickets of classes.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Marking {
    /// The atoms with tokens, by increasing index, each with its count,
    /// above zero.
    pub atoms: Vec<(Atom, u32)>,
    /// The classes with tickets, by increasing index into
    /// [`Basis::classes`], each with its count, above zero.
    pub tickets: Vec<(u32, u32)>,
}

impl Basis {
    /// Returns the basis of `markings`, over the clauses used once in
    /// `classes`, for bodies of the sign `body`.
    pub fn new(body: Sign, classes: Vec<Vec<OccId>>, markings: Vec<Marking>) -> Self {
        Self {
            body,
            classes,
            markings,
        }
    }
}

impl Marking {
    /// Returns the marking of the tokens `atoms` and the tickets
    /// `tickets`.
    pub fn new(atoms: Vec<(Atom, u32)>, tickets: Vec<(u32, u32)>) -> Self {
        Self { atoms, tickets }
    }
}

impl Refutation {
    /// The words of the refutations, as [`Refutation::name`] gives them and
    /// the JSON form's `kind` writes them.
    pub const NAMES: &'static [&'static str] = &[
        "exhausted",
        "unbalanced",
        "one_sided",
        "equation",
        "state_equation",
        "classical",
        "saturated",
        "unbalanced_symbol",
        "one_sided_symbol",
        "basis",
    ];

    /// Returns the refutation's word, its `kind` in the JSON form.
    pub const fn name(&self) -> &'static str {
        match self {
            Refutation::Exhausted => "exhausted",
            Refutation::Unbalanced(_) => "unbalanced",
            Refutation::OneSided(_) => "one_sided",
            Refutation::Equation(_) => "equation",
            Refutation::StateEquation(_) => "state_equation",
            Refutation::Classical(_) => "classical",
            Refutation::Saturated(_) => "saturated",
            Refutation::UnbalancedSymbol(_) => "unbalanced_symbol",
            Refutation::OneSidedSymbol(_) => "one_sided_symbol",
            Refutation::Basis(_) => "basis",
        }
    }

    /// Writes the refutation as a phrase. Each atom and predicate is
    /// written by its name in `sequent`, or by its number where there is
    /// no sequent. The literals of a count are spoken of in the `sides`
    /// the sequent is read with.
    fn write(&self, f: &mut Formatter<'_>, sequent: Option<&Sequent>, sides: Sides) -> FmtResult {
        let intuitionistic = matches!(sides, Sides::Intuitionistic { .. });
        // The wording of a count: two-sided where the sequent was written
        // with hypotheses, with where the counted literals lie.
        let words = |counted: &dyn Fn(Atom) -> bool, least: i32| match (sides, sequent) {
            (Sides::Two, Some(sequent)) => Words::Two(placed(sequent, counted, least > 0)),
            // Under the reading an atom in output position is the
            // positive literal, so the literals' signs are their sides.
            (Sides::Intuitionistic { whole }, sequent) => Words::Intuitionistic {
                fewer_absent: whole
                    && sequent.is_some_and(|s| placed(s, counted, least > 0).fewer_absent),
            },
            (Sides::One | Sides::Two, _) => Words::One,
        };
        let of_symbol = |symbol: Symbol| {
            move |a: Atom| {
                sequent
                    .and_then(|s| s.atom_predicate(a))
                    .is_some_and(|(s, _)| s == symbol)
            }
        };
        let names = sequent.map(Sequent::atom_names);
        let name = |atom: Atom| match names.and_then(|names| names.get(atom.index())) {
            Some(name) => name.clone(),
            None => format!("#{}", atom.get()),
        };
        // A predicate as its literals read: its name, with its arguments
        // left out.
        let predicate = |symbol: Symbol| match sequent.and_then(|s| s.symbols().get(symbol.index()))
        {
            Some((name, _)) => format!("{name}(…)"),
            None => format!("#{}(…)", symbol.get()),
        };
        match self {
            Refutation::UnbalancedSymbol(UnbalancedSymbol {
                symbol,
                least,
                most,
            }) => write_unbalanced(
                f,
                &predicate(*symbol),
                *least,
                *most,
                (None, true),
                words(&of_symbol(*symbol), *least),
            ),
            Refutation::OneSidedSymbol(OneSidedSymbol {
                symbol,
                least,
                most,
                copied,
            }) => write_unbalanced(
                f,
                &predicate(*symbol),
                *least,
                *most,
                (Some(*copied), true),
                words(&of_symbol(*symbol), *least),
            ),
            Refutation::Exhausted => f.write_str("the search was exhaustive"),
            Refutation::Unbalanced(Unbalanced { atom, least, most }) => write_unbalanced(
                f,
                &name(*atom),
                *least,
                *most,
                (None, false),
                words(&|a| a == *atom, *least),
            ),
            Refutation::OneSided(OneSided {
                atom,
                least,
                most,
                copied,
            }) => write_unbalanced(
                f,
                &name(*atom),
                *least,
                *most,
                (Some(*copied), false),
                words(&|a| a == *atom, *least),
            ),
            Refutation::Equation(Equation {
                formulas,
                needed,
                tensors,
                pars,
                ones,
                bottoms,
                mix,
            }) => {
                // A count below zero takes the minus sign the expression uses.
                let needed = if *needed < 0 {
                    format!("−{}", needed.unsigned_abs())
                } else {
                    needed.to_string()
                };
                let exactly = if *mix { "at least " } else { "exactly " };
                if intuitionistic {
                    // A one-sided `⊗` is a `⊗` right of `⊢` or a `⊸` left
                    // of it, a `⅋` the other two, a `1` a `1` right and a
                    // `⊥` a `1` left.
                    write!(
                        f,
                        "the count equation fails: a provable sequent of IMLL has {exactly}#⊗ᵣ + \
                         #⊸ₗ − #⊗ₗ − #⊸ᵣ − #1ᵣ + #1ₗ + 2 formulas (ᵣ right of ⊢, ₗ left of it, an \
                         implication's antecedent on the other side), here {tensors} − {pars} − \
                         {ones} + {bottoms} + 2 = {needed}, and this one has {formulas}",
                    )
                } else {
                    write!(
                        f,
                        "the count equation fails: a provable one-sided sequent of MLL has \
                         {exactly}#⊗ − #⅋ − #1 + #⊥ + 2 formulas, here {tensors} − {pars} − \
                         {ones} + {bottoms} + 2 = {needed}, and this one has {formulas}",
                    )
                }
            }
            Refutation::StateEquation(StateEquation { atoms, clauses, .. }) => {
                f.write_str(
                    "the state equation of the Petri net has no solution, so no firing of its \
                     clauses yields the goal's atoms: weighting",
                )?;
                let once = !clauses.is_empty();
                for (i, &(atom, weight)) in atoms.iter().enumerate() {
                    let sign = if weight < 0 { "−" } else { "" };
                    let between = match i {
                        0 => " each",
                        _ if i + 1 == atoms.len() && !once => " and",
                        _ => ",",
                    };
                    write!(
                        f,
                        "{between} {} by {sign}{}",
                        name(atom),
                        weight.unsigned_abs()
                    )?;
                }
                if once {
                    let between = if atoms.is_empty() { "" } else { " and" };
                    f.write_str(between)?;
                    f.write_str(" the clauses used once by weights of their own")?;
                }
                f.write_str(
                    ", no clause that can fire raises the weighted count of the atoms, and the goal \
                     asks it raised",
                )
            }
            Refutation::Classical(Classical { assignment }) => {
                f.write_str("the classical reading is false")?;
                let truths: Vec<String> = (0..assignment.len())
                    .filter(|&a| assignment[a])
                    .map(|a| name(Atom::new(a as u32)))
                    .collect();
                match truths.len() {
                    _ if assignment.is_empty() => {}
                    0 => f.write_str(" where every atom is false")?,
                    n if n == assignment.len() => f.write_str(" where every atom is true")?,
                    n => write!(
                        f,
                        " where {} {} true and every other atom false",
                        list(&truths),
                        if n == 1 { "is" } else { "are" }
                    )?,
                }
                f.write_str(if intuitionistic {
                    " (! erased, ⊗ and & as and, ⊕ as or, ⊸ as implies, 1 and ⊤ true, 0 false), \
                     and every provable sequent reads as a tautology"
                } else {
                    " (! and ? erased, ⊗ and & as and, ⅋ and ⊕ as or, 1 and ⊤ true, ⊥ and 0 false), \
                     and every provable sequent reads as a tautology"
                })
            }
            Refutation::Saturated(Saturated { sequents, .. }) => write!(
                f,
                "the search saturated: its {} {} closed under the rules, and {} the goal",
                sequents.len(),
                if sequents.len() == 1 {
                    "sequent is"
                } else {
                    "sequents are"
                },
                if sequents.len() == 1 {
                    "it does not yield"
                } else {
                    "none of them yields"
                },
            ),
            Refutation::Basis(Basis { markings, .. }) => write!(
                f,
                "read as a Petri net, the goal's target cannot be covered: a backward search \
                 from it saturated, and none of the {} {} it kept is at most the initial marking",
                markings.len(),
                if markings.len() == 1 {
                    "marking"
                } else {
                    "markings"
                },
            ),
        }
    }
}

/// Returns names as an English list: `a`, `a and b`, `a, b and c`.
fn list(names: &[String]) -> String {
    match names {
        [] => String::new(),
        [one] => one.clone(),
        [init @ .., last] => format!("{} and {last}", init.join(", ")),
    }
}

/// How a refutation's phrase speaks of the sides of `⊢`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Sides {
    /// Of the one-sided sequent: an atom and its negation.
    One,
    /// Of a classical sequent written with formulas left of `⊢`: an atom
    /// left and right of it.
    Two,
    /// Of an intuitionistic sequent: an atom's positive and negative
    /// occurrences.
    Intuitionistic {
        /// Whether the sequent is refuted as a whole, not a goal within
        /// it, so that its literals are the counted ones.
        whole: bool,
    },
}

/// The wording of a count of literals, as [`Sides`] picks it.
#[derive(Clone, Copy, Debug)]
enum Words {
    /// One-sided.
    One,
    /// Two-sided, with where the counted literals lie.
    Two(Placed),
    /// Intuitionistic, with whether the side with fewer of the counted
    /// literals has none.
    Intuitionistic {
        /// The side with fewer of them has none.
        fewer_absent: bool,
    },
}

/// Where the literals of a count lie in a classical sequent written with
/// formulas left of `⊢`, a positive literal counting as right of it and a
/// negative one as left.
#[derive(Clone, Copy, Debug)]
struct Placed {
    /// The side with fewer of them has none.
    fewer_absent: bool,
    /// One counts on the other side than the formula it stands in: it is
    /// negated, or an implication's antecedent.
    crossed: bool,
}

/// Returns where the literals of the atoms `counted` picks lie among the
/// roots of `sequent`, the positive ones being the more where
/// `more_right` is set.
fn placed(sequent: &Sequent, counted: &dyn Fn(Atom) -> bool, more_right: bool) -> Placed {
    /// A positive literal below a term: right of `⊢`.
    const RIGHT: u8 = 1;
    /// A negative literal below a term: left of `⊢`.
    const LEFT: u8 = 2;
    // A pass over the arena, whose terms refer only to terms before them:
    // the sides of the counted literals below each term.
    let mut below: Vec<u8> = Vec::with_capacity(sequent.terms().len());
    for term in sequent.terms() {
        let bits = match *term {
            Term::Atom(a) => {
                if counted(a) {
                    RIGHT
                } else {
                    0
                }
            }
            Term::DualAtom(a) => {
                if counted(a) {
                    LEFT
                } else {
                    0
                }
            }
            Term::One | Term::Bot | Term::Top | Term::Zero => 0,
            Term::Tensor(a, b) | Term::Par(a, b) | Term::With(a, b) | Term::Plus(a, b) => {
                below[a.index()] | below[b.index()]
            }
            Term::Bang(a) | Term::Quest(a) | Term::Forall(a) | Term::Exists(a) => below[a.index()],
        };
        below.push(bits);
    }
    let antecedents = sequent.antecedents().map_or(0, |n| n as usize);
    let (mut all, mut crossed) = (0, false);
    for (i, root) in sequent.roots().iter().enumerate() {
        let bits = below[root.index()];
        all |= bits;
        // A hypothesis is held negated, so a literal of its own side is
        // negative; a positive one below it was negated or an antecedent.
        crossed |= bits & if i < antecedents { RIGHT } else { LEFT } != 0;
    }
    Placed {
        fewer_absent: all & if more_right { LEFT } else { RIGHT } == 0,
        crossed,
    }
}

/// Writes why the literals of the atom `name` cannot all meet in axioms:
/// one literal outnumbers the other between `least` and `most` times.
/// Where `copied` is given, the count is outside the exponentials, and
/// every literal below one has that sign. Where `predicate` is set, the
/// count is over every atom of a predicate. The `words` say how: of the
/// one-sided sequent; of a two-sided classical one, with the atom left and
/// right of `⊢`; or of an intuitionistic one, its positive occurrences
/// counted right of `⊢` and its negative ones left of it.
fn write_unbalanced(
    f: &mut Formatter<'_>,
    name: &str,
    least: i32,
    most: i32,
    (copied, predicate): (Option<Sign>, bool),
    words: Words,
) -> FmtResult {
    let over = if predicate {
        ", every atom of the predicate counted, since one has a bound variable"
    } else {
        ""
    };
    let (a, b) = (least.unsigned_abs(), most.unsigned_abs());
    let (a, b) = (a.min(b), a.max(b));
    let excess = if a == b {
        format!("{a}")
    } else {
        format!("{a} to {b}")
    };
    let times = if a == 1 && b == 1 { "time" } else { "times" };
    let alternatives = if a == b {
        ""
    } else {
        ", whichever additive alternatives a proof takes"
    };
    if let Words::Two(placed) = words {
        // A positive literal counts as right of `⊢`.
        let (more, fewer) = if least > 0 {
            ("right", "left")
        } else {
            ("left", "right")
        };
        let crossed = if placed.crossed {
            " (a negated formula or an implication's antecedent counts on the other side)"
        } else {
            ""
        };
        if copied.is_none() && placed.fewer_absent {
            return write!(
                f,
                "{name} on the {more} has no {name} on the {fewer} to meet in an axiom{over}{crossed}"
            );
        }
        let (place, copies) = match copied {
            Some(sign) => (
                " outside the exponentials",
                format!(
                    ", and every occurrence of it below one, which copies add, is on the {}",
                    match sign {
                        Sign::Atom => "right",
                        Sign::Dual => "left",
                    }
                ),
            ),
            None => ("", String::new()),
        };
        return write!(
            f,
            "{name} occurs {excess} more {times} on the {more} than on the {fewer}{place}\
             {alternatives}{copies}{over}, so they cannot all meet in axioms{crossed}",
        );
    }
    if let Words::Intuitionistic { fewer_absent } = words {
        // An atom in output position is the positive literal, which
        // counts as right of `⊢`.
        let (more, fewer) = if least > 0 {
            ("right", "left")
        } else {
            ("left", "right")
        };
        if copied.is_none() && fewer_absent {
            return write!(
                f,
                "{name} on the {more} has no {name} on the {fewer} to meet in an axiom{over} (an \
                 implication's antecedent counts on the other side)"
            );
        }
        let (place, copies) = match copied {
            Some(sign) => (
                " outside the exponentials",
                format!(
                    ", and every occurrence of it below one, which copies add, is on the {}",
                    match sign {
                        Sign::Atom => "right",
                        Sign::Dual => "left",
                    }
                ),
            ),
            None => ("", String::new()),
        };
        return write!(
            f,
            "{name} occurs {excess} more {times} on the {more} than on the {fewer}{place}\
             {alternatives}{copies}{over}, so they cannot all meet in axioms (an implication's \
             antecedent counts on the other side)",
        );
    }
    let (more, fewer) = if least > 0 {
        (name.to_owned(), format!("~{name}"))
    } else {
        (format!("~{name}"), name.to_owned())
    };
    // The copied literal is the one `copied` names. The engine's
    // refutations make it the one that outnumbers the other; a refutation
    // read from JSON says what its fields say.
    let (place, copies) = match copied {
        Some(sign) => {
            let literal = match sign {
                Sign::Atom => name.to_owned(),
                Sign::Dual => format!("~{name}"),
            };
            (
                "outside the exponentials of the one-sided sequent",
                format!(
                    ", and every literal of the atom below one, which copies add, is {literal}"
                ),
            )
        }
        None => ("in the one-sided sequent", String::new()),
    };
    write!(
        f,
        "{more} occurs {excess} more {times} than {fewer} {place}{alternatives}{copies}{over}, so \
         they cannot all meet in axioms",
    )
}

impl Display for Refutation {
    /// Writes the refutation as a phrase, such as `the search was
    /// exhaustive`, with every atom by its number, as in `#0`.
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        self.write(f, None, Sides::One)
    }
}

/// A refutation with what it refutes: the sequent, the goal within it if
/// that is not the sequent's roots, and the mode. A search's
/// [`Verdict::Unprovable`](super::Verdict::Unprovable) carries one, as a
/// [`Verdict::Proved`](super::Verdict::Proved) carries a proof.
///
/// Its [`Display`] says why the goal is unprovable, with every atom by
/// its name. [`check`](Self::check) verifies a disproof read from
/// elsewhere.
///
/// # Examples
///
#[cfg_attr(feature = "parse", doc = "```")]
#[cfg_attr(not(feature = "parse"), doc = "```ignore")]
/// use deduxit::linear::{Mode, Options, Refutation, Sequent, Verdict, prove};
///
/// let sequent: Sequent = "a |- b".parse()?;
/// let outcome = prove(&sequent, Mode::CLASSICAL, &Options::default())?;
/// let Verdict::Unprovable(disproof) = outcome.verdict else {
///     panic!("a |- b has no proof");
/// };
/// // `~a` has no `a` to meet in an axiom.
/// assert!(matches!(disproof.refutation(), Refutation::Unbalanced(_)));
/// println!("{disproof}");
/// disproof.check()?;
/// # Ok::<(), deduxit::Error>(())
/// ```
///
/// # JSON
///
/// With the feature `serialize`, a disproof is the document `{"version":
/// 1, "sequent": …, "mode": …, "refutation": …, "goal": […]}`. The
/// refutation has the form [`Refutation`] describes, and `goal` lists the
/// goal's members only for a goal other than the roots. An unprovable
/// outcome writes the same keys, so it reads back as its disproof.
///
/// Reading counts the sequent against the limits. It checks that every
/// atom and occurrence the refutation and the goal name is in the
/// sequent (`Error::IndexOutOfBounds` otherwise), but not the refutation
/// itself: that is what [`check`](Self::check) does. A refutation kind
/// the reader does not know is refused (`Error::Json`, kind malformed).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Disproof {
    /// The sequent.
    sequent: Sequent,
    /// The goal refuted, `None` for the sequent's roots.
    goal: Option<Box<[Member]>>,
    /// The mode it is refuted in.
    mode: Mode,
    /// Why it is unprovable.
    refutation: Refutation,
}

impl Disproof {
    /// Returns the disproof of a sequent in a mode.
    pub fn new(sequent: Sequent, mode: Mode, refutation: Refutation) -> Self {
        Self {
            sequent,
            goal: None,
            mode,
            refutation,
        }
    }

    /// Returns the disproof as one of a goal within the sequent.
    #[must_use]
    pub(crate) fn of_goal(self, goal: Box<[Member]>) -> Self {
        Self {
            goal: Some(goal),
            ..self
        }
    }

    /// Returns the sequent.
    pub fn sequent(&self) -> &Sequent {
        &self.sequent
    }

    /// Returns the goal refuted, `None` for the sequent's roots.
    pub fn goal(&self) -> Option<&[Member]> {
        self.goal.as_deref()
    }

    /// Returns the mode it is refuted in.
    pub fn mode(&self) -> Mode {
        self.mode
    }

    /// Returns why the goal is unprovable.
    pub fn refutation(&self) -> &Refutation {
        &self.refutation
    }
}

impl Display for Disproof {
    /// Writes the refutation as a phrase, with every atom by its name. In
    /// an intuitionistic mode it uses the sequent's two-sided terms, and
    /// so does a count of a classical sequent written with formulas left
    /// of `⊢`, refuted as a whole: an atom left and right of `⊢`.
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        let sides = if self.mode.is_intuitionistic() {
            Sides::Intuitionistic {
                whole: self.goal.is_none(),
            }
        } else if self.goal.is_none() && self.sequent.antecedents().is_some_and(|n| n > 0) {
            Sides::Two
        } else {
            Sides::One
        };
        self.refutation.write(f, Some(&self.sequent), sides)
    }
}

/// Why [`Disproof::check`] does not accept a refutation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum RefutationFault {
    /// The refutation is no certificate. An exhausted search rests on the
    /// search itself, which the check cannot verify.
    #[error("a refutation of kind `{kind}` rests on the search and has no check")]
    Uncertified {
        /// The refutation's word ([`Refutation::name`]).
        kind: &'static str,
    },
    /// The refutation's invariant does not hold for this goal and mode.
    /// Weakening discards literals and connectives, a `⊤` absorbs them,
    /// copies add them, and the count equation holds in the
    /// multiplicative fragment only.
    #[error("the {invariant} is no invariant here: {why}")]
    NotApplicable {
        /// The invariant, such as `balance of the atom` or `count
        /// equation`.
        invariant: &'static str,
        /// What defeats it.
        why: &'static str,
    },
    /// The refutation's numbers are not the goal's. It names an atom or an
    /// occurrence the sequent lacks, or states counts that differ from the
    /// goal's.
    #[error("the refutation's numbers are not those of the sequent")]
    Mismatch,
    /// The numbers are the goal's, and they allow a proof. The atom's
    /// excess can be zero, the count equation holds, or some contribution
    /// to the weighted count is zero or below.
    #[error("the counts allow a proof")]
    Holds,
    /// The assignment has another number of values than the sequent has
    /// atoms.
    #[error("the assignment gives {values} values for {atoms} atoms")]
    Assignment {
        /// The atoms of the sequent.
        atoms: usize,
        /// The values given.
        values: usize,
    },
    /// The assignment makes the classical reading true.
    #[error("the classical reading is true under the assignment")]
    Satisfied,
    /// A sequent of a saturated set is not a sequent of the mode. Under
    /// the intuitionistic reading it has other than one member in output
    /// position, or more than one where it absorbs.
    #[error("a sequent of the saturated set is no sequent of the mode")]
    Unshaped,
    /// A rule applied to sequents of a saturated set derives one that
    /// could stand in a proof and that none of them stands for.
    #[error("the saturated set is not closed under the rules")]
    NotClosed,
    /// The sequents of a saturated set stand for every stable sequent of
    /// the goal's asynchronous phase, so they do not rule a proof out.
    #[error("the saturated set gives every stable sequent the goal needs")]
    Filled,
    /// A clause fired backward from a marking of a basis needs a marking
    /// that no marking of the basis is at most.
    #[error("the basis is not closed under the clauses fired backward")]
    Open,
    /// A marking of a basis is at most the initial marking, so the basis
    /// does not rule a firing sequence out.
    #[error("a marking of the basis is at most the initial marking")]
    Reached,
    /// No marking of a basis is at most the target, so the basis says
    /// nothing of the target.
    #[error("no marking of the basis is at most the target")]
    Missed,
}

impl RefutationFault {
    /// Returns the fault's word, as an error's details write it.
    pub const fn name(&self) -> &'static str {
        match self {
            Self::Uncertified { .. } => "uncertified",
            Self::NotApplicable { .. } => "not_applicable",
            Self::Mismatch => "mismatch",
            Self::Holds => "holds",
            Self::Assignment { .. } => "assignment",
            Self::Satisfied => "satisfied",
            Self::Unshaped => "unshaped",
            Self::NotClosed => "not_closed",
            Self::Filled => "filled",
            Self::Open => "open",
            Self::Reached => "reached",
            Self::Missed => "missed",
        }
    }
}

impl Disproof {
    /// Checks that the refutation proves the goal unprovable in the mode,
    /// and returns `Ok` when it does.
    ///
    /// The check shares no code with the search. Each kind is checked by
    /// the invariant it rests on:
    ///
    /// - An assignment ([`Refutation::Classical`]): the goal's classical
    ///   reading, evaluated under it, must be false.
    /// - An unbalanced atom ([`Refutation::Unbalanced`]): its excess of
    ///   positive over negative literals is computed as an interval over
    ///   the additive alternatives. `⊗` and `⅋` add their sides', and `&`
    ///   and `⊕` take the hull of theirs. The interval must be the one
    ///   stated and exclude zero. The mode must not be affine, and the goal
    ///   must have no `⊤` and no `!` or `?` over the atom.
    /// - The count equation ([`Refutation::Equation`]): the goal's
    ///   formulas, `⊗`, `⅋`, `1` and `⊥` must be the ones stated and fail
    ///   it. The goal must be in the multiplicative fragment with units,
    ///   the mode must not be affine, and Mix must be stated exactly where
    ///   the mode allows it.
    /// - A saturated set ([`Refutation::Saturated`]): the rules of the
    ///   focused calculus are read off the forest again, under the set's
    ///   polarity. Each is applied to every choice of premises among the
    ///   set's sequents, and so is Mix where the mode has it. Each sequent
    ///   derived must be one that a sequent of the set stands for, or one
    ///   that no proof holds. The set's sequents must be shaped as the
    ///   mode's are, and some stable sequent of the goal's phase must be
    ///   one that none of them stands for.
    /// - A one-sided count ([`Refutation::OneSided`]): its interval is
    ///   computed outside the goal's exponentials, as an unbalanced atom's,
    ///   with the side that copies reach left open. The sign of the atom's
    ///   literals below the exponentials must be the one stated. Then the
    ///   weighted test with the weight `±1` on the atom must accept the
    ///   goal.
    /// - A state equation ([`Refutation::StateEquation`]): the weighted
    ///   test with the stated weights must accept the goal, read as a Horn
    ///   program with positive bodies, else negative ones (the weights
    ///   negated). The atoms that no firing reaches are declared dead. In
    ///   affine mode the test counts the formulas with one output.
    /// - A basis ([`Refutation::Basis`]): the goal is read as a Petri net
    ///   again, with the bodies' sign stated. The classes must be its
    ///   clauses used once, each class of one body and head. Some marking
    ///   of the basis must be at most the target, none at most the initial
    ///   marking, and for every marking `m` and every clause that can ever
    ///   fire with a head on `m`'s places, some marking must be at most
    ///   `max(m − head, 0) + body`, the least marking from which the clause
    ///   leads to one at least `m`.
    ///
    /// The predicate forms, [`Refutation::UnbalancedSymbol`] and
    /// [`Refutation::OneSidedSymbol`], are checked as their atom forms,
    /// over every atom of the predicate.
    ///
    /// The weighted test is the one deduxit's Rocq library proves sound for
    /// any weights. No formula of the goal may absorb, and every
    /// contribution to the weighted count of its literals must be above
    /// zero, a `?` formula's copies included.
    ///
    /// These seven kinds are certificates. Every derivation keeps the
    /// invariant that each of the five others breaks, the set holds a
    /// sequent for every stable one of every proof, and the basis's upward
    /// closure every marking of every firing sequence a proof gives.
    ///
    /// # Cost
    ///
    /// Time and memory are linear in the goal's occurrences, a member
    /// repeated counted each time, for every kind but the saturated set
    /// and the basis. A basis's check compares, for every marking and
    /// clause, a marking with the basis's markings: quadratic in the basis
    /// times the clauses, over the markings' places.
    /// The set's check applies every rule to every choice of its sequents.
    /// For a set a search gave, that takes at most as long as the search
    /// that saturated. The check has no stop, though. For a set read from
    /// elsewhere it can take time exponential in the `&` under `⅋` of the
    /// goal, and polynomial in the set's size, of the degree of a rule's
    /// holes (a set that repeats one sequent). So a caller checks such a
    /// set within a time limit of its own.
    ///
    /// # Errors
    ///
    /// [`Error::Refutation`](crate::Error::Refutation) with the
    /// [`RefutationFault`]. The error's kind is invalid for a refutation
    /// that does not hold, and unsupported for an exhausted search
    /// ([`RefutationFault::Uncertified`]). The errors of
    /// [`Forest::new`](crate::linear::Forest::new) for a sequent past the
    /// default limits.
    ///
    /// # Examples
    ///
    /// A disproof sent as JSON, read back and checked, and a changed one
    /// that the check refuses:
    ///
    #[cfg_attr(all(feature = "parse", feature = "serialize"), doc = "```")]
    #[cfg_attr(not(all(feature = "parse", feature = "serialize")), doc = "```ignore")]
    /// use deduxit::linear::{Disproof, Mode, Options, Refutation, Sequent, Verdict, prove};
    ///
    /// let sequent: Sequent = "|- a & b, ~a".parse()?;
    /// let outcome = prove(&sequent, Mode::CLASSICAL, &Options::default())?;
    /// let Verdict::Unprovable(disproof) = outcome.verdict else {
    ///     panic!("the sequent has no proof");
    /// };
    /// // False where `a` is true and `b` false.
    /// assert!(matches!(disproof.refutation(), Refutation::Classical(_)));
    /// let json = serde_json::to_string(&disproof)?;
    ///
    /// let read: Disproof = serde_json::from_str(&json)?;
    /// read.check()?;
    ///
    /// // Under this assignment the reading is true.
    /// let changed = json.replace("[true,false]", "[true,true]");
    /// let read: Disproof = serde_json::from_str(&changed)?;
    /// assert_eq!(read.check().unwrap_err().code(), "invalid_refutation");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn check(&self) -> Result<(), crate::Error> {
        let fault = |fault| crate::Error::Refutation(fault);
        let forest = crate::linear::occurrences::Forest::new(&self.sequent)?;
        // The counts carry over to quantifiers, since a quantifier's rule
        // changes none of them. An assignment and a saturated set are
        // propositional.
        if forest.has_binders()
            && matches!(
                self.refutation,
                Refutation::Classical(_) | Refutation::Saturated(_) | Refutation::Basis(_)
            )
        {
            return Err(fault(RefutationFault::NotApplicable {
                invariant: "this refutation",
                why: "it is propositional, and the sequent has a quantifier",
            }));
        }
        let goal: Vec<OccId> = match &self.goal {
            None => forest.roots().to_vec(),
            Some(members) => members
                .iter()
                .map(|m| {
                    m.occurrence(&forest)
                        .ok_or(fault(RefutationFault::Mismatch))
                })
                .collect::<Result<_, _>>()?,
        };
        match &self.refutation {
            Refutation::Classical(classical) => check_classical(&forest, &goal, classical),
            Refutation::Unbalanced(unbalanced) => {
                check_unbalanced(&forest, &goal, self.mode, unbalanced)
            }
            Refutation::UnbalancedSymbol(unbalanced) => {
                check_unbalanced_symbol(&forest, &goal, self.mode, unbalanced)
            }
            Refutation::Equation(equation) => check_equation(&forest, &goal, self.mode, equation),
            Refutation::Saturated(saturated) => {
                match super::saturated::check(&forest, &goal, self.mode, saturated, &mut |_| false)
                {
                    Ok(()) => Ok(()),
                    Err(super::saturated::Failed::Fault(fault)) => Err(fault),
                    Err(super::saturated::Failed::Stopped) => {
                        unreachable!("a check without a stop runs to its end")
                    }
                }
            }
            Refutation::OneSided(one_sided) => {
                check_one_sided(&forest, &goal, self.mode, one_sided)
            }
            Refutation::OneSidedSymbol(one_sided) => {
                check_one_sided_symbol(&forest, &goal, self.mode, one_sided)
            }
            Refutation::StateEquation(equation) => {
                super::weighted::state_equation(&forest, &goal, self.mode, equation).map(|_| ())
            }
            Refutation::Basis(basis) => {
                match super::basis::check(&forest, &goal, basis, &mut |_| false) {
                    Ok(()) => Ok(()),
                    Err(super::basis::Failed::Fault(fault)) => Err(fault),
                    Err(super::basis::Failed::Stopped) => {
                        unreachable!("a check without a stop runs to its end")
                    }
                }
            }
            refutation @ Refutation::Exhausted => Err(RefutationFault::Uncertified {
                kind: refutation.name(),
            }),
        }
        .map_err(fault)
    }
}

/// Checks that `classical` falsifies the classical reading of the goal.
fn check_classical(
    forest: &crate::linear::occurrences::Forest,
    goal: &[OccId],
    classical: &Classical,
) -> Result<(), RefutationFault> {
    use crate::linear::sequents::Kind::*;
    let (atoms, values) = (
        forest.sequent().atom_names().len(),
        classical.assignment.len(),
    );
    if atoms != values {
        return Err(RefutationFault::Assignment { atoms, values });
    }
    let value = |o: OccId| {
        let atom = forest.atom(o).map(|a| classical.assignment[a.index()]);
        atom.unwrap_or(false)
    };
    let mut truth = vec![false; forest.len()];
    for &member in goal {
        // The forest is in preorder: a subtree's occurrences follow its
        // root. Walked from the end, operands come before their connective.
        for o in forest.subtree(member).rev() {
            let side = |side: Option<OccId>| side.is_some_and(|s| truth[s.index()]);
            let (l, r) = (side(forest.left(o)), side(forest.right(o)));
            truth[o.index()] = match forest.kind(o) {
                Atom => value(o),
                DualAtom => !value(o),
                One | Top => true,
                Bot | Zero => false,
                Tensor | With => l && r,
                Par | Plus => l || r,
                Bang | Quest => l,
                Forall | Exists => {
                    unreachable!("an assignment of a sequent with a binder is refused")
                }
            };
        }
        if truth[member.index()] {
            return Err(RefutationFault::Satisfied);
        }
    }
    Ok(())
}

/// Checks that the atom of `unbalanced` has the stated excess interval in
/// the goal, and that the interval excludes zero. In a sequent with a
/// quantifier, the balance is an invariant only where no atom of the
/// atom's predicate has a bound variable, since an instance of such an
/// atom could be this one.
fn check_unbalanced(
    forest: &crate::linear::occurrences::Forest,
    goal: &[OccId],
    mode: Mode,
    unbalanced: &Unbalanced,
) -> Result<(), RefutationFault> {
    let atom = unbalanced.atom;
    let sequent = forest.sequent();
    if atom.index() >= sequent.atom_names().len() {
        return Err(RefutationFault::Mismatch);
    }
    if let Some((symbol, _)) = sequent.atom_predicate(atom)
        && (0..sequent.atom_names().len()).any(|a| {
            let a = Atom::new(a as u32);
            sequent.atom_predicate(a).map(|(s, _)| s) == Some(symbol) && sequent.atom_is_open(a)
        })
    {
        return Err(RefutationFault::NotApplicable {
            invariant: "balance of the atom",
            why: "an atom of its predicate has a bound variable, whose instance may be it",
        });
    }
    check_balance(
        forest,
        goal,
        mode,
        |o| forest.atom(o) == Some(atom),
        (unbalanced.least, unbalanced.most),
    )
}

/// Checks that the literals of the predicate of `unbalanced`, every atom
/// of it counted, have the stated excess interval in the goal, and that
/// it excludes zero, where the balance is an invariant.
fn check_unbalanced_symbol(
    forest: &crate::linear::occurrences::Forest,
    goal: &[OccId],
    mode: Mode,
    unbalanced: &UnbalancedSymbol,
) -> Result<(), RefutationFault> {
    let sequent = forest.sequent();
    if unbalanced.symbol.index() >= sequent.symbols().len() {
        return Err(RefutationFault::Mismatch);
    }
    check_balance(
        forest,
        goal,
        mode,
        |o| {
            forest
                .atom(o)
                .and_then(|a| sequent.atom_predicate(a))
                .is_some_and(|(s, _)| s == unbalanced.symbol)
        },
        (unbalanced.least, unbalanced.most),
    )
}

/// Checks that the literals `counted` picks have the excess interval
/// `stated` in the goal, and that it excludes zero, where the balance is
/// an invariant. It is one because every axiom joins one positive and one
/// negative literal of a group (an atom, or a predicate). `⊗` and `⅋` add
/// their sides' excesses, `&` and `⊕` take the hull of theirs, and a
/// quantifier's rule changes none.
fn check_balance(
    forest: &crate::linear::occurrences::Forest,
    goal: &[OccId],
    mode: Mode,
    counted: impl Fn(OccId) -> bool,
    stated: (i32, i32),
) -> Result<(), RefutationFault> {
    use crate::linear::sequents::Kind::*;
    const INVARIANT: &str = "balance of the atom";
    if mode.affine {
        return Err(RefutationFault::NotApplicable {
            invariant: INVARIANT,
            why: "weakening discards literals",
        });
    }
    // Per occurrence, the least and the greatest excess, or `None` where a
    // `⊤` absorbs or copies of the counted literals can add any number.
    // They are sums of ±1 over the goal's members, each a subtree of the
    // forest's fewer than 2³² occurrences. Leaving an `i64` would take a
    // goal of more than 2³¹ members, which no memory holds.
    let mut span: Vec<Option<(i64, i64)>> = vec![None; forest.len()];
    let (mut least, mut most) = (0, 0);
    for &member in goal {
        for o in forest.subtree(member).rev() {
            let side = |side: Option<OccId>| side.and_then(|s| span[s.index()]);
            let (l, r) = (side(forest.left(o)), side(forest.right(o)));
            let literal = |e| Some(if counted(o) { (e, e) } else { (0, 0) });
            span[o.index()] = match forest.kind(o) {
                Atom => literal(1),
                DualAtom => literal(-1),
                One | Bot | Zero => Some((0, 0)),
                Top => None,
                Tensor | Par => l.zip(r).map(|((a, b), (c, d))| (a + c, b + d)),
                With | Plus => l.zip(r).map(|((a, b), (c, d))| (a.min(c), b.max(d))),
                Bang | Quest => l.filter(|&s| s == (0, 0)),
                Forall | Exists => l,
            };
        }
        let Some((lo, hi)) = span[member.index()] else {
            return Err(RefutationFault::NotApplicable {
                invariant: INVARIANT,
                why: "a ⊤ absorbs, or copies of the atom's literals add any number",
            });
        };
        (least, most) = (least + lo, most + hi);
    }
    if (least, most) != (i64::from(stated.0), i64::from(stated.1)) {
        Err(RefutationFault::Mismatch)
    } else if least <= 0 && 0 <= most {
        Err(RefutationFault::Holds)
    } else {
        Ok(())
    }
}

/// Checks that the atom of `one_sided` has the stated excess interval
/// outside the goal's exponentials, that its literals below them all have
/// the stated sign, and that copies of that sign cannot bring the excess
/// to zero. In a sequent with a quantifier, the count is an invariant
/// only where no atom of the atom's predicate has a bound variable.
fn check_one_sided(
    forest: &crate::linear::occurrences::Forest,
    goal: &[OccId],
    mode: Mode,
    one_sided: &OneSided,
) -> Result<(), RefutationFault> {
    let atom = one_sided.atom;
    let sequent = forest.sequent();
    if atom.index() >= sequent.atom_names().len() {
        return Err(RefutationFault::Mismatch);
    }
    if let Some((symbol, _)) = sequent.atom_predicate(atom)
        && (0..sequent.atom_names().len()).any(|a| {
            let a = Atom::new(a as u32);
            sequent.atom_predicate(a).map(|(s, _)| s) == Some(symbol) && sequent.atom_is_open(a)
        })
    {
        return Err(RefutationFault::NotApplicable {
            invariant: "one-sided count of the atom",
            why: "an atom of its predicate has a bound variable, whose instance may be it",
        });
    }
    check_copies(
        forest,
        goal,
        mode,
        |o| forest.atom(o) == Some(atom),
        (one_sided.least, one_sided.most, one_sided.copied),
    )
}

/// Checks that the literals of the predicate of `one_sided`, every atom of
/// it counted, have the stated excess interval outside the goal's
/// exponentials and the stated sign below them, and that copies cannot
/// bring the excess to zero.
fn check_one_sided_symbol(
    forest: &crate::linear::occurrences::Forest,
    goal: &[OccId],
    mode: Mode,
    one_sided: &OneSidedSymbol,
) -> Result<(), RefutationFault> {
    let sequent = forest.sequent();
    if one_sided.symbol.index() >= sequent.symbols().len() {
        return Err(RefutationFault::Mismatch);
    }
    check_copies(
        forest,
        goal,
        mode,
        |o| {
            forest
                .atom(o)
                .and_then(|a| sequent.atom_predicate(a))
                .is_some_and(|(s, _)| s == one_sided.symbol)
        },
        (one_sided.least, one_sided.most, one_sided.copied),
    )
}

/// Checks a one-sided count of the literals `counted` picks.
///
/// Outside the goal's exponentials, their excess interval must be the
/// stated one: `⊗` and `⅋` add their sides', `&` and `⊕` take the hull,
/// and a `!` or `?` counts nothing. Every one of them below an exponential
/// of the goal must have the stated sign `copied`. The interval must
/// exclude zero on the side that copies of that sign cannot reach: the
/// least above zero for `a`, the greatest below zero for `~a`. Then the
/// weighted test, with the weight `1` on `a` (`−1` for `~a`) and nothing
/// dead, must accept the goal: that is the theorem the count rests on.
/// The mode must not be affine, and the goal must have no `⊤`.
fn check_copies(
    forest: &crate::linear::occurrences::Forest,
    goal: &[OccId],
    mode: Mode,
    counted: impl Fn(OccId) -> bool,
    (least, most, copied): (i32, i32, Sign),
) -> Result<(), RefutationFault> {
    use crate::linear::sequents::Kind::*;
    const INVARIANT: &str = "one-sided count of the atom";
    if mode.affine {
        return Err(RefutationFault::NotApplicable {
            invariant: INVARIANT,
            why: "weakening discards literals",
        });
    }
    // Per occurrence, the least and the greatest excess outside the
    // exponentials, or `None` where a `⊤` absorbs. They are sums of ±1
    // over a member's subtree of fewer than 2³² occurrences, so within an
    // `i64` per member. The sum over the members is checked.
    let mut span: Vec<Option<(i64, i64)>> = vec![None; forest.len()];
    // Which signs of the counted literals lie below an exponential.
    let mut below = [false; 2];
    let (mut lo, mut hi) = (0i64, 0i64);
    for &member in goal {
        // An occurrence's subtree is the ids that follow it, so the end of
        // the outermost exponential met so far says what lies below one.
        let mut inside = 0;
        for o in forest.subtree(member) {
            if o.index() < inside
                && let Some(sign) = forest.sign(o)
                && counted(o)
            {
                below[sign as usize] = true;
            }
            if matches!(forest.kind(o), Bang | Quest) {
                inside = inside.max(o.index() + forest.size(o) as usize);
            }
        }
        for o in forest.subtree(member).rev() {
            let side = |side: Option<OccId>| side.and_then(|s| span[s.index()]);
            let (l, r) = (side(forest.left(o)), side(forest.right(o)));
            let literal = |e| Some(if counted(o) { (e, e) } else { (0, 0) });
            span[o.index()] = match forest.kind(o) {
                Atom => literal(1),
                DualAtom => literal(-1),
                One | Bot | Zero => Some((0, 0)),
                Top => None,
                Tensor | Par => l.zip(r).map(|((a, b), (c, d))| (a + c, b + d)),
                With | Plus => l.zip(r).map(|((a, b), (c, d))| (a.min(c), b.max(d))),
                Bang | Quest => l.map(|_| (0, 0)),
                Forall | Exists => l,
            };
        }
        let Some((a, b)) = span[member.index()] else {
            return Err(RefutationFault::NotApplicable {
                invariant: INVARIANT,
                why: "a ⊤ absorbs",
            });
        };
        let (Some(a), Some(b)) = (lo.checked_add(a), hi.checked_add(b)) else {
            return Err(RefutationFault::Mismatch);
        };
        (lo, hi) = (a, b);
    }
    match below {
        [true, true] => {
            return Err(RefutationFault::NotApplicable {
                invariant: INVARIANT,
                why: "copies add literals of both signs",
            });
        }
        [positive, negative]
            if positive && copied == Sign::Dual || negative && copied == Sign::Atom =>
        {
            return Err(RefutationFault::Mismatch);
        }
        _ => {}
    }
    if (lo, hi) != (i64::from(least), i64::from(most)) {
        return Err(RefutationFault::Mismatch);
    }
    let excluded = match copied {
        Sign::Atom => lo > 0,
        Sign::Dual => hi < 0,
    };
    if !excluded {
        return Err(RefutationFault::Holds);
    }
    let weight = match copied {
        Sign::Atom => 1,
        Sign::Dual => -1,
    };
    let weights = super::weighted::Weights {
        weight: &|o| if counted(o) { weight } else { 0 },
        dead: &|_| false,
        sign: Sign::Atom,
        weakening: false,
    };
    super::weighted::overweight(forest, goal, &weights)
}

/// Checks that the goal has the formulas and connectives `equation`
/// states, which fail the count equation, where it is an invariant.
fn check_equation(
    forest: &crate::linear::occurrences::Forest,
    goal: &[OccId],
    mode: Mode,
    equation: &Equation,
) -> Result<(), RefutationFault> {
    use crate::linear::sequents::Kind::*;
    const INVARIANT: &str = "count equation";
    if mode.affine {
        return Err(RefutationFault::NotApplicable {
            invariant: INVARIANT,
            why: "weakening adds formulas",
        });
    }
    let (mut tensors, mut pars, mut ones, mut bottoms) = (0u64, 0u64, 0u64, 0u64);
    for &member in goal {
        for o in forest.subtree(member) {
            match forest.kind(o) {
                Tensor => tensors += 1,
                Par => pars += 1,
                One => ones += 1,
                Bot => bottoms += 1,
                // A quantifier's rule changes no count.
                Atom | DualAtom | Forall | Exists => {}
                Top | Zero | With | Plus | Bang | Quest => {
                    return Err(RefutationFault::NotApplicable {
                        invariant: INVARIANT,
                        why: "it holds in the multiplicative fragment with units alone",
                    });
                }
            }
        }
    }
    // Counts over the goal's members, each a subtree of fewer than 2³²
    // occurrences. For any goal a memory holds they are far within an
    // `i64`.
    let needed = tensors as i64 - pars as i64 - ones as i64 + bottoms as i64 + 2;
    let formulas = goal.len() as u64;
    let stated = Equation {
        formulas,
        needed,
        tensors,
        pars,
        ones,
        bottoms,
        mix: mode.mix,
    };
    if *equation != stated {
        return Err(RefutationFault::Mismatch);
    }
    let holds = if mode.mix {
        formulas as i64 >= needed
    } else {
        formulas as i64 == needed
    };
    if holds {
        Err(RefutationFault::Holds)
    } else {
        Ok(())
    }
}

#[cfg(all(test, feature = "parse"))]
mod tests {
    use super::*;
    use crate::linear::search::{Options, Verdict, prove};

    /// Returns the disproof the default search gives `input` in `mode`.
    fn refuted(input: &str, mode: Mode) -> Disproof {
        let sequent: Sequent = input.parse().unwrap();
        match prove(&sequent, mode, &Options::default()).unwrap().verdict {
            Verdict::Unprovable(disproof) => *disproof,
            other => panic!("{input}: {other:?}"),
        }
    }

    /// Returns the disproof of `input` in `mode` for `refutation`.
    fn claimed(input: &str, mode: Mode, refutation: Refutation) -> Disproof {
        Disproof::new(input.parse().unwrap(), mode, refutation)
    }

    /// Returns the fault the check finds, `None` where it accepts.
    fn fault(disproof: &Disproof) -> Option<RefutationFault> {
        match disproof.check() {
            Ok(()) => None,
            Err(crate::Error::Refutation(fault)) => Some(fault),
            Err(other) => panic!("{other:?}"),
        }
    }

    /// The check accepts the search's assignments, balances and equations,
    /// and rejects every one that does not hold where the claim is: a
    /// satisfying or short assignment, numbers not the goal's or that
    /// allow a proof, a mode or connectives that defeat the invariant, and
    /// the refutations that rest on the search.
    #[test]
    fn a_wrong_refutation_is_rejected() {
        let classical = Mode::CLASSICAL;
        let assignment = |values: &[bool]| {
            Refutation::Classical(Classical {
                assignment: values.to_vec(),
            })
        };
        let found = refuted("|- a & b, ~a", classical);
        assert_eq!(fault(&found), None);
        assert_eq!(fault(&refuted("a, a -o b |- b * a", classical)), None);
        let wrong = claimed("|- a & b, ~a", classical, assignment(&[true, true]));
        assert_eq!(fault(&wrong), Some(RefutationFault::Satisfied));
        let short = claimed("|- a & b, ~a", classical, assignment(&[true]));
        assert_eq!(
            fault(&short),
            Some(RefutationFault::Assignment {
                atoms: 2,
                values: 1
            })
        );
        // In every mode the reading holds, intuitionistic too.
        let ill = refuted("a + b |- a", Mode::INTUITIONISTIC);
        assert!(matches!(ill.refutation(), Refutation::Classical(_)));
        assert_eq!(fault(&ill), None);

        let unbalanced = |atom: u32, least: i32, most: i32| {
            Refutation::Unbalanced(Unbalanced {
                atom: Atom::new(atom),
                least,
                most,
            })
        };
        let found = refuted("|- (a * a) + (a * a * a), ~a", classical);
        assert_eq!(found.refutation(), &unbalanced(0, 1, 2));
        assert_eq!(fault(&found), None);
        let mix = refuted("a |- b", classical.with_mix());
        assert!(matches!(mix.refutation(), Refutation::Unbalanced(_)));
        assert_eq!(fault(&mix), None);
        for (input, mode, refutation, expected) in [
            (
                "|- a, ~a",
                classical,
                unbalanced(0, 0, 0),
                RefutationFault::Holds,
            ),
            (
                "|- a, ~a",
                classical,
                unbalanced(0, 1, 1),
                RefutationFault::Mismatch,
            ),
            (
                "|- a, a",
                classical,
                unbalanced(1, 2, 2),
                RefutationFault::Mismatch,
            ),
            (
                "|- a + ~a, a",
                classical,
                unbalanced(0, 1, 1),
                RefutationFault::Mismatch,
            ),
            (
                "|- a + ~a, a",
                classical,
                unbalanced(0, 0, 2),
                RefutationFault::Holds,
            ),
        ] {
            assert_eq!(
                fault(&claimed(input, mode, refutation)),
                Some(expected),
                "{input}"
            );
        }
        for (input, mode) in [
            ("|- a, a", classical.with_affine()),
            ("|- a, a, top", classical),
            ("|- ?a, a", classical),
        ] {
            assert!(
                matches!(
                    fault(&claimed(input, mode, unbalanced(0, 2, 2))),
                    Some(RefutationFault::NotApplicable { .. })
                ),
                "{input}"
            );
        }

        let found = refuted("|- a par b, ~a, ~b", classical);
        let Refutation::Equation(equation) = *found.refutation() else {
            panic!("{found:?}");
        };
        assert_eq!(fault(&found), None);
        let stated = |input: &str, mode: Mode, equation: Equation| {
            fault(&claimed(input, mode, Refutation::Equation(equation)))
        };
        let with_mix = Equation {
            mix: true,
            ..equation
        };
        assert_eq!(
            stated("|- a par b, ~a, ~b", classical, with_mix),
            Some(RefutationFault::Mismatch)
        );
        assert_eq!(
            stated("|- a par b, ~a, ~b", classical.with_mix(), with_mix),
            Some(RefutationFault::Holds)
        );
        let holds = Equation {
            formulas: 2,
            needed: 2,
            tensors: 0,
            pars: 0,
            ones: 0,
            bottoms: 0,
            mix: false,
        };
        assert_eq!(
            stated("|- a, ~a", classical, holds),
            Some(RefutationFault::Holds)
        );
        for (input, mode) in [
            ("|- a par b, ~a, ~b", classical.with_affine()),
            ("|- a & b, ~a, ~b", classical),
            ("|- ?(a par b), ~a, ~b", classical),
        ] {
            assert!(
                matches!(
                    stated(input, mode, equation),
                    Some(RefutationFault::NotApplicable { .. })
                ),
                "{input}"
            );
        }

        let exhausted = claimed("|- a & b, ~a", classical, Refutation::Exhausted);
        let error = exhausted.check().unwrap_err();
        assert_eq!(
            (error.code(), error.kind()),
            ("uncertified", crate::ErrorKind::Unsupported)
        );
        let error = wrong.check().unwrap_err();
        assert_eq!(
            (error.code(), error.kind()),
            ("invalid_refutation", crate::ErrorKind::Invalid)
        );
    }

    /// The check accepts the search's one-sided counts and state
    /// equations, in each mode they are given in, and refuses a planted
    /// wrong one of each kind: numbers not the goal's, copies of the other
    /// sign, an interval that copies can bring to zero, weights that leave
    /// a contribution at zero, a goal that is no Horn program, and the
    /// modes where the invariant does not hold.
    #[test]
    fn a_wrong_count_with_copies_or_state_equation_is_refused() {
        let (classical, ill) = (Mode::CLASSICAL, Mode::INTUITIONISTIC);
        for (input, mode, kind) in [
            ("!d, !((c * d * d) -o 1) |- c * c", classical, "one_sided"),
            ("!d, !((c * d * d) -o 1) |- c * c", ill, "one_sided"),
            ("|- ~a * ~a, ?(~a * b), ~b", classical, "one_sided"),
            (
                "!(a -o a * a), !(a * c -o b * c), a |- b",
                ill,
                "state_equation",
            ),
            (
                "!(a -o a * a), !(a * c -o b * c), a |- b",
                classical.with_mix(),
                "state_equation",
            ),
            // Bodies written negative, with a clause that never fires.
            (
                "|- a, ?(~a * (a par a)), ?((~a * ~c) * b), ~b",
                classical,
                "state_equation",
            ),
            // A clause used once, whose ticket weighs.
            (
                "a, a -o b, !(b -o b * c), !(b -o d) |- b * d",
                ill,
                "state_equation",
            ),
            (
                "!(a -o a * c), !(a -o b), a |- a * b",
                ill.with_affine(),
                "state_equation",
            ),
            (
                "|- a, ?(~a * (a par c)), ?(~a * b), ~a * ~b",
                classical.with_affine(),
                "state_equation",
            ),
        ] {
            let found = refuted(input, mode);
            assert_eq!(found.refutation().name(), kind, "{input}");
            assert_eq!(fault(&found), None, "{input}");
        }

        let one_sided = |atom: u32, least: i32, most: i32, copied: Sign| {
            Refutation::OneSided(OneSided {
                atom: Atom::new(atom),
                least,
                most,
                copied,
            })
        };
        // The atom `c` is the second: its copies add `c`.
        let counted = "!d, !((c * d * d) -o 1) |- c * c";
        for (input, mode, refutation, expected) in [
            (
                counted,
                classical,
                one_sided(1, 1, 1, Sign::Atom),
                RefutationFault::Mismatch,
            ),
            (
                counted,
                classical,
                one_sided(1, 2, 2, Sign::Dual),
                RefutationFault::Mismatch,
            ),
            // The copies add `~c`: only a deficit of `~c` would refute.
            (
                "|- c * c, ?~c",
                classical,
                one_sided(0, 2, 2, Sign::Dual),
                RefutationFault::Holds,
            ),
            (
                "|- ~c, ~c, ?c",
                classical,
                one_sided(0, -2, -2, Sign::Dual),
                RefutationFault::Mismatch,
            ),
            (
                "|- ~c, ~c, ?c",
                classical,
                one_sided(0, -2, -2, Sign::Atom),
                RefutationFault::Holds,
            ),
        ] {
            let planted = claimed(input, mode, refutation);
            assert_eq!(fault(&planted), Some(expected), "{input}");
        }
        for (input, mode, atom) in [
            (counted, classical.with_affine(), 1),
            ("|- c * c, ?~c, ?c", classical, 0),
            ("|- c * c, ?~c, top", classical, 0),
        ] {
            assert!(
                matches!(
                    fault(&claimed(input, mode, one_sided(atom, 2, 2, Sign::Atom))),
                    Some(RefutationFault::NotApplicable { .. })
                ),
                "{input}"
            );
        }

        let weighed = |atoms: &[(u32, i64)]| {
            Refutation::StateEquation(StateEquation {
                atoms: atoms.iter().map(|&(a, w)| (Atom::new(a), w)).collect(),
                clauses: Vec::new(),
                dropped: Vec::new(),
            })
        };
        let net = "!(a -o a * a), !(a * c -o b * c), a |- b";
        let found = refuted(net, ill);
        let Refutation::StateEquation(equation) = found.refutation() else {
            panic!("{found:?}");
        };
        assert_eq!(equation.atoms, [(Atom::new(0), -1), (Atom::new(2), 1)]);
        for (input, mode, atoms, expected) in [
            // Found with `a ↦ 2, b ↦ 1, d ↦ 1`; the copies of `b ⊸ d` raise
            // the count with `d ↦ 2`, those of `b ⊸ b ⊗ c` with `c ↦ 1`.
            (
                "a, a -o b, !(b -o b * c), !(b -o d) |- b * d",
                ill,
                &[(0, 1), (1, 1), (3, 2)][..],
                RefutationFault::Holds,
            ),
            (
                "a, a -o b, !(b -o b * c), !(b -o d) |- b * d",
                ill,
                &[(0, 2), (1, 1), (2, 1), (3, 1)][..],
                RefutationFault::Holds,
            ),
            (net, ill, &[(2, 1), (0, -1)][..], RefutationFault::Mismatch),
            (net, ill, &[(0, -1), (7, 1)][..], RefutationFault::Mismatch),
            // The bodies negative, found with `a ↦ 1, b ↦ 1`: with `c ↦ 1`
            // the copies of the first clause raise the count.
            (
                "|- a, ?(~a * (a par c)), ?(~a * b), ~a * ~b",
                classical.with_affine(),
                &[(0, 1), (1, 1), (2, 1)][..],
                RefutationFault::Holds,
            ),
        ] {
            let planted = claimed(input, mode, weighed(atoms));
            assert_eq!(fault(&planted), Some(expected), "{input}");
        }
        for (input, mode) in [
            ("|- a & b, ~a", classical),
            // With weakening the goal has no output, or two.
            ("|- ~a, ~a", classical.with_affine()),
            ("forall x. p(x) |- p(c)", classical),
        ] {
            assert!(
                matches!(
                    fault(&claimed(input, mode, weighed(&[(0, 1)]))),
                    Some(RefutationFault::NotApplicable { .. })
                ),
                "{input}"
            );
        }
    }

    /// A classical disproof of a sequent written with formulas left of
    /// `⊢` speaks of its sides: an atom with no partner on the other side,
    /// an excess counted left and right, and a note where a literal counts
    /// on the other side than its formula. One written `⊢ Γ`, a goal
    /// within the sequent and the JSON form keep the one-sided wording.
    #[test]
    fn a_two_sided_sequent_is_refuted_by_its_sides() {
        let classical = Mode::CLASSICAL;
        for (input, phrase) in [
            (
                "a |- b",
                "a on the left has no a on the right to meet in an axiom",
            ),
            (
                "a, a, a |- a",
                "a occurs 2 more times on the left than on the right, so they cannot all meet in \
                 axioms",
            ),
            (
                "b, ~a |- b",
                "a on the right has no a on the left to meet in an axiom (a negated formula or an \
                 implication's antecedent counts on the other side)",
            ),
            (
                "|- ~a, b",
                "~a occurs 1 more time than a in the one-sided sequent, so they cannot all meet in \
                 axioms",
            ),
        ] {
            assert_eq!(refuted(input, classical).to_string(), phrase, "{input}");
        }
        let goal = refuted("a |- b", classical).of_goal(Box::new([]));
        assert!(goal.to_string().contains("one-sided"), "{goal}");
    }

    /// An intuitionistic count names the side that has none of the atom,
    /// as the two-sided classical one does, and counts the excess where
    /// both sides have some.
    #[test]
    fn an_intuitionistic_count_names_the_empty_side() {
        let intuitionistic = Mode::INTUITIONISTIC;
        for (input, phrase) in [
            (
                "a -o b |- b -o a",
                "a on the right has no a on the left to meet in an axiom (an implication's \
                 antecedent counts on the other side)",
            ),
            (
                "a |- a * a",
                "a occurs 1 more time on the right than on the left, so they cannot all meet in \
                 axioms (an implication's antecedent counts on the other side)",
            ),
        ] {
            assert_eq!(
                refuted(input, intuitionistic).to_string(),
                phrase,
                "{input}"
            );
        }
    }
}