tla-ts 0.0.2 → 0.0.3-dev.252cf06

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 (57) hide show
  1. package/README.md +73 -11
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +52 -1
  4. package/dist/index.d.ts +52 -1
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +2 -2
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +21 -2
  11. package/dist/run-cli.js +21 -2
  12. package/dist/transfer.cjs +19 -15
  13. package/dist/transfer.js +19 -15
  14. package/package.json +3 -1
  15. package/src/ast.ts +134 -4
  16. package/src/check.ts +120 -40
  17. package/src/cli-options.ts +58 -0
  18. package/src/codegen.ts +1 -3
  19. package/src/dependencies.ts +2 -2
  20. package/src/environment.ts +10 -10
  21. package/src/errors.ts +42 -1
  22. package/src/evaluate.ts +57 -114
  23. package/src/exit-codes.ts +20 -30
  24. package/src/fingerprints.ts +46 -11
  25. package/src/instances.ts +12 -11
  26. package/src/levels.ts +8 -8
  27. package/src/lexer.ts +11 -2
  28. package/src/live-check.ts +710 -0
  29. package/src/liveness.ts +5 -523
  30. package/src/obligation-action.ts +384 -0
  31. package/src/obligation-anon.ts +575 -0
  32. package/src/obligation-deref.ts +637 -0
  33. package/src/obligation-expression.ts +1060 -0
  34. package/src/obligation-format.ts +406 -0
  35. package/src/obligation-generate.ts +1074 -0
  36. package/src/obligation-levels.ts +329 -0
  37. package/src/obligation-module.ts +1042 -0
  38. package/src/obligation-printer.ts +889 -0
  39. package/src/obligation-proof.ts +76 -0
  40. package/src/obligation-source.ts +1084 -0
  41. package/src/obligation-tuples.ts +184 -0
  42. package/src/obligations.ts +316 -0
  43. package/src/parallel-worker.ts +166 -210
  44. package/src/parallel.ts +73 -35
  45. package/src/parser.ts +297 -53
  46. package/src/resolve.ts +16 -15
  47. package/src/run-cli.ts +304 -29
  48. package/src/runtime.ts +1 -1
  49. package/src/spec-description.ts +7 -2
  50. package/src/standard-levels.ts +3 -3
  51. package/src/subexpressions.ts +10 -10
  52. package/src/successors.ts +17 -9
  53. package/src/symmetry.ts +14 -2
  54. package/src/tlc-ext.ts +2 -10
  55. package/src/tlc-order.ts +17 -3
  56. package/src/transfer.ts +22 -1
  57. package/src/values.ts +59 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tla-ts",
3
- "version": "0.0.2",
3
+ "version": "0.0.3-dev.252cf06",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
@@ -71,6 +71,8 @@
71
71
  "conformance:sany": "tsx scripts/proof-conformance.ts",
72
72
  "conformance:record": "tsx scripts/record-tlc.ts",
73
73
  "conformance:record-sany": "tsx scripts/record-sany.ts",
74
+ "conformance:record-tlapm": "tsx scripts/record-tlapm.ts",
75
+ "conformance:tlapm": "tsx scripts/tlapm-conformance.ts",
74
76
  "publint": "publint",
75
77
  "test": "vp test",
76
78
  "typecheck": "tsc --noEmit"
package/src/ast.ts CHANGED
@@ -44,6 +44,9 @@ export interface ApplyExpression {
44
44
  kind: "Apply";
45
45
  operator: string;
46
46
  args: Expression[];
47
+ // The number of args each part of a name like F(x)!l(y) takes, when a part before the last takes
48
+ // some.
49
+ argumentCounts?: number[];
47
50
  location: SourceLocation;
48
51
  resolution?: LocalResolution;
49
52
  }
@@ -64,6 +67,7 @@ export interface LambdaExpression {
64
67
  export interface JunctionExpression {
65
68
  kind: "And" | "Or";
66
69
  operands: Expression[];
70
+ isBulleted?: boolean;
67
71
  location: SourceLocation;
68
72
  }
69
73
 
@@ -129,6 +133,7 @@ export interface QuantifierExpression {
129
133
 
130
134
  export interface UnboundedQuantifierExpression {
131
135
  kind: "UnboundedQuantifier";
136
+ quantifier: "Forall" | "Exists";
132
137
  names: string[];
133
138
  isTemporal?: boolean;
134
139
  body: Expression;
@@ -317,6 +322,7 @@ export type Expression =
317
322
  export interface OperatorParam {
318
323
  name: string;
319
324
  arity: number;
325
+ location?: SourceLocation;
320
326
  }
321
327
 
322
328
  export interface OperatorDefinition {
@@ -368,6 +374,8 @@ export interface Instance {
368
374
  substitutions: InstanceSubstitution[];
369
375
  isLocal: boolean;
370
376
  location: SourceLocation;
377
+ instanceRange: SourceRange;
378
+ definitionRange?: SourceRange;
371
379
  }
372
380
 
373
381
  export interface NewSymbol {
@@ -388,18 +396,33 @@ export interface AssumeProve {
388
396
 
389
397
  export type Fact = Expression | { kind: "ModuleFact"; name: string; location: SourceLocation };
390
398
 
399
+ // keyword is the range of the USE or HIDE token and any ONLY after it.
391
400
  export interface UseOrHide {
392
401
  kind: "Use" | "Hide";
393
402
  isOnly: boolean;
394
403
  facts: Fact[];
395
404
  defs: Fact[];
405
+ keyword: SourceRange;
396
406
  location: SourceLocation;
397
407
  }
398
408
 
409
+ // keyword is the range of the BY, OBVIOUS, or OMITTED token.
399
410
  export type Proof =
400
- | { kind: "Obvious" | "Omitted"; location: SourceLocation }
401
- | { kind: "By"; isOnly: boolean; facts: Fact[]; defs: Fact[]; location: SourceLocation }
402
- | { kind: "Steps"; level: number; steps: ProofStep[]; location: SourceLocation };
411
+ | { kind: "Obvious" | "Omitted"; keyword: SourceRange; location: SourceLocation }
412
+ | {
413
+ kind: "By";
414
+ isOnly: boolean;
415
+ facts: Fact[];
416
+ defs: Fact[];
417
+ keyword: SourceRange;
418
+ location: SourceLocation;
419
+ }
420
+ | {
421
+ kind: "Steps";
422
+ level: number;
423
+ steps: ProofStep[];
424
+ location: SourceLocation;
425
+ };
403
426
 
404
427
  export interface DefineStep {
405
428
  kind: "Define";
@@ -419,9 +442,12 @@ export type StepBody =
419
442
  | { kind: "Assert"; isSuffices: boolean; statement: Expression | AssumeProve }
420
443
  | { kind: "Qed" };
421
444
 
445
+ // head is the range of the step number and its statement, without the proof.
422
446
  export interface ProofStep {
423
447
  name: string | undefined;
448
+ token: string;
424
449
  body: StepBody;
450
+ head: SourceRange;
425
451
  proof: Proof | undefined;
426
452
  location: SourceLocation;
427
453
  }
@@ -433,10 +459,23 @@ export interface Theorem {
433
459
  location: SourceLocation;
434
460
  }
435
461
 
462
+ // The units of a module in source order, for consumers that need the order across kinds.
463
+ export type ModuleUnit =
464
+ | { kind: "Constants"; declarations: Declaration[] }
465
+ | { kind: "Variables"; declarations: Declaration[] }
466
+ | { kind: "Recursive"; declarations: Declaration[] }
467
+ | { kind: "Definition"; definition: Definition }
468
+ | { kind: "Instance"; instance: Instance }
469
+ | { kind: "Assumption"; assumption: Assumption }
470
+ | { kind: "Theorem"; theorem: Theorem }
471
+ | { kind: "Use"; use: UseOrHide }
472
+ | { kind: "Module"; module: Module };
473
+
436
474
  export interface Module {
437
475
  name: string;
438
476
  modules: Module[];
439
477
  extends: string[];
478
+ extendsRanges: SourceRange[];
440
479
  constants: Declaration[];
441
480
  variables: Declaration[];
442
481
  definitions: Definition[];
@@ -445,4 +484,95 @@ export interface Module {
445
484
  location: SourceLocation;
446
485
  theorems: Theorem[];
447
486
  uses: UseOrHide[];
448
- }
487
+ units: ModuleUnit[];
488
+ }
489
+
490
+ export const getChildren = (expression: Expression): Expression[] => {
491
+ switch (expression.kind) {
492
+ case "Number":
493
+ case "Decimal":
494
+ case "String":
495
+ case "Identifier":
496
+ case "OperatorReference":
497
+ case "ExceptAt":
498
+ return [];
499
+ case "Apply":
500
+ return expression.args;
501
+ case "Lambda":
502
+ return [expression.body];
503
+ case "And":
504
+ case "Or":
505
+ return expression.operands;
506
+ case "Prime":
507
+ case "Unchanged":
508
+ case "Enabled":
509
+ case "Always":
510
+ case "Eventually":
511
+ return [expression.expression];
512
+ case "If":
513
+ return [expression.condition, expression.consequent, expression.alternative];
514
+ case "Case":
515
+ return [
516
+ ...expression.arms.flatMap((arm) => [arm.condition, arm.body]),
517
+ ...(expression.other ? [expression.other] : []),
518
+ ];
519
+ case "Let":
520
+ return [
521
+ ...expression.definitions.flatMap((definition) =>
522
+ definition.kind === "Function"
523
+ ? [...definition.bounds.map((bound) => bound.set), definition.body]
524
+ : [definition.body],
525
+ ),
526
+ expression.body,
527
+ ];
528
+ case "Forall":
529
+ case "Exists":
530
+ return [...expression.bounds.map((bound) => bound.set), expression.body];
531
+ case "UnboundedQuantifier":
532
+ return [expression.body];
533
+ case "Choose":
534
+ return [...(expression.set ? [expression.set] : []), expression.body];
535
+ case "SetEnumeration":
536
+ case "Tuple":
537
+ return expression.elements;
538
+ case "SetFilter":
539
+ return [expression.bound.set, expression.predicate];
540
+ case "SetMap":
541
+ return [expression.expression, ...expression.bounds.map((bound) => bound.set)];
542
+ case "FunctionConstruction":
543
+ return [...expression.bounds.map((bound) => bound.set), expression.body];
544
+ case "FunctionSet":
545
+ return [expression.domain, expression.range];
546
+ case "Record":
547
+ case "RecordSet":
548
+ return expression.fields.map((field) => field.value);
549
+ case "Except":
550
+ return [
551
+ expression.function,
552
+ ...expression.updates.flatMap((update) => [
553
+ ...update.path.flatMap((step) => (step.kind === "Index" ? step.args : [])),
554
+ update.value,
555
+ ]),
556
+ ];
557
+ case "FunctionApplication":
558
+ return [expression.function, ...expression.args];
559
+ case "FieldAccess":
560
+ return [expression.record];
561
+ case "Product":
562
+ return expression.factors;
563
+ case "BoxAction":
564
+ case "AngleAction":
565
+ return [expression.action, expression.subscript];
566
+ case "WeakFairness":
567
+ case "StrongFairness":
568
+ return [expression.subscript, expression.action];
569
+ case "Label":
570
+ return [expression.body];
571
+ case "Subexpression":
572
+ return expression.selectors.flatMap((selector) => ("args" in selector ? selector.args : []));
573
+ }
574
+ };
575
+
576
+ export const isLocallyResolved = (expression: Expression) =>
577
+ (expression.kind === "Identifier" || expression.kind === "Apply") &&
578
+ expression.resolution?.kind === "Local";
package/src/check.ts CHANGED
@@ -1,15 +1,27 @@
1
- import type { Assumption, Definition, Expression, SourceLocation } from "./ast";
1
+ import {
2
+ getChildren,
3
+ isLocallyResolved,
4
+ type Assumption,
5
+ type Definition,
6
+ type Expression,
7
+ type SourceLocation,
8
+ } from "./ast";
2
9
  import { parseConfig, type ConfigValue, type ModelConfig } from "./config";
3
10
  import { getReadVariables } from "./dependencies";
4
11
  import { createEnvironment } from "./environment";
5
- import { getErrorExitCode, TlaConfigError, TlaEvaluationError, TlaModuleError } from "./errors";
12
+ import {
13
+ getErrorExitCode,
14
+ TlaConfigError,
15
+ TlaEvaluationError,
16
+ TlaModuleError,
17
+ UNEXPECTED_EXCEPTION,
18
+ } from "./errors";
6
19
  import { EXIT_STATUS, setExitCode, setNextStatePhase, withLivenessExitCode } from "./exit-codes";
7
20
  import {
8
21
  describeRange,
9
22
  evaluate,
10
23
  expectBoolean,
11
24
  formatRange,
12
- getChildren,
13
25
  getDefinition,
14
26
  isConstantLevel,
15
27
  isImpure,
@@ -19,13 +31,11 @@ import {
19
31
  } from "./evaluate";
20
32
  import {
21
33
  createLivenessChecker,
22
- expandTemporal,
23
- getLevel,
24
34
  type Lasso,
35
+ type LivenessChecker,
25
36
  type LiveSuccessor,
26
- type ScopedExpression,
27
- type TemporalProperty,
28
- } from "./liveness";
37
+ } from "./live-check";
38
+ import { expandTemporal, getLevel, type ScopedExpression, type TemporalProperty } from "./liveness";
29
39
  import { ACTION_LEVEL, createLevelAnalyzer, TEMPORAL_LEVEL } from "./levels";
30
40
  import { prettyPrint } from "./pretty-print";
31
41
  import {
@@ -54,6 +64,7 @@ import {
54
64
  getValueHash,
55
65
  isFunctionValue,
56
66
  isSameValue,
67
+ isUpdatedElementOf,
57
68
  setStringOrder,
58
69
  toFiniteFunction,
59
70
  type Value,
@@ -93,6 +104,8 @@ export interface TraceStep {
93
104
 
94
105
  export interface Violation {
95
106
  name: string;
107
+ // Every temporal property that a liveness counterexample violates, in the configuration's order.
108
+ properties?: string[];
96
109
  trace: TraceStep[];
97
110
  loopStart: number | undefined;
98
111
  loopLabel?: string;
@@ -139,6 +152,13 @@ interface NamedExpression {
139
152
 
140
153
  interface StateCheck extends NamedExpression {
141
154
  variables: number[] | undefined;
155
+ membership: Membership | undefined;
156
+ }
157
+
158
+ // A conjunct `x \in S` whose S reads no variables.
159
+ interface Membership {
160
+ variable: number;
161
+ set: Expression;
142
162
  }
143
163
 
144
164
  interface ActionProperty {
@@ -481,14 +501,32 @@ const getPossiblePredicates = (names: string[], environment: Environment) =>
481
501
  return { name, expression, scope: undefined };
482
502
  });
483
503
 
504
+ const getMembership = (
505
+ expression: Expression,
506
+ variables: number[] | undefined,
507
+ environment: Environment,
508
+ ): Membership | undefined => {
509
+ if (expression.kind !== "Apply" || expression.operator !== "\\in" || variables?.length !== 1)
510
+ return undefined;
511
+ const element = expression.args[0];
512
+ if (element.kind !== "Identifier" || isLocallyResolved(element)) return undefined;
513
+ return environment.variableIndex.get(element.name) === variables[0]
514
+ ? { variable: variables[0], set: expression.args[1] }
515
+ : undefined;
516
+ };
517
+
484
518
  const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
485
519
  named.flatMap((item) =>
486
- getConjuncts(item.expression, environment).map((expression) => ({
487
- name: item.name,
488
- expression,
489
- scope: item.scope,
490
- variables: getReadVariables(expression, environment),
491
- })),
520
+ getConjuncts(item.expression, environment).map((expression) => {
521
+ const variables = getReadVariables(expression, environment);
522
+ return {
523
+ name: item.name,
524
+ expression,
525
+ scope: item.scope,
526
+ variables,
527
+ membership: getMembership(expression, variables, environment),
528
+ };
529
+ }),
492
530
  );
493
531
 
494
532
  const isUnchangedFor = (stateCheck: StateCheck, state: Value[], parent: Value[] | undefined) => {
@@ -743,10 +781,16 @@ export const createSearch = (
743
781
  invariants.find((invariant) => {
744
782
  if (isUnchangedFor(invariant, state, parent)) return false;
745
783
  try {
746
- return !holdsLikeTlc(
747
- invariant.expression,
748
- getContext(environment, state, undefined, invariant.scope),
749
- );
784
+ const context = getContext(environment, state, undefined, invariant.scope);
785
+ const membership = invariant.membership;
786
+ // The parent satisfies every invariant, so only the entries the step changed can fail.
787
+ return parent && membership
788
+ ? !isUpdatedElementOf(
789
+ state[membership.variable],
790
+ parent[membership.variable],
791
+ evaluate(membership.set, context),
792
+ )
793
+ : !holdsLikeTlc(invariant.expression, context);
750
794
  } catch (error) {
751
795
  // TLC reports a failure only on a state after the initial states as an invariant failure.
752
796
  if (!parent) throw error;
@@ -795,8 +839,7 @@ export const createSearch = (
795
839
  holdsLikeTlc(
796
840
  constraint.expression,
797
841
  getContext(environment, state, next),
798
- (expression) =>
799
- `TLC threw an unexpected exception.\nThis was probably caused by an error in the spec or model.\nSee the User Output or TLC Console for clues to what happened.\nThe exception was a java.lang.RuntimeException\n: ${getExpectedBoolean(expression, environment)}`,
842
+ (expression) => `${UNEXPECTED_EXCEPTION}${getExpectedBoolean(expression, environment)}`,
800
843
  ),
801
844
  ),
802
845
  trackStatePossible:
@@ -929,8 +972,36 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
929
972
  const getFingerprintState = search.getFingerprintState;
930
973
  const nextActions = { value: undefined as SpecAction[] | undefined };
931
974
  const isReducing = config.view !== undefined || config.symmetry !== undefined;
975
+ const tlcFingerprints: number[] = [];
932
976
  const liveness = withLivenessExitCode(() =>
933
- createLivenessChecker(search.properties.liveness, spec.temporals, environment),
977
+ createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
978
+ getFingerprint: (index) =>
979
+ (tlcFingerprints[index] ??= Number(
980
+ BigInt.asIntN(32, runtime.getFingerprint(states[index])),
981
+ )),
982
+ getSuccessors: (state) => {
983
+ const successors: LiveSuccessor[] = [];
984
+ if (spec.next === undefined) return successors;
985
+ forEachSuccessor(
986
+ spec.next,
987
+ state,
988
+ environment,
989
+ (successor) => {
990
+ canonicalizeState(successor.state);
991
+ const isInModel =
992
+ search.satisfiesConstraints(successor.state, state) &&
993
+ search.satisfiesActionConstraints(state, successor.state);
994
+ if (!isInModel) return false;
995
+ const fingerprintState = getFingerprintState(successor.state);
996
+ const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
997
+ successors.push({ index, state: successor.state });
998
+ return false;
999
+ },
1000
+ spec.scope,
1001
+ );
1002
+ return successors;
1003
+ },
1004
+ }),
934
1005
  );
935
1006
  const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
936
1007
 
@@ -1071,7 +1142,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1071
1142
  outDegrees: failureOutDegrees,
1072
1143
  };
1073
1144
  };
1074
- const getPathTrace = (path: number[], loopStart?: number) => {
1145
+ const getPathSteps = (path: number[], loopStart?: number) => {
1075
1146
  const steps = getPathStates(path, NEXT_RECOVERY_FAILURE);
1076
1147
  const last = steps[steps.length - 1].state;
1077
1148
  const isClosed =
@@ -1080,7 +1151,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1080
1151
  loopStart === path.length - 1 ||
1081
1152
  findSuccessorWithFingerprint(last, path[loopStart]) !== undefined;
1082
1153
  if (!isClosed) throw new TlaEvaluationError(NEXT_RECOVERY_FAILURE);
1083
- return toTrace(steps, path, loopStart);
1154
+ return steps;
1084
1155
  };
1085
1156
  const getStateHash = (state: Value[]) => {
1086
1157
  let hash = 0x811c9dc5;
@@ -1146,7 +1217,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1146
1217
  };
1147
1218
  runtime.getCurrentState = () => (current.index === -1 ? undefined : states[current.index]);
1148
1219
  runtime.setLivenessSource = (index) => {
1149
- current.livenessSource = index;
1220
+ current.livenessSource = index ?? -1;
1150
1221
  };
1151
1222
  runtime.getLevel = (state) => {
1152
1223
  const source = current.livenessSource;
@@ -1302,32 +1373,43 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1302
1373
  }
1303
1374
  try {
1304
1375
  const accepted = liveness?.addState(index, state, liveSuccessors);
1305
- current.livenessSource = -1;
1306
- if (!accepted) return undefined;
1376
+ if (!liveness || !accepted) return undefined;
1377
+ const steps = getPathSteps(accepted);
1378
+ const properties = liveness.getViolatedProperties(
1379
+ steps.map((step) => step.state),
1380
+ steps.length - 1,
1381
+ );
1307
1382
  return reportViolation("property-violation", {
1308
- name: accepted.name,
1309
- trace: getPathTrace(accepted.path),
1383
+ name: properties[0],
1384
+ properties,
1385
+ trace: toTrace(steps, accepted),
1310
1386
  });
1311
1387
  } catch (error) {
1312
1388
  return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
1313
1389
  }
1314
1390
  };
1315
- const reportLasso = (lasso: Lasso) => {
1391
+ // TLC names the properties and the step back into the cycle from the states it prints.
1392
+ const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
1316
1393
  const path = [...lasso.prefix, ...lasso.cycle];
1317
- const trace = getPathTrace(path, lasso.prefix.length);
1318
- const back = path[lasso.prefix.length];
1394
+ const loopStart = lasso.prefix.length;
1395
+ const steps = getPathSteps(path, loopStart);
1396
+ const trace = toTrace(steps, path, loopStart);
1397
+ const properties = checker.getViolatedProperties(
1398
+ steps.map((step) => step.state),
1399
+ loopStart,
1400
+ );
1401
+ const back = path[loopStart];
1319
1402
  const last = path[path.length - 1];
1403
+ const lastState = steps[steps.length - 1].state;
1320
1404
  const loopLabel =
1321
- back === last ? undefined : withoutOutput(() => getLabel(last, states[last], states[back]));
1405
+ back === last ? undefined : withoutOutput(() => getLabel(last, lastState, states[back]));
1322
1406
  return reportViolation(
1323
1407
  "liveness-violation",
1324
- { name: lasso.name, trace, loopStart: lasso.prefix.length, loopLabel },
1408
+ { name: properties[0], properties, trace, loopStart, loopLabel },
1325
1409
  () =>
1326
1410
  back === last
1327
1411
  ? trace[trace.length - 1].action
1328
- : getTraceSuccessors(states[last]).find((successor) =>
1329
- isSameState(successor.state, states[back]),
1330
- )?.action,
1412
+ : findSuccessorWithFingerprint(lastState, back)?.action,
1331
1413
  );
1332
1414
  };
1333
1415
  const isPeriodic = liveness !== undefined && options.livenessCheck !== "final";
@@ -1340,7 +1422,6 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1340
1422
  if (!isGrown || periodic.time > (now - startedAt) * LIVENESS_RATIO) return undefined;
1341
1423
  periodic.size = expanded;
1342
1424
  const lasso = liveness?.findViolation();
1343
- current.livenessSource = -1;
1344
1425
  periodic.time += performance.now() - now;
1345
1426
  return lasso;
1346
1427
  };
@@ -1360,15 +1441,14 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1360
1441
  expansion.newStates = 0;
1361
1442
  if (isPeriodic && performance.now() >= periodic.at) {
1362
1443
  const lasso = findPeriodicViolation(index + 1);
1363
- if (lasso) return reportLasso(lasso);
1444
+ if (liveness && lasso) return reportLasso(liveness, lasso);
1364
1445
  }
1365
1446
  }
1366
1447
 
1367
1448
  current.index = -1;
1368
1449
  current.candidate = undefined;
1369
1450
  const lasso = liveness?.findViolation();
1370
- current.livenessSource = -1;
1371
- if (lasso) return reportLasso(lasso);
1451
+ if (liveness && lasso) return reportLasso(liveness, lasso);
1372
1452
 
1373
1453
  const postconditionViolation = search.findPostconditionViolation();
1374
1454
  if (postconditionViolation) {
@@ -0,0 +1,58 @@
1
+ export interface OptionSpec {
2
+ flag: string;
3
+ isRequired?: boolean;
4
+ isRepeatable?: boolean;
5
+ }
6
+
7
+ export interface ParsedOption {
8
+ name: string;
9
+ values: string[];
10
+ }
11
+
12
+ export interface ParsedArgs {
13
+ options: ParsedOption[];
14
+ positionals: string[];
15
+ unknown: string[];
16
+ incomplete: ParsedOption | undefined;
17
+ has: (name: string) => boolean;
18
+ get: (name: string) => string[] | undefined;
19
+ }
20
+
21
+ const formatOption = (spec: OptionSpec) => {
22
+ const text = spec.isRequired ? spec.flag : `[${spec.flag}]`;
23
+ return spec.isRepeatable ? `${text}...` : text;
24
+ };
25
+
26
+ // Each <name> in a flag such as "--toolbox <start> <end>" is one value the option takes, and
27
+ // {options} in the usage stands for the options.
28
+ export const defineCommand = (usage: string, specs: OptionSpec[]) => {
29
+ const arities = new Map(
30
+ specs.map((spec) => {
31
+ const [name, ...values] = spec.flag.split(" ");
32
+ return [name, values.length];
33
+ }),
34
+ );
35
+ const parse = (args: string[]): ParsedArgs => {
36
+ const options: ParsedOption[] = [];
37
+ const positionals: string[] = [];
38
+ const unknown: string[] = [];
39
+ for (let index = 0; index < args.length; index++) {
40
+ const arity = arities.get(args[index]);
41
+ if (arity === undefined) {
42
+ (args[index].startsWith("-") ? unknown : positionals).push(args[index]);
43
+ continue;
44
+ }
45
+ options.push({ name: args[index], values: args.slice(index + 1, index + 1 + arity) });
46
+ index += arity;
47
+ }
48
+ return {
49
+ options,
50
+ positionals,
51
+ unknown,
52
+ incomplete: options.find((option) => option.values.length < (arities.get(option.name) ?? 0)),
53
+ has: (name) => options.some((option) => option.name === name),
54
+ get: (name) => options.findLast((option) => option.name === name)?.values,
55
+ };
56
+ };
57
+ return { usage: usage.replace("{options}", specs.map(formatOption).join(" ")), parse };
58
+ };
package/src/codegen.ts CHANGED
@@ -1,11 +1,10 @@
1
- import type { Expression } from "./ast";
1
+ import { getChildren, isLocallyResolved, type Expression } from "./ast";
2
2
  import { TlaEvaluationError } from "./errors";
3
3
  import {
4
4
  describeLocation,
5
5
  bindValue,
6
6
  expectBoolean,
7
7
  findGlobalName,
8
- getChildren,
9
8
  getEnumeratedElements,
10
9
  getConditionError,
11
10
  getConjunctError,
@@ -14,7 +13,6 @@ import {
14
13
  getEvaluator,
15
14
  getFieldError,
16
15
  isClosedConstant,
17
- isLocallyResolved,
18
16
  isTlcInteger,
19
17
  isValueArgument,
20
18
  isWorthCaching,
@@ -1,5 +1,5 @@
1
- import type { Definition, Expression } from "./ast";
2
- import { getChildren, isImpureBuiltin, type Environment } from "./evaluate";
1
+ import { getChildren, type Definition, type Expression } from "./ast";
2
+ import { isImpureBuiltin, type Environment } from "./evaluate";
3
3
 
4
4
  const STATE_INDEPENDENT_KINDS = new Set([
5
5
  "Prime",
@@ -1,12 +1,13 @@
1
- import type {
2
- Assumption,
3
- Declaration,
4
- Definition,
5
- Expression,
6
- Instance,
7
- Module,
8
- OperatorParam,
9
- SourceLocation,
1
+ import {
2
+ getChildren,
3
+ type Assumption,
4
+ type Declaration,
5
+ type Definition,
6
+ type Expression,
7
+ type Instance,
8
+ type Module,
9
+ type OperatorParam,
10
+ type SourceLocation,
10
11
  } from "./ast";
11
12
  import { BUNDLED_MODULES } from "./bundled-modules";
12
13
  import type { ConfigValue, ModelConfig } from "./config";
@@ -16,7 +17,6 @@ import { EXIT_STATUS, setExitCode } from "./exit-codes";
16
17
  import {
17
18
  createBigNumberError,
18
19
  createRealNumberError,
19
- getChildren,
20
20
  isConstantLevel,
21
21
  isTlcInteger,
22
22
  type Builtin,