@toiroakr/chisel 0.2.0 → 0.3.0

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 (167) hide show
  1. package/README.md +114 -10
  2. package/dist/absence.d.ts +1 -0
  3. package/dist/absence.js +39 -1
  4. package/dist/absence.js.map +1 -1
  5. package/dist/behavior.d.ts +5 -10
  6. package/dist/behavior.js +48 -225
  7. package/dist/behavior.js.map +1 -1
  8. package/dist/beside.d.ts +5 -0
  9. package/dist/beside.js +10 -0
  10. package/dist/beside.js.map +1 -1
  11. package/dist/border.d.ts +3 -1
  12. package/dist/border.js +47 -3
  13. package/dist/border.js.map +1 -1
  14. package/dist/bounds.js +4 -3
  15. package/dist/bounds.js.map +1 -1
  16. package/dist/changes.d.ts +23 -0
  17. package/dist/changes.js +355 -0
  18. package/dist/changes.js.map +1 -0
  19. package/dist/checks.d.ts +1 -0
  20. package/dist/checks.js +295 -0
  21. package/dist/checks.js.map +1 -0
  22. package/dist/cli.d.ts +2 -0
  23. package/dist/cli.js +80 -11
  24. package/dist/cli.js.map +1 -1
  25. package/dist/codec.js +9 -3
  26. package/dist/codec.js.map +1 -1
  27. package/dist/codegen.js +2 -0
  28. package/dist/codegen.js.map +1 -1
  29. package/dist/composition.js +5 -4
  30. package/dist/composition.js.map +1 -1
  31. package/dist/conformance.d.ts +24 -0
  32. package/dist/conformance.js +44 -0
  33. package/dist/conformance.js.map +1 -0
  34. package/dist/data.d.ts +2 -2
  35. package/dist/data.js +5 -3
  36. package/dist/data.js.map +1 -1
  37. package/dist/decimal-js.d.ts +2 -0
  38. package/dist/decimal-js.js +5 -0
  39. package/dist/decimal-js.js.map +1 -0
  40. package/dist/decimal.d.ts +2 -0
  41. package/dist/decimal.js +3 -0
  42. package/dist/decimal.js.map +1 -0
  43. package/dist/domain.d.ts +2 -2
  44. package/dist/domain.js +59 -19
  45. package/dist/domain.js.map +1 -1
  46. package/dist/ensures.js +1 -1
  47. package/dist/ensures.js.map +1 -1
  48. package/dist/errors.d.ts +18 -0
  49. package/dist/errors.js +25 -0
  50. package/dist/errors.js.map +1 -0
  51. package/dist/exact.d.ts +13 -1
  52. package/dist/exact.js +52 -3
  53. package/dist/exact.js.map +1 -1
  54. package/dist/feasibility.js +205 -18
  55. package/dist/feasibility.js.map +1 -1
  56. package/dist/guard-borders.d.ts +2 -0
  57. package/dist/guard-borders.js +112 -24
  58. package/dist/guard-borders.js.map +1 -1
  59. package/dist/guard-scope.d.ts +5 -1
  60. package/dist/guard-scope.js +26 -6
  61. package/dist/guard-scope.js.map +1 -1
  62. package/dist/hooks.d.ts +22 -0
  63. package/dist/hooks.js +2 -0
  64. package/dist/hooks.js.map +1 -0
  65. package/dist/index.d.ts +16 -5
  66. package/dist/index.js +12 -4
  67. package/dist/index.js.map +1 -1
  68. package/dist/interval.d.ts +1 -0
  69. package/dist/interval.js +44 -4
  70. package/dist/interval.js.map +1 -1
  71. package/dist/isolated.d.ts +7 -1
  72. package/dist/isolated.js +53 -5
  73. package/dist/isolated.js.map +1 -1
  74. package/dist/kinds/decimal.d.ts +1 -0
  75. package/dist/kinds/decimal.js +16 -0
  76. package/dist/kinds/decimal.js.map +1 -0
  77. package/dist/kinds/rational.d.ts +1 -0
  78. package/dist/kinds/rational.js +13 -0
  79. package/dist/kinds/rational.js.map +1 -0
  80. package/dist/kinds/temporal.d.ts +1 -0
  81. package/dist/kinds/temporal.js +15 -0
  82. package/dist/kinds/temporal.js.map +1 -0
  83. package/dist/lean/behavior.d.ts +29 -0
  84. package/dist/lean/behavior.js +76 -0
  85. package/dist/lean/behavior.js.map +1 -0
  86. package/dist/lean/given.d.ts +10 -0
  87. package/dist/lean/given.js +15 -0
  88. package/dist/lean/given.js.map +1 -0
  89. package/dist/lean/index.d.ts +11 -0
  90. package/dist/lean/index.js +8 -0
  91. package/dist/lean/index.js.map +1 -0
  92. package/dist/lean/model.d.ts +20 -0
  93. package/dist/lean/model.js +133 -0
  94. package/dist/lean/model.js.map +1 -0
  95. package/dist/lean/protocol.d.ts +14 -0
  96. package/dist/lean/protocol.js +34 -0
  97. package/dist/lean/protocol.js.map +1 -0
  98. package/dist/lean/rule.d.ts +54 -0
  99. package/dist/lean/rule.js +149 -0
  100. package/dist/lean/rule.js.map +1 -0
  101. package/dist/lean/schema.d.ts +46 -0
  102. package/dist/lean/schema.js +155 -0
  103. package/dist/lean/schema.js.map +1 -0
  104. package/dist/machine.d.ts +25 -0
  105. package/dist/machine.js +88 -17
  106. package/dist/machine.js.map +1 -1
  107. package/dist/model.d.ts +39 -5
  108. package/dist/model.js +268 -59
  109. package/dist/model.js.map +1 -1
  110. package/dist/off-grid.d.ts +4 -0
  111. package/dist/off-grid.js +92 -0
  112. package/dist/off-grid.js.map +1 -0
  113. package/dist/outcomes.d.ts +9 -1
  114. package/dist/outcomes.js +92 -10
  115. package/dist/outcomes.js.map +1 -1
  116. package/dist/pairs.js +2 -1
  117. package/dist/pairs.js.map +1 -1
  118. package/dist/partition.js +12 -9
  119. package/dist/partition.js.map +1 -1
  120. package/dist/placeholders.d.ts +3 -0
  121. package/dist/placeholders.js +44 -0
  122. package/dist/placeholders.js.map +1 -0
  123. package/dist/proof.js +189 -37
  124. package/dist/proof.js.map +1 -1
  125. package/dist/protocol.d.ts +22 -0
  126. package/dist/protocol.js +12 -0
  127. package/dist/protocol.js.map +1 -0
  128. package/dist/relations.d.ts +30 -0
  129. package/dist/relations.js +235 -0
  130. package/dist/relations.js.map +1 -0
  131. package/dist/rewrites.d.ts +41 -0
  132. package/dist/rewrites.js +180 -0
  133. package/dist/rewrites.js.map +1 -0
  134. package/dist/rule.d.ts +29 -6
  135. package/dist/rule.js +145 -5
  136. package/dist/rule.js.map +1 -1
  137. package/dist/runtime.d.ts +4 -4
  138. package/dist/runtime.js +3 -3
  139. package/dist/runtime.js.map +1 -1
  140. package/dist/schema.d.ts +44 -28
  141. package/dist/schema.js +42 -109
  142. package/dist/schema.js.map +1 -1
  143. package/dist/specification.d.ts +11 -1
  144. package/dist/specification.js +391 -26
  145. package/dist/specification.js.map +1 -1
  146. package/dist/stops.d.ts +20 -0
  147. package/dist/stops.js +221 -0
  148. package/dist/stops.js.map +1 -0
  149. package/dist/temporal.js +5 -5
  150. package/dist/temporal.js.map +1 -1
  151. package/dist/tla-validate.d.ts +31 -0
  152. package/dist/tla-validate.js +151 -0
  153. package/dist/tla-validate.js.map +1 -0
  154. package/dist/tla.d.ts +13 -6
  155. package/dist/tla.js +184 -41
  156. package/dist/tla.js.map +1 -1
  157. package/dist/verification.d.ts +2 -0
  158. package/dist/verification.js +2 -0
  159. package/dist/verification.js.map +1 -1
  160. package/dist/ways.d.ts +4 -1
  161. package/dist/ways.js +23 -8
  162. package/dist/ways.js.map +1 -1
  163. package/dist/world.d.ts +2 -2
  164. package/dist/world.js +13 -17
  165. package/dist/world.js.map +1 -1
  166. package/package.json +23 -7
  167. package/schema/report.schema.json +169 -1
@@ -5,6 +5,7 @@ import { inheritedAt } from "./guard-scope.js";
5
5
  import { carrierOf } from "./partition.js";
6
6
  import { DEPS, boundTermPath, conjuncts, describeRule, differenceOf, holds, isTerm, positionData, positionOf, readOperand, termData, termPaths, counts, partsOfMeasure, positionTerm } from "./rule.js";
7
7
  import { isVariantsSchema, schemaAtPath } from "./schema.js";
8
+ import { relationsVerdict } from "./relations.js";
8
9
  const negated = {
9
10
  "<": ">=",
10
11
  "<=": ">",
@@ -37,7 +38,16 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
37
38
  const groups = new Map();
38
39
  const relations = [];
39
40
  const opaque = [];
41
+ const relationSteps = [];
42
+ // `==`/`!=` between two finite positions, settled with them by the joint search, and between two
43
+ // strings free of invariants, settled by which values the way makes equal.
44
+ const finiteEqualities = [];
45
+ const stringEqualities = [];
40
46
  const outcomes = new Map();
47
+ // What the way holds of quantified conditions, each written with its negation pushed in, so a
48
+ // condition and the same one written negated (`any(xs, x => all(ys, y => y != x))` failing and
49
+ // `all(xs, x => any(ys, y => y == x))` failing) are told to contradict each other.
50
+ const quantified = new Set();
41
51
  const groupFor = (path, measure) => {
42
52
  const key = JSON.stringify([path, measure]);
43
53
  if (!groups.has(key)) {
@@ -110,9 +120,18 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
110
120
  }
111
121
  const { distinction, outcome } = step;
112
122
  if (distinction.kind === "all" || distinction.kind === "any") {
123
+ const held = identityKey(outcome === true ? distinction : negatedRule(distinction));
124
+ const refused = identityKey(outcome === true ? negatedRule(distinction) : distinction);
125
+ if (quantified.has(refused))
126
+ return { kind: "infeasible", reason: contradicts };
127
+ quantified.add(held);
113
128
  opaque.push(termPaths(distinction));
114
129
  continue;
115
130
  }
131
+ if (distinction.kind === "relation") {
132
+ relationSteps.push({ rule: distinction, outcome: outcome === true });
133
+ continue;
134
+ }
116
135
  if (distinction.kind !== "compare") {
117
136
  continue;
118
137
  }
@@ -122,6 +141,13 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
122
141
  constrainForm(form, operator, 0);
123
142
  continue;
124
143
  }
144
+ const equality = equalityOf(distinction, operator, scope);
145
+ if (equality !== undefined) {
146
+ groupFor(equality.left, "value");
147
+ groupFor(equality.right, "value");
148
+ (equality.finite ? finiteEqualities : stringEqualities).push(equality);
149
+ continue;
150
+ }
125
151
  const relation = relationOf(distinction, operator, groupFor);
126
152
  if (relation === undefined) {
127
153
  opaque.push(termPaths(distinction));
@@ -140,8 +166,26 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
140
166
  constrainForm(form, rule.operator, 0, true);
141
167
  }
142
168
  }
143
- const intervals = new Map();
169
+ // Relations on a place no other step reads are settled among themselves; one
170
+ // whose place another step reads too is read with it as an opaque step.
144
171
  let unsettled = false;
172
+ // A place relations settled among themselves is still read: an opaque step
173
+ // on it (another relation read through a transform, an all/any, a
174
+ // comparison not read) leaves the way undecided.
175
+ const relationReads = [];
176
+ for (const verdict of relationsVerdict(relationSteps, scope)) {
177
+ if (verdict.kind === "infeasible")
178
+ return { kind: "infeasible", reason: contradicts };
179
+ const shared = verdict.paths.some(path => [...groups.values()].some(group => sharesValue(path, group.path)) || opaque.flat().some(other => sharesValue(path, other)));
180
+ if (verdict.kind === "unknown" || shared)
181
+ opaque.push(verdict.paths);
182
+ else
183
+ relationReads.push(...verdict.paths);
184
+ // An unknown verdict is no settled way, searched or not: a transformed or quantified subject was never read, so an invariant of its place may refuse it.
185
+ if (verdict.kind === "unknown")
186
+ unsettled = true;
187
+ }
188
+ const intervals = new Map();
145
189
  for (const [key, group] of groups) {
146
190
  const settled = settle(group, scope);
147
191
  if (settled === false) {
@@ -150,7 +194,14 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
150
194
  intervals.set(key, typeof settled === "object" ? settled : undefined);
151
195
  unsettled ||= settled === undefined && group.constraints.length > 1;
152
196
  }
153
- const joint = jointAssignment([...groups.values()], scope, combinations);
197
+ if (stringEqualities.length > 0) {
198
+ const verdict = equalitiesHold(stringEqualities, groups);
199
+ if (verdict === false)
200
+ return { kind: "infeasible", reason: contradicts };
201
+ if (verdict === undefined)
202
+ unsettled = true;
203
+ }
204
+ const joint = jointAssignment([...groups.values()], scope, combinations, undefined, finiteEqualities);
154
205
  if (joint === "none") {
155
206
  return { kind: "infeasible", reason: contradicts };
156
207
  }
@@ -209,7 +260,7 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
209
260
  return { kind: "infeasible", reason: contradicts };
210
261
  }
211
262
  }
212
- const readPaths = [...groups.values()].map(group => group.path);
263
+ const readPaths = [...[...groups.values()].map(group => group.path), ...relationReads];
213
264
  const overlaps = opaque.some((paths, index) => paths.some(path => [...readPaths, ...opaque.filter((_, other) => other !== index).flat()].some(other => sharesValue(path, other))));
214
265
  return overlaps || unsettled
215
266
  ? { kind: "undecided", reason: unreadable }
@@ -253,10 +304,16 @@ function stepIdentity(step) {
253
304
  ? ["match", termData(step.distinction.on)]
254
305
  : ruleIdentity(step.distinction);
255
306
  }
307
+ // The identity of a rule as one string, told apart by the paths it reads rather than by how it is
308
+ // written, since the key "a.b" is written as the path a.b is.
309
+ function identityKey(rule) {
310
+ return JSON.stringify(ruleIdentity(rule), (_, value) => typeof value === "bigint" ? `${value}n` : value);
311
+ }
256
312
  function ruleIdentity(rule) {
257
313
  const operand = (value) => isTerm(value) ? ["term", termData(value)] : ["constant", value];
258
314
  switch (rule.kind) {
259
315
  case "compare": return [rule.kind, rule.operator, operand(rule.left), operand(rule.right)];
316
+ case "relation": return [rule.kind, rule.relation, operand(rule.subject), operand(rule.other)];
260
317
  case "all":
261
318
  case "any": return [rule.kind, operand(rule.of), rule.element, ruleIdentity(rule.each)];
262
319
  case "not": return [rule.kind, ruleIdentity(rule.rule)];
@@ -269,7 +326,7 @@ function ruleIdentity(rule) {
269
326
  function rangeOf(form, intervals, carrier) {
270
327
  const whole = (edge, direction) => edge === undefined || edge.inclusive || carrier !== integerCarrier
271
328
  ? edge
272
- : { value: edge.value + direction, inclusive: true };
329
+ : { value: Number(edge.value) + direction, inclusive: true };
273
330
  const end = (side) => form.parts.reduce((total, { key, coefficient }) => {
274
331
  const interval = intervals.get(key);
275
332
  const taken = (coefficient > 0) === (side === "lower") ? "lower" : "upper";
@@ -277,7 +334,7 @@ function rangeOf(form, intervals, carrier) {
277
334
  return total === undefined || edge === undefined
278
335
  ? undefined
279
336
  : {
280
- value: total.value + coefficient * edge.value,
337
+ value: total.value + coefficient * Number(edge.value),
281
338
  inclusive: total.inclusive && edge.inclusive,
282
339
  };
283
340
  }, { value: 0, inclusive: true });
@@ -304,7 +361,7 @@ function numericFormOf(rule, scope, prefix) {
304
361
  const parts = form.parts.map(part => ({ ...part, path: [...prefix, ...part.path] }));
305
362
  const kept = parts.every(part => part.measure === "value" &&
306
363
  !leftOutAlong(scope, part.path) &&
307
- ["integer", "number"].includes(schemaAtPath(scope, part.path)?.kind ?? ""));
364
+ ["integer", "number"].includes(schemaThrough(scope, part.path)?.kind ?? ""));
308
365
  return parts.length >= 2 && kept && typeof form.constant === "number" ? { ...form, parts } : undefined;
309
366
  }
310
367
  function leftOutAlong(scope, path) {
@@ -321,7 +378,7 @@ function readableFormOf(rule, scope) {
321
378
  return undefined;
322
379
  }
323
380
  const form = differenceOf(rule);
324
- const kinds = form.parts.map(part => counts(part.measure) ? "integer" : schemaAtPath(scope, part.path)?.kind);
381
+ const kinds = form.parts.map(part => counts(part.measure) ? "integer" : schemaThrough(scope, part.path)?.kind);
325
382
  // A part that may be left out is not one: the comparison holds without it.
326
383
  return typeof form.constant === "number" &&
327
384
  kinds.every(kind => kind === "integer" || kind === "number") &&
@@ -329,6 +386,105 @@ function readableFormOf(rule, scope) {
329
386
  ? form
330
387
  : undefined;
331
388
  }
389
+ // The rule with its negation pushed in through quantifiers and connectives, which every way of
390
+ // reading it the analyses use agrees with: what a way reads is never absent, since a comparison
391
+ // reading a value that may be left out is refused where it is written.
392
+ function negatedRule(rule) {
393
+ switch (rule.kind) {
394
+ case "compare": return { ...rule, operator: negated[rule.operator] };
395
+ case "all": return { kind: "any", of: rule.of, element: rule.element, each: negatedRule(rule.each) };
396
+ case "any": return { kind: "all", of: rule.of, element: rule.element, each: negatedRule(rule.each) };
397
+ case "and": return { kind: "or", rules: rule.rules.map(negatedRule) };
398
+ case "or": return { kind: "and", rules: rule.rules.map(negatedRule) };
399
+ case "not": return rule.rule;
400
+ case "relation": return { kind: "not", rule };
401
+ }
402
+ }
403
+ // A comparison by `==` or `!=` of two values settled without an order: two finite positions, or
404
+ // two strings no invariant of their own narrows.
405
+ function equalityOf(rule, operator, scope) {
406
+ if (operator !== "==" && operator !== "!=")
407
+ return undefined;
408
+ if (!isTerm(rule.left) || !isTerm(rule.right))
409
+ return undefined;
410
+ const left = positionData(rule.left), right = positionData(rule.right);
411
+ if (left === undefined || right === undefined || left.measure !== "value" || right.measure !== "value")
412
+ return undefined;
413
+ const equal = operator === "==";
414
+ if (finiteDomainThrough(scope, left.path) !== undefined && finiteDomainThrough(scope, right.path) !== undefined)
415
+ return { left: left.path, right: right.path, equal, finite: true };
416
+ const free = (path) => {
417
+ const schemas = declaredAt(scope, path);
418
+ return schemas.length > 0 && schemas.every(schema => schema.kind === "string" && schema.invariants.length === 0);
419
+ };
420
+ return free(left.path) && free(right.path) ? { left: left.path, right: right.path, equal, finite: false } : undefined;
421
+ }
422
+ // The schema at a path, or, inside a case of a sum field, the one schema every case declaring the
423
+ // path gives it. A way reads such a field only where an earlier step settled the case (a read of
424
+ // one that may be left out is refused where it is written), so it is read as present.
425
+ function schemaThrough(scope, path) {
426
+ const direct = schemaAtPath(scope, path);
427
+ if (direct !== undefined)
428
+ return direct;
429
+ const declared = declaredAt(scope, path);
430
+ return declared.length > 0 && declared.every(schema => schema === declared[0]) ? declared[0] : undefined;
431
+ }
432
+ // The schemas a path reaches through every case of a sum that declares its next key; none where a
433
+ // key is declared nowhere, or the path passes an optional, whose absence no comparison settles.
434
+ function declaredAt(schema, path) {
435
+ const [key, ...rest] = path;
436
+ if (key === undefined)
437
+ return [schema];
438
+ if (isVariantsSchema(schema)) {
439
+ return Object.values(schema.variants)
440
+ .filter(variant => Object.hasOwn(variant.shape, key))
441
+ .flatMap(variant => declaredAt(variant.shape[key], rest));
442
+ }
443
+ if (schema.kind !== "object")
444
+ return [];
445
+ const { shape } = schema;
446
+ return Object.hasOwn(shape, key) ? declaredAt(shape[key], rest) : [];
447
+ }
448
+ // Whether strings, each any value but the constants the way pins it to or keeps it from, can be
449
+ // equal and unequal as the way asks: those it makes equal are one value, pinned to at most one
450
+ // constant; two it makes unequal are neither one value nor pinned to one constant. A string has
451
+ // values to spare, so nothing else can refute it. Undefined where a string is also ordered.
452
+ function equalitiesHold(equalities, groups) {
453
+ const parent = new Map();
454
+ const find = (key) => { const up = parent.get(key) ?? key; if (up === key)
455
+ return key; const root = find(up); parent.set(key, root); return root; };
456
+ const keyOf = (path) => JSON.stringify([path, "value"]);
457
+ for (const { left, right, equal } of equalities)
458
+ if (equal)
459
+ parent.set(find(keyOf(left)), find(keyOf(right)));
460
+ const pinned = new Map();
461
+ const excluded = new Map();
462
+ for (const { left, right } of equalities)
463
+ for (const key of [keyOf(left), keyOf(right)]) {
464
+ for (const constraint of groups.get(key)?.constraints ?? []) {
465
+ if ((constraint.operator !== "==" && constraint.operator !== "!=") || typeof constraint.bound !== "string")
466
+ return undefined;
467
+ const into = constraint.operator === "==" ? pinned : excluded;
468
+ const root = find(key);
469
+ into.set(root, (into.get(root) ?? new Set()).add(constraint.bound.normalize("NFC")));
470
+ }
471
+ }
472
+ for (const [root, values] of pinned) {
473
+ if (values.size > 1 || [...values].some(value => excluded.get(root)?.has(value)))
474
+ return false;
475
+ }
476
+ for (const { left, right, equal } of equalities) {
477
+ if (equal)
478
+ continue;
479
+ const [a, b] = [find(keyOf(left)), find(keyOf(right))];
480
+ if (a === b)
481
+ return false;
482
+ const [x, y] = [pinned.get(a), pinned.get(b)];
483
+ if (x?.size === 1 && y?.size === 1 && [...x][0] === [...y][0])
484
+ return false;
485
+ }
486
+ return true;
487
+ }
332
488
  function relationOf(rule, operator, groupFor) {
333
489
  if (!isTerm(rule.left) || !isTerm(rule.right)) {
334
490
  return undefined;
@@ -348,8 +504,9 @@ function relationOf(rule, operator, groupFor) {
348
504
  // two decimals or two instants differ in units their readers convert to, which
349
505
  // the steps of a way do not carry.
350
506
  function differenceCarrierOf(sides, scope) {
351
- const kinds = sides.map(side => counts(side.measure) ? "integer" : schemaAtPath(scope, side.path)?.kind);
352
- if (kinds.every(kind => kind === "integer")) {
507
+ const kinds = sides.map(side => counts(side.measure) ? "integer" : schemaThrough(scope, side.path)?.kind);
508
+ // A Date is a whole number of milliseconds, so two of them differ by one, as two integers do.
509
+ if (kinds.every(kind => kind === "integer") || kinds.every(kind => kind === "jsDate")) {
353
510
  return integerCarrier;
354
511
  }
355
512
  return kinds.every(kind => kind === "integer" || kind === "number") ? numberCarrier : undefined;
@@ -364,7 +521,7 @@ function settle(group, scope) {
364
521
  if (domain !== undefined) {
365
522
  return finite(domain, group.constraints);
366
523
  }
367
- const schema = schemaAtPath(scope, path);
524
+ const schema = schemaThrough(scope, path);
368
525
  if (schema === undefined) {
369
526
  return undefined;
370
527
  }
@@ -397,7 +554,7 @@ function admittedBy(domain, schema, scope, path) {
397
554
  return domain.filter(value => own.every(rule => holds(rule, value)));
398
555
  }
399
556
  function sumOwning(scope, path) {
400
- const owner = schemaAtPath(scope, path.slice(0, -1));
557
+ const owner = schemaThrough(scope, path.slice(0, -1));
401
558
  return owner !== undefined && isVariantsSchema(owner) && owner.discriminant === path[path.length - 1]
402
559
  ? owner
403
560
  : undefined;
@@ -600,6 +757,25 @@ export function witnessesOf(way, scope, inputCase, combinations = FEASIBILITY_CO
600
757
  }
601
758
  })();
602
759
  }
760
+ // What finiteDomainAt answers, or, for a position inside a case of a sum field, the values it takes
761
+ // in the one schema every case declaring it gives it.
762
+ function finiteDomainThrough(scope, path) {
763
+ const direct = finiteDomainAt(scope, path);
764
+ if (direct !== undefined)
765
+ return direct;
766
+ const owners = declaredAt(scope, path.slice(0, -1));
767
+ const key = path[path.length - 1];
768
+ if (owners.length === 0 || key === undefined)
769
+ return undefined;
770
+ if (owners.every(owner => isVariantsSchema(owner) && owner.discriminant === key)) {
771
+ return [...new Set(owners.flatMap(owner => owner.variantTags))];
772
+ }
773
+ const leaves = declaredAt(scope, path);
774
+ if (leaves.length === 0 || leaves.some(leaf => leaf !== leaves[0] || leaf.invariants.length > 0))
775
+ return undefined;
776
+ const leaf = leaves[0];
777
+ return leaf.kind === "boolean" ? [true, false] : leaf.kind === "enum" ? leaf.values : undefined;
778
+ }
603
779
  function finiteDomainAt(scope, path) {
604
780
  const discriminated = sumOwning(scope, path);
605
781
  if (discriminated !== undefined) {
@@ -610,13 +786,17 @@ function finiteDomainAt(scope, path) {
610
786
  .filter(rule => deepEqual(boundTermPath(rule), [discriminant]));
611
787
  return discriminated.variantTags.filter(tag => refusing.every(rule => holds(rule, { [discriminant]: tag })));
612
788
  }
613
- const schema = schemaAtPath(scope, path);
789
+ const schema = schemaThrough(scope, path);
614
790
  if (schema?.kind === "boolean") {
615
791
  return admittedBy([true, false], schema, scope, path);
616
792
  }
617
793
  if (schema?.kind === "enum") {
618
794
  return admittedBy(schema.values, schema, scope, path);
619
795
  }
796
+ // A sum's discriminant read where the way settled its case is that case's tag alone.
797
+ if (schema?.kind === "literal") {
798
+ return admittedBy([schema.value], schema, scope, path);
799
+ }
620
800
  return undefined;
621
801
  }
622
802
  // A rule over finite positions: each comparison reads one of them, or what
@@ -667,10 +847,17 @@ function finitePathsOf(rule, scope, prefix) {
667
847
  // many" when there are more combinations to try than the limit. Unless
668
848
  // `whole`, positions no such invariant reaches are left out, and nothing is
669
849
  // tried when there are none.
670
- function jointAssignments(groups, scope, combinations, prefer = () => undefined, whole = false) {
671
- const joint = jointInvariantsOf(scope);
850
+ function jointAssignments(groups, scope, combinations, prefer = () => undefined, whole = false, equalities = []) {
851
+ const joint = [...jointInvariantsOf(scope), ...equalities.map((equality) => ({
852
+ prefix: [],
853
+ paths: [equality.left, equality.right],
854
+ rule: { kind: "compare", operator: equality.equal ? "==" : "!=", left: positionTerm(equality.left, "value"), right: positionTerm(equality.right, "value") },
855
+ }))];
672
856
  const constrained = groups.filter(group => group.measure === "value" && finiteDomainAt(scope, group.path) !== undefined);
673
857
  const component = new Map(constrained.map(group => [JSON.stringify(group.path), group.path]));
858
+ for (const equality of equalities)
859
+ for (const path of [equality.left, equality.right])
860
+ component.set(JSON.stringify(path), path);
674
861
  const involved = [];
675
862
  for (let grew = true; grew;) {
676
863
  grew = false;
@@ -691,7 +878,7 @@ function jointAssignments(groups, scope, combinations, prefer = () => undefined,
691
878
  }
692
879
  const domains = positions.map(path => {
693
880
  const keeps = constrained.filter(group => JSON.stringify(group.path) === JSON.stringify(path));
694
- const kept = finiteDomainAt(scope, path).filter(value => keeps.every(group => group.constraints.every(item => (value === item.bound) === (item.operator === "=="))));
881
+ const kept = finiteDomainThrough(scope, path).filter(value => keeps.every(group => group.constraints.every(item => holds({ kind: "compare", operator: item.operator, left: positionTerm([], "value"), right: item.bound }, value))));
695
882
  const preferred = prefer(path);
696
883
  return kept.includes(preferred) ? [preferred, ...kept.filter(value => value !== preferred)] : kept;
697
884
  });
@@ -713,8 +900,8 @@ function jointAssignments(groups, scope, combinations, prefer = () => undefined,
713
900
  return from(0, []);
714
901
  }
715
902
  // The first of the combinations jointAssignments tries, or "none".
716
- function jointAssignment(groups, scope, combinations, prefer) {
717
- const tried = jointAssignments(groups, scope, combinations, prefer);
903
+ function jointAssignment(groups, scope, combinations, prefer, equalities = []) {
904
+ const tried = jointAssignments(groups, scope, combinations, prefer, false, equalities);
718
905
  if (tried === "too many") {
719
906
  return "too many";
720
907
  }
@@ -790,7 +977,7 @@ export function keepingOrderings(scope, before, after) {
790
977
  }
791
978
  const form = differenceOf(rule);
792
979
  const parts = form.parts.map(part => ({ ...part, path: [...prefix, ...part.path] }));
793
- const numeric = parts.every(part => part.measure === "value" && ["integer", "number"].includes(schemaAtPath(scope, part.path)?.kind ?? ""));
980
+ const numeric = parts.every(part => part.measure === "value" && ["integer", "number"].includes(schemaThrough(scope, part.path)?.kind ?? ""));
794
981
  return parts.length >= 2 && numeric && typeof form.constant === "number"
795
982
  ? [{ rule: rule, parts, constant: form.constant }]
796
983
  : [];