gdp-wired 0.44.0 → 0.46.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 +58 -7
- package/dist/core/bitsets.d.ts +2 -0
- package/dist/core/bitsets.js +23 -0
- package/dist/core/bmc.js +6 -2
- package/dist/core/c/reader.d.ts +9 -1
- package/dist/core/c/reader.js +445 -38
- package/dist/core/calendar.d.ts +29 -0
- package/dist/core/calendar.js +82 -0
- package/dist/core/check.js +3 -2
- package/dist/core/cli.d.ts +4 -0
- package/dist/core/cli.js +335 -39
- package/dist/core/compose.js +3 -1
- package/dist/core/counts.d.ts +5 -2
- package/dist/core/counts.js +335 -88
- package/dist/core/cycles.js +8 -1
- package/dist/core/cyclic.d.ts +1 -0
- package/dist/core/cyclic.js +13 -7
- package/dist/core/data-synth.d.ts +3 -0
- package/dist/core/data-synth.js +252 -0
- package/dist/core/data.d.ts +8 -2
- package/dist/core/data.js +1113 -172
- package/dist/core/discover.js +3 -1
- package/dist/core/draft.d.ts +19 -0
- package/dist/core/draft.js +53 -0
- package/dist/core/dsl.d.ts +39 -3
- package/dist/core/dsl.js +250 -31
- package/dist/core/effect-prims.js +2 -2
- package/dist/core/effect.d.ts +1 -4
- package/dist/core/effect.js +1 -15
- package/dist/core/ex/reader.exs +17 -1
- package/dist/core/exists.d.ts +16 -1
- package/dist/core/exists.js +447 -50
- package/dist/core/extract-c.d.ts +2 -0
- package/dist/core/extract-c.js +185 -31
- package/dist/core/extract-check.d.ts +7 -0
- package/dist/core/extract-check.js +115 -3
- package/dist/core/extract-ex.js +3 -3
- package/dist/core/extract-go-check.d.ts +1 -1
- package/dist/core/extract-go-check.js +21 -13
- package/dist/core/extract-go.d.ts +1 -0
- package/dist/core/extract-go.js +58 -9
- package/dist/core/extract-ir.d.ts +1 -0
- package/dist/core/extract-ir.js +15 -0
- package/dist/core/extract-jvm.d.ts +1 -1
- package/dist/core/extract-jvm.js +65 -14
- package/dist/core/extract-orm.d.ts +12 -0
- package/dist/core/extract-orm.js +244 -0
- package/dist/core/extract-print.js +23 -1
- package/dist/core/extract-py.d.ts +1 -0
- package/dist/core/extract-py.js +4 -4
- package/dist/core/extract-rb.js +3 -3
- package/dist/core/extract-read.d.ts +25 -0
- package/dist/core/extract-read.js +60 -5
- package/dist/core/extract-rs.js +22 -20
- package/dist/core/extract-shapes.js +22 -0
- package/dist/core/extract-sol.d.ts +10 -1
- package/dist/core/extract-sol.js +89 -30
- package/dist/core/extract-step.d.ts +201 -4
- package/dist/core/extract-step.js +1514 -111
- package/dist/core/extract-swift.js +24 -13
- package/dist/core/extract.d.ts +9 -0
- package/dist/core/extract.js +88 -7
- package/dist/core/fiber-run.d.ts +5 -0
- package/dist/core/fiber-run.js +18 -0
- package/dist/core/fresh.d.ts +2 -2
- package/dist/core/fresh.js +7 -3
- package/dist/core/go/reader.go +7 -1
- package/dist/core/infer.d.ts +4 -1
- package/dist/core/infer.js +1020 -246
- package/dist/core/jvm/JavaReader.java +33 -8
- package/dist/core/jvm/Jvm.java +20 -1
- package/dist/core/jvm/KtReader.java +59 -6
- package/dist/core/laws.d.ts +10 -3
- package/dist/core/laws.js +193 -14
- package/dist/core/machine.d.ts +16 -3
- package/dist/core/machine.js +212 -28
- package/dist/core/nonterm.js +14 -1
- package/dist/core/orm/harness.js +61 -0
- package/dist/core/orm-fixtures/schema.prisma +5 -0
- package/dist/core/py/harness.py +260 -21
- package/dist/core/py/reader.py +278 -39
- package/dist/core/rb/reader.rb +52 -5
- package/dist/core/reader-fixtures/README.md +28 -0
- package/dist/core/reader-fixtures/c-693042.cpp +8 -0
- package/dist/core/reader-fixtures/go-685506.go +14 -0
- package/dist/core/reader-fixtures/jvm-724362.kt +8 -0
- package/dist/core/reader-fixtures/jvm-737430.kt +7 -0
- package/dist/core/reader-fixtures/jvm-765282.kt +3 -0
- package/dist/core/reader-fixtures/swift-735726.swift +15 -0
- package/dist/core/reader-fixtures/swift-735990.swift +15 -0
- package/dist/core/reader-fixtures/swift-739158.swift +4 -0
- package/dist/core/reader-fixtures/swift-744834.swift +15 -0
- package/dist/core/reader-fixtures/swift-746946.swift +31 -0
- package/dist/core/reader-fixtures/typescript-673062.ts.txt +2 -0
- package/dist/core/relation.d.ts +1 -1
- package/dist/core/relation.js +29 -2
- package/dist/core/relational.js +4 -2
- package/dist/core/rs/src/main.rs +18 -2
- package/dist/core/sample-cache.d.ts +8 -0
- package/dist/core/sample-cache.js +46 -0
- package/dist/core/security.d.ts +38 -0
- package/dist/core/security.js +88 -0
- package/dist/core/sol/harness.py +63 -7
- package/dist/core/sol-concrete.d.ts +23 -0
- package/dist/core/sol-concrete.js +195 -0
- package/dist/core/solver.js +8 -5
- package/dist/core/swift/Sources/gdp-swiftreader/main.swift +50 -7
- package/dist/core/system.d.ts +62 -1
- package/dist/core/system.js +263 -4
- package/dist/core/transfer.js +39 -9
- package/dist/core/translate.js +39 -10
- package/dist/core/watch.js +51 -6
- package/dist/interchange/check.js +10 -10
- package/dist/interchange/format.d.ts +4 -0
- package/dist/interchange/format.js +24 -6
- package/dist/kernel/cdcl.d.ts +5 -0
- package/dist/kernel/cdcl.js +88 -10
- package/dist/kernel/epr.js +141 -29
- package/dist/kernel/kernel.d.ts +31 -1
- package/dist/kernel/kernel.js +391 -101
- package/dist/kernel/model-sequence.d.ts +3 -0
- package/dist/kernel/model-sequence.js +34 -0
- package/dist/kernel/prove.d.ts +30 -1
- package/dist/kernel/prove.js +481 -71
- package/dist/kernel/reader.d.ts +3 -0
- package/dist/kernel/reader.js +101 -14
- package/dist/kernel/share.d.ts +1 -0
- package/dist/kernel/share.js +27 -0
- package/dist/lean-kernel/emit.d.ts +9 -3
- package/dist/lean-kernel/emit.js +200 -66
- package/dist/lean-kernel/lean-terms.js +5 -0
- package/dist/lean-kernel/library.js +10 -10
- package/package.json +7 -2
package/dist/core/laws.js
CHANGED
|
@@ -29,6 +29,8 @@
|
|
|
29
29
|
// mode == "closed", outcome == SLOW_ERROR, mode == :closed a choice by its value
|
|
30
30
|
import { Choice, Int, Str, Term, all, any, forall, no, not, yes } from "./dsl.js";
|
|
31
31
|
import { agreement, isolation, noninterference } from "./relational.js";
|
|
32
|
+
import { CalendarError, businessDays } from "./calendar.js";
|
|
33
|
+
export { businessDays } from "./calendar.js";
|
|
32
34
|
export class LawError extends Error {
|
|
33
35
|
}
|
|
34
36
|
// --- reading the code's text -------------------------------------------------------------------------------------------
|
|
@@ -56,7 +58,7 @@ export function lawsIn(text, where = '') {
|
|
|
56
58
|
}
|
|
57
59
|
const WORDS = { '->': '.', '::': '.', and: '&&', or: '||', not: '!', implies: '=>', '∧': '&&', '∨': '||', '¬': '!', '→': '=>', '==>': '=>', '≤': '<=', '≥': '>=', '≠': '!=', '∀': 'forall', AND: '&&', OR: '||', NOT: '!' };
|
|
58
60
|
function tokens(s) {
|
|
59
|
-
const out = [], re = /\s*(f?"(?:[^"\\]|\\.)*"|f?'(?:[^'\\]|\\.)*'|`[^`]
|
|
61
|
+
const out = [], re = /\s*(f?"(?:[^"\\]|\\.)*"|f?'(?:[^'\\]|\\.)*'|`[^`]*`|0[xX][0-9a-fA-F]+[uUlL]*|0[bB][01]+[uUlL]*|\d+(?:\.\d+)?[uUlL]*|@?[A-Za-z_][\w?]*|->|::|==>|=>|<=|>=|==|!=|&&|\|\||\/\/|[-+*%/<>()[\],.:!∧∨¬→≤≥≠∀&|])/y;
|
|
60
62
|
let m, at = 0;
|
|
61
63
|
while ((m = re.exec(s))) {
|
|
62
64
|
out.push(WORDS[m[1]] ?? m[1]);
|
|
@@ -67,15 +69,16 @@ function tokens(s) {
|
|
|
67
69
|
return out;
|
|
68
70
|
}
|
|
69
71
|
// Durations, as many seconds: linear over the clock (a day is 86,400 s: the request's time counts seconds, with no
|
|
70
|
-
// time zone, leap second or daylight saving). A month, a year
|
|
71
|
-
//
|
|
72
|
+
// time zone, leap second or daylight saving). A month, a year or the end of a day isn't a fixed number of seconds, so a
|
|
73
|
+
// law reading one is refused: state it in days. Business days are counted over a calendar (core/calendar.ts): only as
|
|
74
|
+
// elapsed between two times, `now - received <= 10 business days`.
|
|
72
75
|
const SECONDS = { ms: 0.001, millisecond: 0.001, milliseconds: 0.001, second: 1, seconds: 1, sec: 1, secs: 1, minute: 60, minutes: 60, min: 60, mins: 60,
|
|
73
76
|
hour: 3600, hours: 3600, day: 86_400, days: 86_400, week: 604_800, weeks: 604_800 };
|
|
74
77
|
const NOT_LINEAR = /^(months?|years?|quarters?|business|working|weekdays?|fortnights?)$/i;
|
|
75
78
|
function seconds(unit) {
|
|
76
79
|
const u = unit.toLowerCase();
|
|
77
80
|
if (NOT_LINEAR.test(u))
|
|
78
|
-
throw new LawError(`${unit}: not a fixed number of seconds (a month or a year has several lengths, a business day depends on the calendar): state it in days, hours or seconds`);
|
|
81
|
+
throw new LawError(`${unit}: not a fixed number of seconds (a month or a year has several lengths, a business day depends on the calendar): state it in days, hours or seconds, or count business days over a calendar (businessDays; in a law, now - t <= 10 business days)`);
|
|
79
82
|
return SECONDS[u];
|
|
80
83
|
}
|
|
81
84
|
// A duration in the clock's units ('s': time.time(), 'ms': Date.now()): duration('30 days') is 2,592,000.
|
|
@@ -116,7 +119,7 @@ export function parse(s) {
|
|
|
116
119
|
const or = left(['||'], () => and()), and = left(['&&'], () => neg());
|
|
117
120
|
const neg = () => (peek() === '!' ? (eat(), { k: 'un', op: 'not', x: neg() }) : cmp());
|
|
118
121
|
const cmp = () => {
|
|
119
|
-
const a =
|
|
122
|
+
const a = bor(), op = peek();
|
|
120
123
|
if (op === 'is') {
|
|
121
124
|
eat();
|
|
122
125
|
const n = peek() === '!' ? (eat(), true) : false;
|
|
@@ -130,10 +133,12 @@ export function parse(s) {
|
|
|
130
133
|
}
|
|
131
134
|
if (op && ['<', '<=', '>', '>=', '==', '!=', 'in'].includes(op)) {
|
|
132
135
|
eat();
|
|
133
|
-
return { k: 'bin', op, a, b:
|
|
136
|
+
return { k: 'bin', op, a, b: bor() };
|
|
134
137
|
}
|
|
135
138
|
return a;
|
|
136
139
|
};
|
|
140
|
+
// (x & K and x | K, a flag tested or set by a constant: below the comparisons, as Python's, so flags & K != 0 is (flags & K) != 0)
|
|
141
|
+
const bor = left(['|'], () => band()), band = left(['&'], () => sum());
|
|
137
142
|
const sum = left(['+', '-'], () => prod()), prod = left(['*', '//', '/', '%'], () => unary());
|
|
138
143
|
const unary = () => (peek() === '-' ? (eat(), { k: 'un', op: '-', x: unary() }) : atom());
|
|
139
144
|
const atom = () => {
|
|
@@ -150,13 +155,19 @@ export function parse(s) {
|
|
|
150
155
|
throw new LawError(`${x}: a number that isn't whole`);
|
|
151
156
|
// (a duration: 30 days, 15 minutes, in seconds until the clock's unit is known)
|
|
152
157
|
const u = peek()?.toLowerCase(), calendar = u === 'calendar' && /^days?$/i.test(t[i + 1] ?? '');
|
|
158
|
+
if ((u === 'business' || u === 'working') && /^days?$/i.test(t[i + 1] ?? '')) {
|
|
159
|
+
eat();
|
|
160
|
+
eat();
|
|
161
|
+
return { k: 'bdur', v: Number(x), text: `${x} ${u} days` };
|
|
162
|
+
}
|
|
153
163
|
if (u && (calendar || Object.hasOwn(SECONDS, u) || NOT_LINEAR.test(u))) {
|
|
154
164
|
eat();
|
|
155
165
|
if (calendar)
|
|
156
166
|
eat();
|
|
157
167
|
return { k: 'dur', v: Number(x) * seconds(calendar ? 'days' : u), text: `${x} ${u}${calendar ? ' days' : ''}` };
|
|
158
168
|
}
|
|
159
|
-
|
|
169
|
+
// (C's and the others' hexadecimal and binary literals, an unsigned or long suffix)
|
|
170
|
+
return { k: 'num', v: Number(x.replace(/[uUlL]+$/, '')) };
|
|
160
171
|
}
|
|
161
172
|
if (/^f?["'`]/.test(x))
|
|
162
173
|
return str(x);
|
|
@@ -241,6 +252,16 @@ export function namesWithClock(x) {
|
|
|
241
252
|
const ns = x.names ?? [], ins = x.rule?.parts?.inputs.map(([n]) => n) ?? [];
|
|
242
253
|
return ns.some(n => n.role === 'clock') || !ins.includes('now') || ns.some(n => n.code === 'now') ? ns : [...ns, { code: 'now', rule: 'now', role: 'clock' }];
|
|
243
254
|
}
|
|
255
|
+
const calendarOf = (opts) => {
|
|
256
|
+
if (!opts.calendar)
|
|
257
|
+
return undefined;
|
|
258
|
+
try {
|
|
259
|
+
return businessDays(opts.calendar, opts.clock === 1000 ? 'ms' : 's');
|
|
260
|
+
}
|
|
261
|
+
catch (e) {
|
|
262
|
+
throw e instanceof CalendarError ? new LawError(`the calendar: ${e.message}`) : e;
|
|
263
|
+
}
|
|
264
|
+
};
|
|
244
265
|
export function buildLaw(x, l, opts = {}) {
|
|
245
266
|
const names = namesWithClock(x), r = x.rule;
|
|
246
267
|
if (!r)
|
|
@@ -249,7 +270,8 @@ export function buildLaw(x, l, opts = {}) {
|
|
|
249
270
|
throw new LawError(`${l.name}: a law is stated as <name>: <expr>`);
|
|
250
271
|
const e = parse(l.expr);
|
|
251
272
|
// A first reading finds what the law reads: a request, a result, the failure or the state before make it a step law.
|
|
252
|
-
const
|
|
273
|
+
const cal = opts.clock === undefined ? undefined : calendarOf(opts);
|
|
274
|
+
const probe = { consts: x.consts, b: {}, a: {}, step: true, method: l.method, keys: new Map(), roles: new Set(), methods: new Set(), clock: opts.clock, cal };
|
|
253
275
|
read(e, probe, names, true);
|
|
254
276
|
// (a law on a method, the line above it, is a law of its step: the state after it, old(x) before)
|
|
255
277
|
const step = !!l.method || ['input', 'answer', 'result', 'failed', 'clock', 'method', 'old'].some(k => probe.roles.has(k));
|
|
@@ -257,12 +279,48 @@ export function buildLaw(x, l, opts = {}) {
|
|
|
257
279
|
if (probe.methods.size > 1)
|
|
258
280
|
throw new LawError(`it reads the arguments of ${[...probe.methods].join(' and ')}: a law of one step is about one method`);
|
|
259
281
|
const gate = step && method && names.some(n => n.role === 'method') ? method : undefined;
|
|
260
|
-
const
|
|
282
|
+
const body0 = (b, a, pins, bits) => read(e, { consts: x.consts, b, a, step, method, keys: new Map(), roles: new Set(), methods: new Set(), clock: opts.clock, cal, pins, ...(bits ? { bits } : {}) }, names, false);
|
|
283
|
+
// (a step law's bit tests: read once to count the keys they take, then at every value of them, the facts implying it)
|
|
284
|
+
const bitBody = (b, a) => {
|
|
285
|
+
const count = { ks: [], used: 0, hyps: [] };
|
|
286
|
+
const plain = body0(b, a, undefined, count);
|
|
287
|
+
if (!count.used)
|
|
288
|
+
return plain;
|
|
289
|
+
if (count.used > 6)
|
|
290
|
+
throw new LawError(`its bit tests take ${count.used} keys in a law of one step (at most 6: two runs of bits at a place above bit 0, three at bit 0)`);
|
|
291
|
+
const at = (...ks) => { const bits = { ks, used: 0, hyps: [] }; const out = body0(b, a, undefined, bits); return all(...bits.hyps).implies(out); };
|
|
292
|
+
const n = count.used;
|
|
293
|
+
return n === 1 ? forall(k => at(k)) : n === 2 ? forall((k, j) => at(k, j)) : n === 3 ? forall((k, j, i) => at(k, j, i)) : n === 4 ? forall((k, j, i, h) => at(k, j, i, h)) : n === 5 ? forall((k, j, i, h, g) => at(k, j, i, h, g)) : forall((k, j, i, h, g, f) => at(k, j, i, h, g, f));
|
|
294
|
+
};
|
|
295
|
+
// (a law of one step reads no function, so business days there pin each time's week to a key, ∀w. guard → law)
|
|
296
|
+
const body = (b, a) => (step && probe.roles.has('bdays') ? pinned(b, a, (bb, aa, p) => body0(bb, aa, p)) : bitBody(b, a));
|
|
261
297
|
if (!step)
|
|
262
|
-
return { kind: 'law', law: r.law(l.name, (env) => (x.quiet ? env[x.quiet].eq(0).implies(
|
|
298
|
+
return { kind: 'law', law: r.law(l.name, (env) => (x.quiet ? env[x.quiet].eq(0).implies(body0(env, env)) : body0(env, env))) };
|
|
263
299
|
const mname = names.find(n => n.role === 'method')?.rule;
|
|
264
300
|
return { kind: 'step', ...(gate ? { method: gate } : {}), law: r.stepLaw(l.name, (b, a) => body(b, a), gate ? (b) => b[mname].is(gate) : undefined) };
|
|
265
301
|
}
|
|
302
|
+
// A law of one step over business days: read once to find its times, then held at every week of each (a key, two for
|
|
303
|
+
// a time that may be below 0), under the guards that only each time's own week meets.
|
|
304
|
+
function pinned(b, a, f) {
|
|
305
|
+
const pins = { times: [] };
|
|
306
|
+
f(b, a, pins);
|
|
307
|
+
const n = pins.times.reduce((k, t) => k + (t.nat ? 1 : 2), 0);
|
|
308
|
+
if (n > 6)
|
|
309
|
+
throw new LawError('business days at more than 6 weeks at once (a time that may be below 0 takes two)');
|
|
310
|
+
const g = (ks) => {
|
|
311
|
+
const at = new Map(), guards = [];
|
|
312
|
+
let i = 0;
|
|
313
|
+
for (const t of pins.times)
|
|
314
|
+
at.set(t.id, t.nat ? Int.of(ks[i++]) : new Int(ks[i++], ks[i++]));
|
|
315
|
+
const out = f(b, a, { times: pins.times, at, guards });
|
|
316
|
+
if ('forall' in out)
|
|
317
|
+
throw new LawError('business days in a law over every key: not yet (state it at one key)');
|
|
318
|
+
return all(...guards).implies(out);
|
|
319
|
+
};
|
|
320
|
+
const fs = [(k) => g([k]), (k, j) => g([k, j]), (k, j, h) => g([k, j, h]), (k, j, h, m) => g([k, j, h, m]),
|
|
321
|
+
(k, j, h, m, o) => g([k, j, h, m, o]), (k, j, h, m, o, p) => g([k, j, h, m, o, p])];
|
|
322
|
+
return n ? forall(fs[n - 1]) : f(b, a, undefined);
|
|
323
|
+
}
|
|
266
324
|
// The expression read: its value (`dry`: only which names it reads, nothing built).
|
|
267
325
|
function read(e, c, names, dry) {
|
|
268
326
|
if (e.k === 'forall') {
|
|
@@ -326,6 +384,7 @@ function val(e, c, names, dry) {
|
|
|
326
384
|
const go = (x) => val(x, c, names, dry);
|
|
327
385
|
switch (e.k) {
|
|
328
386
|
case 'num': return { t: 'num', v: Int.of(e.v) };
|
|
387
|
+
case 'bdur': throw new LawError(`${e.text}: business days are elapsed between two times, compared: now - received <= ${e.v} business days`);
|
|
329
388
|
case 'dur':
|
|
330
389
|
if (c.clock === undefined)
|
|
331
390
|
throw new LawError(`${e.text}: the clock's unit isn't known here (clock = "s" or "ms" in gdp.toml)`);
|
|
@@ -390,6 +449,8 @@ function val(e, c, names, dry) {
|
|
|
390
449
|
// over the integers (a <= x // k is k·a <= x; a < x // k is k·(a + 1) <= x), so it needs no function, which a law
|
|
391
450
|
// of one step can't read)
|
|
392
451
|
const flip = { '<': '>', '<=': '>=', '>': '<', '>=': '<=', '==': '==', '!=': '!=' };
|
|
452
|
+
if (flip[e.op] && (e.a.k === 'bdur' || e.b.k === 'bdur'))
|
|
453
|
+
return { t: 'cond', v: businessCompare(e, flip, c, go, dry) };
|
|
393
454
|
const divk = (x) => (x.k === 'bin' && x.op === '//' && (constOf(x.b) ?? 0) >= 1 ? x : undefined);
|
|
394
455
|
if (flip[e.op] && (divk(e.b) || divk(e.a))) {
|
|
395
456
|
const [op, lhs, d] = divk(e.b) ? [e.op, e.a, divk(e.b)] : [flip[e.op], e.b, divk(e.a)];
|
|
@@ -422,7 +483,51 @@ function val(e, c, names, dry) {
|
|
|
422
483
|
if (['&&', '||', '=>'].includes(e.op))
|
|
423
484
|
return dry ? { t: 'cond', v: yes } : { t: 'cond', v: e.op === '&&' ? all(cond(a), cond(b)) : e.op === '||' ? any(cond(a), cond(b)) : cond(a).implies(cond(b)) };
|
|
424
485
|
if (dry)
|
|
425
|
-
return ['+', '-', '*', '//', '/', '%'].includes(e.op) ? DRY : { t: 'cond', v: yes };
|
|
486
|
+
return ['+', '-', '*', '//', '/', '%', '&', '|'].includes(e.op) ? DRY : { t: 'cond', v: yes };
|
|
487
|
+
// x & K and x | K with K a constant (a literal or a constant of the code): K's runs of ones, ((x div 2^lo) mod 2^n)·2^lo
|
|
488
|
+
// each, as the extractor reads the code's (core/extract-step.ts's bitop); x | K is x + K − (x & K).
|
|
489
|
+
if (e.op === '&' || e.op === '|') {
|
|
490
|
+
const kOf = (v) => { if (v.t !== 'num')
|
|
491
|
+
return undefined; const p = v.v.pos.e, n = v.v.neg.e; return p.op === 'const' && n.op === 'const' ? p.value - n.value : undefined; };
|
|
492
|
+
const [xv, k] = kOf(b) !== undefined ? [a, kOf(b)] : kOf(a) !== undefined ? [b, kOf(a)] : [a, undefined];
|
|
493
|
+
if (k === undefined)
|
|
494
|
+
throw new LawError(`${e.op} with a constant only (a bit operation of two values is outside linear arithmetic)`);
|
|
495
|
+
const x = num(xv), andK = (kk) => {
|
|
496
|
+
if (kk < 0)
|
|
497
|
+
return x.minus(andK(-kk - 1));
|
|
498
|
+
let out = Int.of(0), lo = 0;
|
|
499
|
+
for (let r = kk; r > 0;) {
|
|
500
|
+
if (r % 2 === 0) {
|
|
501
|
+
r /= 2;
|
|
502
|
+
lo++;
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
let n = 0;
|
|
506
|
+
while (r % 2 === 1) {
|
|
507
|
+
r = (r - 1) / 2;
|
|
508
|
+
n++;
|
|
509
|
+
}
|
|
510
|
+
out = out.plus(Int.of(run(lo, n)).times(2 ** lo));
|
|
511
|
+
lo += n;
|
|
512
|
+
}
|
|
513
|
+
return out;
|
|
514
|
+
};
|
|
515
|
+
// ((x div 2^lo) mod 2^n): an invariant's by the rule's functions; a step law's (which reads none) by keys q, m,
|
|
516
|
+
// s at every value of which x ≡ q·2^(lo+n) + m·2^lo + s (mod 2^(lo+n), x's natural form pos + neg·(2^(lo+n) − 1)),
|
|
517
|
+
// m < 2^n, s < 2^lo: m is the run
|
|
518
|
+
function run(lo, n) {
|
|
519
|
+
if (!c.bits)
|
|
520
|
+
return x.divBy(2 ** lo).modBy(2 ** n);
|
|
521
|
+
const take = () => c.bits.ks[c.bits.used++] ?? new Term({ op: 'const', value: 0 });
|
|
522
|
+
const q = take(), m = take(), s0 = lo ? take() : undefined, nat = x.pos.plus(x.neg.times(2 ** (lo + n) - 1));
|
|
523
|
+
let parts = q.times(2 ** (lo + n)).plus(m.times(2 ** lo));
|
|
524
|
+
if (s0)
|
|
525
|
+
parts = parts.plus(s0);
|
|
526
|
+
c.bits.hyps.push(nat.eq(parts), m.lt(2 ** n), ...(s0 ? [s0.lt(2 ** lo)] : []));
|
|
527
|
+
return m;
|
|
528
|
+
}
|
|
529
|
+
return { t: 'num', v: e.op === '&' ? andK(k) : x.plus(k).minus(andK(k)) };
|
|
530
|
+
}
|
|
426
531
|
if (e.op === '+' || e.op === '-')
|
|
427
532
|
return { t: 'num', v: e.op === '+' ? num(a).plus(num(b)) : num(a).minus(num(b)) };
|
|
428
533
|
if (e.op === '*') {
|
|
@@ -444,6 +549,38 @@ function val(e, c, names, dry) {
|
|
|
444
549
|
}
|
|
445
550
|
return fail('an expression of no kind');
|
|
446
551
|
}
|
|
552
|
+
// `later - earlier ⋈ N business days`: the business days from earlier's day to later's (core/calendar.ts) ⋈ N. In a law
|
|
553
|
+
// of one step each time's week is a key (pinned, its guard collected); elsewhere it is divided out.
|
|
554
|
+
function businessCompare(e, flip, c, go, dry) {
|
|
555
|
+
const [op, diff, n] = e.b.k === 'bdur' ? [e.op, e.a, e.b] : [flip[e.op], e.b, e.a];
|
|
556
|
+
if (diff.k !== 'bin' || diff.op !== '-')
|
|
557
|
+
throw new LawError(`${n.text}: compared with the time elapsed between two times, later - earlier (now - received <= ${n.v} business days)`);
|
|
558
|
+
if (c.clock === undefined)
|
|
559
|
+
throw new LawError(`${n.text}: the clock's unit isn't known here (clock = "s" or "ms" in gdp.toml)`);
|
|
560
|
+
if (!c.cal)
|
|
561
|
+
throw new LawError(`${n.text}: business days need a calendar ([calendar] in gdp.toml: weekend = ["sat", "sun"], holidays = ["2026-12-25", ...], utc_offset = "-05:00")`);
|
|
562
|
+
c.roles.add('bdays');
|
|
563
|
+
const later = go(diff.a), earlier = go(diff.b);
|
|
564
|
+
if (dry)
|
|
565
|
+
return yes;
|
|
566
|
+
const cal = c.cal, at = (x, v) => {
|
|
567
|
+
const t = num(v), p = c.pins;
|
|
568
|
+
if (!p || !c.step)
|
|
569
|
+
return t;
|
|
570
|
+
// (one key per time as it is read: the same text, in the state before or after the step)
|
|
571
|
+
const id = `${JSON.stringify(x)}${c.a === c.b ? '@before' : ''}`;
|
|
572
|
+
if (!p.at) {
|
|
573
|
+
if (!p.times.some(q => q.id === id))
|
|
574
|
+
p.times.push({ id, nat: t.neg.e.op === 'const' && t.neg.e.value === 0 });
|
|
575
|
+
return t;
|
|
576
|
+
}
|
|
577
|
+
const pin = cal.pin(t, p.at.get(id));
|
|
578
|
+
p.guards.push(pin.guard);
|
|
579
|
+
return pin.at;
|
|
580
|
+
};
|
|
581
|
+
const d = cal.between(at(diff.b, earlier), at(diff.a, later));
|
|
582
|
+
return op === '<' ? d.lt(n.v) : op === '<=' ? d.atMost(n.v) : op === '>' ? d.gt(n.v) : op === '>=' ? d.atLeast(n.v) : op === '==' ? d.eq(n.v) : d.ne(n.v);
|
|
583
|
+
}
|
|
447
584
|
// (a path's last name: a choice's value written as one, Outcome.SLOW_ERROR)
|
|
448
585
|
const lastName = (x) => (x.segs.length ? x.segs.at(-1).f : x.head);
|
|
449
586
|
const constOf = (x) => (x.k === 'num' ? x.v : x.k === 'un' && x.op === '-' && x.x.k === 'num' ? -x.x.v : undefined);
|
|
@@ -471,6 +608,10 @@ function compare(op, a, b, e) {
|
|
|
471
608
|
throw new LawError(`${show(ox)} isn't one of ${ch.values.join(', ')}`);
|
|
472
609
|
return eq(ch.is(lit));
|
|
473
610
|
}
|
|
611
|
+
// A quantified map key and a string identity use the same id as m[identity].
|
|
612
|
+
// This is equality of keys, not an ordering or coercion between strings and numbers.
|
|
613
|
+
if (a.t === 'key' && b.t === 'str' || a.t === 'str' && b.t === 'key')
|
|
614
|
+
return eq(key(a).eq(key(b)));
|
|
474
615
|
if (a.t === 'str' || b.t === 'str') {
|
|
475
616
|
const [s, o] = a.t === 'str' ? [a.v, b] : [b.v, a];
|
|
476
617
|
return eq(o.t === 'str' ? s.eq(o.v) : o.t === 'lit' ? s.eq(o.v) : fail('a string compared with what isn\'t one'));
|
|
@@ -510,10 +651,21 @@ function path(e, c, names, dry) {
|
|
|
510
651
|
c.methods.add(n.method);
|
|
511
652
|
return rest(e.segs, leaf(n, c, dry), c, names, dry, e);
|
|
512
653
|
}
|
|
654
|
+
// (a call answered per constant arguments, C's: f(em, BUS_OFF) its answer f_x_18, by the code's constants)
|
|
655
|
+
const call0 = e.segs[0];
|
|
656
|
+
if (call0 && 'call' in call0 && call0.args && names.some(x => x.role === 'answer' && x.code.startsWith(`${e.head}_`))) {
|
|
657
|
+
const sig = splitArgs(call0.args).map(a => { const k = /^-?\d+$/.test(a) ? Number(a) : /^[A-Za-z_]\w*$/.test(a) ? c.consts?.[a] : undefined; return typeof k === 'number' ? String(k).replace('-', 'm') : 'x'; }).join('_');
|
|
658
|
+
const per = names.find(x => x.role === 'answer' && x.code === `${e.head}_${sig}`);
|
|
659
|
+
if (!per)
|
|
660
|
+
throw new LawError(`${e.head}(${call0.args}): no answer of the code for these arguments (its answers: ${names.filter(x => x.role === 'answer' && x.code.startsWith(`${e.head}_`)).map(x => x.code).join(', ')})`);
|
|
661
|
+
c.roles.add(per.role);
|
|
662
|
+
return rest(e.segs.slice(1), leaf(per, c, dry), c, names, dry, e);
|
|
663
|
+
}
|
|
513
664
|
// (self.x, this.x, state.x, @x: the state's x; a bare `state` or `s` is a name of the code's, a field named so)
|
|
514
665
|
const explicit = (['self', 'this', 'state', 's'].includes(e.head) && e.segs.length > 0) || e.head.startsWith('@');
|
|
515
666
|
const parts = [e.head.replace(/^@/, ''), ...e.segs.map(s => ('f' in s ? s.f : undefined))];
|
|
516
|
-
|
|
667
|
+
// (s.x and state.x: the state's x, else a state the code names s or state, a C global's field)
|
|
668
|
+
const heads = explicit && !e.head.startsWith('@') ? [parts.slice(1), ...(['s', 'state'].includes(e.head) ? [parts] : [])] : [parts, ...(parts.length > 1 && parts[1] !== undefined ? [parts.slice(1)] : [])];
|
|
517
669
|
for (const [hi, p] of heads.entries()) {
|
|
518
670
|
const fields = [];
|
|
519
671
|
for (const s of p) {
|
|
@@ -530,8 +682,16 @@ function path(e, c, names, dry) {
|
|
|
530
682
|
if (hit.method)
|
|
531
683
|
c.methods.add(hit.method);
|
|
532
684
|
// (an answered call is one value a step, whatever its arguments: written f(...) or f())
|
|
533
|
-
if (hit.role === 'answer' && next && 'call' in next && next.args)
|
|
685
|
+
if (hit.role === 'answer' && next && 'call' in next && next.args) {
|
|
686
|
+
// (a call answered per constant arguments, C's: f(em, BUS_OFF) its answer f_x_18, by the code's constants)
|
|
687
|
+
const sig = splitArgs(next.args).map(a => { const k = /^-?\d+$/.test(a) ? Number(a) : /^[A-Za-z_]\w*$/.test(a) ? c.consts?.[a] : undefined; return typeof k === 'number' ? String(k).replace('-', 'm') : 'x'; }).join('_');
|
|
688
|
+
const per = names.find(x => x.role === 'answer' && x.code === `${code}_${sig}`);
|
|
689
|
+
if (per) {
|
|
690
|
+
c.roles.add(per.role);
|
|
691
|
+
return rest(e.segs.slice(skip + n), leaf(per, c, dry), c, names, dry, e);
|
|
692
|
+
}
|
|
534
693
|
throw new LawError(`${code}(${next.args}): an answered call's value is the request's, one a step: write it ${code}(...)`);
|
|
694
|
+
}
|
|
535
695
|
const segs = e.segs.slice(skip + n - 1 + (hit.role === 'answer' && next && 'call' in next ? 1 : 0));
|
|
536
696
|
// (a map read where it is named: old(m) is the map before the step wherever its cells are read, `k in old(m)`)
|
|
537
697
|
return rest(segs, hit.map ? { t: 'map', n: hit, maps: names.filter(x => x.map && x.code === hit.code), env: c.a } : leaf(hit, c, dry), c, names, dry, e);
|
|
@@ -539,7 +699,12 @@ function path(e, c, names, dry) {
|
|
|
539
699
|
}
|
|
540
700
|
}
|
|
541
701
|
// A method's argument by its method's name (charge.user), where several methods take one of that name.
|
|
542
|
-
// A constant of the module bound to a literal, by its name.
|
|
702
|
+
// A constant of the module bound to a literal, by its name (a qualified one, C++'s A::K, by its path).
|
|
703
|
+
const dotted = e.segs.every(g => 'f' in g) ? [e.head, ...e.segs.map(g => g.f)].join('.') : undefined;
|
|
704
|
+
if (dotted && e.segs.length && c.consts?.[dotted] !== undefined) {
|
|
705
|
+
const v = c.consts[dotted];
|
|
706
|
+
return typeof v === 'number' ? { t: 'num', v: Int.of(v) } : typeof v === 'boolean' ? { t: 'cond', v: v ? yes : no } : { t: 'lit', v };
|
|
707
|
+
}
|
|
543
708
|
const k = c.consts?.[e.head];
|
|
544
709
|
if (k !== undefined && !e.segs.length)
|
|
545
710
|
return typeof k === 'number' ? { t: 'num', v: Int.of(k) } : typeof k === 'boolean' ? { t: 'cond', v: k ? yes : no } : { t: 'lit', v: k };
|
|
@@ -835,3 +1000,17 @@ function split(s, sep) {
|
|
|
835
1000
|
}
|
|
836
1001
|
return [...out, rest].map(t => t.trim()).filter(Boolean);
|
|
837
1002
|
}
|
|
1003
|
+
// (a call's arguments as written, split at the commas outside parentheses)
|
|
1004
|
+
function splitArgs(s) { const out = []; let d = 0, cur = ''; for (const ch of s) {
|
|
1005
|
+
if (ch === '(' || ch === '[')
|
|
1006
|
+
d++;
|
|
1007
|
+
else if (ch === ')' || ch === ']')
|
|
1008
|
+
d--;
|
|
1009
|
+
if (ch === ',' && !d) {
|
|
1010
|
+
out.push(cur.trim());
|
|
1011
|
+
cur = '';
|
|
1012
|
+
}
|
|
1013
|
+
else
|
|
1014
|
+
cur += ch;
|
|
1015
|
+
} if (cur.trim())
|
|
1016
|
+
out.push(cur.trim()); return out; }
|
package/dist/core/machine.d.ts
CHANGED
|
@@ -2,8 +2,6 @@ import { type Constraint, type Verdict } from './solver.js';
|
|
|
2
2
|
export declare const lcg: (x: number) => number;
|
|
3
3
|
export type Rand = (k: number) => number;
|
|
4
4
|
export declare const seeded: (seed: number) => Rand;
|
|
5
|
-
export declare const lcgFloat: (x: number) => number;
|
|
6
|
-
export declare const searchSeeded: (seed: number) => Rand;
|
|
7
5
|
export declare const walk: (t: unknown, f: (o: Record<string, unknown>) => void) => void;
|
|
8
6
|
export declare const walkOnce: (t: unknown, f: (o: object) => void) => void;
|
|
9
7
|
export type E = {
|
|
@@ -33,6 +31,14 @@ export type E = {
|
|
|
33
31
|
op: 'get';
|
|
34
32
|
map: string;
|
|
35
33
|
key: E;
|
|
34
|
+
} | {
|
|
35
|
+
op: 'tup';
|
|
36
|
+
a: E;
|
|
37
|
+
b: E;
|
|
38
|
+
} | {
|
|
39
|
+
op: 'proj';
|
|
40
|
+
k: 0 | 1;
|
|
41
|
+
a: E;
|
|
36
42
|
};
|
|
37
43
|
export type C = {
|
|
38
44
|
op: '<' | '<=' | '=';
|
|
@@ -77,6 +83,7 @@ export type Machine = {
|
|
|
77
83
|
fns?: Record<string, Fn>;
|
|
78
84
|
facts?: Law[];
|
|
79
85
|
leasts?: Record<string, Least>;
|
|
86
|
+
relations?: Record<string, string[]>;
|
|
80
87
|
};
|
|
81
88
|
export type Least = {
|
|
82
89
|
some: string;
|
|
@@ -145,6 +152,8 @@ export type Counted = {
|
|
|
145
152
|
key: string;
|
|
146
153
|
below: E;
|
|
147
154
|
body: E;
|
|
155
|
+
lo?: string;
|
|
156
|
+
base?: number;
|
|
148
157
|
};
|
|
149
158
|
export type MapUpdate = {
|
|
150
159
|
key: E;
|
|
@@ -185,7 +194,7 @@ export type SomeProof = {
|
|
|
185
194
|
at: E;
|
|
186
195
|
};
|
|
187
196
|
};
|
|
188
|
-
export { unreachable } from '../kernel/kernel.js';
|
|
197
|
+
export { unreachable, tuple, tupleParts, part } from '../kernel/kernel.js';
|
|
189
198
|
export declare const x: (name: string) => E;
|
|
190
199
|
export declare const n: (value: number) => E;
|
|
191
200
|
export declare const plus: (a: E, b: E) => E;
|
|
@@ -196,6 +205,8 @@ export declare const max: (a: E, b: E) => E;
|
|
|
196
205
|
export declare const prod: (a: E, b: E) => E;
|
|
197
206
|
export declare const ite: (c: C, then: E, otherwise: E) => E;
|
|
198
207
|
export declare const get: (map: string, key: E) => E;
|
|
208
|
+
export declare const cpair: (a: number, b: number) => number;
|
|
209
|
+
export declare const cunpair: (z: number) => [number, number];
|
|
199
210
|
export declare const less: (a: E, b: E) => C;
|
|
200
211
|
export declare const atMost: (a: E, b: E) => C;
|
|
201
212
|
export declare const same: (a: E, b: E) => C;
|
|
@@ -223,6 +234,7 @@ export type State = {
|
|
|
223
234
|
};
|
|
224
235
|
type Env = Record<string, number>;
|
|
225
236
|
export type Request = Record<string, number | ReadonlyMap<number, number>>;
|
|
237
|
+
export declare const partsOf: (z: number, n: number) => number[];
|
|
226
238
|
export declare const naturalIn: (v: unknown, what: string) => number;
|
|
227
239
|
export declare function evalE(e: E, env: Env, st: State, m: Machine): number;
|
|
228
240
|
export declare const fnValue: (fn: Fn, key: number) => number;
|
|
@@ -252,6 +264,7 @@ export type Compiled = {
|
|
|
252
264
|
};
|
|
253
265
|
};
|
|
254
266
|
export declare const loadJs: (m: Machine) => Compiled;
|
|
267
|
+
export declare function fastStep(m: Machine): ((params: Env, st: State, input: Request) => State) | undefined;
|
|
255
268
|
export type Obligation = {
|
|
256
269
|
phase: 'start' | 'step';
|
|
257
270
|
law: string;
|