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

//! A rule applied forward. Its term is evaluated over the sequents in its
//! holes, step by step, to the sequent it derives, or to nothing where a
//! step's side condition fails.
//!
//! A step derives what the checker derives of that rule. A member a step
//! takes may be absent from a premise in two cases. A `⊤` above may let
//! the premise absorb anything (the `any` flag of a sequent, here `top`).
//! Or, in affine mode, the member is weakened in below the step.
//!
//! Given a node arena, the same evaluation writes the proof term: a node
//! per step and a `Weaken` per member weakened in. So the proof the search
//! returns is the one it judged.

use super::rules::{Partner, Shape, Step};
use crate::linear::occurrences::{Member, OccId};
use crate::linear::proofs::{Node, NodeId};

/// A sequent as the search derives it: the linear zone sorted with its
/// repeats, the unrestricted zone sorted, whether a `⊤` above lets it
/// absorb any further linear member, how many members are in output
/// position, and the node proving it while a proof is written.
///
/// The linear zone holds the formulas used exactly once; the unrestricted
/// zone holds the formulas under `?`, which may be used any number of
/// times.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct Zone {
    /// The linear zone, sorted, with repeats.
    pub(super) gamma: Vec<OccId>,
    /// The unrestricted zone, sorted, without repeats.
    pub(super) theta: Vec<OccId>,
    /// Whether any further linear member is absorbed.
    pub(super) top: bool,
    /// The members of `gamma` in output position.
    ///
    /// It never exceeds the occurrences that the zones in memory hold
    /// together, at four bytes each. So in a `u64` it stays below 2⁶² on
    /// any target, and no sum of two wraps.
    pub(super) outputs: u64,
    /// The node proving the sequent, while a proof is written.
    pub(super) node: Option<NodeId>,
}

/// Inserts into a sorted list, keeping repeats.
pub(super) fn insert(list: &mut Vec<OccId>, o: OccId) {
    let at = list.partition_point(|&x| x <= o);
    list.insert(at, o);
}

/// Removes one copy from a sorted list, and returns whether there was
/// one.
fn remove(list: &mut Vec<OccId>, o: OccId) -> bool {
    match list.binary_search(&o) {
        Ok(at) => {
            list.remove(at);
            true
        }
        Err(_) => false,
    }
}

/// Merges `from` into `into`, both sorted, keeping repeats: the sum of
/// two multisets.
fn sum(into: &mut Vec<OccId>, from: &[OccId]) {
    if from.is_empty() {
        return;
    }
    let mut merged = Vec::with_capacity(into.len() + from.len());
    let (mut i, mut j) = (0, 0);
    while i < into.len() && j < from.len() {
        if into[i] <= from[j] {
            merged.push(into[i]);
            i += 1;
        } else {
            merged.push(from[j]);
            j += 1;
        }
    }
    merged.extend_from_slice(&into[i..]);
    merged.extend_from_slice(&from[j..]);
    *into = merged;
}

/// Merges `from` into `into`, both sorted sets: their union.
fn union(into: &mut Vec<OccId>, from: &[OccId]) {
    for &o in from {
        if let Err(at) = into.binary_search(&o) {
            into.insert(at, o);
        }
    }
}

/// Returns the members of `of` beyond those of `within`, with repeats:
/// what `within` lacks to hold `of`. Both are sorted multisets.
pub(super) fn excess(of: &[OccId], within: &[OccId]) -> Vec<OccId> {
    let mut out = Vec::new();
    let mut j = 0;
    for &o in of {
        while j < within.len() && within[j] < o {
            j += 1;
        }
        if j < within.len() && within[j] == o {
            j += 1;
        } else {
            out.push(o);
        }
    }
    out
}

/// Returns whether one sorted multiset lies within another.
pub(super) fn within(small: &[OccId], large: &[OccId]) -> bool {
    let mut j = 0;
    for &o in small {
        while j < large.len() && large[j] < o {
            j += 1;
        }
        if j == large.len() || large[j] != o {
            return false;
        }
        j += 1;
    }
    true
}

/// The rules of the calculus the search derives in, read forward.
pub(super) struct Calculus<'s, 'a> {
    /// The goal's shape: the forest, the reading and the mode.
    pub(super) shape: &'s Shape<'a>,
}

impl Calculus<'_, '_> {
    /// Whether a zone is a sequent of the mode. Under a reading, it has
    /// one member in output position, or none where `⊤` absorbs one.
    fn shaped(&self, z: &Zone) -> bool {
        self.shape.reading.is_none() || z.outputs == 1 || (z.outputs == 0 && z.top)
    }

    /// Adds a member to the linear zone.
    fn add(&self, z: &mut Zone, o: OccId) {
        insert(&mut z.gamma, o);
        z.outputs += u64::from(self.shape.output(o));
    }

    /// Weakens a member in below a premise's node, writing the `Weaken`
    /// node while a proof is written. Returns false outside affine mode,
    /// and for an output under a reading.
    fn weaken(&self, z: &mut Zone, o: OccId, nodes: &mut Option<&mut Vec<Node>>) -> bool {
        if !self.shape.affine || self.shape.output(o) {
            return false;
        }
        if let (Some(nodes), Some(n)) = (nodes.as_deref_mut(), z.node) {
            z.node = Some(push(nodes, Node::Weaken(Member::from(o), n)));
        }
        true
    }

    /// Weakens a member into a zone, writing the `Weaken` node. Returns
    /// false outside affine mode, and for an output under a reading.
    pub(super) fn weaken_in(&self, z: &mut Zone, o: OccId, nodes: &mut Vec<Node>) -> bool {
        if !self.weaken(z, o, &mut Some(nodes)) {
            return false;
        }
        self.add(z, o);
        true
    }

    /// Takes a principal formula's subformula out of a premise's linear
    /// zone, and returns whether it could.
    ///
    /// The subformula is there, or the premise absorbs it, or it is
    /// weakened in. Under a reading, a premise absorbs an output only when
    /// it has none.
    fn take(&self, z: &mut Zone, o: OccId, nodes: &mut Option<&mut Vec<Node>>) -> bool {
        if remove(&mut z.gamma, o) {
            z.outputs -= u64::from(self.shape.output(o));
            return true;
        }
        if z.top {
            return !(self.shape.output(o) && z.outputs > 0);
        }
        self.weaken(z, o, nodes)
    }

    /// Joins the two premises of a `&`, their subformulas taken. Returns
    /// the joined zone, and the two premises' nodes while a proof is
    /// written, or `None` when they do not join.
    ///
    /// They join when the zones are equal, when an absorbing zone lies
    /// within an exact one (the exact one stays), or when both absorb (the
    /// larger count of each member). In affine mode, the exact sides are
    /// otherwise weakened up to the larger count of each member.
    fn join(
        &self,
        mut l: Zone,
        mut r: Zone,
        nodes: &mut Option<&mut Vec<Node>>,
    ) -> Option<(Zone, Option<(NodeId, NodeId)>)> {
        let mut most = l.gamma.clone();
        sum(&mut most, &excess(&r.gamma, &l.gamma));
        let gamma = match (l.top, r.top) {
            (true, true) => most,
            (false, false) if l.gamma == r.gamma => most,
            (true, false) if within(&l.gamma, &r.gamma) => most,
            (false, true) if within(&r.gamma, &l.gamma) => most,
            _ if self.shape.affine => {
                for side in [&mut l, &mut r] {
                    if !side.top {
                        for o in excess(&most, &side.gamma) {
                            if !self.weaken(side, o, nodes) {
                                return None;
                            }
                        }
                    }
                }
                most
            }
            _ => return None,
        };
        let mut theta = l.theta;
        union(&mut theta, &r.theta);
        let outputs = gamma.iter().map(|&o| u64::from(self.shape.output(o))).sum();
        let zone = Zone {
            gamma,
            theta,
            top: l.top && r.top,
            outputs,
            node: None,
        };
        Some((zone, l.node.zip(r.node)))
    }

    /// Evaluates a rule's steps over the premises and the partners chosen.
    /// Returns the sequent derived, or `None` where a step's condition
    /// fails.
    ///
    /// `premise` gives the zone of a hole, and `partner` the partner of a
    /// slot. It adds to `units` one for each step and each linear member of
    /// the zone the step yields, which the step copied or merged. With
    /// `nodes`, it writes the proof term there, the premises' zones
    /// carrying their nodes.
    pub(super) fn derive(
        &self,
        steps: &[Step],
        premise: &mut dyn FnMut(u32, &mut Zone),
        partner: &dyn Fn(u32) -> Partner,
        mut nodes: Option<&mut Vec<Node>>,
        stack: &mut Vec<Zone>,
        units: &mut u64,
    ) -> Option<Zone> {
        let forest = self.shape.forest;
        stack.clear();
        for &step in steps {
            let z = match step {
                Step::Axiom(p, slot) => {
                    let Partner { x, theta } = partner(slot);
                    let mut z = Zone::default();
                    self.add(&mut z, p);
                    let ax = nodes
                        .as_deref_mut()
                        .map(|n| push(n, Node::Ax(Member::from(p), Member::from(x))));
                    if theta {
                        z.theta.push(x);
                        z.node = ax
                            .zip(nodes.as_deref_mut())
                            .map(|(a, n)| push(n, Node::Copy(Member::from(x), a)));
                    } else {
                        self.add(&mut z, x);
                        z.node = ax;
                    }
                    z
                }
                Step::One(o) => {
                    let mut z = Zone::default();
                    self.add(&mut z, o);
                    z.node = nodes
                        .as_deref_mut()
                        .map(|n| push(n, Node::One(Member::from(o))));
                    z
                }
                Step::Top(o) => {
                    let mut z = Zone::default();
                    self.add(&mut z, o);
                    z.top = true;
                    z.node = nodes
                        .as_deref_mut()
                        .map(|n| push(n, Node::Top(Member::from(o))));
                    z
                }
                Step::Hole(h) => {
                    let mut z = Zone::default();
                    premise(h, &mut z);
                    z
                }
                Step::Tensor(o) => {
                    let mut r = stack.pop()?;
                    let mut l = stack.pop()?;
                    let (a, b) = (forest.left(o)?, forest.right(o)?);
                    if !self.take(&mut l, a, &mut nodes) || !self.take(&mut r, b, &mut nodes) {
                        return None;
                    }
                    sum(&mut l.gamma, &r.gamma);
                    union(&mut l.theta, &r.theta);
                    l.top |= r.top;
                    l.outputs += r.outputs;
                    l.node = match (nodes.as_deref_mut(), l.node, r.node) {
                        (Some(n), Some(x), Some(y)) => {
                            Some(push(n, Node::Tensor(Member::from(o), x, y)))
                        }
                        _ => None,
                    };
                    self.add(&mut l, o);
                    l
                }
                Step::Plus(o, side) => {
                    let mut z = stack.pop()?;
                    let child = match side {
                        crate::linear::proofs::Branch::Left => forest.left(o)?,
                        crate::linear::proofs::Branch::Right => forest.right(o)?,
                    };
                    if !self.take(&mut z, child, &mut nodes) {
                        return None;
                    }
                    z.node = wrap(&mut nodes, z.node, |n| Node::Plus(Member::from(o), side, n));
                    self.add(&mut z, o);
                    z
                }
                Step::Bang(o) => {
                    let mut z = stack.pop()?;
                    if !self.take(&mut z, forest.left(o)?, &mut nodes) || !z.gamma.is_empty() {
                        return None;
                    }
                    z.top = false;
                    z.outputs = 0;
                    z.node = wrap(&mut nodes, z.node, |n| Node::Bang(Member::from(o), n));
                    self.add(&mut z, o);
                    z
                }
                Step::Par(o) => {
                    let mut z = stack.pop()?;
                    let (a, b) = (forest.left(o)?, forest.right(o)?);
                    if !self.take(&mut z, a, &mut nodes) || !self.take(&mut z, b, &mut nodes) {
                        return None;
                    }
                    z.node = wrap(&mut nodes, z.node, |n| Node::Par(Member::from(o), n));
                    self.add(&mut z, o);
                    z
                }
                Step::Bot(o) => {
                    let mut z = stack.pop()?;
                    z.node = wrap(&mut nodes, z.node, |n| Node::Bot(Member::from(o), n));
                    self.add(&mut z, o);
                    z
                }
                Step::With(o) => {
                    let mut r = stack.pop()?;
                    let mut l = stack.pop()?;
                    let (a, b) = (forest.left(o)?, forest.right(o)?);
                    if !self.take(&mut l, a, &mut nodes) || !self.take(&mut r, b, &mut nodes) {
                        return None;
                    }
                    let (mut z, premises) = self.join(l, r, &mut nodes)?;
                    z.node = match (nodes.as_deref_mut(), premises) {
                        (Some(n), Some((x, y))) => Some(push(n, Node::With(Member::from(o), x, y))),
                        _ => None,
                    };
                    self.add(&mut z, o);
                    z
                }
                Step::Quest(o) => {
                    let mut z = stack.pop()?;
                    let a = forest.left(o)?;
                    if let Ok(at) = z.theta.binary_search(&a) {
                        z.theta.remove(at);
                    }
                    z.node = wrap(&mut nodes, z.node, |n| Node::Quest(Member::from(o), n));
                    self.add(&mut z, o);
                    z
                }
                Step::Copy(o) => {
                    let mut z = stack.pop()?;
                    if !self.take(&mut z, o, &mut nodes) {
                        return None;
                    }
                    if let Err(at) = z.theta.binary_search(&o) {
                        z.theta.insert(at, o);
                    }
                    z.node = wrap(&mut nodes, z.node, |n| Node::Copy(Member::from(o), n));
                    z
                }
            };
            *units += 1 + z.gamma.len() as u64;
            if !self.shaped(&z) {
                return None;
            }
            stack.push(z);
        }
        let z = stack.pop()?;
        debug_assert!(stack.is_empty(), "a rule's term has one root");
        Some(z)
    }

    /// Returns the Mix of two sequents: the two side by side.
    pub(super) fn mix(&self, l: &Zone, r: &Zone, nodes: Option<&mut Vec<Node>>) -> Option<Zone> {
        let mut z = l.clone();
        sum(&mut z.gamma, &r.gamma);
        union(&mut z.theta, &r.theta);
        z.top |= r.top;
        z.outputs += r.outputs;
        z.node = match (nodes, l.node, r.node) {
            (Some(n), Some(a), Some(b)) => Some(push(n, Node::Mix(a, b))),
            _ => None,
        };
        Some(z)
    }
}

/// Pushes a node and returns its id.
fn push(nodes: &mut Vec<Node>, node: Node) -> NodeId {
    let id = NodeId::new(u32::try_from(nodes.len()).expect("a proof has fewer than 2³² nodes"));
    nodes.push(node);
    id
}

/// Wraps a premise's node in a unary rule's node, while a proof is written.
fn wrap(
    nodes: &mut Option<&mut Vec<Node>>,
    premise: Option<NodeId>,
    rule: impl FnOnce(NodeId) -> Node,
) -> Option<NodeId> {
    match (nodes.as_deref_mut(), premise) {
        (Some(n), Some(p)) => Some(push(n, rule(p))),
        _ => None,
    }
}