tla-ts 0.0.3-dev.e5b05b9 → 0.0.3-dev.e8bef80

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 (79) 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 +150 -48
  5. package/dist/index.d.ts +150 -48
  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 -22
  12. package/dist/run-cli.js +1352 -23
  13. package/dist/transfer.cjs +83 -43
  14. package/dist/transfer.js +83 -43
  15. package/package.json +5 -1
  16. package/src/ast.ts +28 -4
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -17
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1317 -428
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +226 -207
  25. package/src/environment.ts +221 -75
  26. package/src/errors.ts +130 -15
  27. package/src/evaluate.ts +713 -259
  28. package/src/exit-codes.ts +6 -24
  29. package/src/fingerprints.ts +1 -2
  30. package/src/index.ts +1 -0
  31. package/src/instances.ts +3 -1
  32. package/src/java-overrides.ts +104 -25
  33. package/src/levels.ts +6 -1
  34. package/src/lexer.ts +122 -29
  35. package/src/live-check.ts +204 -148
  36. package/src/liveness.ts +191 -122
  37. package/src/obligation-expression.ts +2 -2
  38. package/src/obligation-source.ts +2 -0
  39. package/src/parallel-worker.ts +6 -1
  40. package/src/parallel.ts +57 -6
  41. package/src/parser.ts +327 -147
  42. package/src/pcal-ast.ts +770 -0
  43. package/src/pcal-parse.ts +1523 -0
  44. package/src/pcal-tokenize.ts +516 -0
  45. package/src/pcal-translate.ts +1230 -0
  46. package/src/pcal-validate.ts +454 -0
  47. package/src/randomization.ts +24 -7
  48. package/src/resolve.ts +252 -87
  49. package/src/run-cli.ts +1162 -200
  50. package/src/runtime.ts +16 -0
  51. package/src/sany-builtins.ts +147 -0
  52. package/src/sany-generator.ts +3128 -0
  53. package/src/sany-levels.ts +1208 -0
  54. package/src/sany-lexer.ts +410 -480
  55. package/src/sany-linter.ts +122 -0
  56. package/src/sany-modules.ts +1398 -0
  57. package/src/sany-nodes.ts +463 -0
  58. package/src/sany-parse-error.ts +2033 -3015
  59. package/src/sany-report.ts +356 -0
  60. package/src/sany-selectors.ts +738 -0
  61. package/src/sany-source.ts +248 -0
  62. package/src/sany-xml.ts +1017 -0
  63. package/src/spec-description.ts +25 -5
  64. package/src/standard-modules.ts +172 -129
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +121 -26
  67. package/src/successors.ts +141 -173
  68. package/src/symmetry.ts +168 -50
  69. package/src/tableau.ts +3 -1
  70. package/src/tlc-ext.ts +13 -17
  71. package/src/tlc-messages.ts +329 -0
  72. package/src/tlc-module.ts +49 -21
  73. package/src/tlc-modules.ts +0 -4
  74. package/src/tlc-order.ts +55 -9
  75. package/src/tlc-random.ts +28 -3
  76. package/src/tlc-replay.ts +655 -0
  77. package/src/tlc-version.ts +18 -0
  78. package/src/transfer.ts +15 -3
  79. package/src/values.ts +664 -317
@@ -1,13 +1,18 @@
1
1
  import {
2
2
  getChildren,
3
+ type AssumeProve,
4
+ type Bound,
3
5
  type Definition,
4
6
  type Expression,
7
+ type Fact,
5
8
  type Instance,
6
9
  type Module,
7
10
  type OperatorParam,
11
+ type Proof,
8
12
  type Selector,
9
13
  type SourceLocation,
10
14
  type SourceRange,
15
+ type StepBody,
11
16
  } from "./ast";
12
17
  import { formatRange, TlaSyntaxError } from "./errors";
13
18
  import { getSourceName, mapChildren, mapDefinition } from "./instances";
@@ -330,19 +335,26 @@ const containsReference = (expression: Expression): boolean =>
330
335
  (expression.kind === "Apply" && expression.operator.includes("!")) ||
331
336
  getChildren(expression).some(containsReference);
332
337
 
333
- // A reference into a theorem whose statement is an ASSUME/PROVE stays for name resolution to check.
334
- // isOperator holds where an operator may be given, as the argument of an operator that may take one.
338
+ // A reference that starts with a name isGoal holds for, such as a theorem whose statement is an
339
+ // ASSUME/PROVE, stays for name resolution to check. isOperator holds where an operator may be given,
340
+ // as the argument of an operator that may take one.
335
341
  const resolveSubexpressions = (
336
342
  expression: Expression,
337
343
  lookup: Lookup,
338
- goals: ReadonlySet<string>,
344
+ isGoal: (name: string) => boolean,
339
345
  isOperator = false,
340
346
  ): Expression => {
341
347
  if (!containsReference(expression)) return expression;
342
348
  if (expression.kind === "Let") {
343
349
  const local: Lookup = (name) =>
344
350
  expression.definitions.find((definition) => definition.name === name) ?? lookup(name);
345
- return mapChildren(expression, (child) => resolveSubexpressions(child, local, goals));
351
+ const mapped = mapChildren(expression, (child) => resolveSubexpressions(child, local, isGoal));
352
+ return mapped.kind === "Let" && mapped.instances
353
+ ? {
354
+ ...mapped,
355
+ instances: mapped.instances.map((instance) => resolveInstance(instance, local, isGoal)),
356
+ }
357
+ : mapped;
346
358
  }
347
359
  const callee = expression.kind === "Apply" ? lookup(expression.operator) : undefined;
348
360
  const mapped =
@@ -353,12 +365,12 @@ const resolveSubexpressions = (
353
365
  resolveSubexpressions(
354
366
  arg,
355
367
  lookup,
356
- goals,
368
+ isGoal,
357
369
  callee?.kind !== "Operator" || callee.params[index]?.arity !== 0,
358
370
  ),
359
371
  ),
360
372
  }
361
- : mapChildren(expression, (child) => resolveSubexpressions(child, lookup, goals));
373
+ : mapChildren(expression, (child) => resolveSubexpressions(child, lookup, isGoal));
362
374
  const selectors =
363
375
  mapped.kind === "Subexpression"
364
376
  ? mapped.selectors
@@ -368,47 +380,130 @@ const resolveSubexpressions = (
368
380
  ? getQualifiedSelectors(mapped.operator, mapped.args, mapped.argumentCounts, lookup)
369
381
  : undefined;
370
382
  const first = selectors?.[0];
371
- if (!selectors || (first?.kind === "Name" && goals.has(first.name))) return mapped;
383
+ if (!selectors || (first?.kind === "Name" && isGoal(first.name))) return mapped;
372
384
  const selected = selectSubexpression(selectors, lookup, mapped, isOperator);
373
385
  if (selected !== mapped) selections.add(selected);
374
386
  return selected;
375
387
  };
376
388
 
389
+ const resolveInstance = (
390
+ instance: Instance,
391
+ lookup: Lookup,
392
+ isGoal: (name: string) => boolean,
393
+ ): Instance => ({
394
+ ...instance,
395
+ substitutions: instance.substitutions.map((substitution) => ({
396
+ ...substitution,
397
+ expression: resolveSubexpressions(substitution.expression, lookup, isGoal, true),
398
+ })),
399
+ });
400
+
377
401
  const createLookup = (definitions: Definition[]): Lookup => {
378
402
  const byName = new Map(definitions.map((definition) => [definition.name, definition]));
379
403
  return (name) => byName.get(name);
380
404
  };
381
405
 
382
- export const resolveInstanceSubexpressions = (instance: Instance, module: Module): Instance => {
383
- const lookup = createLookup(module.definitions);
384
- return {
385
- ...instance,
386
- substitutions: instance.substitutions.map((substitution) => ({
387
- ...substitution,
388
- expression: resolveSubexpressions(substitution.expression, lookup, new Set(), true),
389
- })),
390
- };
391
- };
406
+ export const resolveInstanceSubexpressions = (instance: Instance, module: Module): Instance =>
407
+ resolveInstance(instance, createLookup(module.definitions), () => false);
392
408
 
409
+ // Generator.selectorToNode resolves a name in a theorem, a proof, or a fact as it does in a definition.
393
410
  export const resolveModuleSubexpressions = (module: Module, available: Definition[]): Module => {
394
411
  const lookup = createLookup([...available, ...module.definitions]);
395
- const hasReferences =
396
- module.definitions.some((definition) => containsReference(definition.body)) ||
397
- module.assumptions.some((assumption) => containsReference(assumption.expression));
398
- if (!hasReferences) return module;
399
412
  const goals = new Set(
400
413
  module.theorems.flatMap((theorem) =>
401
414
  theorem.name !== undefined && theorem.statement.kind === "AssumeProve" ? [theorem.name] : [],
402
415
  ),
403
416
  );
404
- return {
417
+ const isGoal = (name: string) => goals.has(name);
418
+ const resolve = (expression: Expression) => resolveSubexpressions(expression, lookup, isGoal);
419
+ // A name in a theorem, a proof, or a fact may also start with a step, a theorem, or a symbol that
420
+ // no definition names.
421
+ const isProofGoal = (name: string) => isGoal(name) || !lookup(name);
422
+ const resolveInProof = (expression: Expression) =>
423
+ resolveSubexpressions(expression, lookup, isProofGoal);
424
+ const resolveFacts = (facts: Fact[]) =>
425
+ facts.map((fact) => (fact.kind === "ModuleFact" ? fact : resolveInProof(fact)));
426
+ const resolveBounds = (bounds: Bound[]) =>
427
+ bounds.map((bound) => ({ ...bound, set: resolveInProof(bound.set) }));
428
+ const resolveStatement = (statement: Expression | AssumeProve): Expression | AssumeProve =>
429
+ statement.kind === "AssumeProve"
430
+ ? {
431
+ ...statement,
432
+ assumptions: statement.assumptions.map((assumption) =>
433
+ assumption.kind === "NewSymbol"
434
+ ? { ...assumption, set: assumption.set && resolveInProof(assumption.set) }
435
+ : resolveStatement(assumption),
436
+ ),
437
+ conclusion: resolveInProof(statement.conclusion),
438
+ }
439
+ : resolveInProof(statement);
440
+ const resolveStep = (body: StepBody): StepBody => {
441
+ switch (body.kind) {
442
+ case "Use":
443
+ case "Hide":
444
+ return { ...body, facts: resolveFacts(body.facts) };
445
+ case "Instance":
446
+ return resolveInstance(body, lookup, isProofGoal);
447
+ case "Define":
448
+ return {
449
+ ...body,
450
+ definitions: body.definitions.map((definition) =>
451
+ definition.kind === "Instance"
452
+ ? resolveInstance(definition, lookup, isProofGoal)
453
+ : mapDefinition(definition, resolveInProof),
454
+ ),
455
+ };
456
+ case "Have":
457
+ case "Case":
458
+ return { ...body, expression: resolveInProof(body.expression) };
459
+ case "Witness":
460
+ return { ...body, expressions: body.expressions.map(resolveInProof) };
461
+ case "Take":
462
+ return { ...body, bounds: resolveBounds(body.bounds) };
463
+ case "Pick":
464
+ return { ...body, bounds: resolveBounds(body.bounds), body: resolveInProof(body.body) };
465
+ case "Assert":
466
+ return { ...body, statement: resolveStatement(body.statement) };
467
+ default:
468
+ return body;
469
+ }
470
+ };
471
+ const resolveProof = (proof: Proof | undefined): Proof | undefined =>
472
+ proof?.kind === "By"
473
+ ? { ...proof, facts: resolveFacts(proof.facts) }
474
+ : proof?.kind === "Steps"
475
+ ? {
476
+ ...proof,
477
+ steps: proof.steps.map((step) => ({
478
+ ...step,
479
+ body: resolveStep(step.body),
480
+ proof: resolveProof(step.proof),
481
+ })),
482
+ }
483
+ : proof;
484
+ const resolved = {
405
485
  ...module,
406
- definitions: module.definitions.map((definition) =>
407
- mapDefinition(definition, (child) => resolveSubexpressions(child, lookup, goals)),
408
- ),
486
+ // A named theorem's statement is resolved as its definition.
487
+ theorems: module.theorems.map((theorem) => ({
488
+ ...theorem,
489
+ statement:
490
+ theorem.name !== undefined && theorem.statement.kind !== "AssumeProve"
491
+ ? theorem.statement
492
+ : resolveStatement(theorem.statement),
493
+ proof: resolveProof(theorem.proof),
494
+ })),
495
+ uses: module.uses.map((use) => ({ ...use, facts: resolveFacts(use.facts) })),
496
+ };
497
+ const hasReferences =
498
+ module.definitions.some((definition) => containsReference(definition.body)) ||
499
+ module.assumptions.some((assumption) => containsReference(assumption.expression));
500
+ if (!hasReferences) return resolved;
501
+ return {
502
+ ...resolved,
503
+ definitions: module.definitions.map((definition) => mapDefinition(definition, resolve)),
409
504
  assumptions: module.assumptions.map((assumption) => ({
410
505
  ...assumption,
411
- expression: resolveSubexpressions(assumption.expression, lookup, goals),
506
+ expression: resolve(assumption.expression),
412
507
  })),
413
508
  };
414
509
  };