tla-ts 0.0.10-dev.96638cd93 → 0.0.10-dev.a17d6d9fa

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 (89) hide show
  1. package/dist/deciders.cjs +30 -30
  2. package/dist/deciders.js +30 -30
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +1 -1
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +1 -1
  9. package/dist/prove.cjs +60 -60
  10. package/dist/prove.js +73 -73
  11. package/dist/run.cjs +42 -42
  12. package/dist/run.js +42 -42
  13. package/dist/transfer.cjs +34 -34
  14. package/dist/transfer.js +29 -29
  15. package/dist/worker.cjs +1 -1
  16. package/dist/worker.js +1 -1
  17. package/package.json +1 -1
  18. package/src/bags.ts +5 -2
  19. package/src/builtins.ts +4 -3
  20. package/src/check-failure.ts +1 -1
  21. package/src/check-trace.ts +90 -51
  22. package/src/check.ts +251 -133
  23. package/src/cli/tlc.ts +3 -2
  24. package/src/codegen.ts +18 -5
  25. package/src/dfid.ts +13 -8
  26. package/src/environment.ts +1 -0
  27. package/src/evaluate.ts +25 -9
  28. package/src/exit-codes.ts +1 -0
  29. package/src/java-hash.ts +14 -0
  30. package/src/liveness-checker.ts +63 -31
  31. package/src/liveness.ts +48 -10
  32. package/src/parser.ts +6 -1
  33. package/src/prover/smt/solver/arith-rewriter.ts +11 -3
  34. package/src/prover/smt/solver/array-rewriter.ts +12 -2
  35. package/src/prover/smt/solver/asserted-formulas.ts +109 -63
  36. package/src/prover/smt/solver/ast.ts +5 -3
  37. package/src/prover/smt/solver/auf-solver.ts +23 -12
  38. package/src/prover/smt/solver/bit-blast-tactic.ts +26 -0
  39. package/src/prover/smt/solver/bound-simplifier.ts +10 -11
  40. package/src/prover/smt/solver/card2bv.ts +31 -15
  41. package/src/prover/smt/solver/cofactor-elim-term-ite.ts +39 -23
  42. package/src/prover/smt/solver/context.ts +24 -7
  43. package/src/prover/smt/solver/dependent-expr-state.ts +1 -1
  44. package/src/prover/smt/solver/elim-bounds.ts +89 -0
  45. package/src/prover/smt/solver/elim-uncnstr.ts +1 -1
  46. package/src/prover/smt/solver/internalizer.ts +1 -1
  47. package/src/prover/smt/solver/lp/lp-settings.ts +4 -0
  48. package/src/prover/smt/solver/model-evaluator.ts +2 -0
  49. package/src/prover/smt/solver/model-generator.ts +82 -42
  50. package/src/prover/smt/solver/opt-context.ts +11 -4
  51. package/src/prover/smt/solver/opt-pareto.ts +19 -0
  52. package/src/prover/smt/solver/opt-solver.ts +3 -2
  53. package/src/prover/smt/solver/poly-rewriter.ts +1 -0
  54. package/src/prover/smt/solver/proto-model.ts +4 -2
  55. package/src/prover/smt/solver/push-app-ite.ts +37 -0
  56. package/src/prover/smt/solver/qsat.ts +16 -6
  57. package/src/prover/smt/solver/rewriter.ts +31 -25
  58. package/src/prover/smt/solver/sat/goal2sat.ts +0 -1
  59. package/src/prover/smt/solver/sat/inc-sat-solver.ts +13 -4
  60. package/src/prover/smt/solver/sat/sat-tactic.ts +9 -2
  61. package/src/prover/smt/solver/smt-kernel.ts +30 -2
  62. package/src/prover/smt/solver/smt-setup.ts +28 -4
  63. package/src/prover/smt/solver/smtlib-parser.ts +26 -3
  64. package/src/prover/smt/solver/smtlib-script.ts +34 -21
  65. package/src/prover/smt/solver/solve-eqs.ts +24 -17
  66. package/src/prover/smt/solver/solver.ts +18 -3
  67. package/src/prover/smt/solver/tactic.ts +68 -41
  68. package/src/prover/smt/solver/theory-bv.ts +11 -1
  69. package/src/prover/smt/solver/theory-fpa.ts +1 -1
  70. package/src/prover/smt/solver/theory-lra-final-check.ts +1 -0
  71. package/src/prover/smt/solver/theory-lra-internalize.ts +1 -0
  72. package/src/prover/smt/solver/theory-lra-propagate.ts +7 -1
  73. package/src/prover/smt/solver/theory-lra.ts +1 -0
  74. package/src/prover/smt/solver/tseitin-cnf.ts +1 -0
  75. package/src/resolve.ts +120 -52
  76. package/src/runtime.ts +4 -0
  77. package/src/search.ts +117 -81
  78. package/src/simulate.ts +110 -50
  79. package/src/spec-description.ts +3 -4
  80. package/src/standard-modules.ts +15 -6
  81. package/src/subexpressions.ts +34 -9
  82. package/src/successors.ts +3 -2
  83. package/src/tlc/random.ts +2 -1
  84. package/src/tlc/replay.ts +3 -2
  85. package/src/tlc/tlc-operators.ts +7 -7
  86. package/src/tlc/tlcext-operators.ts +26 -10
  87. package/src/tlc/trace-spec.ts +6 -20
  88. package/src/typed-tier.ts +3 -2
  89. package/src/values.ts +95 -57
package/src/codegen.ts CHANGED
@@ -165,9 +165,16 @@ const getFreeNames = (expression: Expression, names = new Set<string>()) => {
165
165
  return names;
166
166
  };
167
167
 
168
- const getDelegateContext = (generator: Generator, expression: Expression, frame: Frame) => {
168
+ const getDelegateContext = (
169
+ generator: Generator,
170
+ expression: Expression,
171
+ frame: Frame,
172
+ isEveryLocalCaptured = false,
173
+ ) => {
169
174
  const freeNames = getFreeNames(expression);
170
- const captured = [...frame.locals].filter((entry) => freeNames.has(entry[0]));
175
+ const captured = [...frame.locals].filter(
176
+ (entry) => isEveryLocalCaptured || freeNames.has(entry[0]),
177
+ );
171
178
  if (captured.length === 0) return frame.context;
172
179
  const names = addReference(
173
180
  generator,
@@ -176,8 +183,13 @@ const getDelegateContext = (generator: Generator, expression: Expression, frame:
176
183
  return `extend(${frame.context}, ${names}, [${captured.map((entry) => entry[1]).join(", ")}])`;
177
184
  };
178
185
 
179
- const generateDelegate = (generator: Generator, expression: Expression, frame: Frame) =>
180
- `${addReference(generator, getEvaluator(expression, generator.environment))}(${getDelegateContext(generator, expression, frame)})`;
186
+ const generateDelegate = (
187
+ generator: Generator,
188
+ expression: Expression,
189
+ frame: Frame,
190
+ isEveryLocalCaptured = false,
191
+ ) =>
192
+ `${addReference(generator, getEvaluator(expression, generator.environment))}(${getDelegateContext(generator, expression, frame, isEveryLocalCaptured)})`;
181
193
 
182
194
  const generateHelper = (
183
195
  generator: Generator,
@@ -318,7 +330,8 @@ const generateApply = (
318
330
  const domain = addReference(generator, getDomainOperator(expression));
319
331
  return `${domain}(${generateExpression(generator, args[0], frame)})`;
320
332
  }
321
- if (builtin.name === "TLCEval") return generateDelegate(generator, expression, frame);
333
+ // TLC caches a constant TLCEval only where no name is bound, so the delegate sees every local.
334
+ if (builtin.name === "TLCEval") return generateDelegate(generator, expression, frame, true);
322
335
  if (builtin.name === "\\subseteq" && args.length === 2) {
323
336
  const subset = addReference(generator, getSubsetOperator(expression, builtin));
324
337
  return `${subset}(${generateExpression(generator, args[0], frame)}, ${generateExpression(generator, args[1], frame)})`;
package/src/dfid.ts CHANGED
@@ -131,7 +131,7 @@ export const checkDepthFirst = (options: DepthFirstOptions): number => {
131
131
  withUnexpectedException(() =>
132
132
  createLivenessChecker(temporalProperties, search.spec.temporals, environment, {
133
133
  getFingerprint: () => 0,
134
- getSuccessors: () => [],
134
+ forEachActionSuccessors: () => {},
135
135
  }),
136
136
  ),
137
137
  )?.branches;
@@ -268,16 +268,21 @@ export const checkDepthFirst = (options: DepthFirstOptions): number => {
268
268
  config.alias === undefined
269
269
  ? search.toRecord(state)
270
270
  : search.evaluateAlias(config.alias, state, state, isTool);
271
- const violated = isContinued
272
- ? search.getViolatedInvariants(state)
273
- : [search.findViolatedInvariant(state)].filter((invariant) => invariant !== undefined);
274
- for (const invariant of violated) {
271
+ const reportInvariant = (name: string) =>
275
272
  message(
276
273
  MESSAGE_CODE.invariantViolatedInitial,
277
- `Invariant ${invariant.name} is violated by the initial state:\n${formatState(alias())}`,
274
+ `Invariant ${name} is violated by the initial state:\n${formatState(alias())}`,
278
275
  );
279
- if (!isContinued) return MESSAGE_CODE.invariantViolatedInitial;
280
- checker.continuedCode = MESSAGE_CODE.invariantViolatedInitial;
276
+ if (isContinued) {
277
+ search.forEachViolatedInvariant(state, (invariant) => {
278
+ reportInvariant(invariant.name);
279
+ checker.continuedCode = MESSAGE_CODE.invariantViolatedInitial;
280
+ });
281
+ }
282
+ const violated = isContinued ? undefined : search.findViolatedInvariant(state);
283
+ if (violated) {
284
+ reportInvariant(violated.name);
285
+ return MESSAGE_CODE.invariantViolatedInitial;
281
286
  }
282
287
  const predicate = search.findFailedInitialPredicate(state);
283
288
  if (predicate) {
@@ -1131,6 +1131,7 @@ export const createEnvironment = (
1131
1131
  variableIndex,
1132
1132
  facts,
1133
1133
  startupValues: Array.from({ length: facts.ids.size }, (): Value | undefined => undefined),
1134
+ tlcEvalValues: new WeakMap(),
1134
1135
  substitutedDefinitions,
1135
1136
  instanceParams,
1136
1137
  evaluators: new WeakMap(),
package/src/evaluate.ts CHANGED
@@ -152,6 +152,7 @@ export interface Environment {
152
152
  variableIndex: Map<string, number>;
153
153
  facts: DefinitionFacts;
154
154
  startupValues: (Value | undefined)[];
155
+ tlcEvalValues: WeakMap<Expression, Value>;
155
156
  substitutedDefinitions: Set<Definition>;
156
157
  instanceParams: Set<OperatorParam>;
157
158
  evaluators: WeakMap<Expression, Evaluator>;
@@ -523,10 +524,13 @@ const someElement = (
523
524
  return false;
524
525
  };
525
526
 
526
- export const mapBoundSets = <Result>(bounds: Bound[], map: (set: Expression) => Result) =>
527
+ export const mapBoundSets = <Result>(
528
+ bounds: Bound[],
529
+ map: (set: Expression, index: number) => Result,
530
+ ) =>
527
531
  bounds.reduce<Result[]>((results, bound, index) => {
528
532
  results.push(
529
- index > 0 && bounds[index - 1].set === bound.set ? results[index - 1] : map(bound.set),
533
+ index > 0 && bounds[index - 1].set === bound.set ? results[index - 1] : map(bound.set, index),
530
534
  );
531
535
  return results;
532
536
  }, []);
@@ -1592,11 +1596,17 @@ const compileApply = (
1592
1596
  // Deviation from TLC: TLCEval#tlcEval evaluates an argument SANY takes for constant, such as Print(v, TRUE), in an empty state.
1593
1597
  const isCached =
1594
1598
  !contextualApplications.has(expression) && !isNonconstant(args[0], environment);
1595
- return (context) =>
1596
- global.evaluate(
1597
- getLazyArguments(args, context),
1598
- isCached && context.scope === undefined ? undefined : context,
1599
- );
1599
+ const tlcEvalValues = environment.tlcEvalValues;
1600
+ return (context) => {
1601
+ if (!isCached || context.scope !== undefined) {
1602
+ return global.evaluate(getLazyArguments(args, context), context);
1603
+ }
1604
+ const cached = tlcEvalValues.get(expression);
1605
+ if (cached !== undefined) return cached;
1606
+ const value = global.evaluate(getLazyArguments(args, context), undefined);
1607
+ tlcEvalValues.set(expression, value);
1608
+ return value;
1609
+ };
1600
1610
  }
1601
1611
  if (global?.kind === "Builtin" && global.name === "\\subseteq" && args.length === 2) {
1602
1612
  const left = getEvaluator(args[0], environment);
@@ -1760,8 +1770,14 @@ export const getFunctionBuilder = (
1760
1770
  return markLambdaFunction(functionValue);
1761
1771
  }
1762
1772
  const pairs: [Value, Value][] = [];
1763
- const sets = mapBoundSets(bounds, (set) =>
1764
- getNormalizedElements(evaluate(set, context), set, context.environment.runtime.version),
1773
+ const sets = mapBoundSets(bounds, (set, index) =>
1774
+ getNormalizedElements(
1775
+ bounds.length > 1 && typeof domain === "object" && domain.kind === "ProductSet"
1776
+ ? domain.factors[index]
1777
+ : domain,
1778
+ set,
1779
+ context.environment.runtime.version,
1780
+ ),
1765
1781
  );
1766
1782
  forEachBoundElement(bounds, sets, context.scope, (scope) => {
1767
1783
  const key =
package/src/exit-codes.ts CHANGED
@@ -2,6 +2,7 @@ import type { LivenessChecker } from "./liveness-checker";
2
2
 
3
3
  export const EXIT_STATUS = {
4
4
  success: 0,
5
+ abort: 1,
5
6
  assumptionViolation: 10,
6
7
  deadlock: 11,
7
8
  safetyViolation: 12,
package/src/java-hash.ts CHANGED
@@ -5,3 +5,17 @@ export const getJavaHashCode = (text: string): number => {
5
5
  }
6
6
  return hash;
7
7
  };
8
+
9
+ const getHashMapHash = (text: string) => {
10
+ const hash = getJavaHashCode(text);
11
+ return hash ^ (hash >>> 16);
12
+ };
13
+
14
+ export const getHashMapOrder = (names: string[]) => {
15
+ const unique = [...new Set(names)];
16
+ const capacity = 2 ** Math.max(4, Math.ceil(Math.log2(unique.length / 0.75)));
17
+ return unique
18
+ .map((name, index) => ({ name, index, bucket: getHashMapHash(name) & (capacity - 1) }))
19
+ .sort((left, right) => left.bucket - right.bucket || left.index - right.index)
20
+ .map((entry) => entry.name);
21
+ };
@@ -59,6 +59,7 @@ export interface LiveGraph<State> {
59
59
  index: number,
60
60
  state: State,
61
61
  successors: LiveSuccessor<State>[],
62
+ isRerun?: boolean,
62
63
  ) => number[] | undefined;
63
64
  storeState: (pending: number, index: number) => void;
64
65
  getStateCounts: () => number[];
@@ -67,7 +68,11 @@ export interface LiveGraph<State> {
67
68
 
68
69
  export interface SearchGraph<State> {
69
70
  getFingerprint: (successor: LiveSuccessor<State>) => number;
70
- getSuccessors: (index: number, state: State) => LiveSuccessor<State>[];
71
+ forEachActionSuccessors: (
72
+ index: number,
73
+ state: State,
74
+ onSuccessors: (successors: LiveSuccessor<State>[]) => void,
75
+ ) => void;
71
76
  }
72
77
 
73
78
  export interface LiveEvaluation<State> {
@@ -370,7 +375,7 @@ export const createLivenessChecker = (
370
375
  addInitialState: (index, state, replay) =>
371
376
  traceWith(replay, () => graph.addInitialState(index, state)),
372
377
  addState: (index, state, successors, replay) =>
373
- traceWith(replay, () => graph.addState(index, state, successors)),
378
+ traceWith(replay, () => graph.addState(index, state, successors, replay !== undefined)),
374
379
  storeState: graph.storeState,
375
380
  getStateCounts: graph.getStateCounts,
376
381
  findViolation: graph.findViolation,
@@ -754,39 +759,57 @@ export const createLiveGraph = <State>(
754
759
  actions.push(0);
755
760
  if (existing === undefined) expandLate(behavior, tableau, state, index, target, row, offset);
756
761
  }
757
- const nextStates = search.getSuccessors(index, state);
758
762
  // The results of the step to the state itself come first, then those of each successor.
759
- const actionResults = new Uint8Array((nextStates.length + 1) * actionCount);
760
- actionResults.set(selfResults);
761
- const edges = createEdgeRows(nextStates.length);
762
- nextStates.forEach((successor, position) => {
763
- const successorRow = createRows(1);
764
- checkAction(
765
- behavior,
766
- index,
767
- state,
768
- row,
769
- offset,
770
- successor.state,
771
- edges,
772
- position * actionIds.size,
773
- actionResults,
774
- (position + 1) * actionCount,
775
- );
776
- const isDone = behavior.isDone[successor.index] === true;
777
- for (const next of tableauNode.successors) {
778
- if (!isConsistent(behavior, next, index, successor.state, successorRow, 0)) continue;
779
- const existing = getNodeId(behavior, successor.index, next);
780
- if (existing !== undefined && successors.includes(existing)) continue;
781
- const target = existing ?? recordNode(behavior, successor.index, next);
782
- successors.push(target);
783
- actions.push(position + 1);
784
- if (existing === undefined && isDone) {
785
- expandLate(behavior, tableau, successor.state, successor.index, target, successorRow, 0);
763
+ const actionBatches = [selfResults];
764
+ let stepCount = 1;
765
+ search.forEachActionSuccessors(index, state, (nextStates) => {
766
+ const edges = createEdgeRows(nextStates.length);
767
+ const results = new Uint8Array(nextStates.length * actionCount);
768
+ actionBatches.push(results);
769
+ for (let position = 0; position < nextStates.length; position++) {
770
+ const successor = nextStates[position];
771
+ const successorRow = createRows(1);
772
+ checkAction(
773
+ behavior,
774
+ index,
775
+ state,
776
+ row,
777
+ offset,
778
+ successor.state,
779
+ edges,
780
+ position * actionIds.size,
781
+ results,
782
+ position * actionCount,
783
+ );
784
+ const isDone = behavior.isDone[successor.index] === true;
785
+ for (const next of tableauNode.successors) {
786
+ if (!isConsistent(behavior, next, index, successor.state, successorRow, 0)) continue;
787
+ const existing = getNodeId(behavior, successor.index, next);
788
+ if (existing !== undefined && successors.includes(existing)) continue;
789
+ const target = existing ?? recordNode(behavior, successor.index, next);
790
+ successors.push(target);
791
+ actions.push(stepCount + position);
792
+ if (existing === undefined && isDone) {
793
+ expandLate(
794
+ behavior,
795
+ tableau,
796
+ successor.state,
797
+ successor.index,
798
+ target,
799
+ successorRow,
800
+ 0,
801
+ );
802
+ }
786
803
  }
787
804
  }
805
+ stepCount += nextStates.length;
788
806
  });
789
807
  if (successors.length > 0) {
808
+ const actionResults = new Uint8Array(stepCount * actionCount);
809
+ for (let batch = 0, resultOffset = 0; batch < actionBatches.length; batch++) {
810
+ actionResults.set(actionBatches[batch], resultOffset);
811
+ resultOffset += actionBatches[batch].length;
812
+ }
790
813
  writeNode(behavior, node, successors, actionResults, actions, stateResults);
791
814
  }
792
815
  };
@@ -1141,7 +1164,7 @@ export const createLiveGraph = <State>(
1141
1164
  }
1142
1165
  }
1143
1166
  },
1144
- addState: (index, state, successors) => {
1167
+ addState: (index, state, successors, isRerun) => {
1145
1168
  const set = getSuccessorSet(index, state, successors);
1146
1169
  const rows = createRows(set.length);
1147
1170
  // The set holds the state itself, as a successor of the same index.
@@ -1166,6 +1189,15 @@ export const createLiveGraph = <State>(
1166
1189
  );
1167
1190
  const stateResults = checkState(behavior, index, state, rows, offset);
1168
1191
  const tableau = behavior.solution.tableau;
1192
+ // TLC's rerun for a call stack fails an assertion, without a call stack, on a branch that has every edge of the state.
1193
+ if (
1194
+ isRerun &&
1195
+ (tableau
1196
+ ? getStateNodes(behavior, index).some((node) => behavior.edgeCounts[node] > 0)
1197
+ : set.every((successor) => isWritten(behavior, successor.index)))
1198
+ ) {
1199
+ throw new TlaEvaluationError("The behavior graph holds every edge of the state already.");
1200
+ }
1169
1201
  if (!tableau) {
1170
1202
  addPlainState(behavior, set, actionResults, stateResults, index);
1171
1203
  continue;
package/src/liveness.ts CHANGED
@@ -16,6 +16,7 @@ import {
16
16
  forEachBoundElement,
17
17
  getBoundElements,
18
18
  getDefinition,
19
+ getReevaluatedDefinitions,
19
20
  getStartupValue,
20
21
  lookupScope,
21
22
  resolveApplyOperator,
@@ -323,12 +324,17 @@ export const expandTemporal = (
323
324
  return [{ expression, scope, name: expansion.name, path: expansion.path }];
324
325
  };
325
326
 
326
- const bindLetDefinitions = (
327
+ // TLC takes a formula apart through a LET without binding its definitions, so it evaluates an impure one again at each reference.
328
+ export const bindLetDefinitions = (
327
329
  expression: Expression & { kind: "Let" },
328
330
  scope: Scope | undefined,
329
331
  environment: Environment,
330
332
  ) => {
331
- const bound = bindDefinitions(expression.definitions, getConstantContext(environment, scope));
333
+ const bound = bindDefinitions(
334
+ expression.definitions,
335
+ getConstantContext(environment, scope),
336
+ getReevaluatedDefinitions(expression.definitions, environment),
337
+ );
332
338
  for (let current = bound; current && current !== scope; current = current.parent) {
333
339
  if (current.binding.kind === "Thunk") definitionThunks.add(current.binding);
334
340
  }
@@ -488,6 +494,45 @@ export const toLiveExpression = (
488
494
  return level > ACTION_LEVEL ? parts : toAtom(expression, scope, level);
489
495
  };
490
496
 
497
+ // SANY parses `a /\ b /\ c` as `(a /\ b) /\ c`, so TLC takes a prefix below the temporal level as one formula.
498
+ const convertJunction = (
499
+ expression: Expression & { kind: "And" | "Or" },
500
+ scope: Scope | undefined,
501
+ ) => {
502
+ const operands = expression.operands;
503
+ const range = expression.location.range;
504
+ const prefixEnds = expression.prefixEnds;
505
+ if (expression.isBulleted || !range || !prefixEnds) {
506
+ return toWhole(
507
+ expression,
508
+ scope,
509
+ createJunction(
510
+ expression.kind,
511
+ operands.map((operand) => convert(operand, scope)),
512
+ ),
513
+ );
514
+ }
515
+ return operands.slice(1).reduce(
516
+ (prefix, operand, index) => {
517
+ const node =
518
+ index === operands.length - 2
519
+ ? expression
520
+ : {
521
+ ...expression,
522
+ operands: operands.slice(0, index + 2),
523
+ prefixEnds: prefixEnds.slice(0, index + 1),
524
+ location: { ...expression.location, range: { ...range, end: prefixEnds[index] } },
525
+ };
526
+ return toWhole(
527
+ node,
528
+ scope,
529
+ createJunction(expression.kind, [prefix, convert(operand, scope)]),
530
+ );
531
+ },
532
+ convert(operands[0], scope),
533
+ );
534
+ };
535
+
491
536
  // These forms are translated part by part at every level; any other form below the temporal level is an atom.
492
537
  const convert = (expression: Expression, scope: Scope | undefined): LiveExpression => {
493
538
  const level = getLevel(expression, scope, environment);
@@ -512,14 +557,7 @@ export const toLiveExpression = (
512
557
  }
513
558
  case "And":
514
559
  case "Or":
515
- return toWhole(
516
- expression,
517
- scope,
518
- createJunction(
519
- expression.kind,
520
- expression.operands.map((operand) => convert(operand, scope)),
521
- ),
522
- );
560
+ return convertJunction(expression, scope);
523
561
  case "Forall":
524
562
  case "Exists":
525
563
  return convertQuantifier(expression, scope);
package/src/parser.ts CHANGED
@@ -251,6 +251,7 @@ const OPERATOR_ARGUMENTS = new Set([
251
251
 
252
252
  const SELECTOR_SYMBOLS = new Set(["<<", ">>", ":", "@", "("]);
253
253
 
254
+ // Deviation from SANY: TLAplusParser#OpOrExpr's lookahead omits PROPOSITION, LEMMA and COROLLARY, so SANY parses an operator argument before them as an expression.
254
255
  const UNIT_KEYWORDS = new Set([
255
256
  "ASSUME",
256
257
  "ASSUMPTION",
@@ -1956,7 +1957,11 @@ const parse = (
1956
1957
 
1957
1958
  const parseDeclarationUnit = (): Declaration => {
1958
1959
  const location = getLocation(peek());
1959
- if (isAt("_") && peek(1).kind === "Operator") {
1960
+ const isOperatorDeclaration =
1961
+ isAt("_") &&
1962
+ peek(1).kind === "Operator" &&
1963
+ (INFIX_OPERATORS.has(peek(1).text) || DEFINABLE_POSTFIX_OPERATORS.has(peek(1).text));
1964
+ if (isOperatorDeclaration) {
1960
1965
  advance();
1961
1966
  const name = advance().text;
1962
1967
  return { name, arity: accept("_") ? 2 : 1, location };
@@ -41,6 +41,7 @@ import { Anum } from "./nlsat/algebraic";
41
41
  export const KERNEL_ARITH_OPTIONS: ArithRewriterOptions = {
42
42
  isSom: true,
43
43
  isSortSums: true,
44
+ isArithLhs: false,
44
45
  isArithIneqLhs: true,
45
46
  isGcdRounding: true,
46
47
  isAnumSimp: true,
@@ -52,6 +53,7 @@ export const KERNEL_ARITH_OPTIONS: ArithRewriterOptions = {
52
53
  export const TACTIC_ARITH_OPTIONS: ArithRewriterOptions = {
53
54
  isSom: false,
54
55
  isSortSums: false,
56
+ isArithLhs: false,
55
57
  isArithIneqLhs: false,
56
58
  isGcdRounding: false,
57
59
  isAnumSimp: true,
@@ -118,7 +120,8 @@ export class ArithRewriter extends PolyRewriter {
118
120
  ]),
119
121
  2,
120
122
  );
121
- if (!this.isArithTerm(left) && !this.isArithTerm(right)) return undefined;
123
+ if (!this.options.isArithLhs && !this.isArithTerm(left) && !this.isArithTerm(right))
124
+ return undefined;
122
125
  return this.mkLeGeEq(left, right, KIND_EQ);
123
126
  }
124
127
 
@@ -1259,7 +1262,12 @@ export class ArithRewriter extends PolyRewriter {
1259
1262
  sides[index] = expr;
1260
1263
  };
1261
1264
  const cancelled = manager.withTemporaries(
1262
- () => this.cancelMonomials(originalLeft, originalRight, this.options.isArithIneqLhs),
1265
+ () =>
1266
+ this.cancelMonomials(
1267
+ originalLeft,
1268
+ originalRight,
1269
+ this.options.isArithIneqLhs || this.options.isArithLhs,
1270
+ ),
1263
1271
  (result) => (result === undefined ? [] : [result.lhs, result.rhs]),
1264
1272
  );
1265
1273
  if (cancelled !== undefined) sides.push(cancelled.lhs, cancelled.rhs);
@@ -1366,7 +1374,7 @@ export class ArithRewriter extends PolyRewriter {
1366
1374
  return this.reduce(manager.mkAnd([mkUpper(), lower]), 3);
1367
1375
  }
1368
1376
  const negated =
1369
- this.options.isArithIneqLhs && this.manager.isNumeral(right)
1377
+ (this.options.isArithLhs || this.options.isArithIneqLhs) && this.manager.isNumeral(right)
1370
1378
  ? this.getNegatedPolynomial(left)
1371
1379
  : undefined;
1372
1380
  if (negated !== undefined) {
@@ -24,6 +24,7 @@ export class ArrayRewriter {
24
24
  constructor(
25
25
  private manager: AstManager,
26
26
  private isSortingStore = false,
27
+ private isExpandingSelects = false,
27
28
  ) {}
28
29
 
29
30
  private isOp(expr: number, op: number) {
@@ -193,7 +194,12 @@ export class ArrayRewriter {
193
194
  (index, position) =>
194
195
  manager.isValue(index) && manager.isValue(manager.getArg(array, position + 1)),
195
196
  );
196
- if (!this.isOp(base, OP_CONST_ARRAY) && !areValues) return undefined;
197
+ if (
198
+ !this.isOp(base, OP_CONST_ARRAY) &&
199
+ !areValues &&
200
+ !(this.isExpandingSelects && manager.getRefCount(base) === 1)
201
+ )
202
+ return undefined;
197
203
  const selectBase = this.mkApp(OP_SELECT, [base, ...indices]);
198
204
  const value = manager.getArg(array, indices.length + 1);
199
205
  const eqs = indices.map((index, position) =>
@@ -208,7 +214,11 @@ export class ArrayRewriter {
208
214
  if (this.isOp(array, OP_ITE)) {
209
215
  const thenArray = manager.getArg(array, 1);
210
216
  const elseArray = manager.getArg(array, 2);
211
- if (manager.getRefCount(thenArray) !== 1 && manager.getRefCount(elseArray) !== 1)
217
+ if (
218
+ !this.isExpandingSelects &&
219
+ manager.getRefCount(thenArray) !== 1 &&
220
+ manager.getRefCount(elseArray) !== 1
221
+ )
212
222
  return undefined;
213
223
  const thenSelect = this.mkApp(OP_SELECT, [thenArray, ...indices]);
214
224
  const elseSelect = this.mkApp(OP_SELECT, [elseArray, ...indices]);