@toiroakr/chisel 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +354 -21
- package/dist/absence.d.ts +28 -0
- package/dist/absence.js +266 -0
- package/dist/absence.js.map +1 -0
- package/dist/behavior.d.ts +26 -19
- package/dist/behavior.js +193 -203
- package/dist/behavior.js.map +1 -1
- package/dist/beside.d.ts +5 -0
- package/dist/beside.js +10 -0
- package/dist/beside.js.map +1 -1
- package/dist/border.d.ts +7 -8
- package/dist/border.js +88 -36
- package/dist/border.js.map +1 -1
- package/dist/bounds.d.ts +1 -0
- package/dist/bounds.js +27 -0
- package/dist/bounds.js.map +1 -0
- package/dist/changes.d.ts +23 -0
- package/dist/changes.js +355 -0
- package/dist/changes.js.map +1 -0
- package/dist/checks.d.ts +1 -0
- package/dist/checks.js +295 -0
- package/dist/checks.js.map +1 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +351 -14
- package/dist/cli.js.map +1 -1
- package/dist/codec.js +16 -7
- package/dist/codec.js.map +1 -1
- package/dist/codegen.js +6 -8
- package/dist/codegen.js.map +1 -1
- package/dist/composition.d.ts +3 -3
- package/dist/composition.js +9 -8
- package/dist/composition.js.map +1 -1
- package/dist/conformance.d.ts +24 -0
- package/dist/conformance.js +44 -0
- package/dist/conformance.js.map +1 -0
- package/dist/data.d.ts +3 -2
- package/dist/data.js +7 -3
- package/dist/data.js.map +1 -1
- package/dist/decimal-js.d.ts +2 -0
- package/dist/decimal-js.js +5 -0
- package/dist/decimal-js.js.map +1 -0
- package/dist/decimal.d.ts +2 -0
- package/dist/decimal.js +3 -0
- package/dist/decimal.js.map +1 -0
- package/dist/dependency.d.ts +9 -6
- package/dist/dependency.js +23 -17
- package/dist/dependency.js.map +1 -1
- package/dist/domain.d.ts +6 -2
- package/dist/domain.js +65 -25
- package/dist/domain.js.map +1 -1
- package/dist/ensures.js +3 -3
- package/dist/ensures.js.map +1 -1
- package/dist/equal.d.ts +1 -0
- package/dist/equal.js +135 -0
- package/dist/equal.js.map +1 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +25 -0
- package/dist/errors.js.map +1 -0
- package/dist/exact.d.ts +13 -1
- package/dist/exact.js +52 -3
- package/dist/exact.js.map +1 -1
- package/dist/feasibility.d.ts +5 -3
- package/dist/feasibility.js +309 -53
- package/dist/feasibility.js.map +1 -1
- package/dist/guard-borders.d.ts +14 -5
- package/dist/guard-borders.js +229 -190
- package/dist/guard-borders.js.map +1 -1
- package/dist/guard-scope.d.ts +65 -0
- package/dist/guard-scope.js +384 -0
- package/dist/guard-scope.js.map +1 -0
- package/dist/hooks.d.ts +22 -0
- package/dist/hooks.js +2 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +30 -8
- package/dist/index.js +18 -5
- package/dist/index.js.map +1 -1
- 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 +8 -0
- package/dist/interval.js +213 -23
- package/dist/interval.js.map +1 -1
- package/dist/isolated.d.ts +7 -1
- package/dist/isolated.js +53 -5
- package/dist/isolated.js.map +1 -1
- package/dist/kinds/decimal.d.ts +1 -0
- package/dist/kinds/decimal.js +16 -0
- package/dist/kinds/decimal.js.map +1 -0
- package/dist/kinds/rational.d.ts +1 -0
- package/dist/kinds/rational.js +13 -0
- package/dist/kinds/rational.js.map +1 -0
- package/dist/kinds/temporal.d.ts +1 -0
- package/dist/kinds/temporal.js +15 -0
- package/dist/kinds/temporal.js.map +1 -0
- package/dist/lean/behavior.d.ts +29 -0
- package/dist/lean/behavior.js +76 -0
- package/dist/lean/behavior.js.map +1 -0
- package/dist/lean/given.d.ts +10 -0
- package/dist/lean/given.js +15 -0
- package/dist/lean/given.js.map +1 -0
- package/dist/lean/index.d.ts +11 -0
- package/dist/lean/index.js +8 -0
- package/dist/lean/index.js.map +1 -0
- package/dist/lean/model.d.ts +20 -0
- package/dist/lean/model.js +133 -0
- package/dist/lean/model.js.map +1 -0
- package/dist/lean/protocol.d.ts +14 -0
- package/dist/lean/protocol.js +34 -0
- package/dist/lean/protocol.js.map +1 -0
- package/dist/lean/rule.d.ts +54 -0
- package/dist/lean/rule.js +149 -0
- package/dist/lean/rule.js.map +1 -0
- package/dist/lean/schema.d.ts +46 -0
- package/dist/lean/schema.js +155 -0
- package/dist/lean/schema.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 +122 -0
- package/dist/machine.js +271 -0
- package/dist/machine.js.map +1 -0
- package/dist/model.d.ts +110 -11
- package/dist/model.js +724 -80
- package/dist/model.js.map +1 -1
- package/dist/off-grid.d.ts +4 -0
- package/dist/off-grid.js +92 -0
- package/dist/off-grid.js.map +1 -0
- package/dist/outcomes.d.ts +15 -0
- package/dist/outcomes.js +120 -0
- package/dist/outcomes.js.map +1 -0
- package/dist/pairs.js +14 -5
- package/dist/pairs.js.map +1 -1
- package/dist/partition.d.ts +1 -0
- package/dist/partition.js +101 -19
- package/dist/partition.js.map +1 -1
- package/dist/placeholders.d.ts +3 -0
- package/dist/placeholders.js +44 -0
- package/dist/placeholders.js.map +1 -0
- package/dist/proof.d.ts +6 -0
- package/dist/proof.js +282 -56
- package/dist/proof.js.map +1 -1
- package/dist/protocol.d.ts +22 -0
- package/dist/protocol.js +12 -0
- package/dist/protocol.js.map +1 -0
- package/dist/relations.d.ts +30 -0
- package/dist/relations.js +235 -0
- package/dist/relations.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/rewrites.d.ts +41 -0
- package/dist/rewrites.js +180 -0
- package/dist/rewrites.js.map +1 -0
- package/dist/rule.d.ts +74 -11
- package/dist/rule.js +429 -22
- package/dist/rule.js.map +1 -1
- 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 +44 -28
- package/dist/schema.js +50 -113
- package/dist/schema.js.map +1 -1
- package/dist/specification.d.ts +23 -3
- package/dist/specification.js +1001 -96
- package/dist/specification.js.map +1 -1
- package/dist/stops.d.ts +20 -0
- package/dist/stops.js +221 -0
- package/dist/stops.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/tla-validate.d.ts +31 -0
- package/dist/tla-validate.js +151 -0
- package/dist/tla-validate.js.map +1 -0
- package/dist/tla.d.ts +61 -0
- package/dist/tla.js +722 -0
- package/dist/tla.js.map +1 -0
- package/dist/verification.d.ts +9 -0
- package/dist/verification.js +7 -0
- package/dist/verification.js.map +1 -0
- package/dist/ways.d.ts +11 -5
- package/dist/ways.js +51 -45
- package/dist/ways.js.map +1 -1
- package/dist/world.d.ts +98 -0
- package/dist/world.js +463 -0
- package/dist/world.js.map +1 -0
- package/package.json +49 -10
- package/schema/report.schema.json +169 -1
package/dist/rule.js
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import { Rational, isRational, fractionOf, decimalOf } from "./exact.js";
|
|
2
2
|
import { compareText } from "./text.js";
|
|
3
|
-
import {
|
|
3
|
+
import { deepEqual } from "./equal.js";
|
|
4
|
+
// The latest time, in milliseconds either side of 1970-01-01, that a Date holds.
|
|
5
|
+
export const MAX_DATE_TIME = 8.64e15;
|
|
4
6
|
// Symbol.for, not Symbol(): the CLI loads spec files through tsImport in a
|
|
5
7
|
// separate module graph, and a per-module symbol would not recognise their terms.
|
|
6
8
|
const TERM = Symbol.for("chisel.term");
|
|
9
|
+
export function measureOf(transforms, reading) {
|
|
10
|
+
return transforms.length === 0 ? reading : `${transforms.join(",")}:${reading}`;
|
|
11
|
+
}
|
|
12
|
+
export function partsOfMeasure(measure) {
|
|
13
|
+
const colon = measure.lastIndexOf(":");
|
|
14
|
+
return colon === -1
|
|
15
|
+
? { transforms: [], reading: measure }
|
|
16
|
+
: { transforms: measure.slice(0, colon).split(","), reading: measure.slice(colon + 1) };
|
|
17
|
+
}
|
|
7
18
|
// Each part of a named invariant keeps its name.
|
|
8
19
|
export function conjuncts(rule) {
|
|
9
20
|
if (rule.kind !== "and") {
|
|
@@ -71,6 +82,8 @@ export function termPaths(rule) {
|
|
|
71
82
|
switch (rule.kind) {
|
|
72
83
|
case "compare":
|
|
73
84
|
return [rule.left, rule.right].filter(isTerm).flatMap(term => pathsOf(termData(term)));
|
|
85
|
+
case "relation":
|
|
86
|
+
return [rule.subject, rule.other].filter(isTerm).flatMap(term => pathsOf(termData(term)));
|
|
74
87
|
case "all":
|
|
75
88
|
case "any":
|
|
76
89
|
return [positionOf(rule.of).path];
|
|
@@ -81,6 +94,23 @@ export function termPaths(rule) {
|
|
|
81
94
|
return termPaths(rule.rule);
|
|
82
95
|
}
|
|
83
96
|
}
|
|
97
|
+
// Every path the rule reads, inside a quantifier's condition too, less those
|
|
98
|
+
// of the element it ranges over.
|
|
99
|
+
export function pathsRead(rule) {
|
|
100
|
+
switch (rule.kind) {
|
|
101
|
+
case "compare":
|
|
102
|
+
case "relation":
|
|
103
|
+
return termPaths(rule);
|
|
104
|
+
case "all":
|
|
105
|
+
case "any":
|
|
106
|
+
return [positionOf(rule.of).path, ...pathsRead(rule.each).filter(path => path[0] !== rule.element)];
|
|
107
|
+
case "and":
|
|
108
|
+
case "or":
|
|
109
|
+
return rule.rules.flatMap(pathsRead);
|
|
110
|
+
case "not":
|
|
111
|
+
return pathsRead(rule.rule);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
84
114
|
export function rootsRead(rule) {
|
|
85
115
|
const roots = new Set(termPaths(rule).map(path => path[0]));
|
|
86
116
|
if (rule.kind === "all" || rule.kind === "any") {
|
|
@@ -121,6 +151,13 @@ export function shiftTerms(rule) {
|
|
|
121
151
|
return { ...rule, left: shift(rule.left), right: shift(rule.right) };
|
|
122
152
|
}
|
|
123
153
|
export function stepInto(rule, key) {
|
|
154
|
+
// A relation of a field to a written value is one of the field itself.
|
|
155
|
+
if (rule.kind === "relation") {
|
|
156
|
+
const subject = positionData(rule.subject);
|
|
157
|
+
if (subject === undefined || isTerm(rule.other) || subject.path[0] !== key)
|
|
158
|
+
return undefined;
|
|
159
|
+
return { ...rule, subject: shiftedTerm(termData(rule.subject)) };
|
|
160
|
+
}
|
|
124
161
|
const path = boundTermPath(rule);
|
|
125
162
|
if (rule.kind !== "compare" || path === undefined || path[0] !== key) {
|
|
126
163
|
return undefined;
|
|
@@ -153,6 +190,45 @@ export function holds(rule, value, observe, decide) {
|
|
|
153
190
|
decide?.(rule, outcome);
|
|
154
191
|
return outcome;
|
|
155
192
|
}
|
|
193
|
+
case "relation": {
|
|
194
|
+
const outcome = relates(rule, value);
|
|
195
|
+
decide?.(rule, outcome);
|
|
196
|
+
return outcome;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
// Read as a comparison is: where either side is absent it holds, since the
|
|
201
|
+
// analyses read rules over partial values and no declared rule reaches one.
|
|
202
|
+
export function relates(rule, value) {
|
|
203
|
+
const subject = read(rule.subject, value);
|
|
204
|
+
const other = read(rule.other, value);
|
|
205
|
+
if (subject === undefined || other === undefined) {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
return relationHolds(rule.relation, subject, other);
|
|
209
|
+
}
|
|
210
|
+
// One element as $includes reads it: decimals by their value, however written,
|
|
211
|
+
// and numbers as $eq and Array.prototype.includes read them (0 and -0 one value),
|
|
212
|
+
// anything else as deepEqual does.
|
|
213
|
+
export function sameElement(left, right) {
|
|
214
|
+
if (isDecimal(left) && isDecimal(right))
|
|
215
|
+
return left.equals(right);
|
|
216
|
+
if (typeof left === "number" && typeof right === "number")
|
|
217
|
+
return left === right || (Number.isNaN(left) && Number.isNaN(right));
|
|
218
|
+
return deepEqual(left, right);
|
|
219
|
+
}
|
|
220
|
+
export function relationHolds(relation, subject, other) {
|
|
221
|
+
switch (relation) {
|
|
222
|
+
case "startsWith":
|
|
223
|
+
return typeof subject === "string" && typeof other === "string" && subject.startsWith(other);
|
|
224
|
+
case "endsWith":
|
|
225
|
+
return typeof subject === "string" && typeof other === "string" && subject.endsWith(other);
|
|
226
|
+
case "contains":
|
|
227
|
+
return typeof subject === "string" && typeof other === "string" && subject.includes(other);
|
|
228
|
+
case "hasKey":
|
|
229
|
+
return typeof subject === "object" && subject !== null && !Array.isArray(subject) && typeof other === "string" && Object.hasOwn(subject, other);
|
|
230
|
+
case "includes":
|
|
231
|
+
return Array.isArray(subject) && subject.some(element => sameElement(element, other));
|
|
156
232
|
}
|
|
157
233
|
}
|
|
158
234
|
function compares(rule, value, observe) {
|
|
@@ -163,6 +239,8 @@ function compares(rule, value, observe) {
|
|
|
163
239
|
return true;
|
|
164
240
|
}
|
|
165
241
|
const order = ordering(left, right);
|
|
242
|
+
// Two objects, arrays or nulls have no order, and are equal when they hold the same, as `deepEqual` reads them: a sum of two such values compared with == is the same on both sides of the TLA+ translation.
|
|
243
|
+
const alike = order === 0 || (Number.isNaN(order) && typeof left === "object" && typeof right === "object" && deepEqual(left, right));
|
|
166
244
|
switch (rule.operator) {
|
|
167
245
|
case "<":
|
|
168
246
|
return order < 0;
|
|
@@ -173,9 +251,9 @@ function compares(rule, value, observe) {
|
|
|
173
251
|
case ">=":
|
|
174
252
|
return order >= 0;
|
|
175
253
|
case "==":
|
|
176
|
-
return
|
|
254
|
+
return alike;
|
|
177
255
|
case "!=":
|
|
178
|
-
return
|
|
256
|
+
return !alike;
|
|
179
257
|
}
|
|
180
258
|
}
|
|
181
259
|
// `stepAt` names the step of the value a term reads, where it is not the default
|
|
@@ -199,10 +277,34 @@ export function satisfy(rule, value, stepAt) {
|
|
|
199
277
|
}
|
|
200
278
|
case "compare":
|
|
201
279
|
return satisfyComparison(rule, value, stepAt);
|
|
280
|
+
case "relation":
|
|
281
|
+
return satisfyRelation(rule, value);
|
|
202
282
|
default:
|
|
203
283
|
return value;
|
|
204
284
|
}
|
|
205
285
|
}
|
|
286
|
+
// A placeholder made to keep a relation to a written value: the value is written into the string, the record or the array, and where the container holds nothing to give it the shape of an entry, it is left as it was.
|
|
287
|
+
function satisfyRelation(rule, value) {
|
|
288
|
+
const position = isTerm(rule.other) ? undefined : positionData(rule.subject);
|
|
289
|
+
if (position === undefined || position.measure !== "value")
|
|
290
|
+
return value;
|
|
291
|
+
const { other } = rule;
|
|
292
|
+
const written = writeAt(value, position.path, current => {
|
|
293
|
+
switch (rule.relation) {
|
|
294
|
+
case "startsWith": return typeof current === "string" && typeof other === "string" ? other + current : current;
|
|
295
|
+
case "endsWith":
|
|
296
|
+
case "contains": return typeof current === "string" && typeof other === "string" ? current + other : current;
|
|
297
|
+
case "hasKey": {
|
|
298
|
+
if (typeof current !== "object" || current === null || Array.isArray(current) || typeof other !== "string")
|
|
299
|
+
return current;
|
|
300
|
+
const [first] = Object.values(current);
|
|
301
|
+
return first === undefined ? current : { ...current, [other]: first };
|
|
302
|
+
}
|
|
303
|
+
case "includes": return Array.isArray(current) ? (current.length === 0 ? [other] : [other, ...current.slice(1)]) : current;
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
return holds(rule, written) ? written : value;
|
|
307
|
+
}
|
|
206
308
|
function satisfyComparison(rule, value, stepAt) {
|
|
207
309
|
const [term, other, operator] = isTerm(rule.left) && positionData(rule.left) !== undefined
|
|
208
310
|
? [rule.left, rule.right, rule.operator]
|
|
@@ -221,20 +323,35 @@ function satisfyComparison(rule, value, stepAt) {
|
|
|
221
323
|
return value;
|
|
222
324
|
}
|
|
223
325
|
const { path, measure } = position;
|
|
224
|
-
const named =
|
|
326
|
+
const named = stepAt?.(path);
|
|
225
327
|
if (named !== undefined && typeof named !== "function") {
|
|
226
328
|
// A value of a finite domain is chosen, not stepped to: the first one the rule
|
|
227
|
-
// keeps, whichever way the rule orders them
|
|
329
|
+
// keeps, whichever way the rule orders them, and however it reads the value
|
|
330
|
+
// (`$lowercase()` would otherwise write "high" into an enum of "High").
|
|
228
331
|
const chosen = named.among.find(candidate => holds(rule, writeAt(value, path, () => candidate)));
|
|
229
332
|
return chosen === undefined ? value : writeAt(value, path, () => chosen);
|
|
230
333
|
}
|
|
231
|
-
const move = named ?? step;
|
|
334
|
+
const move = (measure === "value" ? named : undefined) ?? step;
|
|
335
|
+
// A step that cannot go on (the last Date, the last time of the day) hands the bound back, which `!=` then leaves by the other side.
|
|
336
|
+
const above = operator === ">" || operator === "!=" ? move(bound, 1) : undefined;
|
|
232
337
|
const target = operator === ">" || operator === "!="
|
|
233
|
-
? move(bound, 1)
|
|
338
|
+
? operator === "!=" && above === bound ? move(bound, -1) : above
|
|
234
339
|
: operator === "<"
|
|
235
340
|
? move(bound, -1)
|
|
236
341
|
: bound;
|
|
237
|
-
|
|
342
|
+
const written = writeAt(value, path, current => rewrite(current, measure, target));
|
|
343
|
+
if (operator === "==" && !holds(rule, written)) {
|
|
344
|
+
// A rule that the string reads the same transformed, `uppercase($) == $`, is
|
|
345
|
+
// kept by the transformed value itself, which no transform changes again.
|
|
346
|
+
const { transforms, reading } = partsOfMeasure(measure);
|
|
347
|
+
if (reading === "value" && transforms.length > 0) {
|
|
348
|
+
const kept = writeAt(value, path, current => transformed(current, transforms));
|
|
349
|
+
if (holds(rule, kept)) {
|
|
350
|
+
return kept;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return written;
|
|
238
355
|
}
|
|
239
356
|
export function describeRule(rule, path = "$", elements = {}) {
|
|
240
357
|
switch (rule.kind) {
|
|
@@ -248,6 +365,8 @@ export function describeRule(rule, path = "$", elements = {}) {
|
|
|
248
365
|
return `${rule.kind}(${rule.rules.map(part => describeRule(part, path, elements)).join(", ")})`;
|
|
249
366
|
case "not":
|
|
250
367
|
return `not(${describeRule(rule.rule, path, elements)})`;
|
|
368
|
+
case "relation":
|
|
369
|
+
return `${rule.relation}(${describeOperand(rule.subject, path, elements)}, ${describeOperand(rule.other, path, elements)})`;
|
|
251
370
|
case "compare":
|
|
252
371
|
break;
|
|
253
372
|
}
|
|
@@ -270,6 +389,23 @@ export function isDecimal(value) {
|
|
|
270
389
|
typeof value.toFixed === "function" &&
|
|
271
390
|
typeof value.decimalPlaces === "function");
|
|
272
391
|
}
|
|
392
|
+
// A copy of a state whose objects and arrays can be changed without touching
|
|
393
|
+
// the original. A Decimal keeps its digits in an array a callback can change,
|
|
394
|
+
// so it is copied with them, through its own constructor so that another copy
|
|
395
|
+
// of decimal.js is kept; Rational and Temporal values cannot be changed in
|
|
396
|
+
// place, so they are shared.
|
|
397
|
+
export function copyOf(value) {
|
|
398
|
+
if (isDecimal(value))
|
|
399
|
+
return new value.constructor(value);
|
|
400
|
+
if (value instanceof Date)
|
|
401
|
+
return new Date(value.getTime());
|
|
402
|
+
if (Array.isArray(value))
|
|
403
|
+
return value.map(copyOf);
|
|
404
|
+
if (value !== null && typeof value === "object" && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
|
|
405
|
+
return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, copyOf(child)]));
|
|
406
|
+
}
|
|
407
|
+
return value;
|
|
408
|
+
}
|
|
273
409
|
// A decimal of `scale` digits after the point, as a whole number of units of its
|
|
274
410
|
// last digit: exact, and free of the precision a Decimal's arithmetic rounds to.
|
|
275
411
|
// A value off that grid is rounded down or up, as `rounding` says.
|
|
@@ -300,6 +436,10 @@ function step(bound, direction) {
|
|
|
300
436
|
if (typeof bound === "number") {
|
|
301
437
|
return bound + direction;
|
|
302
438
|
}
|
|
439
|
+
if (bound instanceof Date) {
|
|
440
|
+
const moved = bound.getTime() + direction;
|
|
441
|
+
return Math.abs(moved) <= MAX_DATE_TIME ? new Date(moved) : bound;
|
|
442
|
+
}
|
|
303
443
|
const add = bound?.add;
|
|
304
444
|
if (typeof add === "function") {
|
|
305
445
|
const moved = add.call(bound, isPlainDate(bound) ? { days: direction } : { nanoseconds: direction });
|
|
@@ -338,8 +478,60 @@ export function resize(current, size, empty) {
|
|
|
338
478
|
}
|
|
339
479
|
return current;
|
|
340
480
|
}
|
|
481
|
+
// Whether a measure counts (a length), rather than reading a value.
|
|
482
|
+
export function counts(measure) {
|
|
483
|
+
return partsOfMeasure(measure).reading === "length";
|
|
484
|
+
}
|
|
485
|
+
const TRANSFORMS = {
|
|
486
|
+
trim: text => text.trim(),
|
|
487
|
+
lowercase: text => text.toLowerCase(),
|
|
488
|
+
uppercase: text => text.toUpperCase(),
|
|
489
|
+
};
|
|
490
|
+
export function transformed(value, transforms) {
|
|
491
|
+
if (typeof value !== "string") {
|
|
492
|
+
return value;
|
|
493
|
+
}
|
|
494
|
+
return transforms.reduce((text, transform) => TRANSFORMS[transform](text), value);
|
|
495
|
+
}
|
|
496
|
+
// Whether the transforms leave a string as it reads, compared as text is: "İ"
|
|
497
|
+
// lowercases to "i̇" and uppercases back to "I" with a combining dot, which
|
|
498
|
+
// reads as "İ" though it is not the same code points.
|
|
499
|
+
export function leftAsItReads(value, transforms) {
|
|
500
|
+
return typeof value === "string" && compareText(transformed(value, transforms), value) === 0;
|
|
501
|
+
}
|
|
502
|
+
// A location as `measure` reads it: `length(lowercase(trim($.x)))`.
|
|
503
|
+
export function measureLabel(location, measure) {
|
|
504
|
+
const { transforms, reading } = partsOfMeasure(measure);
|
|
505
|
+
const read = transforms.reduce((inner, transform) => `${transform}(${inner})`, location);
|
|
506
|
+
return reading === "length" ? `length(${read})` : read;
|
|
507
|
+
}
|
|
508
|
+
// What `measure` reads of a value found at a term's path.
|
|
509
|
+
export function measured(value, measure) {
|
|
510
|
+
const { transforms, reading } = partsOfMeasure(measure);
|
|
511
|
+
const read = transformed(value, transforms);
|
|
512
|
+
return reading === "length" ? sizeOf(read) : read;
|
|
513
|
+
}
|
|
514
|
+
// A value whose `measure` reads `target`, written on the current value. Each
|
|
515
|
+
// transform gives back a string it then leaves as it is, so a target the
|
|
516
|
+
// transforms leave alone is written as it is, and one they change is not
|
|
517
|
+
// written at all, since no value reads back as it. A length is written by
|
|
518
|
+
// resizing the transformed value, or by a string of `_` where resizing leaves
|
|
519
|
+
// whitespace at an end that `trim` would take off.
|
|
520
|
+
export function rewrite(current, measure, target, empty) {
|
|
521
|
+
const { transforms, reading } = partsOfMeasure(measure);
|
|
522
|
+
if (reading === "value") {
|
|
523
|
+
return transforms.length === 0 || leftAsItReads(target, transforms) ? target : current;
|
|
524
|
+
}
|
|
525
|
+
if (transforms.length === 0 || typeof current !== "string") {
|
|
526
|
+
return resize(current, target, empty);
|
|
527
|
+
}
|
|
528
|
+
const resized = resize(transformed(current, transforms), target);
|
|
529
|
+
return measured(resized, measure) === target ? resized : resize("", target);
|
|
530
|
+
}
|
|
531
|
+
// A Date can be changed after it is written, so a rule keeps its own copy.
|
|
532
|
+
const held = (value) => (value instanceof Date ? new Date(value.getTime()) : value);
|
|
341
533
|
function compare(operator, left, right) {
|
|
342
|
-
return condition({ kind: "compare", operator, left, right });
|
|
534
|
+
return condition({ kind: "compare", operator, left: held(left), right: held(right) });
|
|
343
535
|
}
|
|
344
536
|
const OPERATORS = {
|
|
345
537
|
lt: "<",
|
|
@@ -373,10 +565,20 @@ function termAt(path, measure) {
|
|
|
373
565
|
case "minus":
|
|
374
566
|
return (other) => linearTerm(combined(data, other, -1));
|
|
375
567
|
case "length":
|
|
376
|
-
return () => termAt(path, "length");
|
|
568
|
+
return () => termAt(path, measureOf(partsOfMeasure(measure).transforms, "length"));
|
|
569
|
+
case "trim":
|
|
570
|
+
case "lowercase":
|
|
571
|
+
case "uppercase":
|
|
572
|
+
return () => termAt(path, measureOf([...partsOfMeasure(measure).transforms, name], "value"));
|
|
377
573
|
case "all":
|
|
378
574
|
case "any":
|
|
379
575
|
return (each) => quantified(name, self, each);
|
|
576
|
+
case "includes":
|
|
577
|
+
case "startsWith":
|
|
578
|
+
case "endsWith":
|
|
579
|
+
case "contains":
|
|
580
|
+
case "hasKey":
|
|
581
|
+
return (other) => condition({ kind: "relation", relation: name, subject: self, other });
|
|
380
582
|
default:
|
|
381
583
|
return termAt([...path, key], "value");
|
|
382
584
|
}
|
|
@@ -431,7 +633,7 @@ function combined(data, other, sign) {
|
|
|
431
633
|
const right = linearOf(other);
|
|
432
634
|
const parts = [...base.parts];
|
|
433
635
|
for (const part of right.parts) {
|
|
434
|
-
const index = parts.findIndex(existing => existing.measure === part.measure &&
|
|
636
|
+
const index = parts.findIndex(existing => existing.measure === part.measure && deepEqual(existing.path, part.path));
|
|
435
637
|
const coefficient = sign * part.coefficient;
|
|
436
638
|
if (index === -1) {
|
|
437
639
|
parts.push({ ...part, coefficient });
|
|
@@ -536,12 +738,11 @@ function read(operand, value) {
|
|
|
536
738
|
const found = path.reduce((current, key) => typeof current === "object" && current !== null
|
|
537
739
|
? current[key]
|
|
538
740
|
: undefined, value);
|
|
539
|
-
|
|
540
|
-
return found;
|
|
541
|
-
}
|
|
542
|
-
return sizeOf(found);
|
|
741
|
+
return found === undefined ? found : measured(found, measure);
|
|
543
742
|
}
|
|
544
743
|
function ordering(left, right) {
|
|
744
|
+
if (left instanceof Date && right instanceof Date)
|
|
745
|
+
return left.getTime() - right.getTime();
|
|
545
746
|
if (typeof left === "bigint" && typeof right === "bigint")
|
|
546
747
|
return left < right ? -1 : left > right ? 1 : 0;
|
|
547
748
|
if (isRational(left) && isRational(right))
|
|
@@ -568,12 +769,26 @@ function ordering(left, right) {
|
|
|
568
769
|
}
|
|
569
770
|
return Number.NaN;
|
|
570
771
|
}
|
|
571
|
-
|
|
572
|
-
|
|
772
|
+
// A local bound to a dependency's answer reads as the call, any other by its number.
|
|
773
|
+
export function localLabel(name) {
|
|
774
|
+
const rest = name.slice("#local:".length);
|
|
775
|
+
if (rest.startsWith("call:"))
|
|
776
|
+
return `${rest.slice("call:".length, rest.lastIndexOf(":"))}()`;
|
|
777
|
+
const at = rest.indexOf("@");
|
|
778
|
+
return at === -1 ? `local${rest}` : rest.slice(at + 1);
|
|
779
|
+
}
|
|
780
|
+
// A Date is written as toISOString does: String(date) reads in the time zone of the machine.
|
|
781
|
+
export function describeLiteral(value) {
|
|
782
|
+
if (value instanceof Date)
|
|
783
|
+
return Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
|
|
784
|
+
return typeof value === "string" ? JSON.stringify(value) : String(value);
|
|
785
|
+
}
|
|
786
|
+
export function describeTerm(term, path = "$", elements = {}) {
|
|
787
|
+
return describeOperand(term, path, elements);
|
|
573
788
|
}
|
|
574
789
|
function describeOperand(operand, path, elements = {}) {
|
|
575
790
|
if (!isTerm(operand)) {
|
|
576
|
-
return
|
|
791
|
+
return describeLiteral(operand);
|
|
577
792
|
}
|
|
578
793
|
const data = termData(operand);
|
|
579
794
|
if (data.kind === "linear") {
|
|
@@ -583,12 +798,16 @@ function describeOperand(operand, path, elements = {}) {
|
|
|
583
798
|
const [first = "", ...rest] = keys;
|
|
584
799
|
const location = (first === DEPS
|
|
585
800
|
? ["deps", ...rest]
|
|
586
|
-
:
|
|
587
|
-
? [
|
|
588
|
-
: [
|
|
801
|
+
: first.startsWith("#local:")
|
|
802
|
+
? [localLabel(first), ...rest]
|
|
803
|
+
: elements[first] !== undefined
|
|
804
|
+
? [elements[first], ...rest]
|
|
805
|
+
: [path, ...keys])
|
|
589
806
|
.filter(part => part !== "")
|
|
590
807
|
.join(".");
|
|
591
|
-
|
|
808
|
+
const { transforms, reading } = partsOfMeasure(measure);
|
|
809
|
+
const read = transforms.reduce((inner, transform) => `${transform}(${inner})`, location);
|
|
810
|
+
return reading === "length" ? `length(${read})` : read;
|
|
592
811
|
}
|
|
593
812
|
// A plain date has no time of day, so adding a nanosecond leaves it where it is.
|
|
594
813
|
function isPlainDate(value) {
|
|
@@ -596,4 +815,192 @@ function isPlainDate(value) {
|
|
|
596
815
|
.Temporal?.PlainDate;
|
|
597
816
|
return constructor !== undefined && value instanceof constructor;
|
|
598
817
|
}
|
|
818
|
+
export function rebaseTerm(term, move) {
|
|
819
|
+
const data = termData(term);
|
|
820
|
+
return data.kind === "position"
|
|
821
|
+
? termAt(move(data.path), data.measure)
|
|
822
|
+
: linearTerm({ ...data, parts: data.parts.map(part => ({ ...part, path: move(part.path) })) });
|
|
823
|
+
}
|
|
824
|
+
// The rule with every term moved by `move`, which takes a path and answers the
|
|
825
|
+
// path to read instead; constants, operators, names and element roots are kept.
|
|
826
|
+
export function rebaseRule(rule, move) {
|
|
827
|
+
const term = (value) => (isTerm(value) ? rebaseTerm(value, move) : value);
|
|
828
|
+
const named = (rebased) => (rule.name === undefined ? rebased : { ...rebased, name: rule.name });
|
|
829
|
+
switch (rule.kind) {
|
|
830
|
+
case "compare":
|
|
831
|
+
return named({ kind: "compare", operator: rule.operator, left: term(rule.left), right: term(rule.right) });
|
|
832
|
+
case "relation":
|
|
833
|
+
return named({ kind: "relation", relation: rule.relation, subject: term(rule.subject), other: term(rule.other) });
|
|
834
|
+
case "all":
|
|
835
|
+
case "any":
|
|
836
|
+
return named({ ...rule, of: term(rule.of), each: rebaseRule(rule.each, path => (path[0] === rule.element ? path : move(path))) });
|
|
837
|
+
case "and":
|
|
838
|
+
case "or":
|
|
839
|
+
return named({ kind: rule.kind, rules: rule.rules.map(part => rebaseRule(part, move)) });
|
|
840
|
+
case "not":
|
|
841
|
+
return named({ kind: "not", rule: rebaseRule(rule.rule, move) });
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
// A sum of operands weighed by whole numbers: a term where any operand is one,
|
|
845
|
+
// otherwise the constant it adds up to. Undefined where a term is not a value
|
|
846
|
+
// of a position or an expression of them.
|
|
847
|
+
function foldsExactly(items, folded) {
|
|
848
|
+
try {
|
|
849
|
+
const exact = items.reduce((total, { operand, coefficient }) => total.plus(fractionOf(linearOf(operand).constant).times(fractionOf(coefficient))), fractionOf(0));
|
|
850
|
+
return fractionOf(folded).comparedTo(exact) === 0;
|
|
851
|
+
}
|
|
852
|
+
catch {
|
|
853
|
+
return false;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
export function linearCombination(items) {
|
|
857
|
+
let parts = [];
|
|
858
|
+
let constant = 0;
|
|
859
|
+
for (const { operand, coefficient } of items) {
|
|
860
|
+
const data = linearOf(operand);
|
|
861
|
+
for (const part of data.parts) {
|
|
862
|
+
const index = parts.findIndex(existing => existing.measure === part.measure && deepEqual(existing.path, part.path));
|
|
863
|
+
const weight = part.coefficient * coefficient;
|
|
864
|
+
const total = index === -1 ? weight : parts[index].coefficient + weight;
|
|
865
|
+
// A weight or a running total that rounds could cancel to a value the exact sum does not have.
|
|
866
|
+
if (!Number.isSafeInteger(weight) || !Number.isSafeInteger(total))
|
|
867
|
+
return undefined;
|
|
868
|
+
if (index === -1)
|
|
869
|
+
parts.push({ ...part, coefficient: total });
|
|
870
|
+
else
|
|
871
|
+
parts[index] = { ...parts[index], coefficient: total };
|
|
872
|
+
}
|
|
873
|
+
constant = sum(constant, scaled(data.constant, coefficient));
|
|
874
|
+
}
|
|
875
|
+
parts = parts.filter(part => part.coefficient !== 0);
|
|
876
|
+
// A constant is read exactly, as a term reads it: a fold that rounds is not read.
|
|
877
|
+
if (typeof constant === "number" && (!Number.isFinite(constant) || (Number.isInteger(constant) && !Number.isSafeInteger(constant)) || !foldsExactly(items, constant)))
|
|
878
|
+
return undefined;
|
|
879
|
+
// What decimals add to stays a decimal where their parts cancel.
|
|
880
|
+
if (typeof constant === "number" && items.some(({ operand }) => isDecimal(operand) || (isTerm(operand) && isDecimal(linearOf(operand).constant))))
|
|
881
|
+
constant = decimalOf(fractionOf(constant));
|
|
882
|
+
if (parts.length === 0)
|
|
883
|
+
return constant;
|
|
884
|
+
if (parts.length === 1 && parts[0].coefficient === 1 && constant === 0)
|
|
885
|
+
return termAt(parts[0].path, parts[0].measure);
|
|
886
|
+
return linearTerm({ kind: "linear", parts, constant: constant });
|
|
887
|
+
}
|
|
888
|
+
export function substitutedOperand(value, replace) {
|
|
889
|
+
{
|
|
890
|
+
if (!isTerm(value))
|
|
891
|
+
return { with: value };
|
|
892
|
+
const data = termData(value);
|
|
893
|
+
if (data.kind === "position") {
|
|
894
|
+
const found = replace(data.path);
|
|
895
|
+
if (found === undefined)
|
|
896
|
+
return { with: value };
|
|
897
|
+
if (data.measure === "value")
|
|
898
|
+
return found;
|
|
899
|
+
if (found.over !== undefined)
|
|
900
|
+
return undefined;
|
|
901
|
+
if (isTerm(found.with)) {
|
|
902
|
+
const inner = termData(found.with);
|
|
903
|
+
return inner.kind === "position" && inner.measure === "value" ? { with: termAt(inner.path, data.measure) } : undefined;
|
|
904
|
+
}
|
|
905
|
+
return { with: measured(found.with, data.measure) };
|
|
906
|
+
}
|
|
907
|
+
const items = [{ operand: data.constant, coefficient: 1 }];
|
|
908
|
+
for (const part of data.parts) {
|
|
909
|
+
const found = part.measure === "value" ? replace(part.path) : undefined;
|
|
910
|
+
if (found === undefined && part.measure !== "value" && replace(part.path) !== undefined)
|
|
911
|
+
return undefined;
|
|
912
|
+
if (found?.over !== undefined)
|
|
913
|
+
return undefined;
|
|
914
|
+
items.push({ operand: found === undefined ? termAt(part.path, part.measure) : found.with, coefficient: part.coefficient });
|
|
915
|
+
}
|
|
916
|
+
const combined = linearCombination(items);
|
|
917
|
+
return combined === undefined ? undefined : { with: combined };
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
export function substitutedRule(rule, replace) {
|
|
921
|
+
const operand = (value) => substitutedOperand(value, replace);
|
|
922
|
+
const named = (made) => (rule.name === undefined ? made : { ...made, name: rule.name });
|
|
923
|
+
switch (rule.kind) {
|
|
924
|
+
case "compare": {
|
|
925
|
+
const left = operand(rule.left), right = operand(rule.right);
|
|
926
|
+
if (left === undefined || right === undefined)
|
|
927
|
+
return undefined;
|
|
928
|
+
if (left.over === undefined && right.over === undefined)
|
|
929
|
+
return named({ kind: "compare", operator: rule.operator, left: left.with, right: right.with });
|
|
930
|
+
if (left.over !== undefined && right.over !== undefined)
|
|
931
|
+
return undefined;
|
|
932
|
+
// x / k op y is x op y * k, the order turned where k is negative. A
|
|
933
|
+
// quotient is a rational, which compares as written only with a rational.
|
|
934
|
+
const divisor = (left.over ?? right.over);
|
|
935
|
+
if (!isRational(left.over === undefined ? left.with : right.with))
|
|
936
|
+
return undefined;
|
|
937
|
+
const multiplied = scaledBy(left.over === undefined ? left.with : right.with, divisor);
|
|
938
|
+
if (multiplied === undefined)
|
|
939
|
+
return undefined;
|
|
940
|
+
const operator = divisor < 0 ? mirrored[rule.operator] : rule.operator;
|
|
941
|
+
return left.over !== undefined
|
|
942
|
+
? named({ kind: "compare", operator, left: left.with, right: multiplied })
|
|
943
|
+
: named({ kind: "compare", operator, left: multiplied, right: right.with });
|
|
944
|
+
}
|
|
945
|
+
case "relation": {
|
|
946
|
+
const subject = operand(rule.subject), other = operand(rule.other);
|
|
947
|
+
if (subject === undefined || other === undefined || subject.over !== undefined || other.over !== undefined || !isTerm(subject.with))
|
|
948
|
+
return undefined;
|
|
949
|
+
return named({ kind: "relation", relation: rule.relation, subject: subject.with, other: other.with });
|
|
950
|
+
}
|
|
951
|
+
case "all":
|
|
952
|
+
case "any": {
|
|
953
|
+
const of = operand(rule.of);
|
|
954
|
+
const each = substitutedRule(rule.each, replace);
|
|
955
|
+
return of === undefined || !isTerm(of.with) || each === undefined ? undefined : named({ ...rule, of: of.with, each });
|
|
956
|
+
}
|
|
957
|
+
case "and":
|
|
958
|
+
case "or": {
|
|
959
|
+
const rules = rule.rules.map(part => substitutedRule(part, replace));
|
|
960
|
+
return rules.some(part => part === undefined) ? undefined : named({ kind: rule.kind, rules: rules });
|
|
961
|
+
}
|
|
962
|
+
case "not": {
|
|
963
|
+
const inner = substitutedRule(rule.rule, replace);
|
|
964
|
+
return inner === undefined ? undefined : named({ kind: "not", rule: inner });
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
// A value multiplied by a whole number, a term or a number, decimal or rational
|
|
969
|
+
// constant; undefined where a rational does not come out whole.
|
|
970
|
+
function scaledBy(value, factor) {
|
|
971
|
+
if (isRational(value)) {
|
|
972
|
+
const product = value.times(new Rational(BigInt(factor)));
|
|
973
|
+
return product.denominator === 1n && product.numerator <= BigInt(Number.MAX_SAFE_INTEGER) && product.numerator >= BigInt(Number.MIN_SAFE_INTEGER)
|
|
974
|
+
? Number(product.numerator) : undefined;
|
|
975
|
+
}
|
|
976
|
+
return linearCombination([{ operand: value, coefficient: factor }]);
|
|
977
|
+
}
|
|
978
|
+
export function normalize(rule) {
|
|
979
|
+
if (rule.kind !== "compare") {
|
|
980
|
+
return undefined;
|
|
981
|
+
}
|
|
982
|
+
const left = isTerm(rule.left) ? positionData(rule.left) : undefined;
|
|
983
|
+
const right = isTerm(rule.right) ? positionData(rule.right) : undefined;
|
|
984
|
+
if (left !== undefined && !isTerm(rule.right)) {
|
|
985
|
+
return { operator: rule.operator, bound: rule.right, measure: left.measure };
|
|
986
|
+
}
|
|
987
|
+
if (right !== undefined && !isTerm(rule.left)) {
|
|
988
|
+
return { operator: mirrored[rule.operator], bound: rule.left, measure: right.measure };
|
|
989
|
+
}
|
|
990
|
+
return undefined;
|
|
991
|
+
}
|
|
992
|
+
// The values rules write, constants of comparisons, relations and expressions.
|
|
993
|
+
export function constantsOf(rules) {
|
|
994
|
+
return rules.flatMap((rule) => {
|
|
995
|
+
if (rule.kind === "and" || rule.kind === "or")
|
|
996
|
+
return constantsOf(rule.rules);
|
|
997
|
+
if (rule.kind === "not")
|
|
998
|
+
return constantsOf([rule.rule]);
|
|
999
|
+
if (rule.kind === "all" || rule.kind === "any")
|
|
1000
|
+
return constantsOf([rule.each]);
|
|
1001
|
+
if (rule.kind === "relation")
|
|
1002
|
+
return isTerm(rule.other) ? [] : [rule.other];
|
|
1003
|
+
return [rule.left, rule.right].flatMap(value => !isTerm(value) ? [value] : termData(value).kind === "linear" ? [termData(value).constant] : []);
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
599
1006
|
//# sourceMappingURL=rule.js.map
|