@toiroakr/chisel 0.2.0 → 0.3.1
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 +114 -10
- package/dist/absence.d.ts +1 -0
- package/dist/absence.js +39 -1
- package/dist/absence.js.map +1 -1
- package/dist/behavior.d.ts +5 -10
- package/dist/behavior.js +48 -225
- 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 +3 -1
- package/dist/border.js +47 -3
- package/dist/border.js.map +1 -1
- package/dist/bounds.js +4 -3
- package/dist/bounds.js.map +1 -1
- 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 +2 -0
- package/dist/cli.js +80 -11
- package/dist/cli.js.map +1 -1
- package/dist/codec.js +9 -3
- package/dist/codec.js.map +1 -1
- package/dist/codegen.js +2 -0
- package/dist/codegen.js.map +1 -1
- package/dist/composition.js +5 -4
- 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 +2 -2
- package/dist/data.js +5 -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/domain.d.ts +2 -2
- package/dist/domain.js +59 -19
- package/dist/domain.js.map +1 -1
- package/dist/ensures.js +1 -1
- package/dist/ensures.js.map +1 -1
- 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.js +205 -18
- package/dist/feasibility.js.map +1 -1
- package/dist/guard-borders.d.ts +2 -0
- package/dist/guard-borders.js +112 -24
- package/dist/guard-borders.js.map +1 -1
- package/dist/guard-scope.d.ts +5 -1
- package/dist/guard-scope.js +26 -6
- package/dist/guard-scope.js.map +1 -1
- 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 +16 -5
- package/dist/index.js +12 -4
- package/dist/index.js.map +1 -1
- package/dist/interval.d.ts +1 -0
- package/dist/interval.js +44 -4
- 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.d.ts +25 -0
- package/dist/machine.js +88 -17
- package/dist/machine.js.map +1 -1
- package/dist/model.d.ts +39 -5
- package/dist/model.js +268 -59
- 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 +9 -1
- package/dist/outcomes.js +92 -10
- package/dist/outcomes.js.map +1 -1
- package/dist/pairs.js +2 -1
- package/dist/pairs.js.map +1 -1
- package/dist/partition.js +12 -9
- 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.js +189 -37
- 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/rewrites.d.ts +41 -0
- package/dist/rewrites.js +180 -0
- package/dist/rewrites.js.map +1 -0
- package/dist/rule.d.ts +29 -6
- package/dist/rule.js +145 -5
- package/dist/rule.js.map +1 -1
- package/dist/runtime.d.ts +4 -4
- package/dist/runtime.js +3 -3
- package/dist/runtime.js.map +1 -1
- package/dist/schema.d.ts +44 -28
- package/dist/schema.js +42 -109
- package/dist/schema.js.map +1 -1
- package/dist/specification.d.ts +11 -1
- package/dist/specification.js +391 -26
- 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.js +5 -5
- package/dist/temporal.js.map +1 -1
- 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 +46 -6
- package/dist/tla.js +250 -49
- package/dist/tla.js.map +1 -1
- package/dist/verification.d.ts +2 -0
- package/dist/verification.js +2 -0
- package/dist/verification.js.map +1 -1
- package/dist/ways.d.ts +4 -1
- package/dist/ways.js +23 -8
- package/dist/ways.js.map +1 -1
- package/dist/world.d.ts +2 -2
- package/dist/world.js +13 -17
- package/dist/world.js.map +1 -1
- package/package.json +23 -7
- package/schema/report.schema.json +169 -1
package/dist/rewrites.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { SpecificationError, performSync } from "./behavior.js";
|
|
2
|
+
import { childrenOf, iterationsOf, nodeOf, withWalk } from "./model.js";
|
|
3
|
+
import { describeTerm, isTerm, localLabel, pathsRead, termData } from "./rule.js";
|
|
4
|
+
import { schemaAtPath } from "./schema.js";
|
|
5
|
+
import { integralBounds } from "./domain.js";
|
|
6
|
+
import { isEvaluationLimit } from "./exact.js";
|
|
7
|
+
// The folds an implementation's models write, each once however many cases
|
|
8
|
+
// its decision decides, leaving out a fold that reads nothing a run is handed:
|
|
9
|
+
// it answers one value on every run, and so does either rewrite of it.
|
|
10
|
+
export function foldsOf(implementation) {
|
|
11
|
+
if (implementation === undefined || implementation.pipeline !== undefined || implementation.external !== undefined)
|
|
12
|
+
return [];
|
|
13
|
+
const decisions = new Map();
|
|
14
|
+
for (const [tag, decision] of Object.entries(implementation.cases ?? {})) {
|
|
15
|
+
if (decision?.kind !== "rules")
|
|
16
|
+
continue;
|
|
17
|
+
const tags = decisions.get(decision) ?? [];
|
|
18
|
+
tags.push(tag);
|
|
19
|
+
decisions.set(decision, tags);
|
|
20
|
+
}
|
|
21
|
+
const folds = [];
|
|
22
|
+
for (const [decision, tags] of decisions) {
|
|
23
|
+
const rules = decision;
|
|
24
|
+
const written = rules.runs ?? rules.expression;
|
|
25
|
+
if (written === undefined)
|
|
26
|
+
continue;
|
|
27
|
+
for (const node of new Set(iterationsOf(written))) {
|
|
28
|
+
if (node.kind !== "fold")
|
|
29
|
+
continue;
|
|
30
|
+
const fold = node;
|
|
31
|
+
if (!readsRun(fold))
|
|
32
|
+
continue;
|
|
33
|
+
const values = fold.values;
|
|
34
|
+
const label = isTerm(values) ? `fold over ${termLabel(values)}` : "fold";
|
|
35
|
+
const position = isTerm(values) ? termData(values) : undefined;
|
|
36
|
+
const over = position?.kind === "position" && position.measure === "value" && !position.path[0].startsWith("#") ? position.path : undefined;
|
|
37
|
+
// A list written with one element or none, or an input list every case
|
|
38
|
+
// the fold walks holds to one element at most, takes no second step.
|
|
39
|
+
const written = Array.isArray(values) && nodeOf(values) === undefined && values.length <= 1;
|
|
40
|
+
const bounded = over !== undefined && tags.every(tag => {
|
|
41
|
+
const list = schemaAtPath(implementation.behavior.input.variants[tag], over);
|
|
42
|
+
const upper = list === undefined ? undefined : integralBounds(list, "length").upper;
|
|
43
|
+
return upper !== undefined && upper <= 1;
|
|
44
|
+
});
|
|
45
|
+
const single = written || bounded;
|
|
46
|
+
folds.push({ decision: rules, tags, node, label, ...(over === undefined ? {} : { over }), ...(single ? { single: true } : {}) });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return folds;
|
|
50
|
+
}
|
|
51
|
+
function termLabel(term) {
|
|
52
|
+
const data = termData(term);
|
|
53
|
+
if (data.kind === "position" && data.path[0]?.startsWith("#local:"))
|
|
54
|
+
return [localLabel(data.path[0]), ...data.path.slice(1)].join(".");
|
|
55
|
+
return describeTerm(term);
|
|
56
|
+
}
|
|
57
|
+
function readsRun(fold) {
|
|
58
|
+
const own = new Set([fold.name, fold.accumulator]);
|
|
59
|
+
const seen = new Set();
|
|
60
|
+
let reads = false;
|
|
61
|
+
// The whole input, read by the empty path, is a read of what a run is handed.
|
|
62
|
+
const rooted = (path) => {
|
|
63
|
+
if (path[0] === undefined || !own.has(path[0]))
|
|
64
|
+
reads = true;
|
|
65
|
+
};
|
|
66
|
+
const walk = (value) => {
|
|
67
|
+
if (reads || value === null || typeof value !== "object" || seen.has(value))
|
|
68
|
+
return;
|
|
69
|
+
seen.add(value);
|
|
70
|
+
if (isTerm(value)) {
|
|
71
|
+
const data = termData(value);
|
|
72
|
+
if (data.kind === "position")
|
|
73
|
+
rooted(data.path);
|
|
74
|
+
else
|
|
75
|
+
for (const part of data.parts)
|
|
76
|
+
rooted(part.path);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const node = nodeOf(value);
|
|
80
|
+
// A name a node inside the fold binds is read from within, not from what a run is handed; each is declared once.
|
|
81
|
+
if (node?.kind === "bind" || node?.kind === "map" || node?.kind === "fold")
|
|
82
|
+
own.add(node.name);
|
|
83
|
+
if (node?.kind === "fold")
|
|
84
|
+
own.add(node.accumulator);
|
|
85
|
+
if (node?.kind === "choose")
|
|
86
|
+
for (const path of pathsRead(node.condition))
|
|
87
|
+
rooted(path);
|
|
88
|
+
if (node?.kind === "call")
|
|
89
|
+
reads = true;
|
|
90
|
+
for (const child of childrenOf(value))
|
|
91
|
+
walk(child);
|
|
92
|
+
};
|
|
93
|
+
for (const part of [fold.values, fold.initial, fold.body])
|
|
94
|
+
walk(part);
|
|
95
|
+
return reads;
|
|
96
|
+
}
|
|
97
|
+
export function runWalk(implementation, given, deps, rewrite) {
|
|
98
|
+
const elements = new Map();
|
|
99
|
+
const observe = (node, count) => {
|
|
100
|
+
const counts = elements.get(node) ?? [];
|
|
101
|
+
counts.push(count);
|
|
102
|
+
elements.set(node, counts);
|
|
103
|
+
};
|
|
104
|
+
try {
|
|
105
|
+
const actual = withWalk(rewrite, observe, () => performSync(implementation, given, deps));
|
|
106
|
+
return { actual, elements };
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (error instanceof SpecificationError && error.message.includes("answered a promise"))
|
|
110
|
+
return { pending: true, elements };
|
|
111
|
+
return { error, elements };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export const walks = ["after one step", "from the seed"];
|
|
115
|
+
export function tookSecond(run, node) {
|
|
116
|
+
return (run.elements.get(node) ?? []).some(count => count >= 2);
|
|
117
|
+
}
|
|
118
|
+
// Whether a rewrite of a fold changes how a run on `given` comes out:
|
|
119
|
+
// `answers` says whether what the rewritten run answered is the answer.
|
|
120
|
+
export function tells(implementation, fold, walk, given, deps, answers) {
|
|
121
|
+
const rewritten = runWalk(implementation, given, deps, { node: fold.node, walk });
|
|
122
|
+
// A rewritten run cut short by a limit of evaluation says nothing of how it
|
|
123
|
+
// would have come out.
|
|
124
|
+
if (rewritten.pending || isEvaluationLimit(rewritten.error))
|
|
125
|
+
return "pending";
|
|
126
|
+
return rewritten.error !== undefined || !answers(rewritten.actual);
|
|
127
|
+
}
|
|
128
|
+
export function walksOf(implementation, rows, answers, tagOfGiven) {
|
|
129
|
+
if (implementation === undefined)
|
|
130
|
+
return [];
|
|
131
|
+
const coverage = [];
|
|
132
|
+
for (const fold of foldsOf(implementation)) {
|
|
133
|
+
const ofCase = rows.filter(row => fold.tags.includes(tagOfGiven(row.given) ?? ""));
|
|
134
|
+
const plain = ofCase.map(row => ({ row, run: runWalk(implementation, row.given, row.deps) }));
|
|
135
|
+
const entered = plain.filter(({ run }) => run.elements.has(fold.node));
|
|
136
|
+
const second = plain.filter(({ run }) => tookSecond(run, fold.node));
|
|
137
|
+
// A run that could not be made synchronously might still reach the fold,
|
|
138
|
+
// again if it has been entered already, and take a second element there.
|
|
139
|
+
const pendingPlain = plain.some(({ run }) => run.pending);
|
|
140
|
+
for (const walk of walks) {
|
|
141
|
+
const base = { decision: fold.decision.id, walk: fold.label, rewrite: walk };
|
|
142
|
+
if (fold.single) {
|
|
143
|
+
coverage.push({ ...base, status: "no row owed", reason: "リストが要素を一つしか持たない" });
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
let told;
|
|
147
|
+
let pending = pendingPlain;
|
|
148
|
+
for (const { row } of second) {
|
|
149
|
+
const outcome = tells(implementation, fold, walk, row.given, row.deps, actual => answers(row.expected, actual));
|
|
150
|
+
if (outcome === "pending")
|
|
151
|
+
pending = true;
|
|
152
|
+
else if (outcome) {
|
|
153
|
+
told = row.name;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (told !== undefined)
|
|
158
|
+
coverage.push({ ...base, status: "told", row: told });
|
|
159
|
+
else if (pending)
|
|
160
|
+
coverage.push({ ...base, status: "undecided", reason: "同期に実行できない行がある" });
|
|
161
|
+
else
|
|
162
|
+
coverage.push({
|
|
163
|
+
...base,
|
|
164
|
+
status: "not told",
|
|
165
|
+
reason: entered.length === 0
|
|
166
|
+
? "答えのある行がfoldに入らない"
|
|
167
|
+
: second.length === 0
|
|
168
|
+
? "二つ目の要素まで歩く答えのある行がない"
|
|
169
|
+
: "二つ以上の要素を歩く行も、書き換えで答えが変わらない",
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return coverage;
|
|
174
|
+
}
|
|
175
|
+
// A short sentence a report writes for a rewrite no row tells apart.
|
|
176
|
+
export function describeWalk(item) {
|
|
177
|
+
const rewrite = item.rewrite === "after one step" ? "最初の要素で止めても" : "各段階に前の答えの代わりに初期値を渡しても";
|
|
178
|
+
return `${item.decision}: ${item.walk}を${rewrite}答えの変わる行がない`;
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=rewrites.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rewrites.js","sourceRoot":"","sources":["../src/rewrites.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAExE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAClF,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAuC/C,2EAA2E;AAC3E,+EAA+E;AAC/E,uEAAuE;AACvE,MAAM,UAAU,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gBAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC;iBACzE,IAAI,OAAO;gBAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC,CAAC;;gBACtF,QAAQ,CAAC,IAAI,CAAC;oBACjB,GAAG,IAAI;oBACP,MAAM,EAAE,UAAU;oBAClB,MAAM,EAAE,OAAO,CAAC,MAAM,KAAK,CAAC;wBAC1B,CAAC,CAAC,kBAAkB;wBACpB,CAAC,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC;4BACnB,CAAC,CAAC,qBAAqB;4BACvB,CAAC,CAAC,4BAA4B;iBACnC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,qEAAqE;AACrE,MAAM,UAAU,YAAY,CAAC,IAAkB;IAC7C,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,KAAK,gBAAgB,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,uBAAuB,CAAC;IAC3F,OAAO,GAAG,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC,IAAI,IAAI,OAAO,YAAY,CAAC;AAC/D,CAAC"}
|
package/dist/rule.d.ts
CHANGED
|
@@ -2,8 +2,9 @@ import { Rational } from "./exact.js";
|
|
|
2
2
|
import type { Decimal } from "decimal.js";
|
|
3
3
|
import type { Temporal as TemporalTypes } from "temporal-spec";
|
|
4
4
|
import type { Execution, Guard } from "./behavior.js";
|
|
5
|
-
type Moment = TemporalTypes.Instant | TemporalTypes.PlainDate | TemporalTypes.PlainTime | TemporalTypes.PlainDateTime;
|
|
5
|
+
type Moment = Date | TemporalTypes.Instant | TemporalTypes.PlainDate | TemporalTypes.PlainTime | TemporalTypes.PlainDateTime;
|
|
6
6
|
export type Comparable = number | bigint | Rational | string | Moment | Decimal;
|
|
7
|
+
export declare const MAX_DATE_TIME = 8640000000000000;
|
|
7
8
|
declare const TERM: unique symbol;
|
|
8
9
|
export type Transform = "trim" | "lowercase" | "uppercase";
|
|
9
10
|
type Reading = "value" | "length";
|
|
@@ -69,8 +70,17 @@ interface Transformable {
|
|
|
69
70
|
interface Quantified<E> {
|
|
70
71
|
$all(each: (element: TermOf<E>) => Rule): Rule & Condition;
|
|
71
72
|
$any(each: (element: TermOf<E>) => Rule): Rule & Condition;
|
|
73
|
+
$includes(value: Operand<E>): Rule & Condition;
|
|
72
74
|
}
|
|
73
|
-
|
|
75
|
+
interface Related {
|
|
76
|
+
$startsWith(other: Operand<string>): Rule & Condition;
|
|
77
|
+
$endsWith(other: Operand<string>): Rule & Condition;
|
|
78
|
+
$contains(other: Operand<string>): Rule & Condition;
|
|
79
|
+
}
|
|
80
|
+
interface Keyed {
|
|
81
|
+
$hasKey(key: Operand<string>): Rule & Condition;
|
|
82
|
+
}
|
|
83
|
+
type TermOperator = "$lt" | "$lte" | "$gt" | "$gte" | "$eq" | "$ne" | "$length" | "$trim" | "$lowercase" | "$uppercase" | "$all" | "$any" | "$includes" | "$startsWith" | "$endsWith" | "$contains" | "$hasKey" | "$plus" | "$minus";
|
|
74
84
|
type KeysOfCases<T> = T extends unknown ? keyof T : never;
|
|
75
85
|
type FieldOfCases<T, K extends PropertyKey> = T extends unknown ? (K extends keyof T ? T[K] : never) : never;
|
|
76
86
|
type Fields<T> = {
|
|
@@ -78,9 +88,9 @@ type Fields<T> = {
|
|
|
78
88
|
};
|
|
79
89
|
export type TermOf<T> = Term<T> & (0 extends 1 & T ? {
|
|
80
90
|
readonly [key: string]: any;
|
|
81
|
-
} : [T] extends [boolean] ? Equatable<T> : [T] extends [number | Decimal] ? Ordered<T> & Arithmetic<T> : [T] extends [bigint | Rational] ? Ordered<T> : [T] extends [Moment] ? Ordered<T> : [T] extends [string] ? Ordered<T> & Measured & Transformable : [T] extends [readonly (infer E)[]] ? Measured & Quantified<E> : [T] extends [object] ? string extends keyof T ? unknown extends T[string & keyof T] ? {
|
|
91
|
+
} : [T] extends [boolean] ? Equatable<T> : [T] extends [number | Decimal] ? Ordered<T> & Arithmetic<T> : [T] extends [bigint | Rational] ? Ordered<T> : [T] extends [Moment] ? Ordered<T> : [T] extends [string] ? Ordered<T> & Measured & Transformable & Related : [T] extends [readonly (infer E)[]] ? Measured & Quantified<E> : [T] extends [object] ? string extends keyof T ? unknown extends T[string & keyof T] ? {
|
|
82
92
|
readonly [key: string]: any;
|
|
83
|
-
} : Measured & {
|
|
93
|
+
} : Measured & Keyed & {
|
|
84
94
|
readonly [key: string]: TermOf<Exclude<T[string & keyof T], undefined>>;
|
|
85
95
|
} : Fields<T> : unknown);
|
|
86
96
|
export type InvariantRule<T> = {
|
|
@@ -105,6 +115,13 @@ export interface AnyRule {
|
|
|
105
115
|
readonly element: string;
|
|
106
116
|
readonly each: Rule;
|
|
107
117
|
}
|
|
118
|
+
export type Relation = "startsWith" | "endsWith" | "contains" | "hasKey" | "includes";
|
|
119
|
+
export interface RelationRule {
|
|
120
|
+
readonly kind: "relation";
|
|
121
|
+
readonly relation: Relation;
|
|
122
|
+
readonly subject: Term<unknown>;
|
|
123
|
+
readonly other: Operand<unknown>;
|
|
124
|
+
}
|
|
108
125
|
export interface AndRule {
|
|
109
126
|
readonly kind: "and";
|
|
110
127
|
readonly rules: readonly Rule[];
|
|
@@ -117,10 +134,10 @@ export interface NotRule {
|
|
|
117
134
|
readonly kind: "not";
|
|
118
135
|
readonly rule: Rule;
|
|
119
136
|
}
|
|
120
|
-
export type Rule = (CompareRule | AllRule | AnyRule | AndRule | OrRule | NotRule) & {
|
|
137
|
+
export type Rule = (CompareRule | AllRule | AnyRule | RelationRule | AndRule | OrRule | NotRule) & {
|
|
121
138
|
readonly name?: string;
|
|
122
139
|
};
|
|
123
|
-
export type Distinction = CompareRule | AllRule | AnyRule;
|
|
140
|
+
export type Distinction = CompareRule | AllRule | AnyRule | RelationRule;
|
|
124
141
|
export declare function conjuncts(rule: Rule): readonly Rule[];
|
|
125
142
|
export declare const DEPS = "#deps";
|
|
126
143
|
export declare function depsTerm<T>(): TermOf<T>;
|
|
@@ -140,6 +157,9 @@ export declare function stepInto(rule: Rule, key: string): Rule | undefined;
|
|
|
140
157
|
export type ComparisonObserver = (rule: CompareRule, scope: unknown) => void;
|
|
141
158
|
export type DistinctionObserver = (distinction: Distinction, outcome: boolean) => void;
|
|
142
159
|
export declare function holds(rule: Rule, value: unknown, observe?: ComparisonObserver, decide?: DistinctionObserver): boolean;
|
|
160
|
+
export declare function relates(rule: RelationRule, value: unknown): boolean;
|
|
161
|
+
export declare function sameElement(left: unknown, right: unknown): boolean;
|
|
162
|
+
export declare function relationHolds(relation: Relation, subject: unknown, other: unknown): boolean;
|
|
143
163
|
export declare function satisfy(rule: Rule, value: unknown, stepAt?: StepAt): unknown;
|
|
144
164
|
export type ElementLabels = Readonly<Record<string, string>>;
|
|
145
165
|
export declare function describeRule(rule: Rule, path?: string, elements?: ElementLabels): string;
|
|
@@ -148,6 +168,7 @@ export type StepAt = (path: readonly string[]) => Step | {
|
|
|
148
168
|
readonly among: readonly unknown[];
|
|
149
169
|
} | undefined;
|
|
150
170
|
export declare function isDecimal(value: unknown): value is Decimal;
|
|
171
|
+
export declare function copyOf<V>(value: V): V;
|
|
151
172
|
export declare function decimalUnits(value: Decimal, scale: number, rounding: "floor" | "ceil"): bigint;
|
|
152
173
|
export declare function decimalOfUnits(like: Decimal, units: bigint, scale: number): Decimal;
|
|
153
174
|
export declare function decimalStep(scale: number): Step;
|
|
@@ -165,6 +186,7 @@ export declare function positionData(term: Term<unknown>): PositionTermData | un
|
|
|
165
186
|
export declare function positionOf(term: Term<unknown>): PositionTermData;
|
|
166
187
|
export declare function readOperand(operand: unknown, value: unknown): unknown;
|
|
167
188
|
export declare function localLabel(name: string): string;
|
|
189
|
+
export declare function describeLiteral(value: unknown): string;
|
|
168
190
|
export declare function describeTerm(term: Term<unknown>, path?: string, elements?: ElementLabels): string;
|
|
169
191
|
export declare function rebaseTerm(term: Term<unknown>, move: (path: readonly string[]) => readonly string[]): Term<unknown>;
|
|
170
192
|
export declare function rebaseRule(rule: Rule, move: (path: readonly string[]) => readonly string[]): Rule;
|
|
@@ -183,4 +205,5 @@ export declare function normalize(rule: Rule): {
|
|
|
183
205
|
bound: unknown;
|
|
184
206
|
measure: Measure;
|
|
185
207
|
} | undefined;
|
|
208
|
+
export declare function constantsOf(rules: readonly Rule[]): unknown[];
|
|
186
209
|
export {};
|
package/dist/rule.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Rational, isRational, fractionOf, decimalOf } from "./exact.js";
|
|
2
2
|
import { compareText } from "./text.js";
|
|
3
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");
|
|
@@ -80,6 +82,8 @@ export function termPaths(rule) {
|
|
|
80
82
|
switch (rule.kind) {
|
|
81
83
|
case "compare":
|
|
82
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)));
|
|
83
87
|
case "all":
|
|
84
88
|
case "any":
|
|
85
89
|
return [positionOf(rule.of).path];
|
|
@@ -95,6 +99,7 @@ export function termPaths(rule) {
|
|
|
95
99
|
export function pathsRead(rule) {
|
|
96
100
|
switch (rule.kind) {
|
|
97
101
|
case "compare":
|
|
102
|
+
case "relation":
|
|
98
103
|
return termPaths(rule);
|
|
99
104
|
case "all":
|
|
100
105
|
case "any":
|
|
@@ -146,6 +151,13 @@ export function shiftTerms(rule) {
|
|
|
146
151
|
return { ...rule, left: shift(rule.left), right: shift(rule.right) };
|
|
147
152
|
}
|
|
148
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
|
+
}
|
|
149
161
|
const path = boundTermPath(rule);
|
|
150
162
|
if (rule.kind !== "compare" || path === undefined || path[0] !== key) {
|
|
151
163
|
return undefined;
|
|
@@ -178,6 +190,45 @@ export function holds(rule, value, observe, decide) {
|
|
|
178
190
|
decide?.(rule, outcome);
|
|
179
191
|
return outcome;
|
|
180
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));
|
|
181
232
|
}
|
|
182
233
|
}
|
|
183
234
|
function compares(rule, value, observe) {
|
|
@@ -188,6 +239,8 @@ function compares(rule, value, observe) {
|
|
|
188
239
|
return true;
|
|
189
240
|
}
|
|
190
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));
|
|
191
244
|
switch (rule.operator) {
|
|
192
245
|
case "<":
|
|
193
246
|
return order < 0;
|
|
@@ -198,9 +251,9 @@ function compares(rule, value, observe) {
|
|
|
198
251
|
case ">=":
|
|
199
252
|
return order >= 0;
|
|
200
253
|
case "==":
|
|
201
|
-
return
|
|
254
|
+
return alike;
|
|
202
255
|
case "!=":
|
|
203
|
-
return
|
|
256
|
+
return !alike;
|
|
204
257
|
}
|
|
205
258
|
}
|
|
206
259
|
// `stepAt` names the step of the value a term reads, where it is not the default
|
|
@@ -224,10 +277,34 @@ export function satisfy(rule, value, stepAt) {
|
|
|
224
277
|
}
|
|
225
278
|
case "compare":
|
|
226
279
|
return satisfyComparison(rule, value, stepAt);
|
|
280
|
+
case "relation":
|
|
281
|
+
return satisfyRelation(rule, value);
|
|
227
282
|
default:
|
|
228
283
|
return value;
|
|
229
284
|
}
|
|
230
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
|
+
}
|
|
231
308
|
function satisfyComparison(rule, value, stepAt) {
|
|
232
309
|
const [term, other, operator] = isTerm(rule.left) && positionData(rule.left) !== undefined
|
|
233
310
|
? [rule.left, rule.right, rule.operator]
|
|
@@ -255,8 +332,10 @@ function satisfyComparison(rule, value, stepAt) {
|
|
|
255
332
|
return chosen === undefined ? value : writeAt(value, path, () => chosen);
|
|
256
333
|
}
|
|
257
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;
|
|
258
337
|
const target = operator === ">" || operator === "!="
|
|
259
|
-
? move(bound, 1)
|
|
338
|
+
? operator === "!=" && above === bound ? move(bound, -1) : above
|
|
260
339
|
: operator === "<"
|
|
261
340
|
? move(bound, -1)
|
|
262
341
|
: bound;
|
|
@@ -286,6 +365,8 @@ export function describeRule(rule, path = "$", elements = {}) {
|
|
|
286
365
|
return `${rule.kind}(${rule.rules.map(part => describeRule(part, path, elements)).join(", ")})`;
|
|
287
366
|
case "not":
|
|
288
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)})`;
|
|
289
370
|
case "compare":
|
|
290
371
|
break;
|
|
291
372
|
}
|
|
@@ -308,6 +389,23 @@ export function isDecimal(value) {
|
|
|
308
389
|
typeof value.toFixed === "function" &&
|
|
309
390
|
typeof value.decimalPlaces === "function");
|
|
310
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
|
+
}
|
|
311
409
|
// A decimal of `scale` digits after the point, as a whole number of units of its
|
|
312
410
|
// last digit: exact, and free of the precision a Decimal's arithmetic rounds to.
|
|
313
411
|
// A value off that grid is rounded down or up, as `rounding` says.
|
|
@@ -338,6 +436,10 @@ function step(bound, direction) {
|
|
|
338
436
|
if (typeof bound === "number") {
|
|
339
437
|
return bound + direction;
|
|
340
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
|
+
}
|
|
341
443
|
const add = bound?.add;
|
|
342
444
|
if (typeof add === "function") {
|
|
343
445
|
const moved = add.call(bound, isPlainDate(bound) ? { days: direction } : { nanoseconds: direction });
|
|
@@ -426,8 +528,10 @@ export function rewrite(current, measure, target, empty) {
|
|
|
426
528
|
const resized = resize(transformed(current, transforms), target);
|
|
427
529
|
return measured(resized, measure) === target ? resized : resize("", target);
|
|
428
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);
|
|
429
533
|
function compare(operator, left, right) {
|
|
430
|
-
return condition({ kind: "compare", operator, left, right });
|
|
534
|
+
return condition({ kind: "compare", operator, left: held(left), right: held(right) });
|
|
431
535
|
}
|
|
432
536
|
const OPERATORS = {
|
|
433
537
|
lt: "<",
|
|
@@ -469,6 +573,12 @@ function termAt(path, measure) {
|
|
|
469
573
|
case "all":
|
|
470
574
|
case "any":
|
|
471
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 });
|
|
472
582
|
default:
|
|
473
583
|
return termAt([...path, key], "value");
|
|
474
584
|
}
|
|
@@ -631,6 +741,8 @@ function read(operand, value) {
|
|
|
631
741
|
return found === undefined ? found : measured(found, measure);
|
|
632
742
|
}
|
|
633
743
|
function ordering(left, right) {
|
|
744
|
+
if (left instanceof Date && right instanceof Date)
|
|
745
|
+
return left.getTime() - right.getTime();
|
|
634
746
|
if (typeof left === "bigint" && typeof right === "bigint")
|
|
635
747
|
return left < right ? -1 : left > right ? 1 : 0;
|
|
636
748
|
if (isRational(left) && isRational(right))
|
|
@@ -665,12 +777,18 @@ export function localLabel(name) {
|
|
|
665
777
|
const at = rest.indexOf("@");
|
|
666
778
|
return at === -1 ? `local${rest}` : rest.slice(at + 1);
|
|
667
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
|
+
}
|
|
668
786
|
export function describeTerm(term, path = "$", elements = {}) {
|
|
669
787
|
return describeOperand(term, path, elements);
|
|
670
788
|
}
|
|
671
789
|
function describeOperand(operand, path, elements = {}) {
|
|
672
790
|
if (!isTerm(operand)) {
|
|
673
|
-
return
|
|
791
|
+
return describeLiteral(operand);
|
|
674
792
|
}
|
|
675
793
|
const data = termData(operand);
|
|
676
794
|
if (data.kind === "linear") {
|
|
@@ -711,6 +829,8 @@ export function rebaseRule(rule, move) {
|
|
|
711
829
|
switch (rule.kind) {
|
|
712
830
|
case "compare":
|
|
713
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) });
|
|
714
834
|
case "all":
|
|
715
835
|
case "any":
|
|
716
836
|
return named({ ...rule, of: term(rule.of), each: rebaseRule(rule.each, path => (path[0] === rule.element ? path : move(path))) });
|
|
@@ -822,6 +942,12 @@ export function substitutedRule(rule, replace) {
|
|
|
822
942
|
? named({ kind: "compare", operator, left: left.with, right: multiplied })
|
|
823
943
|
: named({ kind: "compare", operator, left: multiplied, right: right.with });
|
|
824
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
|
+
}
|
|
825
951
|
case "all":
|
|
826
952
|
case "any": {
|
|
827
953
|
const of = operand(rule.of);
|
|
@@ -863,4 +989,18 @@ export function normalize(rule) {
|
|
|
863
989
|
}
|
|
864
990
|
return undefined;
|
|
865
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
|
+
}
|
|
866
1006
|
//# sourceMappingURL=rule.js.map
|