tla-ts 0.0.4-dev.c4929d7 → 0.0.4-dev.ca40dfe

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 (207) hide show
  1. package/README.md +31 -6
  2. package/dist/cli.cjs +1 -1
  3. package/dist/cli.js +1 -1
  4. package/dist/deciders.cjs +27 -27
  5. package/dist/deciders.js +34 -34
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +12 -14
  8. package/dist/index.d.ts +12 -14
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +37 -27
  15. package/dist/prove.js +39 -29
  16. package/dist/run.cjs +45 -0
  17. package/dist/run.js +45 -0
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +2 -2
  20. package/dist/transfer.cjs +76 -86
  21. package/dist/transfer.js +73 -83
  22. package/dist/worker.cjs +6 -6
  23. package/dist/worker.js +6 -6
  24. package/package.json +1 -1
  25. package/src/ast.ts +2 -2
  26. package/src/bags.ts +22 -23
  27. package/src/builtins.ts +16 -8
  28. package/src/check.ts +131 -1154
  29. package/src/{cli-options.ts → cli/options.ts} +54 -9
  30. package/src/cli/run.ts +63 -0
  31. package/src/cli/shared.ts +106 -0
  32. package/src/cli/tlapm.ts +374 -0
  33. package/src/cli/tlc.ts +941 -0
  34. package/src/cli/xml-exporter.ts +221 -0
  35. package/src/cli.ts +1 -1
  36. package/src/codegen.ts +17 -15
  37. package/src/config.ts +5 -5
  38. package/src/dependencies.ts +4 -6
  39. package/src/environment.ts +55 -60
  40. package/src/errors.ts +12 -36
  41. package/src/evaluate.ts +81 -101
  42. package/src/exit-codes.ts +1 -1
  43. package/src/fingerprints.ts +3 -3
  44. package/src/instances.ts +56 -46
  45. package/src/java-overrides.ts +79 -41
  46. package/src/json.ts +31 -36
  47. package/src/levels.ts +10 -13
  48. package/src/{live-check.ts → liveness-checker.ts} +169 -142
  49. package/src/liveness.ts +38 -34
  50. package/src/obligations/action.ts +9 -45
  51. package/src/obligations/{anon.ts → anonymize.ts} +13 -25
  52. package/src/obligations/equality.ts +260 -0
  53. package/src/obligations/expression.ts +42 -517
  54. package/src/obligations/generate.ts +88 -778
  55. package/src/obligations/index.ts +20 -22
  56. package/src/obligations/levels.ts +36 -41
  57. package/src/obligations/module.ts +88 -708
  58. package/src/obligations/normalize.ts +554 -0
  59. package/src/obligations/operators.ts +176 -0
  60. package/src/obligations/pragma.ts +21 -15
  61. package/src/obligations/printer.ts +81 -188
  62. package/src/obligations/proof.ts +0 -1
  63. package/src/obligations/regroup.ts +100 -0
  64. package/src/obligations/simplify.ts +668 -0
  65. package/src/obligations/source.ts +25 -131
  66. package/src/obligations/{deref.ts → subexpression.ts} +48 -56
  67. package/src/obligations/substitution.ts +261 -0
  68. package/src/obligations/temporal.ts +28 -26
  69. package/src/obligations/tuples.ts +37 -53
  70. package/src/parallel-worker.ts +37 -33
  71. package/src/parallel.ts +30 -24
  72. package/src/parser.ts +67 -74
  73. package/src/{pcal-ast.ts → pcal/ast.ts} +2 -17
  74. package/src/{pcal-parse.ts → pcal/parse.ts} +28 -64
  75. package/src/{pcal-tokenize.ts → pcal/tokenize.ts} +10 -39
  76. package/src/{pcal-translate.ts → pcal/translate.ts} +30 -109
  77. package/src/{pcal-validate.ts → pcal/validate.ts} +10 -20
  78. package/src/prove/decide-pool.ts +7 -12
  79. package/src/{obligations/pickle.ts → prove/first-order.ts} +79 -2
  80. package/src/prove/isabelle/decide.ts +3 -6
  81. package/src/prove/isabelle/encode.ts +35 -93
  82. package/src/prove/isabelle/isar/inner-syntax.ts +1 -1
  83. package/src/prove/isabelle/isar/method.ts +3 -3
  84. package/src/prove/isabelle/isar/type-check.ts +1 -1
  85. package/src/prove/isabelle/provers/blast/blast.ts +1 -1
  86. package/src/prove/isabelle/provers/blast/branch.ts +1 -1
  87. package/src/prove/isabelle/provers/blast/term.ts +3 -4
  88. package/src/prove/isabelle/provers/clasimp.ts +1 -1
  89. package/src/prove/isabelle/provers/classical.ts +3 -3
  90. package/src/prove/isabelle/provers/hypsubst.ts +1 -9
  91. package/src/prove/isabelle/pure/conv.ts +1 -1
  92. package/src/prove/isabelle/pure/drule.ts +2 -2
  93. package/src/prove/isabelle/pure/envir.ts +3 -35
  94. package/src/prove/isabelle/pure/logic.ts +4 -76
  95. package/src/prove/isabelle/pure/name.ts +1 -13
  96. package/src/prove/isabelle/pure/net.ts +2 -49
  97. package/src/prove/isabelle/pure/pattern.ts +3 -22
  98. package/src/prove/isabelle/pure/raw-simplifier.ts +3 -18
  99. package/src/prove/isabelle/pure/seq.ts +0 -23
  100. package/src/prove/isabelle/pure/simplifier.ts +2 -8
  101. package/src/prove/isabelle/pure/table.ts +1 -8
  102. package/src/prove/isabelle/pure/tactic.ts +5 -113
  103. package/src/prove/isabelle/pure/tactical.ts +1 -84
  104. package/src/prove/isabelle/pure/term-ord.ts +1 -55
  105. package/src/prove/isabelle/pure/term.ts +8 -128
  106. package/src/prove/isabelle/pure/thm.ts +4 -27
  107. package/src/prove/isabelle/pure/type.ts +0 -12
  108. package/src/prove/isabelle/pure/unify.ts +1 -1
  109. package/src/prove/isabelle/tla/classical-setup.ts +1 -1
  110. package/src/prove/ocaml.ts +1 -1
  111. package/src/prove/prep/action.ts +23 -36
  112. package/src/prove/prep/enabled-axioms.ts +2 -2
  113. package/src/prove/prep/fingerprint.ts +3 -3
  114. package/src/prove/prep/level-comparison.ts +9 -37
  115. package/src/prove/prep/md5.ts +2 -9
  116. package/src/prove/prep/normalize.ts +309 -0
  117. package/src/prove/prove-worker.ts +2 -5
  118. package/src/prove/prove.ts +98 -111
  119. package/src/prove/ptl/encode.ts +19 -31
  120. package/src/prove/smt/axioms.ts +45 -58
  121. package/src/prove/smt/encode.ts +124 -15
  122. package/src/prove/smt/instances.ts +2 -3
  123. package/src/prove/smt/rewrite.ts +24 -31
  124. package/src/prove/smt/solver/ast-util.ts +0 -56
  125. package/src/prove/smt/solver/case-split-queue.ts +1 -1
  126. package/src/prove/smt/solver/enode.ts +1 -1
  127. package/src/prove/smt/solver/lp/dep-intervals.ts +0 -14
  128. package/src/prove/smt/solver/model-macro-solver.ts +1 -1
  129. package/src/prove/smt/solver/nlsat/algebraic.ts +3 -3
  130. package/src/prove/smt/solver/nlsat/atoms.ts +1 -1
  131. package/src/prove/smt/solver/nlsat/explain.ts +1 -1
  132. package/src/prove/smt/solver/nlsat/interval-set.ts +1 -1
  133. package/src/prove/smt/solver/nlsat/levelwise.ts +4 -7
  134. package/src/prove/smt/solver/nlsat/polynomial.ts +4 -4
  135. package/src/prove/smt/solver/nlsat/upolynomial.ts +2 -2
  136. package/src/prove/smt/solver/pattern-inference.ts +1 -1
  137. package/src/prove/smt/solver/relevancy.ts +2 -2
  138. package/src/prove/smt/solver/rewriter.ts +1 -1
  139. package/src/prove/smt/solver/solver.ts +2 -2
  140. package/src/{tlapm-cache.ts → prove/tlapm/cache.ts} +11 -11
  141. package/src/{ocaml-marshal.ts → prove/tlapm/ocaml-marshal.ts} +1 -1
  142. package/src/prove/verdict.ts +0 -5
  143. package/src/prove/zenon/decide.ts +5 -11
  144. package/src/prove/zenon/encode.ts +21 -80
  145. package/src/prove/zenon/eqrel.ts +1 -1
  146. package/src/prove/zenon/equality-rules.ts +1 -1
  147. package/src/prove/zenon/expr.ts +2 -2
  148. package/src/prove/zenon/index-table.ts +1 -1
  149. package/src/prove/zenon/isar.ts +1 -1
  150. package/src/prove/zenon/llproof.ts +1 -1
  151. package/src/prove/zenon/mltoll.ts +1 -1
  152. package/src/prove/zenon/node.ts +1 -1
  153. package/src/prove/zenon/search.ts +1 -1
  154. package/src/prove/zenon/tla/terms.ts +1 -20
  155. package/src/prove/zenon/tla/tla.ts +1 -1
  156. package/src/randomization.ts +19 -24
  157. package/src/resolve.ts +15 -22
  158. package/src/runtime.ts +8 -8
  159. package/src/{sany-builtins.ts → sany/builtins.ts} +1 -1
  160. package/src/{sany-generator.ts → sany/generator.ts} +38 -55
  161. package/src/{sany-levels.ts → sany/levels.ts} +45 -48
  162. package/src/{sany-lexer.ts → sany/lexer.ts} +2 -3
  163. package/src/{sany-linter.ts → sany/linter.ts} +1 -1
  164. package/src/{sany-modules.ts → sany/modules.ts} +2 -2
  165. package/src/{sany-nodes.ts → sany/nodes.ts} +2 -2
  166. package/src/{sany-parse-error.ts → sany/parse-error.ts} +2 -2
  167. package/src/{sany-report.ts → sany/report.ts} +16 -17
  168. package/src/{sany-selectors.ts → sany/selectors.ts} +3 -5
  169. package/src/{sany-source.ts → sany/source.ts} +6 -7
  170. package/src/{sany-xml.ts → sany/xml.ts} +5 -6
  171. package/src/search.ts +969 -0
  172. package/src/spec-description.ts +26 -20
  173. package/src/{standard-levels.ts → standard-definitions.ts} +118 -2
  174. package/src/standard-modules.ts +16 -20
  175. package/src/state-graph.ts +9 -8
  176. package/src/substitution-levels.ts +2 -2
  177. package/src/successors.ts +16 -19
  178. package/src/symmetry.ts +1 -1
  179. package/src/tableau.ts +6 -7
  180. package/src/tlc/fingerprint.ts +214 -0
  181. package/src/{tlc-messages.ts → tlc/messages.ts} +119 -7
  182. package/src/{tlc-order.ts → tlc/order.ts} +7 -7
  183. package/src/{tlc-random.ts → tlc/random.ts} +3 -213
  184. package/src/{tlc-replay.ts → tlc/replay.ts} +40 -36
  185. package/src/{tlc-module.ts → tlc/tlc-operators.ts} +9 -9
  186. package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +30 -25
  187. package/src/transfer.ts +32 -32
  188. package/src/values.ts +72 -67
  189. package/src/worker.ts +1 -4
  190. package/dist/run-cli.cjs +0 -45
  191. package/dist/run-cli.js +0 -45
  192. package/src/canonical.ts +0 -40
  193. package/src/obligations/ctx.ts +0 -64
  194. package/src/prove/prep/defs.ts +0 -33
  195. package/src/prove/prep/elab.ts +0 -156
  196. package/src/prove/prep/enabled.ts +0 -43
  197. package/src/prove/prep/flatten.ts +0 -70
  198. package/src/prove/prep/trivial.ts +0 -22
  199. package/src/prove/smt/axiomatize.ts +0 -69
  200. package/src/prove/smt/simplify.ts +0 -57
  201. package/src/run-cli.ts +0 -1806
  202. package/src/standard-ranges.ts +0 -116
  203. package/src/value-sources.ts +0 -29
  204. /package/src/prove/prep/{pragma.ts → methods.ts} +0 -0
  205. /package/src/{tlc-modules.ts → tlc/jar-data.ts} +0 -0
  206. /package/src/{tlc-summary.ts → tlc/summary.ts} +0 -0
  207. /package/src/{tlc-version.ts → tlc/version.ts} +0 -0
@@ -1,7 +1,5 @@
1
- // tlapm's expression IR (E_t) with de Bruijn indices, and its substitutions (E_subst) and
2
- // alpha-equivalence (E_eq).
1
+ // tlapm's expression IR (E_t) with de Bruijn indices, and the visitors that map over it (Visit).
3
2
 
4
- import { TlaProofError } from "../errors";
5
3
  import type { OldMethod, PragmaSetting } from "./pragma";
6
4
 
7
5
  export interface Locus {
@@ -12,11 +10,13 @@ export interface Locus {
12
10
  endColumn: number;
13
11
  }
14
12
 
15
- export interface ObligationWarning {
13
+ interface ObligationWarning {
16
14
  text: string;
17
15
  channel: "toolbox" | "toolboxOnly" | "stderr" | "stdout";
18
16
  }
19
17
 
18
+ export type Warn = (warning: ObligationWarning) => void;
19
+
20
20
  export const getLocusPosition = (locus: Locus) => {
21
21
  const end = locus.endColumn - 1;
22
22
  if (locus.startLine !== locus.endLine)
@@ -29,8 +29,10 @@ export const getLocusPosition = (locus: Locus) => {
29
29
  export const getLocusText = (locus: Locus) =>
30
30
  `File ${JSON.stringify(locus.file)}, ${getLocusPosition(locus)}`;
31
31
 
32
- export const getAtPrefix = (locus: Locus | undefined) =>
33
- `${locus ? getLocusText(locus) : "<unknown location>"}:\n`;
32
+ export const getLocationText = (locus: Locus | undefined) =>
33
+ locus ? getLocusText(locus) : "<unknown location>";
34
+
35
+ export const getAtPrefix = (locus: Locus | undefined) => `${getLocationText(locus)}:\n`;
34
36
 
35
37
  export const getErrorNote = (locus: Locus | undefined, message: string): ObligationWarning => ({
36
38
  text: `${getAtPrefix(locus)}Error: ${message}`,
@@ -200,7 +202,7 @@ export type ExprCore =
200
202
  | { kind: "At"; isExcept: boolean }
201
203
  | { kind: "Parens"; expression: Expr; form: ParenForm };
202
204
 
203
- interface Field {
205
+ export interface Field {
204
206
  name: string;
205
207
  value: Expr;
206
208
  }
@@ -257,29 +259,6 @@ export interface Sequent {
257
259
  active: Expr;
258
260
  }
259
261
 
260
- export type Substitution =
261
- | { kind: "Shift"; amount: number }
262
- | { kind: "Cons"; expression: Expr; rest: Substitution }
263
- | { kind: "Compose"; outer: Substitution; inner: Substitution }
264
- | { kind: "Bump"; amount: number; rest: Substitution };
265
-
266
- export const shift = (amount: number): Substitution => ({ kind: "Shift", amount });
267
-
268
- export const consSubstitution = (expression: Expr, rest: Substitution): Substitution => ({
269
- kind: "Cons",
270
- expression,
271
- rest,
272
- });
273
-
274
- // Binds the expressions in order, so the last one becomes index 1.
275
- export const consAll = (expressions: Expr[], rest: Substitution) =>
276
- expressions.reduce((inner, expression) => consSubstitution(expression, inner), rest);
277
-
278
- export const bumpBy = (amount: number, substitution: Substitution): Substitution =>
279
- substitution.kind === "Bump"
280
- ? { kind: "Bump", amount: substitution.amount + amount, rest: substitution.rest }
281
- : { kind: "Bump", amount, rest: substitution };
282
-
283
262
  // tlapm's core @@ from, which takes the properties of from.
284
263
  export const at = (core: ExprCore, from: ExprProps): Expr => {
285
264
  const located = from.location ? { ...core, location: from.location } : core;
@@ -298,6 +277,14 @@ export const getHypName = (hyp: Hyp) => {
298
277
  }
299
278
  };
300
279
 
280
+ export const isBuiltinApply = (
281
+ expression: Expr,
282
+ builtin: Builtin,
283
+ ): expression is Extract<Expr, { kind: "Apply" }> =>
284
+ expression.kind === "Apply" &&
285
+ expression.operator.kind === "Internal" &&
286
+ expression.operator.builtin === builtin;
287
+
301
288
  export const applyBuiltin = (builtin: Builtin, args: Expr[], from: ExprProps): Expr =>
302
289
  at({ kind: "Apply", operator: at({ kind: "Internal", builtin }, from), args }, from);
303
290
 
@@ -411,40 +398,27 @@ export const getBoundedFresh = (name: string, domain: Expr): Hyp => ({
411
398
  domainVisibility: "Visible",
412
399
  });
413
400
 
414
- const applyIndex = (substitution: Substitution, index: number, from: Expr): Expr => {
415
- switch (substitution.kind) {
416
- case "Shift":
417
- return at({ kind: "Ix", index: substitution.amount + index }, from);
418
- case "Cons":
419
- return index === 1
420
- ? at(stripProps(substitution.expression), from)
421
- : applyIndex(substitution.rest, index - 1, from);
422
- case "Compose":
423
- return substituteExpr(substitution.outer, applyIndex(substitution.inner, index, from));
424
- case "Bump":
425
- return index <= substitution.amount
426
- ? at({ kind: "Ix", index }, from)
427
- : substituteExpr(
428
- shift(substitution.amount),
429
- applyIndex(substitution.rest, index - substitution.amount, from),
430
- );
431
- }
432
- };
433
-
434
- export const substituteExprs = (substitution: Substitution, expressions: Expr[]) =>
435
- expressions.map((expression) => substituteExpr(substitution, expression));
436
-
437
- // Beta-reduces a lambda applied to arguments, as E_subst.normalize does.
438
- export const normalizeApply = (operator: Expr, args: Expr[]): ExprCore => {
439
- if (operator.kind === "Lambda") {
440
- if (operator.params.length !== args.length) throw new TlaProofError("Arity mismatch");
441
- return stripProps(substituteExpr(consAll(args, shift(0)), operator.body));
442
- }
443
- if (operator.kind === "Apply") {
444
- return { kind: "Apply", operator: operator.operator, args: [...operator.args, ...args] };
445
- }
446
- return args.length === 0 ? stripProps(operator) : { kind: "Apply", operator, args };
447
- };
401
+ export const getFact = (
402
+ expression: Expr,
403
+ visibility: Visibility,
404
+ time: Time,
405
+ location: Locus | undefined,
406
+ ) => ({ kind: "Fact", expression, visibility, time, location }) satisfies Hyp;
407
+
408
+ // What a quantifier binds each name as, a LAMBDA each parameter, and a LET each definition, as
409
+ // Visit.map does.
410
+ export const getBoundFresh = (bound: Bound) => getFresh(bound.name, bound.level);
411
+
412
+ export const getParamFresh = (param: Param) => getFresh(param.name, "Unknown", param.shape);
413
+
414
+ export const getLocalDefn = (definition: Defn) =>
415
+ ({
416
+ kind: "Defn",
417
+ definition,
418
+ whereDefined: "User",
419
+ visibility: "Visible",
420
+ isLocal: true,
421
+ }) satisfies Hyp;
448
422
 
449
423
  // The node without its properties, as tlapm's e.core.
450
424
  export const stripProps = (expression: Expr): ExprCore => {
@@ -453,448 +427,6 @@ export const stripProps = (expression: Expr): ExprCore => {
453
427
  return core;
454
428
  };
455
429
 
456
- export const substituteBounds = (
457
- substitution: Substitution,
458
- bounds: Bound[],
459
- ): [Substitution, Bound[]] => [
460
- bumpBy(bounds.length, substitution),
461
- mapBoundDomains(bounds, (domain) => substituteExpr(substitution, domain)),
462
- ];
463
-
464
- export const substituteDefn = (substitution: Substitution, definition: Defn): Defn => {
465
- switch (definition.kind) {
466
- case "Recursive":
467
- case "Operator":
468
- case "Bpragma":
469
- return mapDefnBody(definition, (body) => substituteExpr(substitution, body));
470
- case "Instance": {
471
- const inner = bumpBy(definition.instance.args.length, substitution);
472
- return {
473
- ...definition,
474
- instance: {
475
- ...definition.instance,
476
- substitutions: definition.instance.substitutions.map((entry) => ({
477
- name: entry.name,
478
- expression: substituteExpr(inner, entry.expression),
479
- })),
480
- },
481
- };
482
- }
483
- }
484
- };
485
-
486
- export const substituteHyp = (substitution: Substitution, hyp: Hyp): Hyp => {
487
- switch (hyp.kind) {
488
- case "Fresh":
489
- return hyp.domain ? { ...hyp, domain: substituteExpr(substitution, hyp.domain) } : hyp;
490
- case "Flex":
491
- return hyp;
492
- case "Defn":
493
- return { ...hyp, definition: substituteDefn(substitution, hyp.definition) };
494
- case "Fact":
495
- return { ...hyp, expression: substituteExpr(substitution, hyp.expression) };
496
- }
497
- };
498
-
499
- export const substituteSequent = (substitution: Substitution, sequent: Sequent): Sequent => {
500
- const [inner, context] = mapAccumulate(substitution, sequent.context, (current, hyp) => [
501
- bumpBy(1, current),
502
- substituteHyp(current, hyp),
503
- ]);
504
- return { context, active: substituteExpr(inner, sequent.active) };
505
- };
506
-
507
- export const substituteExpr = (substitution: Substitution, expression: Expr): Expr => {
508
- const recurse = (inner: Expr) => substituteExpr(substitution, inner);
509
- switch (expression.kind) {
510
- case "Internal":
511
- case "Opaque":
512
- case "String":
513
- case "Num":
514
- case "At":
515
- return expression;
516
- case "Ix":
517
- return applyIndex(substitution, expression.index, expression);
518
- case "Apply":
519
- return at(
520
- normalizeApply(
521
- recurse(expression.operator),
522
- substituteExprs(substitution, expression.args),
523
- ),
524
- expression,
525
- );
526
- case "Bang":
527
- return {
528
- ...expression,
529
- expression: recurse(expression.expression),
530
- selectors: expression.selectors.map((selector) => mapSelectorArgs(selector, recurse)),
531
- };
532
- case "Lambda":
533
- return {
534
- ...expression,
535
- body: substituteExpr(bumpBy(expression.params.length, substitution), expression.body),
536
- };
537
- case "Let": {
538
- const [inner, definitions] = mapAccumulate(
539
- substitution,
540
- expression.definitions,
541
- (current, definition) => [bumpBy(1, current), substituteDefn(current, definition)],
542
- );
543
- return { ...expression, definitions, body: substituteExpr(inner, expression.body) };
544
- }
545
- case "Sequent":
546
- return { ...expression, sequent: substituteSequent(substitution, expression.sequent) };
547
- case "With":
548
- case "Dot":
549
- case "Parens":
550
- return { ...expression, expression: recurse(expression.expression) };
551
- case "If":
552
- return {
553
- ...expression,
554
- condition: recurse(expression.condition),
555
- consequent: recurse(expression.consequent),
556
- alternative: recurse(expression.alternative),
557
- };
558
- case "List":
559
- return { ...expression, items: substituteExprs(substitution, expression.items) };
560
- case "Quant":
561
- case "Fcn": {
562
- const [inner, bounds] = substituteBounds(substitution, expression.bounds);
563
- return { ...expression, bounds, body: substituteExpr(inner, expression.body) };
564
- }
565
- case "Tquant":
566
- return {
567
- ...expression,
568
- body: substituteExpr(bumpBy(expression.names.length, substitution), expression.body),
569
- };
570
- case "Choose":
571
- return {
572
- ...expression,
573
- domain: expression.domain && recurse(expression.domain),
574
- body: substituteExpr(bumpBy(1, substitution), expression.body),
575
- };
576
- case "SetSt":
577
- return {
578
- ...expression,
579
- domain: recurse(expression.domain),
580
- body: substituteExpr(bumpBy(1, substitution), expression.body),
581
- };
582
- case "SetOf": {
583
- const [inner, bounds] = substituteBounds(substitution, expression.bounds);
584
- return { ...expression, bounds, element: substituteExpr(inner, expression.element) };
585
- }
586
- case "SetEnum":
587
- case "Tuple":
588
- return { ...expression, elements: substituteExprs(substitution, expression.elements) };
589
- case "Arrow":
590
- return {
591
- ...expression,
592
- domain: recurse(expression.domain),
593
- range: recurse(expression.range),
594
- };
595
- case "FcnApp":
596
- return {
597
- ...expression,
598
- function: recurse(expression.function),
599
- args: substituteExprs(substitution, expression.args),
600
- };
601
- case "Product":
602
- return { ...expression, factors: substituteExprs(substitution, expression.factors) };
603
- case "Rect":
604
- case "Record":
605
- return {
606
- ...expression,
607
- fields: expression.fields.map((field) => ({
608
- name: field.name,
609
- value: recurse(field.value),
610
- })),
611
- };
612
- case "Except":
613
- return {
614
- ...expression,
615
- function: recurse(expression.function),
616
- specs: mapExceptSpecs(expression.specs, recurse),
617
- };
618
- case "Sub":
619
- case "Tsub":
620
- case "Fair":
621
- return {
622
- ...expression,
623
- action: recurse(expression.action),
624
- subscript: recurse(expression.subscript),
625
- };
626
- case "Case":
627
- return {
628
- ...expression,
629
- arms: expression.arms.map((arm) => ({
630
- condition: recurse(arm.condition),
631
- body: recurse(arm.body),
632
- })),
633
- other: expression.other && recurse(expression.other),
634
- };
635
- }
636
- };
637
-
638
- export const shiftExpr = (amount: number, expression: Expr) =>
639
- amount === 0 ? expression : substituteExpr(shift(amount), expression);
640
-
641
- const isEqualList = <Item>(
642
- left: Item[],
643
- right: Item[],
644
- isEqual: (left: Item, right: Item) => boolean,
645
- ) => left.length === right.length && left.every((item, index) => isEqual(item, right[index]));
646
-
647
- const isEqualOptional = (left: Expr | undefined, right: Expr | undefined) =>
648
- left && right ? isEqualExpr(left, right) : !left && !right;
649
-
650
- const isEqualFields = (left: Field[], right: Field[]) => {
651
- const leftMap = new Map(left.map((field) => [field.name, field.value]));
652
- const rightMap = new Map(right.map((field) => [field.name, field.value]));
653
- if (leftMap.size !== rightMap.size) return false;
654
- for (const [name, value] of leftMap) {
655
- const other = rightMap.get(name);
656
- if (!other || !isEqualExpr(value, other)) return false;
657
- }
658
- return true;
659
- };
660
-
661
- const isEqualBound = (left: Bound, right: Bound) =>
662
- left.level === right.level &&
663
- left.domain.kind === right.domain.kind &&
664
- (left.domain.kind !== "Domain" ||
665
- (right.domain.kind === "Domain" &&
666
- isEqualExpr(left.domain.expression, right.domain.expression)));
667
-
668
- const isEqualSelector = (left: Selector, right: Selector) => {
669
- if (left.kind !== right.kind) return false;
670
- switch (left.kind) {
671
- case "Number":
672
- return right.kind === "Number" && left.value === right.value;
673
- case "Instance":
674
- return right.kind === "Instance" && isEqualList(left.args, right.args, isEqualExpr);
675
- case "Label":
676
- return (
677
- right.kind === "Label" &&
678
- left.label === right.label &&
679
- isEqualList(left.args, right.args, isEqualExpr)
680
- );
681
- default:
682
- return true;
683
- }
684
- };
685
-
686
- const isEqualDefn = (left: Defn, right: Defn): boolean => {
687
- if ((left.kind === "Operator" || left.kind === "Bpragma") && right.kind === left.kind)
688
- return isEqualExpr(left.body, right.body);
689
- if (left.kind === "Instance" && right.kind === "Instance") {
690
- const leftSubstitutions = left.instance.substitutions.map((entry): Field => ({
691
- name: entry.name,
692
- value: entry.expression,
693
- }));
694
- const rightSubstitutions = right.instance.substitutions.map((entry): Field => ({
695
- name: entry.name,
696
- value: entry.expression,
697
- }));
698
- return (
699
- isEqualList(
700
- left.instance.args,
701
- right.instance.args,
702
- (leftArg, rightArg) => leftArg === rightArg,
703
- ) &&
704
- left.instance.module === right.instance.module &&
705
- isEqualFields(leftSubstitutions, rightSubstitutions)
706
- );
707
- }
708
- return false;
709
- };
710
-
711
- export const isEqualHyp = (left: Hyp, right: Hyp): boolean => {
712
- if (left.kind === "Fresh" && right.kind === "Fresh") {
713
- return (
714
- left.level === right.level &&
715
- left.shape === right.shape &&
716
- isEqualOptional(left.domain, right.domain)
717
- );
718
- }
719
- if (left.kind === "Flex" && right.kind === "Flex") return true;
720
- if (left.kind === "Defn" && right.kind === "Defn") {
721
- return isEqualDefn(left.definition, right.definition) && left.isLocal === right.isLocal;
722
- }
723
- if (left.kind === "Fact" && right.kind === "Fact") {
724
- return isEqualExpr(left.expression, right.expression);
725
- }
726
- return false;
727
- };
728
-
729
- const isEqualSequent = (left: Sequent, right: Sequent) =>
730
- isEqualList(left.context, right.context, isEqualHyp) && isEqualExpr(left.active, right.active);
731
-
732
- // Alpha-equivalence, including E_eq's quirk of comparing only the first CASE arm.
733
- export const isEqualExpr = (left: Expr, right: Expr): boolean => {
734
- if (left.kind === "Parens") return isEqualExpr(left.expression, right);
735
- if (right.kind === "Parens") return isEqualExpr(left, right.expression);
736
- switch (left.kind) {
737
- case "Ix":
738
- return right.kind === "Ix" && left.index === right.index;
739
- case "Opaque":
740
- return right.kind === "Opaque" && left.name === right.name;
741
- case "Internal":
742
- return right.kind === "Internal" && left.builtin === right.builtin;
743
- case "Lambda":
744
- return (
745
- right.kind === "Lambda" &&
746
- left.params.length === right.params.length &&
747
- isEqualExpr(left.body, right.body)
748
- );
749
- case "Apply":
750
- return (
751
- right.kind === "Apply" &&
752
- isEqualExpr(left.operator, right.operator) &&
753
- isEqualList(left.args, right.args, isEqualExpr)
754
- );
755
- case "Sequent":
756
- return right.kind === "Sequent" && isEqualSequent(left.sequent, right.sequent);
757
- case "Bang":
758
- return (
759
- right.kind === "Bang" &&
760
- isEqualExpr(left.expression, right.expression) &&
761
- isEqualList(left.selectors, right.selectors, isEqualSelector)
762
- );
763
- case "With":
764
- return right.kind === "With" && isEqualExpr(left.expression, right.expression);
765
- case "Let":
766
- return (
767
- right.kind === "Let" &&
768
- isEqualList(left.definitions, right.definitions, isEqualDefn) &&
769
- isEqualExpr(left.body, right.body)
770
- );
771
- case "If":
772
- return (
773
- right.kind === "If" &&
774
- isEqualExpr(left.condition, right.condition) &&
775
- isEqualExpr(left.consequent, right.consequent) &&
776
- isEqualExpr(left.alternative, right.alternative)
777
- );
778
- case "List":
779
- return (
780
- right.kind === "List" &&
781
- left.bullet === right.bullet &&
782
- isEqualList(left.items, right.items, isEqualExpr)
783
- );
784
- case "Quant":
785
- return (
786
- right.kind === "Quant" &&
787
- left.quantifier === right.quantifier &&
788
- isEqualList(left.bounds, right.bounds, isEqualBound) &&
789
- isEqualExpr(left.body, right.body)
790
- );
791
- case "Tquant":
792
- return (
793
- right.kind === "Tquant" &&
794
- left.quantifier === right.quantifier &&
795
- isEqualList(left.names, right.names, (leftName, rightName) => leftName === rightName) &&
796
- isEqualExpr(left.body, right.body)
797
- );
798
- case "Choose":
799
- return (
800
- right.kind === "Choose" &&
801
- isEqualOptional(left.domain, right.domain) &&
802
- isEqualExpr(left.body, right.body)
803
- );
804
- case "SetSt":
805
- return (
806
- right.kind === "SetSt" &&
807
- isEqualExpr(left.domain, right.domain) &&
808
- isEqualExpr(left.body, right.body)
809
- );
810
- case "SetOf":
811
- return (
812
- right.kind === "SetOf" &&
813
- isEqualExpr(left.element, right.element) &&
814
- isEqualList(left.bounds, right.bounds, isEqualBound)
815
- );
816
- case "SetEnum":
817
- case "Tuple":
818
- return right.kind === left.kind && isEqualList(left.elements, right.elements, isEqualExpr);
819
- case "Arrow":
820
- return (
821
- right.kind === "Arrow" &&
822
- isEqualExpr(left.domain, right.domain) &&
823
- isEqualExpr(left.range, right.range)
824
- );
825
- case "Fcn":
826
- return (
827
- right.kind === "Fcn" &&
828
- isEqualList(left.bounds, right.bounds, isEqualBound) &&
829
- isEqualExpr(left.body, right.body)
830
- );
831
- case "FcnApp":
832
- return (
833
- right.kind === "FcnApp" &&
834
- isEqualList([left.function, ...left.args], [right.function, ...right.args], isEqualExpr)
835
- );
836
- case "Product":
837
- return right.kind === "Product" && isEqualList(left.factors, right.factors, isEqualExpr);
838
- case "Rect":
839
- case "Record":
840
- return right.kind === left.kind && isEqualFields(left.fields, right.fields);
841
- case "Except":
842
- return (
843
- right.kind === "Except" &&
844
- isEqualExpr(left.function, right.function) &&
845
- isEqualList(
846
- left.specs,
847
- right.specs,
848
- (leftSpec, rightSpec) =>
849
- isEqualList(leftSpec.trail, rightSpec.trail, (leftPoint, rightPoint) =>
850
- leftPoint.kind === "Dot"
851
- ? rightPoint.kind === "Dot" && leftPoint.field === rightPoint.field
852
- : rightPoint.kind === "Apply" &&
853
- isEqualExpr(leftPoint.expression, rightPoint.expression),
854
- ) && isEqualExpr(leftSpec.value, rightSpec.value),
855
- )
856
- );
857
- case "Dot":
858
- return (
859
- right.kind === "Dot" &&
860
- isEqualExpr(left.expression, right.expression) &&
861
- left.field === right.field
862
- );
863
- case "Sub":
864
- case "Tsub":
865
- return (
866
- right.kind === left.kind &&
867
- left.modal === right.modal &&
868
- isEqualExpr(left.action, right.action) &&
869
- isEqualExpr(left.subscript, right.subscript)
870
- );
871
- case "Fair":
872
- return (
873
- right.kind === "Fair" &&
874
- left.fairness === right.fairness &&
875
- isEqualExpr(left.subscript, right.subscript) &&
876
- isEqualExpr(left.action, right.action)
877
- );
878
- case "Case": {
879
- if (right.kind !== "Case") return false;
880
- const isArmsEqual =
881
- left.arms.length === 0 || right.arms.length === 0
882
- ? left.arms.length === right.arms.length
883
- : isEqualExpr(left.arms[0].condition, right.arms[0].condition) &&
884
- isEqualExpr(left.arms[0].body, right.arms[0].body);
885
- return isArmsEqual && isEqualOptional(left.other, right.other);
886
- }
887
- case "String":
888
- return right.kind === "String" && left.value === right.value;
889
- case "Num":
890
- return (
891
- right.kind === "Num" && left.integer === right.integer && left.fraction === right.fraction
892
- );
893
- case "At":
894
- return right.kind === "At" && left.isExcept === right.isExcept;
895
- }
896
- };
897
-
898
430
  export const mapBoundDomains = (bounds: Bound[], map: (domain: Expr) => Expr): Bound[] =>
899
431
  bounds.map((bound) =>
900
432
  bound.domain.kind === "Domain"
@@ -916,7 +448,7 @@ export const mapExceptSpecs = (specs: ExceptSpec[], map: (inner: Expr) => Expr):
916
448
  return { trail, value };
917
449
  });
918
450
 
919
- const mapDefnBody = (definition: Defn, map: (body: Expr) => Expr): Defn =>
451
+ export const mapDefnBody = (definition: Defn, map: (body: Expr) => Expr): Defn =>
920
452
  definition.kind === "Operator" || definition.kind === "Bpragma"
921
453
  ? { ...definition, body: map(definition.body) }
922
454
  : definition;
@@ -940,7 +472,7 @@ export const mapScoped = <State>(
940
472
  ): Expr => {
941
473
  const recurse = (inner: Expr) => map(state, inner);
942
474
  const bindBounds = (bounds: Bound[]) =>
943
- bounds.reduce((current, bound) => bind(current, getFresh(bound.name, bound.level)), state);
475
+ bounds.reduce((current, bound) => bind(current, getBoundFresh(bound)), state);
944
476
  switch (expression.kind) {
945
477
  case "Ix":
946
478
  case "Internal":
@@ -951,7 +483,7 @@ export const mapScoped = <State>(
951
483
  return expression;
952
484
  case "Lambda": {
953
485
  const inner = expression.params.reduce((current, param) => {
954
- const fresh = getFresh(param.name, "Unknown", param.shape);
486
+ const fresh = getParamFresh(param);
955
487
  return bind(current, param.location ? { ...fresh, location: param.location } : fresh);
956
488
  }, state);
957
489
  return { ...expression, body: map(inner, expression.body) };
@@ -969,14 +501,7 @@ export const mapScoped = <State>(
969
501
  expression.definitions,
970
502
  (current, definition) => {
971
503
  const mapped = mapDefnBody(definition, (body) => map(current, body));
972
- const hyp: Hyp = {
973
- kind: "Defn",
974
- definition: mapped,
975
- whereDefined: "User",
976
- visibility: "Visible",
977
- isLocal: true,
978
- };
979
- return [bind(current, hyp), mapped];
504
+ return [bind(current, getLocalDefn(mapped)), mapped];
980
505
  },
981
506
  );
982
507
  return { ...expression, definitions, body: map(inner, expression.body) };