deduxit 0.5.0

A collection of logic suites: decides, checks, proves step by step and exports sequents of linear logic and of classical, intuitionistic and minimal propositional logic, with checked proofs, Kripke countermodels and Rocq certificates.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
// deduxit © Fabian Lukas Grubmüller 2026
// Licensed under the EUPL

//! The ordinary suite's kinds and operations.

use serde::{Deserialize, Serialize};

use super::promises::{DECIDES, EXACT, INTERPOLATES, PROVES, RENDERS, SAME_VERDICT};
use super::shared::{Term, Text};
use super::{
    Call, Check, Class, Cost, Kind, Limit, Operation, Parameter, Part, Produced, Role, Status,
    Suite, Verdict, document, object, parameters, part, same_verdict, takes, verdict_of, written,
};
use crate::ordinary::interpolant::{self, Partition};
use crate::ordinary::{
    self, Derivation, Interactive, Logic, Method, Outcome, Sequent, Translation, negative,
};
use crate::{Error, ErrorKind, Limits, Progress};

/// The ordinary suite.
pub(super) static ORDINARY: Ordinary = Ordinary;

/// The ordinary suite.
pub(super) struct Ordinary;

/// Its kinds.
static KINDS: [Kind; 5] = [
    Kind {
        name: "ordinary.sequent",
        role: Role::Statement,
        form: "ordinary_statement",
        level: Some(8),
        stored: true,
        label: "A sequent of ordinary logic, claimed in classical, intuitionistic or minimal logic",
    },
    Kind {
        name: "ordinary.session",
        role: Role::Work,
        form: "ordinary_session",
        level: Some(8),
        stored: true,
        label: "A proof of ordinary logic in progress, in LK or LJ",
    },
    Kind {
        name: "ordinary.outcome",
        role: Role::Evidence,
        form: "ordinary_outcome",
        level: Some(12),
        stored: true,
        label: "A sequent decided: its verdict with a derivation or a countermodel",
    },
    Kind {
        name: "ordinary.derivation",
        role: Role::Evidence,
        form: "ordinary_derivation",
        level: Some(8),
        stored: true,
        label: "A derivation of LK or LJ",
    },
    Kind {
        name: "ordinary.interpolant",
        role: Role::Evidence,
        form: "interpolant",
        level: None,
        stored: true,
        label: "A Craig interpolant with its two halves",
    },
];

/// The parameters of a decision.
static DECIDE_PARAMETERS: [Parameter; 2] = [
    Parameter {
        name: "method",
        values: &["embedding", "ljt", "sat", "table", "tableau"],
        default: None,
        description: "How to decide; left out, the logic's default",
    },
    Parameter {
        name: "translation",
        values: &["affine", "cbn", "cbv", "01"],
        default: None,
        description: "The embedding into linear logic, for the method `embedding`; left out, the logic's default",
    },
];

/// The settings keys a draw reads.
#[cfg(feature = "svg")]
static DRAW_READS: [&str; 7] = [
    "limits.memory_bytes",
    "limits.occurrences",
    "limits.terms",
    "limits.derivation_bytes",
    "limits.work",
    "view",
    "styles.svg",
];

/// The settings keys an export reads.
static EXPORT_READS: [&str; 9] = [
    "limits.memory_bytes",
    "limits.occurrences",
    "limits.terms",
    "limits.derivation_bytes",
    "limits.work",
    "view",
    "styles.text",
    "styles.latex",
    "styles.typst",
];

/// The settings keys a certificate reads.
#[cfg(feature = "rocq")]
static CERTIFICATE_READS: [&str; 7] = [
    "limits.memory_bytes",
    "limits.occurrences",
    "limits.terms",
    "limits.derivation_bytes",
    "limits.work",
    "view",
    "styles.rocq",
];

/// The settings keys a lambda-term reads.
static TERM_READS: [&str; 7] = [
    "limits.memory_bytes",
    "limits.occurrences",
    "limits.terms",
    "limits.derivation_bytes",
    "limits.work",
    "view",
    "lambda",
];

/// The settings keys a session's derivation reads.
static FINISH_READS: [&str; 5] = [
    "limits.memory_bytes",
    "limits.occurrences",
    "limits.terms",
    "limits.derivation_bytes",
    "limits.work",
];

/// Its operations.
static OPERATIONS: &[Operation] = &[
    Operation {
        name: "ordinary.decide",
        from: &[&["ordinary.sequent"]],
        to: "ordinary.outcome",
        class: Class::Derived,
        cost: Cost::Search,
        parameters: &DECIDE_PARAMETERS,
        reads: &super::SEARCH,
        promise: &DECIDES,
        label: "A decision",
    },
    Operation {
        name: "ordinary.start",
        from: &[&["ordinary.sequent"]],
        to: "ordinary.session",
        class: Class::Work,
        cost: Cost::Instant,
        parameters: &[],
        reads: &super::START,
        promise: &EXACT,
        label: "A proof step by step",
    },
    Operation {
        name: "ordinary.goal",
        from: &[&["ordinary.session"]],
        to: "ordinary.sequent",
        class: Class::Copy,
        cost: Cost::Instant,
        parameters: &[Parameter {
            name: "goal",
            values: &[],
            default: None,
            description: "The goal, by its number in the session",
        }],
        reads: &super::SIZES,
        promise: &EXACT,
        label: "A goal opened on its own",
    },
    Operation {
        name: "ordinary.derivation",
        from: &[&["ordinary.session", "ordinary.outcome"]],
        to: "ordinary.derivation",
        class: Class::Derived,
        cost: Cost::Instant,
        parameters: &[],
        reads: &FINISH_READS,
        promise: &PROVES,
        label: "The derivation",
    },
    Operation {
        name: "ordinary.translate",
        from: &[&["ordinary.sequent"]],
        to: "ordinary.sequent",
        class: Class::Derived,
        cost: Cost::Instant,
        parameters: &[
            Parameter {
                name: "translation",
                values: &["kolmogorov", "godel-gentzen", "kuroda", "glivenko"],
                default: Some("kolmogorov"),
                description: "The negative translation",
            },
            Parameter {
                name: "target",
                values: &["intuitionistic", "minimal"],
                default: None,
                description: "The logic the translation is claimed in; left out, the strongest result the translation gives",
            },
        ],
        reads: &super::SIZES,
        promise: &SAME_VERDICT,
        label: "A negative translation",
    },
    Operation {
        name: "ordinary.interpolate",
        from: &[&["ordinary.sequent"]],
        to: "ordinary.interpolant",
        class: Class::Derived,
        cost: Cost::Search,
        parameters: &[
            Parameter {
                name: "partition",
                values: &[],
                default: None,
                description: "The first part, `{\"left\": [positions], \"right\": [positions]}`; left out, every formula left of ⊢",
            },
            Parameter {
                name: "method",
                values: &["embedding", "ljt", "sat"],
                default: None,
                description: "How to decide the sequent; left out, the SAT solver, then the embedding for a derivation",
            },
        ],
        reads: &super::SEARCH,
        promise: &INTERPOLATES,
        label: "A Craig interpolant",
    },
    Operation {
        name: "ordinary.half",
        from: &[&["ordinary.interpolant"]],
        to: "ordinary.sequent",
        class: Class::Derived,
        cost: Cost::Instant,
        parameters: &[Parameter {
            name: "which",
            values: &["first", "second"],
            default: Some("first"),
            description: "The half: the first part with the interpolant right of ⊢, or the interpolant with the second part",
        }],
        reads: &super::SIZES,
        promise: &EXACT,
        label: "A part of an interpolant",
    },
    Operation {
        name: "ordinary.embed",
        from: &[&["ordinary.sequent"]],
        to: "linear.sequent",
        class: Class::Derived,
        cost: Cost::Instant,
        parameters: &[Parameter {
            name: "translation",
            values: &["affine", "cbn", "cbv", "01"],
            default: None,
            description: "The embedding; left out, the logic's default",
        }],
        reads: &super::SIZES,
        promise: &SAME_VERDICT,
        label: "The embedding into linear logic",
    },
    Operation {
        name: "ordinary.term",
        from: &[&["ordinary.session", "ordinary.derivation"]],
        to: "term",
        class: Class::Derived,
        cost: Cost::Instant,
        parameters: &[],
        reads: &TERM_READS,
        promise: &RENDERS,
        label: "The lambda-term",
    },
    #[cfg(feature = "svg")]
    Operation {
        name: "ordinary.draw",
        from: &[&[
            "ordinary.session",
            "ordinary.derivation",
            "ordinary.outcome",
        ]],
        to: "drawing",
        class: Class::Derived,
        cost: Cost::Instant,
        parameters: &[],
        reads: &DRAW_READS,
        promise: &RENDERS,
        label: "A drawing",
    },
    Operation {
        name: "ordinary.export",
        from: &[&["ordinary.session", "ordinary.derivation"]],
        to: "text",
        class: Class::Derived,
        cost: Cost::Instant,
        parameters: &[Parameter {
            name: "format",
            values: &["text", "latex", "typst"],
            default: Some("text"),
            description: "The format",
        }],
        reads: &EXPORT_READS,
        promise: &RENDERS,
        label: "An export",
    },
    #[cfg(feature = "rocq")]
    Operation {
        name: "ordinary.certificate",
        from: &[&["ordinary.derivation", "ordinary.outcome"]],
        to: "certificate",
        class: Class::Derived,
        cost: Cost::Instant,
        parameters: &[],
        reads: &CERTIFICATE_READS,
        promise: &super::promises::CERTIFIES,
        label: "A Rocq certificate",
    },
    Operation {
        name: "ordinary.duplicate",
        from: &[&["ordinary.sequent"]],
        to: "ordinary.sequent",
        class: Class::Copy,
        cost: Cost::Instant,
        parameters: &[],
        reads: &super::SIZES,
        promise: &EXACT,
        label: "A copy",
    },
];

/// The keys a statement adds to its sequent's document.
#[derive(Deserialize)]
struct Claim {
    /// The logic.
    logic: Logic,
}

/// A statement as written: the sequent's document with its logic and how
/// it was typed.
#[derive(Serialize)]
struct Statement<'a> {
    /// The sequent's document.
    #[serde(flatten)]
    sequent: &'a Sequent,
    /// The logic it is claimed in.
    logic: Logic,
    /// The sequent as typed.
    #[serde(skip_serializing_if = "Option::is_none")]
    text: Option<&'a str>,
}

/// Reads a statement: its sequent and its logic.
fn statement(value: &str, limits: &Limits) -> Result<(Sequent, Logic), Error> {
    let claim: Claim = object(value, "ordinary statement")?;
    Ok((document(value, limits)?, claim.logic))
}

/// Writes a statement.
pub(crate) fn write_statement(sequent: &Sequent, logic: Logic) -> Result<String, Error> {
    typed_statement(sequent, logic, None)
}

/// Writes a statement with the text it was typed as.
pub(super) fn typed_statement(
    sequent: &Sequent,
    logic: Logic,
    text: Option<&str>,
) -> Result<String, Error> {
    written(&Statement {
        sequent,
        logic,
        text,
    })
}

/// An interpolant's value.
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Interpolated {
    /// The logic.
    logic: Logic,
    /// The partition: the first part.
    partition: Partition,
    /// The sequent's outcome, as `ordinary.decide` writes one.
    outcome: serde_json::Value,
    /// The interpolant, where the sequent is valid and one was found.
    interpolant: Option<Found>,
}

/// An interpolant found.
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Found {
    /// The formula, as typed.
    formula: String,
    /// The first half, `Γ₁ ⊢ Δ₁, I`, as a statement.
    first: serde_json::Value,
    /// The second half, `I, Γ₂ ⊢ Δ₂`, as a statement.
    second: serde_json::Value,
}

/// Which input a value is, where an operation takes several kinds.
#[derive(Deserialize)]
struct Probe {
    /// An outcome's verdict.
    verdict: Option<serde::de::IgnoredAny>,
    /// A session's history.
    history: Option<serde::de::IgnoredAny>,
}

/// What an input of several kinds is.
enum Input {
    /// A session.
    Session(Box<Interactive>),
    /// An outcome.
    Outcome(Box<Outcome>),
    /// A derivation.
    Derivation(Box<Derivation>),
}

/// Reads the first input of `call`, which may be a session, an outcome or
/// a derivation, refusing a kind the operation does not take.
fn input(call: &Call<'_>, limits: &Limits) -> Result<Input, Error> {
    let value = call.input(0);
    let probe: Probe = object(value, "ordinary value")?;
    Ok(if probe.verdict.is_some() {
        takes(call, "ordinary.outcome")?;
        Input::Outcome(Box::new(document(value, limits)?))
    } else if probe.history.is_some() {
        takes(call, "ordinary.session")?;
        Input::Session(Box::new(document(value, limits)?))
    } else {
        takes(call, "ordinary.derivation")?;
        Input::Derivation(Box::new(document(value, limits)?))
    })
}

/// Returns the derivation an input holds: a session's, finished or not
/// as `finished` asks, or a valid outcome's.
fn derivation_of(
    input: Input,
    finished: bool,
    call: &Call<'_>,
    stop: &mut dyn FnMut(Progress) -> bool,
) -> Result<Derivation, Error> {
    let settings = call.settings;
    match input {
        Input::Session(session) if finished => session.finished(&settings.limits, stop),
        Input::Session(session) => {
            session.derivation_within(&settings.view, &settings.limits, stop)
        }
        Input::Outcome(outcome) => match &outcome.verdict {
            ordinary::Verdict::Valid(evidence) => evidence.derivation().cloned(),
            _ => Err(Error::NoDerivation {
                evidence: "no proof: the sequent is not shown valid",
            }),
        },
        Input::Derivation(derivation) => Ok(*derivation),
    }
}

/// Returns the bound that ended a decision, where one did.
fn limit_of(outcome: &Outcome) -> Option<Limit> {
    match &outcome.verdict {
        ordinary::Verdict::Unknown(reason) => Some(Limit {
            reason: reason.name(),
            setting: reason.setting(),
        }),
        _ => None,
    }
}

/// The parameters of a decision.
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct Decide {
    /// The method.
    method: Option<Method>,
    /// The embedding.
    translation: Option<Translation>,
}

/// The parameters of opening a goal.
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct Goal {
    /// The goal's number.
    goal: u32,
}

/// The parameters of an interpolant.
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct Interpolate {
    /// The first part.
    partition: Option<Partition>,
    /// The method.
    method: Option<Method>,
}

/// The parameters of taking a half.
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct Half {
    /// `second` for the second half.
    which: Option<String>,
}

/// The parameters of an embedding.
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct Embed {
    /// The embedding.
    translation: Option<Translation>,
}

/// The parameters of an export.
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct Format {
    /// The format.
    pub(super) format: Option<String>,
}

/// The formats an export writes.
pub(super) const FORMATS: &[&str] = &[
    "text",
    #[cfg(feature = "latex")]
    "latex",
    #[cfg(feature = "typst")]
    "typst",
];

/// Returns a check's part for a check's error: open where a bound or the
/// stop ended it, failed where it found a fault; any other error is
/// returned.
pub(super) fn checked(name: &'static str, result: Result<(), Error>) -> Result<Part, Error> {
    let status = match result {
        Ok(()) => Status::Held,
        Err(error) => match error.kind() {
            ErrorKind::Invalid | ErrorKind::Defect => Status::Failed,
            ErrorKind::Limit | ErrorKind::Stopped => Status::Open,
            _ => return Err(error),
        },
    };
    Ok(Part {
        name: name.to_owned(),
        status,
        verdict: None,
    })
}

/// The keys every ordinary outcome writes, and the clausal proof's.
#[derive(Deserialize)]
struct OutcomeKeys {
    /// The verdict's name.
    verdict: String,
    /// The logic decided, which a linear outcome lacks.
    #[expect(dead_code, reason = "read to refuse a value that has no logic")]
    logic: Logic,
    /// The proof's counts, for a valid sequent by the SAT solver.
    clausal_proof: Option<serde::de::IgnoredAny>,
}

/// Returns the verdict of an ordinary outcome, read by its reader; an
/// outcome valid by a clausal proof, which has no reader (its counts hold
/// no proof), by its keys alone.
fn outcome_verdict(value: &str, limits: &Limits) -> Result<Verdict, Error> {
    let keys: OutcomeKeys = object(value, "ordinary outcome")?;
    if keys.clausal_proof.is_some() && keys.verdict == "valid" {
        return Ok(Verdict::Valid);
    }
    document::<Outcome>(value, limits)?;
    verdict_of(value)
}

/// Returns a formula's text, as an interpolant's is written.
fn text_of(sequent: &Sequent, id: ordinary::NodeId) -> String {
    sequent.formulas().formula(id).to_string()
}

/// Reads an interpolant's value: where it holds one, its halves are
/// statements of its logic, the first with the formula last right of ⊢
/// and the second with it first left of ⊢.
fn interpolated(value: &str, limits: &Limits) -> Result<Interpolated, Error> {
    let read: Interpolated = object(value, "interpolant")?;
    if let Some(found) = &read.interpolant {
        let fault = |message: &str| Error::Json {
            form: "interpolant",
            message: message.to_owned(),
        };
        let (first, first_logic) = statement(&written(&found.first)?, limits)?;
        let (second, second_logic) = statement(&written(&found.second)?, limits)?;
        if first_logic != read.logic || second_logic != read.logic {
            return Err(fault("its halves are claimed in another logic"));
        }
        let (Some(&last), Some(&head)) = (first.right().last(), second.left().first()) else {
            return Err(fault("a half lacks the interpolant"));
        };
        if text_of(&first, last) != found.formula || text_of(&second, head) != found.formula {
            return Err(fault(
                "its halves hold another formula than the interpolant",
            ));
        }
    }
    Ok(read)
}

/// Returns the texts of the formulas at `ids` in `sequent`, sorted: a part
/// compared as a multiset.
fn texts(sequent: &Sequent, ids: impl IntoIterator<Item = ordinary::NodeId>) -> Vec<String> {
    let mut texts: Vec<String> = ids.into_iter().map(|id| text_of(sequent, id)).collect();
    texts.sort();
    texts
}

/// Returns whether an interpolant's halves are the input's parts by its
/// partition: `Γ₁ ⊢ Δ₁, I` and `I, Γ₂ ⊢ Δ₂`, each part compared as a
/// multiset of formulas.
fn halves_fit(input: &Sequent, partition: &Partition, one: &Sequent, two: &Sequent) -> bool {
    let first_side = |ids: &[ordinary::NodeId], chosen: &[usize], wanted: bool| {
        let picked = ids
            .iter()
            .enumerate()
            .filter(|(position, _)| chosen.contains(position) == wanted)
            .map(|(_, &id)| id);
        texts(input, picked)
    };
    let right = one.right();
    first_side(input.left(), &partition.left, true) == texts(one, one.left().iter().copied())
        && first_side(input.right(), &partition.right, true)
            == texts(one, right[..right.len() - 1].iter().copied())
        && first_side(input.left(), &partition.left, false)
            == texts(two, two.left()[1..].iter().copied())
        && first_side(input.right(), &partition.right, false)
            == texts(two, two.right().iter().copied())
}

/// Returns the names of the atoms of `roots` in `sequent`.
fn atom_names(sequent: &Sequent, roots: &[ordinary::NodeId]) -> Vec<String> {
    let formulas = sequent.formulas();
    let reached = interpolant::atoms_of(formulas, roots);
    formulas
        .atom_names()
        .iter()
        .zip(reached)
        .filter(|&(_, reached)| reached)
        .map(|(name, _)| name.clone())
        .collect()
}

impl Suite for Ordinary {
    fn name(&self) -> &'static str {
        "ordinary"
    }

    fn label(&self) -> &'static str {
        "Ordinary"
    }

    fn kinds(&self) -> &'static [Kind] {
        &KINDS
    }

    fn operations(&self) -> &'static [Operation] {
        OPERATIONS
    }

    fn read(&self, kind: &Kind, value: &str, limits: &Limits) -> Result<(), Error> {
        self.summary(kind, value, limits).map(drop)
    }

    fn summary(&self, kind: &Kind, value: &str, limits: &Limits) -> Result<String, Error> {
        Ok(match kind.name {
            "ordinary.sequent" => statement(value, limits)?.0.to_string(),
            "ordinary.session" => {
                let session: Interactive = document(value, limits)?;
                match session.goals().count() {
                    0 => "Proved".to_owned(),
                    1 => "1 open goal".to_owned(),
                    goals => format!("{goals} open goals"),
                }
            }
            // An outcome valid by a clausal proof has no reader: its words
            // are read alone.
            "ordinary.outcome" => match outcome_verdict(value, limits)? {
                Verdict::Valid => "Valid".to_owned(),
                Verdict::NotValid => "Not valid".to_owned(),
                Verdict::Unknown => "Not decided".to_owned(),
            },
            "ordinary.derivation" => {
                let derivation: Derivation = document(value, limits)?;
                format!("A derivation of {}", derivation.logic().calculus())
            }
            _ => {
                let read = interpolated(value, limits)?;
                match read.interpolant {
                    Some(found) => format!("Interpolant {}", found.formula),
                    None => "No interpolant".to_owned(),
                }
            }
        })
    }

    fn run(
        &self,
        call: &Call<'_>,
        stop: &mut dyn FnMut(Progress) -> bool,
    ) -> Result<Produced, Error> {
        let settings = call.settings;
        let limits = &settings.limits;
        let value = match call.operation.name {
            "ordinary.decide" => {
                let (sequent, logic) = statement(call.input(0), limits)?;
                let p: Decide = parameters(call)?;
                let options = ordinary::Options::default()
                    .with_logic(logic)
                    .with_translation(p.translation)
                    .with_method(p.method);
                let outcome =
                    ordinary::decide_within(&sequent, &options, &settings.search, limits, stop)?;
                return Ok(Produced {
                    value: written(&outcome)?,
                    limit: limit_of(&outcome),
                });
            }
            "ordinary.start" => {
                let (sequent, logic) = statement(call.input(0), limits)?;
                written(&Interactive::within(&sequent, logic, limits)?)?
            }
            "ordinary.goal" => {
                let session: Interactive = document(call.input(0), limits)?;
                let p: Goal = parameters(call)?;
                let (left, right) = session.goal(crate::linear::GoalId::new(p.goal))?;
                let sequent =
                    Sequent::new(session.formulas().clone(), left.to_vec(), right.to_vec())?;
                write_statement(&sequent, session.logic())?
            }
            "ordinary.derivation" => {
                let input = input(call, limits)?;
                written(&derivation_of(input, true, call, stop)?)?
            }
            "ordinary.translate" => {
                let (sequent, logic) = statement(call.input(0), limits)?;
                if logic != Logic::Classical {
                    return Err(Error::InvalidOption {
                        key: "logic",
                        message: "a negative translation translates a sequent of classical logic"
                            .to_owned(),
                    });
                }
                let options: negative::Options = parameters(call)?;
                let translated = negative::translate(&sequent, &options, limits)?;
                write_statement(&translated, options.target())?
            }
            "ordinary.interpolate" => {
                let (sequent, logic) = statement(call.input(0), limits)?;
                let p: Interpolate = parameters(call)?;
                let partition = p.partition.unwrap_or_default();
                let options = ordinary::Options::default()
                    .with_logic(logic)
                    .with_method(p.method);
                let interpolation = interpolant::interpolate(
                    &sequent,
                    &options,
                    &partition,
                    &settings.search,
                    limits,
                    stop,
                )?;
                let found = match &interpolation.interpolant {
                    Some(found) => Some(Found {
                        formula: found.formula().to_string(),
                        first: serde_json::from_str(&write_statement(&found.first, logic)?)
                            .map_err(|e| json_error(&e))?,
                        second: serde_json::from_str(&write_statement(&found.second, logic)?)
                            .map_err(|e| json_error(&e))?,
                    }),
                    None => None,
                };
                let outcome = serde_json::from_str(&written(&interpolation.outcome)?)
                    .map_err(|e| json_error(&e))?;
                return Ok(Produced {
                    value: written(&Interpolated {
                        logic,
                        partition: interpolation.partition.clone(),
                        outcome,
                        interpolant: found,
                    })?,
                    limit: limit_of(&interpolation.outcome),
                });
            }
            "ordinary.half" => {
                let read = interpolated(call.input(0), limits)?;
                let p: Half = parameters(call)?;
                let found = read.interpolant.ok_or_else(|| Error::InvalidOption {
                    key: "which",
                    message: "the sequent has no interpolant to take a half of".to_owned(),
                })?;
                let half = match p.which.as_deref() {
                    None | Some("first") => found.first,
                    Some("second") => found.second,
                    Some(other) => {
                        return Err(Error::UnknownName {
                            what: "half",
                            name: other.into(),
                            known: &["first", "second"],
                        });
                    }
                };
                let half = written(&half)?;
                statement(&half, limits)?;
                half
            }
            "ordinary.embed" => {
                let (sequent, logic) = statement(call.input(0), limits)?;
                let p: Embed = parameters(call)?;
                let translation = p.translation.unwrap_or(Translation::default_for(logic));
                let image = ordinary::translate(&sequent, logic, translation, limits)?;
                super::linear::write_statement(image.sequent(), image.mode())?
            }
            "ordinary.term" => {
                let input = input(call, limits)?;
                let derivation = derivation_of(input, false, call, stop)?;
                let term = derivation.term(limits, &mut *stop)?;
                term_value(&term, settings, stop)?
            }
            #[cfg(feature = "svg")]
            "ordinary.draw" => {
                let input = input(call, limits)?;
                let svg = match input {
                    Input::Outcome(outcome) if outcome.countermodel.is_some() => {
                        let countermodel = outcome
                            .countermodel
                            .as_ref()
                            .unwrap_or_else(|| unreachable!("the guard found a countermodel"));
                        crate::export::svg::countermodel(
                            countermodel,
                            outcome.logic,
                            &settings.styles.svg,
                            limits,
                            stop,
                        )?
                    }
                    input => {
                        let derivation = derivation_of(input, false, call, stop)?;
                        let mut out = String::new();
                        crate::export::svg::write(
                            &derivation,
                            &settings.styles.svg,
                            &mut out,
                            stop,
                        )?;
                        out
                    }
                };
                written(&super::shared::Drawing { svg })?
            }
            "ordinary.export" => {
                let input = input(call, limits)?;
                let derivation = derivation_of(input, false, call, stop)?;
                let p: Format = parameters(call)?;
                let format = p.format.unwrap_or_else(|| "text".to_owned());
                let styles = &settings.styles;
                let mut out = String::new();
                match format.as_str() {
                    "text" => derivation.write_text(&styles.text, &mut out, stop)?,
                    #[cfg(feature = "latex")]
                    "latex" => {
                        crate::export::latex::write(&derivation, &styles.latex, &mut out, stop)?
                    }
                    #[cfg(feature = "typst")]
                    "typst" => {
                        crate::export::typst::write(&derivation, &styles.typst, &mut out, stop)?
                    }
                    other => {
                        return Err(Error::UnknownName {
                            what: "format",
                            name: other.into(),
                            known: FORMATS,
                        });
                    }
                }
                written(&Text { format, text: out })?
            }
            #[cfg(feature = "rocq")]
            "ordinary.certificate" => {
                let input = input(call, limits)?;
                let mut out = String::new();
                match input {
                    Input::Outcome(outcome) if outcome.countermodel.is_some() => {
                        let countermodel = outcome
                            .countermodel
                            .as_ref()
                            .unwrap_or_else(|| unreachable!("the guard found a countermodel"));
                        crate::export::rocq::write_countermodel(
                            countermodel,
                            outcome.logic,
                            &settings.styles.rocq,
                            limits,
                            &mut out,
                            stop,
                        )?;
                    }
                    input => {
                        let derivation = derivation_of(input, true, call, stop)?;
                        crate::export::rocq::write(
                            &derivation,
                            &settings.styles.rocq,
                            &mut out,
                            stop,
                        )?;
                    }
                }
                written(&super::shared::Certificate { text: out })?
            }
            // The copy: the input as it was given.
            _ => {
                statement(call.input(0), limits)?;
                call.input(0).to_owned()
            }
        };
        Ok(Produced::done(value))
    }

    fn check(
        &self,
        call: &Call<'_>,
        output: &str,
        ask: &mut super::Ask<'_>,
        stop: &mut dyn FnMut(Progress) -> bool,
    ) -> Result<Check, Error> {
        let promise = call.operation.promise;
        let limits = &call.settings.limits;
        match promise.name {
            "decides" => decides(output, limits, stop),
            "proves" => {
                let derivation: Derivation = document(output, limits)?;
                let part = checked("derivation", derivation.check(limits, stop))?;
                Ok(Check::whole(promise, part.status))
            }
            "same_verdict" if call.operation.name == "ordinary.embed" => {
                let sequent = verdict_of(&ask("ordinary.decide", &[call.input(0)], "")?)?;
                let image = verdict_of(&ask("linear.prove", &[output], "")?)?;
                Ok(same_verdict(
                    promise,
                    ["sequent", "image"],
                    [sequent, image],
                ))
            }
            "same_verdict" => {
                let sequent = verdict_of(&ask("ordinary.decide", &[call.input(0)], "")?)?;
                let translation = verdict_of(&ask("ordinary.decide", &[output], "")?)?;
                Ok(same_verdict(
                    promise,
                    ["sequent", "translation"],
                    [sequent, translation],
                ))
            }
            "interpolates" => {
                let read = interpolated(output, limits)?;
                let Some(found) = read.interpolant else {
                    return Ok(Check::whole(promise, Status::None));
                };
                let (first, second) = (written(&found.first)?, written(&found.second)?);
                let (one, _) = statement(&first, limits)?;
                let (two, _) = statement(&second, limits)?;
                // `interpolated` read both halves with the formula in place.
                let (input, _) = statement(call.input(0), limits)?;
                let halves = Part {
                    name: "halves".to_owned(),
                    status: if halves_fit(&input, &read.partition, &one, &two) {
                        Status::Held
                    } else {
                        Status::Failed
                    },
                    verdict: None,
                };
                let interpolant = atom_names(&one, &one.right()[one.right().len() - 1..]);
                let mut rest_one = one.left().to_vec();
                rest_one.extend_from_slice(&one.right()[..one.right().len() - 1]);
                let mut rest_two = two.left()[1..].to_vec();
                rest_two.extend_from_slice(two.right());
                let (rest_one, rest_two) =
                    (atom_names(&one, &rest_one), atom_names(&two, &rest_two));
                let shared = interpolant
                    .iter()
                    .all(|atom| rest_one.contains(atom) && rest_two.contains(atom));
                let atoms = Part {
                    name: "atoms".to_owned(),
                    status: if shared { Status::Held } else { Status::Failed },
                    verdict: None,
                };
                let first = verdict_of(&ask("ordinary.decide", &[&first], "")?)?;
                let second = verdict_of(&ask("ordinary.decide", &[&second], "")?)?;
                Ok(Check::of(
                    promise,
                    vec![
                        halves,
                        atoms,
                        part("first", first, Verdict::Valid),
                        part("second", second, Verdict::Valid),
                    ],
                ))
            }
            "certifies" => Ok(Check::whole(promise, Status::Outside)),
            _ => Ok(Check::whole(promise, Status::None)),
        }
    }
}

/// Checks a decision's evidence.
fn decides(
    output: &str,
    limits: &Limits,
    stop: &mut dyn FnMut(Progress) -> bool,
) -> Result<Check, Error> {
    let promise = &DECIDES;
    let keys: OutcomeKeys = object(output, "ordinary outcome")?;
    if keys.clausal_proof.is_some() && keys.verdict == "valid" {
        // The SAT solver's proof was checked when it was made, and the
        // outcome keeps its counts alone.
        return Ok(Check::whole(promise, Status::Recorded));
    }
    let outcome: Outcome = document(output, limits)?;
    let status = match &outcome.verdict {
        ordinary::Verdict::Valid(ordinary::Evidence::Derivation(derivation)) => {
            checked("derivation", derivation.check(limits, stop))?.status
        }
        ordinary::Verdict::Valid(_) => Status::Recorded,
        ordinary::Verdict::NotValid => match &outcome.countermodel {
            Some(countermodel) => {
                checked(
                    "countermodel",
                    countermodel.check(outcome.logic, limits, stop),
                )?
                .status
            }
            None => Status::Recorded,
        },
        ordinary::Verdict::Unknown(_) => Status::None,
    };
    Ok(Check::whole(promise, status))
}

/// Returns a lambda-term's value, written with the settings' `lambda`.
pub(super) fn term_value(
    term: &crate::lambda::Term,
    settings: &crate::Settings,
    stop: &mut dyn FnMut(Progress) -> bool,
) -> Result<String, Error> {
    let (options, limits) = (&settings.lambda, &settings.limits);
    let (mut text, mut judgement) = (String::new(), String::new());
    term.write(&mut text, options, limits, &mut *stop)?;
    term.write_judgement(&mut judgement, options, limits, &mut *stop)?;
    written(&Term { text, judgement })
}

/// Returns the error of a value this module wrote and could not read back.
fn json_error(error: &serde_json::Error) -> Error {
    Error::Json {
        form: "value",
        message: error.to_string(),
    }
}