@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,58 @@
1
+ import type { Book, Run, Sequence, SetValues } from "./records.js";
2
+ import { type Case, type Vocabulary } from "./types.js";
3
+ /**
4
+ * What each of Spex's files for TLC defines, right after its
5
+ * `EXTENDS` line, for the configuration file to bind the
6
+ * declaration's parameters to: the declared values of a set the
7
+ * configuration file cannot write, and for each ordered set its
8
+ * declared values in their order with the order they give. Each
9
+ * definition comes after an empty line; nothing where no set needs
10
+ * one.
11
+ */
12
+ export declare function valuesLines(vocabulary: Vocabulary): string[];
13
+ /**
14
+ * What every configuration file starts with: `Missing` as a value of
15
+ * its own, equal to nothing else; each values set as its declared
16
+ * values, so TLC searches with those; and each set's order as the
17
+ * order the binding lists them in.
18
+ */
19
+ export declare function valuesCfg(vocabulary: Vocabulary): string;
20
+ /** The configuration of a file TLC only evaluates. */
21
+ export declare function evaluateCfg(vocabulary: Vocabulary): string;
22
+ export declare const FORM_MODULE = "SpexForm";
23
+ export declare function formModule(name: string, vocabulary: Vocabulary): string;
24
+ export declare const CASES_MODULE = "SpexCases";
25
+ /** What one `ASSUME` line of the cases file evaluates: a case under a
26
+ * translation, by the case's number from 1, or whether the rule
27
+ * applied in a permitted case (`case` null). */
28
+ export type Assumed = {
29
+ translation: string;
30
+ case: number | null;
31
+ };
32
+ /**
33
+ * The file that evaluates each case of the guard file under each
34
+ * translation. `assumes` gives, by line number from 1, what each of
35
+ * its `ASSUME` lines evaluates, so a line TLC could not evaluate
36
+ * names its translation and its case.
37
+ */
38
+ export declare function casesModule(names: readonly string[], cases: readonly Case[], vocabulary: Vocabulary): {
39
+ text: string;
40
+ assumes: Map<number, Assumed>;
41
+ };
42
+ export declare const COMPARE_MODULE = "SpexCompare";
43
+ export declare function compareCfg(vocabulary: Vocabulary): string;
44
+ export declare function compareModule(names: readonly string[], vocabulary: Vocabulary): string;
45
+ export declare const MODEL_MODULE = "SpexModel";
46
+ export declare function modelCfg(vocabulary: Vocabulary): string;
47
+ export declare function modelModule(name: string, vocabulary: Vocabulary): string;
48
+ export declare const RECORDS_MODULE = "SpexRecords";
49
+ /** The configuration of the file that judges the records: `Missing`
50
+ * alone, since every value stands in the TLA+ file. */
51
+ export declare function recordsCfg(): string;
52
+ /**
53
+ * The file that judges one run's sequences (guards-54): each set over
54
+ * the copy's values, the translation instantiated over them, each
55
+ * different record of the run once, each sequence as the places of its
56
+ * handed records, and the walk. `runs` is how many runs the copy has.
57
+ */
58
+ export declare function recordsModule(translation: string, vocabulary: Vocabulary, sequences: readonly Sequence[], run: Run, values: Readonly<Record<string, SetValues>>, runs: number, book: Book): string;
@@ -0,0 +1,420 @@
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: the TLA+ files Spex writes
5
+ * around a guard's translations, and their configuration files. Each
6
+ * is fixed text with the translations' names, the cases' steps or the
7
+ * records of the code written in. TLC makes every judgement from
8
+ * them; nothing here evaluates TLA+.
9
+ */
10
+ import { recordsNames, setName, spexNames } from "./declaration.js";
11
+ import { callToTla, fieldsToTla, tlaString, tlaText } from "./tla.js";
12
+ import { showOrder } from "./types.js";
13
+ // ---------------------------------------------------------------
14
+ // The declared values
15
+ // ---------------------------------------------------------------
16
+ /** Whether a configuration file can write a set's declared values:
17
+ * TLC reads a string there with its backslashes kept, so a value
18
+ * holding `\` or `"` cannot be written in one. */
19
+ function cfgWritable(members) {
20
+ return members.every((member) => !/["\\]/.test(member));
21
+ }
22
+ /**
23
+ * What each of Spex's files for TLC defines, right after its
24
+ * `EXTENDS` line, for the configuration file to bind the
25
+ * declaration's parameters to: the declared values of a set the
26
+ * configuration file cannot write, and for each ordered set its
27
+ * declared values in their order with the order they give. Each
28
+ * definition comes after an empty line; nothing where no set needs
29
+ * one.
30
+ */
31
+ export function valuesLines(vocabulary) {
32
+ const lines = [];
33
+ for (const [set, members] of Object.entries(vocabulary.sets)) {
34
+ const [values, order, before] = spexNames(set);
35
+ if (!cfgWritable(members)) {
36
+ lines.push("", `\\* values.${set} as the binding declares them, which a configuration file cannot write.`, `${values} == {${members.map(tlaString).join(", ")}}`);
37
+ }
38
+ if (Object.hasOwn(vocabulary.orders, set)) {
39
+ lines.push("", `\\* values.${set} in the order the binding declares.`, `${order} == <<${members.map(tlaString).join(", ")}>>`, `${before}(x, y) ==`, ` \\E i, j \\in 1..Len(${order}) :`, ` i < j /\\ ${order}[i] = x /\\ ${order}[j] = y`);
40
+ }
41
+ }
42
+ return lines;
43
+ }
44
+ /** `valuesLines` as text to follow an `EXTENDS` line. */
45
+ function valuesText(vocabulary) {
46
+ return valuesLines(vocabulary)
47
+ .map((line) => `\n${line}`)
48
+ .join("");
49
+ }
50
+ /**
51
+ * What every configuration file starts with: `Missing` as a value of
52
+ * its own, equal to nothing else; each values set as its declared
53
+ * values, so TLC searches with those; and each set's order as the
54
+ * order the binding lists them in.
55
+ */
56
+ export function valuesCfg(vocabulary) {
57
+ const lines = ["CONSTANT Missing = Missing"];
58
+ for (const [set, members] of Object.entries(vocabulary.sets)) {
59
+ const name = setName(set);
60
+ const [values, , before] = spexNames(set);
61
+ lines.push(cfgWritable(members)
62
+ ? `CONSTANT ${name} = {${members.map(tlaString).join(", ")}}`
63
+ : `CONSTANT ${name} <- ${values}`);
64
+ if (Object.hasOwn(vocabulary.orders, set))
65
+ lines.push(`CONSTANT ${name}Before <- ${before}`);
66
+ }
67
+ return `${lines.join("\n")}\n`;
68
+ }
69
+ /** The configuration of a file TLC only evaluates. */
70
+ export function evaluateCfg(vocabulary) {
71
+ return valuesCfg(vocabulary);
72
+ }
73
+ function instances(names) {
74
+ return names.map((name) => `T_${name} == INSTANCE ${name}`).join("\n");
75
+ }
76
+ function record(names, each) {
77
+ return `[${names.map((name) => `${name} |-> ${each(name)}`).join(", ")}]`;
78
+ }
79
+ // ---------------------------------------------------------------
80
+ // 0. Form: one translation alone
81
+ // ---------------------------------------------------------------
82
+ export const FORM_MODULE = "SpexForm";
83
+ export function formModule(name, vocabulary) {
84
+ return `---- MODULE ${FORM_MODULE} ----
85
+ \\* Generated by Spex. One translation alone: it parses, and its seven operators take their parameters.
86
+ EXTENDS Declaration${valuesText(vocabulary)}
87
+
88
+ T == INSTANCE ${name}
89
+
90
+ Form(s, e, f) ==
91
+ <<T!Start, T!Legal(s, e), T!Effect(s, e), T!After(s, e), T!When(e), T!While(s, e), T!Shall(s, e, f)>>
92
+ ====
93
+ `;
94
+ }
95
+ // ---------------------------------------------------------------
96
+ // 1. Cases, without the code
97
+ // ---------------------------------------------------------------
98
+ export const CASES_MODULE = "SpexCases";
99
+ function walk(name) {
100
+ return `RECURSIVE Walk_${name}(_, _, _, _, _)
101
+ Walk_${name}(steps, forbidden, i, s, applied) ==
102
+ IF i > Len(steps) THEN [pass |-> TRUE, applied |-> applied]
103
+ ELSE
104
+ LET e == steps[i].event
105
+ stated == steps[i].stated
106
+ IN
107
+ IF e \\notin Events THEN [pass |-> FALSE, step |-> i, why |-> "event not declared"]
108
+ ELSE IF {f \\in Effects(e) : Agrees(f, stated)} = {} THEN [pass |-> FALSE, step |-> i, why |-> "no declared effect is as stated"]
109
+ ELSE IF ~T_${name}!Legal(s, e) THEN [pass |-> FALSE, step |-> i, why |-> "not legal"]
110
+ ELSE
111
+ LET applies == T_${name}!When(e) /\\ T_${name}!While(s, e) IN
112
+ IF forbidden /\\ i = Len(steps)
113
+ THEN
114
+ IF ~applies THEN [pass |-> FALSE, step |-> i, why |-> "rule does not apply"]
115
+ ELSE
116
+ LET open == {f \\in Effects(e) : Agrees(f, stated) /\\ T_${name}!Shall(s, e, f)} IN
117
+ IF open = {} THEN [pass |-> TRUE, applied |-> applied]
118
+ ELSE [pass |-> FALSE, step |-> i, why |-> "not forbidden", allowed |-> open]
119
+ ELSE
120
+ LET f == T_${name}!Effect(s, e) IN
121
+ IF f \\notin Effects(e) THEN [pass |-> FALSE, step |-> i, why |-> "effect not declared", effect |-> f]
122
+ ELSE IF ~Agrees(f, stated) THEN [pass |-> FALSE, step |-> i, why |-> "effect differs", effect |-> f]
123
+ ELSE IF applies /\\ ~T_${name}!Shall(s, e, f) THEN [pass |-> FALSE, step |-> i, why |-> "forbidden", effect |-> f]
124
+ ELSE Walk_${name}(steps, forbidden, i + 1, T_${name}!After(s, e), IF applies THEN applied \\cup {i} ELSE applied)`;
125
+ }
126
+ /**
127
+ * The file that evaluates each case of the guard file under each
128
+ * translation. `assumes` gives, by line number from 1, what each of
129
+ * its `ASSUME` lines evaluates, so a line TLC could not evaluate
130
+ * names its translation and its case.
131
+ */
132
+ export function casesModule(names, cases, vocabulary) {
133
+ const lines = [
134
+ `---- MODULE ${CASES_MODULE} ----`,
135
+ "\\* Generated by Spex. Each case of the guard file under each translation, without the code.",
136
+ "EXTENDS Declaration, TLC",
137
+ ...valuesLines(vocabulary),
138
+ "",
139
+ ...instances(names).split("\n"),
140
+ "",
141
+ "\\* The effect f is as a step states: equal in every field the step states.",
142
+ "Agrees(f, stated) == \\A k \\in DOMAIN stated : k \\in DOMAIN f /\\ f[k] = stated[k]",
143
+ "",
144
+ ];
145
+ cases.forEach((entry, at) => {
146
+ lines.push(`\\* ${entry.kind}: ${entry.label.replace(/[^\x20-\x7e]/g, "?")}`, `Case_${at + 1} == <<`);
147
+ entry.steps.forEach((step, index) => {
148
+ const comma = index === entry.steps.length - 1 ? "" : ",";
149
+ lines.push(` [event |-> ${tlaText(callToTla(step))}, stated |-> ${tlaText(fieldsToTla(step.effect))}]${comma}`);
150
+ });
151
+ lines.push(">>", "");
152
+ });
153
+ for (const name of names) {
154
+ lines.push(...walk(name).split("\n"), "");
155
+ cases.forEach((entry, at) => {
156
+ const forbidden = entry.kind === "forbidden" ? "TRUE" : "FALSE";
157
+ lines.push(`Result_${name}_${at + 1} == Walk_${name}(Case_${at + 1}, ${forbidden}, 1, T_${name}!Start, {})`);
158
+ });
159
+ const permitted = cases.flatMap((entry, at) => (entry.kind === "permitted" ? [at + 1] : []));
160
+ const applied = permitted.length === 0
161
+ ? "FALSE"
162
+ : permitted.map((k) => `(Result_${name}_${k}.pass /\\ Result_${name}_${k}.applied # {})`).join(" \\/ ");
163
+ lines.push(`Applied_${name} == ${applied}`, "");
164
+ }
165
+ const assumes = new Map();
166
+ for (const name of names) {
167
+ cases.forEach((_, at) => {
168
+ lines.push(`ASSUME PrintT(<<"case", ${tlaString(name)}, ${at + 1}, Result_${name}_${at + 1}>>)`);
169
+ assumes.set(lines.length, { translation: name, case: at + 1 });
170
+ });
171
+ lines.push(`ASSUME PrintT(<<"applied", ${tlaString(name)}, Applied_${name}>>)`);
172
+ assumes.set(lines.length, { translation: name, case: null });
173
+ }
174
+ lines.push("====", "");
175
+ return { text: lines.join("\n"), assumes };
176
+ }
177
+ // ---------------------------------------------------------------
178
+ // 2. Translations against each other
179
+ // ---------------------------------------------------------------
180
+ export const COMPARE_MODULE = "SpexCompare";
181
+ export function compareCfg(vocabulary) {
182
+ return `${valuesCfg(vocabulary)}INIT Init\nNEXT Next\nINVARIANT Agree\nCHECK_DEADLOCK FALSE\n`;
183
+ }
184
+ export function compareModule(names, vocabulary) {
185
+ const first = names[0];
186
+ return `---- MODULE ${COMPARE_MODULE} ----
187
+ \\* Generated by Spex. The translations side by side over every sequence of events.
188
+ EXTENDS Declaration${valuesText(vocabulary)}
189
+
190
+ ${instances(names)}
191
+
192
+ VARIABLES states, step, diff
193
+
194
+ ${names
195
+ .map((name) => `Applies_${name}(s, e) == T_${name}!When(e) /\\ T_${name}!While(s, e)\nForbidden_${name}(s, e, f) == Applies_${name}(s, e) /\\ ~T_${name}!Shall(s, e, f)`)
196
+ .join("\n")}
197
+
198
+ \\* Each translation's answer, by its name.
199
+ Legal(st, e) == ${record(names, (name) => `T_${name}!Legal(st.${name}, e)`)}
200
+ Effect(st, e) == ${record(names, (name) => `T_${name}!Effect(st.${name}, e)`)}
201
+ Applies(st, e) == ${record(names, (name) => `Applies_${name}(st.${name}, e)`)}
202
+ Forbidden(st, e, f) == ${record(names, (name) => `Forbidden_${name}(st.${name}, e, f)`)}
203
+ After(st, e) == ${record(names, (name) => `T_${name}!After(st.${name}, e)`)}
204
+
205
+ \\* Whether each translation's effect is one the event is declared to give.
206
+ Declared(st, e) == ${record(names, (name) => `T_${name}!Effect(st.${name}, e) \\in Effects(e)`)}
207
+
208
+ Same(answers) == \\A x, y \\in DOMAIN answers : answers[x] = answers[y]
209
+
210
+ \\* The first thing the translations answer differently about the event e, or Missing:
211
+ \\* whether it is legal, the effect it gives, whether the rule applies,
212
+ \\* then, for every declared effect, whether the rule forbids it.
213
+ \\* An effect that is no declared effect is written down before any is compared.
214
+ Difference(st, e) ==
215
+ IF ~Same(Legal(st, e)) THEN [on |-> "legal", event |-> e, answers |-> Legal(st, e)]
216
+ ELSE IF ~Legal(st, e).${first} THEN Missing
217
+ ELSE IF \\E x \\in DOMAIN Declared(st, e) : ~Declared(st, e)[x]
218
+ THEN [on |-> "undeclared", event |-> e, answers |-> Effect(st, e), declared |-> Declared(st, e)]
219
+ ELSE IF ~Same(Effect(st, e)) THEN [on |-> "effect", event |-> e, answers |-> Effect(st, e)]
220
+ ELSE IF ~Same(Applies(st, e)) THEN [on |-> "applies", event |-> e, answers |-> Applies(st, e)]
221
+ ELSE
222
+ LET split == {f \\in Effects(e) : ~Same(Forbidden(st, e, f))} IN
223
+ IF split = {} THEN Missing
224
+ ELSE [on |-> "forbidden", event |-> e, model |-> Effect(st, e).${first},
225
+ answers |-> [f \\in split |-> Forbidden(st, e, f)]]
226
+
227
+ Init ==
228
+ /\\ states = ${record(names, (name) => `T_${name}!Start`)}
229
+ /\\ step = Missing
230
+ /\\ diff = Missing
231
+
232
+ \\* An event is chosen: a difference about it is written down, or it is taken as the next step.
233
+ Take ==
234
+ /\\ step = Missing
235
+ /\\ diff = Missing
236
+ /\\ \\E e \\in Events :
237
+ \\/ /\\ Difference(states, e) # Missing
238
+ /\\ diff' = Difference(states, e)
239
+ /\\ UNCHANGED <<states, step>>
240
+ \\/ /\\ Difference(states, e) = Missing
241
+ /\\ Legal(states, e).${first}
242
+ /\\ step' = [event |-> e, effect |-> Effect(states, e).${first}]
243
+ /\\ UNCHANGED <<states, diff>>
244
+
245
+ \\* The step chosen is made in every translation.
246
+ Land ==
247
+ /\\ step # Missing
248
+ /\\ states' = After(states, step.event)
249
+ /\\ step' = Missing
250
+ /\\ UNCHANGED diff
251
+
252
+ Next == Take \\/ Land
253
+
254
+ Agree == diff = Missing
255
+ ====
256
+ `;
257
+ }
258
+ // ---------------------------------------------------------------
259
+ // 3. On the model
260
+ // ---------------------------------------------------------------
261
+ export const MODEL_MODULE = "SpexModel";
262
+ export function modelCfg(vocabulary) {
263
+ return `${valuesCfg(vocabulary)}INIT Init\nNEXT Next\nINVARIANT Declared\nINVARIANT Kept\nCHECK_DEADLOCK FALSE\n`;
264
+ }
265
+ export function modelModule(name, vocabulary) {
266
+ return `---- MODULE ${MODEL_MODULE} ----
267
+ \\* Generated by Spex. Every state the model reaches, every legal event, against the rule.
268
+ EXTENDS Declaration${valuesText(vocabulary)}
269
+
270
+ M == INSTANCE ${name}
271
+
272
+ VARIABLES state, step
273
+
274
+ Applies(s, e) == M!When(e) /\\ M!While(s, e)
275
+ Forbidden(s, e, f) == Applies(s, e) /\\ ~M!Shall(s, e, f)
276
+
277
+ \\* One step of the model, with whether the rule applies at it.
278
+ Row(s, e) ==
279
+ [ from |-> s,
280
+ event |-> e,
281
+ effect |-> M!Effect(s, e),
282
+ next |-> M!After(s, e),
283
+ applies |-> Applies(s, e) ]
284
+
285
+ Init ==
286
+ /\\ state = M!Start
287
+ /\\ step = Missing
288
+
289
+ \\* A legal event is chosen and its step written down.
290
+ Take ==
291
+ /\\ step = Missing
292
+ /\\ \\E e \\in Events : M!Legal(state, e) /\\ step' = Row(state, e)
293
+ /\\ UNCHANGED state
294
+
295
+ \\* The model moves to the state after it.
296
+ Land ==
297
+ /\\ step # Missing
298
+ /\\ state' = step.next
299
+ /\\ step' = Missing
300
+
301
+ Next == Take \\/ Land
302
+
303
+ \\* The model's effect is one the event is declared to give.
304
+ Declared == step # Missing => step.effect \\in Effects(step.event)
305
+
306
+ \\* The rule does not forbid the model's effect.
307
+ Kept == step # Missing => ~Forbidden(step.from, step.event, step.effect)
308
+ ====
309
+ `;
310
+ }
311
+ // ---------------------------------------------------------------
312
+ // 4. Judging the records of the code
313
+ // ---------------------------------------------------------------
314
+ export const RECORDS_MODULE = "SpexRecords";
315
+ /** The configuration of the file that judges the records: `Missing`
316
+ * alone, since every value stands in the TLA+ file. */
317
+ export function recordsCfg() {
318
+ return "CONSTANT Missing = Missing\n";
319
+ }
320
+ /** A name for a `\*` comment: every character outside printable ASCII
321
+ * written `?`. */
322
+ function commentText(text) {
323
+ return text.replace(/[^\x20-\x7e]/g, "?");
324
+ }
325
+ /** What follows the sequences in every file that judges records. */
326
+ const WALK = `Applies(s, e) == M!When(e) /\\ M!While(s, e)
327
+
328
+ \\* What the model says of one record, the event e with the effect f, in the state s:
329
+ \\* the first of these that holds.
330
+ Said(s, e, f) ==
331
+ IF e \\notin M!Events \\/ f \\notin M!Effects(e) THEN "not declared"
332
+ ELSE IF ~M!Legal(s, e) THEN "not legal"
333
+ ELSE IF Applies(s, e) /\\ ~M!Shall(s, e, f) THEN "forbidden"
334
+ ELSE IF f # M!Effect(s, e) THEN "differs"
335
+ ELSE "matched"
336
+
337
+ \\* A walk: w.state is the model's state, w.matched records were matched, the rule applied
338
+ \\* at w.applied records, w.said is what the model says of the last record judged, and
339
+ \\* w.model is the model's effect there where that record is not matched and its event is legal.
340
+ \\* One record more, r, with what the model says of it, said.
341
+ Step(w, r, said) ==
342
+ [ state |-> IF said = "matched" THEN M!After(w.state, r[1]) ELSE w.state,
343
+ matched |-> IF said = "matched" THEN w.matched + 1 ELSE w.matched,
344
+ applied |-> IF said \\in {"matched", "forbidden", "differs"} /\\ Applies(w.state, r[1])
345
+ THEN w.applied + 1 ELSE w.applied,
346
+ said |-> said,
347
+ model |-> IF said \\in {"forbidden", "differs"} THEN M!Effect(w.state, r[1]) ELSE Missing ]
348
+
349
+ One(w, r) == Step(w, r, Said(w.state, r[1], r[2]))
350
+
351
+ \\* The records lo to hi of the sequence q, in order, no further than the first that is
352
+ \\* not matched: the first half, then the second from where the first ended. A sequence
353
+ \\* is halved so that TLC's evaluation stays shallow however long it is.
354
+ RECURSIVE Walk(_, _, _, _)
355
+ Walk(q, lo, hi, w) ==
356
+ IF lo > hi \\/ w.said # "matched" THEN w
357
+ ELSE IF lo = hi THEN One(w, SpexRecord[q[lo]])
358
+ ELSE Walk(q, (lo + hi) \\div 2 + 1, hi, Walk(q, lo, (lo + hi) \\div 2, w))
359
+
360
+ \\* One sequence from the model's start: how many of its records were matched, at how many
361
+ \\* the rule applied, what the model says of the first not matched, and the model's effect there.
362
+ Judged(q) ==
363
+ LET w == Walk(q, 1, Len(q), [state |-> M!Start, matched |-> 0, applied |-> 0, said |-> "matched", model |-> Missing])
364
+ IN <<w.matched, w.applied, w.said, w.model>>
365
+
366
+ \\* TLC walks each sequence in turn and prints what it found, under the sequence's number.
367
+ ASSUME \\A k \\in 1..Len(SpexSequence) : PrintT(<<"sequence", k, Judged(SpexSequence[k])>>)`;
368
+ /**
369
+ * The file that judges one run's sequences (guards-54): each set over
370
+ * the copy's values, the translation instantiated over them, each
371
+ * different record of the run once, each sequence as the places of its
372
+ * handed records, and the walk. `runs` is how many runs the copy has.
373
+ */
374
+ export function recordsModule(translation, vocabulary, sequences, run, values, runs, book) {
375
+ const out = [
376
+ `---- MODULE ${RECORDS_MODULE} ----`,
377
+ `\\* Generated by Spex. The records of the code, each sequence walked through the model of translation ${translation}.`,
378
+ ...(runs === 1 ? [] : [`\\* Sequences ${run.from + 1} to ${run.to} of ${sequences.length}.`]),
379
+ "EXTENDS Integers, FiniteSets, Sequences, TLC",
380
+ "",
381
+ "\\* The value of a field of a type `T?` that holds nothing.",
382
+ "CONSTANT Missing",
383
+ ];
384
+ const withs = [];
385
+ for (const set of Object.keys(vocabulary.sets)) {
386
+ const name = setName(set);
387
+ const [valuesName, orderName, beforeName] = spexNames(set);
388
+ const [placeName, onceName, notMissingName] = recordsNames(set);
389
+ const { values: all, others } = values[set];
390
+ const rows = [];
391
+ for (let at = 0; at < all.length; at += 8)
392
+ rows.push(` ${all.slice(at, at + 8).map(tlaString).join(", ")}`);
393
+ const more = others === 0 ? "its declared values" : `its declared values and the ${others} other${others === 1 ? "" : "s"} the records hold`;
394
+ const order = Object.hasOwn(vocabulary.orders, set) ? vocabulary.orders[set] : undefined;
395
+ if (order === undefined) {
396
+ out.push("", `\\* values.${set}: ${more}.`, `${valuesName} == {`, rows.join(",\n"), "}", `ASSUME ${notMissingName} == Missing \\notin ${valuesName}`);
397
+ withs.push(`${name} <- ${valuesName}`);
398
+ continue;
399
+ }
400
+ out.push("", `\\* values.${set}: ${more},`, `\\* in the order the binding declares, ${showOrder(order)}. One value comes before another where its`, "\\* place in this list is lower: a strict total order, since TLC finds no value twice in it.", `${orderName} == <<`, rows.join(",\n"), ">>", `${valuesName} == {${orderName}[i] : i \\in 1..Len(${orderName})}`, `ASSUME ${onceName} == Cardinality(${valuesName}) = Len(${orderName})`, `ASSUME ${notMissingName} == Missing \\notin ${valuesName}`, `${placeName} ==`, ` [x \\in ${valuesName} |-> CHOOSE i \\in 1..Len(${orderName}) : ${orderName}[i] = x]`, `${beforeName}(x, y) == ${placeName}[x] < ${placeName}[y]`);
401
+ withs.push(`${name} <- ${valuesName}`, `${name}Before <- ${beforeName}`);
402
+ }
403
+ out.push("", `\\* The model and the rule: translation ${translation}, over those values.`, `M == INSTANCE ${translation}${withs.length === 0 ? "" : ` WITH ${withs.join(", ")}`}`, "", "\\* Each different record, once: its event with its params, then the effect the code gave.", "SpexRecord == <<");
404
+ const texts = [...run.index.keys()].map((record) => book.records[record].text);
405
+ texts.forEach((text, at) => {
406
+ out.push(` ${text}${at === texts.length - 1 ? "" : ","}${(at + 1) % 10 === 0 ? ` \\* ${at + 1}` : ""}`);
407
+ });
408
+ out.push(">>", "", "\\* Each sequence, from a fresh start: its records in order, each by its place in SpexRecord.", "SpexSequence == <<");
409
+ for (let at = run.from; at < run.to; at += 1) {
410
+ const sequence = sequences[at];
411
+ const places = sequence.handed.map((record) => run.index.get(record));
412
+ const rows = [];
413
+ for (let k = 0; k < places.length; k += 24)
414
+ rows.push(places.slice(k, k + 24).join(", "));
415
+ out.push(` \\* ${at - run.from + 1}: ${commentText(sequence.name)}${sequence.why === null ? "" : `; not whole: ${sequence.why}`}`, ` <<${rows.join(",\n ")}>>${at === run.to - 1 ? "" : ","}`);
416
+ }
417
+ out.push(">>", "", WALK, "====", "");
418
+ return out.join("\n");
419
+ }
420
+ //# sourceMappingURL=harness.js.map
@@ -0,0 +1,61 @@
1
+ /** One spec item as a guard uses it. */
2
+ export type Item = {
3
+ id: string;
4
+ /** Its file, relative to the root (starts with `specs/`). */
5
+ file: string;
6
+ /** 1-based line of its heading. */
7
+ line: number;
8
+ /** `Verification`, `External Behavior`, `Internal Behavior`, … */
9
+ section: string | null;
10
+ /** From its heading line to the line before the next heading of
11
+ * the same or a shallower depth, trailing blank lines dropped, line
12
+ * endings as LF, no newline at the end. Approval digests this. */
13
+ text: string;
14
+ /** Its body's first paragraph on one line, each Markdown link
15
+ * replaced by its text, so a citation reads `[<id>]`. */
16
+ statement: string;
17
+ /** The IDs of the spec items its body cites, sorted, each once. */
18
+ cites: string[];
19
+ };
20
+ /** Every spec item of a tree, by ID. */
21
+ export type ItemIndex = {
22
+ byId: ReadonlyMap<string, readonly Item[]>;
23
+ };
24
+ /** One case of a guard item: an entry of its list, in plain words. */
25
+ export type ItemCase = {
26
+ kind: "permitted" | "forbidden";
27
+ /** As written, compared character by character. */
28
+ label: string;
29
+ /** On one line, each Markdown link as its text, without the `;` or
30
+ * `.` that ends the entry. */
31
+ words: string;
32
+ };
33
+ /** The items one guard reads. */
34
+ export type GuardItems = {
35
+ /** The guard item: the Verification item the guard is named by. */
36
+ guard: Item;
37
+ /** The behavior item it checks: the rule. */
38
+ behavior: Item;
39
+ /** The guard item's cases, in the item's order. */
40
+ cases: ItemCase[];
41
+ /** The items the guard item and the behavior item cite directly,
42
+ * other than those two. In ID order. */
43
+ cited: Item[];
44
+ };
45
+ /** Read every spec item under `<root>/specs`. */
46
+ export declare function readItems(root: string): ItemIndex;
47
+ /**
48
+ * The cases of a guard item (guards-37), read from its text: every
49
+ * list item with no indent that starts `permitted — ` or
50
+ * `forbidden — `, with the indented lines that follow it. A list
51
+ * item a reader would take for a case and that is not written so is
52
+ * an error, so that no case the item shows goes unchecked; any other
53
+ * list item is no case. Every such error is a GuardError starting
54
+ * with the item's ID.
55
+ */
56
+ export declare function readCases(item: Item): ItemCase[];
57
+ /**
58
+ * The items of guard `guardId` (guards-2). Every error is a
59
+ * GuardError starting with the ID of the item at fault.
60
+ */
61
+ export declare function guardItems(index: ItemIndex, guardId: string): GuardItems;