@toiroakr/chisel 0.1.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 (191) hide show
  1. package/README.md +354 -21
  2. package/dist/absence.d.ts +28 -0
  3. package/dist/absence.js +266 -0
  4. package/dist/absence.js.map +1 -0
  5. package/dist/behavior.d.ts +26 -19
  6. package/dist/behavior.js +193 -203
  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 +7 -8
  12. package/dist/border.js +88 -36
  13. package/dist/border.js.map +1 -1
  14. package/dist/bounds.d.ts +1 -0
  15. package/dist/bounds.js +27 -0
  16. package/dist/bounds.js.map +1 -0
  17. package/dist/changes.d.ts +23 -0
  18. package/dist/changes.js +355 -0
  19. package/dist/changes.js.map +1 -0
  20. package/dist/checks.d.ts +1 -0
  21. package/dist/checks.js +295 -0
  22. package/dist/checks.js.map +1 -0
  23. package/dist/cli.d.ts +4 -0
  24. package/dist/cli.js +351 -14
  25. package/dist/cli.js.map +1 -1
  26. package/dist/codec.js +16 -7
  27. package/dist/codec.js.map +1 -1
  28. package/dist/codegen.js +6 -8
  29. package/dist/codegen.js.map +1 -1
  30. package/dist/composition.d.ts +3 -3
  31. package/dist/composition.js +9 -8
  32. package/dist/composition.js.map +1 -1
  33. package/dist/conformance.d.ts +24 -0
  34. package/dist/conformance.js +44 -0
  35. package/dist/conformance.js.map +1 -0
  36. package/dist/data.d.ts +3 -2
  37. package/dist/data.js +7 -3
  38. package/dist/data.js.map +1 -1
  39. package/dist/decimal-js.d.ts +2 -0
  40. package/dist/decimal-js.js +5 -0
  41. package/dist/decimal-js.js.map +1 -0
  42. package/dist/decimal.d.ts +2 -0
  43. package/dist/decimal.js +3 -0
  44. package/dist/decimal.js.map +1 -0
  45. package/dist/dependency.d.ts +9 -6
  46. package/dist/dependency.js +23 -17
  47. package/dist/dependency.js.map +1 -1
  48. package/dist/domain.d.ts +6 -2
  49. package/dist/domain.js +65 -25
  50. package/dist/domain.js.map +1 -1
  51. package/dist/ensures.js +3 -3
  52. package/dist/ensures.js.map +1 -1
  53. package/dist/equal.d.ts +1 -0
  54. package/dist/equal.js +135 -0
  55. package/dist/equal.js.map +1 -0
  56. package/dist/errors.d.ts +18 -0
  57. package/dist/errors.js +25 -0
  58. package/dist/errors.js.map +1 -0
  59. package/dist/exact.d.ts +13 -1
  60. package/dist/exact.js +52 -3
  61. package/dist/exact.js.map +1 -1
  62. package/dist/feasibility.d.ts +5 -3
  63. package/dist/feasibility.js +309 -53
  64. package/dist/feasibility.js.map +1 -1
  65. package/dist/guard-borders.d.ts +14 -5
  66. package/dist/guard-borders.js +229 -190
  67. package/dist/guard-borders.js.map +1 -1
  68. package/dist/guard-scope.d.ts +65 -0
  69. package/dist/guard-scope.js +384 -0
  70. package/dist/guard-scope.js.map +1 -0
  71. package/dist/hooks.d.ts +22 -0
  72. package/dist/hooks.js +2 -0
  73. package/dist/hooks.js.map +1 -0
  74. package/dist/index.d.ts +30 -8
  75. package/dist/index.js +18 -5
  76. package/dist/index.js.map +1 -1
  77. package/dist/interleave.d.ts +31 -0
  78. package/dist/interleave.js +202 -0
  79. package/dist/interleave.js.map +1 -0
  80. package/dist/interval.d.ts +8 -0
  81. package/dist/interval.js +213 -23
  82. package/dist/interval.js.map +1 -1
  83. package/dist/isolated.d.ts +7 -1
  84. package/dist/isolated.js +53 -5
  85. package/dist/isolated.js.map +1 -1
  86. package/dist/kinds/decimal.d.ts +1 -0
  87. package/dist/kinds/decimal.js +16 -0
  88. package/dist/kinds/decimal.js.map +1 -0
  89. package/dist/kinds/rational.d.ts +1 -0
  90. package/dist/kinds/rational.js +13 -0
  91. package/dist/kinds/rational.js.map +1 -0
  92. package/dist/kinds/temporal.d.ts +1 -0
  93. package/dist/kinds/temporal.js +15 -0
  94. package/dist/kinds/temporal.js.map +1 -0
  95. package/dist/lean/behavior.d.ts +29 -0
  96. package/dist/lean/behavior.js +76 -0
  97. package/dist/lean/behavior.js.map +1 -0
  98. package/dist/lean/given.d.ts +10 -0
  99. package/dist/lean/given.js +15 -0
  100. package/dist/lean/given.js.map +1 -0
  101. package/dist/lean/index.d.ts +11 -0
  102. package/dist/lean/index.js +8 -0
  103. package/dist/lean/index.js.map +1 -0
  104. package/dist/lean/model.d.ts +20 -0
  105. package/dist/lean/model.js +133 -0
  106. package/dist/lean/model.js.map +1 -0
  107. package/dist/lean/protocol.d.ts +14 -0
  108. package/dist/lean/protocol.js +34 -0
  109. package/dist/lean/protocol.js.map +1 -0
  110. package/dist/lean/rule.d.ts +54 -0
  111. package/dist/lean/rule.js +149 -0
  112. package/dist/lean/rule.js.map +1 -0
  113. package/dist/lean/schema.d.ts +46 -0
  114. package/dist/lean/schema.js +155 -0
  115. package/dist/lean/schema.js.map +1 -0
  116. package/dist/machine-check.d.ts +73 -0
  117. package/dist/machine-check.js +215 -0
  118. package/dist/machine-check.js.map +1 -0
  119. package/dist/machine.d.ts +122 -0
  120. package/dist/machine.js +271 -0
  121. package/dist/machine.js.map +1 -0
  122. package/dist/model.d.ts +110 -11
  123. package/dist/model.js +724 -80
  124. package/dist/model.js.map +1 -1
  125. package/dist/off-grid.d.ts +4 -0
  126. package/dist/off-grid.js +92 -0
  127. package/dist/off-grid.js.map +1 -0
  128. package/dist/outcomes.d.ts +15 -0
  129. package/dist/outcomes.js +120 -0
  130. package/dist/outcomes.js.map +1 -0
  131. package/dist/pairs.js +14 -5
  132. package/dist/pairs.js.map +1 -1
  133. package/dist/partition.d.ts +1 -0
  134. package/dist/partition.js +101 -19
  135. package/dist/partition.js.map +1 -1
  136. package/dist/placeholders.d.ts +3 -0
  137. package/dist/placeholders.js +44 -0
  138. package/dist/placeholders.js.map +1 -0
  139. package/dist/proof.d.ts +6 -0
  140. package/dist/proof.js +282 -56
  141. package/dist/proof.js.map +1 -1
  142. package/dist/protocol.d.ts +22 -0
  143. package/dist/protocol.js +12 -0
  144. package/dist/protocol.js.map +1 -0
  145. package/dist/relations.d.ts +30 -0
  146. package/dist/relations.js +235 -0
  147. package/dist/relations.js.map +1 -0
  148. package/dist/replay.d.ts +45 -0
  149. package/dist/replay.js +157 -0
  150. package/dist/replay.js.map +1 -0
  151. package/dist/rewrites.d.ts +41 -0
  152. package/dist/rewrites.js +180 -0
  153. package/dist/rewrites.js.map +1 -0
  154. package/dist/rule.d.ts +74 -11
  155. package/dist/rule.js +429 -22
  156. package/dist/rule.js.map +1 -1
  157. package/dist/runtime.d.ts +12 -0
  158. package/dist/runtime.js +8 -0
  159. package/dist/runtime.js.map +1 -0
  160. package/dist/schema.d.ts +44 -28
  161. package/dist/schema.js +50 -113
  162. package/dist/schema.js.map +1 -1
  163. package/dist/specification.d.ts +23 -3
  164. package/dist/specification.js +1001 -96
  165. package/dist/specification.js.map +1 -1
  166. package/dist/stops.d.ts +20 -0
  167. package/dist/stops.js +221 -0
  168. package/dist/stops.js.map +1 -0
  169. package/dist/temporal-global.d.ts +4 -0
  170. package/dist/temporal-global.js +11 -0
  171. package/dist/temporal-global.js.map +1 -0
  172. package/dist/temporal.d.ts +22 -0
  173. package/dist/temporal.js +113 -0
  174. package/dist/temporal.js.map +1 -0
  175. package/dist/tla-validate.d.ts +31 -0
  176. package/dist/tla-validate.js +151 -0
  177. package/dist/tla-validate.js.map +1 -0
  178. package/dist/tla.d.ts +61 -0
  179. package/dist/tla.js +722 -0
  180. package/dist/tla.js.map +1 -0
  181. package/dist/verification.d.ts +9 -0
  182. package/dist/verification.js +7 -0
  183. package/dist/verification.js.map +1 -0
  184. package/dist/ways.d.ts +11 -5
  185. package/dist/ways.js +51 -45
  186. package/dist/ways.js.map +1 -1
  187. package/dist/world.d.ts +98 -0
  188. package/dist/world.js +463 -0
  189. package/dist/world.js.map +1 -0
  190. package/package.json +49 -10
  191. package/schema/report.schema.json +169 -1
@@ -1,9 +1,11 @@
1
- import { isDeepStrictEqual } from "node:util";
1
+ import { compatibleBound } from "./interval.js";
2
+ import { deepEqual } from "./equal.js";
2
3
  import { integerCarrier, normalize, numberCarrier } from "./border.js";
3
- import { inheritedAt } from "./guard-borders.js";
4
+ import { inheritedAt } from "./guard-scope.js";
4
5
  import { carrierOf } from "./partition.js";
5
- import { DEPS, boundTermPath, conjuncts, describeRule, differenceOf, holds, isTerm, positionData, positionOf, readOperand, termData, termPaths } from "./rule.js";
6
+ import { DEPS, boundTermPath, conjuncts, describeRule, differenceOf, holds, isTerm, positionData, positionOf, readOperand, termData, termPaths, counts, partsOfMeasure, positionTerm } from "./rule.js";
6
7
  import { isVariantsSchema, schemaAtPath } from "./schema.js";
8
+ import { relationsVerdict } from "./relations.js";
7
9
  const negated = {
8
10
  "<": ">=",
9
11
  "<=": ">",
@@ -36,7 +38,16 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
36
38
  const groups = new Map();
37
39
  const relations = [];
38
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 = [];
39
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();
40
51
  const groupFor = (path, measure) => {
41
52
  const key = JSON.stringify([path, measure]);
42
53
  if (!groups.has(key)) {
@@ -93,7 +104,7 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
93
104
  const identity = stepIdentity(step);
94
105
  const key = step.distinction.kind === "match" ? JSON.stringify(["match", positionData(step.distinction.on)?.path]) : describeRule(step.distinction);
95
106
  const bucket = outcomes.get(key) ?? [];
96
- const seen = bucket.find(previous => isDeepStrictEqual(previous.identity, identity));
107
+ const seen = bucket.find(previous => deepEqual(previous.identity, identity));
97
108
  if (seen !== undefined) {
98
109
  if (seen.outcome !== step.outcome) {
99
110
  return { kind: "infeasible", reason: contradicts };
@@ -102,16 +113,25 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
102
113
  }
103
114
  bucket.push({ identity, outcome: step.outcome });
104
115
  outcomes.set(key, bucket);
105
- const placed = placedConstraintOf(step);
116
+ const placed = placedConstraintOf(step, scope);
106
117
  if (placed !== undefined) {
107
- groups.get(groupFor(placed.path, placed.measure)).constraints.push(placed.constraint);
118
+ groups.get(groupFor(placed.path, placed.measure)).constraints.push(...placed.constraints);
108
119
  continue;
109
120
  }
110
121
  const { distinction, outcome } = step;
111
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);
112
128
  opaque.push(termPaths(distinction));
113
129
  continue;
114
130
  }
131
+ if (distinction.kind === "relation") {
132
+ relationSteps.push({ rule: distinction, outcome: outcome === true });
133
+ continue;
134
+ }
115
135
  if (distinction.kind !== "compare") {
116
136
  continue;
117
137
  }
@@ -121,6 +141,13 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
121
141
  constrainForm(form, operator, 0);
122
142
  continue;
123
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
+ }
124
151
  const relation = relationOf(distinction, operator, groupFor);
125
152
  if (relation === undefined) {
126
153
  opaque.push(termPaths(distinction));
@@ -139,8 +166,26 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
139
166
  constrainForm(form, rule.operator, 0, true);
140
167
  }
141
168
  }
142
- 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.
143
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();
144
189
  for (const [key, group] of groups) {
145
190
  const settled = settle(group, scope);
146
191
  if (settled === false) {
@@ -149,7 +194,14 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
149
194
  intervals.set(key, typeof settled === "object" ? settled : undefined);
150
195
  unsettled ||= settled === undefined && group.constraints.length > 1;
151
196
  }
152
- 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);
153
205
  if (joint === "none") {
154
206
  return { kind: "infeasible", reason: contradicts };
155
207
  }
@@ -208,17 +260,17 @@ export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_
208
260
  return { kind: "infeasible", reason: contradicts };
209
261
  }
210
262
  }
211
- const readPaths = [...groups.values()].map(group => group.path);
263
+ const readPaths = [...[...groups.values()].map(group => group.path), ...relationReads];
212
264
  const overlaps = opaque.some((paths, index) => paths.some(path => [...readPaths, ...opaque.filter((_, other) => other !== index).flat()].some(other => sharesValue(path, other))));
213
265
  return overlaps || unsettled
214
266
  ? { kind: "undecided", reason: unreadable }
215
267
  : { kind: "feasible" };
216
268
  }
217
- // The constraint a step puts on one coordinate: a match, or a comparison with a constant.
218
- function placedConstraintOf(step) {
269
+ // The constraints a step puts on one coordinate: a match, or a comparison with a constant.
270
+ function placedConstraintOf(step, scope) {
219
271
  const { distinction, outcome } = step;
220
272
  if (distinction.kind === "match") {
221
- return { path: positionOf(distinction.on).path, measure: "value", constraint: { operator: "==", bound: outcome } };
273
+ return { path: positionOf(distinction.on).path, measure: "value", constraints: [{ operator: "==", bound: outcome }] };
222
274
  }
223
275
  if (distinction.kind !== "compare") {
224
276
  return undefined;
@@ -227,25 +279,41 @@ function placedConstraintOf(step) {
227
279
  if (normalized === undefined) {
228
280
  return undefined;
229
281
  }
230
- const term = (isTerm(distinction.left) ? distinction.left : distinction.right);
231
- return {
232
- path: positionOf(term).path,
233
- measure: normalized.measure,
234
- constraint: {
235
- operator: outcome === true ? normalized.operator : negated[normalized.operator],
236
- bound: normalized.bound,
237
- },
282
+ const path = positionOf((isTerm(distinction.left) ? distinction.left : distinction.right)).path;
283
+ const constraint = {
284
+ operator: outcome === true ? normalized.operator : negated[normalized.operator],
285
+ bound: normalized.bound,
238
286
  };
287
+ // A transformed read of a finite position, `lowercase($.level)` of an enum,
288
+ // is read off each of its values: the step refuses the values whose reading
289
+ // the constraint does not keep, so it is settled with the value's own steps.
290
+ const { transforms, reading } = partsOfMeasure(normalized.measure);
291
+ const domain = transforms.length > 0 && reading === "value" ? finiteDomainAt(scope, path) : undefined;
292
+ if (domain !== undefined) {
293
+ const kept = { kind: "compare", left: positionTerm([], normalized.measure), operator: constraint.operator, right: constraint.bound };
294
+ return {
295
+ path,
296
+ measure: "value",
297
+ constraints: domain.filter(value => !holds(kept, value)).map(value => ({ operator: "!=", bound: value })),
298
+ };
299
+ }
300
+ return { path, measure: normalized.measure, constraints: [constraint] };
239
301
  }
240
302
  function stepIdentity(step) {
241
303
  return step.distinction.kind === "match"
242
304
  ? ["match", termData(step.distinction.on)]
243
305
  : ruleIdentity(step.distinction);
244
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
+ }
245
312
  function ruleIdentity(rule) {
246
313
  const operand = (value) => isTerm(value) ? ["term", termData(value)] : ["constant", value];
247
314
  switch (rule.kind) {
248
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)];
249
317
  case "all":
250
318
  case "any": return [rule.kind, operand(rule.of), rule.element, ruleIdentity(rule.each)];
251
319
  case "not": return [rule.kind, ruleIdentity(rule.rule)];
@@ -258,7 +326,7 @@ function ruleIdentity(rule) {
258
326
  function rangeOf(form, intervals, carrier) {
259
327
  const whole = (edge, direction) => edge === undefined || edge.inclusive || carrier !== integerCarrier
260
328
  ? edge
261
- : { value: edge.value + direction, inclusive: true };
329
+ : { value: Number(edge.value) + direction, inclusive: true };
262
330
  const end = (side) => form.parts.reduce((total, { key, coefficient }) => {
263
331
  const interval = intervals.get(key);
264
332
  const taken = (coefficient > 0) === (side === "lower") ? "lower" : "upper";
@@ -266,7 +334,7 @@ function rangeOf(form, intervals, carrier) {
266
334
  return total === undefined || edge === undefined
267
335
  ? undefined
268
336
  : {
269
- value: total.value + coefficient * edge.value,
337
+ value: total.value + coefficient * Number(edge.value),
270
338
  inclusive: total.inclusive && edge.inclusive,
271
339
  };
272
340
  }, { value: 0, inclusive: true });
@@ -293,7 +361,7 @@ function numericFormOf(rule, scope, prefix) {
293
361
  const parts = form.parts.map(part => ({ ...part, path: [...prefix, ...part.path] }));
294
362
  const kept = parts.every(part => part.measure === "value" &&
295
363
  !leftOutAlong(scope, part.path) &&
296
- ["integer", "number"].includes(schemaAtPath(scope, part.path)?.kind ?? ""));
364
+ ["integer", "number"].includes(schemaThrough(scope, part.path)?.kind ?? ""));
297
365
  return parts.length >= 2 && kept && typeof form.constant === "number" ? { ...form, parts } : undefined;
298
366
  }
299
367
  function leftOutAlong(scope, path) {
@@ -310,7 +378,7 @@ function readableFormOf(rule, scope) {
310
378
  return undefined;
311
379
  }
312
380
  const form = differenceOf(rule);
313
- const kinds = form.parts.map(part => part.measure === "length" ? "integer" : schemaAtPath(scope, part.path)?.kind);
381
+ const kinds = form.parts.map(part => counts(part.measure) ? "integer" : schemaThrough(scope, part.path)?.kind);
314
382
  // A part that may be left out is not one: the comparison holds without it.
315
383
  return typeof form.constant === "number" &&
316
384
  kinds.every(kind => kind === "integer" || kind === "number") &&
@@ -318,6 +386,105 @@ function readableFormOf(rule, scope) {
318
386
  ? form
319
387
  : undefined;
320
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
+ }
321
488
  function relationOf(rule, operator, groupFor) {
322
489
  if (!isTerm(rule.left) || !isTerm(rule.right)) {
323
490
  return undefined;
@@ -337,8 +504,9 @@ function relationOf(rule, operator, groupFor) {
337
504
  // two decimals or two instants differ in units their readers convert to, which
338
505
  // the steps of a way do not carry.
339
506
  function differenceCarrierOf(sides, scope) {
340
- const kinds = sides.map(side => side.measure === "length" ? "integer" : schemaAtPath(scope, side.path)?.kind);
341
- 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")) {
342
510
  return integerCarrier;
343
511
  }
344
512
  return kinds.every(kind => kind === "integer" || kind === "number") ? numberCarrier : undefined;
@@ -353,7 +521,7 @@ function settle(group, scope) {
353
521
  if (domain !== undefined) {
354
522
  return finite(domain, group.constraints);
355
523
  }
356
- const schema = schemaAtPath(scope, path);
524
+ const schema = schemaThrough(scope, path);
357
525
  if (schema === undefined) {
358
526
  return undefined;
359
527
  }
@@ -372,7 +540,10 @@ function settle(group, scope) {
372
540
  ? []
373
541
  : [{ operator: normalized.operator, bound: normalized.bound }];
374
542
  });
375
- return ordered([...group.constraints, ...invariants], carrier);
543
+ const constraints = [...group.constraints, ...invariants];
544
+ if (constraints.some(item => !compatibleBound(item.bound, counts(measure) ? "number" : schema.kind)))
545
+ return undefined;
546
+ return ordered(constraints, carrier);
376
547
  }
377
548
  // The values of a finite domain the position's own invariants keep, the way its
378
549
  // classes leave out the ones an invariant refuses.
@@ -383,7 +554,7 @@ function admittedBy(domain, schema, scope, path) {
383
554
  return domain.filter(value => own.every(rule => holds(rule, value)));
384
555
  }
385
556
  function sumOwning(scope, path) {
386
- const owner = schemaAtPath(scope, path.slice(0, -1));
557
+ const owner = schemaThrough(scope, path.slice(0, -1));
387
558
  return owner !== undefined && isVariantsSchema(owner) && owner.discriminant === path[path.length - 1]
388
559
  ? owner
389
560
  : undefined;
@@ -394,6 +565,32 @@ function finite(domain, constraints) {
394
565
  }
395
566
  return domain.some(value => constraints.every(item => (value === item.bound) === (item.operator === "==")));
396
567
  }
568
+ // Strings lying between two bounds, nearest the lower first: the lower bound with
569
+ // the lowest code point after it ("a" to "a!" holds "a\u0000"), and the bounds'
570
+ // common prefix followed by each code point between theirs where they part ("Z"
571
+ // to "a" holds "[" and "_"), so a range holding a transformed value is found.
572
+ const BETWEEN_LIMIT = 256;
573
+ function stringsBetween(lower, upper) {
574
+ if (typeof lower !== "string" || typeof upper !== "string") {
575
+ return [];
576
+ }
577
+ const low = [...lower.normalize("NFC")];
578
+ const high = [...upper.normalize("NFC")];
579
+ let common = 0;
580
+ while (common < Math.min(low.length, high.length) && low[common] === high[common]) {
581
+ common += 1;
582
+ }
583
+ const prefix = low.slice(0, common).join("");
584
+ const from = common < low.length ? low[common].codePointAt(0) + 1 : 0;
585
+ const to = common < high.length ? high[common].codePointAt(0) : 0;
586
+ const parted = [];
587
+ for (let point = from; point < Math.min(to, from + BETWEEN_LIMIT); point += 1) {
588
+ if (point < 0xd800 || point > 0xdfff) {
589
+ parted.push(prefix + String.fromCodePoint(point));
590
+ }
591
+ }
592
+ return [`${lower}\u0000`, ...parted];
593
+ }
397
594
  function ordered(constraints, carrier) {
398
595
  let lower = carrier.floor === undefined ? undefined : { value: carrier.floor.value, inclusive: true };
399
596
  let upper = carrier.ceiling === undefined ? undefined : { value: carrier.ceiling.value, inclusive: true };
@@ -414,7 +611,9 @@ function ordered(constraints, carrier) {
414
611
  unequal.push(bound);
415
612
  }
416
613
  }
417
- const within = (value) => above(value, lower, carrier) &&
614
+ // A value nothing reads as is never held: lowercase($) == "ABC" never holds.
615
+ const within = (value) => carrier.produces?.(value) !== false &&
616
+ above(value, lower, carrier) &&
418
617
  below(value, upper, carrier) &&
419
618
  unequal.every(refused => carrier.compare(value, refused) !== 0);
420
619
  if (equal.length > 0) {
@@ -430,7 +629,23 @@ function ordered(constraints, carrier) {
430
629
  const { step } = carrier;
431
630
  if (step === undefined) {
432
631
  const order = carrier.compare(lower.value, upper.value);
433
- return order < 0 || (order === 0 && within(lower.value)) ? interval : false;
632
+ if (!(order < 0 || (order === 0 && within(lower.value)))) {
633
+ return false;
634
+ }
635
+ if (carrier.produces === undefined) {
636
+ return interval;
637
+ }
638
+ // A transformed string reads only as what its transforms give back, and a
639
+ // range may hold none of them (no lowercased string lies from "A" to "Z"):
640
+ // it is settled only where a value is found in it.
641
+ const found = [
642
+ lower.inclusive ? lower.value : undefined,
643
+ carrier.past?.(lower.value, 1),
644
+ upper.inclusive ? upper.value : undefined,
645
+ carrier.past?.(upper.value, -1),
646
+ ...stringsBetween(lower.value, upper.value),
647
+ ].some(value => value !== undefined && within(value));
648
+ return found ? interval : undefined;
434
649
  }
435
650
  let candidate = lower.inclusive ? lower.value : step(lower.value, 1);
436
651
  for (let tried = 0; tried <= unequal.length; tried += 1) {
@@ -518,14 +733,16 @@ export function witnessesOf(way, scope, inputCase, combinations = FEASIBILITY_CO
518
733
  groups.get(key).constraints.push(constraint);
519
734
  };
520
735
  for (const step of way.steps) {
521
- const placed = placedConstraintOf(step);
736
+ const placed = placedConstraintOf(step, scope);
522
737
  if (placed === undefined ||
523
738
  placed.measure !== "value" ||
524
- (placed.constraint.operator !== "==" && placed.constraint.operator !== "!=") ||
739
+ placed.constraints.some(constraint => constraint.operator !== "==" && constraint.operator !== "!=") ||
525
740
  finiteDomainAt(scope, placed.path) === undefined) {
526
741
  return undefined;
527
742
  }
528
- constrain(placed.path, placed.constraint);
743
+ for (const constraint of placed.constraints) {
744
+ constrain(placed.path, constraint);
745
+ }
529
746
  }
530
747
  if (inputCase !== undefined && groups.has(JSON.stringify([inputCase.discriminant]))) {
531
748
  constrain([inputCase.discriminant], { operator: "==", bound: inputCase.tag });
@@ -540,6 +757,25 @@ export function witnessesOf(way, scope, inputCase, combinations = FEASIBILITY_CO
540
757
  }
541
758
  })();
542
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
+ }
543
779
  function finiteDomainAt(scope, path) {
544
780
  const discriminated = sumOwning(scope, path);
545
781
  if (discriminated !== undefined) {
@@ -547,20 +783,24 @@ function finiteDomainAt(scope, path) {
547
783
  const discriminant = path[path.length - 1];
548
784
  const refusing = [...discriminated.invariants, ...inheritedAt(scope, path.slice(0, -1))]
549
785
  .flatMap(conjuncts)
550
- .filter(rule => isDeepStrictEqual(boundTermPath(rule), [discriminant]));
786
+ .filter(rule => deepEqual(boundTermPath(rule), [discriminant]));
551
787
  return discriminated.variantTags.filter(tag => refusing.every(rule => holds(rule, { [discriminant]: tag })));
552
788
  }
553
- const schema = schemaAtPath(scope, path);
789
+ const schema = schemaThrough(scope, path);
554
790
  if (schema?.kind === "boolean") {
555
791
  return admittedBy([true, false], schema, scope, path);
556
792
  }
557
793
  if (schema?.kind === "enum") {
558
794
  return admittedBy(schema.values, schema, scope, path);
559
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
+ }
560
800
  return undefined;
561
801
  }
562
- // A rule over finite positions: each comparison reads one of them against a
563
- // constant with == or !=, combined with and/or/not. Its positions, or
802
+ // A rule over finite positions: each comparison reads one of them, or what
803
+ // its transforms make of it, against a constant with == or !=, combined with and/or/not. Its positions, or
564
804
  // undefined when any part of it reads something else.
565
805
  function finitePathsOf(rule, scope, prefix) {
566
806
  switch (rule.kind) {
@@ -572,13 +812,15 @@ function finitePathsOf(rule, scope, prefix) {
572
812
  }
573
813
  const paths = sides.map(side => [...prefix, ...side.path]);
574
814
  return (rule.operator === "==" || rule.operator === "!=") &&
575
- sides.every(side => side.measure === "value") &&
815
+ sides.every(side => !counts(side.measure)) &&
576
816
  paths.every(path => finiteDomainAt(scope, path) !== undefined)
577
817
  ? paths
578
818
  : undefined;
579
819
  }
580
820
  const normalized = normalize(rule);
581
- if (normalized === undefined || normalized.measure !== "value") {
821
+ // A transformed value, `lowercase($.level)`, is read off each value the
822
+ // combinations try, as the invariant is held with `holds`.
823
+ if (normalized === undefined || counts(normalized.measure)) {
582
824
  return undefined;
583
825
  }
584
826
  if (normalized.operator !== "==" && normalized.operator !== "!=") {
@@ -605,10 +847,17 @@ function finitePathsOf(rule, scope, prefix) {
605
847
  // many" when there are more combinations to try than the limit. Unless
606
848
  // `whole`, positions no such invariant reaches are left out, and nothing is
607
849
  // tried when there are none.
608
- function jointAssignments(groups, scope, combinations, prefer = () => undefined, whole = false) {
609
- 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
+ }))];
610
856
  const constrained = groups.filter(group => group.measure === "value" && finiteDomainAt(scope, group.path) !== undefined);
611
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);
612
861
  const involved = [];
613
862
  for (let grew = true; grew;) {
614
863
  grew = false;
@@ -629,7 +878,7 @@ function jointAssignments(groups, scope, combinations, prefer = () => undefined,
629
878
  }
630
879
  const domains = positions.map(path => {
631
880
  const keeps = constrained.filter(group => JSON.stringify(group.path) === JSON.stringify(path));
632
- 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))));
633
882
  const preferred = prefer(path);
634
883
  return kept.includes(preferred) ? [preferred, ...kept.filter(value => value !== preferred)] : kept;
635
884
  });
@@ -651,8 +900,8 @@ function jointAssignments(groups, scope, combinations, prefer = () => undefined,
651
900
  return from(0, []);
652
901
  }
653
902
  // The first of the combinations jointAssignments tries, or "none".
654
- function jointAssignment(groups, scope, combinations, prefer) {
655
- 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);
656
905
  if (tried === "too many") {
657
906
  return "too many";
658
907
  }
@@ -696,7 +945,7 @@ export function keepingInvariants(scope, before, after, combinations = FEASIBILI
696
945
  const groups = related.map((path) => ({
697
946
  path,
698
947
  measure: "value",
699
- constraints: isDeepStrictEqual(readAt(before, path), readAt(after, path))
948
+ constraints: deepEqual(readAt(before, path), readAt(after, path))
700
949
  ? []
701
950
  : [{ operator: "==", bound: readAt(after, path) }],
702
951
  }));
@@ -728,7 +977,7 @@ export function keepingOrderings(scope, before, after) {
728
977
  }
729
978
  const form = differenceOf(rule);
730
979
  const parts = form.parts.map(part => ({ ...part, path: [...prefix, ...part.path] }));
731
- 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 ?? ""));
732
981
  return parts.length >= 2 && numeric && typeof form.constant === "number"
733
982
  ? [{ rule: rule, parts, constant: form.constant }]
734
983
  : [];
@@ -748,7 +997,7 @@ export function keepingOrderings(scope, before, after) {
748
997
  }
749
998
  const moving = broken.parts.find(part => Math.abs(part.coefficient) === 1 &&
750
999
  !moved.has(JSON.stringify(part.path)) &&
751
- isDeepStrictEqual(readAt(before, part.path), readAt(after, part.path)));
1000
+ deepEqual(readAt(before, part.path), readAt(after, part.path)));
752
1001
  if (moving === undefined) {
753
1002
  return undefined;
754
1003
  }
@@ -795,9 +1044,13 @@ function nearestKeeping(scope, path, operator, bound) {
795
1044
  // Whether no combination the invariants relating finite positions keep gives
796
1045
  // the position at `path` this value.
797
1046
  export function refusedJointly(scope, path, value, combinations = FEASIBILITY_COMBINATION_LIMIT) {
798
- return (finiteDomainAt(scope, path) !== undefined &&
799
- jointAssignment([{ path, measure: "value", constraints: [{ operator: "==", bound: value }] }], scope, combinations) ===
800
- "none");
1047
+ return refusedTogether(scope, [{ path, value }], combinations);
1048
+ }
1049
+ // No combination the invariants relating finite positions keep gives every
1050
+ // one of these finite positions its value.
1051
+ export function refusedTogether(scope, coordinates, combinations = FEASIBILITY_COMBINATION_LIMIT) {
1052
+ return (coordinates.every(({ path }) => finiteDomainAt(scope, path) !== undefined) &&
1053
+ jointAssignment(coordinates.map(({ path, value }) => ({ path, measure: "value", constraints: [{ operator: "==", bound: value }] })), scope, combinations) === "none");
801
1054
  }
802
1055
  // The invariants the scope holds that relate two or more finite positions,
803
1056
  // each with the positions it reads.
@@ -825,7 +1078,7 @@ export function finiteReach(decision, scope, combinations, fixed = []) {
825
1078
  }
826
1079
  const positions = [...distinctPaths([...read, ...fixed])];
827
1080
  const invariants = fieldInvariants(scope, []);
828
- const has = (path) => positions.some(other => isDeepStrictEqual(other, path));
1081
+ const has = (path) => positions.some(other => deepEqual(other, path));
829
1082
  for (let grew = true; grew;) {
830
1083
  grew = false;
831
1084
  for (const { prefix, rule } of invariants) {
@@ -902,7 +1155,10 @@ function decisionPaths(decision) {
902
1155
  return false;
903
1156
  }
904
1157
  paths.push(data.path);
905
- return data.measure === "value";
1158
+ // A transformed value is read off each value of the domain as the guard
1159
+ // reads it (`lowercase($.level)` of each case of an enum); a string
1160
+ // has no domain, so its transformed read stays out of the plan.
1161
+ return !counts(data.measure);
906
1162
  });
907
1163
  case "not":
908
1164
  return collect(rule.rule);