@toiroakr/chisel 0.2.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 +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 +13 -6
- package/dist/tla.js +184 -41
- 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/model.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { isPromiseLike, snapshotValue } from "./data.js";
|
|
2
|
-
import { Rational, isRational, fractionOf, decimalOf, EvaluationLimit } from "./exact.js";
|
|
2
|
+
import { Rational, isRational, fractionOf, decimalOf, EvaluationLimit, OutOfRoom, roundedAt } from "./exact.js";
|
|
3
3
|
import { DEPS, depsTerm, describeTerm, holds, rebaseRule, rebaseTerm, pathsRead as pathsReadOfRule, isDecimal, isTerm, linearCombination, positionData, readOperand, selfTerm, substitutedOperand, substitutedRule, termData, rootTerm, positionTerm, withLocal } from "./rule.js";
|
|
4
|
-
import { isVariantsSchema } from "./schema.js";
|
|
5
|
-
import { outcomesOf } from "./outcomes.js";
|
|
4
|
+
import { array, isVariantsSchema } from "./schema.js";
|
|
5
|
+
import { NO_FACTS, outcomesOf } from "./outcomes.js";
|
|
6
6
|
import { countBetween, moveTemporal } from "./temporal.js";
|
|
7
7
|
const EXPRESSION = Symbol.for("chisel.expression");
|
|
8
8
|
function expression(node) {
|
|
@@ -61,11 +61,38 @@ export function matchValue(schema, value, cases) {
|
|
|
61
61
|
body = expression({ kind: "choose", condition: { kind: "compare", left: positionTerm([name, schema.discriminant], "value"), operator: "==", right: tags[index] }, yes: bodies[index], no: body });
|
|
62
62
|
return expression({ kind: "bind", name, schema, value, body });
|
|
63
63
|
}
|
|
64
|
+
const STEPS = new WeakMap();
|
|
65
|
+
// A step's arguments are named by a tuple: `[Element]` for a map's, and
|
|
66
|
+
// `[Accumulator, Element]` for a fold's, the accumulator first.
|
|
67
|
+
export function step(build) {
|
|
68
|
+
if (typeof build !== "function")
|
|
69
|
+
throw new Error("step takes the step's body as a function of its arguments");
|
|
70
|
+
const value = Object.freeze({ description: "a step handed over as a value" });
|
|
71
|
+
STEPS.set(value, build);
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
// The step values a choose tree holds, with each leaf made into what `leaf`
|
|
75
|
+
// builds of its body.
|
|
76
|
+
function steppedBy(tree, leaf) {
|
|
77
|
+
const build = typeof tree === "object" && tree !== null ? STEPS.get(tree) : undefined;
|
|
78
|
+
if (build !== undefined)
|
|
79
|
+
return leaf(build);
|
|
80
|
+
const node = nodeOf(tree);
|
|
81
|
+
if (node?.kind === "choose")
|
|
82
|
+
return expression({ kind: "choose", condition: node.condition, yes: steppedBy(node.yes, leaf), no: steppedBy(node.no, leaf) });
|
|
83
|
+
throw new Error("A walk takes a step written in place, one made with step(), or a choice among such steps");
|
|
84
|
+
}
|
|
64
85
|
export function map(element, output, values, build) {
|
|
86
|
+
if (typeof build !== "function") {
|
|
87
|
+
return bind(array(element), values, list => steppedBy(build, body => map(element, output, list, body)));
|
|
88
|
+
}
|
|
65
89
|
const name = localName(values, "element");
|
|
66
90
|
return expression({ kind: "map", name, element: element, output: output, values, body: build(rootTerm(name)) });
|
|
67
91
|
}
|
|
68
92
|
export function fold(element, output, values, initial, build) {
|
|
93
|
+
if (typeof build !== "function") {
|
|
94
|
+
return bind(array(element), values, list => bind(output, initial, seed => steppedBy(build, body => fold(element, output, list, seed, body))));
|
|
95
|
+
}
|
|
69
96
|
const name = localName(values, "element"), accumulator = localName(undefined, "accumulator");
|
|
70
97
|
return expression({ kind: "fold", name, accumulator, element: element, output: output, values, initial, body: build(rootTerm(accumulator), rootTerm(name)) });
|
|
71
98
|
}
|
|
@@ -109,16 +136,23 @@ export function dependencyName(dependency) {
|
|
|
109
136
|
return position.path[1];
|
|
110
137
|
}
|
|
111
138
|
export function concat(left, right) {
|
|
139
|
+
evaluators.concat = concatOf;
|
|
112
140
|
return expression({ kind: "operation", operator: "concat", left, right });
|
|
113
141
|
}
|
|
114
142
|
export function arithmetic(operator, left, right) {
|
|
143
|
+
for (const name of ["add", "subtract", "multiply", "divide"])
|
|
144
|
+
evaluators[name] = arithmeticOf;
|
|
115
145
|
return expression({ kind: "operation", operator, left, right });
|
|
116
146
|
}
|
|
147
|
+
function temporalOf(node, left, right) {
|
|
148
|
+
return node.operator === "between" ? countBetween(left, right, node.unit) : moveTemporal(node.operator, left, right, node.unit, node.overflow);
|
|
149
|
+
}
|
|
117
150
|
function temporal(operator, left, right, unit, options = {}) {
|
|
118
151
|
const overflow = options.overflow ?? "constrain";
|
|
119
152
|
// Refused here, as Temporal would refuse it on every run: the proof reads no overflow.
|
|
120
153
|
if (overflow !== "constrain" && overflow !== "reject")
|
|
121
154
|
throw new Error(`${operator} takes an overflow of "constrain" or "reject", not ${String(overflow)}`);
|
|
155
|
+
evaluators.temporal = temporalOf;
|
|
122
156
|
return expression({ kind: "temporal", operator, left, right, unit, overflow });
|
|
123
157
|
}
|
|
124
158
|
export function plus(value, amount, unit, options) {
|
|
@@ -131,8 +165,27 @@ export function between(start, end, unit) {
|
|
|
131
165
|
return temporal("between", start, end, unit);
|
|
132
166
|
}
|
|
133
167
|
export function quotient(left, right) {
|
|
168
|
+
evaluators.quotient = quotientOf;
|
|
134
169
|
return expression({ kind: "operation", operator: "quotient", left, right });
|
|
135
170
|
}
|
|
171
|
+
const ROUNDINGS = ["half-up", "half-down", "half-even", "up", "down", "ceiling", "floor"];
|
|
172
|
+
// A decimal of `scale` digits after the point, rounded as `mode` says
|
|
173
|
+
// (ADR-0128): a decimal, a rational, an integer or an int64 rounded exactly, at
|
|
174
|
+
// any scale, so it always has an answer.
|
|
175
|
+
export function round(value, scale, mode) {
|
|
176
|
+
if (!Number.isSafeInteger(scale) || scale < 0)
|
|
177
|
+
throw new Error(`round takes a whole number of digits, not ${scale}`);
|
|
178
|
+
if (!ROUNDINGS.includes(mode))
|
|
179
|
+
throw new Error(`round takes a rounding mode of ${ROUNDINGS.join(", ")}, not ${String(mode)}`);
|
|
180
|
+
evaluators.round = roundOf;
|
|
181
|
+
return expression({ kind: "operation", operator: "round", left: value, right: scale, mode });
|
|
182
|
+
}
|
|
183
|
+
// The quotient of two numbers as a decimal of `scale` digits, rounded as
|
|
184
|
+
// `mode` says, as Souther's Decimal.divide is: it has an answer wherever the
|
|
185
|
+
// divisor is not nought.
|
|
186
|
+
export function divide(left, right, scale, mode) {
|
|
187
|
+
return round(quotient(left, right), scale, mode);
|
|
188
|
+
}
|
|
136
189
|
export function nodeOf(value) {
|
|
137
190
|
return typeof value === "object" && value !== null && !isTerm(value)
|
|
138
191
|
? value[EXPRESSION] : undefined;
|
|
@@ -240,20 +293,22 @@ export function choicesOf(template) {
|
|
|
240
293
|
visit(template);
|
|
241
294
|
return nodes;
|
|
242
295
|
}
|
|
243
|
-
function checkedTemplate(value, ancestors = new Set(), remaining = { value: 10000 }) {
|
|
296
|
+
export function checkedTemplate(value, ancestors = new Set(), remaining = { value: 10000 }) {
|
|
244
297
|
if (--remaining.value < 0)
|
|
245
298
|
throw new Error("Model declaration exceeds 10000 nodes");
|
|
246
299
|
if (typeof value === "function" || typeof value === "symbol")
|
|
247
300
|
throw new Error("Model expressions cannot contain callbacks or symbols");
|
|
301
|
+
if (typeof value === "object" && value !== null && STEPS.has(value))
|
|
302
|
+
throw new Error("A step made with step() is handed to map or fold, not left in a model");
|
|
248
303
|
if (isTerm(value))
|
|
249
304
|
return value;
|
|
250
305
|
if (atom(value)) {
|
|
251
306
|
if (isDecimal(value))
|
|
252
307
|
return snapshotValue(value);
|
|
253
|
-
if (
|
|
254
|
-
if (
|
|
255
|
-
throw new Error("Model
|
|
256
|
-
return value
|
|
308
|
+
if (value instanceof Date) {
|
|
309
|
+
if (Number.isNaN(value.getTime()))
|
|
310
|
+
throw new Error("Model constants cannot be an invalid Date");
|
|
311
|
+
return snapshotValue(value);
|
|
257
312
|
}
|
|
258
313
|
if (value && typeof value === "object" && !isDecimal(value) && !isRational(value) && !String(value.constructor?.name).startsWith("Plain") && value.constructor?.name !== "Instant")
|
|
259
314
|
throw new Error("Unsupported model constant");
|
|
@@ -283,16 +338,47 @@ function checkedTemplate(value, ancestors = new Set(), remaining = { value: 1000
|
|
|
283
338
|
export function model(id, build) {
|
|
284
339
|
const template = checkedTemplate(build(selfTerm(), depsTerm()));
|
|
285
340
|
return Object.freeze({
|
|
286
|
-
kind: "rules", id, expression: template,
|
|
341
|
+
kind: "rules", id, expression: template, runs: template,
|
|
287
342
|
guards: guardsOf(template),
|
|
288
343
|
otherwise: () => { throw new Error("Expression models are interpreted"); },
|
|
289
344
|
});
|
|
290
345
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
346
|
+
let walkRewrite;
|
|
347
|
+
let walkObserver;
|
|
348
|
+
export function withWalk(rewrite, observer, run) {
|
|
349
|
+
const [rewriteBefore, observerBefore] = [walkRewrite, walkObserver];
|
|
350
|
+
walkRewrite = rewrite;
|
|
351
|
+
walkObserver = observer;
|
|
352
|
+
try {
|
|
353
|
+
return run();
|
|
354
|
+
}
|
|
355
|
+
finally {
|
|
356
|
+
walkRewrite = rewriteBefore;
|
|
357
|
+
walkObserver = observerBefore;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
let stopObserver;
|
|
361
|
+
export function withStops(observer, run) {
|
|
362
|
+
const before = stopObserver;
|
|
363
|
+
stopObserver = observer;
|
|
364
|
+
try {
|
|
365
|
+
return run();
|
|
366
|
+
}
|
|
367
|
+
finally {
|
|
368
|
+
stopObserver = before;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
function stopping(step, compute) {
|
|
372
|
+
if (stopObserver === undefined)
|
|
373
|
+
return compute();
|
|
374
|
+
try {
|
|
375
|
+
return compute();
|
|
376
|
+
}
|
|
377
|
+
catch (error) {
|
|
378
|
+
stopObserver(step, error);
|
|
379
|
+
throw error;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
296
382
|
export function interpret(template, scope, options = {}) {
|
|
297
383
|
let remaining = options.steps ?? 100000;
|
|
298
384
|
const choices = choicesOf(template);
|
|
@@ -303,12 +389,27 @@ export function interpret(template, scope, options = {}) {
|
|
|
303
389
|
throw new Error(`Invalid local construction: ${result.issues.map(issue => `${issue.path}: ${issue.message}`).join("; ")}`);
|
|
304
390
|
return snapshotValue(result.value);
|
|
305
391
|
}
|
|
392
|
+
const inPart = new Set();
|
|
306
393
|
function* read(value, current = scope) {
|
|
307
394
|
if (--remaining < 0)
|
|
308
395
|
throw new EvaluationLimit("Model evaluation step budget exceeded");
|
|
309
396
|
if (isTerm(value))
|
|
310
397
|
return readOperand(value, current);
|
|
311
398
|
const node = nodeOf(value);
|
|
399
|
+
if (node !== undefined && options.parts?.has(node) && !inPart.has(node)) {
|
|
400
|
+
const around = distinguishing;
|
|
401
|
+
const steps = [];
|
|
402
|
+
distinguishing = (distinction, outcome) => steps.push({ distinction, outcome });
|
|
403
|
+
inPart.add(node);
|
|
404
|
+
try {
|
|
405
|
+
return yield* read(value, current);
|
|
406
|
+
}
|
|
407
|
+
finally {
|
|
408
|
+
inPart.delete(node);
|
|
409
|
+
distinguishing = around;
|
|
410
|
+
options.iterate?.(node, steps);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
312
413
|
if (node?.kind === "bind")
|
|
313
414
|
return yield* read(node.body, withLocal(current, node.name, parsed(node.schema, yield* read(node.value, current))));
|
|
314
415
|
if (node?.kind === "map" || node?.kind === "fold") {
|
|
@@ -316,8 +417,16 @@ export function interpret(template, scope, options = {}) {
|
|
|
316
417
|
if (!Array.isArray(values))
|
|
317
418
|
throw new Error("Iteration expects an array");
|
|
318
419
|
let accumulator = node.kind === "fold" ? parsed(node.output, yield* read(node.initial, current)) : undefined;
|
|
420
|
+
const seed = accumulator;
|
|
421
|
+
const walk = node.kind === "fold" && walkRewrite?.node === node ? walkRewrite.walk : undefined;
|
|
422
|
+
if (node.kind === "fold")
|
|
423
|
+
walkObserver?.(node, values.length);
|
|
319
424
|
const result = [];
|
|
320
|
-
for (const value of values) {
|
|
425
|
+
for (const [index, value] of values.entries()) {
|
|
426
|
+
if (walk === "after one step" && index > 0)
|
|
427
|
+
break;
|
|
428
|
+
if (walk === "from the seed")
|
|
429
|
+
accumulator = seed;
|
|
321
430
|
const elementScope = withLocal(current, node.name, parsed(node.element, value));
|
|
322
431
|
const iterationScope = node.kind === "fold" ? withLocal(elementScope, node.accumulator, accumulator) : elementScope;
|
|
323
432
|
const around = distinguishing;
|
|
@@ -352,43 +461,21 @@ export function interpret(template, scope, options = {}) {
|
|
|
352
461
|
return yield* read(outcome ? node.yes : node.no, current);
|
|
353
462
|
}
|
|
354
463
|
if (node?.kind === "construct") {
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
464
|
+
const value = yield* read(node.value, current);
|
|
465
|
+
return stopping(node, () => {
|
|
466
|
+
const parsed = node.schema.parse(value);
|
|
467
|
+
if (!parsed.success)
|
|
468
|
+
throw new Error(`Invalid construction: ${parsed.issues.map(issue => `${issue.path}: ${issue.message}`).join("; ")}`);
|
|
469
|
+
return parsed.value;
|
|
470
|
+
});
|
|
359
471
|
}
|
|
360
472
|
if (node?.kind === "temporal") {
|
|
361
473
|
const left = yield* read(node.left, current), right = yield* read(node.right, current);
|
|
362
|
-
return node
|
|
474
|
+
return stopping(node, () => evaluate("temporal", node, left, right));
|
|
363
475
|
}
|
|
364
476
|
if (node?.kind === "operation") {
|
|
365
477
|
const left = yield* read(node.left, current), right = yield* read(node.right, current);
|
|
366
|
-
|
|
367
|
-
if (typeof left !== "string" || typeof right !== "string")
|
|
368
|
-
throw new Error("concat expects strings");
|
|
369
|
-
return (left + right).normalize("NFC");
|
|
370
|
-
}
|
|
371
|
-
if (node.operator === "quotient")
|
|
372
|
-
return fractionOf(left).dividedBy(fractionOf(right));
|
|
373
|
-
if (isDecimal(left) && isDecimal(right)) {
|
|
374
|
-
const a = fractionOf(left), b = fractionOf(right);
|
|
375
|
-
return decimalOf(node.operator === "add" ? a.plus(b) : node.operator === "subtract" ? a.minus(b) : node.operator === "multiply" ? a.times(b) : a.dividedBy(b));
|
|
376
|
-
}
|
|
377
|
-
if (isRational(left) && isRational(right))
|
|
378
|
-
return node.operator === "add" ? left.plus(right) : node.operator === "subtract" ? left.minus(right) : node.operator === "multiply" ? left.times(right) : left.dividedBy(right);
|
|
379
|
-
if (typeof left === "bigint" && typeof right === "bigint") {
|
|
380
|
-
if (node.operator === "divide" && (right === 0n || left % right !== 0n))
|
|
381
|
-
throw new Error("Integer division must be exact and nonzero");
|
|
382
|
-
return node.operator === "add" ? left + right : node.operator === "subtract" ? left - right : node.operator === "multiply" ? left * right : left / right;
|
|
383
|
-
}
|
|
384
|
-
if (typeof left !== "number" || typeof right !== "number")
|
|
385
|
-
throw new Error("Arithmetic expects matching numeric types");
|
|
386
|
-
if (node.operator === "divide" && right === 0)
|
|
387
|
-
throw new Error("Division by zero");
|
|
388
|
-
const result = node.operator === "add" ? left + right : node.operator === "subtract" ? left - right : node.operator === "multiply" ? left * right : left / right;
|
|
389
|
-
if (!Number.isFinite(result) || Number.isInteger(result) && !Number.isSafeInteger(result))
|
|
390
|
-
throw new Error("Arithmetic result is outside the exact number range");
|
|
391
|
-
return result;
|
|
478
|
+
return stopping(node, () => operate(node, left, right));
|
|
392
479
|
}
|
|
393
480
|
if (atom(value))
|
|
394
481
|
return value;
|
|
@@ -413,6 +500,72 @@ export function interpret(template, scope, options = {}) {
|
|
|
413
500
|
}
|
|
414
501
|
return resume(evaluation, first.value);
|
|
415
502
|
}
|
|
503
|
+
// Each builder registers how its node is worked out, so a model that never builds
|
|
504
|
+
// one leaves the arithmetic it needs out of the bundle.
|
|
505
|
+
// Kept on the global object under a registered symbol, as the terms are, since the
|
|
506
|
+
// CLI loads a specification in a module graph of its own.
|
|
507
|
+
const EVALUATORS = Symbol.for("chisel.evaluators");
|
|
508
|
+
const evaluators = (globalThis[EVALUATORS] ??= {});
|
|
509
|
+
function evaluate(operator, node, left, right) {
|
|
510
|
+
const found = evaluators[operator];
|
|
511
|
+
if (found === undefined)
|
|
512
|
+
throw new Error(`No way to work out ${operator}`);
|
|
513
|
+
return found(node, left, right);
|
|
514
|
+
}
|
|
515
|
+
// What the analyses install, since a declaration made with the package root calls none of the builders above that register these.
|
|
516
|
+
export function installEvaluators() {
|
|
517
|
+
evaluators.concat = concatOf;
|
|
518
|
+
for (const name of ["add", "subtract", "multiply", "divide"])
|
|
519
|
+
evaluators[name] = arithmeticOf;
|
|
520
|
+
evaluators.temporal = temporalOf;
|
|
521
|
+
evaluators.quotient = quotientOf;
|
|
522
|
+
evaluators.round = roundOf;
|
|
523
|
+
}
|
|
524
|
+
function operate(node, left, right) {
|
|
525
|
+
try {
|
|
526
|
+
return evaluate(node.operator, node, left, right);
|
|
527
|
+
}
|
|
528
|
+
catch (error) {
|
|
529
|
+
// A whole number larger than the platform builds is a run short of room.
|
|
530
|
+
if (error instanceof RangeError)
|
|
531
|
+
throw new OutOfRoom(`No room to work out ${node.operator}: ${error.message}`);
|
|
532
|
+
throw error;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
function roundOf(node, left, right) {
|
|
536
|
+
if (typeof left === "number" && !Number.isSafeInteger(left))
|
|
537
|
+
throw new Error("round takes an exact number");
|
|
538
|
+
return decimalOf(roundedAt(fractionOf(left), right, node.mode ?? "half-even"));
|
|
539
|
+
}
|
|
540
|
+
function concatOf(_node, left, right) {
|
|
541
|
+
if (typeof left !== "string" || typeof right !== "string")
|
|
542
|
+
throw new Error("concat expects strings");
|
|
543
|
+
return (left + right).normalize("NFC");
|
|
544
|
+
}
|
|
545
|
+
function quotientOf(_node, left, right) {
|
|
546
|
+
return fractionOf(left).dividedBy(fractionOf(right));
|
|
547
|
+
}
|
|
548
|
+
function arithmeticOf(node, left, right) {
|
|
549
|
+
if (isDecimal(left) && isDecimal(right)) {
|
|
550
|
+
const a = fractionOf(left), b = fractionOf(right);
|
|
551
|
+
return decimalOf(node.operator === "add" ? a.plus(b) : node.operator === "subtract" ? a.minus(b) : node.operator === "multiply" ? a.times(b) : a.dividedBy(b));
|
|
552
|
+
}
|
|
553
|
+
if (isRational(left) && isRational(right))
|
|
554
|
+
return node.operator === "add" ? left.plus(right) : node.operator === "subtract" ? left.minus(right) : node.operator === "multiply" ? left.times(right) : left.dividedBy(right);
|
|
555
|
+
if (typeof left === "bigint" && typeof right === "bigint") {
|
|
556
|
+
if (node.operator === "divide" && (right === 0n || left % right !== 0n))
|
|
557
|
+
throw new Error("Integer division must be exact and nonzero");
|
|
558
|
+
return node.operator === "add" ? left + right : node.operator === "subtract" ? left - right : node.operator === "multiply" ? left * right : left / right;
|
|
559
|
+
}
|
|
560
|
+
if (typeof left !== "number" || typeof right !== "number")
|
|
561
|
+
throw new Error("Arithmetic expects matching numeric types");
|
|
562
|
+
if (node.operator === "divide" && right === 0)
|
|
563
|
+
throw new Error("Division by zero");
|
|
564
|
+
const result = node.operator === "add" ? left + right : node.operator === "subtract" ? left - right : node.operator === "multiply" ? left * right : left / right;
|
|
565
|
+
if (!Number.isFinite(result) || Number.isInteger(result) && !Number.isSafeInteger(result))
|
|
566
|
+
throw new Error("Arithmetic result is outside the exact number range");
|
|
567
|
+
return result;
|
|
568
|
+
}
|
|
416
569
|
async function resume(evaluation, pending) {
|
|
417
570
|
for (;;) {
|
|
418
571
|
let settled;
|
|
@@ -428,18 +581,74 @@ async function resume(evaluation, pending) {
|
|
|
428
581
|
pending = step.value;
|
|
429
582
|
}
|
|
430
583
|
}
|
|
431
|
-
|
|
584
|
+
// The parts of a model whose values meet only in its answer: each field of the answer (and each
|
|
585
|
+
// element of a list in it), through plain objects and lists and the bodies of binds of a value
|
|
586
|
+
// that chooses nothing, that holds a choice. No part reads what another computes, so the way a run
|
|
587
|
+
// takes through one never turns on the way it takes through another, and each part's ways are
|
|
588
|
+
// listed on their own rather than combined with every way of every other part. Undefined where
|
|
589
|
+
// fewer than two parts hold a choice, which leaves the model one part, as it always was.
|
|
590
|
+
const partLabels = new WeakMap();
|
|
591
|
+
const partTemplates = new WeakMap();
|
|
592
|
+
const partsCache = new WeakMap();
|
|
593
|
+
export function independentParts(template) {
|
|
594
|
+
if (template === null || typeof template !== "object")
|
|
595
|
+
return undefined;
|
|
596
|
+
const cached = partsCache.get(template);
|
|
597
|
+
if (cached !== undefined)
|
|
598
|
+
return cached ?? undefined;
|
|
599
|
+
const parts = [];
|
|
600
|
+
const collect = (value, at) => {
|
|
601
|
+
const node = nodeOf(value);
|
|
602
|
+
if (node === undefined) {
|
|
603
|
+
if (isTerm(value) || value === null || typeof value !== "object")
|
|
604
|
+
return;
|
|
605
|
+
if (Array.isArray(value))
|
|
606
|
+
value.forEach((child, index) => collect(child, `${at}[${index}]`));
|
|
607
|
+
else
|
|
608
|
+
for (const [key, child] of Object.entries(value))
|
|
609
|
+
collect(child, at === "" ? key : `${at}.${key}`);
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
if (choicesOf(value).length === 0)
|
|
613
|
+
return;
|
|
614
|
+
if (node.kind === "bind" && choicesOf(node.value).length === 0) {
|
|
615
|
+
collect(node.body, at);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (!parts.includes(node)) {
|
|
619
|
+
parts.push(node);
|
|
620
|
+
partTemplates.set(node, value);
|
|
621
|
+
if (!partLabels.has(node))
|
|
622
|
+
partLabels.set(node, at === "" ? "答え" : at);
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
collect(template, "");
|
|
626
|
+
const found = parts.length >= 2 ? parts : undefined;
|
|
627
|
+
partsCache.set(template, found ?? null);
|
|
628
|
+
return found;
|
|
629
|
+
}
|
|
630
|
+
export function partTemplate(node) {
|
|
631
|
+
return partTemplates.get(node);
|
|
632
|
+
}
|
|
633
|
+
export function partLabel(node) {
|
|
634
|
+
return node !== null && typeof node === "object" ? partLabels.get(node) : undefined;
|
|
635
|
+
}
|
|
636
|
+
export function isIteration(node) {
|
|
637
|
+
const kind = node?.kind;
|
|
638
|
+
return kind === "map" || kind === "fold";
|
|
639
|
+
}
|
|
640
|
+
export function* modelPaths(template, facts = NO_FACTS) {
|
|
432
641
|
const node = nodeOf(template);
|
|
433
642
|
if (node?.kind === "choose") {
|
|
434
|
-
for (const outcome of outcomesOf(node.condition))
|
|
435
|
-
for (const branch of modelPaths(outcome.result ? node.yes : node.no)) {
|
|
436
|
-
yield { value: branch.value, steps: [...outcome.steps, ...branch.steps], choices: [{ condition: node.condition, outcome: outcome.result }, ...branch.choices] };
|
|
643
|
+
for (const outcome of outcomesOf(node.condition, facts))
|
|
644
|
+
for (const branch of modelPaths(outcome.result ? node.yes : node.no, outcome.facts)) {
|
|
645
|
+
yield { value: branch.value, steps: [...outcome.steps, ...branch.steps], choices: [{ condition: node.condition, outcome: outcome.result }, ...branch.choices], facts: branch.facts };
|
|
437
646
|
}
|
|
438
647
|
return;
|
|
439
648
|
}
|
|
440
649
|
const children = node?.kind === "map" ? [node.values] : node?.kind === "fold" ? [node.values, node.initial] : childrenOf(template);
|
|
441
650
|
if (children.length === 0) {
|
|
442
|
-
yield { value: template, steps: [], choices: [] };
|
|
651
|
+
yield { value: template, steps: [], choices: [], facts };
|
|
443
652
|
return;
|
|
444
653
|
}
|
|
445
654
|
function* product(index, before) {
|
|
@@ -447,12 +656,12 @@ export function* modelPaths(template) {
|
|
|
447
656
|
yield before;
|
|
448
657
|
return;
|
|
449
658
|
}
|
|
450
|
-
for (const child of modelPaths(children[index]))
|
|
451
|
-
yield* product(index + 1, { value: undefined, values: [...before.values, child.value], steps: [...before.steps, ...child.steps], choices: [...before.choices, ...child.choices] });
|
|
659
|
+
for (const child of modelPaths(children[index], before.facts))
|
|
660
|
+
yield* product(index + 1, { value: undefined, values: [...before.values, child.value], steps: [...before.steps, ...child.steps], choices: [...before.choices, ...child.choices], facts: child.facts });
|
|
452
661
|
}
|
|
453
|
-
for (const path of product(0, { value: undefined, values: [], steps: [], choices: [] })) {
|
|
662
|
+
for (const path of product(0, { value: undefined, values: [], steps: [], choices: [], facts })) {
|
|
454
663
|
const value = replaceChildren(template, path.values);
|
|
455
|
-
yield { value, steps: path.steps, choices: path.choices };
|
|
664
|
+
yield { value, steps: path.steps, choices: path.choices, facts: path.facts };
|
|
456
665
|
}
|
|
457
666
|
}
|
|
458
667
|
export function replaceChildren(template, values) {
|
|
@@ -670,20 +879,20 @@ function writePath(value, path, leaf) {
|
|
|
670
879
|
function deepEqualValue(left, right) {
|
|
671
880
|
return left === right || (isDecimal(left) && isDecimal(right) && left.equals(right));
|
|
672
881
|
}
|
|
673
|
-
export function* iterationEntries(template, steps = []) {
|
|
882
|
+
export function* iterationEntries(template, steps = [], facts = NO_FACTS) {
|
|
674
883
|
const node = nodeOf(template);
|
|
675
884
|
if (node?.kind === "choose") {
|
|
676
885
|
// A choice whose branches hold no iteration is not expanded: its outcomes may be far more than any iteration needs.
|
|
677
886
|
if (iterationsOf(node.yes).length === 0 && iterationsOf(node.no).length === 0)
|
|
678
887
|
return;
|
|
679
|
-
for (const outcome of outcomesOf(node.condition))
|
|
680
|
-
yield* iterationEntries(outcome.result ? node.yes : node.no, [...steps, ...outcome.steps]);
|
|
888
|
+
for (const outcome of outcomesOf(node.condition, facts))
|
|
889
|
+
yield* iterationEntries(outcome.result ? node.yes : node.no, [...steps, ...outcome.steps], outcome.facts);
|
|
681
890
|
return;
|
|
682
891
|
}
|
|
683
892
|
if (node?.kind === "map" || node?.kind === "fold")
|
|
684
893
|
yield { node, steps };
|
|
685
894
|
for (const child of childrenOf(template))
|
|
686
|
-
yield* iterationEntries(child, steps);
|
|
895
|
+
yield* iterationEntries(child, steps, facts);
|
|
687
896
|
}
|
|
688
897
|
// Every map and fold in a model, each with the body one element is read in.
|
|
689
898
|
export function iterationsOf(template) {
|