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
@@ -7,8 +7,10 @@ import {
7
7
  consAll,
8
8
  consSubstitution,
9
9
  getErrorNote,
10
+ getBoundFresh,
10
11
  getChildren,
11
12
  getFresh,
13
+ getParamFresh,
12
14
  mapChildren,
13
15
  mapSelectorArgs,
14
16
  shift,
@@ -17,7 +19,7 @@ import {
17
19
  type Defn,
18
20
  type Expr,
19
21
  type Hyp,
20
- type ObligationWarning,
22
+ type Warn,
21
23
  type Param,
22
24
  type Selector,
23
25
  } from "./expression";
@@ -70,7 +72,7 @@ const findLabel = (expression: Expr, label: string, context: Hyp[]): Label | und
70
72
  (found, bound) =>
71
73
  found ?? (bound.domain.kind === "Domain" ? search(bound.domain.expression) : undefined),
72
74
  undefined,
73
- ) ?? search(body, [...context, ...bounds.map((bound) => getFresh(bound.name, bound.level))]);
75
+ ) ?? search(body, [...context, ...bounds.map(getBoundFresh)]);
74
76
  switch (expression.kind) {
75
77
  case "Parens":
76
78
  if (expression.form.kind !== "Xlabel") return search(expression.expression);
@@ -78,10 +80,7 @@ const findLabel = (expression: Expr, label: string, context: Hyp[]): Label | und
78
80
  ? { context, expression: expression.expression, indices: expression.form.indices }
79
81
  : undefined;
80
82
  case "Lambda":
81
- return search(expression.body, [
82
- ...context,
83
- ...expression.params.map((param) => getFresh(param.name, "Unknown", param.shape)),
84
- ]);
83
+ return search(expression.body, [...context, ...expression.params.map(getParamFresh)]);
85
84
  case "Let": {
86
85
  const inner = [...context];
87
86
  for (const definition of expression.definitions) {
@@ -144,11 +143,7 @@ const getReversal = (count: number, paramCount: number) =>
144
143
  const getIndices = (count: number) =>
145
144
  Array.from({ length: count }, (_item, index): Expr => ({ kind: "Ix", index: count - index }));
146
145
 
147
- const instantiate = (
148
- onWarning: (warning: ObligationWarning) => void,
149
- expression: Expr,
150
- args: Expr[],
151
- ): Expr => {
146
+ const instantiate = (onWarning: Warn, expression: Expr, args: Expr[]): Expr => {
152
147
  switch (expression.kind) {
153
148
  case "Quant":
154
149
  case "Fcn":
@@ -214,11 +209,7 @@ const instantiate = (
214
209
  }
215
210
  };
216
211
 
217
- const abstract = (
218
- onWarning: (warning: ObligationWarning) => void,
219
- definitionCount: number,
220
- expression: Expr,
221
- ): [Param[], Expr] => {
212
+ const abstract = (onWarning: Warn, definitionCount: number, expression: Expr): [Param[], Expr] => {
222
213
  const params = ((): Param[] => {
223
214
  switch (expression.kind) {
224
215
  case "Quant":
@@ -244,8 +235,7 @@ const abstract = (
244
235
  };
245
236
 
246
237
  const getNumbered = (
247
- onWarning: (warning: ObligationWarning) => void,
248
- definitions: Defn[],
238
+ onWarning: Warn,
249
239
  expression: Expr,
250
240
  position: number,
251
241
  origin: Expr,
@@ -260,7 +250,10 @@ const getNumbered = (
260
250
  checkAtMost(items.length);
261
251
  return items[position - 1];
262
252
  };
263
- const result = ((): [Defn[], Expr] => {
253
+ if (expression.kind === "With") {
254
+ return getNumbered(onWarning, expression.expression, position, origin, message);
255
+ }
256
+ const result = ((): Expr => {
264
257
  switch (expression.kind) {
265
258
  case "Sequent": {
266
259
  const sequent = expression.sequent;
@@ -287,102 +280,78 @@ const getNumbered = (
287
280
  { failure: "Expr.Deref.deref_num" },
288
281
  );
289
282
  }
290
- return [definitions, shifted];
283
+ return shifted;
291
284
  }
292
285
  case "Apply":
293
- return [definitions, pick(expression.args)];
294
- case "With":
295
- return getNumbered(
296
- onWarning,
297
- definitions,
298
- expression.expression,
299
- position,
300
- origin,
301
- message,
302
- );
286
+ return pick(expression.args);
303
287
  case "If":
304
- return [
305
- definitions,
306
- pick([expression.condition, expression.consequent, expression.alternative]),
307
- ];
288
+ return pick([expression.condition, expression.consequent, expression.alternative]);
308
289
  case "List":
309
- return [definitions, pick(expression.items)];
290
+ return pick(expression.items);
310
291
  case "SetEnum":
311
292
  case "Tuple":
312
- return [definitions, pick(expression.elements)];
293
+ return pick(expression.elements);
313
294
  case "Product":
314
- return [definitions, pick(expression.factors)];
295
+ return pick(expression.factors);
315
296
  case "Let":
316
297
  checkAtMost(1);
317
- return [[...definitions, ...expression.definitions], expression.body];
298
+ return expression.body;
318
299
  case "Quant":
319
300
  case "SetOf":
320
301
  case "Fcn":
321
- return [
322
- definitions,
323
- pick(
324
- expression.bounds.flatMap((bound) =>
325
- bound.domain.kind === "Domain" ? [bound.domain.expression] : [],
326
- ),
302
+ return pick(
303
+ expression.bounds.flatMap((bound) =>
304
+ bound.domain.kind === "Domain" ? [bound.domain.expression] : [],
327
305
  ),
328
- ];
306
+ );
329
307
  case "Choose":
330
308
  case "SetSt":
331
309
  if (!expression.domain) break;
332
- return [definitions, pick([expression.domain])];
310
+ return pick([expression.domain]);
333
311
  case "Arrow":
334
- return [definitions, pick([expression.domain, expression.range])];
312
+ return pick([expression.domain, expression.range]);
335
313
  case "FcnApp": {
336
314
  const argument =
337
315
  expression.args.length === 1
338
316
  ? expression.args[0]
339
317
  : at({ kind: "Tuple", elements: expression.args }, expression);
340
- return [definitions, pick([expression.function, argument])];
318
+ return pick([expression.function, argument]);
341
319
  }
342
320
  case "Rect":
343
321
  case "Record":
344
- return [definitions, pick(expression.fields.map((field) => field.value))];
322
+ return pick(expression.fields.map((field) => field.value));
345
323
  case "Dot":
346
- return [
347
- definitions,
348
- pick([
349
- expression.expression,
350
- at({ kind: "String", value: expression.field }, expression),
351
- ]),
352
- ];
324
+ return pick([
325
+ expression.expression,
326
+ at({ kind: "String", value: expression.field }, expression),
327
+ ]);
353
328
  case "Except":
354
- return [
355
- definitions,
356
- pick([expression.function, ...expression.specs.map((spec) => spec.value)]),
357
- ];
329
+ return pick([expression.function, ...expression.specs.map((spec) => spec.value)]);
358
330
  case "Sub":
359
331
  case "Tsub":
360
- return [definitions, pick([expression.action, expression.subscript])];
332
+ return pick([expression.action, expression.subscript]);
361
333
  case "Fair":
362
- return [definitions, pick([expression.subscript, expression.action])];
334
+ return pick([expression.subscript, expression.action]);
363
335
  case "Case":
364
- return [
365
- definitions,
366
- pick([
367
- ...expression.arms.map((arm) =>
368
- at({ kind: "Tuple", elements: [arm.condition, arm.body] }, arm.body),
369
- ),
370
- ...(expression.other
371
- ? [at({ kind: "Tuple", elements: [BAD, expression.other] }, expression.other)]
372
- : []),
373
- ]),
374
- ];
336
+ return pick([
337
+ ...expression.arms.map((arm) =>
338
+ at({ kind: "Tuple", elements: [arm.condition, arm.body] }, arm.body),
339
+ ),
340
+ ...(expression.other
341
+ ? [at({ kind: "Tuple", elements: [BAD, expression.other] }, expression.other)]
342
+ : []),
343
+ ]);
375
344
  }
376
345
  const emptyMessage = "Expression has no subexpressions";
377
346
  onWarning(getErrorNote(expression.location, emptyMessage));
378
347
  throw new TlaProofError(emptyMessage, { failure: "Expr.Deref.deref_num" });
379
348
  })();
380
- if (isBad(result[1])) {
349
+ if (isBad(result)) {
381
350
  const badMessage = `Cannot resolve subexpression reference !${position}`;
382
351
  onWarning(getErrorNote(expression.location, badMessage));
383
352
  throw new TlaProofError(badMessage, { failure: "Expr.Deref.deref_num" });
384
353
  }
385
- return result;
354
+ return [expression.kind === "Let" ? expression.definitions : [], result];
386
355
  };
387
356
 
388
357
  // Reorders label arguments given in source order to the order of the label's de Bruijn indices.
@@ -397,7 +366,7 @@ const permute = (indices: number[], args: Expr[]) => {
397
366
  };
398
367
 
399
368
  const resolveLabel = (
400
- onWarning: (warning: ObligationWarning) => void,
369
+ onWarning: Warn,
401
370
  definitionCount: number,
402
371
  expression: Expr,
403
372
  label: string,
@@ -460,7 +429,7 @@ const resolveLabel = (
460
429
  };
461
430
 
462
431
  const resolveLetOperator = (
463
- onWarning: (warning: ObligationWarning) => void,
432
+ onWarning: Warn,
464
433
  definitionCount: number,
465
434
  expression: Expr,
466
435
  label: string,
@@ -516,12 +485,7 @@ const resolveLetOperator = (
516
485
  );
517
486
  };
518
487
 
519
- const deref = (
520
- onWarning: (warning: ObligationWarning) => void,
521
- isLabel: boolean,
522
- state: Deref,
523
- selectors: Selector[],
524
- ): Expr => {
488
+ const deref = (onWarning: Warn, isLabel: boolean, state: Deref, selectors: Selector[]): Expr => {
525
489
  const [selector, ...rest] = selectors;
526
490
  if (!selector) {
527
491
  const withDefinitions =
@@ -547,7 +511,6 @@ const deref = (
547
511
  const numbered = (position: number, message: string) => {
548
512
  const [definitions, inner] = getNumbered(
549
513
  onWarning,
550
- [],
551
514
  expression,
552
515
  position,
553
516
  state.expression,
@@ -608,11 +571,7 @@ const deref = (
608
571
  );
609
572
  };
610
573
 
611
- const getInitialDeref = (
612
- onWarning: (warning: ObligationWarning) => void,
613
- operator: Expr,
614
- args: Expr[],
615
- ): Deref => {
574
+ const getInitialDeref = (onWarning: Warn, operator: Expr, args: Expr[]): Deref => {
616
575
  if (args.length > 0) {
617
576
  return { params: [], definitions: [], expression: instantiate(onWarning, operator, args) };
618
577
  }
@@ -622,7 +581,7 @@ const getInitialDeref = (
622
581
  };
623
582
 
624
583
  export const resolveSubexpression = (
625
- onWarning: (warning: ObligationWarning) => void,
584
+ onWarning: Warn,
626
585
  operator: Expr,
627
586
  args: Expr[],
628
587
  selectors: Selector[],
@@ -17,6 +17,8 @@ export interface ObligationWarning {
17
17
  channel: "toolbox" | "toolboxOnly" | "stderr" | "stdout";
18
18
  }
19
19
 
20
+ export type Warn = (warning: ObligationWarning) => void;
21
+
20
22
  export const getLocusPosition = (locus: Locus) => {
21
23
  const end = locus.endColumn - 1;
22
24
  if (locus.startLine !== locus.endLine)
@@ -29,8 +31,10 @@ export const getLocusPosition = (locus: Locus) => {
29
31
  export const getLocusText = (locus: Locus) =>
30
32
  `File ${JSON.stringify(locus.file)}, ${getLocusPosition(locus)}`;
31
33
 
32
- export const getAtPrefix = (locus: Locus | undefined) =>
33
- `${locus ? getLocusText(locus) : "<unknown location>"}:\n`;
34
+ export const getLocationText = (locus: Locus | undefined) =>
35
+ locus ? getLocusText(locus) : "<unknown location>";
36
+
37
+ export const getAtPrefix = (locus: Locus | undefined) => `${getLocationText(locus)}:\n`;
34
38
 
35
39
  export const getErrorNote = (locus: Locus | undefined, message: string): ObligationWarning => ({
36
40
  text: `${getAtPrefix(locus)}Error: ${message}`,
@@ -298,6 +302,14 @@ export const getHypName = (hyp: Hyp) => {
298
302
  }
299
303
  };
300
304
 
305
+ export const isBuiltinApply = (
306
+ expression: Expr,
307
+ builtin: Builtin,
308
+ ): expression is Extract<Expr, { kind: "Apply" }> =>
309
+ expression.kind === "Apply" &&
310
+ expression.operator.kind === "Internal" &&
311
+ expression.operator.builtin === builtin;
312
+
301
313
  export const applyBuiltin = (builtin: Builtin, args: Expr[], from: ExprProps): Expr =>
302
314
  at({ kind: "Apply", operator: at({ kind: "Internal", builtin }, from), args }, from);
303
315
 
@@ -341,7 +353,6 @@ export const getScopeAt = (scope: Scope | undefined, depth: number) => {
341
353
  return current;
342
354
  };
343
355
 
344
- // Maps the items in order, threading a state from each item to the next.
345
356
  export const mapAccumulate = <State, Item, Result>(
346
357
  state: State,
347
358
  items: Item[],
@@ -412,6 +423,28 @@ export const getBoundedFresh = (name: string, domain: Expr): Hyp => ({
412
423
  domainVisibility: "Visible",
413
424
  });
414
425
 
426
+ export const getFact = (
427
+ expression: Expr,
428
+ visibility: Visibility,
429
+ time: Time,
430
+ location: Locus | undefined,
431
+ ) => ({ kind: "Fact", expression, visibility, time, location }) satisfies Hyp;
432
+
433
+ // What a quantifier binds each name as, a LAMBDA each parameter, and a LET each definition, as
434
+ // Visit.map does.
435
+ export const getBoundFresh = (bound: Bound) => getFresh(bound.name, bound.level);
436
+
437
+ export const getParamFresh = (param: Param) => getFresh(param.name, "Unknown", param.shape);
438
+
439
+ export const getLocalDefn = (definition: Defn) =>
440
+ ({
441
+ kind: "Defn",
442
+ definition,
443
+ whereDefined: "User",
444
+ visibility: "Visible",
445
+ isLocal: true,
446
+ }) satisfies Hyp;
447
+
415
448
  const applyIndex = (substitution: Substitution, index: number, from: Expr): Expr => {
416
449
  switch (substitution.kind) {
417
450
  case "Shift":
@@ -546,6 +579,8 @@ export const substituteExpr = (substitution: Substitution, expression: Expr): Ex
546
579
  case "Sequent":
547
580
  return { ...expression, sequent: substituteSequent(substitution, expression.sequent) };
548
581
  case "With":
582
+ case "Dot":
583
+ case "Parens":
549
584
  return { ...expression, expression: recurse(expression.expression) };
550
585
  case "If":
551
586
  return {
@@ -556,7 +591,8 @@ export const substituteExpr = (substitution: Substitution, expression: Expr): Ex
556
591
  };
557
592
  case "List":
558
593
  return { ...expression, items: substituteExprs(substitution, expression.items) };
559
- case "Quant": {
594
+ case "Quant":
595
+ case "Fcn": {
560
596
  const [inner, bounds] = substituteBounds(substitution, expression.bounds);
561
597
  return { ...expression, bounds, body: substituteExpr(inner, expression.body) };
562
598
  }
@@ -582,6 +618,7 @@ export const substituteExpr = (substitution: Substitution, expression: Expr): Ex
582
618
  return { ...expression, bounds, element: substituteExpr(inner, expression.element) };
583
619
  }
584
620
  case "SetEnum":
621
+ case "Tuple":
585
622
  return { ...expression, elements: substituteExprs(substitution, expression.elements) };
586
623
  case "Arrow":
587
624
  return {
@@ -589,10 +626,6 @@ export const substituteExpr = (substitution: Substitution, expression: Expr): Ex
589
626
  domain: recurse(expression.domain),
590
627
  range: recurse(expression.range),
591
628
  };
592
- case "Fcn": {
593
- const [inner, bounds] = substituteBounds(substitution, expression.bounds);
594
- return { ...expression, bounds, body: substituteExpr(inner, expression.body) };
595
- }
596
629
  case "FcnApp":
597
630
  return {
598
631
  ...expression,
@@ -601,8 +634,6 @@ export const substituteExpr = (substitution: Substitution, expression: Expr): Ex
601
634
  };
602
635
  case "Product":
603
636
  return { ...expression, factors: substituteExprs(substitution, expression.factors) };
604
- case "Tuple":
605
- return { ...expression, elements: substituteExprs(substitution, expression.elements) };
606
637
  case "Rect":
607
638
  case "Record":
608
639
  return {
@@ -618,20 +649,13 @@ export const substituteExpr = (substitution: Substitution, expression: Expr): Ex
618
649
  function: recurse(expression.function),
619
650
  specs: mapExceptSpecs(expression.specs, recurse),
620
651
  };
621
- case "Dot":
622
- return { ...expression, expression: recurse(expression.expression) };
623
652
  case "Sub":
624
653
  case "Tsub":
625
- return {
626
- ...expression,
627
- action: recurse(expression.action),
628
- subscript: recurse(expression.subscript),
629
- };
630
654
  case "Fair":
631
655
  return {
632
656
  ...expression,
633
- subscript: recurse(expression.subscript),
634
657
  action: recurse(expression.action),
658
+ subscript: recurse(expression.subscript),
635
659
  };
636
660
  case "Case":
637
661
  return {
@@ -642,8 +666,6 @@ export const substituteExpr = (substitution: Substitution, expression: Expr): Ex
642
666
  })),
643
667
  other: expression.other && recurse(expression.other),
644
668
  };
645
- case "Parens":
646
- return { ...expression, expression: recurse(expression.expression) };
647
669
  }
648
670
  };
649
671
 
@@ -656,6 +678,9 @@ const isEqualList = <Item>(
656
678
  isEqual: (left: Item, right: Item) => boolean,
657
679
  ) => left.length === right.length && left.every((item, index) => isEqual(item, right[index]));
658
680
 
681
+ const isEqualOptional = (left: Expr | undefined, right: Expr | undefined) =>
682
+ left && right ? isEqualExpr(left, right) : !left && !right;
683
+
659
684
  const isEqualFields = (left: Field[], right: Field[]) => {
660
685
  const leftMap = new Map(left.map((field) => [field.name, field.value]));
661
686
  const rightMap = new Map(right.map((field) => [field.name, field.value]));
@@ -693,9 +718,7 @@ const isEqualSelector = (left: Selector, right: Selector) => {
693
718
  };
694
719
 
695
720
  const isEqualDefn = (left: Defn, right: Defn): boolean => {
696
- if (left.kind === "Operator" && right.kind === "Operator")
697
- return isEqualExpr(left.body, right.body);
698
- if (left.kind === "Bpragma" && right.kind === "Bpragma")
721
+ if ((left.kind === "Operator" || left.kind === "Bpragma") && right.kind === left.kind)
699
722
  return isEqualExpr(left.body, right.body);
700
723
  if (left.kind === "Instance" && right.kind === "Instance") {
701
724
  const leftSubstitutions = left.instance.substitutions.map((entry): Field => ({
@@ -721,9 +744,11 @@ const isEqualDefn = (left: Defn, right: Defn): boolean => {
721
744
 
722
745
  export const isEqualHyp = (left: Hyp, right: Hyp): boolean => {
723
746
  if (left.kind === "Fresh" && right.kind === "Fresh") {
724
- if (left.level !== right.level || left.shape !== right.shape) return false;
725
- if (!left.domain || !right.domain) return !left.domain && !right.domain;
726
- return isEqualExpr(left.domain, right.domain);
747
+ return (
748
+ left.level === right.level &&
749
+ left.shape === right.shape &&
750
+ isEqualOptional(left.domain, right.domain)
751
+ );
727
752
  }
728
753
  if (left.kind === "Flex" && right.kind === "Flex") return true;
729
754
  if (left.kind === "Defn" && right.kind === "Defn") {
@@ -805,11 +830,11 @@ export const isEqualExpr = (left: Expr, right: Expr): boolean => {
805
830
  isEqualExpr(left.body, right.body)
806
831
  );
807
832
  case "Choose":
808
- if (right.kind !== "Choose") return false;
809
- if (left.domain && right.domain) {
810
- return isEqualExpr(left.domain, right.domain) && isEqualExpr(left.body, right.body);
811
- }
812
- return !left.domain && !right.domain && isEqualExpr(left.body, right.body);
833
+ return (
834
+ right.kind === "Choose" &&
835
+ isEqualOptional(left.domain, right.domain) &&
836
+ isEqualExpr(left.body, right.body)
837
+ );
813
838
  case "SetSt":
814
839
  return (
815
840
  right.kind === "SetSt" &&
@@ -823,7 +848,8 @@ export const isEqualExpr = (left: Expr, right: Expr): boolean => {
823
848
  isEqualList(left.bounds, right.bounds, isEqualBound)
824
849
  );
825
850
  case "SetEnum":
826
- return right.kind === "SetEnum" && isEqualList(left.elements, right.elements, isEqualExpr);
851
+ case "Tuple":
852
+ return right.kind === left.kind && isEqualList(left.elements, right.elements, isEqualExpr);
827
853
  case "Arrow":
828
854
  return (
829
855
  right.kind === "Arrow" &&
@@ -841,14 +867,11 @@ export const isEqualExpr = (left: Expr, right: Expr): boolean => {
841
867
  right.kind === "FcnApp" &&
842
868
  isEqualList([left.function, ...left.args], [right.function, ...right.args], isEqualExpr)
843
869
  );
844
- case "Tuple":
845
- return right.kind === "Tuple" && isEqualList(left.elements, right.elements, isEqualExpr);
846
870
  case "Product":
847
871
  return right.kind === "Product" && isEqualList(left.factors, right.factors, isEqualExpr);
848
872
  case "Rect":
849
- return right.kind === "Rect" && isEqualFields(left.fields, right.fields);
850
873
  case "Record":
851
- return right.kind === "Record" && isEqualFields(left.fields, right.fields);
874
+ return right.kind === left.kind && isEqualFields(left.fields, right.fields);
852
875
  case "Except":
853
876
  return (
854
877
  right.kind === "Except" &&
@@ -893,9 +916,7 @@ export const isEqualExpr = (left: Expr, right: Expr): boolean => {
893
916
  ? left.arms.length === right.arms.length
894
917
  : isEqualExpr(left.arms[0].condition, right.arms[0].condition) &&
895
918
  isEqualExpr(left.arms[0].body, right.arms[0].body);
896
- if (!isArmsEqual) return false;
897
- if (left.other && right.other) return isEqualExpr(left.other, right.other);
898
- return !left.other && !right.other;
919
+ return isArmsEqual && isEqualOptional(left.other, right.other);
899
920
  }
900
921
  case "String":
901
922
  return right.kind === "String" && left.value === right.value;
@@ -953,7 +974,7 @@ export const mapScoped = <State>(
953
974
  ): Expr => {
954
975
  const recurse = (inner: Expr) => map(state, inner);
955
976
  const bindBounds = (bounds: Bound[]) =>
956
- bounds.reduce((current, bound) => bind(current, getFresh(bound.name, bound.level)), state);
977
+ bounds.reduce((current, bound) => bind(current, getBoundFresh(bound)), state);
957
978
  switch (expression.kind) {
958
979
  case "Ix":
959
980
  case "Internal":
@@ -964,7 +985,7 @@ export const mapScoped = <State>(
964
985
  return expression;
965
986
  case "Lambda": {
966
987
  const inner = expression.params.reduce((current, param) => {
967
- const fresh = getFresh(param.name, "Unknown", param.shape);
988
+ const fresh = getParamFresh(param);
968
989
  return bind(current, param.location ? { ...fresh, location: param.location } : fresh);
969
990
  }, state);
970
991
  return { ...expression, body: map(inner, expression.body) };
@@ -982,14 +1003,7 @@ export const mapScoped = <State>(
982
1003
  expression.definitions,
983
1004
  (current, definition) => {
984
1005
  const mapped = mapDefnBody(definition, (body) => map(current, body));
985
- const hyp: Hyp = {
986
- kind: "Defn",
987
- definition: mapped,
988
- whereDefined: "User",
989
- visibility: "Visible",
990
- isLocal: true,
991
- };
992
- return [bind(current, hyp), mapped];
1006
+ return [bind(current, getLocalDefn(mapped)), mapped];
993
1007
  },
994
1008
  );
995
1009
  return { ...expression, definitions, body: map(inner, expression.body) };
@@ -60,7 +60,7 @@ const getSentinel = (): ScanElement => ({
60
60
  element: { size: UNKNOWN, token: { kind: "Text", text: "" }, length: 0 },
61
61
  });
62
62
 
63
- const createFormatter = (): Formatter => {
63
+ const createFormatter = (maxIndent: number): Formatter => {
64
64
  const systemBox: QueueElement = {
65
65
  size: UNKNOWN,
66
66
  token: { kind: "Begin", indent: 0, box: "hov" },
@@ -72,7 +72,7 @@ const createFormatter = (): Formatter => {
72
72
  scanStack: [getSentinel(), { leftTotal: 1, element: systemBox }],
73
73
  formatStack: [],
74
74
  margin: 78,
75
- maxIndent: 68,
75
+ maxIndent,
76
76
  spaceLeft: 78,
77
77
  currentIndent: 0,
78
78
  isNewLine: true,
@@ -242,7 +242,7 @@ const flush = (formatter: Formatter) => {
242
242
  const flushNewline = (formatter: Formatter) => {
243
243
  flush(formatter);
244
244
  const output = [...formatter.output, "\n"];
245
- Object.assign(formatter, createFormatter());
245
+ Object.assign(formatter, createFormatter(formatter.maxIndent));
246
246
  formatter.output = output;
247
247
  };
248
248
 
@@ -258,8 +258,6 @@ type Command =
258
258
 
259
259
  export type Doc = (Command | Doc)[];
260
260
 
261
- type FormatArgument = string | Doc;
262
-
263
261
  const BOX_KINDS: Record<string, BoxKind> = { h: "h", v: "v", hv: "hv", hov: "hov", b: "b" };
264
262
 
265
263
  const getText = (value: string): Command => ({ kind: "Text", text: value, size: getLength(value) });
@@ -282,7 +280,7 @@ const parseOpen = (directive: string): Command => {
282
280
 
283
281
  // Interprets the subset of OCaml format strings that tlapm's printers use, so ported printers read
284
282
  // like the OCaml they come from. Each %s or %a consumes one argument.
285
- export const format = (pattern: string, ...args: FormatArgument[]): Doc => {
283
+ export const format = (pattern: string, ...args: (string | Doc)[]): Doc => {
286
284
  const doc: Doc = [];
287
285
  let argumentIndex = 0;
288
286
  let pending = "";
@@ -399,8 +397,9 @@ const renderInto = (formatter: Formatter, doc: Doc) => {
399
397
  }
400
398
  };
401
399
 
402
- export const renderDoc = (doc: Doc) => {
403
- const formatter = createFormatter();
400
+ // 68 is Format's default maximum indent; tlapm's stderr formatter allows 35.
401
+ export const renderDoc = (doc: Doc, maxIndent = 68) => {
402
+ const formatter = createFormatter(maxIndent);
404
403
  renderInto(formatter, doc);
405
404
  return flush(formatter);
406
405
  };