deduxit 0.3.0

Logic suites, linear logic first: decides, checks, proves step by step and exports sequents of classical and intuitionistic linear logic, and ordinary propositional logic through its embeddings.
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
// deduxit © Fabian Lukas Grubmüller 2026
// Licensed under the EUPL

//! Why a sequent is unprovable: the refutation an exhaustive search or a
//! test of the sequent gives, and the disproof that holds it with the
//! sequent, the goal and the mode it means something in.

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

/// Why a sequent is unprovable. Most refutations rest on the search that
/// was exhaustive; where the literals or the connectives of the sequent
/// alone rule out a proof, the refutation says which, as the focused
/// engine checks them on every sequent it searches; and 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`), the disproof's by name. Of a goal off the roots, the
/// "one-sided sequent" a count speaks of is the goal: its members, which
/// may lie below an exponential of the sequent, are counted as written.
///
/// In JSON (feature `serialize`) a refutation, as an unprovable outcome's
/// and a disproof's `refutation`, is tagged by `kind`: `{"kind": "exhausted"}`, `{"kind": "unbalanced",
/// "atom", "least", "most"}` (the atom by its index into the sequent
/// beside it), `{"kind": "one_sided", "atom", "least", "most", "copied"}`
/// (`copied` the literal `"atom"` or `"dual"`), `{"kind": "equation", "formulas", "needed", "tensors",
/// "pars", "ones", "bottoms", "mix"}` or `{"kind": "state_equation",
/// "atoms": [[atom, weight], …], "clauses": [[occurrence, weight], …],
/// "dropped": [occurrence, …]}` or `{"kind": "classical", "assignment":
/// [true, false, …]}` (a value per atom of the sequent, by index) or
/// `{"kind": "saturated", "positive": ["atom", "dual", …], "sequents":
/// [{"linear": [occurrence, …], "unrestricted": [occurrence, …],
/// "absorbs": false}, …]}` or `{"kind": "unbalanced_symbol", "symbol",
/// "least", "most"}` or `{"kind": "one_sided_symbol", "symbol", "least",
/// "most", "copied"}` (the predicate by its index into the sequent's
/// symbols), and reads back so. A reader refuses a
/// kind it does not know: read as another, 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),
    /// Nor can they where copies may add literals of the atom, but only
    /// 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, one of whose atoms has a bound
    /// variable, cannot all meet their duals in axioms, counted over all
    /// its atoms.
    UnbalancedSymbol(UnbalancedSymbol),
    /// Nor can they where copies may add literals of the predicate, but
    /// only of one sign.
    OneSidedSymbol(OneSidedSymbol),
}

/// The literals of a predicate that cannot all meet their duals in axioms,
/// counted as [`Unbalanced`] counts an atom's but over every atom of the
/// predicate together: one of them has a bound variable, so an instance
/// of it may be any atom of the predicate, and only literals of one
/// predicate meet.
#[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 below a `?` or `!`, where copies may add any number of
/// them, 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 literal of the predicate that every one below an exponential
    /// is.
    pub copied: Sign,
}

/// The literals of an atom that cannot all meet their duals in axioms:
/// whichever additive alternatives a proof takes, the positive literal
/// occurs between `least` and `most` times more than the negative one
/// (fewer, where these are negative), and never as 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 that 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 one is `copied`, so copies can
/// only add that literal, and outside the exponentials it already occurs
/// between `least` and `most` times more than the other one (counted as
/// [`Unbalanced`] counts, so `least` above zero for the atom and `most`
/// below zero for its 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 literal of the atom that every one below an exponential is.
    pub copied: Sign,
}

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

/// Weights for a Horn program's Petri net, every place included, that
/// its state equation fails under (Farkas' lemma): weighting the tokens
/// of each atom and the ticket of each clause used once as given (zero
/// where not listed), no clause that can fire raises the weighted count,
/// while the goal asks it raised, so no sequence of firings reaches the
/// goal; in affine mode, where the weights are not negative, none covers
/// it either. A clause with an input that no marking reached 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: `!` and `?` erased, `⊗` and `&` read 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 are closed
/// under the rules of the focused calculus and leave a stable sequent of
/// the goal's asynchronous phase underived: no proof of the goal exists.
/// Every rule (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 derives one that a sequent of the set stands for, or
/// one no proof holds (a member twice, or two a `⊗`, `&` or `⊕`
/// separates, outside the subformulas of `?`), and so does Mix where the
/// mode has it; so by induction every stable sequent of a focused proof
/// is one a sequent of the set stands for, and some stable sequent of the
/// goal's phase is one none of them does. The rules are those of the
/// polarity `positive` names, under which focusing is as complete 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: positive formulas and negative
/// literals in its linear zone, subformulas of `?` in its unrestricted
/// one, as occurrences of the sequent's forest. It stands for the
/// sequents with exactly this linear zone and an unrestricted zone that
/// holds this one, and where it `absorbs` (a `⊤` above) or in affine mode
/// for those with a larger linear zone too, a member in output position
/// absorbed only where the zone has none and weakened in never. The
/// subformulas of the `?` the goal reaches through `⅋` alone, which every
/// sequent of a proof holds, may be left out of its 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 }
    }
}

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",
    ];

    /// 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",
        }
    }

    /// Writes the refutation as a phrase, each atom and predicate by its
    /// name in `sequent`, or by its number without one; of an
    /// `intuitionistic` sequent in its two-sided terms, an atom's
    /// occurrences positive right of `⊢` and negative left of it.
    fn write(
        &self,
        f: &mut Formatter<'_>,
        sequent: Option<&Sequent>,
        intuitionistic: bool,
    ) -> FmtResult {
        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 and 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),
                intuitionistic,
            ),
            Refutation::OneSidedSymbol(OneSidedSymbol {
                symbol,
                least,
                most,
                copied,
            }) => write_unbalanced(
                f,
                &predicate(*symbol),
                *least,
                *most,
                (Some(*copied), true),
                intuitionistic,
            ),
            Refutation::Exhausted => f.write_str("the search was exhaustive"),
            Refutation::Unbalanced(Unbalanced { atom, least, most }) => write_unbalanced(
                f,
                &name(*atom),
                *least,
                *most,
                (None, false),
                intuitionistic,
            ),
            Refutation::OneSided(OneSided {
                atom,
                least,
                most,
                copied,
            }) => write_unbalanced(
                f,
                &name(*atom),
                *least,
                *most,
                (Some(*copied), false),
                intuitionistic,
            ),
            Refutation::Equation(Equation {
                formulas,
                needed,
                tensors,
                pars,
                ones,
                bottoms,
                mix,
            }) => {
                // A count below zero with the minus sign of the expression.
                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: {} {} closed under the rules, and the goal needs one {}",
                sequents.len(),
                if sequents.len() == 1 {
                    "sequent"
                } else {
                    "sequents"
                },
                if sequents.len() == 1 {
                    "it does not give"
                } else {
                    "they do not give"
                },
            ),
        }
    }
}

/// 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(", ")),
    }
}

/// Writes why the literals of an atom named `name` cannot all meet in
/// axioms: the one outnumbers the other between `least` and `most` times,
/// outside the exponentials where some literals, all `copied`, lie below
/// one, counted over every atom of a predicate where `predicate` says so;
/// of an `intuitionistic` sequent as its positive and negative
/// occurrences, which are its atoms right and left of `⊢`.
fn write_unbalanced(
    f: &mut Formatter<'_>,
    name: &str,
    least: i32,
    most: i32,
    (copied, predicate): (Option<Sign>, bool),
    intuitionistic: bool,
) -> 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 intuitionistic {
        // An atom in output position is the positive literal.
        let (more, fewer) = if least > 0 {
            ("positively", "negatively")
        } else {
            ("negatively", "positively")
        };
        let (place, copies) = match copied {
            Some(sign) => (
                " outside the exponentials",
                format!(
                    ", and every occurrence of it below one, which copies add, is {}",
                    match sign {
                        Sign::Atom => "positive",
                        Sign::Dual => "negative",
                    }
                ),
            ),
            None => ("", String::new()),
        };
        return write!(
            f,
            "{name} occurs {more} {excess} more {times} than {fewer}{place}{alternatives}{copies} \
             (positive right of ⊢, negative left of it, an implication's antecedent on the other \
             side){over}, so they cannot all meet in axioms",
        );
    }
    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, which the engine's
    // refutations make the one that outnumbers; a form 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, false)
    }
}

/// A refutation with what it refutes: the sequent, the goal within it if
/// another than 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.
///
/// # JSON
///
/// With the feature `serialize` a disproof is the document `{"version":
/// 1, "sequent": …, "mode": …, "refutation": …, "goal": […]}`: the
/// refutation in the form [`Refutation`] gives, the goal's members only
/// for a goal off the roots. An unprovable outcome writes the same keys,
/// so it reads back as its disproof. Reading counts the sequent against
/// the limits and checks every atom and occurrence the refutation and the
/// goal name against it (`Error::IndexOutOfBounds` otherwise), not the
/// refutation itself; 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 in the sequent's two-sided terms.
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        self.refutation
            .write(f, Some(&self.sequent), self.mode.is_intuitionistic())
    }
}

/// 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, and the check cannot verify it.
    #[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 invariant does not hold where the goal is: weakening discards
    /// literals and connectives, a `⊤` absorbs them, copies add them, and
    /// the count equation holds in the multiplicative fragment alone.
    #[error("the {invariant} is no invariant here: {why}")]
    NotApplicable {
        /// The invariant: `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: an atom or an
    /// occurrence the sequent lacks, or counts that differ from its own.
    #[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 none 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,
}

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",
        }
    }
}

impl Disproof {
    /// Checks the refutation against the sequent, the goal and the mode,
    /// sharing no code with the search: an assignment by evaluating the
    /// goal's classical reading, which must be false; an unbalanced atom
    /// by its excess of positive over negative literals, whose interval
    /// over the additive alternatives (`⊗` and `⅋` adding their sides, `&`
    /// and `⊕` taking the hull of theirs) must be the one stated and
    /// exclude zero, outside affine mode and with no `⊤` and no `!` or `?`
    /// over the atom in the goal; the count equation by the goal's
    /// formulas, `⊗`, `⅋`, `1` and `⊥`, which must be the ones stated and
    /// fail it, outside affine mode and in the multiplicative fragment
    /// with units, with Mix exactly where the mode allows it; a saturated
    /// set by reading the rules of the focused calculus off the forest
    /// again, under the set's polarity, and applying each to every choice
    /// of premises among its sequents, each sequent derived one that a
    /// sequent of the set stands for or that no proof holds, Mix too
    /// where the mode has it, its sequents shaped as the mode's are, and
    /// a stable sequent of the goal's phase that none stands for; a
    /// one-sided count by its interval outside the goal's exponentials and
    /// the sign of the atom's literals below them, as an unbalanced atom's
    /// with the copies' side open, and then by the weighted test with the
    /// weight `±1` on the atom; a state equation by the weighted test with
    /// the stated weights, the goal read as a Horn program with positive
    /// bodies, else negative ones (the weights negated), the atoms no
    /// firing reaches declared dead, and in affine mode its count of the
    /// formulas with one output. 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 is above zero, a `?` formula's copies included. Those six
    /// are certificates: every derivation keeps the invariant the others
    /// break, and the set holds a sequent for every stable one of every
    /// proof. Time and memory are linear in the goal's occurrences (a
    /// member repeated counted each time) for all but the set, whose
    /// check applies every
    /// rule to every choice of its sequents, which takes as long as the
    /// search that saturated did at most for a set a search gave, but has
    /// no stop here: for a set read from elsewhere it can take time
    /// exponential in the `&` under `⅋` of the goal and of the degree of
    /// a rule's holes in the set's size (a set repeating 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`]: of kind invalid for a refutation that does not
    /// hold, unsupported for an exhausted search
    /// ([`RefutationFault::Uncertified`]); and the errors of
    /// [`Forest::new`](crate::linear::Forest::new) for a sequent past the default
    /// limits.
    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, a quantifier's rule
        // changing none of them; an assignment and a saturated set are
        // propositional.
        if forest.has_binders()
            && matches!(
                self.refutation,
                Refutation::Classical(_) | Refutation::Saturated(_)
            )
        {
            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 @ 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 {
        // Preorder: a subtree's occurrences follow its root, so its
        // operands are evaluated first from the end.
        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, which excludes zero, where the balance is an invariant: of a
/// sequent with a quantifier, only where no atom of its predicate has a
/// bound variable, which an instance could make the atom.
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, which
/// 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, which excludes zero, where the balance is an
/// invariant: 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, `None` where a
    // `⊤` absorbs or copies of the counted literals can add any number:
    // sums of ±1 over the goal's members, each a subtree of the forest's
    // fewer than 2³² occurrences: a goal past 2³¹ members would be needed
    // to leave an `i64`, 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, below which its literals all have the
/// stated sign, and that copies of that sign cannot bring the excess to
/// zero, where the count is an invariant: of a sequent with a quantifier,
/// only where no atom of its 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, which copies cannot bring
/// 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 (`⊗` and `⅋` adding their
/// sides, `&` and `⊕` taking the hull, a `!` or `?` counting nothing) is
/// the stated one, every one of them below an exponential of the goal has
/// the stated sign `copied`, and the interval excludes zero on the side
/// 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, which
/// is the theorem the count rests on. Outside affine mode and with no `⊤`
/// in the goal.
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, `None` where a `⊤` absorbs: sums of ±1 over a member's
    // subtree, fewer than 2³² occurrences, so within an `i64` per member;
    // the members' sum 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 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: far within an `i64` for any goal a memory holds.
    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}"
            );
        }
    }
}