@sublang/spex 6.0.0 → 7.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/README.md +419 -12
  2. package/dist/cli.js +12 -1
  3. package/dist/commands/add.d.ts +1 -1
  4. package/dist/commands/add.js +2 -2
  5. package/dist/commands/environment.d.ts +3 -3
  6. package/dist/commands/environment.js +5 -7
  7. package/dist/commands/export.js +2 -2
  8. package/dist/commands/home.d.ts +2 -2
  9. package/dist/commands/home.js +3 -3
  10. package/dist/commands/list.js +1 -1
  11. package/dist/commands/login.js +12 -7
  12. package/dist/commands/logout.js +2 -3
  13. package/dist/commands/publish.js +1 -1
  14. package/dist/commands/remove.js +1 -1
  15. package/dist/commands/run.d.ts +1 -1
  16. package/dist/commands/run.js +16 -0
  17. package/dist/commands/settle.d.ts +1 -1
  18. package/dist/commands/settle.js +1 -1
  19. package/dist/guards/approval.d.ts +56 -0
  20. package/dist/guards/approval.js +158 -0
  21. package/dist/guards/checks.d.ts +207 -0
  22. package/dist/guards/checks.js +681 -0
  23. package/dist/guards/commands.d.ts +15 -0
  24. package/dist/guards/commands.js +313 -0
  25. package/dist/guards/copy.d.ts +67 -0
  26. package/dist/guards/copy.js +306 -0
  27. package/dist/guards/declaration.d.ts +45 -0
  28. package/dist/guards/declaration.js +224 -0
  29. package/dist/guards/files.d.ts +137 -0
  30. package/dist/guards/files.js +544 -0
  31. package/dist/guards/harness.d.ts +58 -0
  32. package/dist/guards/harness.js +420 -0
  33. package/dist/guards/items.d.ts +61 -0
  34. package/dist/guards/items.js +161 -0
  35. package/dist/guards/patch.d.ts +55 -0
  36. package/dist/guards/patch.js +258 -0
  37. package/dist/guards/processes.d.ts +49 -0
  38. package/dist/guards/processes.js +233 -0
  39. package/dist/guards/proofs.d.ts +88 -0
  40. package/dist/guards/proofs.js +503 -0
  41. package/dist/guards/records.d.ts +176 -0
  42. package/dist/guards/records.js +513 -0
  43. package/dist/guards/report.d.ts +26 -0
  44. package/dist/guards/report.js +492 -0
  45. package/dist/guards/runner.d.ts +42 -0
  46. package/dist/guards/runner.js +167 -0
  47. package/dist/guards/tla.d.ts +51 -0
  48. package/dist/guards/tla.js +352 -0
  49. package/dist/guards/tlapm.d.ts +81 -0
  50. package/dist/guards/tlapm.js +172 -0
  51. package/dist/guards/tlc.d.ts +191 -0
  52. package/dist/guards/tlc.js +457 -0
  53. package/dist/guards/translation.d.ts +66 -0
  54. package/dist/guards/translation.js +202 -0
  55. package/dist/guards/types.d.ts +185 -0
  56. package/dist/guards/types.js +437 -0
  57. package/dist/guards/verify.d.ts +290 -0
  58. package/dist/guards/verify.js +1191 -0
  59. package/dist/guards/xml.d.ts +18 -0
  60. package/dist/guards/xml.js +105 -0
  61. package/dist/lib/environment/archive.d.ts +2 -2
  62. package/dist/lib/environment/archive.js +1 -1
  63. package/dist/lib/environment/builtins.d.ts +10 -12
  64. package/dist/lib/environment/builtins.js +17 -19
  65. package/dist/lib/environment/exports.d.ts +14 -13
  66. package/dist/lib/environment/exports.js +31 -16
  67. package/dist/lib/environment/format.d.ts +2 -2
  68. package/dist/lib/environment/format.js +3 -3
  69. package/dist/lib/environment/git-source.js +6 -3
  70. package/dist/lib/environment/install.d.ts +14 -16
  71. package/dist/lib/environment/install.js +66 -61
  72. package/dist/lib/environment/lock.d.ts +4 -3
  73. package/dist/lib/environment/lock.js +6 -5
  74. package/dist/lib/environment/marker.js +1 -2
  75. package/dist/lib/environment/own-paths.d.ts +7 -10
  76. package/dist/lib/environment/own-paths.js +10 -13
  77. package/dist/lib/environment/placed-record.d.ts +3 -2
  78. package/dist/lib/environment/placed-record.js +5 -4
  79. package/dist/lib/environment/registry.d.ts +1 -1
  80. package/dist/lib/environment/registry.js +1 -1
  81. package/dist/lib/environment/requests.d.ts +2 -2
  82. package/dist/lib/environment/requests.js +2 -2
  83. package/dist/lib/environment/resolve.d.ts +4 -4
  84. package/dist/lib/environment/resolve.js +3 -4
  85. package/dist/lib/environment/store.d.ts +34 -8
  86. package/dist/lib/environment/store.js +138 -43
  87. package/dist/lib/files.d.ts +72 -12
  88. package/dist/lib/files.js +200 -65
  89. package/dist/lib/git-host.d.ts +52 -258
  90. package/dist/lib/git-host.js +52 -265
  91. package/dist/lib/home-file.d.ts +13 -10
  92. package/dist/lib/home-file.js +28 -18
  93. package/dist/lib/instance.d.ts +1 -1
  94. package/dist/lib/instance.js +1 -1
  95. package/dist/lint.d.ts +22 -0
  96. package/dist/lint.js +36 -0
  97. package/dist/testing/standin-registry.js +1 -1
  98. package/package.json +7 -3
  99. package/scaffold/.file-history.json +14 -7
  100. package/scaffold/i18n/zh/source-pins.json +41 -0
  101. package/scaffold/i18n/zh/specs/decisions/000-spec-structure-format.md +0 -1
  102. package/scaffold/i18n/zh/specs/intents/000-spdx-headers.md +0 -1
  103. package/scaffold/i18n/zh/specs/map.md +0 -1
  104. package/scaffold/i18n/zh/specs/meta.md +18 -35
  105. package/scaffold/i18n/zh/specs/packages/git.md +0 -1
  106. package/scaffold/i18n/zh/specs/packages/licensing.md +0 -1
  107. package/scaffold/specs/meta.md +18 -3
  108. package/scripts/test-exit-guard.mjs +28 -0
@@ -0,0 +1,167 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
3
+ /*
4
+ * Guards package (guards-5, guards-13, guards-36), DR-244 and DR-252:
5
+ * the process that causes events on one copy's code. `spex verify`
6
+ * starts it with the copy as its working folder and the path of a job
7
+ * file. It loads the binding's module from the copy and causes each
8
+ * sequence of the job from a fresh start, writing the effects it
9
+ * observed as it goes. It judges nothing: what an effect means is
10
+ * said elsewhere. What the code prints takes no part in a result.
11
+ *
12
+ * Before the module loads, it asks Node to tell it of every module
13
+ * resolved from then on, so a project file loaded from outside the
14
+ * copy is seen and named. Everything this file needs is imported
15
+ * above that point. Only modules resolved in this process's main
16
+ * thread are seen: not those a worker thread loads, nor those of a
17
+ * process the code starts.
18
+ */
19
+ import * as nodeModule from "node:module";
20
+ import { appendFileSync, readFileSync, realpathSync } from "node:fs";
21
+ import { join, relative, sep } from "node:path";
22
+ import { fileURLToPath, pathToFileURL } from "node:url";
23
+ import { inside } from "./copy.js";
24
+ import { GuardError, decodeFields, vocabulary, } from "./types.js";
25
+ /** A failing binding (guards-36); its message is the cause. */
26
+ class Failing extends Error {
27
+ }
28
+ function words(error) {
29
+ const text = error instanceof Error ? error.message : String(error);
30
+ return text.split("\n")[0];
31
+ }
32
+ // ---------------------------------------------------------------
33
+ // Project files loaded from outside the copy (guards-13)
34
+ // ---------------------------------------------------------------
35
+ const outside = [];
36
+ function watchModules(root, copy) {
37
+ nodeModule.registerHooks({
38
+ resolve(specifier, context, nextResolve) {
39
+ const resolved = nextResolve(specifier, context);
40
+ if (resolved.url.startsWith("file:")) {
41
+ let path = fileURLToPath(resolved.url);
42
+ try {
43
+ path = realpathSync.native(path);
44
+ }
45
+ catch {
46
+ // A path that is not there fails on its own when loaded.
47
+ }
48
+ const mine = !inside(copy, path) && inside(root, path) && !path.split(sep).includes("node_modules");
49
+ if (mine)
50
+ outside.push(relative(root, path).split(sep).join("/"));
51
+ }
52
+ return resolved;
53
+ },
54
+ });
55
+ }
56
+ async function loadModule(job) {
57
+ let loaded;
58
+ try {
59
+ loaded = (await import(pathToFileURL(join(job.copy, ...job.module.split("/"))).href));
60
+ }
61
+ catch (error) {
62
+ throw new Failing(`binding.mjs does not load: ${words(error)}`);
63
+ }
64
+ if (typeof loaded.start !== "function")
65
+ throw new Failing("binding.mjs exports no start function");
66
+ const events = loaded.events;
67
+ if (typeof events !== "object" || events === null)
68
+ throw new Failing("binding.mjs exports no events");
69
+ for (const event of Object.keys(job.events)) {
70
+ if (!Object.hasOwn(events, event) || typeof events[event] !== "function") {
71
+ throw new Failing(`binding.mjs exports no function for event ${event}`);
72
+ }
73
+ }
74
+ if (loaded.stop !== undefined && typeof loaded.stop !== "function") {
75
+ throw new Failing("binding.mjs exports a stop that is not a function");
76
+ }
77
+ return loaded;
78
+ }
79
+ /** Cause one event and return its effect, read by its declared type. */
80
+ async function cause(binding, declaration, run, call) {
81
+ let observed;
82
+ try {
83
+ observed = await binding.events[call.event](run, structuredClone(call.params));
84
+ }
85
+ catch (error) {
86
+ throw new Failing(`${call.event} threw: ${words(error)}`);
87
+ }
88
+ const declared = declaration.events[call.event].effect;
89
+ // An event that declares no effect may return nothing.
90
+ if (observed === undefined && Object.keys(declared).length === 0)
91
+ observed = {};
92
+ try {
93
+ return decodeFields(declared, observed, declaration.vocabulary);
94
+ }
95
+ catch (error) {
96
+ if (!(error instanceof GuardError))
97
+ throw error;
98
+ throw new Failing(`${call.event} returned what is not its declared effect: ${error.message}`);
99
+ }
100
+ }
101
+ // ---------------------------------------------------------------
102
+ // The sequences
103
+ // ---------------------------------------------------------------
104
+ // A call that waits on something that never comes is stopped by the
105
+ // parent at the time limit; without this the process would just end.
106
+ const awake = setInterval(() => { }, 1 << 30);
107
+ const job = JSON.parse(readFileSync(process.argv[2], "utf8"));
108
+ function say(line) {
109
+ appendFileSync(job.result, `${JSON.stringify(line)}\n`);
110
+ }
111
+ /** Tell of a project file loaded from outside the copy, once. */
112
+ function strayed() {
113
+ if (outside.length === 0)
114
+ return false;
115
+ say({ kind: "outside", file: outside[0] });
116
+ return true;
117
+ }
118
+ async function run() {
119
+ const declaration = { vocabulary: vocabulary(job.values), events: job.events };
120
+ try {
121
+ watchModules(job.root, job.copy);
122
+ const binding = await loadModule(job);
123
+ if (strayed())
124
+ return;
125
+ for (const [index, sequence] of job.sequences.entries()) {
126
+ const effects = [];
127
+ let state;
128
+ try {
129
+ state = await binding.start();
130
+ }
131
+ catch (error) {
132
+ throw new Failing(`start threw: ${words(error)}`);
133
+ }
134
+ // The sequence's own failure comes before `stop`'s.
135
+ let failure;
136
+ try {
137
+ for (const call of sequence)
138
+ effects.push(await cause(binding, declaration, state, call));
139
+ }
140
+ catch (error) {
141
+ failure = error;
142
+ }
143
+ try {
144
+ await binding.stop?.(state);
145
+ }
146
+ catch (error) {
147
+ failure ??= new Failing(`stop threw: ${words(error)}`);
148
+ }
149
+ if (failure !== undefined)
150
+ throw failure;
151
+ if (strayed())
152
+ return;
153
+ say({ kind: "observed", at: index, effects });
154
+ }
155
+ say({ kind: "finished" });
156
+ }
157
+ catch (error) {
158
+ if (!(error instanceof Failing))
159
+ throw error;
160
+ if (!strayed())
161
+ say({ kind: "ended", cause: error.message });
162
+ }
163
+ }
164
+ await run();
165
+ clearInterval(awake);
166
+ process.exit(0);
167
+ //# sourceMappingURL=runner.js.map
@@ -0,0 +1,51 @@
1
+ import { GuardError, type Call, type Declaration, type Fields, type Type, type Value } from "./types.js";
2
+ export type Tla = string | number | boolean | {
3
+ m: string;
4
+ } | {
5
+ s: Tla[];
6
+ } | {
7
+ r: Record<string, Tla>;
8
+ } | {
9
+ t: Tla[];
10
+ } | {
11
+ f: [Tla, Tla][];
12
+ };
13
+ /** The value of a field of a type `T?` that holds nothing. */
14
+ export declare const MISSING: Tla;
15
+ /** The one form of a value: members and fields in a fixed order, a
16
+ * member once, a record or function of nothing as the empty tuple. */
17
+ export declare function canon(value: Tla): Tla;
18
+ /** One text per value: two values are equal exactly when their keys
19
+ * are. */
20
+ export declare function key(value: Tla): string;
21
+ /** What TLC printed could not be read as a value. */
22
+ export declare class Unreadable extends GuardError {
23
+ }
24
+ /** Read one value that is the whole of `text`. */
25
+ export declare function parseTla(text: string): Tla;
26
+ /** Read every value of a text that holds values one after another. */
27
+ export declare function parseTlaAll(text: string): Tla[];
28
+ /** Read the variables of one state of a TLC state dump or of a
29
+ * printed sequence: lines `/\ name = value`, the value on as many
30
+ * lines as TLC took. */
31
+ export declare function parseState(text: string): Record<string, Tla>;
32
+ /** A string as TLA+ writes it. */
33
+ export declare function tlaString(text: string): string;
34
+ /** A value as TLA+ text. */
35
+ export declare function tlaText(value: Tla): string;
36
+ /** A value of a declared type as a TLA+ value. */
37
+ export declare function toTla(value: Value): Tla;
38
+ /** Named values as a TLA+ record; no field gives the empty tuple. */
39
+ export declare function fieldsToTla(fields: Fields): Tla;
40
+ /** An event with its params as the record the declaration in TLA+
41
+ * holds for it: the field `event` names it. */
42
+ export declare function callToTla(call: Call): Tla;
43
+ /** A TLA+ record as the named values of `declared`, or null where it
44
+ * is no such record. */
45
+ export declare function fieldsFromTla(declared: Record<string, Type>, value: Tla, declaration: Declaration): Fields | null;
46
+ /** A TLA+ event record as a call, or null where it is no declared
47
+ * event with its params. */
48
+ export declare function callFromTla(value: Tla, declaration: Declaration): Call | null;
49
+ /** A TLA+ value as a report prints it where it is no value of a
50
+ * declared type: `none`, `{a, b}`, `<a, b>`, `{k: v}`. */
51
+ export declare function showTla(value: Tla): string;
@@ -0,0 +1,352 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
3
+ /*
4
+ * Guards package, DR-244 and DR-252: TLA+ values as TLC prints them.
5
+ * This file reads a printed value, writes a value as TLA+ text,
6
+ * compares two values for equality and turns a value of a declared
7
+ * type into a TLA+ value and back. It evaluates no TLA+.
8
+ *
9
+ * A value in memory:
10
+ * "text" a string true / false 12
11
+ * { m: "Missing" } a model value
12
+ * { s: [..] } a set, members in one fixed order, each once
13
+ * { r: {k: v} } a record, fields in name order
14
+ * { t: [..] } a tuple; a function over no value is { t: [] }
15
+ * { f: [[k, v]] } any other function, pairs in one fixed order
16
+ */
17
+ import { GuardError, decodeFields, showText, } from "./types.js";
18
+ /** The value of a field of a type `T?` that holds nothing. */
19
+ export const MISSING = { m: "Missing" };
20
+ /** Give a record a field. TLC prints a function over strings as a
21
+ * record, so a field's name can be any value a binding declares or a
22
+ * translation writes, `__proto__` among them, which a plain assignment
23
+ * would lose: the field is defined as the record's own, whatever its
24
+ * name, so two values that differ in it stay two. */
25
+ function put(record, name, value) {
26
+ Object.defineProperty(record, name, { value, enumerable: true, writable: true, configurable: true });
27
+ }
28
+ function byKey(a, b) {
29
+ const x = JSON.stringify(a);
30
+ const y = JSON.stringify(b);
31
+ return x < y ? -1 : x > y ? 1 : 0;
32
+ }
33
+ /** The one form of a value: members and fields in a fixed order, a
34
+ * member once, a record or function of nothing as the empty tuple. */
35
+ export function canon(value) {
36
+ if (typeof value !== "object")
37
+ return value;
38
+ if ("m" in value)
39
+ return { m: value.m };
40
+ if ("s" in value) {
41
+ const members = value.s.map(canon).sort(byKey);
42
+ return { s: members.filter((member, at) => at === 0 || byKey(member, members[at - 1]) !== 0) };
43
+ }
44
+ if ("t" in value)
45
+ return { t: value.t.map(canon) };
46
+ if ("r" in value) {
47
+ const names = Object.keys(value.r).sort();
48
+ if (names.length === 0)
49
+ return { t: [] };
50
+ const fields = {};
51
+ for (const name of names)
52
+ put(fields, name, canon(value.r[name]));
53
+ return { r: fields };
54
+ }
55
+ if (value.f.length === 0)
56
+ return { t: [] };
57
+ return {
58
+ f: value.f.map(([k, v]) => [canon(k), canon(v)]).sort((a, b) => byKey(a[0], b[0])),
59
+ };
60
+ }
61
+ /** One text per value: two values are equal exactly when their keys
62
+ * are. */
63
+ export function key(value) {
64
+ return JSON.stringify(canon(value));
65
+ }
66
+ // ---------------------------------------------------------------
67
+ // Reading what TLC printed
68
+ // ---------------------------------------------------------------
69
+ /** What TLC printed could not be read as a value. */
70
+ export class Unreadable extends GuardError {
71
+ }
72
+ class Reader {
73
+ text;
74
+ at = 0;
75
+ constructor(text) {
76
+ this.text = text;
77
+ }
78
+ fail(what) {
79
+ const near = this.text.slice(this.at, this.at + 40).replace(/\n/g, "\\n");
80
+ throw new Unreadable(`TLC printed what is not a value (${what}) at offset ${this.at}: ${near}`);
81
+ }
82
+ space() {
83
+ while (this.at < this.text.length && /\s/.test(this.text[this.at]))
84
+ this.at += 1;
85
+ }
86
+ take(word) {
87
+ this.space();
88
+ if (!this.text.startsWith(word, this.at))
89
+ return false;
90
+ this.at += word.length;
91
+ return true;
92
+ }
93
+ need(word) {
94
+ if (!this.take(word))
95
+ this.fail(`expected ${word}`);
96
+ }
97
+ list(close) {
98
+ const items = [];
99
+ if (this.take(close))
100
+ return items;
101
+ do
102
+ items.push(this.value());
103
+ while (this.take(","));
104
+ this.need(close);
105
+ return items;
106
+ }
107
+ /** A name: a model value, `TRUE`, `FALSE` or a state's variable.
108
+ * A record's field is read with a wider pattern: TLC prints a function
109
+ * over strings as a record wherever every key is made of letters,
110
+ * digits and `_`, and such a key may start with a digit. */
111
+ word(name = /[A-Za-z_][A-Za-z0-9_]*/y) {
112
+ this.space();
113
+ name.lastIndex = this.at;
114
+ const found = name.exec(this.text);
115
+ if (found === null)
116
+ return this.fail("expected a name");
117
+ this.at += found[0].length;
118
+ return found[0];
119
+ }
120
+ string() {
121
+ let out = "";
122
+ this.at += 1;
123
+ for (;;) {
124
+ const c = this.text[this.at++];
125
+ if (c === undefined)
126
+ this.fail("a string does not end");
127
+ if (c === '"')
128
+ return out;
129
+ if (c !== "\\") {
130
+ out += c;
131
+ continue;
132
+ }
133
+ const e = this.text[this.at++];
134
+ out += e === "n" ? "\n" : e === "t" ? "\t" : e === "r" ? "\r" : e === "f" ? "\f" : e;
135
+ }
136
+ }
137
+ value() {
138
+ this.space();
139
+ if (this.text[this.at] === '"')
140
+ return this.string();
141
+ if (this.take("<<"))
142
+ return { t: this.list(">>") };
143
+ if (this.take("{"))
144
+ return { s: this.list("}") };
145
+ if (this.take("[")) {
146
+ const fields = {};
147
+ do {
148
+ const name = this.word(/[A-Za-z0-9_]+/y);
149
+ this.need("|->");
150
+ put(fields, name, this.value());
151
+ } while (this.take(","));
152
+ this.need("]");
153
+ return { r: fields };
154
+ }
155
+ if (this.take("(")) {
156
+ const pairs = [];
157
+ do {
158
+ const k = this.value();
159
+ this.need(":>");
160
+ pairs.push([k, this.value()]);
161
+ } while (this.take("@@"));
162
+ this.need(")");
163
+ return { f: pairs };
164
+ }
165
+ const number = /-?\d+/y;
166
+ number.lastIndex = this.at;
167
+ const digits = number.exec(this.text);
168
+ if (digits !== null) {
169
+ this.at += digits[0].length;
170
+ const low = Number(digits[0]);
171
+ // TLC prints an interval it never enumerated as lo..hi.
172
+ if (this.text.startsWith("..", this.at)) {
173
+ this.at += 2;
174
+ number.lastIndex = this.at;
175
+ const high = number.exec(this.text);
176
+ if (high === null)
177
+ return this.fail("expected a number after ..");
178
+ this.at += high[0].length;
179
+ const members = [];
180
+ for (let n = low; n <= Number(high[0]); n += 1)
181
+ members.push(n);
182
+ return { s: members };
183
+ }
184
+ return low;
185
+ }
186
+ const word = this.word();
187
+ if (word === "TRUE")
188
+ return true;
189
+ if (word === "FALSE")
190
+ return false;
191
+ return { m: word };
192
+ }
193
+ }
194
+ /** Read one value that is the whole of `text`. */
195
+ export function parseTla(text) {
196
+ const reader = new Reader(text);
197
+ const value = reader.value();
198
+ reader.space();
199
+ if (reader.at !== text.length)
200
+ reader.fail("something follows the value");
201
+ return canon(value);
202
+ }
203
+ /** Read every value of a text that holds values one after another. */
204
+ export function parseTlaAll(text) {
205
+ const reader = new Reader(text);
206
+ const values = [];
207
+ for (;;) {
208
+ reader.space();
209
+ if (reader.at >= text.length)
210
+ return values;
211
+ values.push(canon(reader.value()));
212
+ }
213
+ }
214
+ /** Read the variables of one state of a TLC state dump or of a
215
+ * printed sequence: lines `/\ name = value`, the value on as many
216
+ * lines as TLC took. */
217
+ export function parseState(text) {
218
+ const reader = new Reader(text);
219
+ const state = {};
220
+ while (reader.take("/\\")) {
221
+ const name = reader.word();
222
+ reader.need("=");
223
+ state[name] = canon(reader.value());
224
+ }
225
+ reader.space();
226
+ if (reader.at !== text.length)
227
+ reader.fail("something follows the state");
228
+ return state;
229
+ }
230
+ // ---------------------------------------------------------------
231
+ // Writing TLA+ text
232
+ // ---------------------------------------------------------------
233
+ /** A string as TLA+ writes it. */
234
+ export function tlaString(text) {
235
+ if (!/^[\x20-\x7e]*$/.test(text)) {
236
+ throw new GuardError(`a TLA+ string holds printable ASCII only: ${JSON.stringify(text)}`);
237
+ }
238
+ if (text.includes("`"))
239
+ throw new GuardError(`a TLA+ string holds no backtick: ${JSON.stringify(text)}`);
240
+ return `"${text.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
241
+ }
242
+ /** A value as TLA+ text. */
243
+ export function tlaText(value) {
244
+ if (typeof value === "string")
245
+ return tlaString(value);
246
+ if (typeof value === "boolean")
247
+ return value ? "TRUE" : "FALSE";
248
+ if (typeof value === "number")
249
+ return value < 0 ? `(${value})` : String(value);
250
+ if ("m" in value)
251
+ return value.m;
252
+ if ("s" in value)
253
+ return `{${value.s.map(tlaText).join(", ")}}`;
254
+ if ("t" in value)
255
+ return `<<${value.t.map(tlaText).join(", ")}>>`;
256
+ if ("r" in value) {
257
+ const names = Object.keys(value.r);
258
+ if (names.length === 0)
259
+ return "<<>>";
260
+ return `[${names.map((name) => `${name} |-> ${tlaText(value.r[name])}`).join(", ")}]`;
261
+ }
262
+ return `(${value.f.map(([k, v]) => `${tlaText(k)} :> ${tlaText(v)}`).join(" @@ ")})`;
263
+ }
264
+ // ---------------------------------------------------------------
265
+ // Values of declared types
266
+ // ---------------------------------------------------------------
267
+ /** A value of a declared type as a TLA+ value. */
268
+ export function toTla(value) {
269
+ if (value === null)
270
+ return MISSING;
271
+ if (Array.isArray(value))
272
+ return canon({ s: value.map(toTla) });
273
+ return value;
274
+ }
275
+ /** Named values as a TLA+ record; no field gives the empty tuple. */
276
+ export function fieldsToTla(fields) {
277
+ const record = {};
278
+ for (const [name, value] of Object.entries(fields))
279
+ record[name] = toTla(value);
280
+ return canon({ r: record });
281
+ }
282
+ /** An event with its params as the record the declaration in TLA+
283
+ * holds for it: the field `event` names it. */
284
+ export function callToTla(call) {
285
+ const record = { event: call.event };
286
+ for (const [name, value] of Object.entries(call.params))
287
+ record[name] = toTla(value);
288
+ return canon({ r: record });
289
+ }
290
+ /** A TLA+ value as plain JSON data, a set as a list and the missing
291
+ * value as null; undefined where JSON holds no such value. */
292
+ function toData(value) {
293
+ if (typeof value !== "object")
294
+ return value;
295
+ if ("m" in value)
296
+ return value.m === "Missing" ? null : undefined;
297
+ if ("s" in value)
298
+ return value.s.map(toData);
299
+ if ("t" in value)
300
+ return value.t.length === 0 ? {} : undefined;
301
+ if ("r" in value) {
302
+ const record = {};
303
+ for (const [name, field] of Object.entries(value.r))
304
+ put(record, name, toData(field));
305
+ return record;
306
+ }
307
+ return undefined;
308
+ }
309
+ /** A TLA+ record as the named values of `declared`, or null where it
310
+ * is no such record. */
311
+ export function fieldsFromTla(declared, value, declaration) {
312
+ try {
313
+ return decodeFields(declared, toData(value), declaration.vocabulary);
314
+ }
315
+ catch (error) {
316
+ if (error instanceof GuardError)
317
+ return null;
318
+ throw error;
319
+ }
320
+ }
321
+ /** A TLA+ event record as a call, or null where it is no declared
322
+ * event with its params. */
323
+ export function callFromTla(value, declaration) {
324
+ if (typeof value !== "object" || !("r" in value))
325
+ return null;
326
+ const { event, ...params } = value.r;
327
+ if (typeof event !== "string" || !Object.hasOwn(declaration.events, event))
328
+ return null;
329
+ const fields = fieldsFromTla(declaration.events[event].params, canon({ r: params }), declaration);
330
+ return fields === null ? null : { event, params: fields };
331
+ }
332
+ /** A TLA+ value as a report prints it where it is no value of a
333
+ * declared type: `none`, `{a, b}`, `<a, b>`, `{k: v}`. */
334
+ export function showTla(value) {
335
+ if (typeof value === "string")
336
+ return showText(value);
337
+ if (typeof value !== "object")
338
+ return String(value);
339
+ if ("m" in value)
340
+ return value.m === "Missing" ? "none" : value.m;
341
+ if ("s" in value)
342
+ return `{${value.s.map(showTla).join(", ")}}`;
343
+ if ("t" in value)
344
+ return `<${value.t.map(showTla).join(", ")}>`;
345
+ if ("r" in value) {
346
+ return `{${Object.entries(value.r)
347
+ .map(([name, field]) => `${showText(name)}: ${showTla(field)}`)
348
+ .join(", ")}}`;
349
+ }
350
+ return `{${value.f.map(([k, v]) => `${showTla(k)}: ${showTla(v)}`).join(", ")}}`;
351
+ }
352
+ //# sourceMappingURL=tla.js.map
@@ -0,0 +1,81 @@
1
+ /** The proof system as found: the program, the version it names,
2
+ * its standard modules' folder, and where it was found. */
3
+ export type ProofSystem = {
4
+ kind: "found";
5
+ program: string;
6
+ version: string;
7
+ /** What `tlapm --where` names: the folder of its standard
8
+ * modules, TLAPS.tla among them. */
9
+ stdlib: string;
10
+ /** The solvers it would run, each as it names it with its
11
+ * version, in the order it lists them; none where it lists none. */
12
+ solvers: string[];
13
+ /** Its bytes are a pinned release's. */
14
+ pinned: boolean;
15
+ /** The variable that named it, where one did. */
16
+ variable: string | null;
17
+ } | {
18
+ kind: "none";
19
+ cause: string;
20
+ };
21
+ /** The variable that names a proof system to use. */
22
+ export declare const TLAPM_VARIABLE = "SPEX_TLAPM";
23
+ /**
24
+ * The releases Spex pins, by host (`<platform>-<arch>` as Node names
25
+ * them). None is pinned (DR-252): the proof system's maintainers
26
+ * publish a rolling build, replaced under one name, so a host finds it
27
+ * through the variable alone and a result names the version it reports.
28
+ */
29
+ export declare const TLAPM_PINS: Readonly<Record<string, {
30
+ version: string;
31
+ url: string;
32
+ sha256: string;
33
+ }>>;
34
+ /** The environment the proof system runs in: the host's, without
35
+ * the variables through which tlapm takes options (`TLAPM_CACHE_DIR`,
36
+ * `TLAPM_SMT_SOLVER` and the like). */
37
+ export declare function tlapmEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
38
+ /** How a program ended that did not end well: with its status, or,
39
+ * where a signal ended it and it has none, before it finished. */
40
+ export declare function endedAs(status: number | null): string;
41
+ /**
42
+ * The solvers a proof system would run, read from what `--config`
43
+ * prints: each line `<name> version == "<version>"` in order, without
44
+ * the line of `SMT`, its name for the solver it takes by default,
45
+ * which another line names. None where it prints no such line.
46
+ */
47
+ export declare function solversOf(printed: string): string[];
48
+ /** Find the proof system. It never throws: without one the proofs
49
+ * are not accepted, and the rest of a guard is checked all the same. */
50
+ export declare function findProofSystem(env?: NodeJS.ProcessEnv): ProofSystem;
51
+ /** The words of the command line after the program, for the proof
52
+ * file `<proofs>.tla` and the module of its statements `<proved>.tla`
53
+ * in the folder it runs in. Every path is relative to that folder. */
54
+ export declare function tlapmArgs(proofs: string, proved: string): string[];
55
+ /** A place tlapm names in a module. */
56
+ export type TlapmPlace = {
57
+ module: string;
58
+ line: number;
59
+ col: number;
60
+ };
61
+ /** What a run of tlapm said of the modules it was given. */
62
+ export type TlapmVerdict = {
63
+ kind: "proved";
64
+ }
65
+ /** It did not prove everything: the first thing it reports, with
66
+ * its place where it names one. */
67
+ | {
68
+ kind: "not proved";
69
+ what: "failed" | "omitted" | "missing" | "said";
70
+ place: TlapmPlace | null;
71
+ words: string;
72
+ };
73
+ /**
74
+ * Read what tlapm printed, on both its outputs in order, for the
75
+ * modules it was given. They are proved only where it exited 0 and,
76
+ * for each module, printed the line of the module's span followed by
77
+ * `[INFO]: All K obligation(s) proved.` with K at least 1, and
78
+ * printed no line of an error, of a proof omitted or missing, or of a
79
+ * proof incomplete.
80
+ */
81
+ export declare function tlapmVerdict(printed: string, status: number | null, modules: readonly string[]): TlapmVerdict;