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

//! The negative translations of classical logic into intuitionistic and
//! minimal logic, each with its claim checked.
//!
//! A negative translation turns a classical sequent into one that is
//! valid in a weaker logic exactly when the first is valid classically:
//!
//! | translation | what it does | into |
//! |---|---|---|
//! | [`Kolmogorov`](Translation::Kolmogorov) | `¬¬` before every subformula | minimal logic |
//! | [`GodelGentzen`](Translation::GodelGentzen) | `¬¬` before every atom, `A ∨ B` as `¬(¬A ∧ ¬B)` | minimal logic |
//! | [`Kuroda`](Translation::Kuroda) | `¬¬` before every formula of the sequent | intuitionistic logic |
//! | [`Glivenko`](Translation::Glivenko) | `¬¬` before the formula right of `⊢` | intuitionistic logic |
//!
//! In propositional logic Kuroda's translation puts `¬¬` before each
//! formula, since it adds more only after a `∀`; Glivenko's theorem says
//! that `Γ ⊢ A` is classically valid exactly when `Γ ⊢ ¬¬A` is
//! intuitionistically valid. Several formulas right of `⊢` are read as
//! their disjunction, which is translated.
//!
//! [`translate`] returns the translated sequent. [`compare`] decides the
//! sequent classically and its translation in the target logic, and
//! returns both outcomes with their evidence; verdicts that differ are
//! [`Error::ClaimFails`], a defect, never an answer.
//!
#![cfg_attr(feature = "parse", doc = "```")]
#![cfg_attr(not(feature = "parse"), doc = "```ignore")]
//! use deduxit::ordinary::negative::{self, Agreement, Options, Translation};
//! use deduxit::ordinary::Sequent;
//!
//! let sequent: Sequent = "a \\/ ~a".parse()?;
//! let options = Options::default().with_translation(Translation::GodelGentzen);
//! let translated = negative::translate(&sequent, &options, &deduxit::Limits::default())?;
//! assert_eq!(translated.to_string(), "⊢ ¬(¬¬¬a ∧ ¬¬¬¬a)");
//! let comparison = negative::compare_default(&sequent, &options)?;
//! assert_eq!(comparison.agreement, Agreement::Valid);
//! # Ok::<(), deduxit::Error>(())
//! ```

use super::{Formulas, Logic, Node, NodeId, Outcome, Sequent, Verdict, decide_within};
use crate::Error;
use crate::limits::{Limits, Progress};
use crate::linear::search;
use std::fmt::{Display, Formatter, Result as FmtResult};

/// A negative translation of classical logic, by its name.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(rename_all = "kebab-case"))]
pub enum Translation {
    /// Kolmogorov's: `¬¬` before every subformula. Into minimal logic.
    #[default]
    Kolmogorov,
    /// Gödel and Gentzen's: `¬¬` before every atom, and `A ∨ B` as
    /// `¬(¬A ∧ ¬B)`. Into minimal logic.
    GodelGentzen,
    /// Kuroda's: `¬¬` before every formula of the sequent. Into
    /// intuitionistic logic.
    Kuroda,
    /// Glivenko's theorem: `¬¬` before the formula right of `⊢`, the
    /// hypotheses as they are. Into intuitionistic logic.
    Glivenko,
}

impl Translation {
    /// Every translation, in the order of [`NAMES`](Self::NAMES).
    pub const ALL: &'static [Self] = &[
        Self::Kolmogorov,
        Self::GodelGentzen,
        Self::Kuroda,
        Self::Glivenko,
    ];

    /// The translations' names, which [`name`](Self::name) writes and
    /// `FromStr` reads.
    pub const NAMES: &'static [&'static str] =
        &["kolmogorov", "godel-gentzen", "kuroda", "glivenko"];

    /// Returns the translation's name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Kolmogorov => "kolmogorov",
            Self::GodelGentzen => "godel-gentzen",
            Self::Kuroda => "kuroda",
            Self::Glivenko => "glivenko",
        }
    }

    /// Returns what a sentence calls it: "Kolmogorov's translation".
    pub const fn title(self) -> &'static str {
        match self {
            Self::Kolmogorov => "Kolmogorov's translation",
            Self::GodelGentzen => "Gödel and Gentzen's translation",
            Self::Kuroda => "Kuroda's translation",
            Self::Glivenko => "Glivenko's translation",
        }
    }

    /// Returns the weakest logic its claim holds in: minimal logic for
    /// Kolmogorov's and Gödel and Gentzen's, intuitionistic logic for
    /// Kuroda's and Glivenko's.
    pub const fn target(self) -> Logic {
        match self {
            Self::Kolmogorov | Self::GodelGentzen => Logic::Minimal,
            Self::Kuroda | Self::Glivenko => Logic::Intuitionistic,
        }
    }

    /// Returns whether its claim holds in `logic`: in its
    /// [`target`](Self::target), and in intuitionistic logic, which
    /// proves what minimal logic proves and no classical sequent that
    /// minimal logic does not prove of these translations.
    pub const fn reaches(self, logic: Logic) -> bool {
        match logic {
            Logic::Classical => false,
            Logic::Intuitionistic => true,
            Logic::Minimal => matches!(self.target(), Logic::Minimal),
        }
    }
}

impl Display for Translation {
    /// Writes the translation's [`name`](Self::name).
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str(self.name())
    }
}

impl std::str::FromStr for Translation {
    type Err = Error;

    /// Reads a translation's [`name`](Self::name), one of
    /// [`NAMES`](Self::NAMES).
    fn from_str(name: &str) -> Result<Self, Error> {
        Self::ALL
            .iter()
            .copied()
            .find(|t| t.name() == name)
            .ok_or_else(|| Error::UnknownName {
                what: "negative translation",
                name: name.into(),
                known: Self::NAMES,
            })
    }
}

/// Which negative translation to take, and into which logic.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(default, deny_unknown_fields))]
pub struct Options {
    /// The translation.
    pub translation: Translation,
    /// The logic translated into, or `None` for the translation's own
    /// [`target`](Translation::target).
    pub target: Option<Logic>,
}

impl Options {
    /// Returns the options with the translation given.
    #[must_use]
    pub const fn with_translation(self, translation: Translation) -> Self {
        Self {
            translation,
            ..self
        }
    }

    /// Returns the options with the target logic given, `None` for the
    /// translation's own.
    #[must_use]
    pub const fn with_target(self, target: Option<Logic>) -> Self {
        Self { target, ..self }
    }

    /// Returns the logic these options translate into.
    pub const fn target(&self) -> Logic {
        match self.target {
            Some(logic) => logic,
            None => self.translation.target(),
        }
    }
}

/// How a node of a classical formula is translated: its connective kept
/// or a disjunction turned into a negated conjunction, and how many `¬`
/// go before the result.
#[derive(Clone, Copy)]
struct Row {
    /// What becomes of the connective.
    shape: Shape,
    /// The `¬` written before the translated node.
    negations: u8,
}

/// What a translation makes of a node's connective.
#[derive(Clone, Copy)]
enum Shape {
    /// The same connective over the translated operands.
    Same,
    /// `A ∨ B` as `¬(¬A ∧ ¬B)` over the translated operands.
    DeMorgan,
}

/// Returns the row of `node` in the table of `translation`. Quantifiers
/// get their rows here: Kuroda's translation puts `¬¬` after every `∀`,
/// and Gödel and Gentzen's writes `∃x A` as `¬∀x ¬A`.
const fn row(translation: Translation, node: Node) -> Row {
    const KEEP: Row = Row {
        shape: Shape::Same,
        negations: 0,
    };
    const DOUBLE: Row = Row {
        shape: Shape::Same,
        negations: 2,
    };
    match (translation, node) {
        (Translation::Kolmogorov, _) => DOUBLE,
        (Translation::GodelGentzen, Node::Atom(_)) => DOUBLE,
        (Translation::GodelGentzen, Node::Or(..)) => Row {
            shape: Shape::DeMorgan,
            negations: 0,
        },
        (
            Translation::GodelGentzen,
            Node::True
            | Node::False
            | Node::Not(_)
            | Node::And(..)
            | Node::Implies(..)
            | Node::Iff(..),
        )
        | (Translation::Kuroda | Translation::Glivenko, _) => KEEP,
    }
}

/// Returns the `¬` a translation puts before each translated hypothesis
/// and before the translated formula right of `⊢`.
const fn sides(translation: Translation) -> (u8, u8) {
    match translation {
        Translation::Kolmogorov | Translation::GodelGentzen => (0, 0),
        Translation::Kuroda => (2, 2),
        Translation::Glivenko => (0, 2),
    }
}

/// Returns the sequent translated by `options`, every formula built from
/// its table over the arena, its subformulas first, without recursion.
/// Several formulas right of `⊢` are translated as their disjunction, the
/// first leftmost; none stays none, which intuitionistic and minimal logic
/// read as `⊥`.
///
/// # Errors
///
/// [`Error::NegativeTarget`] for a target logic where the translation's
/// claim does not hold; [`Refusal::Occurrences`](crate::Refusal) for a
/// translation that unfolds past `limits.occurrences`;
/// [`Refusal::Index`](crate::Refusal) when the arena is full.
pub fn translate(sequent: &Sequent, options: &Options, limits: &Limits) -> Result<Sequent, Error> {
    let translation = options.translation;
    let target = options.target();
    if !translation.reaches(target) {
        return Err(Error::NegativeTarget {
            translation,
            logic: target,
        });
    }
    // The formulas right of `⊢` as one disjunction, in a copy of the arena.
    let mut source = sequent.formulas().clone();
    let mut goal = None;
    for &d in sequent.right() {
        goal = Some(match goal {
            None => d,
            Some(g) => source.add(Node::Or(g, d))?,
        });
    }
    // The nodes the sequent reaches: every operand precedes its formula.
    let mut reached = vec![false; source.len()];
    for &root in sequent.left().iter().chain(&goal) {
        reached[root.index()] = true;
    }
    for k in (0..source.len()).rev() {
        if reached[k] {
            let (a, b) = source.nodes[k].operands();
            for id in [a, b].into_iter().flatten() {
                reached[id.index()] = true;
            }
        }
    }
    let mut out = Formulas::default();
    // A node the sequent does not reach keeps a translation it never reads.
    let mut image: Vec<NodeId> = vec![NodeId(0); source.len()];
    for (k, &node) in source.nodes.iter().enumerate() {
        if !reached[k] {
            continue;
        }
        // An operand is reached when its formula is, and precedes it.
        let of = |id: NodeId| image[id.index()];
        let Row { shape, negations } = row(translation, node);
        let mut id = match (shape, node) {
            (Shape::DeMorgan, Node::Or(a, b)) => {
                let na = out.add(Node::Not(of(a)))?;
                let nb = out.add(Node::Not(of(b)))?;
                let both = out.add(Node::And(na, nb))?;
                out.add(Node::Not(both))?
            }
            (_, Node::Atom(a)) => out.atom(source.atom_name(a))?,
            (_, Node::True) => out.add(Node::True)?,
            (_, Node::False) => out.add(Node::False)?,
            (_, Node::Not(a)) => out.add(Node::Not(of(a)))?,
            (_, Node::And(a, b)) => out.add(Node::And(of(a), of(b)))?,
            (_, Node::Or(a, b)) => out.add(Node::Or(of(a), of(b)))?,
            (_, Node::Implies(a, b)) => out.add(Node::Implies(of(a), of(b)))?,
            (_, Node::Iff(a, b)) => out.add(Node::Iff(of(a), of(b)))?,
        };
        id = negated(&mut out, id, negations)?;
        image[k] = id;
    }
    let (hypotheses, goals) = sides(translation);
    let mut left = Vec::with_capacity(sequent.left().len());
    for &h in sequent.left() {
        let id = image[h.index()];
        left.push(negated(&mut out, id, hypotheses)?);
    }
    let right = match goal {
        Some(g) => {
            let id = image[g.index()];
            vec![negated(&mut out, id, goals)?]
        }
        None => Vec::new(),
    };
    super::bound(&out, &left, &right, limits)?;
    Sequent::new(out, left, right)
}

/// Returns `id` under `count` negations, added to the arena.
fn negated(out: &mut Formulas, mut id: NodeId, count: u8) -> Result<NodeId, Error> {
    for _ in 0..count {
        id = out.add(Node::Not(id))?;
    }
    Ok(id)
}

/// What the two verdicts of a [`Comparison`] are, which agree.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "serialize", serde(rename_all = "snake_case"))]
pub enum Agreement {
    /// The sequent is valid classically and its translation in the target
    /// logic.
    Valid,
    /// Neither is valid.
    NotValid,
    /// One of the two searches stopped before it decided, so whether they
    /// agree is not known.
    Unknown,
}

/// A classical sequent, its negative translation and both decided: what
/// [`compare`] returns.
///
/// # JSON
///
/// With the feature `serialize` a comparison is the document
/// `{"version": 16, "deduxit": "0.5.0", "translation": "kolmogorov",
/// "target": "minimal", "agreement": "valid", "sequent": {…},
/// "translated": {…}, "classical": {…}, "decided": {…}}`
/// ([`wire`](crate::wire)), written only: the two sequents in the form of
/// a [`Sequent`] and the two outcomes in the form of an
/// [`Outcome`], each without a `version`.
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct Comparison {
    /// The translation taken.
    pub translation: Translation,
    /// The classical sequent.
    pub sequent: Sequent,
    /// Its translation.
    pub translated: Sequent,
    /// The classical sequent decided in classical logic.
    pub classical: Outcome,
    /// The translation decided in the target logic, which is its `logic`.
    pub decided: Outcome,
    /// The verdicts, which agree.
    pub agreement: Agreement,
}

/// Translates `sequent` by `options`, decides it in classical logic and
/// its translation in the target logic, each by that logic's default
/// method under `search`, `limits` and `stop`, and compares the verdicts.
///
/// # Errors
///
/// [`translate`]'s and [`decide_within`]'s errors, and
/// [`Error::ClaimFails`] where one verdict is valid and the other not
/// valid, a defect.
pub fn compare(
    sequent: &Sequent,
    options: &Options,
    search: &search::Options,
    limits: &Limits,
    mut stop: impl FnMut(Progress) -> bool,
) -> Result<Comparison, Error> {
    let translated = translate(sequent, options, limits)?;
    let classical = decide_within(
        sequent,
        &super::Options::default().with_logic(Logic::Classical),
        search,
        limits,
        &mut stop,
    )?;
    let target = options.target();
    let decided = decide_within(
        &translated,
        &super::Options::default().with_logic(target),
        search,
        limits,
        &mut stop,
    )?;
    let fails = |was: &str, is: &str| Error::ClaimFails {
        claim: options.translation.title().to_owned(),
        reason: format!(
            "the sequent is {was} in classical logic and its translation {is} in {target} logic"
        ),
    };
    let agreement = match (&classical.verdict, &decided.verdict) {
        (Verdict::Valid(_), Verdict::Valid(_)) => Agreement::Valid,
        (Verdict::NotValid, Verdict::NotValid) => Agreement::NotValid,
        (Verdict::Unknown(_), _) | (_, Verdict::Unknown(_)) => Agreement::Unknown,
        (Verdict::Valid(_), Verdict::NotValid) => return Err(fails("valid", "not valid")),
        (Verdict::NotValid, Verdict::Valid(_)) => return Err(fails("not valid", "valid")),
    };
    Ok(Comparison {
        translation: options.translation,
        sequent: sequent.clone(),
        translated,
        classical,
        decided,
        agreement,
    })
}

/// Compares as [`compare`] does within the default [`Limits`] and with the
/// linear search's default options.
///
/// # Errors
///
/// As [`compare`].
pub fn compare_default(sequent: &Sequent, options: &Options) -> Result<Comparison, Error> {
    compare(
        sequent,
        options,
        &search::Options::default(),
        &Limits::default(),
        |_| false,
    )
}

#[cfg(all(test, feature = "parse"))]
mod tests {
    use super::*;

    /// Each translation writes the formulas its table says, and keeps
    /// the hypotheses where its table keeps them.
    #[test]
    fn each_table_writes_its_formulas() {
        let sequent: Sequent = "a, a -> b |- b \\/ c".parse().unwrap();
        let limits = Limits::default();
        let written: Vec<String> = Translation::ALL
            .iter()
            .map(|&t| {
                let options = Options::default().with_translation(t);
                translate(&sequent, &options, &limits).unwrap().to_string()
            })
            .collect();
        assert_eq!(
            written,
            [
                "¬¬a, ¬¬(¬¬a → ¬¬b) ⊢ ¬¬(¬¬b ∨ ¬¬c)",
                "¬¬a, ¬¬a → ¬¬b ⊢ ¬(¬¬¬b ∧ ¬¬¬c)",
                "¬¬a, ¬¬(a → b) ⊢ ¬¬(b ∨ c)",
                "a, a → b ⊢ ¬¬(b ∨ c)",
            ]
        );
    }

    /// Every translation keeps the classical verdict of sequents that
    /// minimal or intuitionistic logic alone would answer otherwise, and
    /// of sequents that are not valid at all: `compare` decides both and
    /// refuses a disagreement.
    #[test]
    fn every_translation_keeps_the_verdict() {
        let cases = [
            ("a \\/ ~a", true),
            ("~~a -> a", true),
            ("((a -> b) -> a) -> a", true),
            ("false -> a", true),
            ("a -> b |- ~a \\/ b", true),
            ("~(a /\\ b) |- ~a \\/ ~b", true),
            ("a <-> b, b |- a", true),
            ("a |- b, a", true),
            ("a \\/ b |- a", false),
            ("a -> b", false),
            ("~a, b |-", false),
            ("true |- a <-> ~a", false),
        ];
        for (text, valid) in cases {
            let sequent: Sequent = text.parse().unwrap();
            for &t in Translation::ALL {
                let options = Options::default().with_translation(t);
                let comparison = compare_default(&sequent, &options)
                    .unwrap_or_else(|e| panic!("{text} by {t}: {e}"));
                let expected = if valid {
                    Agreement::Valid
                } else {
                    Agreement::NotValid
                };
                assert_eq!(comparison.agreement, expected, "{text} by {t}");
                assert_eq!(comparison.decided.logic, t.target());
            }
        }
    }

    /// Generated sequents keep their verdict under every translation.
    #[test]
    fn generated_sequents_keep_their_verdicts() {
        use crate::linear::search::generate::Rng;
        let mut rng = Rng::new(63);
        for _ in 0..200 {
            let sequent = super::super::tests::generated_sequent(&mut rng);
            for &t in Translation::ALL {
                let options = Options::default().with_translation(t);
                if let Err(error) = compare_default(&sequent, &options) {
                    panic!("{sequent} by {t}: {error}");
                }
            }
        }
    }

    /// A translation is refused into a logic where its claim does not
    /// hold, and past the occurrence bound.
    #[test]
    fn targets_and_bounds_are_kept() {
        let sequent: Sequent = "a -> a".parse().unwrap();
        let limits = Limits::default();
        for (t, logic, ok) in [
            (Translation::Kuroda, Logic::Minimal, false),
            (Translation::Glivenko, Logic::Minimal, false),
            (Translation::Kolmogorov, Logic::Classical, false),
            (Translation::Kolmogorov, Logic::Intuitionistic, true),
        ] {
            let options = Options::default()
                .with_translation(t)
                .with_target(Some(logic));
            assert_eq!(
                translate(&sequent, &options, &limits).is_ok(),
                ok,
                "{t} into {logic}"
            );
        }
        let small = Limits::default().with_occurrences(Some(5));
        assert!(matches!(
            translate(&sequent, &Options::default(), &small),
            Err(Error::Refused(crate::Refusal::Occurrences { .. }))
        ));
    }
}