@toiroakr/chisel 0.0.0-stage → 0.1.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.
- package/README.md +464 -2
- package/dist/behavior.d.ts +181 -0
- package/dist/behavior.js +422 -0
- package/dist/behavior.js.map +1 -0
- package/dist/beside.d.ts +27 -0
- package/dist/beside.js +88 -0
- package/dist/beside.js.map +1 -0
- package/dist/border.d.ts +64 -0
- package/dist/border.js +426 -0
- package/dist/border.js.map +1 -0
- package/dist/cli.d.ts +5 -0
- package/dist/cli.js +567 -0
- package/dist/cli.js.map +1 -0
- package/dist/codec.d.ts +6 -0
- package/dist/codec.js +83 -0
- package/dist/codec.js.map +1 -0
- package/dist/codegen.d.ts +2 -0
- package/dist/codegen.js +65 -0
- package/dist/codegen.js.map +1 -0
- package/dist/composition.d.ts +38 -0
- package/dist/composition.js +242 -0
- package/dist/composition.js.map +1 -0
- package/dist/data.d.ts +16 -0
- package/dist/data.js +49 -0
- package/dist/data.js.map +1 -0
- package/dist/dependency.d.ts +98 -0
- package/dist/dependency.js +155 -0
- package/dist/dependency.js.map +1 -0
- package/dist/domain.d.ts +8 -0
- package/dist/domain.js +217 -0
- package/dist/domain.js.map +1 -0
- package/dist/ensures.d.ts +15 -0
- package/dist/ensures.js +88 -0
- package/dist/ensures.js.map +1 -0
- package/dist/exact.d.ts +21 -0
- package/dist/exact.js +80 -0
- package/dist/exact.js.map +1 -0
- package/dist/feasibility.d.ts +61 -0
- package/dist/feasibility.js +984 -0
- package/dist/feasibility.js.map +1 -0
- package/dist/guard-borders.d.ts +50 -0
- package/dist/guard-borders.js +809 -0
- package/dist/guard-borders.js.map +1 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/interval.d.ts +3 -0
- package/dist/interval.js +161 -0
- package/dist/interval.js.map +1 -0
- package/dist/isolated.d.ts +9 -0
- package/dist/isolated.js +41 -0
- package/dist/isolated.js.map +1 -0
- package/dist/model.d.ts +95 -0
- package/dist/model.js +343 -0
- package/dist/model.js.map +1 -0
- package/dist/pairs.d.ts +26 -0
- package/dist/pairs.js +107 -0
- package/dist/pairs.js.map +1 -0
- package/dist/partition.d.ts +44 -0
- package/dist/partition.js +275 -0
- package/dist/partition.js.map +1 -0
- package/dist/proof.d.ts +15 -0
- package/dist/proof.js +482 -0
- package/dist/proof.js.map +1 -0
- package/dist/report-json.d.ts +11 -0
- package/dist/report-json.js +94 -0
- package/dist/report-json.js.map +1 -0
- package/dist/rule.d.ts +146 -0
- package/dist/rule.js +599 -0
- package/dist/rule.js.map +1 -0
- package/dist/schema.d.ts +163 -0
- package/dist/schema.js +524 -0
- package/dist/schema.js.map +1 -0
- package/dist/specification.d.ts +256 -0
- package/dist/specification.js +1583 -0
- package/dist/specification.js.map +1 -0
- package/dist/text.d.ts +1 -0
- package/dist/text.js +11 -0
- package/dist/text.js.map +1 -0
- package/dist/ways.d.ts +25 -0
- package/dist/ways.js +95 -0
- package/dist/ways.js.map +1 -0
- package/package.json +53 -4
- package/schema/report.schema.json +1062 -0
|
@@ -0,0 +1,984 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import { integerCarrier, normalize, numberCarrier } from "./border.js";
|
|
3
|
+
import { inheritedAt } from "./guard-borders.js";
|
|
4
|
+
import { carrierOf } from "./partition.js";
|
|
5
|
+
import { DEPS, boundTermPath, conjuncts, describeRule, differenceOf, holds, isTerm, positionData, positionOf, readOperand, termData, termPaths } from "./rule.js";
|
|
6
|
+
import { isVariantsSchema, schemaAtPath } from "./schema.js";
|
|
7
|
+
const negated = {
|
|
8
|
+
"<": ">=",
|
|
9
|
+
"<=": ">",
|
|
10
|
+
">": "<=",
|
|
11
|
+
">=": "<",
|
|
12
|
+
"==": "!=",
|
|
13
|
+
"!=": "==",
|
|
14
|
+
};
|
|
15
|
+
const mirrored = {
|
|
16
|
+
"<": ">",
|
|
17
|
+
"<=": ">=",
|
|
18
|
+
">": "<",
|
|
19
|
+
">=": "<=",
|
|
20
|
+
"==": "==",
|
|
21
|
+
"!=": "!=",
|
|
22
|
+
};
|
|
23
|
+
export const contradicts = "ガードの条件がこの値について両立しない";
|
|
24
|
+
export const unreadable = "読めない条件が同じ値を読む別の条件と重なる";
|
|
25
|
+
export const unreached = "どの値の組もこの分岐を通らない";
|
|
26
|
+
// How many combinations of finite values feasibility tries against the
|
|
27
|
+
// invariants relating them, before leaving a way or a point undecided.
|
|
28
|
+
export const FEASIBILITY_COMBINATION_LIMIT = 4096;
|
|
29
|
+
export function tooManyWays(limit) {
|
|
30
|
+
return `道筋が上限の${limit}本を超える`;
|
|
31
|
+
}
|
|
32
|
+
export function tooManyCombinations(limit) {
|
|
33
|
+
return `不変条件とあわせて調べる値の組が上限の${limit}通りを超える`;
|
|
34
|
+
}
|
|
35
|
+
export function feasibilityOf(way, scope, placement, combinations = FEASIBILITY_COMBINATION_LIMIT) {
|
|
36
|
+
const groups = new Map();
|
|
37
|
+
const relations = [];
|
|
38
|
+
const opaque = [];
|
|
39
|
+
const outcomes = new Map();
|
|
40
|
+
const groupFor = (path, measure) => {
|
|
41
|
+
const key = JSON.stringify([path, measure]);
|
|
42
|
+
if (!groups.has(key)) {
|
|
43
|
+
groups.set(key, { path, measure, constraints: [] });
|
|
44
|
+
}
|
|
45
|
+
return key;
|
|
46
|
+
};
|
|
47
|
+
const forms = new Map();
|
|
48
|
+
let constant = true;
|
|
49
|
+
const constrain = (weighed, operator, bound, onlyKnown = false) => {
|
|
50
|
+
const merged = new Map();
|
|
51
|
+
for (const { key, coefficient } of weighed) {
|
|
52
|
+
merged.set(key, (merged.get(key) ?? 0) + coefficient);
|
|
53
|
+
}
|
|
54
|
+
const parts = [...merged]
|
|
55
|
+
.filter(([, coefficient]) => coefficient !== 0)
|
|
56
|
+
.sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
|
|
57
|
+
.map(([key, coefficient]) => ({ key, coefficient }));
|
|
58
|
+
if (parts.length === 0) {
|
|
59
|
+
constant &&= holds({ kind: "compare", left: 0, operator, right: bound }, undefined);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const sign = parts[0].coefficient < 0 ? -1 : 1;
|
|
63
|
+
const canonical = parts.map(part => ({ key: part.key, coefficient: sign * part.coefficient }));
|
|
64
|
+
const key = JSON.stringify(canonical);
|
|
65
|
+
if (onlyKnown && !forms.has(key)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (!forms.has(key)) {
|
|
69
|
+
forms.set(key, { parts: canonical, constraints: [] });
|
|
70
|
+
}
|
|
71
|
+
forms
|
|
72
|
+
.get(key)
|
|
73
|
+
.constraints.push(sign === 1 ? { operator, bound } : { operator: mirrored[operator], bound: -bound });
|
|
74
|
+
};
|
|
75
|
+
const differ = (left, right, operator, bound) => constrain([
|
|
76
|
+
{ key: left, coefficient: 1 },
|
|
77
|
+
{ key: right, coefficient: -1 },
|
|
78
|
+
], operator, bound);
|
|
79
|
+
const constrainForm = (form, operator, bound, onlyKnown = false) => constrain(form.parts.map(part => ({ key: groupFor(part.path, part.measure), coefficient: part.coefficient })), operator, bound - form.constant, onlyKnown);
|
|
80
|
+
if (placement !== undefined && "between" in placement) {
|
|
81
|
+
const [left, right] = placement.between;
|
|
82
|
+
differ(groupFor(left.path, left.measure), groupFor(right.path, right.measure), placement.operator, placement.bound);
|
|
83
|
+
}
|
|
84
|
+
else if (placement !== undefined && "form" in placement) {
|
|
85
|
+
constrainForm(placement.form, placement.operator, placement.bound);
|
|
86
|
+
}
|
|
87
|
+
else if (placement !== undefined) {
|
|
88
|
+
groups
|
|
89
|
+
.get(groupFor(placement.path, placement.measure))
|
|
90
|
+
.constraints.push({ operator: placement.operator, bound: placement.bound });
|
|
91
|
+
}
|
|
92
|
+
for (const step of way.steps) {
|
|
93
|
+
const identity = stepIdentity(step);
|
|
94
|
+
const key = step.distinction.kind === "match" ? JSON.stringify(["match", positionData(step.distinction.on)?.path]) : describeRule(step.distinction);
|
|
95
|
+
const bucket = outcomes.get(key) ?? [];
|
|
96
|
+
const seen = bucket.find(previous => isDeepStrictEqual(previous.identity, identity));
|
|
97
|
+
if (seen !== undefined) {
|
|
98
|
+
if (seen.outcome !== step.outcome) {
|
|
99
|
+
return { kind: "infeasible", reason: contradicts };
|
|
100
|
+
}
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
bucket.push({ identity, outcome: step.outcome });
|
|
104
|
+
outcomes.set(key, bucket);
|
|
105
|
+
const placed = placedConstraintOf(step);
|
|
106
|
+
if (placed !== undefined) {
|
|
107
|
+
groups.get(groupFor(placed.path, placed.measure)).constraints.push(placed.constraint);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const { distinction, outcome } = step;
|
|
111
|
+
if (distinction.kind === "all" || distinction.kind === "any") {
|
|
112
|
+
opaque.push(termPaths(distinction));
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (distinction.kind !== "compare") {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const operator = outcome === true ? distinction.operator : negated[distinction.operator];
|
|
119
|
+
const form = readableFormOf(distinction, scope);
|
|
120
|
+
if (form !== undefined) {
|
|
121
|
+
constrainForm(form, operator, 0);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const relation = relationOf(distinction, operator, groupFor);
|
|
125
|
+
if (relation === undefined) {
|
|
126
|
+
opaque.push(termPaths(distinction));
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
relations.push(relation);
|
|
130
|
+
differ(relation.left, relation.right, relation.operator, 0);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// An input invariant ordering numbers holds on every way, so it bounds a form
|
|
134
|
+
// the way constrains. Not added to a form of its own: one more form sharing
|
|
135
|
+
// the way's positions would leave forms that are settled now undecided.
|
|
136
|
+
for (const { prefix, rule } of scopedInvariants(scope, [])) {
|
|
137
|
+
const form = numericFormOf(rule, scope, prefix);
|
|
138
|
+
if (form !== undefined && form.parts.every(part => groups.has(JSON.stringify([part.path, part.measure])))) {
|
|
139
|
+
constrainForm(form, rule.operator, 0, true);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const intervals = new Map();
|
|
143
|
+
let unsettled = false;
|
|
144
|
+
for (const [key, group] of groups) {
|
|
145
|
+
const settled = settle(group, scope);
|
|
146
|
+
if (settled === false) {
|
|
147
|
+
return { kind: "infeasible", reason: contradicts };
|
|
148
|
+
}
|
|
149
|
+
intervals.set(key, typeof settled === "object" ? settled : undefined);
|
|
150
|
+
unsettled ||= settled === undefined && group.constraints.length > 1;
|
|
151
|
+
}
|
|
152
|
+
const joint = jointAssignment([...groups.values()], scope, combinations);
|
|
153
|
+
if (joint === "none") {
|
|
154
|
+
return { kind: "infeasible", reason: contradicts };
|
|
155
|
+
}
|
|
156
|
+
if (joint === "too many") {
|
|
157
|
+
return { kind: "undecided", reason: tooManyCombinations(combinations) };
|
|
158
|
+
}
|
|
159
|
+
if (!constant) {
|
|
160
|
+
return { kind: "infeasible", reason: contradicts };
|
|
161
|
+
}
|
|
162
|
+
const related = [...forms.values()].flatMap(form => form.parts.map(part => part.key));
|
|
163
|
+
for (const form of forms.values()) {
|
|
164
|
+
const carrier = differenceCarrierOf(form.parts.map(part => groups.get(part.key)), scope);
|
|
165
|
+
if (carrier === undefined) {
|
|
166
|
+
unsettled ||= form.constraints.length > 1;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (ordered(form.constraints, carrier) === false) {
|
|
170
|
+
return { kind: "infeasible", reason: contradicts };
|
|
171
|
+
}
|
|
172
|
+
const alone = form.parts.every(part => related.filter(other => other === part.key).length === 1 && intervals.get(part.key) !== undefined);
|
|
173
|
+
// A pair shared with another form is still settled on its intervals below,
|
|
174
|
+
// as a relation, so only one it alone constrains is read on its range here.
|
|
175
|
+
if (isPair(form)) {
|
|
176
|
+
if (alone && ordered([...form.constraints, ...rangeOf(form, intervals, carrier)], carrier) === false) {
|
|
177
|
+
return { kind: "infeasible", reason: contradicts };
|
|
178
|
+
}
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const free = form.parts.every(part => intervals.get(part.key)?.lower === undefined && intervals.get(part.key)?.upper === undefined);
|
|
182
|
+
if (alone && free) {
|
|
183
|
+
unsettled ||= carrier === integerCarrier && !form.parts.some(part => Math.abs(part.coefficient) === 1);
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
// Integers weighed by more than one leave gaps in the values their sum takes,
|
|
187
|
+
// so only a sum of whole steps is read as the one interval it fills.
|
|
188
|
+
const range = alone && (carrier !== integerCarrier || form.parts.every(part => Math.abs(part.coefficient) === 1))
|
|
189
|
+
? rangeOf(form, intervals, carrier)
|
|
190
|
+
: undefined;
|
|
191
|
+
if (range === undefined) {
|
|
192
|
+
unsettled = true;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (ordered([...form.constraints, ...range], carrier) === false) {
|
|
196
|
+
return { kind: "infeasible", reason: contradicts };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
for (const relation of relations) {
|
|
200
|
+
const left = intervals.get(relation.left);
|
|
201
|
+
const right = intervals.get(relation.right);
|
|
202
|
+
const shared = [relation.left, relation.right].some(key => related.filter(other => other === key).length > 1);
|
|
203
|
+
if (left === undefined || right === undefined || shared) {
|
|
204
|
+
unsettled = true;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (!admitsRelation(relation.operator, left, right)) {
|
|
208
|
+
return { kind: "infeasible", reason: contradicts };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const readPaths = [...groups.values()].map(group => group.path);
|
|
212
|
+
const overlaps = opaque.some((paths, index) => paths.some(path => [...readPaths, ...opaque.filter((_, other) => other !== index).flat()].some(other => sharesValue(path, other))));
|
|
213
|
+
return overlaps || unsettled
|
|
214
|
+
? { kind: "undecided", reason: unreadable }
|
|
215
|
+
: { kind: "feasible" };
|
|
216
|
+
}
|
|
217
|
+
// The constraint a step puts on one coordinate: a match, or a comparison with a constant.
|
|
218
|
+
function placedConstraintOf(step) {
|
|
219
|
+
const { distinction, outcome } = step;
|
|
220
|
+
if (distinction.kind === "match") {
|
|
221
|
+
return { path: positionOf(distinction.on).path, measure: "value", constraint: { operator: "==", bound: outcome } };
|
|
222
|
+
}
|
|
223
|
+
if (distinction.kind !== "compare") {
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
const normalized = normalize(distinction);
|
|
227
|
+
if (normalized === undefined) {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
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
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function stepIdentity(step) {
|
|
241
|
+
return step.distinction.kind === "match"
|
|
242
|
+
? ["match", termData(step.distinction.on)]
|
|
243
|
+
: ruleIdentity(step.distinction);
|
|
244
|
+
}
|
|
245
|
+
function ruleIdentity(rule) {
|
|
246
|
+
const operand = (value) => isTerm(value) ? ["term", termData(value)] : ["constant", value];
|
|
247
|
+
switch (rule.kind) {
|
|
248
|
+
case "compare": return [rule.kind, rule.operator, operand(rule.left), operand(rule.right)];
|
|
249
|
+
case "all":
|
|
250
|
+
case "any": return [rule.kind, operand(rule.of), rule.element, ruleIdentity(rule.each)];
|
|
251
|
+
case "not": return [rule.kind, ruleIdentity(rule.rule)];
|
|
252
|
+
case "and":
|
|
253
|
+
case "or": return [rule.kind, rule.rules.map(ruleIdentity)];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// The values a form takes as each of its parts ranges over its own interval,
|
|
257
|
+
// as the constraints that bound it.
|
|
258
|
+
function rangeOf(form, intervals, carrier) {
|
|
259
|
+
const whole = (edge, direction) => edge === undefined || edge.inclusive || carrier !== integerCarrier
|
|
260
|
+
? edge
|
|
261
|
+
: { value: edge.value + direction, inclusive: true };
|
|
262
|
+
const end = (side) => form.parts.reduce((total, { key, coefficient }) => {
|
|
263
|
+
const interval = intervals.get(key);
|
|
264
|
+
const taken = (coefficient > 0) === (side === "lower") ? "lower" : "upper";
|
|
265
|
+
const edge = whole(interval[taken], taken === "lower" ? 1 : -1);
|
|
266
|
+
return total === undefined || edge === undefined
|
|
267
|
+
? undefined
|
|
268
|
+
: {
|
|
269
|
+
value: total.value + coefficient * edge.value,
|
|
270
|
+
inclusive: total.inclusive && edge.inclusive,
|
|
271
|
+
};
|
|
272
|
+
}, { value: 0, inclusive: true });
|
|
273
|
+
const lower = end("lower");
|
|
274
|
+
const upper = end("upper");
|
|
275
|
+
return [
|
|
276
|
+
...(lower === undefined ? [] : [{ operator: lower.inclusive ? ">=" : ">", bound: lower.value }]),
|
|
277
|
+
...(upper === undefined ? [] : [{ operator: upper.inclusive ? "<=" : "<", bound: upper.value }]),
|
|
278
|
+
];
|
|
279
|
+
}
|
|
280
|
+
function isPair(form) {
|
|
281
|
+
return form.parts.length === 2 && form.parts[0].coefficient === 1 && form.parts[1].coefficient === -1;
|
|
282
|
+
}
|
|
283
|
+
// An invariant comparing two or more integer or number positions that are never
|
|
284
|
+
// left out, read as one form over paths from the scope's root.
|
|
285
|
+
function numericFormOf(rule, scope, prefix) {
|
|
286
|
+
if (rule.kind !== "compare" ||
|
|
287
|
+
rule.operator === "==" ||
|
|
288
|
+
rule.operator === "!=" ||
|
|
289
|
+
![rule.left, rule.right].every(operand => isTerm(operand) || typeof operand === "number")) {
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
const form = differenceOf(rule);
|
|
293
|
+
const parts = form.parts.map(part => ({ ...part, path: [...prefix, ...part.path] }));
|
|
294
|
+
const kept = parts.every(part => part.measure === "value" &&
|
|
295
|
+
!leftOutAlong(scope, part.path) &&
|
|
296
|
+
["integer", "number"].includes(schemaAtPath(scope, part.path)?.kind ?? ""));
|
|
297
|
+
return parts.length >= 2 && kept && typeof form.constant === "number" ? { ...form, parts } : undefined;
|
|
298
|
+
}
|
|
299
|
+
function leftOutAlong(scope, path) {
|
|
300
|
+
return path.some((_, index) => {
|
|
301
|
+
const parent = schemaAtPath(scope, path.slice(0, index));
|
|
302
|
+
const field = parent?.kind === "object" ? parent.shape[path[index]] : undefined;
|
|
303
|
+
return field?.kind === "optional";
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
// An expression of integer or number positions, read as the left side less the
|
|
307
|
+
// right; a decimal or another kind of part leaves the comparison unread.
|
|
308
|
+
function readableFormOf(rule, scope) {
|
|
309
|
+
if (![rule.left, rule.right].some(operand => isTerm(operand) && positionData(operand) === undefined)) {
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
const form = differenceOf(rule);
|
|
313
|
+
const kinds = form.parts.map(part => part.measure === "length" ? "integer" : schemaAtPath(scope, part.path)?.kind);
|
|
314
|
+
// A part that may be left out is not one: the comparison holds without it.
|
|
315
|
+
return typeof form.constant === "number" &&
|
|
316
|
+
kinds.every(kind => kind === "integer" || kind === "number") &&
|
|
317
|
+
!form.parts.some(part => leftOutAlong(scope, part.path))
|
|
318
|
+
? form
|
|
319
|
+
: undefined;
|
|
320
|
+
}
|
|
321
|
+
function relationOf(rule, operator, groupFor) {
|
|
322
|
+
if (!isTerm(rule.left) || !isTerm(rule.right)) {
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
const left = positionData(rule.left);
|
|
326
|
+
const right = positionData(rule.right);
|
|
327
|
+
if (left === undefined || right === undefined) {
|
|
328
|
+
return undefined;
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
left: groupFor(left.path, left.measure),
|
|
332
|
+
right: groupFor(right.path, right.measure),
|
|
333
|
+
operator,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
// Only a difference counted in whole steps or in plain numbers is settled here;
|
|
337
|
+
// two decimals or two instants differ in units their readers convert to, which
|
|
338
|
+
// the steps of a way do not carry.
|
|
339
|
+
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")) {
|
|
342
|
+
return integerCarrier;
|
|
343
|
+
}
|
|
344
|
+
return kinds.every(kind => kind === "integer" || kind === "number") ? numberCarrier : undefined;
|
|
345
|
+
}
|
|
346
|
+
function sharesValue(left, right) {
|
|
347
|
+
const length = Math.min(left.length, right.length);
|
|
348
|
+
return left.slice(0, length).every((key, index) => key === right[index]);
|
|
349
|
+
}
|
|
350
|
+
function settle(group, scope) {
|
|
351
|
+
const { path, measure } = group;
|
|
352
|
+
const domain = measure === "value" ? finiteDomainAt(scope, path) : undefined;
|
|
353
|
+
if (domain !== undefined) {
|
|
354
|
+
return finite(domain, group.constraints);
|
|
355
|
+
}
|
|
356
|
+
const schema = schemaAtPath(scope, path);
|
|
357
|
+
if (schema === undefined) {
|
|
358
|
+
return undefined;
|
|
359
|
+
}
|
|
360
|
+
const carrier = carrierOf(schema, measure);
|
|
361
|
+
if (carrier === undefined) {
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
const invariants = [...schema.invariants, ...inheritedAt(scope, path)]
|
|
365
|
+
.flatMap(conjuncts)
|
|
366
|
+
.flatMap((rule) => {
|
|
367
|
+
if (boundTermPath(rule)?.length !== 0) {
|
|
368
|
+
return [];
|
|
369
|
+
}
|
|
370
|
+
const normalized = normalize(rule);
|
|
371
|
+
return normalized === undefined || normalized.measure !== measure
|
|
372
|
+
? []
|
|
373
|
+
: [{ operator: normalized.operator, bound: normalized.bound }];
|
|
374
|
+
});
|
|
375
|
+
return ordered([...group.constraints, ...invariants], carrier);
|
|
376
|
+
}
|
|
377
|
+
// The values of a finite domain the position's own invariants keep, the way its
|
|
378
|
+
// classes leave out the ones an invariant refuses.
|
|
379
|
+
function admittedBy(domain, schema, scope, path) {
|
|
380
|
+
const own = [...schema.invariants, ...inheritedAt(scope, path)]
|
|
381
|
+
.flatMap(conjuncts)
|
|
382
|
+
.filter(rule => boundTermPath(rule)?.length === 0);
|
|
383
|
+
return domain.filter(value => own.every(rule => holds(rule, value)));
|
|
384
|
+
}
|
|
385
|
+
function sumOwning(scope, path) {
|
|
386
|
+
const owner = schemaAtPath(scope, path.slice(0, -1));
|
|
387
|
+
return owner !== undefined && isVariantsSchema(owner) && owner.discriminant === path[path.length - 1]
|
|
388
|
+
? owner
|
|
389
|
+
: undefined;
|
|
390
|
+
}
|
|
391
|
+
function finite(domain, constraints) {
|
|
392
|
+
if (constraints.some(item => item.operator !== "==" && item.operator !== "!=")) {
|
|
393
|
+
return undefined;
|
|
394
|
+
}
|
|
395
|
+
return domain.some(value => constraints.every(item => (value === item.bound) === (item.operator === "==")));
|
|
396
|
+
}
|
|
397
|
+
function ordered(constraints, carrier) {
|
|
398
|
+
let lower = carrier.floor === undefined ? undefined : { value: carrier.floor.value, inclusive: true };
|
|
399
|
+
let upper = carrier.ceiling === undefined ? undefined : { value: carrier.ceiling.value, inclusive: true };
|
|
400
|
+
const equal = [];
|
|
401
|
+
const unequal = [];
|
|
402
|
+
for (const { operator, bound } of constraints) {
|
|
403
|
+
const edge = { value: bound, inclusive: operator === ">=" || operator === "<=" };
|
|
404
|
+
if (operator === ">" || operator === ">=") {
|
|
405
|
+
lower = tighter(lower, edge, 1, carrier);
|
|
406
|
+
}
|
|
407
|
+
else if (operator === "<" || operator === "<=") {
|
|
408
|
+
upper = tighter(upper, edge, -1, carrier);
|
|
409
|
+
}
|
|
410
|
+
else if (operator === "==") {
|
|
411
|
+
equal.push(bound);
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
unequal.push(bound);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const within = (value) => above(value, lower, carrier) &&
|
|
418
|
+
below(value, upper, carrier) &&
|
|
419
|
+
unequal.every(refused => carrier.compare(value, refused) !== 0);
|
|
420
|
+
if (equal.length > 0) {
|
|
421
|
+
const [value] = equal;
|
|
422
|
+
return equal.every(other => carrier.compare(other, value) === 0) && within(value)
|
|
423
|
+
? { carrier, lower: { value, inclusive: true }, upper: { value, inclusive: true } }
|
|
424
|
+
: false;
|
|
425
|
+
}
|
|
426
|
+
const interval = { carrier, lower, upper };
|
|
427
|
+
if (lower === undefined || upper === undefined) {
|
|
428
|
+
return interval;
|
|
429
|
+
}
|
|
430
|
+
const { step } = carrier;
|
|
431
|
+
if (step === undefined) {
|
|
432
|
+
const order = carrier.compare(lower.value, upper.value);
|
|
433
|
+
return order < 0 || (order === 0 && within(lower.value)) ? interval : false;
|
|
434
|
+
}
|
|
435
|
+
let candidate = lower.inclusive ? lower.value : step(lower.value, 1);
|
|
436
|
+
for (let tried = 0; tried <= unequal.length; tried += 1) {
|
|
437
|
+
if (candidate === undefined || !below(candidate, upper, carrier)) {
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
if (within(candidate)) {
|
|
441
|
+
return interval;
|
|
442
|
+
}
|
|
443
|
+
candidate = step(candidate, 1);
|
|
444
|
+
}
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
function admitsRelation(operator, left, right) {
|
|
448
|
+
const { carrier } = left;
|
|
449
|
+
switch (operator) {
|
|
450
|
+
case "==":
|
|
451
|
+
return reaches(left.lower, right.upper, carrier) && reaches(right.lower, left.upper, carrier);
|
|
452
|
+
case "!=":
|
|
453
|
+
return !(single(left) &&
|
|
454
|
+
single(right) &&
|
|
455
|
+
carrier.compare(left.lower.value, right.lower.value) === 0);
|
|
456
|
+
case "<":
|
|
457
|
+
return strictlyReaches(left.lower, right.upper, carrier);
|
|
458
|
+
case "<=":
|
|
459
|
+
return reaches(left.lower, right.upper, carrier);
|
|
460
|
+
case ">":
|
|
461
|
+
case ">=":
|
|
462
|
+
return admitsRelation(mirrored[operator], right, left);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
function reaches(low, high, carrier) {
|
|
466
|
+
if (low === undefined || high === undefined) {
|
|
467
|
+
return true;
|
|
468
|
+
}
|
|
469
|
+
const order = carrier.compare(low.value, high.value);
|
|
470
|
+
return order < 0 || (order === 0 && low.inclusive && high.inclusive);
|
|
471
|
+
}
|
|
472
|
+
function strictlyReaches(low, high, carrier) {
|
|
473
|
+
return low === undefined || high === undefined || carrier.compare(low.value, high.value) < 0;
|
|
474
|
+
}
|
|
475
|
+
function single(interval) {
|
|
476
|
+
return (interval.lower !== undefined &&
|
|
477
|
+
interval.upper !== undefined &&
|
|
478
|
+
interval.carrier.compare(interval.lower.value, interval.upper.value) === 0);
|
|
479
|
+
}
|
|
480
|
+
function above(value, lower, carrier) {
|
|
481
|
+
if (lower === undefined) {
|
|
482
|
+
return true;
|
|
483
|
+
}
|
|
484
|
+
const order = carrier.compare(value, lower.value);
|
|
485
|
+
return order > 0 || (lower.inclusive && order === 0);
|
|
486
|
+
}
|
|
487
|
+
function below(value, upper, carrier) {
|
|
488
|
+
if (upper === undefined) {
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
const order = carrier.compare(value, upper.value);
|
|
492
|
+
return order < 0 || (upper.inclusive && order === 0);
|
|
493
|
+
}
|
|
494
|
+
function tighter(current, next, direction, carrier) {
|
|
495
|
+
if (current === undefined) {
|
|
496
|
+
return next;
|
|
497
|
+
}
|
|
498
|
+
const order = carrier.compare(next.value, current.value) * direction;
|
|
499
|
+
if (order > 0) {
|
|
500
|
+
return next;
|
|
501
|
+
}
|
|
502
|
+
if (order < 0) {
|
|
503
|
+
return current;
|
|
504
|
+
}
|
|
505
|
+
return current.inclusive ? next : current;
|
|
506
|
+
}
|
|
507
|
+
// The combinations of values a way's finite positions can take together with
|
|
508
|
+
// the finite positions an invariant relates them to, keeping those
|
|
509
|
+
// invariants; undefined when a step reads anything else or there are more
|
|
510
|
+
// combinations to try than the limit.
|
|
511
|
+
export function witnessesOf(way, scope, inputCase, combinations = FEASIBILITY_COMBINATION_LIMIT) {
|
|
512
|
+
const groups = new Map();
|
|
513
|
+
const constrain = (path, constraint) => {
|
|
514
|
+
const key = JSON.stringify(path);
|
|
515
|
+
if (!groups.has(key)) {
|
|
516
|
+
groups.set(key, { path, measure: "value", constraints: [] });
|
|
517
|
+
}
|
|
518
|
+
groups.get(key).constraints.push(constraint);
|
|
519
|
+
};
|
|
520
|
+
for (const step of way.steps) {
|
|
521
|
+
const placed = placedConstraintOf(step);
|
|
522
|
+
if (placed === undefined ||
|
|
523
|
+
placed.measure !== "value" ||
|
|
524
|
+
(placed.constraint.operator !== "==" && placed.constraint.operator !== "!=") ||
|
|
525
|
+
finiteDomainAt(scope, placed.path) === undefined) {
|
|
526
|
+
return undefined;
|
|
527
|
+
}
|
|
528
|
+
constrain(placed.path, placed.constraint);
|
|
529
|
+
}
|
|
530
|
+
if (inputCase !== undefined && groups.has(JSON.stringify([inputCase.discriminant]))) {
|
|
531
|
+
constrain([inputCase.discriminant], { operator: "==", bound: inputCase.tag });
|
|
532
|
+
}
|
|
533
|
+
const tried = jointAssignments([...groups.values()], scope, combinations, undefined, true);
|
|
534
|
+
if (tried === "too many") {
|
|
535
|
+
return undefined;
|
|
536
|
+
}
|
|
537
|
+
return (function* () {
|
|
538
|
+
for (const assignment of tried) {
|
|
539
|
+
yield [...assignment].map(([key, value]) => ({ path: JSON.parse(key), value }));
|
|
540
|
+
}
|
|
541
|
+
})();
|
|
542
|
+
}
|
|
543
|
+
function finiteDomainAt(scope, path) {
|
|
544
|
+
const discriminated = sumOwning(scope, path);
|
|
545
|
+
if (discriminated !== undefined) {
|
|
546
|
+
// A case the sum's invariants refuse is no value the discriminant can take.
|
|
547
|
+
const discriminant = path[path.length - 1];
|
|
548
|
+
const refusing = [...discriminated.invariants, ...inheritedAt(scope, path.slice(0, -1))]
|
|
549
|
+
.flatMap(conjuncts)
|
|
550
|
+
.filter(rule => isDeepStrictEqual(boundTermPath(rule), [discriminant]));
|
|
551
|
+
return discriminated.variantTags.filter(tag => refusing.every(rule => holds(rule, { [discriminant]: tag })));
|
|
552
|
+
}
|
|
553
|
+
const schema = schemaAtPath(scope, path);
|
|
554
|
+
if (schema?.kind === "boolean") {
|
|
555
|
+
return admittedBy([true, false], schema, scope, path);
|
|
556
|
+
}
|
|
557
|
+
if (schema?.kind === "enum") {
|
|
558
|
+
return admittedBy(schema.values, schema, scope, path);
|
|
559
|
+
}
|
|
560
|
+
return undefined;
|
|
561
|
+
}
|
|
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
|
|
564
|
+
// undefined when any part of it reads something else.
|
|
565
|
+
function finitePathsOf(rule, scope, prefix) {
|
|
566
|
+
switch (rule.kind) {
|
|
567
|
+
case "compare": {
|
|
568
|
+
if (isTerm(rule.left) && isTerm(rule.right)) {
|
|
569
|
+
const sides = [rule.left, rule.right].map(side => positionData(side));
|
|
570
|
+
if (sides.some(side => side === undefined)) {
|
|
571
|
+
return undefined;
|
|
572
|
+
}
|
|
573
|
+
const paths = sides.map(side => [...prefix, ...side.path]);
|
|
574
|
+
return (rule.operator === "==" || rule.operator === "!=") &&
|
|
575
|
+
sides.every(side => side.measure === "value") &&
|
|
576
|
+
paths.every(path => finiteDomainAt(scope, path) !== undefined)
|
|
577
|
+
? paths
|
|
578
|
+
: undefined;
|
|
579
|
+
}
|
|
580
|
+
const normalized = normalize(rule);
|
|
581
|
+
if (normalized === undefined || normalized.measure !== "value") {
|
|
582
|
+
return undefined;
|
|
583
|
+
}
|
|
584
|
+
if (normalized.operator !== "==" && normalized.operator !== "!=") {
|
|
585
|
+
return undefined;
|
|
586
|
+
}
|
|
587
|
+
const term = (isTerm(rule.left) ? rule.left : rule.right);
|
|
588
|
+
const path = [...prefix, ...positionOf(term).path];
|
|
589
|
+
return finiteDomainAt(scope, path) === undefined ? undefined : [path];
|
|
590
|
+
}
|
|
591
|
+
case "not":
|
|
592
|
+
return finitePathsOf(rule.rule, scope, prefix);
|
|
593
|
+
case "and":
|
|
594
|
+
case "or": {
|
|
595
|
+
const parts = rule.rules.map(part => finitePathsOf(part, scope, prefix));
|
|
596
|
+
return parts.some(part => part === undefined) ? undefined : parts.flat();
|
|
597
|
+
}
|
|
598
|
+
default:
|
|
599
|
+
return undefined;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
// The combinations of values for the finite positions the groups constrain,
|
|
603
|
+
// together with every finite position an invariant relating two or more of
|
|
604
|
+
// them reaches, that keep those invariants, in the order of `prefer`; "too
|
|
605
|
+
// many" when there are more combinations to try than the limit. Unless
|
|
606
|
+
// `whole`, positions no such invariant reaches are left out, and nothing is
|
|
607
|
+
// tried when there are none.
|
|
608
|
+
function jointAssignments(groups, scope, combinations, prefer = () => undefined, whole = false) {
|
|
609
|
+
const joint = jointInvariantsOf(scope);
|
|
610
|
+
const constrained = groups.filter(group => group.measure === "value" && finiteDomainAt(scope, group.path) !== undefined);
|
|
611
|
+
const component = new Map(constrained.map(group => [JSON.stringify(group.path), group.path]));
|
|
612
|
+
const involved = [];
|
|
613
|
+
for (let grew = true; grew;) {
|
|
614
|
+
grew = false;
|
|
615
|
+
for (const invariant of joint) {
|
|
616
|
+
const { paths } = invariant;
|
|
617
|
+
if (!involved.includes(invariant) && paths.some(path => component.has(JSON.stringify(path)))) {
|
|
618
|
+
involved.push(invariant);
|
|
619
|
+
for (const path of paths) {
|
|
620
|
+
component.set(JSON.stringify(path), path);
|
|
621
|
+
}
|
|
622
|
+
grew = true;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
const positions = [...component.values()];
|
|
627
|
+
if (involved.length === 0 && !whole) {
|
|
628
|
+
return [new Map()];
|
|
629
|
+
}
|
|
630
|
+
const domains = positions.map(path => {
|
|
631
|
+
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 === "=="))));
|
|
633
|
+
const preferred = prefer(path);
|
|
634
|
+
return kept.includes(preferred) ? [preferred, ...kept.filter(value => value !== preferred)] : kept;
|
|
635
|
+
});
|
|
636
|
+
if (domains.reduce((product, domain) => product * domain.length, 1) > combinations) {
|
|
637
|
+
return "too many";
|
|
638
|
+
}
|
|
639
|
+
function* from(index, chosen) {
|
|
640
|
+
if (index === positions.length) {
|
|
641
|
+
const value = positions.reduce((built, path, at) => writeAt(built, path, chosen[at]), {});
|
|
642
|
+
if (involved.every(({ prefix, rule }) => holds(rule, readAt(value, prefix)))) {
|
|
643
|
+
yield new Map(positions.map((path, at) => [JSON.stringify(path), chosen[at]]));
|
|
644
|
+
}
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
for (const candidate of domains[index]) {
|
|
648
|
+
yield* from(index + 1, [...chosen, candidate]);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
return from(0, []);
|
|
652
|
+
}
|
|
653
|
+
// The first of the combinations jointAssignments tries, or "none".
|
|
654
|
+
function jointAssignment(groups, scope, combinations, prefer) {
|
|
655
|
+
const tried = jointAssignments(groups, scope, combinations, prefer);
|
|
656
|
+
if (tried === "too many") {
|
|
657
|
+
return "too many";
|
|
658
|
+
}
|
|
659
|
+
for (const first of tried) {
|
|
660
|
+
return first;
|
|
661
|
+
}
|
|
662
|
+
return "none";
|
|
663
|
+
}
|
|
664
|
+
function writeAt(value, path, leaf) {
|
|
665
|
+
const [key, ...rest] = path;
|
|
666
|
+
if (key === undefined) {
|
|
667
|
+
return leaf;
|
|
668
|
+
}
|
|
669
|
+
const record = (typeof value === "object" && value !== null ? value : {});
|
|
670
|
+
return { ...record, [key]: writeAt(record[key], rest, leaf) };
|
|
671
|
+
}
|
|
672
|
+
// Every invariant written on the scope or on an object its fields hold, with
|
|
673
|
+
// the path of the object it is written on.
|
|
674
|
+
function scopedInvariants(schema, prefix) {
|
|
675
|
+
if (schema.kind !== "object") {
|
|
676
|
+
return [];
|
|
677
|
+
}
|
|
678
|
+
const { shape } = schema;
|
|
679
|
+
return [
|
|
680
|
+
...schema.invariants.map(rule => ({ prefix, rule })),
|
|
681
|
+
...Object.entries(shape).flatMap(([key, field]) => scopedInvariants(field, [...prefix, key])),
|
|
682
|
+
];
|
|
683
|
+
}
|
|
684
|
+
function readAt(value, path) {
|
|
685
|
+
return path.reduce((current, key) => typeof current === "object" && current !== null
|
|
686
|
+
? current[key]
|
|
687
|
+
: undefined, value);
|
|
688
|
+
}
|
|
689
|
+
// The value `after` becomes when the finite positions an invariant relates,
|
|
690
|
+
// other than those `after` moved away from `before`, are chosen again so every
|
|
691
|
+
// such invariant holds, keeping what `after` holds wherever it can; undefined
|
|
692
|
+
// when no choice keeps them, when there are more combinations than the limit,
|
|
693
|
+
// or when it would take another case of a sum field.
|
|
694
|
+
export function keepingInvariants(scope, before, after, combinations = FEASIBILITY_COMBINATION_LIMIT) {
|
|
695
|
+
const related = distinctPaths(jointInvariantsOf(scope).flatMap(invariant => invariant.paths));
|
|
696
|
+
const groups = related.map((path) => ({
|
|
697
|
+
path,
|
|
698
|
+
measure: "value",
|
|
699
|
+
constraints: isDeepStrictEqual(readAt(before, path), readAt(after, path))
|
|
700
|
+
? []
|
|
701
|
+
: [{ operator: "==", bound: readAt(after, path) }],
|
|
702
|
+
}));
|
|
703
|
+
const assignment = jointAssignment(groups, scope, combinations, path => readAt(after, path));
|
|
704
|
+
if (typeof assignment === "string") {
|
|
705
|
+
return undefined;
|
|
706
|
+
}
|
|
707
|
+
let kept = after;
|
|
708
|
+
for (const [key, value] of assignment) {
|
|
709
|
+
const path = JSON.parse(key);
|
|
710
|
+
if (readAt(kept, path) === value) {
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
713
|
+
if (sumOwning(scope, path) !== undefined) {
|
|
714
|
+
return undefined;
|
|
715
|
+
}
|
|
716
|
+
kept = writeAt(kept, path, value);
|
|
717
|
+
}
|
|
718
|
+
return kept;
|
|
719
|
+
}
|
|
720
|
+
// The value `after` becomes when each invariant ordering two or more integer or
|
|
721
|
+
// number positions that it breaks is kept by moving one of its positions weighed
|
|
722
|
+
// by one, and one `after` did not move away from `before`, to the nearest value
|
|
723
|
+
// that keeps it; undefined when an invariant is left broken.
|
|
724
|
+
export function keepingOrderings(scope, before, after) {
|
|
725
|
+
const orderings = scopedInvariants(scope, []).flatMap(({ prefix, rule }) => {
|
|
726
|
+
if (rule.kind !== "compare" || ![rule.left, rule.right].every(operand => isTerm(operand) || typeof operand === "number")) {
|
|
727
|
+
return [];
|
|
728
|
+
}
|
|
729
|
+
const form = differenceOf(rule);
|
|
730
|
+
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 ?? ""));
|
|
732
|
+
return parts.length >= 2 && numeric && typeof form.constant === "number"
|
|
733
|
+
? [{ rule: rule, parts, constant: form.constant }]
|
|
734
|
+
: [];
|
|
735
|
+
});
|
|
736
|
+
let kept = after;
|
|
737
|
+
// A part moved once stays: moving it again to keep a later ordering would
|
|
738
|
+
// break the one it was moved for, so a chain is kept by moving its next part.
|
|
739
|
+
const moved = new Set();
|
|
740
|
+
for (let round = 0; round <= orderings.length; round++) {
|
|
741
|
+
const broken = orderings.find(({ rule, parts, constant }) => {
|
|
742
|
+
const values = parts.map(part => readAt(kept, part.path));
|
|
743
|
+
return (values.every(value => typeof value === "number") &&
|
|
744
|
+
!holds({ ...rule, left: parts.reduce((total, part, index) => total + part.coefficient * values[index], constant), right: 0 }, undefined));
|
|
745
|
+
});
|
|
746
|
+
if (broken === undefined) {
|
|
747
|
+
return kept;
|
|
748
|
+
}
|
|
749
|
+
const moving = broken.parts.find(part => Math.abs(part.coefficient) === 1 &&
|
|
750
|
+
!moved.has(JSON.stringify(part.path)) &&
|
|
751
|
+
isDeepStrictEqual(readAt(before, part.path), readAt(after, part.path)));
|
|
752
|
+
if (moving === undefined) {
|
|
753
|
+
return undefined;
|
|
754
|
+
}
|
|
755
|
+
const rest = broken.parts.reduce((total, part) => (part === moving ? total : total + part.coefficient * readAt(kept, part.path)), broken.constant);
|
|
756
|
+
// coefficient * x + rest <operator> 0, read as a bound on x itself.
|
|
757
|
+
const operator = moving.coefficient === 1 ? broken.rule.operator : mirrored[broken.rule.operator];
|
|
758
|
+
const bound = -rest * moving.coefficient;
|
|
759
|
+
const current = readAt(kept, moving.path);
|
|
760
|
+
const nearest = operator === "!=" ? current + 1 : nearestKeeping(scope, moving.path, operator, bound);
|
|
761
|
+
if (nearest === undefined) {
|
|
762
|
+
return undefined;
|
|
763
|
+
}
|
|
764
|
+
kept = writeAt(kept, moving.path, nearest);
|
|
765
|
+
moved.add(JSON.stringify(moving.path));
|
|
766
|
+
}
|
|
767
|
+
return undefined;
|
|
768
|
+
}
|
|
769
|
+
// The value nearest `bound` on the side `operator` keeps that the position's
|
|
770
|
+
// own bounds admit: an integer steps by one, and a number, which has no step,
|
|
771
|
+
// takes the middle of what is left when less than one is.
|
|
772
|
+
function nearestKeeping(scope, path, operator, bound) {
|
|
773
|
+
const settled = settle({ path, measure: "value", constraints: [{ operator, bound }] }, scope);
|
|
774
|
+
if (typeof settled !== "object") {
|
|
775
|
+
return settled === false ? undefined : bound;
|
|
776
|
+
}
|
|
777
|
+
const lower = settled.lower?.value;
|
|
778
|
+
const upper = settled.upper?.value;
|
|
779
|
+
if (operator === ">" || operator === ">=") {
|
|
780
|
+
const from = lower ?? bound;
|
|
781
|
+
if (settled.carrier === integerCarrier || settled.lower?.inclusive !== false) {
|
|
782
|
+
return settled.lower?.inclusive === false ? from + 1 : from;
|
|
783
|
+
}
|
|
784
|
+
return upper !== undefined && upper - from <= 1 ? (from + upper) / 2 : from + 1;
|
|
785
|
+
}
|
|
786
|
+
if (operator === "<" || operator === "<=") {
|
|
787
|
+
const from = upper ?? bound;
|
|
788
|
+
if (settled.carrier === integerCarrier || settled.upper?.inclusive !== false) {
|
|
789
|
+
return settled.upper?.inclusive === false ? from - 1 : from;
|
|
790
|
+
}
|
|
791
|
+
return lower !== undefined && from - lower <= 1 ? (from + lower) / 2 : from - 1;
|
|
792
|
+
}
|
|
793
|
+
return bound;
|
|
794
|
+
}
|
|
795
|
+
// Whether no combination the invariants relating finite positions keep gives
|
|
796
|
+
// the position at `path` this value.
|
|
797
|
+
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");
|
|
801
|
+
}
|
|
802
|
+
// The invariants the scope holds that relate two or more finite positions,
|
|
803
|
+
// each with the positions it reads.
|
|
804
|
+
function jointInvariantsOf(scope) {
|
|
805
|
+
return scopedInvariants(scope, []).flatMap(({ prefix, rule }) => {
|
|
806
|
+
const paths = finitePathsOf(rule, scope, prefix);
|
|
807
|
+
const distinct = paths === undefined ? [] : distinctPaths(paths);
|
|
808
|
+
return distinct.length < 2 ? [] : [{ prefix, rule, paths: distinct }];
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
function distinctPaths(paths) {
|
|
812
|
+
return [...new Map(paths.map(path => [JSON.stringify(path), path])).values()];
|
|
813
|
+
}
|
|
814
|
+
// When every position a decision's guards and match read is finite (a
|
|
815
|
+
// boolean, an enum or a sum's discriminant, an optional one only where the
|
|
816
|
+
// field itself may be left out) and their combinations, together with the
|
|
817
|
+
// finite positions an invariant on only finite positions connects to them,
|
|
818
|
+
// are within the limit: the ways those combinations take, each with one
|
|
819
|
+
// combination taking it, of the combinations that keep every such invariant.
|
|
820
|
+
// Undefined otherwise.
|
|
821
|
+
export function finiteReach(decision, scope, combinations, fixed = []) {
|
|
822
|
+
const read = decisionPaths(decision);
|
|
823
|
+
if (read === undefined) {
|
|
824
|
+
return undefined;
|
|
825
|
+
}
|
|
826
|
+
const positions = [...distinctPaths([...read, ...fixed])];
|
|
827
|
+
const invariants = fieldInvariants(scope, []);
|
|
828
|
+
const has = (path) => positions.some(other => isDeepStrictEqual(other, path));
|
|
829
|
+
for (let grew = true; grew;) {
|
|
830
|
+
grew = false;
|
|
831
|
+
for (const { prefix, rule } of invariants) {
|
|
832
|
+
const paths = termPaths(rule).map(path => [...prefix, ...path]);
|
|
833
|
+
if (!paths.some(has) || !paths.every(path => domainWithAbsence(scope, path) !== undefined)) {
|
|
834
|
+
continue;
|
|
835
|
+
}
|
|
836
|
+
for (const path of paths.filter(path => !has(path))) {
|
|
837
|
+
positions.push(path);
|
|
838
|
+
grew = true;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
const domains = positions.map(path => domainWithAbsence(scope, path));
|
|
843
|
+
if (domains.some(domain => domain === undefined)) {
|
|
844
|
+
return undefined;
|
|
845
|
+
}
|
|
846
|
+
if (domains.reduce((product, domain) => product * domain.length, 1) > combinations) {
|
|
847
|
+
return undefined;
|
|
848
|
+
}
|
|
849
|
+
const keys = new Set(positions.map(path => JSON.stringify(path)));
|
|
850
|
+
const kept = invariants.filter(({ prefix, rule }) => termPaths(rule).every(path => keys.has(JSON.stringify([...prefix, ...path]))));
|
|
851
|
+
const reached = new Map();
|
|
852
|
+
const chosen = [];
|
|
853
|
+
const visit = (index) => {
|
|
854
|
+
if (index === positions.length) {
|
|
855
|
+
const value = positions.reduce((built, path, at) => (chosen[at] === undefined ? built : writeAt(built, path, chosen[at])), {});
|
|
856
|
+
if (!kept.every(({ prefix, rule }) => holds(rule, readAt(value, prefix)))) {
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
const way = traceFinite(decision, value);
|
|
860
|
+
const key = JSON.stringify(way.steps.map(step => [describeStep(step), step.outcome]).concat([[String(way.exit)]]));
|
|
861
|
+
if (!reached.has(key)) {
|
|
862
|
+
reached.set(key, { way, witness: new Map(positions.map((path, at) => [JSON.stringify(path), chosen[at]])) });
|
|
863
|
+
}
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
for (const candidate of domains[index]) {
|
|
867
|
+
chosen[index] = candidate;
|
|
868
|
+
visit(index + 1);
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
visit(0);
|
|
872
|
+
return [...reached.values()].sort((left, right) => inWayOrder(left.way, right.way));
|
|
873
|
+
}
|
|
874
|
+
// The order waysOf lists ways in: depth first, a condition holding before it
|
|
875
|
+
// fails, a match's cases as written.
|
|
876
|
+
function inWayOrder(left, right) {
|
|
877
|
+
const rank = (step) => {
|
|
878
|
+
if (step.distinction.kind === "match") {
|
|
879
|
+
return Object.keys(step.distinction.cases).indexOf(String(step.outcome));
|
|
880
|
+
}
|
|
881
|
+
return step.outcome === true ? 0 : 1;
|
|
882
|
+
};
|
|
883
|
+
for (let index = 0; index < Math.min(left.steps.length, right.steps.length); index++) {
|
|
884
|
+
const difference = rank(left.steps[index]) - rank(right.steps[index]);
|
|
885
|
+
if (difference !== 0) {
|
|
886
|
+
return difference;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
return left.steps.length - right.steps.length;
|
|
890
|
+
}
|
|
891
|
+
function describeStep(step) {
|
|
892
|
+
return step.distinction.kind === "match" ? `match ${positionOf(step.distinction.on).path.join(".")}` : describeRule(step.distinction);
|
|
893
|
+
}
|
|
894
|
+
function decisionPaths(decision) {
|
|
895
|
+
const paths = [];
|
|
896
|
+
const collect = (rule) => {
|
|
897
|
+
switch (rule.kind) {
|
|
898
|
+
case "compare":
|
|
899
|
+
return [rule.left, rule.right].filter(isTerm).every(side => {
|
|
900
|
+
const data = positionData(side);
|
|
901
|
+
if (data === undefined) {
|
|
902
|
+
return false;
|
|
903
|
+
}
|
|
904
|
+
paths.push(data.path);
|
|
905
|
+
return data.measure === "value";
|
|
906
|
+
});
|
|
907
|
+
case "not":
|
|
908
|
+
return collect(rule.rule);
|
|
909
|
+
case "and":
|
|
910
|
+
case "or":
|
|
911
|
+
return rule.rules.every(collect);
|
|
912
|
+
default:
|
|
913
|
+
return false;
|
|
914
|
+
}
|
|
915
|
+
};
|
|
916
|
+
if (!decision.guards.every(candidate => collect(candidate.condition))) {
|
|
917
|
+
return undefined;
|
|
918
|
+
}
|
|
919
|
+
const { otherwise } = decision;
|
|
920
|
+
if (typeof otherwise !== "function") {
|
|
921
|
+
paths.push(positionOf(otherwise.on).path);
|
|
922
|
+
}
|
|
923
|
+
return paths.some(path => path[0] === DEPS) ? undefined : paths;
|
|
924
|
+
}
|
|
925
|
+
function domainWithAbsence(scope, path) {
|
|
926
|
+
const parent = rawAt(scope, path.slice(0, -1));
|
|
927
|
+
const last = path[path.length - 1];
|
|
928
|
+
const domain = finiteDomainAt(scope, path);
|
|
929
|
+
if (parent === undefined || last === undefined || domain === undefined) {
|
|
930
|
+
return undefined;
|
|
931
|
+
}
|
|
932
|
+
if (isVariantsSchema(parent)) {
|
|
933
|
+
return parent.discriminant === last ? domain : undefined;
|
|
934
|
+
}
|
|
935
|
+
if (parent.kind !== "object") {
|
|
936
|
+
return undefined;
|
|
937
|
+
}
|
|
938
|
+
const field = parent.shape[last];
|
|
939
|
+
return field?.kind === "optional" ? [...domain, undefined] : domain;
|
|
940
|
+
}
|
|
941
|
+
// The schema at a path without stepping through an optional, which a
|
|
942
|
+
// combination could not write a field under.
|
|
943
|
+
function rawAt(schema, keys) {
|
|
944
|
+
const [key, ...rest] = keys;
|
|
945
|
+
if (key === undefined) {
|
|
946
|
+
return schema;
|
|
947
|
+
}
|
|
948
|
+
if (schema.kind !== "object") {
|
|
949
|
+
return undefined;
|
|
950
|
+
}
|
|
951
|
+
const field = schema.shape[key];
|
|
952
|
+
return field === undefined ? undefined : rawAt(field, rest);
|
|
953
|
+
}
|
|
954
|
+
// Every invariant written on the scope, an object or a field it holds, or
|
|
955
|
+
// what an optional field holds, with the path of what it is written on.
|
|
956
|
+
function fieldInvariants(schema, prefix) {
|
|
957
|
+
const own = schema.invariants.map(rule => ({ prefix, rule }));
|
|
958
|
+
if (schema.kind === "optional") {
|
|
959
|
+
return [...own, ...fieldInvariants(schema.schema, prefix)];
|
|
960
|
+
}
|
|
961
|
+
if (schema.kind !== "object") {
|
|
962
|
+
return own;
|
|
963
|
+
}
|
|
964
|
+
const { shape } = schema;
|
|
965
|
+
return [...own, ...Object.entries(shape).flatMap(([key, field]) => fieldInvariants(field, [...prefix, key]))];
|
|
966
|
+
}
|
|
967
|
+
function traceFinite(decision, value) {
|
|
968
|
+
const steps = [];
|
|
969
|
+
const distinguish = (distinction, outcome) => {
|
|
970
|
+
steps.push({ distinction, outcome });
|
|
971
|
+
};
|
|
972
|
+
for (const [index, candidate] of decision.guards.entries()) {
|
|
973
|
+
if (!holds(candidate.condition, value, undefined, distinguish)) {
|
|
974
|
+
return { steps, exit: index };
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
const { otherwise } = decision;
|
|
978
|
+
if (typeof otherwise === "function") {
|
|
979
|
+
return { steps, exit: "otherwise" };
|
|
980
|
+
}
|
|
981
|
+
steps.push({ distinction: otherwise, outcome: String(readOperand(otherwise.on, value)) });
|
|
982
|
+
return { steps, exit: "case" };
|
|
983
|
+
}
|
|
984
|
+
//# sourceMappingURL=feasibility.js.map
|