tla-ts 0.0.3-dev.f362080 → 0.0.3-dev.fac17cc

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 (81) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +149 -8
  5. package/dist/index.d.ts +149 -8
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -20
  12. package/dist/run-cli.js +1351 -20
  13. package/dist/transfer.cjs +86 -28
  14. package/dist/transfer.js +86 -28
  15. package/package.json +5 -1
  16. package/src/ast.ts +56 -6
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -11
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1348 -227
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +252 -179
  25. package/src/environment.ts +431 -120
  26. package/src/errors.ts +210 -5
  27. package/src/evaluate.ts +716 -136
  28. package/src/exit-codes.ts +5 -4
  29. package/src/index.ts +1 -0
  30. package/src/instances.ts +4 -2
  31. package/src/java-overrides.ts +104 -25
  32. package/src/levels.ts +183 -37
  33. package/src/lexer.ts +32 -4
  34. package/src/live-check.ts +204 -146
  35. package/src/liveness.ts +192 -65
  36. package/src/obligation-expression.ts +2 -2
  37. package/src/obligation-source.ts +2 -0
  38. package/src/parallel-worker.ts +6 -1
  39. package/src/parallel.ts +57 -6
  40. package/src/parser.ts +425 -144
  41. package/src/pcal-ast.ts +770 -0
  42. package/src/pcal-parse.ts +1523 -0
  43. package/src/pcal-tokenize.ts +516 -0
  44. package/src/pcal-translate.ts +1230 -0
  45. package/src/pcal-validate.ts +454 -0
  46. package/src/randomization.ts +24 -7
  47. package/src/resolve.ts +1408 -400
  48. package/src/run-cli.ts +1167 -155
  49. package/src/runtime.ts +16 -0
  50. package/src/sany-builtins.ts +147 -0
  51. package/src/sany-generator.ts +3112 -0
  52. package/src/sany-levels.ts +1208 -0
  53. package/src/sany-lexer.ts +545 -0
  54. package/src/sany-linter.ts +122 -0
  55. package/src/sany-modules.ts +1398 -0
  56. package/src/sany-nodes.ts +463 -0
  57. package/src/sany-parse-error.ts +2161 -0
  58. package/src/sany-report.ts +354 -0
  59. package/src/sany-selectors.ts +738 -0
  60. package/src/sany-source.ts +248 -0
  61. package/src/sany-xml.ts +1017 -0
  62. package/src/spec-description.ts +26 -7
  63. package/src/standard-modules.ts +203 -146
  64. package/src/standard-ranges.ts +116 -0
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +221 -161
  67. package/src/substitution-levels.ts +38 -17
  68. package/src/successors.ts +162 -114
  69. package/src/symmetry.ts +168 -50
  70. package/src/tableau.ts +3 -1
  71. package/src/tlc-ext.ts +13 -17
  72. package/src/tlc-messages.ts +329 -0
  73. package/src/tlc-module.ts +49 -21
  74. package/src/tlc-modules.ts +0 -4
  75. package/src/tlc-order.ts +55 -9
  76. package/src/tlc-random.ts +28 -3
  77. package/src/tlc-replay.ts +655 -0
  78. package/src/tlc-version.ts +18 -0
  79. package/src/transfer.ts +15 -3
  80. package/src/value-sources.ts +29 -0
  81. package/src/values.ts +683 -256
@@ -0,0 +1,1398 @@
1
+ import type { TlcVersion } from "./tlc-version";
2
+ import { REAL_TIME, REALS, TOOLBOX } from "./bundled-modules";
3
+
4
+ const BAGS = `----------------------------- MODULE Bags --------------------------------
5
+ (**************************************************************************)
6
+ (* A bag, also called a multiset, is a set that can contain multiple *)
7
+ (* copies of the same element. A bag can have infinitely many elements, *)
8
+ (* but only finitely many copies of any single element. *)
9
+ (* *)
10
+ (* We represent a bag in the usual way as a function whose range is a *)
11
+ (* subset of the positive integers. An element e belongs to bag B iff e *)
12
+ (* is in the domain of B, in which case bag B contains B[e] copies of e. *)
13
+ (**************************************************************************)
14
+ (**************************************************************************)
15
+ (* All these definitions other than SubBag are overridden by TLC in the *)
16
+ (* Java class tlc2.module.Bags. Each operator is overridden by the Java *)
17
+ (* method with the same name, except that the mapping for TLA+ infix *)
18
+ (* operators is defined in the static block at the beginning of the Java *)
19
+ (* class. *)
20
+ (**************************************************************************)
21
+ LOCAL INSTANCE Naturals
22
+
23
+ IsABag(B) ==
24
+ (************************************************************************)
25
+ (* True iff B is a bag. *)
26
+ (************************************************************************)
27
+ B \\in [DOMAIN B -> {n \\in Nat : n > 0}]
28
+
29
+ BagToSet(B) == DOMAIN B
30
+ (************************************************************************)
31
+ (* The set of elements at least one copy of which is in B. *)
32
+ (************************************************************************)
33
+
34
+ SetToBag(S) == [e \\in S |-> 1]
35
+ (************************************************************************)
36
+ (* The bag that contains one copy of every element of the set S. *)
37
+ (************************************************************************)
38
+
39
+ BagIn(e,B) == e \\in BagToSet(B)
40
+ (************************************************************************)
41
+ (* The \\in operator for bags. *)
42
+ (************************************************************************)
43
+
44
+ EmptyBag == SetToBag({})
45
+
46
+ B1 (+) B2 ==
47
+ (************************************************************************)
48
+ (* The union of bags B1 and B2. *)
49
+ (************************************************************************)
50
+ [e \\in (DOMAIN B1) \\cup (DOMAIN B2) |->
51
+ (IF e \\in DOMAIN B1 THEN B1[e] ELSE 0)
52
+ + (IF e \\in DOMAIN B2 THEN B2[e] ELSE 0) ]
53
+
54
+ B1 (-) B2 ==
55
+ (************************************************************************)
56
+ (* The bag B1 with the elements of B2 removed--that is, with one copy *)
57
+ (* of an element removed from B1 for each copy of the same element in *)
58
+ (* B2. If B2 has at least as many copies of e as B1, then B1 (-) B2 *)
59
+ (* has no copies of e. *)
60
+ (************************************************************************)
61
+ LET B == [e \\in DOMAIN B1 |-> IF e \\in DOMAIN B2 THEN B1[e] - B2[e]
62
+ ELSE B1[e]]
63
+ IN [e \\in {d \\in DOMAIN B : B[d] > 0} |-> B[e]]
64
+
65
+ LOCAL Sum(f) ==
66
+ (******************************************************************)
67
+ (* The sum of f[x] for all x in DOMAIN f. The definition assumes *)
68
+ (* that f is a Nat-valued function and that f[x] equals 0 for all *)
69
+ (* but a finite number of elements x in DOMAIN f. *)
70
+ (******************************************************************)
71
+ LET DSum[S \\in SUBSET DOMAIN f] ==
72
+ LET elt == CHOOSE e \\in S : TRUE
73
+ IN IF S = {} THEN 0
74
+ ELSE f[elt] + DSum[S \\ {elt}]
75
+ IN DSum[DOMAIN f]
76
+
77
+ BagUnion(S) ==
78
+ (************************************************************************)
79
+ (* The bag union of all elements of the set S of bags. *)
80
+ (************************************************************************)
81
+ [e \\in UNION {BagToSet(B) : B \\in S} |->
82
+ Sum( [B \\in S |-> IF BagIn(e, B) THEN B[e] ELSE 0] ) ]
83
+
84
+ B1 \\sqsubseteq B2 ==
85
+ (************************************************************************)
86
+ (* The subset operator for bags. B1 \\sqsubseteq B2 iff, for all e, bag *)
87
+ (* B2 has at least as many copies of e as bag B1 does. *)
88
+ (************************************************************************)
89
+ /\\ (DOMAIN B1) \\subseteq (DOMAIN B2)
90
+ /\\ \\A e \\in DOMAIN B1 : B1[e] \\leq B2[e]
91
+
92
+ SubBag(B) ==
93
+ (************************************************************************)
94
+ (* The set of all subbags of bag B. *)
95
+ (* *)
96
+ (* The following definition is not the one described in the TLA+ book, *)
97
+ (* but rather one that TLC can evaluate. *)
98
+ (************************************************************************)
99
+
100
+ LET RemoveFromDom(x, f) == [y \\in (DOMAIN f) \\ {x} |-> f[y]]
101
+ Combine(x, BagSet) ==
102
+ BagSet \\cup
103
+ {[y \\in (DOMAIN f) \\cup {x} |-> IF y = x THEN i ELSE f[y]] :
104
+ f \\in BagSet, i \\in 1..B[x]}
105
+ Biggest == LET Range == {B[x] : x \\in DOMAIN B}
106
+ IN IF Range = {} THEN 0
107
+ ELSE CHOOSE r \\in Range :
108
+ \\A s \\in Range : r \\geq s
109
+ RSB[BB \\in UNION {[S -> 1..Biggest] : S \\in SUBSET DOMAIN B}] ==
110
+ IF BB = << >> THEN {<< >>}
111
+ ELSE LET x == CHOOSE x \\in DOMAIN BB : TRUE
112
+ IN Combine(x, RSB[RemoveFromDom(x, BB)])
113
+ IN RSB[B]
114
+
115
+ (******************* Here is the definition from the TLA+ book. ********
116
+ LET AllBagsOfSubset ==
117
+ (******************************************************************)
118
+ (* The set of all bags SB such that BagToSet(SB) \\subseteq *)
119
+ (* BagToSet(B). *)
120
+ (******************************************************************)
121
+ UNION {[SB -> {n \\in Nat : n > 0}] : SB \\in SUBSET BagToSet(B)}
122
+ IN {SB \\in AllBagsOfSubset : \\A e \\in DOMAIN SB : SB[e] \\leq B[e]}
123
+ ***************************************************************************)
124
+
125
+ BagOfAll(F(_), B) ==
126
+ (************************************************************************)
127
+ (* The bag analog of the set {F(x) : x \\in B} for a set B. It's the bag *)
128
+ (* that contains, for each element e of B, one copy of F(e) for every *)
129
+ (* copy of e in B. This defines a bag iff, for any value v, the set of *)
130
+ (* e in B such that F(e) = v is finite. *)
131
+ (************************************************************************)
132
+ [e \\in {F(d) : d \\in BagToSet(B)} |->
133
+ Sum( [d \\in BagToSet(B) |-> IF F(d) = e THEN B[d] ELSE 0] ) ]
134
+
135
+ BagCardinality(B) ==
136
+ (************************************************************************)
137
+ (* If B is a finite bag (one such that BagToSet(B) is a finite set), *)
138
+ (* then this is its cardinality (the total number of copies of elements *)
139
+ (* in B). Its value is unspecified if B is infinite. *)
140
+ (************************************************************************)
141
+ Sum(B)
142
+
143
+ CopiesIn(e, B) ==
144
+ (************************************************************************)
145
+ (* If B is a bag, then CopiesIn(e, B) is the number of copies of e in *)
146
+ (* B. If ~BagIn(e, B), then CopiesIn(e, B) = 0. *)
147
+ (************************************************************************)
148
+ IF BagIn(e, B) THEN B[e] ELSE 0
149
+ =============================================================================
150
+
151
+ (* Last modified on Fri 26 Jan 2007 at 8:45:03 PST by lamport *)
152
+
153
+ 6 Apr 99 : Modified version for standard module set
154
+ 7 Dec 98 : Corrected error found by Stephan Merz.
155
+ 6 Dec 98 : Modified comments based on suggestions by Lyle Ramshaw.
156
+ 5 Dec 98 : Initial version.
157
+ `;
158
+
159
+ // The standard modules of SANY's tla2sany/StandardModules (tla2tools.jar 2026.10.28), verbatim,
160
+ // since the XML that SANY exports locates every node of them and keeps their comments.
161
+ export const SANY_STANDARD_MODULES = new Map([
162
+ ["Bags", BAGS],
163
+ [
164
+ "FiniteSets",
165
+ `---------------------------- MODULE FiniteSets -----------------------------
166
+ (***************************************************************************)
167
+ (* The two definitions in this standard module are overridden by TLC in *)
168
+ (* the Java class tlc2.module.FiniteSets. Each operator is overridden by *)
169
+ (* the Java method with the same name. *)
170
+ (***************************************************************************)
171
+ LOCAL INSTANCE Naturals
172
+ LOCAL INSTANCE Sequences
173
+ (*************************************************************************)
174
+ (* Imports the definitions from Naturals and Sequences, but doesn't *)
175
+ (* export them. *)
176
+ (*************************************************************************)
177
+
178
+ IsFiniteSet(S) ==
179
+ (*************************************************************************)
180
+ (* A set S is finite iff there is a finite sequence containing all its *)
181
+ (* elements. *)
182
+ (*************************************************************************)
183
+ \\E seq \\in Seq(S) : \\A s \\in S : \\E n \\in 1..Len(seq) : seq[n] = s
184
+
185
+ Cardinality(S) ==
186
+ (*************************************************************************)
187
+ (* Cardinality is defined only for finite sets. *)
188
+ (*************************************************************************)
189
+ LET CS[T \\in SUBSET S] == IF T = {} THEN 0
190
+ ELSE 1 + CS[T \\ {CHOOSE x : x \\in T}]
191
+ IN CS[S]
192
+ =============================================================================
193
+ `,
194
+ ],
195
+ [
196
+ "Integers",
197
+ `-------------------------------- MODULE Integers ----------------------------
198
+ (***************************************************************************)
199
+ (* This module provides dummy definitions of the operators that are *)
200
+ (* defined by the real Integers module. It is expected that any tool will *)
201
+ (* provide its own implementations of these operators. See the book *)
202
+ (* "Specifying Systems" for the real Integers module. *)
203
+ (***************************************************************************)
204
+ (***************************************************************************)
205
+ (* The two definitions here and the definitions imported from the Naturals *)
206
+ (* module are overridden by TLC in the Java class tlc2.module.Integers. *)
207
+ (* Each operator is overridden by the Java method with the same name, *)
208
+ (* except that the mappings for the prefix - operator and the TLA+ infix *)
209
+ (* operators are defined in the static block at the beginning of the Java *)
210
+ (* class. *)
211
+ (***************************************************************************)
212
+ EXTENDS Naturals
213
+
214
+ Int == { }
215
+ (***************************************************************************)
216
+ (* This defines the prefix - operator. *)
217
+ (***************************************************************************)
218
+ -. a == 0 - a
219
+ =============================================================================
220
+ `,
221
+ ],
222
+ [
223
+ "Json",
224
+ `-------------------------------- MODULE Json --------------------------------
225
+
226
+ LOCAL INSTANCE Sequences
227
+ LOCAL INSTANCE FiniteSets
228
+ LOCAL INSTANCE TLC
229
+ (*************************************************************************)
230
+ (* Imports the definitions from the modules, but doesn't export them. *)
231
+ (*************************************************************************)
232
+
233
+ -----------------------------------------------------------------------------
234
+
235
+ LOCAL IsSequence(F, D) == TRUE \\/ FALSE
236
+
237
+ LOCAL ToJsonKeyValue(F, d) ==
238
+ ToString(ToString(d)) \\o ":" \\o ToString(F[d])
239
+
240
+ RECURSIVE ToJsonObjectString(_,_)
241
+ LOCAL ToJsonObjectString(F, D) == \\* LOCAL just a hint for humans.
242
+ LET d == CHOOSE d \\in D: TRUE
243
+ IN IF D = DOMAIN F
244
+ THEN "{" \\o ToJsonKeyValue(F, d) \\o IF D \\ {d} = {}
245
+ THEN "}"
246
+ ELSE ToJsonObjectString(F, D \\ {d})
247
+ ELSE ", " \\o ToJsonKeyValue(F, d) \\o IF D \\ {d} = {}
248
+ THEN "}"
249
+ ELSE ToJsonObjectString(F, D \\ {d})
250
+
251
+ RECURSIVE ToJsonArrayString(_,_)
252
+ LOCAL ToJsonArrayString(F, D) == \\* LOCAL just a hint for humans.
253
+ LET d == CHOOSE d \\in D: TRUE
254
+ IN IF D = DOMAIN F
255
+ THEN "[" \\o ToString(F[d]) \\o IF D \\ {d} = {}
256
+ THEN "]"
257
+ ELSE ToJsonArrayString(F, D \\ {d})
258
+ ELSE ", " \\o ToString(F[d]) \\o IF D \\ {d} = {}
259
+ THEN "]"
260
+ ELSE ToJsonArrayString(F, D \\ {d})
261
+
262
+ RECURSIVE ToJsonString(_,_)
263
+ LOCAL ToJsonString(F, D) == \\* LOCAL just a hint for humans.
264
+ IF IsFiniteSet(F) \\/ IsSequence(F, D) THEN
265
+ ToJsonArrayString(F, D)
266
+ ELSE
267
+ ToJsonObjectString(F, D)
268
+
269
+ ToJson(value) ==
270
+ (*************************************************************************)
271
+ (* Converts the given value to a JSON string. Records are converted to *)
272
+ (* JSON objects, tuples to JSON arrays, and scalar values as their JSON *)
273
+ (* representation. *)
274
+ (*************************************************************************)
275
+ IF DOMAIN value = {} THEN
276
+ "{}"
277
+ ELSE
278
+ ToJsonString(value, DOMAIN value)
279
+
280
+ ToJsonArray(value) ==
281
+ (*************************************************************************)
282
+ (* Converts the given tuple value to a JSON array. *)
283
+ (*************************************************************************)
284
+ IF DOMAIN value = {} THEN
285
+ "[]"
286
+ ELSE
287
+ ToJsonArrayString(value, DOMAIN value)
288
+
289
+ ToJsonObject(value) ==
290
+ (*************************************************************************)
291
+ (* Converts the given tuple value to a JSON object. *)
292
+ (*************************************************************************)
293
+ IF DOMAIN value = {} THEN
294
+ "{}"
295
+ ELSE
296
+ ToJsonObjectString(value, DOMAIN value)
297
+
298
+ JsonSerialize(absoluteFilename, value) ==
299
+ (*************************************************************************)
300
+ (* Serializes a tuple of values to the given file as (plain) JSON. *)
301
+ (* Records will be serialized as a JSON objects, and tuples as arrays. *)
302
+ (*************************************************************************)
303
+ TRUE
304
+
305
+ JsonDeserialize(absoluteFilename) ==
306
+ (*************************************************************************)
307
+ (* Deserializes JSON values from the given file. JSON objects will be *)
308
+ (* deserialized to records, and arrays will be deserialized to tuples. *)
309
+ (*************************************************************************)
310
+ CHOOSE val : TRUE
311
+
312
+ ndJsonSerialize(absoluteFilename, value) ==
313
+ (*************************************************************************)
314
+ (* Serializes a tuple of values to the given file as newline delimited *)
315
+ (* JSON. Records will be serialized as a JSON objects, and tuples as *)
316
+ (* arrays. *)
317
+ (*************************************************************************)
318
+ TRUE
319
+
320
+ ndJsonDeserialize(absoluteFilename) ==
321
+ (*************************************************************************)
322
+ (* Deserializes JSON values from the given file. JSON values must be *)
323
+ (* separated by newlines. JSON objects will be deserialized to records, *)
324
+ (* and arrays will be deserialized to tuples. *)
325
+ (*************************************************************************)
326
+ CHOOSE val : TRUE
327
+
328
+ =============================================================================
329
+ `,
330
+ ],
331
+ [
332
+ "Naturals",
333
+ `-------------------------------- MODULE Naturals ----------------------------
334
+ (***************************************************************************)
335
+ (* This module provides dummy definitions of the operators that are *)
336
+ (* defined by the real Naturals module. It is expected that any tool will *)
337
+ (* provide its own implementations of these operators. See the book *)
338
+ (* "Specifying Systems" for the real Naturals module. *)
339
+ (***************************************************************************)
340
+ (***************************************************************************)
341
+ (* These definitions are all overridden by TLC in the Java class *)
342
+ (* tlc2.module.Naturals. Each operator is overridden by the Java method *)
343
+ (* with the same name, except that the mapping for TLA+ infix operators *)
344
+ (* is defined in the static block at the beginning of the Java class. *)
345
+ (***************************************************************************)
346
+ Nat == { }
347
+ a+b == {a, b}
348
+
349
+ a-b == CHOOSE n : b + n = a
350
+ a*b == TRUE
351
+ a^b == {a, b}
352
+ a<b == a = b
353
+ a>b == a = b
354
+ a \\leq b == a = b
355
+ a \\geq b == a = b
356
+ (***************************************************************************)
357
+ (* a .. b is defined to equal {i \\in Int : (a \\leq i) /\\ (i \\leq b)} *)
358
+ (* where Int is the set of all integers. *)
359
+ (* *)
360
+ (* a % b and a \\div b are defined so that for any integers a and b *)
361
+ (* with b > 0 , the following formula is true: *)
362
+ (* *)
363
+ (* a = b * (a \\div b) + (a % b) *)
364
+ (***************************************************************************)
365
+ a % b == {a, b}
366
+ a \\div b == {a, b}
367
+ a .. b == {a, b}
368
+ =============================================================================
369
+ `,
370
+ ],
371
+ [
372
+ "Randomization",
373
+ `-------------------------- MODULE Randomization------------------------------
374
+ (***************************************************************************)
375
+ (* This module defines operators for choosing pseudo-random subsets of a *)
376
+ (* set. It is useful for inductive invariance checking, where the *)
377
+ (* operators appear only in the initial predicate. However, it may have *)
378
+ (* other uses. *)
379
+ (* *)
380
+ (* In breadth-first search model checking, the pseudo-random choices made *)
381
+ (* when computing possible steps satisfying the next-state relation are *)
382
+ (* determined by the first state of the step. Thus, the choices made for *)
383
+ (* a particular state will be the same in successive runs of TLC. This is *)
384
+ (* done to permit TLC to generate an error trace if an error is found. *)
385
+ (* This applies only when TLC is run in breadth-first search mode. In *)
386
+ (* particular, the choices made in simulation mode are independent of the *)
387
+ (* state for which they are made. *)
388
+ (***************************************************************************)
389
+
390
+ (***************************************************************************)
391
+ (* Except for TestRandomSetOfSubsets, all these definitions are overridden *)
392
+ (* by TLC in the Java class tlc2.module.Randomization. Each operator is *)
393
+ (* overridden by the Java method with the same name. *)
394
+ (***************************************************************************)
395
+ LOCAL INSTANCE Naturals
396
+ LOCAL INSTANCE FiniteSets
397
+
398
+ (***************************************************************************)
399
+ (* RandomSubset(k, S) equals a randomly chosen subset of S containing k *)
400
+ (* elements, where 0 < k < Cardinality(S). *)
401
+ (***************************************************************************)
402
+ RandomSubset(k, S) == CHOOSE T \\in SUBSET S : Cardinality(T) = k
403
+
404
+ (***************************************************************************)
405
+ (* RandomSetOfSubsets(k, n, S) equals a pseudo-randomly chosen set of *)
406
+ (* subsets of S -- that is, a randomly chosen subset of SUBSET S . Thus, *)
407
+ (* each element T of this set is a subset of S. Each such T is chosen so *)
408
+ (* that each element of S has a probability n / Cardinality(S) of being in *)
409
+ (* T. Thus, the average number of elements in each chosen subset T is n. *)
410
+ (* The set RandomSetOfSubsets(k, n, S) is obtained by making k such *)
411
+ (* choices of T . Because this can produce duplicate choices, the number *)
412
+ (* of elements T in this set may be less than k. The average number of *)
413
+ (* elements in RandomSetOfSubsets(k, n, S) seems to be difficult to *)
414
+ (* compute in general. However, there is very little variation in the *)
415
+ (* actual number of elements in the chosen set for fixed values of k, n, *)
416
+ (* and Cardinality(S). You can therefore use the operator *)
417
+ (* TestRandomSetOfSubsets defined below to find out approximately how *)
418
+ (* close to k the cardinality of the chosen set of subsets is. *)
419
+ (***************************************************************************)
420
+ RandomSetOfSubsets(k, n, S) == CHOOSE T \\in SUBSET SUBSET S :
421
+ Cardinality(T) \\leq k
422
+
423
+ (***************************************************************************)
424
+ (* The value of TestRandomSetOfSubsets(k, n, S) is a sequence of five *)
425
+ (* values that are the cardinality of the set of subsets produced by five *)
426
+ (* executions of RandomSetOfSubsets(k, n, S). For constant values of k, *)
427
+ (* n, and S, you can enter TestRandomSetOfSubsets(k, n, S) in the Evaluate *)
428
+ (* Constant Expression section of a TLC model in the TLA+ Toolbox. *)
429
+ (* Running TLC will then tell you the approximate number of elements in *)
430
+ (* the set of subsets produced by RandomSetOfSubsets for these parameters. *)
431
+ (* You can then choose k to obtain a set of the desired size. *)
432
+ (***************************************************************************)
433
+ TestRandomSetOfSubsets(k, n, S) ==
434
+ [i \\in 1..5 |-> Cardinality(RandomSetOfSubsets(k, n, S))]
435
+
436
+ =============================================================================
437
+ `,
438
+ ],
439
+ ["RealTime", `${REAL_TIME}\n`],
440
+ ["Reals", `${REALS}\n`],
441
+ [
442
+ "Sequences",
443
+ `------------------------------ MODULE Sequences -----------------------------
444
+ (***************************************************************************)
445
+ (* Defines operators on finite sequences, where a sequence of length n is *)
446
+ (* represented as a function whose domain is the set 1..n (the set *)
447
+ (* {1, 2, ... , n}). This is also how TLA+ defines an n-tuple, so *)
448
+ (* tuples are sequences. *)
449
+ (***************************************************************************)
450
+ (***************************************************************************)
451
+ (* These definitions are all overridden by TLC in the Java class *)
452
+ (* tlc2.module.Sequences. Each operator is overridden by the Java method *)
453
+ (* with the same name, except that the mapping for TLA+ infix operators *)
454
+ (* is defined in the static block at the beginning of the Java class. *)
455
+ (***************************************************************************)
456
+
457
+ LOCAL INSTANCE Naturals
458
+ (*************************************************************************)
459
+ (* Imports the definitions from Naturals, but doesn't export them. *)
460
+ (*************************************************************************)
461
+
462
+ Seq(S) == UNION {[1..n -> S] : n \\in Nat}
463
+ (*************************************************************************)
464
+ (* The set of all sequences of elements in S. *)
465
+ (*************************************************************************)
466
+
467
+ Len(s) == CHOOSE n \\in Nat : DOMAIN s = 1..n
468
+ (*************************************************************************)
469
+ (* The length of sequence s. *)
470
+ (*************************************************************************)
471
+
472
+ s \\o t == [i \\in 1..(Len(s) + Len(t)) |-> IF i \\leq Len(s) THEN s[i]
473
+ ELSE t[i-Len(s)]]
474
+ (*************************************************************************)
475
+ (* The sequence obtained by concatenating sequences s and t. *)
476
+ (*************************************************************************)
477
+
478
+ Append(s, e) == s \\o <<e>>
479
+ (**************************************************************************)
480
+ (* The sequence obtained by appending element e to the end of sequence s. *)
481
+ (**************************************************************************)
482
+
483
+ Head(s) == s[1]
484
+ Tail(s) == CASE s # << >> -> [i \\in 1..(Len(s)-1) |-> s[i+1]]
485
+ (*************************************************************************)
486
+ (* The usual head (first) and tail (rest) operators. (Definition of Tail *)
487
+ (* changed on 4 Jun 2013 because original defined Tail(<< >>) = << >> . *)
488
+ (*************************************************************************)
489
+
490
+ SubSeq(s, m, n) == [i \\in 1..(1+n-m) |-> s[i+m-1]]
491
+ (*************************************************************************)
492
+ (* The sequence <<s[m], s[m+1], ... , s[n]>>. *)
493
+ (*************************************************************************)
494
+
495
+ SelectSeq(s, Test(_)) ==
496
+ (*************************************************************************)
497
+ (* The subsequence of s consisting of all elements s[i] such that *)
498
+ (* Test(s[i]) is true. *)
499
+ (*************************************************************************)
500
+ LET F[i \\in 0..Len(s)] ==
501
+ (*******************************************************************)
502
+ (* F[i] equals SelectSeq(SubSeq(s, 1, i), Test) . *)
503
+ (*******************************************************************)
504
+ IF i = 0 THEN << >>
505
+ ELSE IF Test(s[i]) THEN Append(F[i-1], s[i])
506
+ ELSE F[i-1]
507
+ IN F[Len(s)]
508
+ =============================================================================
509
+ `,
510
+ ],
511
+ [
512
+ "TLC",
513
+ `------------------------------- MODULE TLC ----------------------------------
514
+ (***************************************************************************)
515
+ (* This is a standard module for use with the TLC model checker. *)
516
+ (* Operators not explained by comments here are explained in the book *)
517
+ (* "Specifying Systems". *)
518
+ (* *)
519
+ (* The definitions of all the operators in this module are overridden by *)
520
+ (* TLC with methods defined in the Java class tlc2.module.TLC. Each *)
521
+ (* definition is overridden with the method of the same name, except that *)
522
+ (* the mapping from infix operators to Java methods is specified in the *)
523
+ (* static block at the beginning of the Java class. *)
524
+ (***************************************************************************)
525
+ LOCAL INSTANCE Naturals
526
+ LOCAL INSTANCE Sequences
527
+ LOCAL INSTANCE FiniteSets
528
+ -----------------------------------------------------------------------------
529
+ Print(out, val) == val
530
+ PrintT(out) == TRUE
531
+ (************************************************************************)
532
+ (* This expression equals TRUE, but evaluating it causes TLC to print *)
533
+ (* the value of out. *)
534
+ (************************************************************************)
535
+
536
+ Assert(val, out) == IF val = TRUE THEN TRUE
537
+ ELSE CHOOSE v : TRUE
538
+ JavaTime == CHOOSE n : n \\in Nat
539
+
540
+ (***************************************************************************)
541
+ (* TLC can read and set a special list of values while evaluating *)
542
+ (* expressions using the operators TLCSet and TLCGet. When TLC evaluates *)
543
+ (* TLCSet(i,v), for any positive integer i and arbitrary value v, it *)
544
+ (* obtains the value TRUE and sets element number i of the list to v. *)
545
+ (* When TLC evaluates TLCGet(i), the value it obtains is the current value *)
546
+ (* of the element number i of this list. *)
547
+ (* *)
548
+ (* One use of this feature is to check TLC's progress during long *)
549
+ (* computations. For example, suppose TLC is evaluating a formula *)
550
+ (* \\A x \\in S : P where S is a large set, so it evaluates P many times. *)
551
+ (* You can use TLCGet, TLCSet, and Print to print something after every *)
552
+ (* 1000 times TLC evaluates P. *)
553
+ (* *)
554
+ (* As explained in the description of the TLCEval operator below, you may *)
555
+ (* also want to use this feature to count how many times TLC is evaluating *)
556
+ (* an expression e. To use value number i as the counter, just replace e *)
557
+ (* by *)
558
+ (* *)
559
+ (* IF TLCSet(i, TLCGet(i)+1) THEN e ELSE 42 *)
560
+ (* *)
561
+ (* (The ELSE expression is never evaluated.) *)
562
+ (* *)
563
+ (* TLCGet accepts some pre-defined string values to query TLC state. The *)
564
+ (* values are as follows: *)
565
+ (* *)
566
+ (* - TLCGet("distinct") evaluates to the total number of distinct states *)
567
+ (* found by TLC so far, globally. *)
568
+ (* - TLCGet("queue") evaluates to the number of states currently in the *)
569
+ (* queue to be checked. *)
570
+ (* - TLCGet("duration") evaluates to the number of seconds elapsed since *)
571
+ (* model checking began. *)
572
+ (* - TLCGet("diameter") evaluates to the length of the longest behavior *)
573
+ (* found by TLC so far, globally (equals one in the initial predicate).*)
574
+ (* - TLCGet("level") is the length of the current behavior (equals zero *)
575
+ (* in the evaluation of the initial predicate). *)
576
+ (* *)
577
+ (* For reasons of efficiency, TLCGet and TLCSet behave somewhat strangely *)
578
+ (* when TLC is run with multiple worker threads. Each worker thread *)
579
+ (* maintains its own individual copy of the list of values on which it *)
580
+ (* evaluates TLCGet and TLCSet. The worker threads are activated only *)
581
+ (* after the computation and invariance checking of the initial states. *)
582
+ (* Before then, evaluating TLCSet(i,v) sets the element i of the list *)
583
+ (* maintained by all threads. Thus, the lists of all the worker threads *)
584
+ (* can be initialized by putting the appropriate TLCSet expression in an *)
585
+ (* ASSUME expression or in the initial predicate. *)
586
+ (***************************************************************************)
587
+ TLCGet(i) == CHOOSE n : TRUE
588
+ TLCSet(i, v) == TRUE
589
+ -----------------------------------------------------------------------------
590
+ d :> e == [x \\in {d} |-> e]
591
+ f @@ g == [x \\in (DOMAIN f) \\cup (DOMAIN g) |->
592
+ IF x \\in DOMAIN f THEN f[x] ELSE g[x]]
593
+ Permutations(S) ==
594
+ {f \\in [S -> S] : \\A w \\in S : \\E v \\in S : f[v]=w}
595
+ -----------------------------------------------------------------------------
596
+ (***************************************************************************)
597
+ (* In the following definition, we use Op as the formal parameter rather *)
598
+ (* than \\prec because TLC Version 1 can't handle infix formal parameters. *)
599
+ (***************************************************************************)
600
+ SortSeq(s, Op(_, _)) ==
601
+ LET Perm == CHOOSE p \\in Permutations(1 .. Len(s)) :
602
+ \\A i, j \\in 1..Len(s) :
603
+ (i < j) => Op(s[p[i]], s[p[j]]) \\/ (s[p[i]] = s[p[j]])
604
+ IN [i \\in 1..Len(s) |-> s[Perm[i]]]
605
+
606
+ (***************************************************************************)
607
+ (* TLC evaluates RandomElement(S) to be a pseudo-randomly chosen element *)
608
+ (* of the set S, where each element of S is chosen with equal probability. *)
609
+ (* This feature was added to enable the computation of statistical *)
610
+ (* properties of a specification's executions by running TLC in simulation *)
611
+ (* mode. We don't know if anyone has ever done this. *)
612
+ (* *)
613
+ (* In breadth-first search model checking, the pseudo-random choices made *)
614
+ (* when computing possible steps satisfying the next-state relation are *)
615
+ (* determined by the first state of the step. Thus, the choices made for *)
616
+ (* a particular state will be the same in successive runs of TLC. This is *)
617
+ (* done to permit TLC to generate an error trace if an error is found. *)
618
+ (* This applies only when TLC is run in breadth-first search mode. The *)
619
+ (* random choices made in simulation mode are independent of the state for *)
620
+ (* which they are made. *)
621
+ (***************************************************************************)
622
+ RandomElement(s) == CHOOSE x \\in s : TRUE
623
+
624
+ (***************************************************************************)
625
+ (* The constant value Any has the special property that, for any value v, *)
626
+ (* TLC evaluates the expression v \\in Any to equal TRUE. The special *)
627
+ (* value Any was introduced because TLA+ originally allowed only functions *)
628
+ (* to be defined recursively, and it was sometimes convenient to define a *)
629
+ (* function with domain Any. It is retained for backwards compatibility. *)
630
+ (***************************************************************************)
631
+ Any == CHOOSE x : TRUE
632
+
633
+ ToString(v) == (CHOOSE x \\in [a : v, b : STRING] : TRUE).b
634
+ (************************************************************************)
635
+ (* This equals a string that is the TLA+ representation of the value *)
636
+ (* that TLC obtains by evaluating v. *)
637
+ (************************************************************************)
638
+
639
+ (***************************************************************************)
640
+ (* TLC often uses lazy evaluation. For example, it may not enumerate the *)
641
+ (* elements of a set of the form {x \\in T : P(x)} unless it has to; and it *)
642
+ (* doesn't have to if it only needs to check if an element e is in that *)
643
+ (* set. (TLC can do that by evaluating x \\in T and P(e).) TLC uses *)
644
+ (* heuristics to determine when it should completely evaluate an *)
645
+ (* expression. Those heuristics work well most of the time. However, *)
646
+ (* sometimes lazy evaluation can result in the expression ultimately being *)
647
+ (* evaluated multiple times instead of just once. This can especially be *)
648
+ (* a problem when evaluating a recursively defined operator. You can get *)
649
+ (* TLC to fully evaluate an expression exp and not use lazy evaluation by *)
650
+ (* replacing exp with TLCEval(exp). *)
651
+ (* *)
652
+ (* If TLC is taking a long time to evaluate something, you can check if *)
653
+ (* lazy evaluation is the source of the problem by using the TLCSet and *)
654
+ (* TLCGet operators to count how many times expressions are being *)
655
+ (* evaluated. *)
656
+ (***************************************************************************)
657
+ TLCEval(v) == v
658
+
659
+ =============================================================================`,
660
+ ],
661
+ [
662
+ "TLCExt",
663
+ `------------------------------- MODULE TLCExt -------------------------------
664
+
665
+ LOCAL INSTANCE TLC
666
+ LOCAL INSTANCE Integers
667
+ (*************************************************************************)
668
+ (* Imports the definitions from the modules, but doesn't export them. *)
669
+ (*************************************************************************)
670
+
671
+ -----------------------------------------------------------------------------
672
+
673
+ \\* AssertEq(a, b) is logically equivalent to the expression 'a = b'. If a and b are not equal,
674
+ \\* however, AssertEq(a, b) will print out the values of 'a' and 'b'. If the two values are equal,
675
+ \\* nothing will be printed.
676
+ AssertEq(a, b) ==
677
+ IF a # b THEN
678
+ /\\ PrintT("Assertion failed")
679
+ /\\ PrintT(a)
680
+ /\\ PrintT(b)
681
+ /\\ a = b
682
+ ELSE a = b
683
+
684
+
685
+ AssertError(err, exp) ==
686
+ LET FailsEval(e) == CHOOSE b \\in BOOLEAN : TRUE \\* Expression failed to evaluate.
687
+ TLCError == CHOOSE s \\in STRING : TRUE \\* TLC error string.
688
+ IN IF FailsEval(exp) THEN Assert(err = TLCError, TLCError) ELSE TRUE
689
+
690
+ -----------------------------------------------------------------------------
691
+
692
+ TLCGetOrDefault(key, defaultVal) ==
693
+ CHOOSE n : TRUE
694
+
695
+ TLCGetAndSet(key, Op(_,_), val, defaultVal) ==
696
+ LET oldVal == TLCGetOrDefault(key, defaultVal)
697
+ IN CHOOSE v \\in {oldVal} : TLCSet(key, Op(oldVal, val))
698
+
699
+ -----------------------------------------------------------------------------
700
+
701
+ (* HERE BE DRAGONS! The operators below are experimental! You will probably not need them! *)
702
+
703
+ Trace ==
704
+ (******************************************************************************)
705
+ (* The sequence of states (represented as a record whose DOMAIN is the set of *)
706
+ (* a spec's variables) from an initial state to the current state. In other *)
707
+ (* words, a prefix - of a behavior - ending with the current state. A way to *)
708
+ (* think about this operator is to see it as an implicit history variable *)
709
+ (* that does not have to be explicitly specified at the level of the spec. *)
710
+ (* *)
711
+ (* Note that op is incompatible with TLC!RandomElement and Randomization (see *)
712
+ (* tlc2.tool.TLCTrace.getTrace(LongVec)) and will cause TLC to crash. This *)
713
+ (* technical limitation could be removed though. *)
714
+ (* *)
715
+ (* Beware that Trace is prohibitively expensive when evaluated as part of *)
716
+ (* the next-state relation, invariants, properties, or any constraint in *)
717
+ (* model-checking mode. Consider using a real history variable instead. *)
718
+ (******************************************************************************)
719
+ TRUE \\* TODO
720
+
721
+ -----------------------------------------------------------------------------
722
+
723
+ CounterExample ==
724
+ (****************************************************************************)
725
+ (* In the scope of POSTCONDITION, the operator CounterExample is equivalent *)
726
+ (* to a (directed) graph represented as [state: States, action: Actions] *)
727
+ (* with *)
728
+ (* States \\subseteq [ Variables -> Values ] *)
729
+ (* and *)
730
+ (* Actions \\subseteq *)
731
+ (* (CounterExample.state \\X Action \\X CounterExample.state). *)
732
+ (* If TLC found no violations, then CounterExample.states equals {} and *)
733
+ (* CounterExample.actions equals {}. If only initial state violates a *)
734
+ (* (safety) property, CounterExample.state is a set of those states, and *)
735
+ (* CounterExample.actions = {}. *)
736
+ (****************************************************************************)
737
+ TRUE
738
+
739
+ ToTrace(CE) ==
740
+ (*
741
+ This definition is implemented by the TLCExt#toTrace Java module override. It
742
+ is commented because FiniteSetsExt is part of the CommunityModules, but not
743
+ tla2tools.
744
+
745
+ LET F == INSTANCE FiniteSetsExt
746
+ IN F!FoldSet(LAMBDA a, acc: acc @@ [n \\in {a[1]} |-> a[2]], [n \\in {} |-> n], CounterExample.state)
747
+
748
+ *)
749
+ TRUE
750
+
751
+ -----------------------------------------------------------------------------
752
+
753
+ TLCModelValue(str) ==
754
+ (******************************************************************************)
755
+ (* A model value is an unspecified value that TLC considers to be unequal to *)
756
+ (* any value that you can express in TLA+. *)
757
+ (* *)
758
+ (* In most cases, TLC model values can and should be declared via the TLC *)
759
+ (* config file. However, for large sets of model values, it can become *)
760
+ (* tedious to enumerate them explicitly. Large sets of model values usually *)
761
+ (* appear when simulating TLA+ specs with TLC. The following example defines *)
762
+ (* a set of 32 model values: *)
763
+ (* *)
764
+ (* SetOfModelValues == {TLCModelValue("T_MV" \\o ToString(i)) : i \\in 1..32} *)
765
+ (* *)
766
+ (* As a matter of fact, the example defines a set of "typed" model values: *)
767
+ (* TLC considers a typed model value to be unequal to any other model value *)
768
+ (* of the same type. However, it produces an error when evaluating an *)
769
+ (* expression requires it to determine if a typed model value is equal to any *)
770
+ (* value other than a model value of the same type or an ordinary model *)
771
+ (* value. *)
772
+ (* *)
773
+ (* A model-value type consists of a single letter, so there are 52 different *)
774
+ (* types because a and A are different types. (TLC actually accepts *)
775
+ (* digits and underscore as types; don't use them.) A model value has type *)
776
+ (* T if and only if its name begins with the two characters T_ . *)
777
+ (* *)
778
+ (* TLCModelValue may only appear in constant definitions! Expect bogus *)
779
+ (* behavior if TLCModelValue appears in the behavior spec, constraints, *)
780
+ (* invariants, or properties. *)
781
+ (******************************************************************************)
782
+ CHOOSE v: ToString(v) = str
783
+
784
+ TLCDefer(expression) ==
785
+ (******************************************************************************)
786
+ (* Defer evaluation of expression to some later time or never. *)
787
+ (* *)
788
+ (* For TLC's simulation mode, later is defined as the point of time when *)
789
+ (* simulation has chosen a successor state to extend the prefix of the *)
790
+ (* current behavior. *)
791
+ (* *)
792
+ (* A use case is TLCDefer(TLCSet(42, someVal)), which sets someVal only *)
793
+ (* for the states of the behavior and not the set of all successor states *)
794
+ (* of all states in the behavior. *)
795
+ (******************************************************************************)
796
+ TRUE
797
+
798
+ -----------------------------------------------------------------------------
799
+
800
+ TLCNoOp(val) ==
801
+ (******************************************************************************)
802
+ (* No-operation operator (does nothing). Only useful to debug TLC's *)
803
+ (* evaluator that is written in Java: Insert TLCNoOp into the expression *)
804
+ (* whose evaluation you wish to debug and set a breakpoint in this *)
805
+ (* operator's Java module override TLCExt#tlcNoOp in TLCExt.java. *)
806
+ (******************************************************************************)
807
+ val
808
+
809
+
810
+ PickSuccessor(exp) ==
811
+ (******************************************************************************)
812
+ (* When set as an action constraint in the configuration file, interactively *)
813
+ (* pick successor states during state exploration, iff the expression exp *)
814
+ (* evaluates to FALSE. To always pick successor states manually, use *)
815
+ (* PickSuccessor(FALSE). To pick successor states when the current prefix of *)
816
+ (* behaviors exceeds 22 states, use PickSuccessor(TLCGet("level") < 23). *)
817
+ (* Evaluates to TRUE when evaluated in the context of a constant- or *)
818
+ (* state-level formula. Also evaluates to TRUE if PickSuccessor appears as *)
819
+ (* part of the next-state relation and the successor state has not been *)
820
+ (* fully determined yet. *)
821
+ (******************************************************************************)
822
+ IF (exp)
823
+ THEN TRUE
824
+ ELSE CHOOSE bool \\in BOOLEAN : TRUE
825
+
826
+ -----------------------------------------------------------------------------
827
+
828
+ TLCCache(expression, closure) ==
829
+ (******************************************************************************)
830
+ (* Equals the value that an earlier evaluation of the expression evaluated to *)
831
+ (* iff the closure of the earlier evaluation and this evaluation are equal. *)
832
+ (* *)
833
+ (* Works in constant and state-level contexts. *)
834
+ (******************************************************************************)
835
+ expression
836
+
837
+ TLCFP(var) ==
838
+ (******************************************************************************)
839
+ (* Equals the value that TLC gives to the given input when the value is *)
840
+ (* being fingerprinted. *)
841
+ (* Implementation note: Equals the lower 32 bits until TLC supports longs *)
842
+ (******************************************************************************)
843
+ CHOOSE i \\in Int: TRUE
844
+
845
+ -----------------------------------------------------------------------------
846
+
847
+ TLCEvalDefinition(definitionName) ==
848
+ (******************************************************************************)
849
+ (* Equals the value of the definition with the given name. *)
850
+ (* Throws an error if the definition cannot be found. *)
851
+ (******************************************************************************)
852
+ TRUE \\* TODO
853
+
854
+ ============================================================================
855
+ `,
856
+ ],
857
+ ["Toolbox", `${TOOLBOX}\n`],
858
+ [
859
+ "_DotTrace",
860
+ `------------------------------- MODULE _DotTrace -------------------------------
861
+ LOCAL INSTANCE TLC
862
+ LOCAL INSTANCE TLCExt
863
+ LOCAL INSTANCE GraphViz
864
+ LOCAL INSTANCE IOUtils
865
+ LOCAL INSTANCE Sequences
866
+ LOCAL INSTANCE Functions
867
+
868
+ CONSTANT _DotTraceFile
869
+
870
+ _DotTrace ==
871
+ IF CounterExample.state = {} \\/ ("console" \\in DOMAIN CounterExample /\\ CounterExample["console"] = FALSE) THEN TRUE ELSE
872
+ /\\ LET N == CounterExample.state
873
+ E == { <<e[1], e[3], e[2]>> : e \\in CounterExample.action }
874
+ G == [node |-> N, edge |-> E] IN
875
+ Serialize(DotDiGraph(G, LAMBDA v: FoldFunctionOnSet(
876
+ LAMBDA x, y: ToString(x) \\o "\\n" \\o y, "", v[2], DOMAIN v[2]),
877
+ LAMBDA e: e[3].name),
878
+ _DotTraceFile,
879
+ [
880
+ format |-> "TXT",
881
+ charset |-> "UTF-8",
882
+ openOptions |-> <<"WRITE", "CREATE", "TRUNCATE_EXISTING">>
883
+ ]
884
+ ).exitValue = 0
885
+ /\\ PrintT("CounterExample written: " \\o _DotTraceFile)
886
+
887
+ =============================================================================
888
+ `,
889
+ ],
890
+ [
891
+ "_JsonTrace",
892
+ `------------------------------- MODULE _JsonTrace -------------------------------
893
+ LOCAL INSTANCE TLC
894
+ LOCAL INSTANCE TLCExt
895
+ LOCAL INSTANCE Json
896
+ LOCAL INSTANCE Sequences
897
+ LOCAL INSTANCE Naturals
898
+
899
+ CONSTANT _JsonTraceFile
900
+
901
+ _JsonTrace ==
902
+ IF CounterExample.state = {} \\/ ("console" \\in DOMAIN CounterExample /\\ CounterExample["console"] = FALSE) THEN TRUE ELSE
903
+ /\\ LET trace == ToTrace(CounterExample)
904
+ vars == UNION { DOMAIN trace[i] : i \\in DOMAIN trace }
905
+ IN JsonSerialize(_JsonTraceFile, [counterexample |-> CounterExample, vars |-> vars])
906
+ /\\ PrintT("CounterExample written: " \\o _JsonTraceFile)
907
+
908
+ ----------------------------------------------------------------------------
909
+ \\* Deserialize a trace created by _JsonTrace above.
910
+
911
+ CONSTANT _JsonTraceInputFile \\* Used by -loadTrace json
912
+
913
+ \\* This operator has a Java module override (tlc2.module._JsonTrace#tlcState).
914
+ LOCAL _TLCState(level) ==
915
+ Trace[level]
916
+
917
+ LOCAL _JsonTraceDump == JsonDeserialize(_JsonTraceInputFile)
918
+
919
+ \\* A liveness counterexample's action graph contains a closing edge that
920
+ \\* completes the lasso. In a forward path s1->s2->...->sn every edge
921
+ \\* satisfies target[1] > source[1]; the closing edge is the unique edge
922
+ \\* where target[1] <= source[1] (back-to-state: <, stuttering: =).
923
+ LOCAL _JsonTraceLassoTarget ==
924
+ LET actions == _JsonTraceDump["counterexample"]["action"]
925
+ IN IF \\E i \\in DOMAIN actions : actions[i][3][1] <= actions[i][1][1]
926
+ THEN LET i == CHOOSE i \\in DOMAIN actions :
927
+ actions[i][3][1] <= actions[i][1][1]
928
+ IN actions[i][3][1]
929
+ ELSE 0
930
+
931
+ LOCAL _JsonTraceConstraint ==
932
+ LET level == TLCGet("level")
933
+ \\* DOMAIN trace is meaningful only because we serialize TLA+ sets as JSON arrays. JSON has
934
+ \\* no set type---only the world's favorite least-common-denominator of strings, numbers,
935
+ \\* and nested lists---so on the wire every set is an ordered sequence. If that encoding
936
+ \\* ever changed (or JSON miraculously grew a real set type), indexing states by DOMAIN
937
+ \\* would stop matching what we dump.
938
+ \\* Rebuilding vars likewise assumes the array order matches TLC's normalized set order from
939
+ \\* when the trace was written; the format does not pin down a stronger invariant.
940
+ trace == _JsonTraceDump["counterexample"]["state"]
941
+ \\* JSON deserializes sets as tuples, so convert back to a set
942
+ vars == {_JsonTraceDump["vars"][i] : i \\in DOMAIN _JsonTraceDump["vars"]}
943
+ \\* For liveness properties, TLC trace dumps stop at the state *before* the
944
+ \\* lasso is closed. When replaying such a trace, TLC may request a state with
945
+ \\* level = Len(trace) + 1,
946
+ \\* which does not exist in the dump. In that case, the constraint
947
+ \\* is intentionally vacuously satisfied.
948
+ \\* Since the names of the spec's variables are not known, Trace[level] is
949
+ \\* used as a generic reference to the variables of the current state.
950
+ IN level \\in DOMAIN trace =>
951
+ \\* When loading a trace with a subset of variables, only check the variables
952
+ \\* that exist in both the trace and the current state. This allows trace
953
+ \\* replay with specs that have different variable sets than the original spec
954
+ \\* that produced the trace. Another scenario is when the spec uses ALIAS
955
+ \\* to rename, add, or remove variables.
956
+ \\A v \\in vars \\cap (DOMAIN _TLCState(level)):
957
+ _TLCState(level)[v] = trace[level][2][v]
958
+
959
+ \\* VIEW for trace replay. Includes TLCGet("level") to prevent premature
960
+ \\* state collapsing within the trace. For liveness traces, the level
961
+ \\* beyond the trace is mapped to the lasso target so the cycle can close.
962
+ LOCAL _JsonTraceView ==
963
+ LET level == TLCGet("level")
964
+ traceLen == Len(_JsonTraceDump["counterexample"]["state"])
965
+ lassoTarget == _JsonTraceLassoTarget
966
+ IN IF level <= traceLen
967
+ THEN << level, _TLCState(level) >>
968
+ ELSE << lassoTarget, _TLCState(level) >>
969
+
970
+ =============================================================================
971
+ `,
972
+ ],
973
+ [
974
+ "_Possible",
975
+ `------------------------------ MODULE _Possible ------------------------------
976
+ LOCAL INSTANCE TLC
977
+ LOCAL INSTANCE TLCExt
978
+ LOCAL INSTANCE Naturals
979
+ LOCAL INSTANCE Sequences
980
+
981
+ LOCAL _Update(Op(_,_), val) ==
982
+ TLCSet("s:_possible", Op(TLCGetOrDefault("s:_possible", <<>>), val))
983
+
984
+ _Track(expr, name) ==
985
+ _Update(LAMBDA old, b:
986
+ IF b
987
+ THEN
988
+ IF name \\in DOMAIN old
989
+ THEN [old EXCEPT ![name] = @ + 1]
990
+ ELSE old @@ (name :> 1)
991
+ ELSE old @@ (name :> 0), expr)
992
+
993
+ LOCAL _Merge(a, b) ==
994
+ [name \\in (DOMAIN a) \\cup (DOMAIN b) |->
995
+ (IF name \\in DOMAIN a THEN a[name] ELSE 0) +
996
+ (IF name \\in DOMAIN b THEN b[name] ELSE 0)]
997
+
998
+ _Counts == \\* Java override for efficiency
999
+ LET workers == TLCGet("all:named")["s:_possible"]
1000
+ n == Len(workers)
1001
+ Fold[i \\in 0..n] ==
1002
+ IF i = 0 THEN <<>>
1003
+ ELSE _Merge(Fold[i-1], workers[i])
1004
+ IN Fold[n]
1005
+
1006
+ _CheckName(name) ==
1007
+ LET counts == _Counts
1008
+ IN (name \\notin DOMAIN counts \\/ counts[name] = 0)
1009
+ => (PrintT(counts) /\\ FALSE)
1010
+
1011
+ _PrintCounts == PrintT(_Counts)
1012
+
1013
+ =============================================================================
1014
+ `,
1015
+ ],
1016
+ [
1017
+ "_TLAPlusCounterExample",
1018
+ `----------------------- MODULE _TLAPlusCounterExample ------------------------
1019
+ LOCAL INSTANCE IOUtils
1020
+ LOCAL INSTANCE TLC
1021
+ LOCAL INSTANCE TLCExt
1022
+ LOCAL INSTANCE Sequences
1023
+ LOCAL INSTANCE SequencesExt
1024
+
1025
+ CONSTANT _TLAPlusCounterExampleFile
1026
+
1027
+ LOCAL _TLAPlusCounterExampleModule ==
1028
+ LET ModuleName == ReplaceFirstSubSeq("", ".tla", _TLAPlusCounterExampleFile) IN
1029
+ "---- MODULE " \\o ModuleName \\o " ----\\n" \\o
1030
+ "LOCAL INSTANCE TLC\\n\\n" \\o
1031
+ "CounterExample == \\n\\t" \\o
1032
+ ToString(CounterExample) \\o
1033
+ "\\n\\n===="
1034
+
1035
+ _TLAPlusCounterExample ==
1036
+ IF CounterExample.state = {} \\/ ("console" \\in DOMAIN CounterExample /\\ CounterExample["console"] = FALSE) THEN TRUE ELSE
1037
+ /\\ Serialize(_TLAPlusCounterExampleModule,
1038
+ _TLAPlusCounterExampleFile,
1039
+ [
1040
+ format |-> "TXT",
1041
+ charset |-> "UTF-8",
1042
+ openOptions |-> <<"WRITE", "CREATE", "TRUNCATE_EXISTING">>
1043
+ ]
1044
+ ).exitValue = 0
1045
+ /\\ PrintT("CounterExample written: " \\o _TLAPlusCounterExampleFile)
1046
+
1047
+ =============================================================================
1048
+ `,
1049
+ ],
1050
+ [
1051
+ "_TLCActionTrace",
1052
+ `------- MODULE _TLCActionTrace ------
1053
+ LOCAL INSTANCE IOUtils
1054
+ LOCAL INSTANCE TLC
1055
+ LOCAL INSTANCE TLCExt
1056
+ LOCAL INSTANCE Sequences
1057
+ LOCAL INSTANCE SequencesExt
1058
+ LOCAL INSTANCE FiniteSets
1059
+ LOCAL INSTANCE FiniteSetsExt
1060
+
1061
+ LOCAL _extends ==
1062
+ LET M == FoldSet(LAMBDA a, acc: acc \\cup {a[2].location.module}, {}, CounterExample.action)
1063
+ IN FoldSet(LAMBDA module, acc: acc \\o (IF acc = "" THEN "EXTENDS " ELSE ", ") \\o module, "", M)
1064
+
1065
+ LOCAL _next ==
1066
+ LET Params(r) == FoldLeft(LAMBDA acc, p: acc \\o (IF acc = "" THEN "" ELSE ", ") \\o ToString(r.context[p]),
1067
+ "", r.parameters)
1068
+ Signature(r) == IF "parameters" \\in DOMAIN r THEN r.name \\o "(" \\o Params(r) \\o ")" ELSE r.name
1069
+ ToDisjunct(i, r) == "\\\\/ " \\o ToString(i) \\o " = _idx /\\\\ " \\o Signature(r) \\o "\\n"
1070
+ IN FoldSet(LAMBDA a, acc: acc \\o ToDisjunct(a[1][1], a[2]), "", CounterExample.action)
1071
+
1072
+ CONSTANT _TLCTraceFile
1073
+
1074
+ LOCAL _TLCTraceModule ==
1075
+ LET ModuleName == ReplaceFirstSubSeq("", ".tla", _TLCTraceFile)
1076
+ L == ToString(Cardinality(CounterExample.action)) IN
1077
+ "---- MODULE " \\o ModuleName \\o " ----\\n" \\o
1078
+ \\*TODO callsignature symbols may not be defined in the same modules where the actions are defined.
1079
+ \\*TODO E.g., model values are typically defined in the MC file.
1080
+ _extends \\o "\\n\\n" \\o
1081
+ "_length == " \\o L \\o "\\n\\n" \\o
1082
+ "_next(_idx) ==\\n" \\o _next \\o "\\n" \\o
1083
+ "_actionConstraint ==\\n" \\o
1084
+ "_next(LET T == INSTANCE TLC IN T!TLCGet(\\"level\\"))\\n" \\o
1085
+ "\\n===="
1086
+
1087
+ _TLCTrace ==
1088
+ IF CounterExample.action = {} \\/ ("console" \\in DOMAIN CounterExample /\\ CounterExample["console"] = FALSE) THEN TRUE ELSE
1089
+ /\\ Serialize(_TLCTraceModule,
1090
+ _TLCTraceFile,
1091
+ [
1092
+ format |-> "TXT",
1093
+ charset |-> "UTF-8",
1094
+ openOptions |-> <<"WRITE", "CREATE", "TRUNCATE_EXISTING">>
1095
+ ]
1096
+ ).exitValue = 0
1097
+ /\\ PrintT("CounterExample written: " \\o _TLCTraceFile)
1098
+
1099
+ ======`,
1100
+ ],
1101
+ [
1102
+ "_TLCTESpec",
1103
+ `----------------------- MODULE _TLCTESpec ------------------------
1104
+ LOCAL INSTANCE IOUtils
1105
+ LOCAL INSTANCE TLC
1106
+ LOCAL INSTANCE TLCExt
1107
+ LOCAL INSTANCE Sequences
1108
+ LOCAL INSTANCE SequencesExt
1109
+ LOCAL INSTANCE FiniteSets
1110
+ LOCAL INSTANCE FiniteSetsExt
1111
+
1112
+ LOCAL _extends ==
1113
+ LET M == FoldSet(LAMBDA a, acc: acc \\cup {a[2].location.module}, {}, CounterExample.action)
1114
+ IN FoldSet(LAMBDA module, acc: acc \\o (IF acc = "" THEN "EXTENDS " ELSE ", ") \\o module, "", M)
1115
+
1116
+ LOCAL _Vars ==
1117
+ UNION { DOMAIN pair[2] : pair \\in CounterExample.state }
1118
+
1119
+ LOCAL _ToConjunt(v, prime, idx) ==
1120
+ "/\\\\ " \\o v \\o prime \\o " = Trace[" \\o idx \\o "]." \\o v \\o "\\n"
1121
+
1122
+ CONSTANT _TLCTraceFile
1123
+
1124
+ LOCAL _conjunct(prime, idx) ==
1125
+ FoldSet(LAMBDA v, acc: acc \\o _ToConjunt(v, prime, idx), "", _Vars)
1126
+
1127
+ LOCAL _subVars ==
1128
+ FoldSet(LAMBDA v, acc: acc \\o (IF acc = "" THEN "" ELSE ", ") \\o v, "", _Vars)
1129
+
1130
+ LOCAL _TLCTraceModule ==
1131
+ LET ModuleName == ReplaceFirstSubSeq("", ".tla", _TLCTraceFile) IN
1132
+ "---- MODULE " \\o ModuleName \\o " ----\\n" \\o
1133
+ \\*TODO E.g., model values are typically defined in the MC file.
1134
+ _extends \\o "\\n\\n" \\o
1135
+ "LOCAL INSTANCE TLC\\n\\n" \\o
1136
+ "Trace == \\n\\t" \\o
1137
+ ToString(ToTrace(CounterExample)) \\o
1138
+ "\\n\\n" \\o
1139
+ "_init ==\\n" \\o _conjunct("", "1") \\o
1140
+ "\\n" \\o
1141
+ "_next ==\\n\\\\E i,j \\\\in DOMAIN Trace:\\n/\\\\ i = TLCGet(\\"level\\")\\n/\\\\ j = i + 1\\n" \\o
1142
+ _conjunct("", "i") \\o _conjunct("'", "j") \\o
1143
+ "\\n(* Allow infinite stuttering to prevent deadlock on termination. *)" \\o
1144
+ "\\n_terminating ==\\n\\tUNCHANGED <<" \\o _subVars \\o ">>" \\o
1145
+ "\\n\\n_spec == \\n\\t_init /\\\\ [][_next \\\\/ _terminating]_<<" \\o _subVars \\o ">>\\n" \\o
1146
+ "\\n===="
1147
+ \\* TODO: Append in-file config here.
1148
+
1149
+ _TLCTrace ==
1150
+ IF CounterExample.state = {} \\/ ("console" \\in DOMAIN CounterExample /\\ CounterExample["console"] = FALSE) THEN TRUE ELSE
1151
+ /\\ Serialize(_TLCTraceModule,
1152
+ _TLCTraceFile,
1153
+ [
1154
+ format |-> "TXT",
1155
+ charset |-> "UTF-8",
1156
+ openOptions |-> <<"WRITE", "CREATE", "TRUNCATE_EXISTING">>
1157
+ ]
1158
+ ).exitValue = 0
1159
+ /\\ PrintT("CounterExample written: " \\o _TLCTraceFile)
1160
+
1161
+ =============================================================================
1162
+ `,
1163
+ ],
1164
+ [
1165
+ "_TLCTrace",
1166
+ `---------------------------- MODULE _TLCTrace ----------------------------
1167
+ LOCAL INSTANCE TLC
1168
+ LOCAL INSTANCE TLCExt
1169
+ LOCAL INSTANCE Sequences
1170
+ LOCAL INSTANCE Naturals
1171
+
1172
+ \\* This operator has a Java module override (tlc2.module._TLCTrace#ioDeserialize).
1173
+ LOCAL _TLCTraceDeserialize(absoluteFilename) ==
1174
+ TRUE
1175
+
1176
+ \\* This operator has a Java module override (tlc2.module._TLCTrace#ioSerialize).
1177
+ LOCAL _TLCTraceSerialize(val, absoluteFilename) ==
1178
+ TRUE
1179
+
1180
+ ----------------------------------------------------------------------------
1181
+ \\* Serialize a trace to a file.
1182
+
1183
+ CONSTANT _TLCTraceFile
1184
+
1185
+ LOCAL _TLCTrace0(verbose) ==
1186
+ IF CounterExample.state = {} \\/ ("console" \\in DOMAIN CounterExample /\\ CounterExample["console"] = FALSE) THEN TRUE ELSE
1187
+ /\\ LET trace == ToTrace(CounterExample)
1188
+ vars == UNION { DOMAIN trace[i] : i \\in DOMAIN trace }
1189
+ IN _TLCTraceSerialize([counterexample |-> CounterExample, vars |-> vars], _TLCTraceFile)
1190
+ /\\ IF verbose THEN PrintT("CounterExample written: " \\o _TLCTraceFile) ELSE TRUE
1191
+
1192
+ LOCAL _TLCTraceSilent ==
1193
+ _TLCTrace0(FALSE)
1194
+
1195
+ _TLCTrace ==
1196
+ _TLCTrace0(TRUE)
1197
+
1198
+ ----------------------------------------------------------------------------
1199
+ \\* Deserialize a trace created by _TLCTrace above.
1200
+
1201
+ CONSTANT _TLCTraceInputFile \\* Used by -loadTrace tlc
1202
+
1203
+ LOCAL _TLCTraceFileDeserialized ==
1204
+ _TLCTraceDeserialize(_TLCTraceInputFile)
1205
+
1206
+ \\* This operator has a Java module override (tlc2.module._TLCTrace#tlcState).
1207
+ LOCAL _TLCState(level) ==
1208
+ Trace[level]
1209
+
1210
+ \\* A liveness counterexample's action graph contains a closing edge that
1211
+ \\* completes the lasso. In a forward path s1->s2->...->sn every edge
1212
+ \\* satisfies target[1] > source[1]; the closing edge is the unique edge
1213
+ \\* where target[1] <= source[1] (back-to-state: <, stuttering: =).
1214
+ LOCAL _TLCTraceLassoTarget ==
1215
+ LET actions == _TLCTraceFileDeserialized["counterexample"]["action"]
1216
+ IN IF \\E e \\in actions : e[3][1] <= e[1][1]
1217
+ THEN (CHOOSE e \\in actions : e[3][1] <= e[1][1])[3][1]
1218
+ ELSE 0
1219
+
1220
+ LOCAL _TLCTraceConstraint ==
1221
+ LET level == TLCGet("level")
1222
+ dump == _TLCTraceFileDeserialized
1223
+ trace == dump["counterexample"]["state"]
1224
+ vars == dump["vars"]
1225
+ \\* The trace (CounterExample.state) is a set of <<level, state>> pairs,
1226
+ \\* not a function/sequence.
1227
+ \\* For liveness properties, TLC trace dumps stop at the state *before* the
1228
+ \\* lasso is closed. When replaying such a trace, TLC may request a state with
1229
+ \\* a level that does not exist in the dump. In that case, no pair matches and
1230
+ \\* the implication is vacuously satisfied.
1231
+ IN \\A pair \\in trace:
1232
+ pair[1] = level =>
1233
+ \\* When loading a trace with a subset of variables, only check the
1234
+ \\* variables that exist in both the trace and the current state.
1235
+ \\* This allows trace replay with specs that have different variable
1236
+ \\* sets than the original spec that produced the trace. Another
1237
+ \\* scenario is when the spec uses ALIAS to rename, add, or remove
1238
+ \\* variables.
1239
+ \\A v \\in vars \\cap DOMAIN _TLCState(level):
1240
+ _TLCState(level)[v] = pair[2][v]
1241
+
1242
+ \\* VIEW for trace replay. Includes TLCGet("level") to prevent premature
1243
+ \\* state collapsing within the trace. For liveness traces, the level
1244
+ \\* beyond the trace is mapped to the lasso target so the cycle can close.
1245
+ LOCAL _TLCTraceView ==
1246
+ LET level == TLCGet("level")
1247
+ trace == _TLCTraceFileDeserialized["counterexample"]["state"]
1248
+ lassoTarget == _TLCTraceLassoTarget
1249
+ IN IF \\E pair \\in trace : pair[1] = level
1250
+ THEN << level, _TLCState(level) >>
1251
+ ELSE << lassoTarget, _TLCState(level) >>
1252
+
1253
+ =============================================================================
1254
+ `,
1255
+ ],
1256
+ [
1257
+ "_TLCTracePlain",
1258
+ `----------------------- MODULE _TLCTracePlain ------------------------
1259
+ LOCAL INSTANCE IOUtils
1260
+ LOCAL INSTANCE TLC
1261
+ LOCAL INSTANCE TLCExt
1262
+ LOCAL INSTANCE Sequences
1263
+ LOCAL INSTANCE SequencesExt
1264
+
1265
+ CONSTANT _TLCTraceFile
1266
+
1267
+ LOCAL _TLCTraceModule ==
1268
+ LET ModuleName == ReplaceFirstSubSeq("", ".tla", _TLCTraceFile) IN
1269
+ "---- MODULE " \\o ModuleName \\o " ----\\n" \\o
1270
+ "LOCAL INSTANCE TLC\\n\\n" \\o
1271
+ "Trace == \\n\\t" \\o
1272
+ ToString(ToTrace(CounterExample)) \\o
1273
+ "\\n\\n===="
1274
+
1275
+ _TLCTrace ==
1276
+ IF CounterExample.state = {} \\/ ("console" \\in DOMAIN CounterExample /\\ CounterExample["console"] = FALSE) THEN TRUE ELSE
1277
+ /\\ Serialize(_TLCTraceModule,
1278
+ _TLCTraceFile,
1279
+ [
1280
+ format |-> "TXT",
1281
+ charset |-> "UTF-8",
1282
+ openOptions |-> <<"WRITE", "CREATE", "TRUNCATE_EXISTING">>
1283
+ ]
1284
+ ).exitValue = 0
1285
+ /\\ PrintT("CounterExample written: " \\o _TLCTraceFile)
1286
+
1287
+ =============================================================================
1288
+ `,
1289
+ ],
1290
+ ]);
1291
+
1292
+ // The standard modules of TLC 1.7.4's tla2tools.jar, which has none of the later ones and older
1293
+ // Bags, Randomization and Toolbox modules.
1294
+ export const SANY_174_STANDARD_MODULES = new Map([
1295
+ ...[...SANY_STANDARD_MODULES].filter(([name]) =>
1296
+ ["FiniteSets", "Integers", "Naturals", "RealTime", "Reals", "Sequences", "TLC"].includes(name),
1297
+ ),
1298
+ ["Bags", BAGS.replace("\nLOCAL INSTANCE Naturals\n", "\nEXTENDS TLC\nLOCAL INSTANCE Naturals\n")],
1299
+ [
1300
+ "Randomization",
1301
+ `-------------------------- MODULE Randomization------------------------------
1302
+ (***************************************************************************)
1303
+ (* This module defines operators for choosing pseudo-random subsets of a *)
1304
+ (* set. It is useful for inductive invariance checking, where the *)
1305
+ (* operators appear only in the initial predicate. However, it may have *)
1306
+ (* other uses. *)
1307
+ (* *)
1308
+ (* In breadth-first search model checking, the pseudo-random choices made *)
1309
+ (* when computing possible steps satisfying the next-state relation are *)
1310
+ (* determined by the first state of the step. Thus, the choices made for *)
1311
+ (* a particular state will be the same in successive runs of TLC. This is *)
1312
+ (* done to permit TLC to generate an error trace if an error is found. *)
1313
+ (* This applies only when TLC is run in breadth-first search mode. In *)
1314
+ (* particular, the choices made in simulation mode are independent of the *)
1315
+ (* state for which they are made. *)
1316
+ (***************************************************************************)
1317
+
1318
+ (***************************************************************************)
1319
+ (* Except for TestRandomSetOfSubsets, all these definitions are overridden *)
1320
+ (* by TLC in the Java class tlc2.module.Randomization. Each operator is *)
1321
+ (* overridden by the Java method with the same name. *)
1322
+ (***************************************************************************)
1323
+ LOCAL INSTANCE Naturals
1324
+ LOCAL INSTANCE FiniteSets
1325
+
1326
+ (***************************************************************************)
1327
+ (* RandomSubset(k, S) equals a randomly chosen subset of S containing k *)
1328
+ (* elements, where 0 < k < Cardinality(S). *)
1329
+ (***************************************************************************)
1330
+ RandomSubset(k, S) == CHOOSE T \\in SUBSET S : Cardinality(T) = T
1331
+
1332
+ (***************************************************************************)
1333
+ (* RandomSetOfSubsets(k, n, S) equals a pseudo-randomly chosen set of *)
1334
+ (* subsets of S -- that is, a randomly chosen subset of SUBSET S . Thus, *)
1335
+ (* each element T of this set is a subset of S. Each such T is chosen so *)
1336
+ (* that each element of S has a probability n / Cardinality(S) of being in *)
1337
+ (* T. Thus, the average number of elements in each chosen subset T is n. *)
1338
+ (* The set RandomSetOfSubsets(k, n, S) is obtained by making k such *)
1339
+ (* choices of T . Because this can produce duplicate choices, the number *)
1340
+ (* of elements T in this set may be less than k. The average number of *)
1341
+ (* elements in RandomSetOfSubsets(k, n, S) seems to be difficult to *)
1342
+ (* compute in general. However, there is very little variation in the *)
1343
+ (* actual number of elements in the chosen set for fixed values of k, n, *)
1344
+ (* and Cardinality(S). You can therefore use the operator *)
1345
+ (* TestRandomSetOfSubsets defined below to find out approximately how *)
1346
+ (* close to k the cardinality of the chosen set of subsets is. *)
1347
+ (***************************************************************************)
1348
+ RandomSetOfSubsets(k, n, S) == CHOOSE T \\in SUBSET SUBSET S :
1349
+ Cardinality(T) \\leq k
1350
+
1351
+ (***************************************************************************)
1352
+ (* The value of TestRandomSetOfSubsets(k, n, S) is a sequence of five *)
1353
+ (* values that are the cardinality of the set of subsets produced by five *)
1354
+ (* executions of RandomSetOfSubsets(k, n, S). For constant values of k, *)
1355
+ (* n, and S, you can enter TestRandomSetOfSubsets(k, n, S) in the Evaluate *)
1356
+ (* Constant Expression section of a TLC model in the TLA+ Toolbox. *)
1357
+ (* Running TLC will then tell you the approximate number of elements in *)
1358
+ (* the set of subsets produced by RandomSetOfSubsets for these parameters. *)
1359
+ (* You can then choose k to obtain a set of the desired size. *)
1360
+ (***************************************************************************)
1361
+ TestRandomSetOfSubsets(k, n, S) ==
1362
+ [i \\in 1..5 |-> Cardinality(RandomSetOfSubsets(k, n, S))]
1363
+
1364
+ =============================================================================
1365
+ `,
1366
+ ],
1367
+ [
1368
+ "Toolbox",
1369
+ `----------------------------- MODULE Toolbox --------------------------------
1370
+ (***************************************************************************)
1371
+ (* *)
1372
+ (***************************************************************************)
1373
+ LOCAL INSTANCE Naturals \\* a + b
1374
+ LOCAL INSTANCE FiniteSets \\* Cardinality
1375
+ LOCAL INSTANCE TLC \\* TLCGet
1376
+
1377
+ -----------------------------------------------------------------------------
1378
+
1379
+ (* Trace Exploration *)
1380
+
1381
+ _TETrace ==
1382
+ CHOOSE f \\in [ (Nat \\ {0}) -> STRING ] : TRUE
1383
+
1384
+ LOCAL _TETraceLength == Cardinality(DOMAIN _TETrace)
1385
+
1386
+ _TEPosition ==
1387
+ IF TLCGet("level") >= _TETraceLength
1388
+ THEN _TETraceLength
1389
+ ELSE TLCGet("level") + 1
1390
+
1391
+ =============================================================================
1392
+ `,
1393
+ ],
1394
+ ]);
1395
+
1396
+ // The standard modules of the SANY of a TLC.
1397
+ export const getSanyStandardModules = (version: TlcVersion = "master") =>
1398
+ version === "1.7.4" ? SANY_174_STANDARD_MODULES : SANY_STANDARD_MODULES;