deduxit 0.4.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
// deduxit © Fabian Lukas Grubmüller 2026
// Licensed under the EUPL

//! A second, independent implementation of the proof checker, which the
//! tests compare the checker with. The checker must accept and reject
//! exactly what this one does, with the same error, so a change to a rule
//! is made in both.
//!
//! It is written to be easy to read, not to be fast or small. It derives
//! a sequent for every node and keeps them all, as ordered sets and
//! sorted lists, so its memory is the sum of every node's sequent. It
//! reads first-order instances by writing their terms out as text, and
//! uses only the instance table's data, not the checker's lookups. It is
//! compiled for tests only.

use super::check::{CheckError, Dyadic, Fault};
use super::multiset::Multiset;
use super::{Branch, Node, NodeId, Proof};
#[cfg(feature = "parse")]
use crate::linear::Sequent;
use crate::linear::fragment::Mode;
use crate::linear::occurrences::{Forest, Member, OccId, Reading, Side};
use crate::linear::sequents::{Eigen, FoTerm, FoTermId, Kind};
use std::collections::{BTreeMap, BTreeSet};

/// What a subproof proves, as the oracle derives it from the node's
/// premises: the dyadic sequent `⊢ Θ ; Γ`. With `any` set it proves
/// `⊢ Θ ; Γ, Δ` for every `Δ`, because a `⊤` leaf above absorbs whatever
/// context it is given.
///
/// `Θ` is the least unrestricted zone the subproof needs: the occurrences
/// it copies and does not itself move into `Θ` with a `?` step. Weakening
/// and contraction on `Θ` are implicit, so any larger zone does as well.
/// So a proof is correct when its root needs an empty `Θ` and derives the
/// sequent's formulas, and the oracle never has to know the actual zone.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Derived {
    /// The unrestricted zone the subproof needs.
    pub(crate) theta: BTreeSet<Member>,
    /// The linear zone.
    pub(crate) gamma: Multiset<Member>,
    /// Whether a `⊤` above absorbs any further linear context.
    pub(crate) any: bool,
    /// The eigenvariables the witnesses and cut frames above name, less
    /// those the `∀` steps above introduce.
    pub(crate) eigens: BTreeSet<Eigen>,
}

impl Derived {
    /// Returns the sequent as ids for an error report.
    fn to_dyadic(&self) -> Dyadic {
        Dyadic {
            theta: self.theta.iter().copied().collect(),
            gamma: self.gamma.as_slice().to_vec(),
            any: self.any,
        }
    }
}

/// Checks that a proof proves its sequent, as [`Proof::check`] does but
/// with no bound on memory or work.
///
/// The proof passes when every node applies its rule to what its premises
/// derive, the mode allows every rule, and the root derives exactly the
/// sequent's formulas with nothing left in the unrestricted zone. In
/// intuitionistic mode the sequent must also have an intuitionistic
/// reading, and every sequent of the proof one formula right of `⊢`.
/// Otherwise the error names the first node that fails, in arena order,
/// with what it needed.
pub(crate) fn check(proof: &Proof, mode: Mode) -> Result<(), CheckError> {
    let reading = if mode.intuitionistic {
        match Reading::of_mode(proof.forest(), mode) {
            Ok(reading) => Some(reading),
            Err(e) => {
                return Err(CheckError::invalid(
                    proof.root(),
                    proof.node(proof.root()),
                    vec![],
                    Fault::Shape(e),
                ));
            }
        }
    } else {
        None
    };
    let derived = derive(proof, mode, reading.as_ref())?;
    conclude(proof, mode, &derived)
}

/// Derives what every node proves, in arena order.
///
/// # Errors
///
/// The first node that misapplies its rule, uses one the mode forbids or
/// derives a zone the rest of the proof cannot consume; with a reading,
/// also the first sequent that breaks the one-succedent condition.
pub(crate) fn derive(
    proof: &Proof,
    mode: Mode,
    reading: Option<&Reading>,
) -> Result<Vec<Derived>, CheckError> {
    let mut derived = Vec::with_capacity(proof.nodes().len());
    let (nodes, roots) = (proof.nodes().len(), proof.conclusion().len());
    let frames = Frames::of(proof);
    for id in proof.ids() {
        // The most members a zone may have that the later nodes can still
        // bring down to the roots: each consumes two at most.
        let room = roots + 2 * (nodes - 1 - id.index());
        let d = Step::new(proof, mode, id, &derived, reading, &frames).derive(room)?;
        derived.push(d);
    }
    Ok(derived)
}

/// The proof's instances as the oracle reads them, and the faults that
/// rest on the proof as a whole. Every member is known by its occurrence
/// and its frame's terms written out, the innermost binder's first, so
/// two instances are equal when their texts are.
struct Frames {
    /// The forest's length.
    first: usize,
    /// Per member past the forest, its occurrence, its frame's terms and
    /// their texts.
    members: Vec<(OccId, Vec<FoTermId>, Vec<String>)>,
    /// Every member past the forest by its occurrence and frame's texts.
    by: BTreeMap<(OccId, Vec<String>), Member>,
    /// The terms of the sequent and the proof.
    terms: Vec<FoTerm>,
    /// Their argument lists, by list id.
    lists: BTreeMap<u32, Vec<FoTermId>>,
    /// The symbols' names.
    symbols: Vec<String>,
    /// The fault of a node, where the proof gives it one: a member under
    /// binders named bare, or a second introduction of an eigenvariable.
    faults: BTreeMap<NodeId, Fault>,
    /// The eigenvariables a `∀` step introduces; any other is a constant.
    introduced: BTreeSet<Eigen>,
}

impl Frames {
    /// Reads the instances of `proof`.
    fn of(proof: &Proof) -> Self {
        let forest = proof.forest();
        let fo = &forest.sequent().fo;
        let mut this = Self {
            first: forest.len(),
            members: vec![],
            by: BTreeMap::new(),
            terms: fo.terms.clone(),
            lists: BTreeMap::new(),
            symbols: fo.symbols.iter().map(|(n, _)| n.clone()).collect(),
            faults: BTreeMap::new(),
            introduced: BTreeSet::new(),
        };
        for (k, &(start, len)) in fo.lists.iter().enumerate() {
            let items = fo.args[start as usize..(start + len) as usize].to_vec();
            this.lists.insert(k as u32, items);
        }
        if let Some(table) = proof.instances() {
            let base = fo.lists.len() as u32;
            this.terms.extend_from_slice(table.terms());
            for (k, &(start, len)) in table.lists.iter().enumerate() {
                let items = table.args[start as usize..(start + len) as usize].to_vec();
                this.lists.insert(base + k as u32, items);
            }
            for (n, &(o, frame)) in table.members.iter().enumerate() {
                let terms = table.frame_terms(frame);
                let texts: Vec<String> = terms.iter().map(|&t| this.text(t, &[])).collect();
                this.by
                    .insert((o, texts.clone()), Member::new((this.first + n) as u32));
                this.members.push((o, terms, texts));
            }
        }
        // The number of binders above an occurrence, found by walking its
        // parents.
        let binders = |o: OccId| {
            let mut n = 0;
            let mut at = o;
            while let Some(p) = forest.parent(at) {
                if matches!(forest.kind(p), Kind::Forall | Kind::Exists) {
                    n += 1;
                }
                at = p;
            }
            n
        };
        let mut first: BTreeMap<Eigen, NodeId> = BTreeMap::new();
        for id in proof.ids() {
            let node = proof.node(id);
            if let Some(member) = node
                .members()
                .find(|m| m.index() < this.first && binders(m.occ()) > 0)
            {
                this.faults.insert(id, Fault::Instance { member });
            } else if let Node::Forall(_, a, _) = node {
                match first.get(&a) {
                    Some(_) => {
                        this.faults.insert(id, Fault::Reintroduced { eigen: a });
                    }
                    None => {
                        first.insert(a, id);
                    }
                }
            }
        }
        this.introduced = first.keys().copied().collect();
        this
    }

    /// Writes the term `t` out, every bound variable `i` as `frame[i]`.
    fn text(&self, t: FoTermId, frame: &[String]) -> String {
        match self.terms[t.index()] {
            FoTerm::Bound(i) => frame
                .get(i as usize)
                .cloned()
                .unwrap_or_else(|| format!("#{i}")),
            FoTerm::Eigen(a) => format!("#e{}", a.get()),
            FoTerm::Meta(k) => format!("#m{k}"),
            FoTerm::App(f, args) => {
                let items: Vec<String> = self.lists[&args.get()]
                    .iter()
                    .map(|&u| self.text(u, frame))
                    .collect();
                format!("{}({})", self.symbols[f.index()], items.join(","))
            }
        }
    }

    /// Adds the eigenvariables of the term `t` to `eigens`.
    fn eigens(&self, t: FoTermId, eigens: &mut BTreeSet<Eigen>) {
        match self.terms[t.index()] {
            FoTerm::Eigen(a) => {
                eigens.insert(a);
            }
            FoTerm::App(_, args) => {
                for &u in &self.lists[&args.get()] {
                    self.eigens(u, eigens);
                }
            }
            FoTerm::Bound(_) | FoTerm::Meta(_) => {}
        }
    }

    /// Returns the occurrence `m` stands for.
    fn occ(&self, m: Member) -> OccId {
        match m.index().checked_sub(self.first) {
            None => m.occ(),
            Some(at) => self.members[at].0,
        }
    }

    /// Returns the frame of `m` written out, empty for an occurrence.
    fn frame(&self, m: Member) -> Vec<String> {
        match m.index().checked_sub(self.first) {
            None => vec![],
            Some(at) => self.members[at].2.clone(),
        }
    }

    /// Returns the member that is `o` under the frame `frame` written out.
    fn member(&self, o: OccId, frame: Vec<String>) -> Member {
        if frame.is_empty() {
            return Member::from(o);
        }
        self.by[&(o, frame)]
    }
}

/// Checks that the root derives the proof's sequent, needs no unrestricted
/// occurrence and carries no eigenvariable a `∀` step introduces.
pub(crate) fn conclude(proof: &Proof, mode: Mode, derived: &[Derived]) -> Result<(), CheckError> {
    let root = proof.root();
    let d = &derived[root.index()];
    let frames = Frames::of(proof);
    if let Some(&eigen) = d.eigens.iter().find(|a| frames.introduced.contains(a)) {
        return Err(Step::new(proof, mode, root, derived, None, &frames)
            .fail(Fault::Eigenvariable { eigen }));
    }
    let roots = Multiset::of(proof.conclusion().into_iter().map(Member::from));
    let concludes = if d.any {
        d.gamma.is_subset(&roots)
    } else {
        d.gamma == roots
    };
    if d.theta.is_empty() && concludes {
        Ok(())
    } else {
        let frames = Frames::of(proof);
        Err(
            Step::new(proof, mode, root, derived, None, &frames).fail(Fault::Conclusion {
                derived: d.to_dyadic(),
            }),
        )
    }
}

/// One node being checked, with what its premises derived.
struct Step<'a> {
    /// The forest the occurrences index.
    forest: &'a Forest,
    /// The rules in force.
    mode: Mode,
    /// The node.
    id: NodeId,
    /// Its rule instance.
    node: Node,
    /// What the nodes before it derived.
    derived: &'a [Derived],
    /// The intuitionistic reading, in intuitionistic mode.
    reading: Option<&'a Reading<'a>>,
    /// The proof's instances.
    frames: &'a Frames,
}

impl<'a> Step<'a> {
    /// Returns the step for node `id`, whose premises have their entries
    /// in `derived`.
    fn new(
        proof: &'a Proof,
        mode: Mode,
        id: NodeId,
        derived: &'a [Derived],
        reading: Option<&'a Reading<'a>>,
        frames: &'a Frames,
    ) -> Self {
        Self {
            forest: proof.forest(),
            mode,
            id,
            node: proof.node(id),
            derived,
            reading,
            frames,
        }
    }

    /// Returns the occurrence a member stands for.
    fn occ(&self, m: Member) -> OccId {
        self.frames.occ(m)
    }

    /// Returns the member of the subformula `c` of `m`'s occurrence, under
    /// `m`'s frame.
    fn sub(&self, m: Member, c: OccId) -> Member {
        self.frames.member(c, self.frames.frame(m))
    }

    /// Returns the member of the body of `m`'s binder, under `m`'s frame
    /// with the term `t` pushed.
    fn body(&self, m: Member, t: FoTermId) -> Member {
        let mut frame = vec![self.frames.text(t, &[])];
        frame.extend(self.frames.frame(m));
        self.frames.member(OccId::new(self.occ(m).get() + 1), frame)
    }

    /// Returns the atom of the literal `m` written out, its frame's terms
    /// in place of its bound variables, and whether it is negated.
    fn atom(&self, m: Member) -> (String, bool) {
        let o = self.occ(m);
        let f = self.forest;
        let atom = f.atom(o).unwrap();
        let negated = f.kind(o) == Kind::DualAtom;
        let Some((p, args)) = f.sequent().atom_predicate(atom) else {
            return (f.sequent().atom_name(atom).to_owned(), negated);
        };
        let frame = self.frames.frame(m);
        let items: Vec<String> = f
            .sequent()
            .arguments(args)
            .iter()
            .map(|&t| self.frames.text(t, &frame))
            .collect();
        let name = &f.sequent().symbols()[p.index()].0;
        (format!("{name}({})", items.join(",")), negated)
    }

    /// Returns the error of `fault` at this node.
    fn fail(&self, fault: Fault) -> CheckError {
        let premises = self
            .node
            .premises()
            .map(|p| self.derived[p.index()].to_dyadic())
            .collect();
        CheckError::invalid(self.id, self.node, premises, fault)
    }

    /// Returns a copy of what premise `p` derived.
    fn premise(&self, p: NodeId) -> Derived {
        self.derived[p.index()].clone()
    }

    /// Fails with [`Fault::Kind`] unless `m` has the kind the rule acts on.
    fn expect(&self, m: Member, kind: Kind) -> Result<(), CheckError> {
        if self.forest.kind(self.occ(m)) == kind {
            Ok(())
        } else {
            Err(self.fail(Fault::Kind { member: m }))
        }
    }

    /// Consumes one copy of `o` from the linear zone of `d`, the derived
    /// sequent of premise `premise`. A `⊤` above stands in for a missing
    /// one, but never for a second goal.
    fn take(&self, d: &mut Derived, o: Member, premise: usize) -> Result<(), CheckError> {
        if d.gamma.remove(o) {
            return Ok(());
        }
        if !d.any {
            return Err(self.fail(Fault::Missing { premise, member: o }));
        }
        // The `⊤` puts `o` into the premise's sequent beside its zone, and
        // that sequent may have one goal at most.
        if let Some(reading) = self.reading
            && reading.position(self.occ(o)) == Side::Output
            && self.outputs(&d.gamma) > 0
        {
            return Err(self.fail(Fault::Succedents { count: 2 }));
        }
        Ok(())
    }

    /// Counts the formulas of a linear zone in output position: zero
    /// without a reading.
    fn outputs(&self, gamma: &Multiset<Member>) -> usize {
        self.reading.map_or(0, |r| {
            r.outputs(gamma.as_slice().iter().map(|&m| self.occ(m)))
        })
    }

    /// Checks the one-succedent condition on what the node derived: one
    /// output at most, and exactly one unless a `⊤` above supplies it.
    fn one_succedent(&self, d: Derived) -> Result<Derived, CheckError> {
        if self.reading.is_none() {
            return Ok(d);
        }
        let outputs = self.outputs(&d.gamma);
        if outputs > 1 || (outputs == 0 && !d.any) {
            return Err(self.fail(Fault::Succedents { count: outputs }));
        }
        Ok(d)
    }

    /// Returns the left subformula of `m`, which must have one.
    fn left(&self, m: Member) -> Member {
        self.sub(m, self.forest.left(self.occ(m)).unwrap())
    }

    /// Returns the right subformula of `m`, which must have one.
    fn right(&self, m: Member) -> Member {
        self.sub(m, self.forest.right(self.occ(m)).unwrap())
    }

    /// Returns the sequent with only `o` in its linear zone, absorbing
    /// when `any` is set.
    fn just(&self, o: Member, any: bool) -> Derived {
        Derived {
            theta: BTreeSet::new(),
            gamma: Multiset::of([o]),
            any,
            eigens: BTreeSet::new(),
        }
    }

    /// Returns the sequent both premises of a two-premise rule give
    /// together: the unrestricted zones united, the linear zones summed,
    /// absorbing if either is.
    fn join(&self, l: Derived, r: Derived) -> Derived {
        Derived {
            theta: &l.theta | &r.theta,
            gamma: l.gamma.sum(&r.gamma),
            any: l.any || r.any,
            eigens: &l.eigens | &r.eigens,
        }
    }

    /// Derives what the node proves from what its premises derived. Its
    /// linear zone may have `room` members at most ([`Fault::Surplus`]).
    fn derive(self, room: usize) -> Result<Derived, CheckError> {
        let d = self.rule()?;
        if d.gamma.as_slice().len() > room {
            return Err(self.fail(Fault::Surplus));
        }
        self.one_succedent(d).and_then(|d| self.antecedent(d))
    }

    /// In the Lambek calculus without empty antecedents, checks that what
    /// the node derived holds a formula in input position.
    fn antecedent(&self, d: Derived) -> Result<Derived, CheckError> {
        let Some(reading) = self
            .reading
            .filter(|_| !self.mode.allows_empty_antecedents())
        else {
            return Ok(d);
        };
        if d.gamma
            .as_slice()
            .iter()
            .any(|&o| reading.position(self.occ(o)) == Side::Input)
        {
            Ok(d)
        } else {
            Err(self.fail(Fault::EmptyAntecedent))
        }
    }

    /// Returns whether the conclusion of a `⊗` at `o` over the contexts
    /// `left` and `right`, listed in occurrence order and turned to start
    /// after `o`, is the right context followed by the left one.
    fn arcs(&self, o: OccId, left: &Multiset<Member>, right: &Multiset<Member>) -> bool {
        let mut members: Vec<(OccId, bool)> = left
            .as_slice()
            .iter()
            .map(|&x| (self.occ(x), true))
            .chain(right.as_slice().iter().map(|&x| (self.occ(x), false)))
            .collect();
        members.sort_unstable();
        let turn = members.partition_point(|&(x, _)| x < o);
        members.rotate_left(turn);
        members.windows(2).all(|w| !(w[0].1 && !w[1].1))
    }

    /// Applies the node's rule to what its premises derived.
    fn rule(&self) -> Result<Derived, CheckError> {
        use Node::*;
        let f = self.forest;
        if self.mode.is_ordered() && !matches!(self.node, Ax(..) | Par(..) | Tensor(..)) {
            return Err(self.fail(Fault::Forbidden));
        }
        if let Some(fault) = self.frames.faults.get(&self.id) {
            return Err(self.fail(fault.clone()));
        }
        match self.node {
            Ax(a, b) => {
                for o in [a, b] {
                    if !f.is_literal(self.occ(o)) {
                        return Err(self.fail(Fault::Kind { member: o }));
                    }
                }
                let ((x, nx), (y, ny)) = (self.atom(a), self.atom(b));
                if x != y || nx == ny {
                    return Err(self.fail(Fault::NotDual));
                }
                Ok(Derived {
                    theta: BTreeSet::new(),
                    gamma: Multiset::of([a, b]),
                    any: false,
                    eigens: BTreeSet::new(),
                })
            }
            One(o) => {
                self.expect(o, Kind::One)?;
                Ok(self.just(o, false))
            }
            Top(o) => {
                self.expect(o, Kind::Top)?;
                Ok(self.just(o, true))
            }
            Bot(o, p) => {
                self.expect(o, Kind::Bot)?;
                let mut d = self.premise(p);
                d.gamma.insert(o);
                Ok(d)
            }
            Par(o, p) => {
                self.expect(o, Kind::Par)?;
                let mut d = self.premise(p);
                self.take(&mut d, self.left(o), 0)?;
                self.take(&mut d, self.right(o), 0)?;
                d.gamma.insert(o);
                Ok(d)
            }
            Tensor(o, l, r) => {
                self.expect(o, Kind::Tensor)?;
                let (mut dl, mut dr) = (self.premise(l), self.premise(r));
                self.take(&mut dl, self.left(o), 0)?;
                self.take(&mut dr, self.right(o), 1)?;
                if self.mode.is_ordered() && !self.arcs(self.occ(o), &dl.gamma, &dr.gamma) {
                    return Err(self.fail(Fault::Order));
                }
                let mut d = self.join(dl, dr);
                d.gamma.insert(o);
                Ok(d)
            }
            With(o, l, r) => {
                self.expect(o, Kind::With)?;
                let (mut dl, mut dr) = (self.premise(l), self.premise(r));
                self.take(&mut dl, self.left(o), 0)?;
                self.take(&mut dr, self.right(o), 1)?;
                // The conclusion's context is what both premises can prove
                // it under: an absorbing premise adapts to the other one.
                let (gamma, any) = match (dl.any, dr.any) {
                    (false, false) if dl.gamma == dr.gamma => (dl.gamma, false),
                    (true, false) if dl.gamma.is_subset(&dr.gamma) => (dr.gamma, false),
                    (false, true) if dr.gamma.is_subset(&dl.gamma) => (dl.gamma, false),
                    (true, true) => (dl.gamma.union(&dr.gamma), true),
                    _ => return Err(self.fail(Fault::Differ)),
                };
                let mut gamma = gamma;
                gamma.insert(o);
                Ok(Derived {
                    theta: &dl.theta | &dr.theta,
                    gamma,
                    any,
                    eigens: &dl.eigens | &dr.eigens,
                })
            }
            Plus(o, side, p) => {
                self.expect(o, Kind::Plus)?;
                let mut d = self.premise(p);
                let chosen = match side {
                    Branch::Left => self.left(o),
                    Branch::Right => self.right(o),
                };
                self.take(&mut d, chosen, 0)?;
                d.gamma.insert(o);
                Ok(d)
            }
            Bang(o, p) => {
                self.expect(o, Kind::Bang)?;
                let mut d = self.premise(p);
                self.take(&mut d, self.left(o), 0)?;
                if !d.gamma.is_empty() {
                    return Err(self.fail(Fault::NotEmpty));
                }
                // Promotion fixes the linear zone: a ⊤ above cannot absorb
                // past it.
                Ok(Derived {
                    theta: d.theta,
                    gamma: Multiset::of([o]),
                    any: false,
                    eigens: d.eigens,
                })
            }
            Quest(o, p) => {
                self.expect(o, Kind::Quest)?;
                let mut d = self.premise(p);
                d.theta.remove(&self.left(o));
                d.gamma.insert(o);
                Ok(d)
            }
            Copy(a, p) => {
                if f.parent(self.occ(a)).map(|q| f.kind(q)) != Some(Kind::Quest) {
                    return Err(self.fail(Fault::NotUnderQuest { member: a }));
                }
                let mut d = self.premise(p);
                self.take(&mut d, a, 0)?;
                d.theta.insert(a);
                Ok(d)
            }
            Weaken(o, p) => {
                let occ = self.occ(o);
                // Weakening a `?` formula is a rule of every mode; the goal
                // is never weakened.
                if !self.mode.affine && f.kind(occ) != Kind::Quest {
                    return Err(self.fail(Fault::Forbidden));
                }
                if self
                    .reading
                    .is_some_and(|r| r.position(occ) == Side::Output)
                {
                    return Err(self.fail(Fault::Succedents { count: 0 }));
                }
                let mut d = self.premise(p);
                d.gamma.insert(o);
                Ok(d)
            }
            Mix(l, r) => {
                // Mix has no intuitionistic form: a premise would lack the
                // goal.
                if !self.mode.mix || self.mode.intuitionistic {
                    return Err(self.fail(Fault::Forbidden));
                }
                Ok(self.join(self.premise(l), self.premise(r)))
            }
            Cut(a, l, r) => {
                // The occurrence of `A⊥` at the place `a` has in `A`,
                // found from the cut pairs themselves, under `a`'s frame.
                let o = self.occ(a);
                let Some(dual) = f
                    .cut_pairs()
                    .find(|&(root, _)| root <= o && o.get() < root.get() + f.size(root))
                    .map(|(root, dual)| OccId::new(dual.get() + (o.get() - root.get())))
                else {
                    return Err(self.fail(Fault::NotACut { member: a }));
                };
                let dual = self.sub(a, dual);
                let (mut dl, mut dr) = (self.premise(l), self.premise(r));
                self.take(&mut dl, a, 0)?;
                self.take(&mut dr, dual, 1)?;
                let mut d = self.join(dl, dr);
                // The cut chooses its frame's terms as a witness is chosen,
                // so their eigenvariables come in.
                if let Some(at) = a.index().checked_sub(self.frames.first) {
                    for &t in &self.frames.members[at].1 {
                        self.frames.eigens(t, &mut d.eigens);
                    }
                }
                Ok(d)
            }
            Forall(o, a, p) => {
                self.expect(o, Kind::Forall)?;
                let mut d = self.premise(p);
                let term = self
                    .frames
                    .terms
                    .iter()
                    .position(|&t| t == FoTerm::Eigen(a))
                    .expect("the table holds every eigenvariable's term");
                let body = self.body(o, FoTermId::new(term as u32));
                self.take(&mut d, body, 0)?;
                d.eigens.remove(&a);
                d.gamma.insert(o);
                Ok(d)
            }
            Exists(o, w, p) => {
                self.expect(o, Kind::Exists)?;
                let mut d = self.premise(p);
                self.take(&mut d, self.body(o, w), 0)?;
                self.frames.eigens(w, &mut d.eigens);
                d.gamma.insert(o);
                Ok(d)
            }
        }
    }
}

/// Returns proofs to test a pass over proofs on, each with the mode it was
/// found in:
/// - the engines' proofs of generated sequents, classical and
///   intuitionistic, linear and affine;
/// - their proofs of the smallest instance of every family;
/// - the Lambek search's proofs of generated sequents, with and without
///   empty antecedents;
/// - with the feature `interactive`, proofs with cuts of the generated
///   sequents, at the end.
#[cfg(feature = "parse")]
pub(crate) fn proofs() -> Vec<(Proof, Mode)> {
    use crate::linear::search::generate::{self, IllRules, Rng, Rules};
    use crate::linear::search::{Options, Verdict, prove_within};
    let classical = Mode::CLASSICAL;
    let mut cases: Vec<(String, Mode, Options)> = vec![];
    for (k, rules) in Rules::ALL.into_iter().enumerate() {
        let mut rng = Rng::new(900 + k as u64);
        for _ in 0..25 {
            let budget = 2 + rng.below(9);
            let provable = generate::provable(&mut rng, rules, 3, budget);
            let mode = if rules.mix {
                classical.with_mix()
            } else {
                classical
            };
            let options = Options::default().with_copies(Some(provable.copies));
            let text = generate::sequent(&provable.formulas);
            cases.push((text.clone(), mode, options.clone()));
            cases.push((text, mode.with_affine(), options));
        }
    }
    for (k, rules) in IllRules::ALL.into_iter().enumerate() {
        let mut rng = Rng::new(950 + k as u64);
        for _ in 0..25 {
            let budget = 2 + rng.below(9);
            let ill = generate::ill(&mut rng, rules, 3, budget);
            let options = Options::default().with_copies(Some(ill.copies));
            let text = generate::two_sided(&ill.hypotheses, &ill.goal);
            cases.push((text.clone(), Mode::INTUITIONISTIC, options.clone()));
            cases.push((text, Mode::INTUITIONISTIC.with_affine(), options));
        }
    }
    let mut proofs: Vec<(Proof, Mode)> = vec![];
    for (text, mode, options) in cases {
        let sequent: Sequent = text.parse().unwrap();
        let mut polls = 0;
        let outcome = prove_within(&sequent, mode, &options, &crate::Limits::default(), |_| {
            polls += 1;
            polls > 20_000
        });
        if let Ok(Verdict::Proved(proof)) = outcome.map(|o| o.verdict) {
            proofs.push((*proof, mode));
        }
    }
    // Proofs with cuts of the generated sequents, made now from the
    // generated proofs alone and added at the end.
    #[cfg(feature = "interactive")]
    let cuts = crate::linear::proofs::interactive::with_cuts(&proofs, &mut Rng::new(41));
    for family in crate::linear::families::FAMILIES {
        let instance = family.instance(family.sizes[0], 0).unwrap();
        let options = match instance.copies {
            Some(copies) => Options::default().with_copies(Some(copies)),
            None => Options::default(),
        };
        let mut polls = 0;
        let outcome = prove_within(
            &instance.sequent,
            instance.mode,
            &options,
            &crate::Limits::default(),
            |_| {
                polls += 1;
                polls > 20_000
            },
        );
        if let Ok(Verdict::Proved(proof)) = outcome.map(|o| o.verdict) {
            proofs.push((*proof, instance.mode));
        }
    }
    let mut rng = Rng::new(36);
    for k in 0..60 {
        let mode = if k % 2 == 0 {
            Mode::LAMBEK
        } else {
            Mode::LAMBEK.with_empty_antecedents()
        };
        let budget = 2 + rng.below(8);
        let (hypotheses, goal) = generate::lambek(&mut rng, 3, budget, k % 2 == 1);
        let text = generate::lambek_text(&hypotheses, &goal);
        let sequent = Sequent::parse_ordered(
            &text,
            crate::linear::Order::Cyclic,
            &crate::Limits::default(),
        )
        .unwrap();
        if let Ok(Verdict::Proved(proof)) =
            crate::linear::search::prove(&sequent, mode, &Options::default()).map(|o| o.verdict)
        {
            proofs.push((*proof, mode));
        }
    }
    #[cfg(feature = "interactive")]
    proofs.extend(cuts);
    proofs
}