tla-ts 0.0.4-dev.8fefa42 → 0.0.4-dev.a11a30a

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 (60) hide show
  1. package/README.md +27 -130
  2. package/dist/deciders.cjs +1 -1
  3. package/dist/deciders.js +1 -1
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +1 -3
  6. package/dist/index.d.ts +1 -3
  7. package/dist/index.js +1 -1
  8. package/dist/prove-worker.cjs +1 -1
  9. package/dist/prove-worker.js +1 -1
  10. package/dist/prove.cjs +506 -25
  11. package/dist/prove.js +506 -25
  12. package/dist/run-cli.cjs +35 -28
  13. package/dist/run-cli.js +35 -28
  14. package/dist/tlapm.cjs +2 -2
  15. package/dist/tlapm.js +2 -2
  16. package/package.json +1 -1
  17. package/src/obligations/action.ts +56 -99
  18. package/src/obligations/anon.ts +37 -41
  19. package/src/obligations/deref.ts +49 -90
  20. package/src/obligations/expression.ts +63 -49
  21. package/src/obligations/format.ts +7 -8
  22. package/src/obligations/generate.ts +60 -155
  23. package/src/obligations/index.ts +6 -4
  24. package/src/obligations/levels.ts +20 -32
  25. package/src/obligations/module.ts +93 -127
  26. package/src/obligations/pickle.ts +1 -3
  27. package/src/obligations/pragma.ts +14 -14
  28. package/src/obligations/printer.ts +17 -21
  29. package/src/obligations/proof.ts +0 -1
  30. package/src/obligations/source.ts +72 -73
  31. package/src/obligations/temporal.ts +29 -27
  32. package/src/obligations/tuples.ts +37 -53
  33. package/src/ocaml-marshal.ts +166 -0
  34. package/src/prove/decide-pool.ts +12 -17
  35. package/src/prove/deciders.ts +7 -1
  36. package/src/prove/first-order.ts +78 -0
  37. package/src/prove/isabelle/encode.ts +96 -127
  38. package/src/prove/ocaml.ts +1 -1
  39. package/src/prove/prep/action.ts +39 -51
  40. package/src/prove/prep/defs.ts +19 -13
  41. package/src/prove/prep/elab.ts +1 -1
  42. package/src/prove/prep/level-comparison.ts +15 -38
  43. package/src/prove/prep/pragma.ts +4 -4
  44. package/src/prove/prove-worker.ts +4 -11
  45. package/src/prove/prove.ts +230 -149
  46. package/src/prove/ptl/encode.ts +5 -6
  47. package/src/prove/smt/axioms.ts +78 -79
  48. package/src/prove/smt/data.ts +16 -45
  49. package/src/prove/smt/encode.ts +18 -23
  50. package/src/prove/smt/extensionality.ts +14 -12
  51. package/src/prove/smt/flatten.ts +3 -18
  52. package/src/prove/smt/format.ts +14 -3
  53. package/src/prove/smt/instances.ts +4 -5
  54. package/src/prove/smt/rewrite.ts +36 -48
  55. package/src/prove/smt/simplify.ts +10 -10
  56. package/src/prove/tlapm/tlapm.ts +13 -13
  57. package/src/prove/verdict.ts +5 -0
  58. package/src/prove/zenon/encode.ts +36 -101
  59. package/src/run-cli.ts +264 -115
  60. package/src/tlapm-cache.ts +236 -0
@@ -8,24 +8,26 @@ import {
8
8
  consSubstitution,
9
9
  exprifySequent,
10
10
  getErrorNote,
11
+ getFact,
12
+ isBuiltinApply,
11
13
  isEqualExpr,
12
14
  mapAccumulate,
15
+ mapChildren,
13
16
  normalizeApply,
14
17
  shift,
15
18
  shiftExpr,
16
19
  substituteBounds,
17
20
  substituteExpr,
21
+ stripProps,
18
22
  type Bound,
19
23
  type Builtin,
20
24
  type Expr,
21
25
  type Hyp,
22
26
  type Locus,
23
- type ObligationWarning,
27
+ type Warn,
24
28
  type Sequent,
25
29
  type Time,
26
30
  type Visibility,
27
- mapChildren,
28
- stripProps,
29
31
  } from "./expression";
30
32
  import { format, renderDoc, type Doc } from "./format";
31
33
  import { printExpression } from "./printer";
@@ -50,7 +52,6 @@ const AT_NOWHERE: Expr = { kind: "At", isExcept: false };
50
52
  const getProps = (from: Step) => ({
51
53
  location: from.location,
52
54
  step: from.step,
53
- isSuppressed: from.isSuppressed,
54
55
  method: from.method,
55
56
  });
56
57
 
@@ -64,10 +65,7 @@ const isBinary = (
64
65
  expression: Expr,
65
66
  builtin: Builtin,
66
67
  ): expression is Extract<Expr, { kind: "Apply" }> =>
67
- expression.kind === "Apply" &&
68
- expression.operator.kind === "Internal" &&
69
- expression.operator.builtin === builtin &&
70
- expression.args.length === 2;
68
+ isBuiltinApply(expression, builtin) && expression.args.length === 2;
71
69
 
72
70
  const getMessage = (pattern: string, ...args: (string | Doc)[]) =>
73
71
  renderDoc(format(pattern, ...args));
@@ -130,22 +128,10 @@ const proveAssertion = (context: Hyp[], sequent: Sequent, step: Step, time: Time
130
128
  context: [
131
129
  ...context,
132
130
  ...hyps,
133
- {
134
- kind: "Fact",
135
- expression: getTrue(step),
136
- visibility: "Hidden",
137
- time,
138
- location: step.location,
139
- },
131
+ getFact(getTrue(step), "Hidden", time, step.location),
140
132
  getStepDefinition(step, body, scopeTime),
141
- {
142
- kind: "Fact",
143
- expression: at({ kind: "Ix", index: 1 }, step),
144
- visibility: "Hidden",
145
- time: scopeTime,
146
- location: step.location,
147
- },
148
- ] satisfies Hyp[],
133
+ getFact(at({ kind: "Ix", index: 1 }, step), "Hidden", scopeTime, step.location),
134
+ ],
149
135
  active: shiftExpr(3, sequent.active),
150
136
  time: scopeTime,
151
137
  };
@@ -164,14 +150,8 @@ const useAssertion = (
164
150
  context: [
165
151
  ...context,
166
152
  getStepDefinition(step, at(exprifySequent(sequent), step), time),
167
- {
168
- kind: "Fact",
169
- expression: at({ kind: "Ix", index: 1 }, step),
170
- visibility,
171
- time,
172
- location: step.location,
173
- },
174
- ] satisfies Hyp[],
153
+ getFact(at({ kind: "Ix", index: 1 }, step), visibility, time, step.location),
154
+ ],
175
155
  active: shiftExpr(2, goal),
176
156
  };
177
157
  };
@@ -187,7 +167,7 @@ const getFactTime = (context: Hyp[], fact: Expr, time: Time): Time => {
187
167
  : "Always";
188
168
  };
189
169
 
190
- export const setDefinition = (visibility: Visibility, context: Hyp[], def: UseDef): Hyp[] => {
170
+ const setDefinition = (visibility: Visibility, context: Hyp[], def: UseDef): Hyp[] => {
191
171
  if (def.kind !== "Dx") throw new TlaProofError(`Unknown definition ${def.name}`);
192
172
  const index = context.length - def.index;
193
173
  const hyp = context[index];
@@ -197,15 +177,13 @@ export const setDefinition = (visibility: Visibility, context: Hyp[], def: UseDe
197
177
  };
198
178
 
199
179
  const isDomainMatch = (fact: Expr, domain: Expr) =>
200
- fact.kind === "Apply" &&
201
- fact.operator.kind === "Internal" &&
202
- fact.operator.builtin === "Mem" &&
180
+ isBuiltinApply(fact, "Mem") &&
203
181
  fact.args[0]?.kind === "Ix" &&
204
182
  fact.args[0].index === 0 &&
205
183
  isEqualExpr(domain, fact.args[1]);
206
184
 
207
- export const setExpression = (
208
- onWarning: (warning: ObligationWarning) => void,
185
+ const setExpression = (
186
+ onWarning: Warn,
209
187
  visibility: Visibility,
210
188
  fact: Expr,
211
189
  context: Hyp[],
@@ -219,7 +197,9 @@ export const setExpression = (
219
197
  if (hyp.kind === "Fact" && isEqualExpr(hyp.expression, current)) {
220
198
  isFound = true;
221
199
  result[index] = { ...hyp, visibility };
222
- } else if (hyp.kind === "Fresh" && hyp.domain && isDomainMatch(current, hyp.domain)) {
200
+ continue;
201
+ }
202
+ if (hyp.kind === "Fresh" && hyp.domain && isDomainMatch(current, hyp.domain)) {
223
203
  isFound = true;
224
204
  result[index] = { ...hyp, domainVisibility: visibility };
225
205
  }
@@ -238,18 +218,6 @@ export interface LineRange {
238
218
  end: number;
239
219
  }
240
220
 
241
- export interface Generator {
242
- generate: (sequent: Sequent, proof: Proof, time: Time) => Proof;
243
- collect: (proof: Proof) => Obligation[];
244
- mutate: (
245
- context: Hyp[],
246
- mode: "Use" | "Hide",
247
- usable: Usable,
248
- location: Locus | undefined,
249
- ) => [Hyp[], Obligation[]];
250
- simplify: (context: Hyp[], goal: Expr, proof: Proof, time: Time) => Proof;
251
- }
252
-
253
221
  // The bounds after a witnessed one, where a ditto takes the witnessed domain.
254
222
  const getRemainingBounds = (first: Bound, rest: Bound[]): Bound[] => {
255
223
  const shifted = substituteBounds(shift(1), rest)[1];
@@ -266,7 +234,7 @@ const getRemainingBounds = (first: Bound, rest: Bound[]): Bound[] => {
266
234
 
267
235
  // A WITNESS proves each membership and subset it relies on, then the goal from them.
268
236
  const getWitnessProof = (step: Step, aux: Expr[]): Proof => {
269
- if (step.isSuppressed || aux.length === 0) return { kind: "Obvious", ...getProps(step) };
237
+ if (aux.length === 0) return { kind: "Obvious", ...getProps(step) };
270
238
  const steps = aux.map((item, index): Step => {
271
239
  const active = shiftExpr(2 * index + 2, item);
272
240
  return {
@@ -323,17 +291,8 @@ export const joinSummaries = (left: ProofSummary, right: ProofSummary): ProofSum
323
291
  });
324
292
 
325
293
  const getStepSummary = (step: Step): ProofSummary => {
326
- switch (step.kind) {
327
- case "Use":
328
- return { ...EMPTY_SUMMARY, total: step.obligations?.length ?? 0 };
329
- case "Assert":
330
- case "Suffices":
331
- case "Pcase":
332
- case "Pick":
333
- return getProofSummary(step.proof);
334
- default:
335
- return EMPTY_SUMMARY;
336
- }
294
+ if (step.kind === "Use") return { ...EMPTY_SUMMARY, total: step.obligations?.length ?? 0 };
295
+ return "proof" in step ? getProofSummary(step.proof) : EMPTY_SUMMARY;
337
296
  };
338
297
 
339
298
  export const getProofSummary = (proof: Proof): ProofSummary => {
@@ -360,8 +319,8 @@ export const createGenerator = (
360
319
  counter: Counter,
361
320
  inputFiles: Set<string>,
362
321
  lines: LineRange,
363
- onWarning: (warning: ObligationWarning) => void,
364
- ): Generator => {
322
+ onWarning: Warn,
323
+ ) => {
365
324
  const generate = (sequent: Sequent, proof: Proof, time: Time): Proof => {
366
325
  const location = proof.location;
367
326
  if (
@@ -418,11 +377,12 @@ export const createGenerator = (
418
377
  context: [
419
378
  ...sequent.context,
420
379
  {
421
- kind: "Fact",
422
- expression: fact,
423
- visibility: "Visible",
424
- time: getFactTime(sequent.context, fact, time),
425
- location: fact.location,
380
+ ...getFact(
381
+ fact,
382
+ "Visible",
383
+ getFactTime(sequent.context, fact, time),
384
+ fact.location,
385
+ ),
426
386
  useLocation: step.useLocation,
427
387
  },
428
388
  ],
@@ -542,19 +502,11 @@ export const createGenerator = (
542
502
  item.steps.forEach(visitStep);
543
503
  return;
544
504
  }
545
- if (item.obligations && !item.isSuppressed) obligations.push(...item.obligations);
505
+ if (item.obligations) obligations.push(...item.obligations);
546
506
  };
547
507
  const visitStep = (step: Step) => {
548
- if (step.kind === "Use" && step.obligations && !step.isSuppressed)
549
- obligations.push(...step.obligations);
550
- if (
551
- step.kind === "Assert" ||
552
- step.kind === "Suffices" ||
553
- step.kind === "Pcase" ||
554
- step.kind === "Pick"
555
- ) {
556
- visitProof(step.proof);
557
- }
508
+ if (step.kind === "Use" && step.obligations) obligations.push(...step.obligations);
509
+ if ("proof" in step) visitProof(step.proof);
558
510
  };
559
511
  visitProof(proof);
560
512
  return obligations;
@@ -583,15 +535,9 @@ export const createGenerator = (
583
535
 
584
536
  const renumberStep = (level: number, step: Step): Step => {
585
537
  const renumbered: Step = { ...step, step: renumberNumber(level, step.step) };
586
- switch (renumbered.kind) {
587
- case "Assert":
588
- case "Suffices":
589
- case "Pcase":
590
- case "Pick":
591
- return { ...renumbered, proof: renumber(level + 1, renumbered.proof) };
592
- default:
593
- return renumbered;
594
- }
538
+ return "proof" in renumbered
539
+ ? { ...renumbered, proof: renumber(level + 1, renumbered.proof) }
540
+ : renumbered;
595
541
  };
596
542
 
597
543
  const exposeConnective = (context: Hyp[], goal: Expr): Expr => {
@@ -605,11 +551,7 @@ export const createGenerator = (
605
551
  const operator: Expr = { ...body, location: goal.location };
606
552
  return exposeConnective(context, at(normalizeApply(operator, goal.args), operator));
607
553
  }
608
- if (
609
- goal.kind === "Apply" &&
610
- goal.operator.kind === "Internal" &&
611
- goal.operator.builtin === "Prime"
612
- ) {
554
+ if (isBuiltinApply(goal, "Prime")) {
613
555
  const inner = exposeConnective(context, goal.args[0]);
614
556
  const prime = (expression: Expr) => applyBuiltin("Prime", [expression], expression);
615
557
  if (inner.kind === "Quant") {
@@ -694,7 +636,6 @@ export const createGenerator = (
694
636
  usable: { facts: [shiftExpr(index, fact)], defs: [] },
695
637
  isOnly: false,
696
638
  location: fact.location,
697
- isSuppressed: proof.isSuppressed,
698
639
  useLocation: proof.location,
699
640
  }));
700
641
  const forget: Step[] = proof.isOnly
@@ -709,7 +650,6 @@ export const createGenerator = (
709
650
  kind: "Obvious",
710
651
  location: proof.location,
711
652
  step: getAnonymous(level),
712
- isSuppressed: proof.isSuppressed,
713
653
  method: proof.method,
714
654
  };
715
655
  return {
@@ -718,27 +658,24 @@ export const createGenerator = (
718
658
  qed: { proof: qedProof, location: qedProof.location, step: qedProof.step },
719
659
  location: proof.location,
720
660
  step: proof.step,
721
- isSuppressed: proof.isSuppressed,
722
661
  };
723
662
  }
724
663
  case "Steps": {
725
- let currentContext = context;
726
- let currentGoal = goal;
727
- let currentTime = time;
728
- const steps = proof.steps.flatMap((step) => {
729
- const [nextContext, nextGoal, simplified, nextTime] = simplifyStep(
730
- currentContext,
731
- currentGoal,
732
- step,
733
- currentTime,
734
- );
735
- currentContext = nextContext;
736
- currentGoal = nextGoal;
737
- currentTime = nextTime;
738
- return simplified;
739
- });
740
- const qedProof = simplify(currentContext, currentGoal, proof.qed.proof, currentTime);
741
- return { ...proof, steps, qed: { ...proof.qed, proof: qedProof } };
664
+ const [inner, steps] = mapAccumulate(
665
+ { context, goal, time },
666
+ proof.steps,
667
+ (state, step) => {
668
+ const [nextContext, nextGoal, simplified, nextTime] = simplifyStep(
669
+ state.context,
670
+ state.goal,
671
+ step,
672
+ state.time,
673
+ );
674
+ return [{ context: nextContext, goal: nextGoal, time: nextTime }, simplified];
675
+ },
676
+ );
677
+ const qedProof = simplify(inner.context, inner.goal, proof.qed.proof, inner.time);
678
+ return { ...proof, steps: steps.flat(), qed: { ...proof.qed, proof: qedProof } };
742
679
  }
743
680
  }
744
681
  };
@@ -778,19 +715,14 @@ export const createGenerator = (
778
715
  isOnly: step.isOnly,
779
716
  location: step.location,
780
717
  step: step.step,
781
- isSuppressed: step.isSuppressed,
782
718
  method: step.method,
783
719
  };
784
720
  const simplified = simplify(context, goal, byProof, time);
785
721
  if (simplified.kind !== "Steps")
786
722
  throw new TlaProofError("simplifying BY does not produce a non-leaf proof!");
787
- const assumed = step.usable.facts.map((): Hyp => ({
788
- kind: "Fact",
789
- expression: AT_NOWHERE,
790
- visibility: "Visible",
791
- time,
792
- location: step.location,
793
- }));
723
+ const assumed = step.usable.facts.map(() =>
724
+ getFact(AT_NOWHERE, "Visible", time, step.location),
725
+ );
794
726
  return [
795
727
  [...context, ...assumed],
796
728
  shiftExpr(step.usable.facts.length, goal),
@@ -836,16 +768,9 @@ export const createGenerator = (
836
768
  ];
837
769
  case "Have": {
838
770
  const connective = exposeConnective(context, goal);
839
- const fact: Hyp = {
840
- kind: "Fact",
841
- expression: step.expression,
842
- visibility: "Visible",
843
- time,
844
- location: step.location,
845
- };
846
771
  const isImplication = isBinary(connective, "Implies");
847
772
  const sequent: Sequent = {
848
- context: [fact],
773
+ context: [getFact(step.expression, "Visible", time, step.location)],
849
774
  active: isImplication ? shiftExpr(1, connective.args[1]) : connective,
850
775
  };
851
776
  const proof: Proof = isImplication
@@ -892,15 +817,7 @@ export const createGenerator = (
892
817
  const assertion: Step = {
893
818
  kind: "Assert",
894
819
  sequent: {
895
- context: [
896
- {
897
- kind: "Fact",
898
- expression: step.expression,
899
- visibility: "Visible",
900
- time,
901
- location: step.location,
902
- },
903
- ],
820
+ context: [getFact(step.expression, "Visible", time, step.location)],
904
821
  active: shiftExpr(1, goal),
905
822
  },
906
823
  proof: step.proof,
@@ -930,13 +847,7 @@ export const createGenerator = (
930
847
  if (shifted.kind !== "Quant") throw new TlaProofError("Proof.Simplify.simplify_step/PICK");
931
848
  const hyps = [
932
849
  ...getBoundHyps(shifted.bounds),
933
- {
934
- kind: "Fact",
935
- expression: shifted.body,
936
- visibility: "Visible",
937
- time: assertTime,
938
- location: shifted.body.location,
939
- } satisfies Hyp,
850
+ getFact(shifted.body, "Visible", assertTime, shifted.body.location),
940
851
  ];
941
852
  const [nextContext, nextGoal, sufficed, nextTime] = simplifyStep(
942
853
  assertContext,
@@ -1041,15 +952,7 @@ export const createGenerator = (
1041
952
  return {
1042
953
  kind: "Suffices",
1043
954
  sequent: {
1044
- context: [
1045
- {
1046
- kind: "Fact",
1047
- expression: getTrue(goal),
1048
- visibility: "Visible",
1049
- time: "Now",
1050
- location: step.location,
1051
- },
1052
- ],
955
+ context: [getFact(getTrue(goal), "Visible", "Now", step.location)],
1053
956
  active: goal,
1054
957
  },
1055
958
  proof: { kind: "Error", message: stripped, ...getProps(step) },
@@ -1145,3 +1048,5 @@ export const createGenerator = (
1145
1048
 
1146
1049
  return { generate, collect, mutate, simplify };
1147
1050
  };
1051
+
1052
+ export type Generator = ReturnType<typeof createGenerator>;
@@ -8,7 +8,7 @@ import {
8
8
  getLocusPosition,
9
9
  getLocusPrefix,
10
10
  type Locus,
11
- type ObligationWarning,
11
+ type Warn,
12
12
  type Sequent,
13
13
  } from "./expression";
14
14
  import { createGenerator, type LineRange } from "./generate";
@@ -33,7 +33,7 @@ import { tokenize } from "../lexer";
33
33
  import { parseTlapmModule } from "../parser";
34
34
  import type { Module } from "../ast";
35
35
 
36
- export interface ObligationSource {
36
+ interface ObligationSource {
37
37
  file: string;
38
38
  source: string;
39
39
  }
@@ -41,6 +41,7 @@ export interface ObligationSource {
41
41
  export interface PrintedObligation {
42
42
  id: number;
43
43
  location: string;
44
+ locus: Locus | undefined;
44
45
  text: string;
45
46
  kind: ObligationKind;
46
47
  sequent: Sequent;
@@ -58,7 +59,7 @@ export const WHOLE_MODULE: LineRange = { start: 0, end: Infinity };
58
59
 
59
60
  interface ObligationOptions {
60
61
  lines?: LineRange;
61
- onWarning?: (warning: ObligationWarning) => void;
62
+ onWarning?: Warn;
62
63
  }
63
64
 
64
65
  const MODULE_HEADER = /-{4,} *MODULE/;
@@ -127,7 +128,7 @@ const loadMule = (
127
128
  input: ObligationSource,
128
129
  counter: Counter,
129
130
  isMain: boolean,
130
- onWarning: (warning: ObligationWarning) => void,
131
+ onWarning: Warn,
131
132
  citation: string,
132
133
  ): Mule => {
133
134
  if (!MODULE_HEADER.test(input.source)) throw new TlaProofError("No tokens in file");
@@ -316,6 +317,7 @@ export const getObligations = (
316
317
  {
317
318
  id: index + 1,
318
319
  location: formatLocus(location),
320
+ locus: location,
319
321
  text,
320
322
  kind: obligation.kind,
321
323
  sequent: obligation.sequent,
@@ -1,13 +1,17 @@
1
- // A port of tlapm's Expr.Levels: the level of an expression (0 constant, 1 state, 2 action,
2
- // 3 temporal, 4 undefined), the weights of an operator's arguments, and the declared names the
3
- // level depends on.
1
+ // Expr.Levels: the level of an expression (0 constant, 1 state, 2 action, 3 temporal, 4 undefined),
2
+ // the weights of an operator's arguments, and the declared names the level depends on.
4
3
 
5
4
  import { TlaProofError, TlapmBugError } from "../errors";
6
5
  import {
7
6
  adjoin,
7
+ adjoinAll,
8
8
  getBoundedFresh,
9
+ getBoundFresh,
9
10
  getFresh,
11
+ getLocalDefn,
12
+ getParamFresh,
10
13
  getScopeAt,
14
+ isBuiltinApply,
11
15
  mapAccumulate,
12
16
  type Bound,
13
17
  type Builtin,
@@ -152,11 +156,7 @@ const getBoundsLevel = (scope: Scope | undefined, bounds: Bound[], body: Expr) =
152
156
  const domains = bounds.flatMap((bound) =>
153
157
  bound.domain.kind === "Domain" ? [getLevelInfo(scope, bound.domain.expression)] : [],
154
158
  );
155
- const inner = bounds.reduce(
156
- (current, bound) => adjoin(current, getFresh(bound.name, bound.level)),
157
- scope,
158
- );
159
- return combine([getLevelInfo(inner, body), ...domains]);
159
+ return combine([getLevelInfo(adjoinAll(scope, bounds.map(getBoundFresh)), body), ...domains]);
160
160
  };
161
161
 
162
162
  const applyOperator = (operator: LevelInfo, args: LevelInfo[]): LevelInfo => {
@@ -189,11 +189,10 @@ export const getLevelInfo = (scope: Scope | undefined, expression: Expr): LevelI
189
189
  case "Num":
190
190
  return getInfo(0);
191
191
  case "Lambda": {
192
- const inner = expression.params.reduce(
193
- (current, param) => adjoin(current, getFresh(param.name, "Unknown", param.shape)),
194
- scope,
192
+ const body = getLevelInfo(
193
+ adjoinAll(scope, expression.params.map(getParamFresh)),
194
+ expression.body,
195
195
  );
196
- const body = getLevelInfo(inner, expression.body);
197
196
  const names = new Set(expression.params.map((param) => param.name));
198
197
  return {
199
198
  level: body.level,
@@ -205,12 +204,7 @@ export const getLevelInfo = (scope: Scope | undefined, expression: Expr): LevelI
205
204
  const operator = expression.operator;
206
205
  // The action frontend commutes a prime onto an operator, and tlapm keeps the level that
207
206
  // the application had.
208
- if (
209
- operator.kind === "Apply" &&
210
- operator.operator.kind === "Internal" &&
211
- operator.operator.builtin === "Prime" &&
212
- operator.args.length === 1
213
- ) {
207
+ if (isBuiltinApply(operator, "Prime") && operator.args.length === 1) {
214
208
  return recurse({ ...expression, operator: operator.args[0] });
215
209
  }
216
210
  if (operator.kind === "Opaque") {
@@ -256,20 +250,11 @@ export const getLevelInfo = (scope: Scope | undefined, expression: Expr): LevelI
256
250
  case "Dot":
257
251
  case "Parens":
258
252
  return recurse(expression.expression);
259
- case "Let": {
260
- const inner = expression.definitions.reduce(
261
- (current, definition) =>
262
- adjoin(current, {
263
- kind: "Defn",
264
- definition,
265
- whereDefined: "User",
266
- visibility: "Visible",
267
- isLocal: true,
268
- }),
269
- scope,
253
+ case "Let":
254
+ return getLevelInfo(
255
+ adjoinAll(scope, expression.definitions.map(getLocalDefn)),
256
+ expression.body,
270
257
  );
271
- return getLevelInfo(inner, expression.body);
272
- }
273
258
  case "If":
274
259
  return combine(
275
260
  [expression.condition, expression.consequent, expression.alternative].map(recurse),
@@ -283,7 +268,10 @@ export const getLevelInfo = (scope: Scope | undefined, expression: Expr): LevelI
283
268
  return getBoundsLevel(scope, expression.bounds, expression.element);
284
269
  case "Tquant":
285
270
  getLevelInfo(
286
- expression.names.reduce((current, name) => adjoin(current, { kind: "Flex", name }), scope),
271
+ adjoinAll(
272
+ scope,
273
+ expression.names.map((name) => ({ kind: "Flex", name })),
274
+ ),
287
275
  expression.body,
288
276
  );
289
277
  return getInfo(3);