@toiroakr/chisel 0.0.0-stage → 0.2.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 +693 -2
- package/dist/absence.d.ts +27 -0
- package/dist/absence.js +228 -0
- package/dist/absence.js.map +1 -0
- package/dist/behavior.d.ts +193 -0
- package/dist/behavior.js +589 -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 +61 -0
- package/dist/border.js +434 -0
- package/dist/border.js.map +1 -0
- package/dist/bounds.d.ts +1 -0
- package/dist/bounds.js +26 -0
- package/dist/bounds.js.map +1 -0
- package/dist/cli.d.ts +7 -0
- package/dist/cli.js +835 -0
- package/dist/cli.js.map +1 -0
- package/dist/codec.d.ts +6 -0
- package/dist/codec.js +86 -0
- package/dist/codec.js.map +1 -0
- package/dist/codegen.d.ts +2 -0
- package/dist/codegen.js +61 -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 +17 -0
- package/dist/data.js +51 -0
- package/dist/data.js.map +1 -0
- package/dist/dependency.d.ts +101 -0
- package/dist/dependency.js +161 -0
- package/dist/dependency.js.map +1 -0
- package/dist/domain.d.ts +12 -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/equal.d.ts +1 -0
- package/dist/equal.js +135 -0
- package/dist/equal.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 +63 -0
- package/dist/feasibility.js +1053 -0
- package/dist/feasibility.js.map +1 -0
- package/dist/guard-borders.d.ts +57 -0
- package/dist/guard-borders.js +760 -0
- package/dist/guard-borders.js.map +1 -0
- package/dist/guard-scope.d.ts +61 -0
- package/dist/guard-scope.js +364 -0
- package/dist/guard-scope.js.map +1 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/interleave.d.ts +31 -0
- package/dist/interleave.js +202 -0
- package/dist/interleave.js.map +1 -0
- package/dist/interval.d.ts +10 -0
- package/dist/interval.js +311 -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/machine-check.d.ts +73 -0
- package/dist/machine-check.js +215 -0
- package/dist/machine-check.js.map +1 -0
- package/dist/machine.d.ts +97 -0
- package/dist/machine.js +200 -0
- package/dist/machine.js.map +1 -0
- package/dist/model.d.ts +160 -0
- package/dist/model.js +778 -0
- package/dist/model.js.map +1 -0
- package/dist/outcomes.d.ts +7 -0
- package/dist/outcomes.js +38 -0
- package/dist/outcomes.js.map +1 -0
- package/dist/pairs.d.ts +26 -0
- package/dist/pairs.js +115 -0
- package/dist/pairs.js.map +1 -0
- package/dist/partition.d.ts +45 -0
- package/dist/partition.js +354 -0
- package/dist/partition.js.map +1 -0
- package/dist/proof.d.ts +21 -0
- package/dist/proof.js +556 -0
- package/dist/proof.js.map +1 -0
- package/dist/replay.d.ts +45 -0
- package/dist/replay.js +157 -0
- package/dist/replay.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 +186 -0
- package/dist/rule.js +866 -0
- package/dist/rule.js.map +1 -0
- package/dist/runtime.d.ts +12 -0
- package/dist/runtime.js +8 -0
- package/dist/runtime.js.map +1 -0
- package/dist/schema.d.ts +163 -0
- package/dist/schema.js +528 -0
- package/dist/schema.js.map +1 -0
- package/dist/specification.d.ts +266 -0
- package/dist/specification.js +2123 -0
- package/dist/specification.js.map +1 -0
- package/dist/temporal-global.d.ts +4 -0
- package/dist/temporal-global.js +11 -0
- package/dist/temporal-global.js.map +1 -0
- package/dist/temporal.d.ts +22 -0
- package/dist/temporal.js +113 -0
- package/dist/temporal.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/tla.d.ts +54 -0
- package/dist/tla.js +579 -0
- package/dist/tla.js.map +1 -0
- package/dist/verification.d.ts +7 -0
- package/dist/verification.js +5 -0
- package/dist/verification.js.map +1 -0
- package/dist/ways.d.ts +28 -0
- package/dist/ways.js +86 -0
- package/dist/ways.js.map +1 -0
- package/dist/world.d.ts +98 -0
- package/dist/world.js +467 -0
- package/dist/world.js.map +1 -0
- package/package.json +76 -4
- package/schema/report.schema.json +1062 -0
|
@@ -0,0 +1,760 @@
|
|
|
1
|
+
import { temporalNeighbor, timelineOf } from "./temporal.js";
|
|
2
|
+
import { deepEqual } from "./equal.js";
|
|
3
|
+
import { bordersOf, decimalUnitsCarrier, integerCarrier, nanosecondCarrier, normalize, numberCarrier } from "./border.js";
|
|
4
|
+
import { carrierOf, positionsOf, writeExactly } from "./partition.js";
|
|
5
|
+
import { DEPS, boundTermPath, conjuncts, describeRule, holds, isDecimal, isTerm, readOperand, selfTerm, shiftTerms, termData, termPaths, positionData, positionOf, positionTerm, differenceOf, withDeps, decimalOfUnits, decimalUnits, counts, measured, measureLabel } from "./rule.js";
|
|
6
|
+
import { enumOf, int, isVariantsSchema, object, variants } from "./schema.js";
|
|
7
|
+
import { casesOnTrail, enter, everyStepsOf, framesAt, framesFor, guardScope, inheritedAt, labelsOf, locate, onTrail, pathOf, schemaAlong, schemaAt, segmentsOf, thresholdsIn } from "./guard-scope.js";
|
|
8
|
+
// The case as feasibility reads it: the invariants written on the input sum
|
|
9
|
+
// hold of every case, as the partition passes them down, and read the
|
|
10
|
+
// discriminant as the one value it has under this case.
|
|
11
|
+
export function feasibilityScope(definition, tag, decision, steps = []) {
|
|
12
|
+
const scope = guardScope(definition, tag, decision, steps);
|
|
13
|
+
const { discriminant } = definition.input;
|
|
14
|
+
return {
|
|
15
|
+
...scope,
|
|
16
|
+
shape: Object.hasOwn(scope.shape, discriminant)
|
|
17
|
+
? scope.shape
|
|
18
|
+
: { ...scope.shape, [discriminant]: enumOf([tag]) },
|
|
19
|
+
invariants: [...scope.invariants, ...definition.input.invariants],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function guardBordersOf(implementation) {
|
|
23
|
+
const input = implementation.behavior.input;
|
|
24
|
+
const positions = positionsOf(input, { containers: true });
|
|
25
|
+
const at = (segments) => positions.find(position => deepEqual(position.segments, segments));
|
|
26
|
+
return Object.entries(implementation.cases).flatMap(([tag, decision]) => decision.kind !== "rules"
|
|
27
|
+
? []
|
|
28
|
+
: decision.guards.flatMap(candidate => walk(candidate.condition, framesFor(implementation.behavior, tag, decision), at, asGuard).map(drawn => ({
|
|
29
|
+
...drawn,
|
|
30
|
+
origin: { decision, scope: feasibilityScope(implementation.behavior, tag, decision), tag },
|
|
31
|
+
}))));
|
|
32
|
+
}
|
|
33
|
+
export function ensuresBordersOf(definition) {
|
|
34
|
+
const input = definition.input;
|
|
35
|
+
const positions = positionsOf(input);
|
|
36
|
+
const at = (segments) => positions.find(position => deepEqual(position.segments, segments));
|
|
37
|
+
return definition.ensures.flatMap(clause => conjuncts(clause.rule).flatMap(part => {
|
|
38
|
+
const onInput = unrooted(part);
|
|
39
|
+
if (onInput === undefined) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
return input.variantTags.flatMap(tag => walk(onInput, framesAt(input.variants[tag], `@${tag}`, "$"), at, {
|
|
43
|
+
source: "ensures",
|
|
44
|
+
describe: () => `${clause.name}: ${describeRule(part, "")}`,
|
|
45
|
+
}));
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
// An invariant comparing one position with a constant draws its border on that
|
|
49
|
+
// position (src/border.ts); this one draws those comparing two positions, which
|
|
50
|
+
// no single position carries.
|
|
51
|
+
export function invariantPairBordersOf(definition) {
|
|
52
|
+
const positions = positionsOf(definition.input, { containers: true });
|
|
53
|
+
const at = (segments) => positions.find(position => deepEqual(position.segments, segments));
|
|
54
|
+
const reading = {
|
|
55
|
+
source: "invariant",
|
|
56
|
+
describe: (compared, read) => describeRule(compared, read.root.label, labelsOf(read)),
|
|
57
|
+
};
|
|
58
|
+
const invariants = pairInvariantsOf(definition);
|
|
59
|
+
return definition.input.variantTags.flatMap(tag => {
|
|
60
|
+
const drawn = invariants.filter(invariant => invariant.tag === tag).flatMap(({ frame, rule }) => {
|
|
61
|
+
const frames = { root: frame, elements: {} };
|
|
62
|
+
if (settledAlone(rule) !== undefined) {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
const borders = [rule.left, rule.right].some(operand => isTerm(operand) && positionData(operand) === undefined)
|
|
66
|
+
? betweenExpression(rule, frames, at, reading)
|
|
67
|
+
: isTerm(rule.left) && isTerm(rule.right)
|
|
68
|
+
? between(rule, rule.left, rule.right, frames, at, reading)
|
|
69
|
+
: [];
|
|
70
|
+
const keys = frame.segments.slice(1).map(segment => segment.slice(1));
|
|
71
|
+
return borders.map(border => ({
|
|
72
|
+
...border,
|
|
73
|
+
coordinateOf: (reached) => border.coordinateOf({
|
|
74
|
+
...reached,
|
|
75
|
+
scope: keys.reduce((value, key) => value?.[key], reached.scope),
|
|
76
|
+
}),
|
|
77
|
+
}));
|
|
78
|
+
});
|
|
79
|
+
return drawn.map(border => withoutPairRefusedPoints(border, drawn));
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
// An input invariant whose sides cancel, such as $.x < $.x, holds of no value,
|
|
83
|
+
// unless a field it reads may be left out: a comparison with it absent holds.
|
|
84
|
+
export function invariantContradictionsOf(definition) {
|
|
85
|
+
return pairInvariantsOf(definition).flatMap(({ frame, rule }) => settledAlone(rule) === false &&
|
|
86
|
+
!readPathsOf(rule).some(path => mayBeLeftOut(definition, [...frame.segments, ...path.map(key => `.${key}`)]))
|
|
87
|
+
? [
|
|
88
|
+
`${frame.path}: 不変条件を満たす値がありません (invariant ${rule.name === undefined ? "" : `${rule.name}: `}${describeRule(rule, frame.label)})`,
|
|
89
|
+
]
|
|
90
|
+
: []);
|
|
91
|
+
}
|
|
92
|
+
function readPathsOf(rule) {
|
|
93
|
+
return [rule.left, rule.right].flatMap(operand => {
|
|
94
|
+
if (!isTerm(operand)) {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
const data = termData(operand);
|
|
98
|
+
return data.kind === "position" ? [data.path] : data.parts.map(part => part.path);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
// Whether a position an object invariant reads, named by segments such as
|
|
102
|
+
// ["@case", ".key"], is or lies under a field that may be left out.
|
|
103
|
+
export function mayBeLeftOut(definition, segments) {
|
|
104
|
+
const [caseSegment, ...keys] = segments;
|
|
105
|
+
return leavesOut(definition.input.variants[caseSegment.slice(1)], keys);
|
|
106
|
+
}
|
|
107
|
+
// A sum field leaves a path out when any of its cases does: the path names a
|
|
108
|
+
// field its cases share, and a value may be of the case that omits it.
|
|
109
|
+
function leavesOut(schema, keys) {
|
|
110
|
+
if (schema === undefined) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
if (schema.kind === "optional") {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
if (isVariantsSchema(schema)) {
|
|
117
|
+
const [key, ...rest] = keys;
|
|
118
|
+
return key?.startsWith("@") === true
|
|
119
|
+
? leavesOut(schema.variants[key.slice(1)], rest)
|
|
120
|
+
: schema.variantTags.some(tag => leavesOut(schema.variants[tag], keys));
|
|
121
|
+
}
|
|
122
|
+
const [key, ...rest] = keys;
|
|
123
|
+
if (key === undefined || schema.kind !== "object" || !key.startsWith(".")) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
return leavesOut(schema.shape[key.slice(1)], rest);
|
|
127
|
+
}
|
|
128
|
+
function pairInvariantsOf(definition) {
|
|
129
|
+
const input = definition.input;
|
|
130
|
+
return input.variantTags.flatMap(tag => {
|
|
131
|
+
const scope = input.variants[tag];
|
|
132
|
+
return objectFramesIn({ scope, path: `@${tag}`, segments: [`@${tag}`], label: "$" }).flatMap(frame => [...(frame.scope === scope ? input.invariants : []), ...frame.scope.invariants]
|
|
133
|
+
.flatMap(conjuncts)
|
|
134
|
+
.flatMap(rule => (rule.kind === "compare" ? [{ tag, frame, rule }] : [])));
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
// Whether a comparison of numbers holds whatever the positions hold, when its
|
|
138
|
+
// positions cancel; undefined while one is left to draw a border on.
|
|
139
|
+
function comparesTwoPositions(drawn) {
|
|
140
|
+
return [drawn.comparison.left, drawn.comparison.right].every(operand => isTerm(operand) && positionData(operand) !== undefined);
|
|
141
|
+
}
|
|
142
|
+
function settledAlone(rule) {
|
|
143
|
+
if (![rule.left, rule.right].every(operand => isTerm(operand) || typeof operand === "number" || isDecimal(operand))) {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
const form = differenceOf(rule);
|
|
147
|
+
if (form.parts.length > 0) {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
// Compared by its sign, so a decimal constant is read as a number is.
|
|
151
|
+
const sign = typeof form.constant === "number" ? Math.sign(form.constant) : form.constant.comparedTo(0);
|
|
152
|
+
return holds({ kind: "compare", left: sign, operator: rule.operator, right: 0 }, undefined);
|
|
153
|
+
}
|
|
154
|
+
// Optionals, arrays, records and sum fields are not descended, as the invariants
|
|
155
|
+
// relating finite positions are not (src/feasibility.ts).
|
|
156
|
+
function objectFramesIn(frame) {
|
|
157
|
+
if (frame.scope.kind !== "object") {
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
160
|
+
const shape = frame.scope.shape;
|
|
161
|
+
return [
|
|
162
|
+
frame,
|
|
163
|
+
...Object.entries(shape).flatMap(([key, field]) => objectFramesIn({ scope: field, path: pathOf(frame, [key]), segments: segmentsOf(frame, [key]), label: "$" })),
|
|
164
|
+
];
|
|
165
|
+
}
|
|
166
|
+
// Not left to bordersOf, which excludes a point another bound on the same
|
|
167
|
+
// measure refuses: each comparison of two positions is drawn on a difference of
|
|
168
|
+
// its own, so the bounds on one difference never meet there.
|
|
169
|
+
function withoutPairRefusedPoints(border, all) {
|
|
170
|
+
const [first, second] = (border.reads ?? []).map(segments => JSON.stringify(segments));
|
|
171
|
+
const others = all.flatMap(other => {
|
|
172
|
+
const [otherFirst, otherSecond] = (other.reads ?? []).map(segments => JSON.stringify(segments));
|
|
173
|
+
if (other === border || first === undefined || !comparesTwoPositions(border) || !comparesTwoPositions(other)) {
|
|
174
|
+
return [];
|
|
175
|
+
}
|
|
176
|
+
return otherFirst === first && otherSecond === second
|
|
177
|
+
? [{ other, sign: 1 }]
|
|
178
|
+
: otherFirst === second && otherSecond === first
|
|
179
|
+
? [{ other, sign: -1 }]
|
|
180
|
+
: [];
|
|
181
|
+
});
|
|
182
|
+
const refuses = (witness) => others.some(({ other, sign }) => {
|
|
183
|
+
const value = sign * (typeof witness === "bigint" ? Number(witness > 0n) - Number(witness < 0n) : witness);
|
|
184
|
+
return !holds({ ...other.comparison, left: value, right: 0 }, undefined);
|
|
185
|
+
});
|
|
186
|
+
return {
|
|
187
|
+
...border,
|
|
188
|
+
border: {
|
|
189
|
+
...border.border,
|
|
190
|
+
points: border.border.points.map(point => point.status === "owed" && point.witness !== undefined && refuses(point.witness)
|
|
191
|
+
? { ...point, status: "excluded" }
|
|
192
|
+
: point),
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function unrooted(rule) {
|
|
197
|
+
if (rule.kind !== "compare") {
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
const terms = [rule.left, rule.right].filter(isTerm);
|
|
201
|
+
if (terms.length === 0 || termPaths(rule).some(path => path[0] !== "input")) {
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
return shiftTerms(rule);
|
|
205
|
+
}
|
|
206
|
+
export function guardPartitionsOf(implementation) {
|
|
207
|
+
const input = implementation.behavior.input;
|
|
208
|
+
const thresholds = Object.entries(implementation.cases).flatMap(([tag, decision]) => decision.kind !== "rules"
|
|
209
|
+
? []
|
|
210
|
+
: decision.guards.flatMap(candidate => thresholdsIn(candidate.condition, framesFor(implementation.behavior, tag, decision))));
|
|
211
|
+
const trails = [...new Set(thresholds.map(threshold => JSON.stringify(threshold.segments)))];
|
|
212
|
+
return trails.flatMap(trail => {
|
|
213
|
+
const atPath = thresholds.filter(threshold => JSON.stringify(threshold.segments) === trail);
|
|
214
|
+
const partition = partitionAt(atPath[0].path, atPath[0].segments, atPath[0].schema, atPath[0].inherited, atPath.map(threshold => threshold.rule));
|
|
215
|
+
return partition === undefined ? [] : [partition];
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
function partitionAt(path, segments, schema, inherited, rules) {
|
|
219
|
+
const carrier = carrierOf(schema, "value");
|
|
220
|
+
if (carrier === undefined) {
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
const cuts = rules.flatMap(rule => {
|
|
224
|
+
const { operator, bound } = normalize(rule);
|
|
225
|
+
return operator === "==" || operator === "!="
|
|
226
|
+
? [
|
|
227
|
+
{ value: bound, lowerHoldsIt: false },
|
|
228
|
+
{ value: bound, lowerHoldsIt: true },
|
|
229
|
+
]
|
|
230
|
+
: [{ value: bound, lowerHoldsIt: operator === "<=" || operator === ">" }];
|
|
231
|
+
});
|
|
232
|
+
const unique = cuts
|
|
233
|
+
.filter((cut, index) => cuts.findIndex(other => carrier.compare(other.value, cut.value) === 0 &&
|
|
234
|
+
other.lowerHoldsIt === cut.lowerHoldsIt) === index)
|
|
235
|
+
.sort((left, right) => carrier.compare(left.value, right.value));
|
|
236
|
+
const { lower, upper } = admittedRange([...schema.invariants, ...inherited], carrier);
|
|
237
|
+
const edges = [
|
|
238
|
+
lower,
|
|
239
|
+
...unique.flatMap(cut => [
|
|
240
|
+
{ value: cut.value, inclusive: cut.lowerHoldsIt },
|
|
241
|
+
{ value: cut.value, inclusive: !cut.lowerHoldsIt },
|
|
242
|
+
]),
|
|
243
|
+
upper,
|
|
244
|
+
];
|
|
245
|
+
const classes = [];
|
|
246
|
+
const excluded = [];
|
|
247
|
+
for (let index = 0; index < edges.length; index += 2) {
|
|
248
|
+
const from = edges[index];
|
|
249
|
+
const to = edges[index + 1];
|
|
250
|
+
const contains = (value) => (from === undefined ||
|
|
251
|
+
carrier.compare(value, from.value) > 0 ||
|
|
252
|
+
(from.inclusive && carrier.compare(value, from.value) === 0)) &&
|
|
253
|
+
(to === undefined ||
|
|
254
|
+
carrier.compare(value, to.value) < 0 ||
|
|
255
|
+
(to.inclusive && carrier.compare(value, to.value) === 0));
|
|
256
|
+
const witness = from === undefined
|
|
257
|
+
? to === undefined
|
|
258
|
+
? undefined
|
|
259
|
+
: to.inclusive
|
|
260
|
+
? to.value
|
|
261
|
+
: (carrier.step?.(to.value, -1) ?? carrier.past?.(to.value, -1))
|
|
262
|
+
: from.inclusive
|
|
263
|
+
? from.value
|
|
264
|
+
: (carrier.step?.(from.value, 1) ?? carrier.past?.(from.value, 1));
|
|
265
|
+
const single = from !== undefined &&
|
|
266
|
+
to !== undefined &&
|
|
267
|
+
from.inclusive &&
|
|
268
|
+
to.inclusive &&
|
|
269
|
+
carrier.compare(from.value, to.value) === 0;
|
|
270
|
+
const name = single
|
|
271
|
+
? `v = ${carrier.format(from.value)}`
|
|
272
|
+
: [
|
|
273
|
+
from === undefined ? "" : `${carrier.format(from.value)} ${from.inclusive ? "<=" : "<"} `,
|
|
274
|
+
"v",
|
|
275
|
+
to === undefined ? "" : ` ${to.inclusive ? "<=" : "<"} ${carrier.format(to.value)}`,
|
|
276
|
+
].join("");
|
|
277
|
+
if (witness === undefined || !contains(witness)) {
|
|
278
|
+
excluded.push(name);
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
classes.push({ name, witness, contains });
|
|
282
|
+
}
|
|
283
|
+
return { path, segments, classes, excluded };
|
|
284
|
+
}
|
|
285
|
+
function admittedRange(invariants, carrier) {
|
|
286
|
+
let lower;
|
|
287
|
+
let upper;
|
|
288
|
+
for (const rule of invariants.flatMap(conjuncts)) {
|
|
289
|
+
if (rule.kind !== "compare" || boundTermPath(rule)?.length !== 0) {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const normalized = normalize(rule);
|
|
293
|
+
if (normalized === undefined || normalized.measure !== "value") {
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
const { operator, bound } = normalized;
|
|
297
|
+
if (operator === ">=" || operator === ">") {
|
|
298
|
+
const edge = { value: bound, inclusive: operator === ">=" };
|
|
299
|
+
if (lower === undefined || carrier.compare(bound, lower.value) > 0) {
|
|
300
|
+
lower = edge;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (operator === "<=" || operator === "<") {
|
|
304
|
+
const edge = { value: bound, inclusive: operator === "<=" };
|
|
305
|
+
if (upper === undefined || carrier.compare(bound, upper.value) < 0) {
|
|
306
|
+
upper = edge;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return { lower, upper };
|
|
311
|
+
}
|
|
312
|
+
const asGuard = {
|
|
313
|
+
source: "guard",
|
|
314
|
+
describe: (rule, frames) => describeRule(rule, frames.root.label, labelsOf(frames)),
|
|
315
|
+
};
|
|
316
|
+
function walk(rule, frames, at, reading) {
|
|
317
|
+
if (rule.kind === "and" || rule.kind === "or") {
|
|
318
|
+
return rule.rules.flatMap(part => walk(part, frames, at, reading));
|
|
319
|
+
}
|
|
320
|
+
if (rule.kind === "not") {
|
|
321
|
+
return walk(rule.rule, frames, at, reading);
|
|
322
|
+
}
|
|
323
|
+
if (rule.kind === "all" || rule.kind === "any") {
|
|
324
|
+
const inner = enter(rule, frames);
|
|
325
|
+
return inner === undefined ? [] : walk(rule.each, inner, at, reading);
|
|
326
|
+
}
|
|
327
|
+
if ([rule.left, rule.right].some(operand => isTerm(operand) && positionData(operand) === undefined)) {
|
|
328
|
+
return betweenExpression(rule, frames, at, reading);
|
|
329
|
+
}
|
|
330
|
+
if (isTerm(rule.left) && isTerm(rule.right)) {
|
|
331
|
+
return between(rule, rule.left, rule.right, frames, at, reading);
|
|
332
|
+
}
|
|
333
|
+
const term = isTerm(rule.left) ? rule.left : isTerm(rule.right) ? rule.right : undefined;
|
|
334
|
+
if (term === undefined) {
|
|
335
|
+
return [];
|
|
336
|
+
}
|
|
337
|
+
const { frame, keys } = locate(term, frames);
|
|
338
|
+
const { measure } = positionOf(term);
|
|
339
|
+
const whole = schemaAt(frame.scope, keys);
|
|
340
|
+
if (whole === undefined) {
|
|
341
|
+
return [];
|
|
342
|
+
}
|
|
343
|
+
// A field the cases of a sum share is drawn under each, read as that case declares it.
|
|
344
|
+
return everyStepsOf(frame.scope, keys).flatMap(steps => {
|
|
345
|
+
const schema = schemaAlong(frame.scope, keys, steps) ?? whole;
|
|
346
|
+
const borders = bordersOf([rule], found => carrierOf(schema, found), {
|
|
347
|
+
source: reading.source,
|
|
348
|
+
describe: () => reading.describe(rule, frames),
|
|
349
|
+
admits: coordinate => measure !== "value" ||
|
|
350
|
+
(schema.parse(coordinate).success &&
|
|
351
|
+
inheritedAt(frame.scope, keys, steps).every(inherited => holds(inherited, coordinate))),
|
|
352
|
+
});
|
|
353
|
+
const positionPath = pathOf(frame, keys, steps);
|
|
354
|
+
const positionSegments = [...frame.segments, ...steps];
|
|
355
|
+
return borders.map(border => ({
|
|
356
|
+
path: positionPath,
|
|
357
|
+
segments: positionSegments,
|
|
358
|
+
comparison: rule,
|
|
359
|
+
border,
|
|
360
|
+
cases: casesOnTrail(frame.scope, keys, steps).map(item => ({ at: frame === frames.root ? item.at : [positionOf(term).path[0], ...item.at], discriminant: item.sum.discriminant, tag: item.tag })),
|
|
361
|
+
coordinateOf: (reached) => onTrail(frame.scope, keys, steps, frame === frames.root ? reached.scope : readOperand(positionTerm([positionOf(term).path[0]], "value"), reached.scope)) ? readOperand(term, reached.scope) : undefined,
|
|
362
|
+
compose: (given, coordinate) => {
|
|
363
|
+
const position = at(positionSegments);
|
|
364
|
+
return position === undefined ? undefined : writeExactly(position, given, measure, coordinate);
|
|
365
|
+
},
|
|
366
|
+
}));
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
function betweenExpression(rule, frames, at, reading) {
|
|
370
|
+
const form = differenceOf(rule);
|
|
371
|
+
if (rule.operator === "==" || rule.operator === "!=" || typeof form.constant !== "number") {
|
|
372
|
+
return [];
|
|
373
|
+
}
|
|
374
|
+
// Every trail through the cases of the sums each part passes, the parts under one sum read under the same case of it.
|
|
375
|
+
const options = form.parts.map(part => {
|
|
376
|
+
const term = positionTerm(part.path, part.measure);
|
|
377
|
+
const { frame, keys } = locate(term, frames);
|
|
378
|
+
return everyStepsOf(frame.scope, keys).map(steps => ({
|
|
379
|
+
part, term, frame, keys, steps,
|
|
380
|
+
cases: casesOnTrail(frame.scope, keys, steps).map(item => ({ at: frame === frames.root ? item.at : [positionOf(term).path[0], ...item.at], discriminant: item.sum.discriminant, tag: item.tag })),
|
|
381
|
+
}));
|
|
382
|
+
});
|
|
383
|
+
const agree = (a, b) => a.every(one => b.every(other => one.at.join("\u0000") !== other.at.join("\u0000") || one.tag === other.tag));
|
|
384
|
+
const trails = options.reduce((all, choices) => all.flatMap(prefix => choices.filter(choice => prefix.every(held => agree(held.cases, choice.cases))).map(choice => [...prefix, choice])), [[]]);
|
|
385
|
+
return trails.flatMap(chosen => betweenExpressionOn(rule, form, chosen, frames, at, reading));
|
|
386
|
+
}
|
|
387
|
+
function betweenExpressionOn(rule, form, chosen, frames, at, reading) {
|
|
388
|
+
const parts = chosen.map(({ part, term, frame, keys, steps }) => {
|
|
389
|
+
const schema = schemaAlong(frame.scope, keys, steps) ?? schemaAt(frame.scope, keys);
|
|
390
|
+
const standsIn = frame === frames.root && keys[0] === DEPS;
|
|
391
|
+
return {
|
|
392
|
+
term,
|
|
393
|
+
coefficient: part.coefficient,
|
|
394
|
+
measure: part.measure,
|
|
395
|
+
standsIn,
|
|
396
|
+
path: measureLabel(standsIn ? ["deps", ...keys.slice(1)].join(".") : pathOf(frame, keys, steps), part.measure),
|
|
397
|
+
segments: [...frame.segments, ...steps],
|
|
398
|
+
kind: counts(part.measure) ? "integer" : schema?.kind,
|
|
399
|
+
onTrail: (scope) => onTrail(frame.scope, keys, steps, frame === frames.root ? scope : readOperand(positionTerm([positionOf(term).path[0]], "value"), scope)),
|
|
400
|
+
};
|
|
401
|
+
});
|
|
402
|
+
const cases = chosen.flatMap(item => item.cases).filter((item, index, all) => all.findIndex(other => other.at.join("\u0000") === item.at.join("\u0000")) === index);
|
|
403
|
+
const carrier = parts.every(part => part.kind === "integer")
|
|
404
|
+
? integerCarrier
|
|
405
|
+
: parts.every(part => part.kind === "integer" || part.kind === "number")
|
|
406
|
+
? numberCarrier
|
|
407
|
+
: undefined;
|
|
408
|
+
if (carrier === undefined || parts.length === 0) {
|
|
409
|
+
return [];
|
|
410
|
+
}
|
|
411
|
+
const constant = form.constant;
|
|
412
|
+
const difference = {
|
|
413
|
+
...(rule.name === undefined ? {} : { name: rule.name }),
|
|
414
|
+
kind: "compare",
|
|
415
|
+
operator: rule.operator,
|
|
416
|
+
left: selfTerm(),
|
|
417
|
+
right: 0,
|
|
418
|
+
};
|
|
419
|
+
const borders = bordersOf([difference], () => carrier, {
|
|
420
|
+
source: reading.source,
|
|
421
|
+
describe: () => reading.describe(rule, frames),
|
|
422
|
+
admits: () => true,
|
|
423
|
+
});
|
|
424
|
+
const valueOf = (part, scope) => {
|
|
425
|
+
const value = readOperand(part.term, scope);
|
|
426
|
+
return value === undefined ? undefined : value;
|
|
427
|
+
};
|
|
428
|
+
const written = parts
|
|
429
|
+
.map((part, index) => {
|
|
430
|
+
const size = Math.abs(part.coefficient);
|
|
431
|
+
const body = size === 1 ? part.path : `${size} * ${part.path}`;
|
|
432
|
+
return index === 0 ? (part.coefficient < 0 ? `−${body}` : body) : `${part.coefficient < 0 ? "−" : "+"} ${body}`;
|
|
433
|
+
})
|
|
434
|
+
.join(" ");
|
|
435
|
+
const path = constant === 0 ? written : `${written} ${constant < 0 ? "−" : "+"} ${Math.abs(constant)}`;
|
|
436
|
+
// Each part weighed by one can be moved onto any value, and the first the input
|
|
437
|
+
// takes is written: an earlier one may be held to a bound of its own.
|
|
438
|
+
const movable = parts.filter(part => !part.standsIn && Math.abs(part.coefficient) === 1);
|
|
439
|
+
const weighed = parts.length > 1 && parts.every(part => !part.standsIn)
|
|
440
|
+
? {
|
|
441
|
+
parts: parts.map(part => ({
|
|
442
|
+
path: part.path,
|
|
443
|
+
coefficient: part.coefficient,
|
|
444
|
+
valueOf: scope => valueOf(part, scope),
|
|
445
|
+
write: (given, value) => at(part.segments)?.write(given, part.measure, value),
|
|
446
|
+
})),
|
|
447
|
+
}
|
|
448
|
+
: undefined;
|
|
449
|
+
return borders.map(border => ({
|
|
450
|
+
path,
|
|
451
|
+
segments: (parts.find(part => !part.standsIn) ?? parts[0]).segments,
|
|
452
|
+
reads: parts.filter(part => !part.standsIn).map(part => part.segments),
|
|
453
|
+
comparison: rule,
|
|
454
|
+
border,
|
|
455
|
+
...(weighed === undefined ? {} : { form: weighed }),
|
|
456
|
+
cases,
|
|
457
|
+
coordinateOf: reached => {
|
|
458
|
+
if (!parts.every(part => part.onTrail(reached.scope)))
|
|
459
|
+
return undefined;
|
|
460
|
+
let total = constant;
|
|
461
|
+
for (const part of parts) {
|
|
462
|
+
const value = valueOf(part, reached.scope);
|
|
463
|
+
if (value === undefined) {
|
|
464
|
+
return undefined;
|
|
465
|
+
}
|
|
466
|
+
total += part.coefficient * value;
|
|
467
|
+
}
|
|
468
|
+
return total;
|
|
469
|
+
},
|
|
470
|
+
sides: movable.length,
|
|
471
|
+
compose: (given, coordinate, deps, side) => {
|
|
472
|
+
const moving = movable[side ?? 0];
|
|
473
|
+
const moved = moving === undefined ? undefined : at(moving.segments);
|
|
474
|
+
if (moving === undefined || moved === undefined) {
|
|
475
|
+
return undefined;
|
|
476
|
+
}
|
|
477
|
+
const partValue = (part, scope) => {
|
|
478
|
+
const value = part.standsIn
|
|
479
|
+
? readOperand(part.term, withDeps({}, deps))
|
|
480
|
+
: at(part.segments)?.valuesIn(scope)[0];
|
|
481
|
+
return value === undefined || part.standsIn || !counts(part.measure) ? value : measured(value, part.measure);
|
|
482
|
+
};
|
|
483
|
+
let rest = constant;
|
|
484
|
+
for (const part of parts) {
|
|
485
|
+
if (part === moving) {
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
const value = partValue(part, given);
|
|
489
|
+
if (value === undefined) {
|
|
490
|
+
return undefined;
|
|
491
|
+
}
|
|
492
|
+
rest += part.coefficient * value;
|
|
493
|
+
}
|
|
494
|
+
const written = moved.write(given, moving.measure, (coordinate - rest) / moving.coefficient);
|
|
495
|
+
// Moving a part that another reads too, as `length(trim($.code))` reads
|
|
496
|
+
// `$.code`, moves both, so the row is kept only where it stands at the point.
|
|
497
|
+
const shared = parts.some(part => part !== moving && !part.standsIn && deepEqual(part.segments, moving.segments));
|
|
498
|
+
return !shared ||
|
|
499
|
+
parts.reduce((total, part) => {
|
|
500
|
+
const value = partValue(part, written);
|
|
501
|
+
return total === undefined || value === undefined ? undefined : total + part.coefficient * value;
|
|
502
|
+
}, constant) === coordinate
|
|
503
|
+
? written
|
|
504
|
+
: undefined;
|
|
505
|
+
},
|
|
506
|
+
}));
|
|
507
|
+
}
|
|
508
|
+
function between(rule, left, right, frames, at, reading) {
|
|
509
|
+
const options = [left, right].map(term => {
|
|
510
|
+
const { frame, keys } = locate(term, frames);
|
|
511
|
+
return everyStepsOf(frame.scope, keys).map((steps) => ({
|
|
512
|
+
term, frame, keys, steps,
|
|
513
|
+
cases: casesOnTrail(frame.scope, keys, steps).map(item => ({ at: frame === frames.root ? item.at : [positionOf(term).path[0], ...item.at], discriminant: item.sum.discriminant, tag: item.tag })),
|
|
514
|
+
}));
|
|
515
|
+
});
|
|
516
|
+
// Two sides under one sum are read under the same case of it.
|
|
517
|
+
const compatible = (a, b) => a.cases.every(one => b.cases.every(other => one.at.join("\u0000") !== other.at.join("\u0000") || one.tag === other.tag));
|
|
518
|
+
return options[0].flatMap(a => options[1].filter(b => compatible(a, b)).flatMap(b => betweenOn(rule, [a, b], frames, at, reading)));
|
|
519
|
+
}
|
|
520
|
+
function betweenOn(rule, options, frames, at, reading) {
|
|
521
|
+
const sides = options.map(({ term, frame, keys, steps }) => {
|
|
522
|
+
const { measure } = positionOf(term);
|
|
523
|
+
const schema = schemaAlong(frame.scope, keys, steps) ?? schemaAt(frame.scope, keys);
|
|
524
|
+
const standsIn = frame === frames.root && keys[0] === DEPS;
|
|
525
|
+
return {
|
|
526
|
+
term,
|
|
527
|
+
measure,
|
|
528
|
+
standsIn,
|
|
529
|
+
path: measureLabel(standsIn ? ["deps", ...keys.slice(1)].join(".") : pathOf(frame, keys, steps), measure),
|
|
530
|
+
segments: [...frame.segments, ...steps],
|
|
531
|
+
kind: counts(measure) ? "integer" : schema?.kind,
|
|
532
|
+
scale: measure === "value" && schema?.kind === "decimal" ? schema.scale : 0,
|
|
533
|
+
onTrail: (scope) => onTrail(frame.scope, keys, steps, frame === frames.root ? scope : readOperand(positionTerm([positionOf(term).path[0]], "value"), scope)),
|
|
534
|
+
};
|
|
535
|
+
});
|
|
536
|
+
const [first, second] = sides;
|
|
537
|
+
const carrier = differenceCarrier(first, second);
|
|
538
|
+
if (carrier === undefined || rule.operator === "==" || rule.operator === "!=") {
|
|
539
|
+
return [];
|
|
540
|
+
}
|
|
541
|
+
const moment = first.kind === "decimal" || second.kind === "decimal"
|
|
542
|
+
? decimalMoment(first, second)
|
|
543
|
+
: first.kind === second.kind
|
|
544
|
+
? MOMENTS[first.kind ?? ""]
|
|
545
|
+
: undefined;
|
|
546
|
+
const difference = {
|
|
547
|
+
...(rule.name === undefined ? {} : { name: rule.name }),
|
|
548
|
+
kind: "compare",
|
|
549
|
+
operator: rule.operator,
|
|
550
|
+
left: selfTerm(),
|
|
551
|
+
right: carrier === nanosecondCarrier || first.kind === "decimal" || second.kind === "decimal" ? 0n : 0,
|
|
552
|
+
};
|
|
553
|
+
const borders = bordersOf([difference], () => carrier, {
|
|
554
|
+
source: reading.source,
|
|
555
|
+
describe: () => reading.describe(rule, frames),
|
|
556
|
+
admits: () => true,
|
|
557
|
+
});
|
|
558
|
+
const read = (side, scopeValue) => {
|
|
559
|
+
const value = readOperand(side.term, scopeValue);
|
|
560
|
+
if (value === undefined) {
|
|
561
|
+
return undefined;
|
|
562
|
+
}
|
|
563
|
+
return moment === undefined ? value : moment.read(value, side);
|
|
564
|
+
};
|
|
565
|
+
const form = sides.every(side => !side.standsIn && (side.kind === "integer" || side.kind === "number"))
|
|
566
|
+
? {
|
|
567
|
+
parts: sides.map((side, index) => ({
|
|
568
|
+
path: side.path,
|
|
569
|
+
coefficient: index === 0 ? 1 : -1,
|
|
570
|
+
valueOf: scope => read(side, scope),
|
|
571
|
+
write: (given, value) => at(side.segments)?.write(given, side.measure, value),
|
|
572
|
+
})),
|
|
573
|
+
}
|
|
574
|
+
: undefined;
|
|
575
|
+
return borders.map(border => ({
|
|
576
|
+
path: `${first.path} − ${second.path}`,
|
|
577
|
+
segments: (first.standsIn ? second : first).segments,
|
|
578
|
+
reads: sides.filter(side => !side.standsIn).map(side => side.segments),
|
|
579
|
+
comparison: rule,
|
|
580
|
+
border,
|
|
581
|
+
...(form === undefined ? {} : { form }),
|
|
582
|
+
cases: [...options[0].cases, ...options[1].cases.filter(other => !options[0].cases.some(one => one.at.join("\u0000") === other.at.join("\u0000")))],
|
|
583
|
+
coordinateOf: reached => {
|
|
584
|
+
if (!first.onTrail(reached.scope) || !second.onTrail(reached.scope))
|
|
585
|
+
return undefined;
|
|
586
|
+
const a = read(first, reached.scope);
|
|
587
|
+
const b = read(second, reached.scope);
|
|
588
|
+
return a === undefined || b === undefined ? undefined : a - b;
|
|
589
|
+
},
|
|
590
|
+
compose: (given, coordinate, deps, side) => {
|
|
591
|
+
const otherSide = side === 1;
|
|
592
|
+
// Move the side that can take every value of the difference: the finer of
|
|
593
|
+
// a decimal and an integer, and never a stand-in, which a row writes as given.
|
|
594
|
+
const preferred = first.standsIn || (!second.standsIn && second.scale > first.scale);
|
|
595
|
+
const swappable = !first.standsIn && !second.standsIn && first.scale === second.scale;
|
|
596
|
+
const moveSecond = otherSide === true && swappable ? !preferred : preferred;
|
|
597
|
+
const [moving, fixed, sign] = moveSecond ? [second, first, -1] : [first, second, 1];
|
|
598
|
+
const moved = moving.standsIn ? undefined : at(moving.segments);
|
|
599
|
+
const other = fixed.standsIn
|
|
600
|
+
? readOperand(fixed.term, withDeps({}, deps))
|
|
601
|
+
: at(fixed.segments)?.valuesIn(given)[0];
|
|
602
|
+
if (moved === undefined || other === undefined) {
|
|
603
|
+
return undefined;
|
|
604
|
+
}
|
|
605
|
+
if (moment?.write !== undefined) {
|
|
606
|
+
const units = moment.read(other, fixed) + BigInt(sign) * coordinate;
|
|
607
|
+
const written = moment.write(units, moving, other);
|
|
608
|
+
return written === undefined ? undefined : moved.write(given, moving.measure, written);
|
|
609
|
+
}
|
|
610
|
+
if (moment !== undefined) {
|
|
611
|
+
const amount = sign * Number(coordinate);
|
|
612
|
+
const shifted = moment.shift(other, amount);
|
|
613
|
+
if (shifted !== undefined) {
|
|
614
|
+
return moved.write(given, moving.measure, shifted);
|
|
615
|
+
}
|
|
616
|
+
// Moving this side would carry it past an end of the day, so the other
|
|
617
|
+
// side is moved against the value this one already holds instead.
|
|
618
|
+
const held = fixed.standsIn ? undefined : at(fixed.segments);
|
|
619
|
+
const current = moved.valuesIn(given)[0];
|
|
620
|
+
const counter = current === undefined ? undefined : moment.shift(current, -amount);
|
|
621
|
+
return held === undefined || counter === undefined
|
|
622
|
+
? undefined
|
|
623
|
+
: held.write(given, fixed.measure, counter);
|
|
624
|
+
}
|
|
625
|
+
const base = counts(fixed.measure) && !fixed.standsIn ? measured(other, fixed.measure) : other;
|
|
626
|
+
const written = moved.write(given, moving.measure, base + sign * coordinate);
|
|
627
|
+
// Moving one side of `length(trim($.code)) < length($.code)` moves the
|
|
628
|
+
// other too, so the row is kept only where it stands at the point.
|
|
629
|
+
if (fixed.standsIn || !deepEqual(fixed.segments, moving.segments)) {
|
|
630
|
+
return written;
|
|
631
|
+
}
|
|
632
|
+
const [a, b] = [first, second].map(side => measured(at(side.segments)?.valuesIn(written)[0], side.measure));
|
|
633
|
+
return typeof a === "number" && typeof b === "number" && a - b === coordinate ? written : undefined;
|
|
634
|
+
},
|
|
635
|
+
}));
|
|
636
|
+
}
|
|
637
|
+
// Reads two decimals as whole numbers of units of the finer scale of the two, so
|
|
638
|
+
// their difference is exact, and writes one back where the other side's value
|
|
639
|
+
// plus the difference lands on its own scale.
|
|
640
|
+
function decimalMoment(first, second) {
|
|
641
|
+
const scale = Math.max(first.scale, second.scale);
|
|
642
|
+
return {
|
|
643
|
+
read: value => decimalUnits(value, scale, "floor"),
|
|
644
|
+
shift: () => undefined,
|
|
645
|
+
write: (units, side, like) => {
|
|
646
|
+
const coarser = 10n ** BigInt(scale - side.scale);
|
|
647
|
+
return units % coarser === 0n ? decimalOfUnits(like, units / coarser, side.scale) : undefined;
|
|
648
|
+
},
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
const MOMENTS = {
|
|
652
|
+
instant: {
|
|
653
|
+
read: value => value.epochNanoseconds,
|
|
654
|
+
shift: (value, amount) => temporalNeighbor(value, { nanoseconds: amount }),
|
|
655
|
+
},
|
|
656
|
+
date: {
|
|
657
|
+
// Counted in the ISO calendar, since until refuses two dates in different
|
|
658
|
+
// calendars and a date in any calendar is one the schema accepts.
|
|
659
|
+
read: value => value.constructor
|
|
660
|
+
.from("1970-01-01")
|
|
661
|
+
.until(value.withCalendar("iso8601"), { largestUnit: "days" })
|
|
662
|
+
.total("days"),
|
|
663
|
+
shift: (value, amount) => temporalNeighbor(value, { days: amount }),
|
|
664
|
+
},
|
|
665
|
+
datetime: {
|
|
666
|
+
// A date-time can lie beyond the instant range, so it is not read through a UTC instant.
|
|
667
|
+
read: value => timelineOf(value).numerator,
|
|
668
|
+
shift: (value, amount) => temporalNeighbor(value, { nanoseconds: amount }),
|
|
669
|
+
},
|
|
670
|
+
time: {
|
|
671
|
+
read: value => BigInt(value.constructor.from("00:00").until(value, { largestUnit: "hours" }).total("nanoseconds")),
|
|
672
|
+
shift: (value, amount) => {
|
|
673
|
+
const shifted = value.add({ nanoseconds: amount });
|
|
674
|
+
const order = value.constructor.compare(shifted, value);
|
|
675
|
+
return Math.sign(order) === Math.sign(amount) ? shifted : undefined;
|
|
676
|
+
},
|
|
677
|
+
},
|
|
678
|
+
};
|
|
679
|
+
function differenceCarrier({ kind: first, scale: firstScale }, { kind: second, scale: secondScale }) {
|
|
680
|
+
const numeric = (kind) => kind === "integer" || kind === "number";
|
|
681
|
+
if (first === "integer" && second === "integer") {
|
|
682
|
+
return integerCarrier;
|
|
683
|
+
}
|
|
684
|
+
// Two decimals count their difference in units of the finer scale; a decimal
|
|
685
|
+
// is not ordered against any other kind.
|
|
686
|
+
if (first === "decimal" && second === "decimal") {
|
|
687
|
+
return decimalUnitsCarrier(Math.max(firstScale, secondScale));
|
|
688
|
+
}
|
|
689
|
+
if (first === "decimal" || second === "decimal") {
|
|
690
|
+
return undefined;
|
|
691
|
+
}
|
|
692
|
+
if (numeric(first) && numeric(second)) {
|
|
693
|
+
return numberCarrier;
|
|
694
|
+
}
|
|
695
|
+
if (first !== second) {
|
|
696
|
+
return undefined;
|
|
697
|
+
}
|
|
698
|
+
return first === "date" ? integerCarrier : first === "instant" || first === "datetime" || first === "time" ? nanosecondCarrier : undefined;
|
|
699
|
+
}
|
|
700
|
+
export function comparisonsNotReadOf(implementation) {
|
|
701
|
+
const input = implementation.behavior.input;
|
|
702
|
+
return Object.entries(implementation.cases).flatMap(([tag, decision]) => decision.kind !== "rules"
|
|
703
|
+
? []
|
|
704
|
+
: decision.guards.flatMap(candidate => unreadIn(candidate.condition, framesFor(implementation.behavior, tag, decision)).map(text => `${decision.id}: ${text}`)));
|
|
705
|
+
}
|
|
706
|
+
function unreadIn(rule, frames) {
|
|
707
|
+
switch (rule.kind) {
|
|
708
|
+
case "and":
|
|
709
|
+
case "or":
|
|
710
|
+
return rule.rules.flatMap(part => unreadIn(part, frames));
|
|
711
|
+
case "not":
|
|
712
|
+
return unreadIn(rule.rule, frames);
|
|
713
|
+
case "all":
|
|
714
|
+
case "any": {
|
|
715
|
+
const inner = enter(rule, frames);
|
|
716
|
+
return inner === undefined
|
|
717
|
+
? [describeRule(rule, frames.root.label, labelsOf(frames))]
|
|
718
|
+
: unreadIn(rule.each, inner);
|
|
719
|
+
}
|
|
720
|
+
case "compare": {
|
|
721
|
+
const terms = [rule.left, rule.right].filter(isTerm);
|
|
722
|
+
// Two constants settle without reading anything.
|
|
723
|
+
if (terms.length === 0)
|
|
724
|
+
return [];
|
|
725
|
+
if (terms.some(term => positionData(term) === undefined)) {
|
|
726
|
+
const kinds = differenceOf(rule).parts.map(part => {
|
|
727
|
+
const { frame, keys } = locate(positionTerm(part.path, part.measure), frames);
|
|
728
|
+
return counts(part.measure) ? "integer" : schemaAt(frame.scope, keys)?.kind;
|
|
729
|
+
});
|
|
730
|
+
return kinds.every(kind => kind === "integer" || kind === "number")
|
|
731
|
+
? []
|
|
732
|
+
: [describeRule(rule, frames.root.label, labelsOf(frames))];
|
|
733
|
+
}
|
|
734
|
+
const schemas = terms.map(term => {
|
|
735
|
+
const { frame, keys } = locate(term, frames);
|
|
736
|
+
const schema = schemaAt(frame.scope, keys);
|
|
737
|
+
// The length of an enum is not read: see carrierOf.
|
|
738
|
+
return counts(positionOf(term).measure) ? (schema?.kind === "enum" ? undefined : int()) : schema;
|
|
739
|
+
});
|
|
740
|
+
const sides = schemas.map((schema) => ({
|
|
741
|
+
kind: schema?.kind,
|
|
742
|
+
scale: schema?.kind === "decimal" ? schema.scale : 0,
|
|
743
|
+
}));
|
|
744
|
+
const kind = schemas[0]?.kind;
|
|
745
|
+
const readable = terms.length === 2
|
|
746
|
+
? rule.operator === "==" ||
|
|
747
|
+
rule.operator === "!=" ||
|
|
748
|
+
differenceCarrier(sides[0], sides[1]) !== undefined
|
|
749
|
+
: kind === "boolean" ||
|
|
750
|
+
// An enum's values are ordered by their text, not as they were named,
|
|
751
|
+
// so only an equality with one of them is read.
|
|
752
|
+
(kind === "enum" && (rule.operator === "==" || rule.operator === "!=")) ||
|
|
753
|
+
kind === "variants" ||
|
|
754
|
+
kind === "literal" ||
|
|
755
|
+
(schemas[0] !== undefined && carrierOf(schemas[0], "value") !== undefined);
|
|
756
|
+
return readable ? [] : [describeRule(rule, frames.root.label, labelsOf(frames))];
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
//# sourceMappingURL=guard-borders.js.map
|