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
// deduxit © Fabian Lukas Grubmüller 2026
// Licensed under the EUPL
//! The wire forms: how a value crosses a process boundary (JSON, or any
//! format serde writes), and the rule that keeps a stored document
//! readable by later releases. Needs the cargo feature `serialize` (on by
//! default).
//!
//! # The level
//!
//! Every top-level document starts with `"version": n`, the lowest wire
//! level whose reader understands all of it; [`LEVEL`] is the highest
//! this build reads and writes. The writer computes the level from the
//! content, so a value that a later release represents as this one does
//! keeps its bytes. Level 1 is that of 0.1.0; level 2 adds the classical
//! refutation (`{"kind": "classical", "assignment": […]}`) and the
//! ordered modes (`"mode": "cyclic"`, `"lambek"` and `"lambek-star"`, the
//! rules of the Lambek calculus's divisions, and `"order": "cyclic"` in a
//! proof structure), so a disproof or an unprovable outcome that holds
//! the refutation and a document of an ordered mode are written at level 2;
//! level 3 adds the refutation by a saturated set (`{"kind":
//! "saturated", "positive": […], "sequents": […]}`), so a disproof or an
//! unprovable outcome that holds it is written at level 3; level 4 adds
//! the proof structure with boxes, a form whose `"vertices"` key makes its
//! links name vertices rather than occurrences, so it is always written at
//! level 4 (and the proof structure's reader refuses a document with that
//! key); level 5 adds the intuitionistic sequent, a form of its own whose
//! terms are the connectives of intuitionistic linear logic, always
//! written at level 5; level 6 adds the cut: a sequent's cut formulas
//! (`"cuts": […]`, without which a proof's cut nodes would name
//! occurrences past its forest) and the proof's tag `cut`, so a document
//! whose sequent holds a cut formula is written at level 6, and so is a
//! proof with a cut, whose sequent holds the cut's formula, and the
//! record of a cut elimination, whose proof's sequent holds them; level 7
//! adds the first-order sequent (`"symbols"`, `"fo"`, `"binders"`, an atom
//! as `{"P": [symbol, [terms]]}`, the terms `∀` and `∃`), so a document whose
//! sequent is first-order is written at level 7; level 8 adds the forms of
//! ordinary logic, an ordinary sequent, a derivation of LK or LJ, the
//! outcome of deciding an ordinary sequent and a proof in progress of
//! one, each always written at level 8; every other document is
//! written at level 1. A reader refuses a
//! document above its level by name ([`Error::Version`], code
//! `unsupported_version`), before the other keys when `version` comes
//! first, as the writer puts it; a document without `version` reads as
//! level 1. Nested values carry no `version`.
//!
//! What raises the level: a new tag, a new value of an enumeration a
//! reader refuses when it does not know it (a mode, a criterion, a
//! refutation's or a reason's kind), a key whose presence changes what the
//! other keys mean. What does not: a key an older reader may ignore
//! without misreading (a counter, an error's detail).
//!
//! # Reading
//!
//! [`upgrade`] is the one reader of every form: it reads a document of any
//! level up to [`LEVEL`] as a value of this release, every older level
//! through one step per level that changed a form (none so far: level 2
//! only adds a kind of refutation and a mode, level 3 a kind of
//! refutation, levels 4 and 5 a form each, level 6 a key and a tag, level
//! 7 keys, tags and a shape of atom, level 8 forms), and
//! counts every sequent's occurrences against `limits.occurrences` before
//! anything unfolds. [`Within`] is the same as a serde seed, for a value
//! nested in a document of the caller's; the plain `Deserialize` impls read
//! within the default [`Limits`]. A data form (a sequent, a proof, a
//! structure, a session) ignores the keys it does not know, which is what
//! lets an outcome be read as a proof; an options form refuses them by
//! name. The keys every level-1 value has are required, so a document of
//! another shape is refused naming the key ([`Error::Json`]); the names
//! of the forms before this release (`ids`, `var_dict`, `proof`, a mode as
//! an object of flags) are not read.
//!
//! # The forms
//!
//! Enumerations are written by name, never by number; integers are exact
//! in JavaScript (a bound past 2⁵³ is written `null`, and a written count
//! that saturates is `u64::MAX`, meaning "at least"). With the running
//! example `A, A -o B |- B`, `⊢ ~A, A ⊗ ~B, B`:
//!
//! - **Sequent**: `{"version": 1, "terms": [{"D": 0}, {"V": 0}, {"D": 1},
//! {"⊗": [1, 2]}, {"V": 1}], "roots": [0, 3, 4], "atoms": ["A", "B"],
//! "antecedents": 2}`, see [`Sequent`](crate::linear::Sequent).
//! - **Proof**: `{"version": 1, "sequent": {…}, "nodes": [{"ax": [0, 2]},
//! {"ax": [3, 4]}, {"⊗": [1, 0, 1]}], "mode": "classical"}`, with `goal`
//! for a proof of a goal off the roots, see [`Proof`](crate::linear::Proof). A
//! proof with cuts is written at level 6: its sequent holds `"cuts": […]`
//! and a cut node is `{"cut": [a, l, r]}`, `a` the occurrence of the cut
//! formula. A proof of a first-order sequent is written at level 7, as
//! its sequent is: the nodes `{"∀": [m, a, p]}` (a member, its
//! eigenvariable, the premise) and `{"∃": [m, w, p]}` (a member, its
//! witness, a term, the premise), and `"instances": {"members":
//! [[occurrence, frame], …], "frames": [[parent, term], …], "terms":
//! […]}`, the members past the forest, the frames with the innermost
//! binder's term first and `null` past the outermost, and the proof's
//! terms numbered on from the sequent's `fo`, an eigenvariable `{"E":
//! k}`; see [`Instances`](crate::linear::Instances).
//! - **Disproof**: `{"version": 1, "sequent": {…}, "mode": "classical",
//! "refutation": {"kind": "unbalanced", "atom": 0, "least": 2, "most":
//! 2}}`, with `goal` for a goal off the roots, see
//! [`Disproof`](crate::linear::Disproof).
//! - **Proof structure**: `{"version": 1, "sequent": {…}, "mix": false,
//! "links": [[0, 2], [3, 4]]}`, see
//! [`ProofStructure`](crate::linear::ProofStructure). A structure with cut
//! links is written at level 6, its sequent holding the cut formulas:
//! `"cuts": [[2, 5]]`, each cut link by its two vertices, a key written
//! only where there are cut links, as in a structure with boxes.
//! - **Proof structure with boxes**: `{"version": 4, "sequent": {…},
//! "mix": false, "vertices": [[3, 1, 1], …], "links": [[2, 3]],
//! "weakenings": []}`: the roots are vertices `0` to `r − 1`, then each
//! of `vertices` as `[occurrence, parent, box]`, `box` the `!` vertex of
//! its innermost box or `null`; `links` and `weakenings` (a `?` vertex
//! without copies and the literal it hangs on) name vertices; see
//! [`MellStructure`](crate::linear::MellStructure). With cut links it is
//! written at level 6, an end of a cut link `[occurrence, null, box]`.
//! - **Intuitionistic sequent**: `{"version": 5, "terms": [{"V": 0},
//! {"V": 1}, {"⊸": [0, 1]}], "atoms": ["A", "B"], "hypotheses": [2,
//! 0], "goal": 1}` for `A ⊸ B, A ⊢ B`: the terms `V`, `1`, `0`, `⊤`,
//! `⊗`, `⊸` (the antecedent first), `&`, `⊕` and `!`, every operand
//! before its term; see [`IllSequent`](crate::linear::IllSequent). With the
//! quantifiers `∀` and `∃` and the first-order keys it is written at
//! level 7, as the one-sided sequent is.
//! - **First-order sequent**: `{"version": 7, "terms": [{"D": 0}, {"∃":
//! 0}, {"V": 1}], "roots": [1, 2], "atoms": [{"P": [0, [0]]},
//! {"P": [0, [1]]}], "symbols": [["p", 1], ["a", 0]], "fo": [{"B": 0},
//! {"F": [1, []]}], "binders": ["x"], "antecedents": 1}` for `forall x.
//! p(x) |- p(a)`: `symbols` each a name with its arity, `fo` the terms
//! (`B` a bound variable, the innermost binder's `0`; `F` a symbol
//! applied to terms of `fo`, each before its term), every atom a
//! predicate applied to terms, a binder as `{"∀": body}` or `{"∃":
//! body}`, and `binders` the variables' names, one per binder in the
//! order of the terms.
//! - **Session**: `{"version": 1, "sequent": {…}, "mode":
//! "intuitionistic", "inferences": […], "history": […]}`, and for a
//! session that keeps how its sequent was written `"written":
//! {"inputs": […], "implications": […]}`, a key that raises no level,
//! since a reader that ignores it reads the session as it is and draws
//! it one-sided; see `Interactive` (feature `interactive`).
//! - **Ordinary sequent**: `{"version": 8, "formulas": [{"V": 0}, {"V":
//! 1}, {"→": [0, 1]}], "atoms": ["a", "b"], "left": [2, 0], "right": [1]}`
//! for `a → b, a ⊢ b`: the formulas `V`, `⊤`, `⊥`, `¬`, `∧`, `∨`, `→`
//! and `↔`, every operand before its formula, and the two sides as
//! indices into them; see [`ordinary::Sequent`](crate::ordinary::Sequent).
//! - **Ordinary derivation**: `{"version": 8, "logic": "intuitionistic",
//! "sequent": {…}, "inferences": [{"left": [0], "right": [0], "rule":
//! "ax"}, …, {"left": [2, 0], "right": [1], "rule": "→L", "principal":
//! ["left", 0], "premises": [0, 1]}]}`: the sequent with every formula
//! the inferences name, the inferences premises before conclusions and
//! the root last, each its two sides, its rule's name, its principal
//! formula's side and position and its premises; reading one checks its
//! shape (a tree, its root last), and [`Derivation::check`] its rules;
//! see [`ordinary::Derivation`](crate::ordinary::Derivation).
//!
//! [`Derivation::check`]: crate::ordinary::Derivation::check
//! - **Ordinary outcome** (written only): `{"version": 8, "deduxit":
//! "0.3.0", "verdict": "valid", "logic": "classical", "translation":
//! "affine", "engine": "focus", "statistics": {…}, "derivation": {…}}`,
//! `verdict` one of `valid`, `not_valid` and `unknown` (with a `reason`
//! in an outcome's form), the engine and statistics the image's search's;
//! see [`ordinary::Outcome`](crate::ordinary::Outcome).
//! - **Ordinary session**: `{"version": 8, "logic": "intuitionistic",
//! "sequent": {…}, "inferences": […], "history": […]}`, the inferences in
//! the state's own order, the root first, an open goal its two sides
//! alone; reading one checks every inference by the checker's rules; see
//! `ordinary::Interactive` (feature `interactive`).
//! - **Cut elimination** (written only): `{"version": 6, "proof": {…},
//! "steps": [{"cut": 7, "formula": 9, "case": "tensor_par"}, …],
//! "end": "cut_free"}`, or `"end": "refused"` with the `reason` in an
//! error's form; see [`Elimination`](crate::linear::proofs::cut::Elimination).
//! - **Outcome** (written only): `{"version": 1, "deduxit": "0.1.0",
//! "verdict": "proved", "checked": true, "fragment": "MLL", "mode":
//! "classical", "engine": "net", "statistics": {…}, "sequent": {…},
//! "nodes": […]}`, a proof's keys for `proved` and a disproof's for
//! `unprovable`, so that it reads back as a proof or as a disproof; see
//! [`Outcome`](crate::linear::Outcome).
use crateError;
use crateLimits;
use ;
use RefCell;
use PhantomData;
/// The highest wire level this build reads and writes.
pub const LEVEL: u32 = 8;
/// The level of every document but a disproof (or an unprovable
/// outcome) with a classical refutation, which is level 2, or with a
/// saturated set, which is level 3, and a document of an ordered mode.
pub const BASE: u32 = 1;
/// The level of a proof structure with boxes, which level 4 added.
pub const BOXES: u32 = 4;
/// The level of an intuitionistic sequent, which level 5 added.
pub const ILL: u32 = 5;
/// A form [`upgrade`] and [`Within`] read: one whose reading counts a
/// sequent against the caller's limits. Sealed: the crate's data forms
/// alone implement it.
/// Reads a value of the form `T` within limits, as a serde seed: the
/// value nested in a document of the caller's that [`upgrade`] reads
/// whole.
/// Reads a document of any released level up to [`LEVEL`] and returns it
/// as a value of this release, an older level through each level's step
/// in turn (there is none so far), every sequent within
/// `limits.occurrences`. It reads one value and leaves the deserializer
/// there: a caller whose input is that one document ends the
/// deserializer (`end()` in serde_json), which refuses what follows it.
///
/// # Errors
///
/// [`Error::Version`] for a document above [`LEVEL`]; the error of the
/// form's checks for a value they refuse (an index out of bounds, a
/// sequent past the bound, links that are no links); [`Error::Json`] for a
/// document of another shape, naming what is missing or wrong.
///
/// # Examples
///
/// use deduxit::{Error, Limits, wire, linear::Sequent};
///
/// let json = r#"{"version": 1, "terms": [{"V": 0}], "roots": [0], "atoms": ["A"]}"#;
/// let mut document = serde_json::Deserializer::from_str(json);
/// let sequent: Sequent = wire::upgrade(&mut document, &Limits::default())?;
/// document.end().expect("one document and nothing after it");
/// assert_eq!(sequent.to_string(), "⊢ A");
///
/// let newer = r#"{"version": 9, "terms": []}"#;
/// let mut document = serde_json::Deserializer::from_str(newer);
/// let error = wire::upgrade::<Sequent, _>(&mut document, &Limits::default()).unwrap_err();
/// assert!(matches!(error, Error::Version { found: 9, .. }));
/// # Ok::<(), Error>(())
/// ```
thread_local!
/// Returns the deserializer's error for one of the library's own, which
/// [`upgrade`] then answers instead of [`Error::Json`].
pub
/// Reads a `version` key: a level up to [`LEVEL`], refused above it.
pub
/// The level of a sequent with cut formulas, which level 6 added.
pub const CUTS: u32 = 6;
/// The level of a first-order sequent, which level 7 added.
pub const TERMS: u32 = 7;
/// The level of the forms of ordinary logic, which level 8 added.
pub const ORDINARY: u32 = 8;
/// The level of a document that holds `sequent`: 7 where it is
/// first-order, which level 7 added, 6 where it has cut formulas, which
/// level 6 added, else 1.
pub
/// The level of a document of `mode`, or of no mode: 2 for an ordered
/// mode, which level 2 added, else 1.
pub const
/// The level of a disproof with `refutation` in `mode`: 3 for the
/// saturated set, which level 3 added; 2 for the classical refutation and
/// an ordered mode, which level 2 added; else 1.
pub const