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

//! A derivation in a calculus given as data, laid out as a Fitch proof:
//! numbered lines, each subproof's lines together under its hypotheses.

use super::derivation::Kind;
use super::{Calculus, Derivation, NodeId, Step, TermId};
use crate::hash::{HashMap, HashSet};
use crate::limits::Refusal;
use crate::{Error, Limits};
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::fmt::{self, Display, Formatter, Write};

/// What the estimate counts per line, in bytes: its number, its
/// justification and the layout's numbers.
const LINE: u64 = 256;

/// What the estimate counts per member of a line's sequent, in bytes: a
/// short formula's text.
const MEMBER: u64 = 64;

/// What the estimate counts per proof a line lies in, in bytes: the text
/// form's `│ ` (four bytes) and the SVG's share of the line beside it.
const BAR: u64 = 8;

/// The choices of a Fitch proof's text, whatever format writes it.
///
/// Start from `FitchOptions::default()` and change the fields you want,
/// by the `with_*` builders or by assignment. With serde a JSON object
/// with some of the fields reads as the defaults with those changed.
///
/// Needs the cargo feature `serialize`.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct FitchOptions {
    /// What a hypothesis's line gives as its justification: nothing by
    /// default, as Fitch and the LaTeX package `fitch` write it; some
    /// textbooks write `Hyp` or `PR`.
    pub hypothesis: String,
    /// What an open line, a goal of a proof in progress, gives as its
    /// justification: `?` by default.
    pub open: String,
}

impl Default for FitchOptions {
    /// No justification beside a hypothesis, and `?` beside an open line.
    fn default() -> Self {
        Self {
            hypothesis: String::new(),
            open: "?".to_owned(),
        }
    }
}

impl FitchOptions {
    /// Returns the options with another [`hypothesis`](Self::hypothesis).
    #[must_use]
    pub fn with_hypothesis(self, hypothesis: impl Into<String>) -> Self {
        Self {
            hypothesis: hypothesis.into(),
            ..self
        }
    }

    /// Returns the options with another [`open`](Self::open).
    #[must_use]
    pub fn with_open(self, open: impl Into<String>) -> Self {
        Self {
            open: open.into(),
            ..self
        }
    }
}

/// A line cited by another: one line, or a subproof by its first and its
/// last line.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Cite {
    /// The line of this number.
    Line(u32),
    /// The subproof from the first number to the second.
    Range(u32, u32),
}

/// A line of a Fitch proof.
#[derive(Clone, Debug)]
pub(crate) struct Line {
    /// The node it shows.
    pub(crate) node: NodeId,
    /// How many subproofs it lies in: 0 in the main proof.
    pub(crate) depth: u32,
    /// The lines and subproofs it is justified by, in its rule's order.
    pub(crate) cites: Vec<Cite>,
    /// How many subproofs open at it.
    pub(crate) opens: u32,
    /// How many subproofs end at it.
    pub(crate) closes: u32,
    /// Whether it is the last hypothesis of its proof or subproof, which
    /// a bar under it closes.
    pub(crate) closes_hypotheses: bool,
}

/// A proof or subproof: its lines, by index, and its hypotheses first.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Block {
    /// How many subproofs it lies in: 0 for the main proof.
    pub(crate) depth: u32,
    /// Its first line.
    pub(crate) first: u32,
    /// How many of its lines, the first ones, are its hypotheses.
    pub(crate) hypotheses: u32,
    /// Its last line.
    pub(crate) last: u32,
}

/// A derivation in a calculus given as data, with the calculus that
/// writes its formulas, as a Fitch proof: numbered lines, each a formula
/// and its justification, the rule and the lines it follows from. A
/// subproof's lines stand together under its hypotheses, which a bar
/// closes, and a line beside them runs from its first line to its last.
/// A rule that discharges cites the subproof by its first and last line,
/// `→I 2–4`; the subproof's last line is the premise it discharges in.
///
/// Every export takes it as it takes a derivation:
/// `export::svg::to_string(&fitch, &style)`, `latex` (the package `fitch`)
/// and `typst` (the package `fitchcraft`); `Display` writes it as text.
///
/// The lines need not be in the derivation's order: each subproof's lines
/// are put together, its hypotheses first and its conclusion last, every
/// line after the lines it cites, and otherwise in the derivation's
/// order. A derivation where no such order exists, a subproof whose
/// conclusion a line inside it uses, or one that two rules conclude
/// differently, is refused ([`Error::NotFitch`]): it can still be drawn
/// as a tree ([`Drawing`](super::Drawing)). A node used by several lines is
/// one line, cited by each, and so are the hypotheses of one subproof
/// with equal formulas: a derivation has a hypothesis node per use.
///
/// Needs the cargo feature `serialize`.
#[derive(Clone, Debug)]
pub struct Fitch<'a> {
    /// The calculus whose print forms write the formulas.
    calculus: &'a Calculus,
    /// The derivation.
    derivation: &'a Derivation,
    /// The choices of the text.
    options: FitchOptions,
    /// The lines in order.
    lines: Vec<Line>,
    /// The main proof first, then every subproof in the order it opens.
    #[cfg_attr(
        not(feature = "svg"),
        expect(
            dead_code,
            reason = "only the SVG drawing reads the blocks: the other writers read the lines' flags"
        )
    )]
    blocks: Vec<Block>,
}

/// A part of a proof or subproof as the ordering moves it: a line, or a
/// subproof inside it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum Unit {
    /// The line of this node.
    Node(u32),
    /// The subproof of this scope.
    Block(u32),
}

impl<'a> Fitch<'a> {
    /// Returns the derivation laid out as a Fitch proof, its formulas
    /// written by `calculus` and its text chosen by `options`.
    ///
    /// # Errors
    ///
    /// [`Error::InvalidDerivation`] for a derivation without nodes, or
    /// whose sequents have other zones than the calculus's;
    /// [`Error::NotFitch`] for one no order writes as a Fitch proof, with
    /// the reason; [`Refusal::Output`] when its text is estimated past
    /// `limits.derivation_bytes`, and [`Refusal::Work`] past `limits.work`.
    pub fn new(
        calculus: &'a Calculus,
        derivation: &'a Derivation,
        options: &FitchOptions,
        limits: &Limits,
    ) -> Result<Self, Error> {
        let invalid = |reason: String| Error::InvalidDerivation {
            calculus: calculus.name().into(),
            reason,
        };
        if derivation.zones() != calculus.zones.len() {
            return Err(invalid(format!(
                "its sequents have {} zones, the calculus's {}",
                derivation.zones(),
                calculus.zones.len()
            )));
        }
        let Some(root) = derivation.root() else {
            return Err(invalid("it has no node".to_owned()));
        };
        let refused = |reason: String| Error::NotFitch { reason };
        let count = derivation.len();
        let parents = derivation.scope_parents();
        let id = |node: u32| derivation.at(node as usize).id();
        let scope = |node: u32| derivation.at(node as usize).scope().index() as u32;
        // A scope's depth; a parent is made before its children.
        let depth = depths(parents);
        // The first node of each scope, by which a reason names a subproof
        // and the order ties a subproof's place.
        let mut firsts = vec![u32::MAX; parents.len()];
        for node in (0..count as u32).rev() {
            firsts[scope(node) as usize] = node;
        }
        let first_node = |scope: u32| firsts[scope as usize];
        // The node whose line shows each node. A hypothesis is a node per
        // use, so the hypotheses of one scope with equal sequents are one
        // line, the first one's.
        let mut shown: Vec<u32> = (0..count as u32).collect();
        let mut uses: HashMap<(u32, Vec<&[TermId]>), u32> = HashMap::default();
        for (node, at) in derivation.nodes().enumerate() {
            if derivation.nodes[node].kind == Kind::Hypothesis {
                let sequent = (0..derivation.zones()).map(|z| at.zone(z)).collect();
                shown[node] = *uses
                    .entry((scope(node as u32), sequent))
                    .or_insert(node as u32);
            }
        }
        // The text's size, before anything is laid out.
        let mut estimate = 0u64;
        for node in derivation.nodes() {
            let members = (0..derivation.zones()).map(|z| node.zone(z).len() as u64);
            let bars = u64::from(depth[node.scope().index()]) + 1;
            estimate = estimate
                .saturating_add(LINE)
                .saturating_add(MEMBER.saturating_mul(members.sum()))
                .saturating_add(BAR.saturating_mul(bars));
        }
        if let Some(limit) = limits.derivation_bytes
            && estimate > limit
        {
            return Err(Error::Refused(Refusal::Output {
                what: "Fitch proof",
                estimate_bytes: estimate,
                limit_bytes: limit,
                least_bytes: None,
            }));
        }
        let mut climb = Climb {
            parents,
            depth: &depth,
            work: 0,
            limit: limits.work,
        };
        // The root as the line that shows it: a hypothesis may be merged.
        let root_index = shown[root.id().index()];
        if scope(root_index) != 0 {
            return Err(refused(
                "the last node lies inside a subproof, not in the main proof".to_owned(),
            ));
        }

        // Each scope's parts, what each part waits for, and the line each
        // subproof concludes.
        let mut parts: Vec<Vec<Unit>> = vec![Vec::new(); parents.len()];
        let mut seen: HashSet<Unit> = HashSet::default();
        for node in (0..count as u32).filter(|&node| shown[node as usize] == node) {
            let mut level = scope(node);
            let mut part = Unit::Node(node);
            // The node, and every subproof around it up to the main proof,
            // each a part of the one around it.
            loop {
                if !seen.insert(part) {
                    break;
                }
                parts[level as usize].push(part);
                if level == 0 {
                    break;
                }
                climb.tick()?;
                part = Unit::Block(level);
                level = parents[level as usize];
            }
        }
        let mut waits: HashMap<Unit, Vec<Unit>> = HashMap::default();
        let mut edges: HashSet<(Unit, Unit)> = HashSet::default();
        let mut owed: HashMap<Unit, u32> = HashMap::default();
        let mut concludes: Vec<Option<u32>> = vec![None; parents.len()];
        for node in 0..count as u32 {
            let at = scope(node);
            for &premise in derivation.at(node as usize).premises() {
                let premise = shown[premise.index()];
                let there = scope(premise);
                // A premise in the scope or one around it, or a subproof
                // directly inside one of those, which the node discharges.
                let (level, from) = if climb.inside(at, there)?.is_some() || at == there {
                    (there, Unit::Node(premise))
                } else if there != 0
                    && (parents[there as usize] == at
                        || climb.inside(at, parents[there as usize])?.is_some())
                {
                    match concludes[there as usize] {
                        Some(other) if other != premise => {
                            return Err(refused(format!(
                                "two rules take the subproof of node {} with different \
                                 conclusions, nodes {} and {}",
                                first_node(there),
                                other,
                                premise
                            )));
                        }
                        _ => concludes[there as usize] = Some(premise),
                    }
                    (parents[there as usize], Unit::Block(there))
                } else {
                    return Err(refused(format!(
                        "node {node} cites node {premise}, which lies in a subproof out of \
                         its reach"
                    )));
                };
                let Some(to) = climb.unit(scope(node), node, level)? else {
                    return Err(refused(format!(
                        "node {node} cites node {premise}, which lies in a subproof out of \
                         its reach"
                    )));
                };
                if to != from && edges.insert((from, to)) {
                    waits.entry(from).or_default().push(to);
                    *owed.entry(to).or_default() += 1;
                }
            }
        }

        // Each scope's order: its hypotheses first, its conclusion last,
        // every part after what it waits for, and otherwise by the
        // derivation's order.
        let key = |part: Unit| match part {
            Unit::Node(node) => node,
            Unit::Block(s) => first_node(s),
        };
        let hypothesis = |part: Unit| matches!(part, Unit::Node(n) if derivation.nodes[n as usize].kind == Kind::Hypothesis);
        let mut orders: Vec<Vec<Unit>> = vec![Vec::new(); parents.len()];
        for (level, list) in parts.iter().enumerate() {
            if list.is_empty() {
                continue;
            }
            // A conclusion that is one of the hypotheses stands first among
            // them: a Fitch proof would repeat it last, which this one does
            // not write, and the citation of the whole subproof holds it.
            let last = if level == 0 {
                Some(Unit::Node(root_index))
            } else {
                concludes[level].map(Unit::Node)
            }
            .filter(|&part| !hypothesis(part));
            let mut ready: BinaryHeap<Reverse<(bool, u32, Unit)>> = BinaryHeap::new();
            for &part in list {
                if owed.get(&part).copied().unwrap_or(0) == 0 && Some(part) != last {
                    ready.push(Reverse((!hypothesis(part), key(part), part)));
                }
            }
            let order = &mut orders[level];
            while let Some(Reverse((_, _, part))) = ready.pop() {
                climb.tick()?;
                order.push(part);
                for &next in waits.get(&part).map_or(&[][..], Vec::as_slice) {
                    let left = owed.entry(next).or_default();
                    *left -= 1;
                    if *left == 0 && Some(next) != last {
                        ready.push(Reverse((!hypothesis(next), key(next), next)));
                    }
                }
            }
            if let Some(last) = last
                && owed.get(&last).copied().unwrap_or(0) == 0
                && !order.contains(&last)
            {
                order.push(last);
            }
            if order.len() != list.len() {
                let what = if level == 0 {
                    "the main proof".to_owned()
                } else {
                    format!("the subproof of node {}", first_node(level as u32))
                };
                return Err(refused(format!(
                    "no order of {what} puts every line after the lines it cites and the \
                     line it concludes last: a line inside it uses its conclusion, or it \
                     uses a subproof that uses it"
                )));
            }
            // Hypotheses after another line cannot open a subproof.
            let opening = order.iter().take_while(|&&part| hypothesis(part)).count();
            if order.iter().skip(opening).any(|&part| hypothesis(part)) {
                return Err(refused(format!(
                    "a hypothesis of {} cannot stand among its first lines",
                    if level == 0 {
                        "the main proof".to_owned()
                    } else {
                        format!("the subproof of node {}", first_node(level as u32))
                    }
                )));
            }
        }

        // The lines, each subproof's in place, by a stack of its own.
        let mut lines: Vec<Line> = Vec::with_capacity(count);
        let mut blocks: Vec<Block> = Vec::new();
        let mut number = vec![0u32; count];
        let mut spans = vec![(0u32, 0u32); parents.len()];
        let mut stack: Vec<(u32, usize, usize)> = vec![(0, 0, 0)];
        blocks.push(Block {
            depth: 0,
            first: 0,
            hypotheses: 0,
            last: 0,
        });
        while let Some(&mut (level, ref mut at, block)) = stack.last_mut() {
            let order = &orders[level as usize];
            let Some(&part) = order.get(*at) else {
                let last = lines.len() as u32 - 1;
                blocks[block].last = last;
                spans[level as usize] = (blocks[block].first, last);
                stack.pop();
                continue;
            };
            *at += 1;
            match part {
                Unit::Node(node) => {
                    if hypothesis(part) {
                        blocks[block].hypotheses += 1;
                    }
                    number[node as usize] = lines.len() as u32 + 1;
                    lines.push(Line {
                        node: id(node),
                        depth: depth[level as usize],
                        cites: Vec::new(),
                        opens: 0,
                        closes: 0,
                        closes_hypotheses: false,
                    });
                }
                Unit::Block(inner) => {
                    blocks.push(Block {
                        depth: depth[inner as usize],
                        first: lines.len() as u32,
                        hypotheses: 0,
                        last: lines.len() as u32,
                    });
                    stack.push((inner, 0, blocks.len() - 1));
                }
            }
        }
        for block in &blocks {
            if block.depth > 0 {
                lines[block.first as usize].opens += 1;
                lines[block.last as usize].closes += 1;
            }
            if block.hypotheses > 0 {
                lines[(block.first + block.hypotheses - 1) as usize].closes_hypotheses = true;
            }
        }
        for line in &mut lines {
            let at = scope(line.node.index() as u32);
            for &premise in derivation.node(line.node).premises() {
                let there = scope(premise.index() as u32);
                let cite = if there == at || climb.inside(at, there)?.is_some() {
                    Cite::Line(number[shown[premise.index()] as usize])
                } else {
                    let (first, last) = spans[there as usize];
                    Cite::Range(first + 1, last + 1)
                };
                line.cites.push(cite);
            }
        }
        Ok(Self {
            calculus,
            derivation,
            options: options.clone(),
            lines,
            blocks,
        })
    }

    /// Returns how many lines it has.
    pub fn len(&self) -> usize {
        self.lines.len()
    }

    /// Returns whether it has no line, which a derivation's Fitch proof
    /// never is.
    pub fn is_empty(&self) -> bool {
        self.lines.is_empty()
    }

    /// Returns the node each line shows, in the lines' order: line `n`,
    /// counted from 1, shows the `n - 1`th. A hypothesis used several
    /// times shows the first of its nodes.
    pub fn nodes(&self) -> impl ExactSizeIterator<Item = NodeId> + '_ {
        self.lines.iter().map(|line| line.node)
    }

    /// Returns the lines.
    #[cfg(any(feature = "latex", feature = "typst", feature = "svg"))]
    pub(crate) fn lines(&self) -> &[Line] {
        &self.lines
    }

    /// Returns the main proof, then every subproof in the order it opens.
    #[cfg(feature = "svg")]
    pub(crate) fn blocks(&self) -> &[Block] {
        &self.blocks
    }

    /// Returns how a line is justified: its rule's label in the label
    /// markup, `None` for a hypothesis and an open line.
    #[cfg(any(feature = "latex", feature = "typst", feature = "svg"))]
    pub(crate) fn rule(&self, line: &Line) -> Option<String> {
        match self.derivation.node(line.node).step() {
            Step::Rule(name) => Some(super::draw::markup(name)),
            Step::Hypothesis | Step::Open => None,
        }
    }

    /// Returns what a line that cites no rule gives as its justification:
    /// the options' text for a hypothesis or an open line, empty for a
    /// rule's.
    pub(crate) fn note(&self, line: &Line) -> &str {
        match self.derivation.node(line.node).step() {
            Step::Rule(_) => "",
            Step::Hypothesis => &self.options.hypothesis,
            Step::Open => &self.options.open,
        }
    }

    /// Returns whether a line is a hypothesis.
    #[cfg(any(feature = "latex", feature = "svg"))]
    pub(crate) fn is_hypothesis(&self, line: &Line) -> bool {
        self.derivation.node(line.node).step() == Step::Hypothesis
    }

    /// Writes a line's citations with `range` between a subproof's first
    /// and last line: `2–4, 5`.
    pub(crate) fn write_cites(line: &Line, range: &str, out: &mut impl Write) -> fmt::Result {
        for (k, cite) in line.cites.iter().enumerate() {
            if k > 0 {
                out.write_str(", ")?;
            }
            match *cite {
                Cite::Line(n) => write!(out, "{n}")?,
                Cite::Range(a, b) => write!(out, "{a}{range}{b}")?,
            }
        }
        Ok(())
    }

    /// Writes a line's formula in a target's notation.
    #[cfg(any(feature = "latex", feature = "typst", feature = "svg"))]
    pub(crate) fn formula(
        &self,
        line: &Line,
        notation: &crate::linear::sequents::notation::Notation,
        out: &mut String,
    ) {
        let spelling = super::print::Spelling::of(notation, false, false);
        self.calculus
            .write_sequent_in(self.derivation, line.node, spelling, out);
    }

    /// Writes a line's formula as text.
    pub(crate) fn plain_formula(&self, line: &Line, out: &mut impl Write) -> fmt::Result {
        write!(
            out,
            "{}",
            self.calculus.show_sequent(self.derivation, line.node)
        )
    }

    /// Writes the name of a line's rule as the rule file names it;
    /// nothing for a hypothesis and an open line.
    #[cfg(feature = "svg")]
    pub(crate) fn plain_rule(&self, line: &Line, out: &mut impl Write) -> fmt::Result {
        match self.derivation.node(line.node).step() {
            Step::Rule(name) => out.write_str(name),
            Step::Hypothesis | Step::Open => Ok(()),
        }
    }

    /// Writes a line's justification as text: its rule and citations, or
    /// the options' text for a hypothesis or an open line.
    pub(crate) fn plain_justification(&self, line: &Line, out: &mut impl Write) -> fmt::Result {
        match self.derivation.node(line.node).step() {
            Step::Rule(name) => {
                out.write_str(name)?;
                if !line.cites.is_empty() {
                    out.write_char(' ')?;
                    Self::write_cites(line, "–", out)?;
                }
                Ok(())
            }
            Step::Hypothesis | Step::Open => out.write_str(self.note(line)),
        }
    }
}

/// Returns the depth of every scope, 0 for the main proof; a parent is
/// made before its children.
pub(super) fn depths(parents: &[u32]) -> Vec<u32> {
    let mut depth = vec![0u32; parents.len()];
    for s in 1..parents.len() {
        depth[s] = depth[parents[s] as usize] + 1;
    }
    depth
}

/// A walk up the scopes, its steps counted against the work bound.
pub(super) struct Climb<'d> {
    /// The parent of each scope, the main proof's its own.
    pub(super) parents: &'d [u32],
    /// The depth of each scope, 0 for the main proof.
    pub(super) depth: &'d [u32],
    /// The steps taken.
    pub(super) work: u64,
    /// The most steps, [`Limits::work`].
    pub(super) limit: Option<u64>,
}

impl Climb<'_> {
    /// Counts a step, and refuses past the bound.
    pub(super) fn tick(&mut self) -> Result<(), Error> {
        self.work = self.work.saturating_add(1);
        match self.limit {
            Some(limit) if self.work > limit => Err(Error::Refused(Refusal::Work { limit })),
            _ => Ok(()),
        }
    }

    /// Returns the scope directly inside `level` that holds `inner`, or
    /// `None` where `level` does not lie around `inner`.
    pub(super) fn inside(&mut self, mut inner: u32, level: u32) -> Result<Option<u32>, Error> {
        let below = self.depth[level as usize] + 1;
        while self.depth[inner as usize] > below {
            self.tick()?;
            inner = self.parents[inner as usize];
        }
        Ok(
            (self.depth[inner as usize] == below && self.parents[inner as usize] == level)
                .then_some(inner),
        )
    }

    /// Returns the part of the proof at `level` that holds the node in
    /// the scope `at`: the node's line, or the subproof around it.
    fn unit(&mut self, at: u32, node: u32, level: u32) -> Result<Option<Unit>, Error> {
        if at == level {
            return Ok(Some(Unit::Node(node)));
        }
        Ok(self.inside(at, level)?.map(Unit::Block))
    }
}

impl Display for Fitch<'_> {
    /// Writes the proof as text, a line each: its number, a `│` for the
    /// main proof and each subproof it lies in, its formula and its
    /// justification, and a `├──` under a proof's or subproof's last
    /// hypothesis.
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let width = self.lines.len().to_string().len();
        // Each line's text before its justification, to align them.
        let mut heads = Vec::with_capacity(self.lines.len());
        for (index, line) in self.lines.iter().enumerate() {
            let mut head = format!("{:>width$} ", index + 1);
            for _ in 0..=line.depth {
                head.push_str("│ ");
            }
            self.plain_formula(line, &mut head)?;
            heads.push(head);
        }
        let column = heads.iter().map(|h| h.chars().count()).max().unwrap_or(0);
        for (index, line) in self.lines.iter().enumerate() {
            let head = &heads[index];
            let mut justification = String::new();
            self.plain_justification(line, &mut justification)?;
            if justification.is_empty() {
                f.write_str(head)?;
            } else {
                let pad = column - head.chars().count();
                write!(f, "{head}{:pad$}   {justification}", "")?;
            }
            f.write_char('\n')?;
            // A bar under the last hypothesis of its proof or subproof.
            if line.closes_hypotheses {
                write!(f, "{:width$} ", "")?;
                for _ in 0..line.depth {
                    f.write_str("│ ")?;
                }
                f.write_str("├───\n")?;
            }
        }
        Ok(())
    }
}