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

use super::{MellStructure, Parts, Placed, Placement};
use crate::Error;
use crate::limits::{Limits, Refusal};
use crate::linear::fragment::{Fragment, Mode};
use crate::linear::nets::{Criterion, NetError, ProofStructure, VertexId};
use crate::linear::occurrences::{Forest, Member, OccId};
use crate::linear::proofs::{Node, NodeId, Proof};
use crate::linear::search::generate::{self, IllRules, Rng, Rules};
use crate::linear::search::{Engine, Options, Verdict};
use crate::linear::sequents::{Kind, Sequent};

/// Wraps a raw vertex id.
const fn v(id: u32) -> VertexId {
    VertexId::new(id)
}

/// Wraps a raw occurrence id.
const fn o(id: u32) -> OccId {
    OccId::new(id)
}

/// The forest of a sequent.
fn forest(input: &str) -> Forest {
    let s: Sequent = input.parse().unwrap_or_else(|e| panic!("{input:?}: {e}"));
    Forest::new(&s).unwrap()
}

/// Builds a structure from its non-root vertices as (occurrence, parent,
/// box), its links and weakenings, or returns the error.
fn build(
    input: &str,
    vertices: &[(u32, u32, Option<u32>)],
    links: &[(u32, u32)],
    weakenings: &[(u32, u32)],
) -> Result<MellStructure, Error> {
    let parts = Parts {
        vertices: vertices
            .iter()
            .map(|&(oc, p, b)| Placed::new(o(oc), v(p), b.map(v)))
            .collect(),
        cuts: Vec::new(),
        links: links.iter().map(|&(x, y)| (v(x), v(y))).collect(),
        weakenings: weakenings.iter().map(|&(x, y)| (v(x), v(y))).collect(),
    };
    MellStructure::from_parts(forest(input), Criterion::MLL, &parts, &Limits::default())
}

/// Checks that a structure is a net, sequentializes into a proof the
/// checker accepts, and desequentializes back into a net, the same up to
/// the links its weakenings hang on where no occurrence has two vertices:
/// otherwise the term does not say which instance a rule acts on.
fn round_trip(net: &MellStructure) -> Proof {
    assert_eq!(net.is_correct(|_| false), Ok(()));
    let proof = net.sequentialize(&Limits::default(), |_| false).unwrap();
    let mode = if net.criterion().mix {
        Mode::CLASSICAL.with_mix()
    } else {
        Mode::CLASSICAL
    };
    assert_eq!(proof.mode(), Some(mode));
    assert_eq!(proof.check(mode), Ok(()));
    let back =
        MellStructure::from_proof(&proof, net.criterion(), &Limits::default(), |_| false).unwrap();
    let mut seen = vec![false; net.forest().len()];
    let repeats = net.vertices().any(|x| {
        let o = net.occurrence(x).unwrap().index();
        std::mem::replace(&mut seen[o], true)
    });
    assert!(
        repeats || isomorphic(net, &back),
        "{net:?}\nback as\n{back:?}"
    );
    proof
}

/// One pair of vertices to match, or two lists of copies to match in some
/// order.
#[derive(Clone)]
enum Pending {
    /// These two vertices.
    Pair(u32, u32),
    /// These copies, in some order.
    Bag(Vec<u32>, Vec<u32>),
}

/// Returns whether two structures over one forest are the same up to the
/// order of each `?` vertex's copies, the numbering of the vertices and the
/// links the weakenings hang on: a bijection that keeps occurrences,
/// premises, boxes and links. A search over the copies' orders, for small
/// structures.
pub(in crate::linear::nets) fn isomorphic(a: &MellStructure, b: &MellStructure) -> bool {
    if a.len() != b.len() || a.links().len() != b.links().len() {
        return false;
    }
    let roots = a.forest().roots().len() as u32;
    let pending = (0..roots).map(|r| Pending::Pair(r, r)).collect();
    matches(a, b, vec![None; a.len()], vec![None; b.len()], pending)
}

/// Extends a partial bijection over the pending pairs, backtracking over
/// the order of copies.
fn matches(
    a: &MellStructure,
    b: &MellStructure,
    mut map: Vec<Option<u32>>,
    mut back: Vec<Option<u32>>,
    mut pending: Vec<Pending>,
) -> bool {
    while let Some(next) = pending.pop() {
        match next {
            Pending::Pair(x, y) => {
                match (map[x as usize], back[y as usize]) {
                    (Some(m), _) if m == y => continue,
                    (None, None) => {}
                    _ => return false,
                }
                let (vx, vy) = (v(x), v(y));
                if a.occurrence(vx) != b.occurrence(vy) {
                    return false;
                }
                map[x as usize] = Some(y);
                back[y as usize] = Some(x);
                match (a.inside(vx), b.inside(vy)) {
                    (None, None) => {}
                    (Some(p), Some(q)) => pending.push(Pending::Pair(p.get(), q.get())),
                    _ => return false,
                }
                match (a.partner(vx), b.partner(vy)) {
                    (None, None) => {}
                    (Some(p), Some(q)) => pending.push(Pending::Pair(p.get(), q.get())),
                    _ => return false,
                }
                let px: Vec<u32> = a.premises(vx).map(VertexId::get).collect();
                let py: Vec<u32> = b.premises(vy).map(VertexId::get).collect();
                if px.len() != py.len() {
                    return false;
                }
                if a.forest().kind(a.occurrence(vx).unwrap()) == Kind::Quest {
                    pending.push(Pending::Bag(px, py));
                } else {
                    pending.extend(px.into_iter().zip(py).map(|(p, q)| Pending::Pair(p, q)));
                }
            }
            Pending::Bag(xs, ys) => {
                let Some((&first, rest)) = xs.split_first() else {
                    continue;
                };
                return (0..ys.len()).any(|j| {
                    let mut others = ys.clone();
                    let y = others.remove(j);
                    let mut pending = pending.clone();
                    pending.push(Pending::Bag(rest.to_vec(), others));
                    pending.push(Pending::Pair(first, y));
                    matches(a, b, map.clone(), back.clone(), pending)
                });
            }
        }
    }
    true
}

/// Proves a sequent within `copies` copies and a bound of polls of its
/// stop, which keeps a debug build's search on a large generated sequent
/// short, with the focused engine, or in intuitionistic mode the engine
/// the dispatch gives; `None` unless it found a proof.
fn proof_of(sequent: &Sequent, mode: Mode, copies: u32) -> Option<Proof> {
    let engine = (!mode.is_intuitionistic()).then_some(Engine::Focus);
    let options = Options::default()
        .with_copies(Some(copies))
        .with_engine(engine);
    let mut polls = 0u32;
    let outcome =
        crate::linear::search::prove_within(sequent, mode, &options, &Limits::default(), |_| {
            polls += 1;
            polls > 20_000
        })
        .ok()?;
    match outcome.verdict {
        Verdict::Proved(proof) => Some(*proof),
        Verdict::Unprovable(_) | Verdict::Unknown(_) => None,
    }
}

/// Fixed nets: a box with a copy inside it, its contraction outside, a
/// weakening, and the witnesses of what is no net.
#[test]
fn classic_nets() {
    // ⊢ ?~a, !a: occurrences 0 ?~a, 1 ~a, 2 !a, 3 a; vertices 0 ?~a, 1 !a
    // (the roots), 2 a in the box of 1, 3 the copy ~a in that box.
    let net = build(
        "|- ?~a, !a",
        &[(3, 1, Some(1)), (1, 0, Some(1))],
        &[(2, 3)],
        &[],
    )
    .unwrap();
    assert_eq!(net.auxiliary_doors(v(1)), [v(0)]);
    let proof = round_trip(&net);
    assert_eq!(
        proof.nodes(),
        [
            Node::Ax(Member::from(o(1)), Member::from(o(3))),
            Node::Copy(Member::from(o(1)), NodeId::new(0)),
            Node::Bang(Member::from(o(2)), NodeId::new(1)),
            Node::Quest(Member::from(o(0)), NodeId::new(2)),
        ]
    );
    // The copy outside the box links out of it.
    assert!(matches!(
        build("|- ?~a, !a", &[(3, 1, Some(1)), (1, 0, None)], &[(2, 3)], &[]),
        Err(Error::Net(e)) if *e == NetError::Placement { vertex: v(2), fault: Placement::Link }
    ));
    // ⊢ ?~a, !a ⊗ !a: two boxes, a copy in each, one contraction: 0 ?~a,
    // 1 ~a, 2 ⊗, 3 !a, 4 a, 5 !a, 6 a. Vertices 0 ?~a, 1 ⊗, 2 !a, 3 !a,
    // 4 a, 5 a, 6 and 7 the copies.
    let net = build(
        "|- ?~a, !a * !a",
        &[
            (3, 1, None),
            (5, 1, None),
            (4, 2, Some(2)),
            (6, 3, Some(3)),
            (1, 0, Some(2)),
            (1, 0, Some(3)),
        ],
        &[(4, 6), (5, 7)],
        &[],
    )
    .unwrap();
    round_trip(&net);
    // ⊢ ?b, a, ~a: the weakening hangs on the link.
    let weakened = build("|- ?b, a, ~a", &[], &[(1, 2)], &[(0, 1)]).unwrap();
    round_trip(&weakened);
    assert_eq!(
        build("|- ?b, a, ~a", &[], &[(1, 2)], &[])
            .unwrap()
            .is_correct(|_| false),
        Err(NetError::Unattached { vertex: v(0) })
    );
    // ⊢ ?c ⊗ (a ⅋ (b ⅋ ~b)), ~a, whose components the weakening count
    // takes for a net: wherever the weakening hangs, a switching cycle runs
    // through the ⊗. Occurrences 0 ⊗, 1 ?c, 2 c, 3 ⅋, 4 a, 5 ⅋, 6 b, 7 ~b,
    // 8 ~a; vertices 0 ⊗, 1 ~a, 2 ?c, 3 ⅋, 4 a, 5 ⅋, 6 b, 7 ~b.
    for literal in [1, 4, 6, 7] {
        let net = build(
            "|- ?c * (a par (b par ~b)), ~a",
            &[
                (1, 0, None),
                (3, 0, None),
                (4, 3, None),
                (5, 3, None),
                (6, 5, None),
                (7, 5, None),
            ],
            &[(4, 1), (6, 7)],
            &[(2, literal)],
        )
        .unwrap();
        assert!(
            matches!(
                net.is_correct(|_| false),
                Err(NetError::SwitchingCycle { .. })
            ),
            "hung on {literal}"
        );
    }
    // ⊢ !(a ⊗ b), ?~a, ?~b with both copies in the box is a net; with
    // ⊢ !(a ⅋ b) the box's contents fall apart. Occurrences 0 !, 1 ⊗ or
    // ⅋, 2 a, 3 b, 4 ?~a, 5 ~a, 6 ?~b, 7 ~b; vertices 0 !, 1 ?~a, 2 ?~b,
    // 3 the connective, 4 a, 5 b, 6 ~a, 7 ~b.
    let vertices = [
        (1, 0, Some(0)),
        (2, 3, Some(0)),
        (3, 3, Some(0)),
        (5, 1, Some(0)),
        (7, 2, Some(0)),
    ];
    let net = build("|- !(a * b), ?~a, ?~b", &vertices, &[(4, 6), (5, 7)], &[]).unwrap();
    round_trip(&net);
    let net = build("|- !(a par b), ?~a, ?~b", &vertices, &[(4, 6), (5, 7)], &[]).unwrap();
    let Err(NetError::InBox { door, error }) = net.is_correct(|_| false) else {
        panic!("a box whose contents fall apart is no net");
    };
    assert_eq!(door, v(0));
    assert!(matches!(*error, NetError::Disconnected { ref parts } if parts.len() == 2));
    assert_eq!(
        NetError::InBox { door, error }.describe(&net).to_string(),
        "inside the box of !(a ⅋ b)[0], every switching falls into 2 parts; keeping every left \
         premise, they are {a ⅋ b[3], ?~a[1]} and {b[5], ?~b[2]}"
    );
}

/// Random proofs of unit-free MELL, with and without Mix, found by the
/// focused engine: each desequentializes into a net, which sequentializes
/// into a proof the checker accepts, which desequentializes into the same
/// net up to the links its weakenings hang on.
#[test]
fn round_trips() {
    let mut cases = (0, 0, 0);
    for (seed, mix) in [(11, false), (12, true)] {
        let mut rng = Rng::new(seed);
        for _ in 0..120 {
            let rules = Rules {
                units: false,
                additives: false,
                mix,
                exponentials: true,
            };
            let budget = 2 + rng.below(10);
            let provable = generate::provable(&mut rng, rules, 3, budget);
            let text = generate::sequent(&provable.formulas);
            let sequent: Sequent = text.parse().unwrap();
            let mode = if mix {
                Mode::CLASSICAL.with_mix()
            } else {
                Mode::CLASSICAL
            };
            let Some(proof) = proof_of(&sequent, mode, provable.copies) else {
                continue;
            };
            let criterion = Criterion {
                mix,
                ..Criterion::MLL
            };
            let net = MellStructure::from_proof(&proof, criterion, &Limits::default(), |_| false)
                .unwrap_or_else(|e| panic!("{text:?}: {e}"));
            round_trip(&net);
            cases.0 += 1;
            if net.boxes().next().is_some() {
                cases.1 += 1;
            }
            if net.vertices().any(|q| net.hangs_on(q).is_some()) {
                cases.2 += 1;
            }
        }
    }
    assert!(cases.0 > 100 && cases.1 > 30 && cases.2 > 10, "{cases:?}");
}

/// On MLL sequents the criterion is the one of [`ProofStructure`]: random
/// linkings of random sequents get the same verdict.
#[test]
fn agrees_with_the_multiplicative_criterion() {
    let mut rng = Rng::new(21);
    let mut verdicts = [0; 2];
    for i in 0..300 {
        let rules = Rules {
            units: false,
            additives: false,
            mix: i % 3 == 2,
            exponentials: false,
        };
        let budget = 2 + rng.below(9);
        let mut formulas = generate::provable(&mut rng, rules, 3, budget).formulas;
        if i % 2 == 1 {
            generate::mutate(&mut rng, &mut formulas, 3);
        }
        let s: Sequent = generate::sequent(&formulas).parse().unwrap();
        let forest = Forest::new(&s).unwrap();
        let mut links = Vec::new();
        for a in 0..s.atom_names().len() {
            let atom = crate::linear::sequents::Atom::new(a as u32);
            let atoms = forest.literals(atom, crate::linear::occurrences::Sign::Atom);
            let mut duals = forest
                .literals(atom, crate::linear::occurrences::Sign::Dual)
                .to_vec();
            for k in (1..duals.len()).rev() {
                duals.swap(k, rng.below(k + 1));
            }
            links.extend(atoms.iter().zip(duals).map(|(&x, y)| (x, y)));
        }
        let mix = i % 4 == 0;
        let criterion = Criterion {
            mix,
            ..Criterion::MLL
        };
        let pairs: Vec<_> = links
            .iter()
            .map(|&(x, y)| (v(x.get()), v(y.get())))
            .collect();
        let mll = ProofStructure::from_links(forest.clone(), criterion, &pairs).unwrap();
        // The same trees with the roots first: vertex numbers by a map.
        let roots = forest.roots().len() as u32;
        let mut vertex = vec![0; forest.len()];
        for (i, &r) in forest.roots().iter().enumerate() {
            vertex[r.index()] = i as u32;
        }
        let mut next = roots;
        let mut vertices = Vec::new();
        for occurrence in forest.ids() {
            if let Some(p) = forest.parent(occurrence) {
                vertex[occurrence.index()] = next;
                next += 1;
                vertices.push(Placed::new(occurrence, v(vertex[p.index()]), None));
            }
        }
        let parts = Parts {
            vertices,
            cuts: Vec::new(),
            links: links
                .iter()
                .map(|&(x, y)| (v(vertex[x.index()]), v(vertex[y.index()])))
                .collect(),
            weakenings: Vec::new(),
        };
        let net = MellStructure::from_parts(forest, criterion, &parts, &Limits::default()).unwrap();
        let expected = mll.is_correct(|_| false).is_ok();
        assert_eq!(
            net.is_correct(|_| false).is_ok(),
            expected,
            "{s} with {links:?}"
        );
        verdicts[usize::from(expected)] += 1;
        if expected {
            round_trip(&net);
        }
    }
    assert!(verdicts[0] > 50 && verdicts[1] > 50, "{verdicts:?}");
}

/// A node of a box depth in [`enumerate`]: a vertex, or the door of a `?`
/// vertex.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Place {
    /// A vertex of the depth.
    Vertex(u32),
    /// The door of this `?` vertex.
    Door(u32),
}

/// An edge of a box depth in [`enumerate`]: its two ends and the vertex
/// whose switching keeps it, if any.
type Edge = (Place, Place, Option<Place>);

/// Returns the representative of `x` in a union-find without ranks.
fn find(parent: &[usize], mut x: usize) -> usize {
    while parent[x] != x {
        x = parent[x];
    }
    x
}

/// The criterion by brute force, written from the definition without the
/// structure's graph: at every box depth, its vertices, the `!` vertices
/// of the boxes in it, a door for every `?` vertex outside with a wire
/// into it; every switching of every depth acyclic, and connected
/// without Mix. `None` when a depth has more than 4 096 switchings.
fn enumerate(net: &MellStructure) -> Option<bool> {
    let f = net.forest();
    let kind = |x: VertexId| f.kind(net.occurrence(x).unwrap());
    let mut depths: Vec<Option<VertexId>> = vec![None];
    depths.extend(net.boxes().map(Some));
    for depth in depths {
        let mut nodes: Vec<Place> = net
            .vertices()
            .filter(|&x| net.inside(x) == depth)
            .map(|x| Place::Vertex(x.get()))
            .collect();
        // Edges: (a, b, switch), the switch the vertex whose choice keeps
        // the edge, or `None`.
        let mut edges: Vec<Edge> = Vec::new();
        let add = |edges: &mut Vec<Edge>, e: Edge| {
            if !edges.contains(&e) {
                edges.push(e);
            }
        };
        for x in net.vertices().filter(|&x| net.inside(x) == depth) {
            if let Some(p) = net.partner(x)
                && p > x
            {
                add(
                    &mut edges,
                    (Place::Vertex(x.get()), Place::Vertex(p.get()), None),
                );
            }
            if matches!(kind(x), Kind::Tensor | Kind::Par) {
                for c in net.premises(x) {
                    let switch = (kind(x) == Kind::Par).then_some(Place::Vertex(x.get()));
                    add(
                        &mut edges,
                        (Place::Vertex(x.get()), Place::Vertex(c.get()), switch),
                    );
                }
            }
        }
        // Wires of every `?` vertex: the end at its own depth, each box
        // border crossed on the way out.
        for q in net.vertices().filter(|&q| kind(q) == Kind::Quest) {
            let ends: Vec<VertexId> = net.premises(q).chain(net.hangs_on(q)).collect();
            for end in ends {
                let mut at = end;
                let mut b = net.inside(end);
                loop {
                    let here = b;
                    let source = if here == net.inside(q) {
                        Place::Vertex(q.get())
                    } else {
                        Place::Door(q.get())
                    };
                    if here == depth {
                        if source != Place::Vertex(q.get()) && !nodes.contains(&source) {
                            nodes.push(source);
                        }
                        add(&mut edges, (source, Place::Vertex(at.get()), Some(source)));
                    }
                    if here == net.inside(q) {
                        break;
                    }
                    let bang = here.unwrap();
                    at = bang;
                    b = net.inside(bang);
                }
            }
        }
        let switched: Vec<Place> = {
            let mut s: Vec<Place> = edges.iter().filter_map(|e| e.2).collect();
            s.sort_by_key(|p| format!("{p:?}"));
            s.dedup();
            s
        };
        let choices: Vec<Vec<usize>> = switched
            .iter()
            .map(|&s| {
                (0..edges.len())
                    .filter(|&i| edges[i].2 == Some(s))
                    .collect()
            })
            .collect();
        let count: usize = choices.iter().map(Vec::len).product();
        if count > 4096 {
            return None;
        }
        for mut k in 0..count {
            let mut kept: Vec<usize> = (0..edges.len()).filter(|&i| edges[i].2.is_none()).collect();
            for c in &choices {
                kept.push(c[k % c.len()]);
                k /= c.len();
            }
            // Union-find over the nodes.
            let index = |p: Place| nodes.iter().position(|&q| q == p).unwrap();
            let mut parent: Vec<usize> = (0..nodes.len()).collect();
            for &i in &kept {
                let (a, b) = (
                    find(&parent, index(edges[i].0)),
                    find(&parent, index(edges[i].1)),
                );
                if a == b {
                    return Some(false);
                }
                parent[a] = b;
            }
            if !net.criterion().mix && kept.len() + 1 != nodes.len() {
                return Some(false);
            }
        }
    }
    Some(true)
}

/// The criterion against [`enumerate`] on random structures: nets of
/// random proofs, and their mutants, in which a link, a copy's box or a
/// weakening's literal moves where the structure allows.
#[test]
fn agrees_with_enumeration() {
    let mut cases = [0; 2];
    for (seed, mix) in [(31, false), (32, true)] {
        let mut rng = Rng::new(seed);
        for _ in 0..150 {
            let rules = Rules {
                units: false,
                additives: false,
                mix,
                exponentials: true,
            };
            let budget = 2 + rng.below(8);
            let provable = generate::provable(&mut rng, rules, 2, budget);
            let sequent: Sequent = generate::sequent(&provable.formulas).parse().unwrap();
            let mode = if mix {
                Mode::CLASSICAL.with_mix()
            } else {
                Mode::CLASSICAL
            };
            let Some(proof) = proof_of(&sequent, mode, provable.copies) else {
                continue;
            };
            let criterion = Criterion {
                mix,
                ..Criterion::MLL
            };
            let net = MellStructure::from_proof(&proof, criterion, &Limits::default(), |_| false)
                .unwrap();
            for round in 0..8 {
                let mut parts = net.parts();
                if round > 0 {
                    mutate(&net, &mut parts, &mut rng);
                }
                let Ok(mutant) = MellStructure::from_parts(
                    net.forest().clone(),
                    criterion,
                    &parts,
                    &Limits::default(),
                ) else {
                    continue;
                };
                let Some(expected) = enumerate(&mutant) else {
                    continue;
                };
                let verdict = mutant.is_correct(|_| false);
                assert_eq!(verdict.is_ok(), expected, "{mutant:?}: {verdict:?}");
                cases[usize::from(expected)] += 1;
                if expected {
                    round_trip(&mutant);
                }
            }
        }
    }
    assert!(cases[0] > 30 && cases[1] > 100, "{cases:?}");
}

/// In intuitionistic mode the net is the one of the one-sided sequent and
/// the criterion without Mix decides it: a fixed net and its crossed
/// linking, whose cycle lies in the box; random intuitionistic proofs of
/// the focused engine, whose nets the criterion accepts; and their
/// mutants, whose verdict is [`enumerate`]'s and which, accepted,
/// sequentialize into terms the checker accepts in intuitionistic mode.
/// Affine modes have no structures with boxes.
#[test]
fn intuitionistic_nets() {
    assert_eq!(
        MellStructure::exist(Fragment::MELL, Mode::INTUITIONISTIC),
        Err(NetError::NotMell {
            fragment: Fragment::MELL,
            mode: Mode::INTUITIONISTIC
        })
    );
    let imell = Fragment::MLL.union(Fragment::EXPONENTIALS);
    assert_eq!(MellStructure::exist(imell, Mode::INTUITIONISTIC), Ok(()));
    for affine in [
        Mode::INTUITIONISTIC.with_affine(),
        Mode::CLASSICAL.with_affine(),
    ] {
        assert!(MellStructure::exist(imell, affine).is_err(), "{affine}");
    }
    // ⊢ !((a ⊸ a) ⊗ (a ⊸ a)): 0 !, 1 ⊗, 2 ⅋, 3 ~a, 4 a, 5 ⅋, 6 ~a, 7 a,
    // vertex k occurrence k, all in the box of 0.
    let text = "|- !((a -o a) * (a -o a))";
    let vertices = [
        (1, 0, Some(0)),
        (2, 1, Some(0)),
        (3, 2, Some(0)),
        (4, 2, Some(0)),
        (5, 1, Some(0)),
        (6, 5, Some(0)),
        (7, 5, Some(0)),
    ];
    let net = build(text, &vertices, &[(3, 4), (6, 7)], &[]).unwrap();
    let proof = round_trip(&net);
    assert_eq!(proof.check(Mode::INTUITIONISTIC), Ok(()));
    let crossed = build(text, &vertices, &[(3, 7), (6, 4)], &[]).unwrap();
    assert!(
        matches!(
            crossed.is_correct(|_| false),
            Err(NetError::InBox { door, error })
                if door == v(0) && matches!(*error, NetError::SwitchingCycle { .. })
        ),
        "{:?}",
        crossed.is_correct(|_| false)
    );

    let criterion = Criterion::of(Mode::INTUITIONISTIC).unwrap();
    let rules = IllRules {
        units: false,
        additives: false,
        zero: false,
        exponentials: true,
    };
    let mut rng = Rng::new(41);
    let mut cases = [0; 3];
    for _ in 0..200 {
        let budget = 2 + rng.below(9);
        let ill = generate::ill(&mut rng, rules, 2, budget);
        let text = generate::two_sided(&ill.hypotheses, &ill.goal);
        let sequent: Sequent = text.parse().unwrap();
        let Some(proof) = proof_of(&sequent, Mode::INTUITIONISTIC, ill.copies) else {
            continue;
        };
        let net = MellStructure::from_proof(&proof, criterion, &Limits::default(), |_| false)
            .unwrap_or_else(|e| panic!("{text:?}: {e}"));
        assert!(net.reading().is_ok(), "{text:?}");
        cases[2] += usize::from(net.boxes().next().is_some());
        for round in 0..8 {
            let mut parts = net.parts();
            if round > 0 {
                mutate(&net, &mut parts, &mut rng);
            }
            let Ok(mutant) = MellStructure::from_parts(
                net.forest().clone(),
                criterion,
                &parts,
                &Limits::default(),
            ) else {
                continue;
            };
            let verdict = mutant.is_correct(|_| false);
            if let Some(expected) = enumerate(&mutant) {
                assert_eq!(verdict.is_ok(), expected, "{mutant:?}: {verdict:?}");
            }
            cases[usize::from(verdict.is_ok())] += 1;
            if verdict.is_ok() {
                let term = round_trip(&mutant);
                assert_eq!(
                    term.check(Mode::INTUITIONISTIC),
                    Ok(()),
                    "{text:?}: {mutant:?}"
                );
            }
        }
    }
    assert!(
        cases[0] > 30 && cases[1] > 150 && cases[2] > 40,
        "{cases:?}"
    );
}

/// Moves one link, one copy's box or one weakening's literal of a
/// structure's parts at random.
fn mutate(net: &MellStructure, parts: &mut Parts, rng: &mut Rng) {
    let roots = net.forest().roots().len();
    match rng.below(3) {
        0 if parts.links.len() >= 2 => {
            let (i, j) = (rng.below(parts.links.len()), rng.below(parts.links.len()));
            let ((a, b), (c, d)) = (parts.links[i], parts.links[j]);
            if net
                .forest()
                .dual_literals(net.occurrence(a).unwrap(), net.occurrence(d).unwrap())
            {
                parts.links[i] = (a, d);
                parts.links[j] = (c, b);
            }
        }
        1 => {
            let copies: Vec<usize> = (0..parts.vertices.len())
                .filter(|&i| {
                    let p = parts.vertices[i].parent.unwrap();
                    net.forest().kind(net.occurrence(p).unwrap()) == Kind::Quest
                })
                .collect();
            let boxes: Vec<VertexId> = net.boxes().collect();
            if let (false, false) = (copies.is_empty(), boxes.is_empty()) {
                let i = copies[rng.below(copies.len())];
                let b = boxes[rng.below(boxes.len())];
                let inside = if rng.one_in(3) { None } else { Some(b) };
                // The copy's tree moves with it, but for what lies in
                // boxes of its own.
                let moved = VertexId::new((roots + i) as u32);
                let old = parts.vertices[i].inside;
                for k in 0..parts.vertices.len() {
                    let mut x = Some(VertexId::new((roots + k) as u32));
                    while let Some(y) = x
                        && y != moved
                    {
                        x = net.parent(y);
                    }
                    if x.is_some() && parts.vertices[k].inside == old {
                        parts.vertices[k].inside = inside;
                    }
                }
            }
        }
        _ if !parts.weakenings.is_empty() => {
            let i = rng.below(parts.weakenings.len());
            let literals: Vec<VertexId> = net
                .vertices()
                .filter(|&x| net.forest().is_literal(net.occurrence(x).unwrap()))
                .collect();
            parts.weakenings[i].1 = literals[rng.below(literals.len())];
        }
        _ => {}
    }
}

/// A term of a few dozen nodes that unfolds into more vertices than a
/// structure holds is refused before anything is made: each level uses
/// the one below twice, so the 48th makes 3 · 2⁴⁸ vertices.
#[test]
fn an_unfolding_past_the_vertices_is_refused() {
    // ⊢ ?(a ⅋ ~a): 0 ?, 1 ⅋, 2 a, 3 ~a.
    let f = forest("|- ?(a par ~a)");
    let m = |id| Member::from(o(id));
    let mut nodes = vec![Node::Ax(m(2), m(3)), Node::Par(m(1), NodeId::new(0))];
    for _ in 0..48 {
        let last = NodeId::new(nodes.len() as u32 - 1);
        nodes.push(Node::Mix(last, last));
        nodes.push(Node::Copy(m(1), NodeId::new(nodes.len() as u32 - 1)));
    }
    nodes.push(Node::Copy(m(1), NodeId::new(nodes.len() as u32 - 1)));
    nodes.push(Node::Quest(m(0), NodeId::new(nodes.len() as u32 - 1)));
    let root = NodeId::new(nodes.len() as u32 - 1);
    let proof = Proof::new(f, nodes, root).unwrap();
    let mix = Mode::CLASSICAL.with_mix();
    assert_eq!(proof.check(mix), Ok(()));
    let criterion = Criterion::MLL.with_mix();
    let refused = MellStructure::from_proof(&proof, criterion, &Limits::default(), |_| false);
    assert!(
        matches!(refused, Err(Error::Refused(Refusal::Index { count, .. })) if count > 1 << 49),
        "{refused:?}"
    );
}

/// The text form: the sequent, each `?` vertex's copies with the box of a
/// copy outside the `?`'s, or the literal a weakening hangs on, the links
/// by their first id, and the verdict.
#[test]
fn text_form() {
    let sequent: Sequent = "!a, !b |- !a".parse().unwrap();
    let outcome =
        crate::linear::search::prove(&sequent, Mode::CLASSICAL, &Options::default()).unwrap();
    let Verdict::Proved(proof) = &outcome.verdict else {
        panic!("{:?}", outcome.verdict);
    };
    let net =
        MellStructure::from_proof(proof, Criterion::MLL, &Limits::default(), |_| false).unwrap();
    assert_eq!(
        net.to_string(),
        "⊢ ?~a, ?~b, !a\n?~a[0]: ~a[3] in !a[2]\n?~b[1]: no copy, hangs on a[4]\n~a[3] — a[4]\nproof net"
    );
}