gdp-wired 0.50.6 → 0.52.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 +2 -2
- package/dist/core/accel-refute.d.ts +53 -0
- package/dist/core/accel-refute.js +997 -0
- package/dist/core/cli.d.ts +2 -0
- package/dist/core/cli.js +8 -1
- package/dist/core/cycles.d.ts +2 -0
- package/dist/core/cycles.js +12 -9
- package/dist/core/dsl.d.ts +11 -2
- package/dist/core/dsl.js +194 -29
- package/dist/core/fixed-division.d.ts +9 -0
- package/dist/core/fixed-division.js +87 -0
- package/dist/core/fixed-integers.d.ts +33 -0
- package/dist/core/fixed-integers.js +279 -0
- package/dist/core/live-support.d.ts +5 -0
- package/dist/core/live-support.js +148 -0
- package/dist/core/live.js +194 -37
- package/dist/core/lockstep.d.ts +5 -0
- package/dist/core/lockstep.js +23 -0
- package/dist/core/machine.d.ts +1 -0
- package/dist/core/machine.js +33 -3
- package/dist/core/ordered-pair-seeds.d.ts +2 -0
- package/dist/core/ordered-pair-seeds.js +109 -0
- package/dist/core/proof-output.d.ts +38 -1
- package/dist/core/proof-output.js +38 -0
- package/dist/core/search-termination.d.ts +4 -0
- package/dist/core/search-termination.js +1 -1
- package/dist/core/source-check.d.ts +4 -1
- package/dist/core/source-check.js +5 -2
- package/dist/core/sparse-kind-proof.d.ts +7 -0
- package/dist/core/sparse-kind-proof.js +49 -0
- package/dist/core/step-seeds.d.ts +46 -0
- package/dist/core/step-seeds.js +316 -0
- package/dist/kernel/integer-primitives.d.ts +38 -0
- package/dist/kernel/integer-primitives.js +179 -0
- package/dist/kernel/kernel.d.ts +1 -0
- package/dist/kernel/kernel.js +44 -5
- package/dist/kernel/prove.js +13 -0
- package/dist/kernel/word-division-cuts.d.ts +6 -0
- package/dist/kernel/word-division-cuts.js +69 -0
- package/dist/kernel/word-division.d.ts +12 -0
- package/dist/kernel/word-division.js +167 -0
- package/dist/lean-kernel/Cert.lean +222 -0
- package/dist/lean-kernel/Count.lean +1568 -0
- package/dist/lean-kernel/Ghost.lean +373 -0
- package/dist/lean-kernel/GhostMap.lean +426 -0
- package/dist/lean-kernel/Kernel.lean +2881 -0
- package/dist/lean-kernel/Live.lean +1168 -0
- package/dist/lean-kernel/Progress.lean +543 -0
- package/dist/lean-kernel/Sets.lean +1975 -0
- package/dist/lean-kernel/StepLaw.lean +128 -0
- package/dist/lean-kernel/Steps.lean +477 -0
- package/dist/lean-kernel/Sums.lean +916 -0
- package/dist/lean-kernel/emit.d.ts +8 -1
- package/dist/lean-kernel/emit.js +59 -15
- package/dist/lean-kernel/integer-primitives.d.ts +17 -0
- package/dist/lean-kernel/integer-primitives.js +81 -0
- package/dist/lean-kernel/lean-aggregate/FiniteFold.lean +44 -0
- package/dist/lean-kernel/lean-aggregate/GroupedTransport.lean +308 -0
- package/dist/lean-kernel/lean-aggregate/aggregate.d.ts +13 -0
- package/dist/lean-kernel/lean-aggregate/aggregate.js +68 -0
- package/dist/lean-kernel/lean-aggregate/assets.d.ts +5 -0
- package/dist/lean-kernel/lean-aggregate/assets.js +7 -0
- package/dist/lean-kernel/lean-aggregate/audit.d.ts +3 -0
- package/dist/lean-kernel/lean-aggregate/audit.js +13 -0
- package/dist/lean-kernel/lean-aggregate/exact.d.ts +15 -0
- package/dist/lean-kernel/lean-aggregate/exact.js +51 -0
- package/dist/lean-kernel/lean-aggregate/index.d.ts +19 -0
- package/dist/lean-kernel/lean-aggregate/index.js +52 -0
- package/dist/lean-kernel/lean-aggregate/point-fold.d.ts +27 -0
- package/dist/lean-kernel/lean-aggregate/point-fold.js +79 -0
- package/dist/lean-kernel/lean-aggregate/replay-fold.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/replay-fold.js +163 -0
- package/dist/lean-kernel/lean-aggregate/source.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/source.js +141 -0
- package/dist/lean-kernel/lean-aggregate/weighted-flags.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/weighted-flags.js +26 -0
- package/dist/lean-kernel/lean-aggregate/weighted-replay.d.ts +7 -0
- package/dist/lean-kernel/lean-aggregate/weighted-replay.js +53 -0
- package/dist/lean-kernel/lean-aggregate/weighted-seeded.d.ts +30 -0
- package/dist/lean-kernel/lean-aggregate/weighted-seeded.js +64 -0
- package/dist/lean-kernel/lean-aggregate/weighted.d.ts +18 -0
- package/dist/lean-kernel/lean-aggregate/weighted.js +115 -0
- package/dist/lean-kernel/lean-aggregate/worker.d.ts +10 -0
- package/dist/lean-kernel/lean-aggregate/worker.js +46 -0
- package/dist/lean-kernel/library.js +13 -0
- package/dist/lean-kernel/word-division-export.d.ts +26 -0
- package/dist/lean-kernel/word-division-export.js +172 -0
- package/dist/lean-kernel/word-division-lower.d.ts +10 -0
- package/dist/lean-kernel/word-division-lower.js +103 -0
- package/docs/lean-aggregate.md +29 -0
- package/package.json +6 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { FixedWord, fixedSelect } from "./fixed-integers.js";
|
|
2
|
+
import { Term, all, any, lit } from "./dsl.js";
|
|
3
|
+
import { wordDivisionKey, wordDivisionName } from "../kernel/word-division.js";
|
|
4
|
+
export { wordDivisionKey, wordDivisionName } from "../kernel/word-division.js";
|
|
5
|
+
// Preserve the capture API for DSL assembly; intrinsic getters need no flags.
|
|
6
|
+
let active;
|
|
7
|
+
export function captureFixed(f) {
|
|
8
|
+
const previous = active, flags = Object.create(null);
|
|
9
|
+
active = flags;
|
|
10
|
+
try {
|
|
11
|
+
return { value: f(), flags };
|
|
12
|
+
}
|
|
13
|
+
finally {
|
|
14
|
+
active = previous;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const zero = () => lit(0);
|
|
18
|
+
const no = () => zero().eq(1);
|
|
19
|
+
const radixOf = (a) => a.width === 8 ? 256 : 65536;
|
|
20
|
+
const pure = (w, signed = w.signed) => new FixedWord(w.width, signed, w.limbs);
|
|
21
|
+
const same = (a, b) => all(...a.map((x, i) => x.eq(b[i])));
|
|
22
|
+
const nonzero = (a) => any(...a.map(x => x.ne(0)));
|
|
23
|
+
const neg = (a) => pure(a).negate('WRAP');
|
|
24
|
+
const sign = (a) => a.limbs[a.limbs.length - 1].atLeast(radixOf(a) / 2);
|
|
25
|
+
// Intrinsic quotients and residues are keyed by both unsigned magnitudes.
|
|
26
|
+
// Known inputs use BigInt for metadata; symbolic limbs leave val undefined.
|
|
27
|
+
const magnitude = (limbs, radix) => {
|
|
28
|
+
let value = 0n;
|
|
29
|
+
for (let i = limbs.length - 1; i >= 0; i--) {
|
|
30
|
+
const digit = limbs[i].val;
|
|
31
|
+
if (digit === undefined)
|
|
32
|
+
return undefined;
|
|
33
|
+
value = value * radix + BigInt(digit);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
};
|
|
37
|
+
const select = (c, a, b) => fixedSelect(c, a, b);
|
|
38
|
+
export function divideFixed(a, b, rounding, mode, remainder) {
|
|
39
|
+
if (!(a instanceof FixedWord) || !(b instanceof FixedWord) || a.width !== b.width || a.signed !== b.signed)
|
|
40
|
+
throw new TypeError('fixed integers must have identical width and signedness');
|
|
41
|
+
if (!['WRAP', 'SATURATE', 'TRAP'].includes(mode))
|
|
42
|
+
throw new RangeError('an explicit overflow mode is required');
|
|
43
|
+
if (!['FLOOR', 'TRUNCATING', 'EUCLIDEAN'].includes(rounding))
|
|
44
|
+
throw new RangeError('unknown division rounding');
|
|
45
|
+
if (!active)
|
|
46
|
+
throw new Error('symbolic fixed integer division requires captureFixed');
|
|
47
|
+
const radix = radixOf(a), n = a.limbs.length;
|
|
48
|
+
const aa = pure(a), bb = pure(b);
|
|
49
|
+
const aNeg = a.signed ? sign(aa) : no(), bNeg = b.signed ? sign(bb) : no();
|
|
50
|
+
const am = pure(a.signed ? select(aNeg, neg(aa), aa) : aa, false);
|
|
51
|
+
const bm = pure(b.signed ? select(bNeg, neg(bb), bb) : bb, false);
|
|
52
|
+
const key = wordDivisionKey([...am.limbs, ...bm.limbs].map(x => x.e));
|
|
53
|
+
const av = magnitude(am.limbs, BigInt(radix)), bv = magnitude(bm.limbs, BigInt(radix));
|
|
54
|
+
if (bv === 0n)
|
|
55
|
+
return new FixedWord(a.width, a.signed, Array.from({ length: n }, zero), zero().eq(0));
|
|
56
|
+
const qv = av === undefined || bv === undefined ? undefined : av / bv;
|
|
57
|
+
const rv = av === undefined || bv === undefined ? undefined : av % bv;
|
|
58
|
+
const digits = (kind, value) => Array.from({ length: n }, (_, i) => new Term({ op: 'get', map: wordDivisionName(a.width, kind, i), key }, value === undefined ? undefined : Number(value / (BigInt(radix) ** BigInt(i)) % BigInt(radix))));
|
|
59
|
+
const uq = new FixedWord(a.width, false, digits('q', qv));
|
|
60
|
+
const ur = new FixedWord(a.width, false, digits('r', rv));
|
|
61
|
+
const qNegative = a.signed ? aNeg.iff(bNeg).not() : no();
|
|
62
|
+
let q = a.signed ? select(qNegative, neg(pure(uq, true)), pure(uq, true)) : uq;
|
|
63
|
+
let r = a.signed ? select(aNeg, neg(pure(ur, true)), pure(ur, true)) : ur;
|
|
64
|
+
const rNotZero = nonzero(ur.limbs);
|
|
65
|
+
if (a.signed && rounding === 'FLOOR') {
|
|
66
|
+
const adjust = all(rNotZero, qNegative);
|
|
67
|
+
q = select(adjust, q.minus(new FixedWord(a.width, true, [lit(1), ...Array.from({ length: n - 1 }, zero)]), 'WRAP'), q);
|
|
68
|
+
r = select(adjust, r.plus(bb, 'WRAP'), r);
|
|
69
|
+
}
|
|
70
|
+
else if (a.signed && rounding === 'EUCLIDEAN') {
|
|
71
|
+
const adjust = all(rNotZero, aNeg);
|
|
72
|
+
const one = new FixedWord(a.width, true, [lit(1), ...Array.from({ length: n - 1 }, zero)]);
|
|
73
|
+
q = select(adjust, select(bNeg, q.plus(one, 'WRAP'), q.minus(one, 'WRAP')), q);
|
|
74
|
+
r = select(adjust, r.plus(pure(bm, true), 'WRAP'), r);
|
|
75
|
+
}
|
|
76
|
+
const minimum = Array.from({ length: n }, (_, i) => lit(i === n - 1 ? radix / 2 : 0));
|
|
77
|
+
const maximum = Array.from({ length: n }, (_, i) => lit(i === n - 1 ? radix / 2 - 1 : radix - 1));
|
|
78
|
+
const overflow = a.signed ? all(aNeg, same(a.limbs, minimum), same(b.limbs, Array.from({ length: n }, () => lit(radix - 1)))) : no();
|
|
79
|
+
if (a.signed) {
|
|
80
|
+
const min = new FixedWord(a.width, true, minimum);
|
|
81
|
+
const max = new FixedWord(a.width, true, maximum);
|
|
82
|
+
q = select(overflow, mode === 'SATURATE' ? max : min, q);
|
|
83
|
+
r = select(overflow, new FixedWord(a.width, true, Array.from({ length: n }, zero)), r);
|
|
84
|
+
}
|
|
85
|
+
const failure = any(...[a.failure, b.failure, nonzero(b.limbs).not(), mode === 'TRAP' ? overflow : undefined].filter((x) => !!x));
|
|
86
|
+
return new FixedWord(a.width, a.signed, (remainder ? r : q).limbs, failure);
|
|
87
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Kind, Term, Cond, type Raw } from './dsl.js';
|
|
2
|
+
export type FixedWidth = 8 | 16 | 32 | 64;
|
|
3
|
+
export type Overflow = 'WRAP' | 'SATURATE' | 'TRAP';
|
|
4
|
+
export type DivisionRounding = 'FLOOR' | 'TRUNCATING' | 'EUCLIDEAN';
|
|
5
|
+
export declare class FixedWord {
|
|
6
|
+
readonly width: FixedWidth;
|
|
7
|
+
readonly signed: boolean;
|
|
8
|
+
readonly limbs: readonly Term[];
|
|
9
|
+
readonly failure?: Cond | undefined;
|
|
10
|
+
constructor(width: FixedWidth, signed: boolean, limbs: readonly Term[], failure?: Cond | undefined);
|
|
11
|
+
private check;
|
|
12
|
+
private finish;
|
|
13
|
+
private checkComparison;
|
|
14
|
+
eq(b: FixedWord): Cond;
|
|
15
|
+
ne(b: FixedWord): Cond;
|
|
16
|
+
lt(b: FixedWord): Cond;
|
|
17
|
+
atMost(b: FixedWord): Cond;
|
|
18
|
+
gt(b: FixedWord): Cond;
|
|
19
|
+
atLeast(b: FixedWord): Cond;
|
|
20
|
+
plus(b: FixedWord, mode: Overflow): FixedWord;
|
|
21
|
+
minus(b: FixedWord, mode: Overflow): FixedWord;
|
|
22
|
+
negate(mode: Overflow): FixedWord;
|
|
23
|
+
mul(b: FixedWord, mode: Overflow): FixedWord;
|
|
24
|
+
cast(width: FixedWidth, signed: boolean, mode: Overflow): FixedWord;
|
|
25
|
+
div(b: FixedWord, rounding: DivisionRounding, mode: Overflow): FixedWord;
|
|
26
|
+
rem(b: FixedWord, rounding: DivisionRounding, mode: Overflow): FixedWord;
|
|
27
|
+
private divide;
|
|
28
|
+
}
|
|
29
|
+
export declare const fixedFailure: (value: unknown) => Cond | undefined;
|
|
30
|
+
export declare const fixedSelect: (condition: Cond, a: FixedWord, b: FixedWord) => FixedWord;
|
|
31
|
+
export declare const fixedInt: (width: FixedWidth, signed: boolean) => Kind<FixedWord> & {
|
|
32
|
+
lift(value: Raw | bigint): FixedWord;
|
|
33
|
+
};
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { Kind, Term, lit, when, all, any } from "./dsl.js";
|
|
2
|
+
import { divideFixed } from "./fixed-division.js";
|
|
3
|
+
const validKind = (width, signed) => {
|
|
4
|
+
if (![8, 16, 32, 64].includes(width))
|
|
5
|
+
throw new RangeError('fixed width must be 8, 16, 32, or 64');
|
|
6
|
+
if (typeof signed !== 'boolean')
|
|
7
|
+
throw new TypeError('fixed signedness must be boolean');
|
|
8
|
+
};
|
|
9
|
+
const base = (w) => w === 8 ? 256 : 65536;
|
|
10
|
+
const digits = (w) => w === 8 ? 1 : w / 16;
|
|
11
|
+
const falseCond = () => lit(0).eq(1);
|
|
12
|
+
// Store lowers a word to plain Terms. Keep its failure on fresh wrappers rather
|
|
13
|
+
// than mutating the shared limbs, which may also belong to an untrapped word.
|
|
14
|
+
const storedTermFailures = new WeakMap();
|
|
15
|
+
const validMode = (mode) => {
|
|
16
|
+
if (mode !== 'WRAP' && mode !== 'SATURATE' && mode !== 'TRAP')
|
|
17
|
+
throw new RangeError('an explicit overflow mode is required');
|
|
18
|
+
};
|
|
19
|
+
const failures = (...cs) => {
|
|
20
|
+
const present = cs.filter((c) => c !== undefined);
|
|
21
|
+
return present.length ? any(...present) : undefined;
|
|
22
|
+
};
|
|
23
|
+
const select = (c, a, b) => a.map((x, i) => when(c, x, b[i]));
|
|
24
|
+
const constant = (w, signed, value) => {
|
|
25
|
+
const mod = 1n << BigInt(w);
|
|
26
|
+
let x = ((value % mod) + mod) % mod;
|
|
27
|
+
const radix = BigInt(base(w));
|
|
28
|
+
return Array.from({ length: digits(w) }, () => { const d = Number(x % radix); x /= radix; return lit(d); });
|
|
29
|
+
};
|
|
30
|
+
const equal = (a, b) => all(...a.map((x, i) => x.eq(b[i])));
|
|
31
|
+
const less = (a, b) => {
|
|
32
|
+
let result = falseCond();
|
|
33
|
+
for (let i = 0; i < a.length; i++)
|
|
34
|
+
result = any(a[i].lt(b[i]), all(a[i].eq(b[i]), result));
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
const addRaw = (a, b, radix) => {
|
|
38
|
+
let carry = lit(0);
|
|
39
|
+
const low = [];
|
|
40
|
+
for (let i = 0; i < a.length; i++) {
|
|
41
|
+
const sum = a[i].plus(b[i]).plus(carry); // at most 2*65535+1
|
|
42
|
+
low.push(sum.modBy(radix));
|
|
43
|
+
carry = sum.divBy(radix);
|
|
44
|
+
}
|
|
45
|
+
return { low, carry };
|
|
46
|
+
};
|
|
47
|
+
const invert = (xs, radix) => xs.map(x => lit(radix - 1).minus(x));
|
|
48
|
+
const negateRaw = (xs, radix) => addRaw(invert(xs, radix), [lit(1), ...Array.from({ length: xs.length - 1 }, () => lit(0))], radix).low;
|
|
49
|
+
const signOf = (xs, radix) => xs[xs.length - 1].atLeast(radix / 2);
|
|
50
|
+
const magnitude = (xs, radix, negative) => select(negative, negateRaw(xs, radix), xs);
|
|
51
|
+
const maxWord = (w, signed) => constant(w, signed, (1n << BigInt(w - (signed ? 1 : 0))) - 1n);
|
|
52
|
+
const minWord = (w, signed) => constant(w, signed, signed ? -(1n << BigInt(w - 1)) : 0n);
|
|
53
|
+
const rawBigInt = (w, signed, xs) => {
|
|
54
|
+
if (xs.some(x => x.val === undefined))
|
|
55
|
+
return undefined;
|
|
56
|
+
let n = 0n;
|
|
57
|
+
for (let i = xs.length - 1; i >= 0; i--)
|
|
58
|
+
n = n * BigInt(base(w)) + BigInt(xs[i].val);
|
|
59
|
+
return signed && n >= (1n << BigInt(w - 1)) ? n - (1n << BigInt(w)) : n;
|
|
60
|
+
};
|
|
61
|
+
const parse = (w, signed, raw) => {
|
|
62
|
+
if (typeof raw === 'number') {
|
|
63
|
+
if (w === 64 || !Number.isSafeInteger(raw))
|
|
64
|
+
throw new RangeError('unsafe or 64-bit number: use a decimal string or bigint');
|
|
65
|
+
}
|
|
66
|
+
else if (typeof raw !== 'string' && typeof raw !== 'bigint')
|
|
67
|
+
throw new TypeError('fixed integer requires an integer or decimal string');
|
|
68
|
+
if (typeof raw === 'string' && !/^-?(0|[1-9][0-9]*)$/.test(raw))
|
|
69
|
+
throw new RangeError('fixed integer requires a decimal integer string');
|
|
70
|
+
const n = BigInt(raw);
|
|
71
|
+
const lo = signed ? -(1n << BigInt(w - 1)) : 0n;
|
|
72
|
+
const hi = (1n << BigInt(w - (signed ? 1 : 0))) - 1n;
|
|
73
|
+
if (n < lo || n > hi)
|
|
74
|
+
throw new RangeError(`${n} outside ${signed ? 'signed' : 'unsigned'} ${w}-bit range`);
|
|
75
|
+
return n;
|
|
76
|
+
};
|
|
77
|
+
export class FixedWord {
|
|
78
|
+
width;
|
|
79
|
+
signed;
|
|
80
|
+
limbs;
|
|
81
|
+
failure;
|
|
82
|
+
constructor(width, signed, limbs, failure) {
|
|
83
|
+
this.width = width;
|
|
84
|
+
this.signed = signed;
|
|
85
|
+
this.limbs = limbs;
|
|
86
|
+
this.failure = failure;
|
|
87
|
+
validKind(width, signed);
|
|
88
|
+
if (limbs.length !== digits(width))
|
|
89
|
+
throw new RangeError('incorrect fixed integer limb count');
|
|
90
|
+
}
|
|
91
|
+
check(other) {
|
|
92
|
+
if (!(other instanceof FixedWord))
|
|
93
|
+
throw new TypeError('fixed integer operand required');
|
|
94
|
+
if (this.width !== other.width || this.signed !== other.signed)
|
|
95
|
+
throw new TypeError('fixed integers must have identical width and signedness; cast explicitly');
|
|
96
|
+
}
|
|
97
|
+
finish(low, overflow, mode, negative, inherited) {
|
|
98
|
+
validMode(mode);
|
|
99
|
+
const result = mode === 'SATURATE' ? select(overflow, select(negative, minWord(this.width, this.signed), maxWord(this.width, this.signed)), low) : low;
|
|
100
|
+
return new FixedWord(this.width, this.signed, result, failures(inherited, mode === 'TRAP' ? overflow : undefined));
|
|
101
|
+
}
|
|
102
|
+
checkComparison(b) {
|
|
103
|
+
this.check(b);
|
|
104
|
+
// A Cond has no failure channel. A comparison of a possibly trapped result
|
|
105
|
+
// must not erase its failure and accidentally establish a law about that result.
|
|
106
|
+
if ((this.failure && this.failure.ok !== false) || (b.failure && b.failure.ok !== false))
|
|
107
|
+
throw new Error('fixed integer comparison may discard a failure; handle the failure first');
|
|
108
|
+
}
|
|
109
|
+
eq(b) { this.checkComparison(b); return equal(this.limbs, b.limbs); }
|
|
110
|
+
ne(b) { return this.eq(b).not(); }
|
|
111
|
+
lt(b) {
|
|
112
|
+
this.checkComparison(b);
|
|
113
|
+
const aNeg = this.signed ? signOf(this.limbs, base(this.width)) : falseCond();
|
|
114
|
+
const bNeg = this.signed ? signOf(b.limbs, base(this.width)) : falseCond();
|
|
115
|
+
return this.signed ? any(all(aNeg, bNeg.not()), all(aNeg.iff(bNeg), less(this.limbs, b.limbs))) : less(this.limbs, b.limbs);
|
|
116
|
+
}
|
|
117
|
+
atMost(b) { return any(this.lt(b), this.eq(b)); }
|
|
118
|
+
gt(b) { return b.lt(this); }
|
|
119
|
+
atLeast(b) { return b.atMost(this); }
|
|
120
|
+
plus(b, mode) {
|
|
121
|
+
validMode(mode);
|
|
122
|
+
this.check(b);
|
|
123
|
+
const { low, carry } = addRaw(this.limbs, b.limbs, base(this.width));
|
|
124
|
+
const na = signOf(this.limbs, base(this.width)), nb = signOf(b.limbs, base(this.width)), nr = signOf(low, base(this.width));
|
|
125
|
+
const over = this.signed ? all(na.iff(nb), na.iff(nr).not()) : carry.eq(1);
|
|
126
|
+
return this.finish(low, over, mode, this.signed ? na : falseCond(), failures(this.failure, b.failure));
|
|
127
|
+
}
|
|
128
|
+
minus(b, mode) {
|
|
129
|
+
validMode(mode);
|
|
130
|
+
this.check(b);
|
|
131
|
+
// Add the complement and one; carry=0 iff unsigned subtraction borrowed.
|
|
132
|
+
const radix = base(this.width);
|
|
133
|
+
const complement = invert(b.limbs, radix);
|
|
134
|
+
const { low, carry } = addRaw(this.limbs, complement, radix);
|
|
135
|
+
const incremented = addRaw(low, [lit(1), ...Array.from({ length: low.length - 1 }, () => lit(0))], radix);
|
|
136
|
+
const noBorrow = any(carry.eq(1), incremented.carry.eq(1));
|
|
137
|
+
const na = signOf(this.limbs, radix), nb = signOf(b.limbs, radix), nr = signOf(incremented.low, radix);
|
|
138
|
+
const over = this.signed ? all(na.iff(nb).not(), nr.iff(na).not()) : noBorrow.not();
|
|
139
|
+
return this.finish(incremented.low, over, mode, this.signed ? na : lit(0).eq(0), failures(this.failure, b.failure));
|
|
140
|
+
}
|
|
141
|
+
negate(mode) { return new FixedWord(this.width, this.signed, constant(this.width, this.signed, 0n)).minus(this, mode); }
|
|
142
|
+
mul(b, mode) {
|
|
143
|
+
validMode(mode);
|
|
144
|
+
this.check(b);
|
|
145
|
+
const radix = base(this.width), n = this.limbs.length;
|
|
146
|
+
const an = this.signed ? signOf(this.limbs, radix) : falseCond();
|
|
147
|
+
const bn = this.signed ? signOf(b.limbs, radix) : falseCond();
|
|
148
|
+
const a = this.signed ? magnitude(this.limbs, radix, an) : this.limbs;
|
|
149
|
+
const bb = this.signed ? magnitude(b.limbs, radix, bn) : b.limbs;
|
|
150
|
+
const out = [];
|
|
151
|
+
let carry = lit(0);
|
|
152
|
+
for (let k = 0; k < 2 * n; k++) {
|
|
153
|
+
let col = carry;
|
|
154
|
+
for (let i = Math.max(0, k - n + 1); i <= Math.min(k, n - 1); i++)
|
|
155
|
+
col = col.plus(a[i].mul(bb[k - i]));
|
|
156
|
+
// At most 4*65535²+4*65535 < 2^35; no intermediate exceeds 2^53.
|
|
157
|
+
out.push(col.modBy(radix));
|
|
158
|
+
carry = col.divBy(radix);
|
|
159
|
+
}
|
|
160
|
+
const productNegative = an.iff(bn).not();
|
|
161
|
+
const upper = out.slice(n);
|
|
162
|
+
const limit = constant(this.width, this.signed, 1n << BigInt(this.width - 1));
|
|
163
|
+
const positiveLimit = maxWord(this.width, this.signed);
|
|
164
|
+
const magnitudeLow = out.slice(0, n);
|
|
165
|
+
const high = any(...upper.map(x => x.ne(0)));
|
|
166
|
+
const overflow = this.signed
|
|
167
|
+
? any(high, all(productNegative, less(limit, magnitudeLow)), all(productNegative.not(), less(positiveLimit, magnitudeLow)))
|
|
168
|
+
: high;
|
|
169
|
+
const low = this.signed ? select(productNegative, negateRaw(magnitudeLow, radix), magnitudeLow) : magnitudeLow;
|
|
170
|
+
return this.finish(low, overflow, mode, productNegative, failures(this.failure, b.failure));
|
|
171
|
+
}
|
|
172
|
+
cast(width, signed, mode) {
|
|
173
|
+
validMode(mode);
|
|
174
|
+
validKind(width, signed);
|
|
175
|
+
const radix = base(this.width), negative = this.signed ? signOf(this.limbs, radix) : falseCond();
|
|
176
|
+
const mag = this.signed ? magnitude(this.limbs, radix, negative) : this.limbs;
|
|
177
|
+
// Conversion of limbs uses only <=65535*256 and division by the constant radix.
|
|
178
|
+
let source = mag;
|
|
179
|
+
if (radix !== base(width))
|
|
180
|
+
source = width === 8 ? [mag[0].modBy(256)] : [mag[0]];
|
|
181
|
+
const low = Array.from({ length: digits(width) }, (_, i) => source[i] ?? lit(0));
|
|
182
|
+
const sourceHigh = mag.slice(digits(width));
|
|
183
|
+
const high = any(...sourceHigh.map(x => x.ne(0)), ...(radix !== base(width) && width === 8 ? [mag[0].divBy(256).ne(0)] : []));
|
|
184
|
+
const limit = signed ? constant(width, true, 1n << BigInt(width - 1)) : maxWord(width, false);
|
|
185
|
+
const overflow = any(high, all(negative, signed ? less(limit, low) : lit(0).eq(0)), all(negative.not(), less(signed ? maxWord(width, true) : maxWord(width, false), low)));
|
|
186
|
+
const twos = negative.ok === false ? low : select(negative, negateRaw(low, base(width)), low);
|
|
187
|
+
const result = mode === 'SATURATE' ? select(overflow, select(negative, minWord(width, signed), maxWord(width, signed)), twos) : twos;
|
|
188
|
+
return new FixedWord(width, signed, result, failures(this.failure, mode === 'TRAP' ? overflow : undefined));
|
|
189
|
+
}
|
|
190
|
+
div(b, rounding, mode) { return this.divide(b, rounding, mode, false); }
|
|
191
|
+
rem(b, rounding, mode) { return this.divide(b, rounding, mode, true); }
|
|
192
|
+
divide(b, rounding, mode, remainder) {
|
|
193
|
+
this.check(b);
|
|
194
|
+
validMode(mode);
|
|
195
|
+
if (!['FLOOR', 'TRUNCATING', 'EUCLIDEAN'].includes(rounding))
|
|
196
|
+
throw new RangeError('unknown division rounding');
|
|
197
|
+
const aVal = rawBigInt(this.width, this.signed, this.limbs);
|
|
198
|
+
const bVal = rawBigInt(b.width, b.signed, b.limbs);
|
|
199
|
+
if (aVal === undefined || bVal === undefined)
|
|
200
|
+
return divideFixed(this, b, rounding, mode, remainder);
|
|
201
|
+
const min = this.signed ? -(1n << BigInt(this.width - 1)) : 0n;
|
|
202
|
+
const zero = bVal === 0n;
|
|
203
|
+
const overflow = this.signed && aVal === min && bVal === -1n;
|
|
204
|
+
let q = zero ? 0n : aVal / bVal;
|
|
205
|
+
let r = zero ? 0n : aVal % bVal;
|
|
206
|
+
if (!zero && r !== 0n) {
|
|
207
|
+
if (rounding === 'FLOOR' && ((r < 0n) !== (bVal < 0n))) {
|
|
208
|
+
q--;
|
|
209
|
+
r += bVal;
|
|
210
|
+
}
|
|
211
|
+
if (rounding === 'EUCLIDEAN' && r < 0n) {
|
|
212
|
+
q -= bVal < 0n ? -1n : 1n;
|
|
213
|
+
r += bVal < 0n ? -bVal : bVal;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (overflow) {
|
|
217
|
+
q = mode === 'SATURATE' ? -min - 1n : min;
|
|
218
|
+
r = 0n;
|
|
219
|
+
}
|
|
220
|
+
const divZero = zero ? lit(0).eq(0) : falseCond();
|
|
221
|
+
const trapOverflow = overflow && mode === 'TRAP' ? lit(0).eq(0) : undefined;
|
|
222
|
+
return new FixedWord(this.width, this.signed, constant(this.width, this.signed, remainder ? r : q), failures(this.failure, b.failure, divZero, trapOverflow));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
export const fixedFailure = (value) => value instanceof FixedWord ? value.failure : value instanceof Term ? storedTermFailures.get(value) : undefined;
|
|
226
|
+
export const fixedSelect = (condition, a, b) => {
|
|
227
|
+
if (a.width !== b.width || a.signed !== b.signed)
|
|
228
|
+
throw new TypeError('selected fixed integers need identical kinds');
|
|
229
|
+
const failure = a.failure || b.failure ? any(all(condition, a.failure ?? falseCond()), all(condition.not(), b.failure ?? falseCond())) : undefined;
|
|
230
|
+
return new FixedWord(a.width, a.signed, select(condition, a.limbs, b.limbs), failure);
|
|
231
|
+
};
|
|
232
|
+
export const fixedInt = (width, signed) => {
|
|
233
|
+
validKind(width, signed);
|
|
234
|
+
class FixedKind extends Kind {
|
|
235
|
+
vars(name) { return Array.from({ length: digits(width) }, (_, i) => `${name}$${i}`); }
|
|
236
|
+
assume(name) { return this.value(name).limbs.map(x => x.lt(base(width))); }
|
|
237
|
+
build(name, get) { return new FixedWord(width, signed, this.vars(name).map(get)); }
|
|
238
|
+
invariant(v) { return v.limbs.map(x => x.lt(base(width))); }
|
|
239
|
+
lift(value) { return new FixedWord(width, signed, constant(width, signed, parse(width, signed, value))); }
|
|
240
|
+
encode(name, raw) {
|
|
241
|
+
const limbs = constant(width, signed, parse(width, signed, raw));
|
|
242
|
+
return Object.fromEntries(this.vars(name).map((v, i) => [v, limbs[i].val]));
|
|
243
|
+
}
|
|
244
|
+
random(name, rand, _bound) {
|
|
245
|
+
return Object.fromEntries(this.vars(name).map(v => [v, rand(base(width))]));
|
|
246
|
+
}
|
|
247
|
+
sample(rand, bound) {
|
|
248
|
+
const n = BigInt(rand(Math.min(1 + Math.max(0, bound), signed ? base(width) / 2 : base(width))));
|
|
249
|
+
return width === 64 ? n.toString() : Number(n);
|
|
250
|
+
}
|
|
251
|
+
store(name, value) {
|
|
252
|
+
const word = value instanceof FixedWord ? value : this.lift(value);
|
|
253
|
+
if (word.width !== width || word.signed !== signed)
|
|
254
|
+
throw new TypeError('stored fixed integer has wrong kind');
|
|
255
|
+
return Object.fromEntries(this.vars(name).map((v, i) => {
|
|
256
|
+
const limb = word.limbs[i];
|
|
257
|
+
if (!word.failure)
|
|
258
|
+
return [v, limb];
|
|
259
|
+
const stored = new Term(limb.e, limb.val);
|
|
260
|
+
storedTermFailures.set(stored, word.failure);
|
|
261
|
+
return [v, stored];
|
|
262
|
+
}));
|
|
263
|
+
}
|
|
264
|
+
decode(name, vars) {
|
|
265
|
+
let n = 0n;
|
|
266
|
+
for (const v of this.vars(name).reverse()) {
|
|
267
|
+
const limb = vars[v];
|
|
268
|
+
if (limb === undefined || !Number.isInteger(limb) || limb < 0 || limb >= base(width))
|
|
269
|
+
throw new RangeError(`invalid fixed limb ${v}`);
|
|
270
|
+
n = n * BigInt(base(width)) + BigInt(limb);
|
|
271
|
+
}
|
|
272
|
+
if (signed && n >= 1n << BigInt(width - 1))
|
|
273
|
+
n -= 1n << BigInt(width);
|
|
274
|
+
return width === 64 ? n.toString() : Number(n);
|
|
275
|
+
}
|
|
276
|
+
empty() { return width === 64 ? '0' : 0; }
|
|
277
|
+
}
|
|
278
|
+
return new FixedKind();
|
|
279
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type Law, type Machine } from './machine.js';
|
|
2
|
+
import { type Liveness } from '../kernel/kernel.js';
|
|
3
|
+
import { type LeadsVerdict } from './cycles.js';
|
|
4
|
+
export declare function helpfulWriteSupports(m: Machine, l: Liveness): Law[];
|
|
5
|
+
export declare function leadsByWriteSupports(m: Machine, laws: Law[], l: Liveness, ms: number): LeadsVerdict | undefined;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// Candidate invariants for simple helpful steps. Everything in this file is search guidance: even a syntactically
|
|
2
|
+
// matched write may take a different branch or alias a watched key. Only proveLiveness may accept the proposals.
|
|
3
|
+
import { walk } from "./machine.js";
|
|
4
|
+
import { livenessHelpful, substE, trueC } from "../kernel/kernel.js";
|
|
5
|
+
import { clock, proveLiveness, proveProgress, withDeadline } from "../kernel/prove.js";
|
|
6
|
+
import { showC } from "./cycles.js";
|
|
7
|
+
const id = (x) => JSON.stringify(x);
|
|
8
|
+
const same = (a, b) => id(a) === id(b);
|
|
9
|
+
const v = (name) => ({ op: 'var', name }), k = (value) => ({ op: 'const', value });
|
|
10
|
+
const at = (map, key) => ({ op: 'get', map, key });
|
|
11
|
+
const eq = (a, b) => ({ op: '=', a, b }), le = (a, b) => ({ op: '<=', a, b });
|
|
12
|
+
const implies = (x, y) => ({ op: 'or', xs: [{ op: 'not', x }, y] });
|
|
13
|
+
const and = (x, y) => ({ op: 'and', xs: [x, y] });
|
|
14
|
+
const conjuncts = (x) => x.op === 'and' ? x.xs.flatMap(conjuncts) : [x];
|
|
15
|
+
const addends = (x) => x.op === 'add' ? [...addends(x.a), ...addends(x.b)] : [x];
|
|
16
|
+
const outcomes = (x) => x.op === 'if' ? [...outcomes(x.then), ...outcomes(x.else)] : [x];
|
|
17
|
+
const mentions = (t, f) => {
|
|
18
|
+
let hit = false;
|
|
19
|
+
walk(t, o => { if (f(o))
|
|
20
|
+
hit = true; });
|
|
21
|
+
return hit;
|
|
22
|
+
};
|
|
23
|
+
const reads = (t, names) => mentions(t, o => o.op === 'var' && names.includes(o.name));
|
|
24
|
+
export function helpfulWriteSupports(m, l) {
|
|
25
|
+
if (m.history || m.sums && Object.keys(m.sums).length || m.inputMaps?.length || livenessHelpful(l).length !== 1)
|
|
26
|
+
return [];
|
|
27
|
+
const request = livenessHelpful(l)[0].request, stored = Object.entries(m.step.maps ?? {});
|
|
28
|
+
if (!stored.length || stored.length > 32)
|
|
29
|
+
return [];
|
|
30
|
+
const updates = Object.entries(m.step.state ?? {}), found = new Map();
|
|
31
|
+
const add = (holds) => {
|
|
32
|
+
const key = id(holds);
|
|
33
|
+
if (found.size >= 8 || found.has(key))
|
|
34
|
+
return;
|
|
35
|
+
found.set(key, { name: `liveness write support ${found.size + 1}: ${showC(holds)}`, holds });
|
|
36
|
+
};
|
|
37
|
+
// A helpful request names a key past a max/frontier. Where a scalar is either retained or written from that
|
|
38
|
+
// same request key, compare its value with the written expression at the frontier. Ordinary freshness laws about
|
|
39
|
+
// cells beyond the frontier are separately supplied by freshLaws. No clock assumption is added to the machine.
|
|
40
|
+
for (const [mark, next] of updates)
|
|
41
|
+
if (next.op === 'max') {
|
|
42
|
+
const key = same(next.a, v(mark)) ? next.b : same(next.b, v(mark)) ? next.a : undefined;
|
|
43
|
+
if (key?.op !== 'var' || !request[key.name] || !reads(request[key.name], [mark]) ||
|
|
44
|
+
!stored.some(([, w]) => 'key' in w && same(w.key, key)))
|
|
45
|
+
continue;
|
|
46
|
+
for (const [x, e] of updates)
|
|
47
|
+
if (e.op === 'if' && same(e.else, v(x)) && reads(e.then, [key.name])) {
|
|
48
|
+
if (mentions(e.then, o => o.op === 'flag' || o.op === 'get') || reads(e.then, m.inputs.filter(y => y !== key.name)))
|
|
49
|
+
continue;
|
|
50
|
+
add(le(v(x), substE(e.then, { vars: { [key.name]: v(mark) }, maps: {}, flags: {} })));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// A fixed observer is pending when its row's tag equals a constant. Follow its write selector to sibling payloads
|
|
54
|
+
// at the same write key and simple bounds on those payloads. Each projection is only a proposed reachable law.
|
|
55
|
+
for (const trigger of conjuncts(l.from))
|
|
56
|
+
if (trigger.op === '=') {
|
|
57
|
+
const tag = trigger.a.op === 'get' && trigger.b.op === 'const' ? trigger.a :
|
|
58
|
+
trigger.b.op === 'get' && trigger.a.op === 'const' ? trigger.b : undefined;
|
|
59
|
+
if (!tag || reads(tag.key, [...m.inputs, ...Object.keys(m.state)]) || mentions(tag.key, o => o.op === 'get'))
|
|
60
|
+
continue;
|
|
61
|
+
const write = m.step.maps?.[tag.map];
|
|
62
|
+
if (!write || !('key' in write) || write.value.op !== 'if' || !same(write.value.else, at(tag.map, write.key)))
|
|
63
|
+
continue;
|
|
64
|
+
const guard = write.value.c, bounds = new Map(), expanded = new Set();
|
|
65
|
+
const scan = (x) => walk(x, o => {
|
|
66
|
+
if (o.op === 'flag' && typeof o.name === 'string' && !expanded.has(o.name)) {
|
|
67
|
+
expanded.add(o.name);
|
|
68
|
+
const f = m.step.flags?.[o.name];
|
|
69
|
+
if (f)
|
|
70
|
+
scan(f);
|
|
71
|
+
}
|
|
72
|
+
if (o.op === '<=' && o.b?.op === 'var' && o.a?.op === 'add' && !reads(o.b, m.inputs)) {
|
|
73
|
+
bounds.set(id(o), { a: o.a, b: o.b });
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
scan(guard);
|
|
77
|
+
for (const [field, w] of stored) {
|
|
78
|
+
if (field === tag.map || !('key' in w) || !same(w.key, write.key) || w.value.op !== 'if')
|
|
79
|
+
continue;
|
|
80
|
+
const cells = at(field, tag.key), init = m.maps?.[field], old = at(field, w.key);
|
|
81
|
+
if (!same(w.value.else, old) || id(w.value.c) !== id(guard)) {
|
|
82
|
+
// A Boolean payload can be encoded after the row update as `if (updated predicate) then 1 else 0`.
|
|
83
|
+
// Recognize a sibling only if that predicate reads its own old bit and the row's update selector.
|
|
84
|
+
const c = w.value.c;
|
|
85
|
+
if ((init === 0 || init === 1) && same(w.value.then, k(1)) && same(w.value.else, k(0)) &&
|
|
86
|
+
mentions(c, o => o.op === 'get' && same(o, old)) && mentions(c, o => id(o) === id(guard))) {
|
|
87
|
+
add(implies(trigger, eq(cells, k(init))));
|
|
88
|
+
}
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const val = w.value.then, leaves = outcomes(val);
|
|
92
|
+
// A co-written small choice or bit; the pending state may preserve its initial value.
|
|
93
|
+
if (init !== undefined && leaves.every(x => x.op === 'const' && x.value <= 4) &&
|
|
94
|
+
leaves.some(x => same(x, k(init))))
|
|
95
|
+
add(implies(trigger, eq(cells, k(init))));
|
|
96
|
+
for (const { a, b } of bounds.values())
|
|
97
|
+
if (addends(a).some(x => same(val, x))) {
|
|
98
|
+
add(implies(trigger, le(cells, b)));
|
|
99
|
+
// A simultaneously accumulated value using the same amount may be capped by the same guard bound.
|
|
100
|
+
for (const [x, e] of updates)
|
|
101
|
+
if (!same(v(x), b) && mentions(e, o => o.op === 'add' && addends(o).some(t => same(t, val))))
|
|
102
|
+
add(le(v(x), b));
|
|
103
|
+
// With a parameter-or-request payload, zero in the watched cell may identify the zero parameter case.
|
|
104
|
+
if (leaves.some(x => x.op === 'var' && m.inputs.includes(x.name))) {
|
|
105
|
+
for (const x of leaves)
|
|
106
|
+
if (x.op === 'var' && m.params.includes(x.name))
|
|
107
|
+
add(implies(and(trigger, eq(cells, k(0))), eq(x, k(0))));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return [...found.values()];
|
|
113
|
+
}
|
|
114
|
+
// Proof proposal only. The ordinary prover checks initiation, simultaneous preservation, stay, coverage,
|
|
115
|
+
// all helpful branches and the original fairness obligations. A wrong projection costs at most this stage's grant.
|
|
116
|
+
export function leadsByWriteSupports(m, laws, l, ms) {
|
|
117
|
+
if (ms < 500 || l.fairness === 'strong')
|
|
118
|
+
return undefined;
|
|
119
|
+
const until = clock() + ms, rank = [k(0)], candidates = helpfulWriteSupports(m, l);
|
|
120
|
+
if (!candidates.length)
|
|
121
|
+
return undefined;
|
|
122
|
+
let alive = candidates.filter(c => !laws.some(law => law.name === c.name));
|
|
123
|
+
while (alive.length && clock() < until) {
|
|
124
|
+
const all = [...laws, ...alive];
|
|
125
|
+
const out = withDeadline(until, () => proveLiveness(m, all, { ...l, rank }));
|
|
126
|
+
if (out.proven && out.checked) {
|
|
127
|
+
// The legacy step proof with unnamed weak-help fields need not show enabledness. For this new route, require
|
|
128
|
+
// one checked completion in each pending state: fill the spare fields with constants (a proof witness, not a
|
|
129
|
+
// new assumption about the scheduler or what values all executions must send).
|
|
130
|
+
const h = livenessHelpful(l)[0];
|
|
131
|
+
let progress;
|
|
132
|
+
if (m.assume && m.inputs.some(i => !(i in h.request))) {
|
|
133
|
+
const request = Object.fromEntries(m.inputs.map(i => [i, h.request[i] ?? k(0)]));
|
|
134
|
+
const a = withDeadline(until, () => proveProgress(m, all, { name: `${l.name}: helpful completion is allowed`,
|
|
135
|
+
when: and(and(l.from, { op: 'not', x: l.to }), h.when), then: trueC, request }));
|
|
136
|
+
if (!a.proven || !a.checked)
|
|
137
|
+
return undefined;
|
|
138
|
+
progress = a;
|
|
139
|
+
}
|
|
140
|
+
return { proven: true, checked: true, rank, laws: alive, evidence: { machine: m, laws: all, liveness: { ...l, rank }, report: out, ...(progress ? { progress } : {}) }, found: { rank: '(0)', laws: alive.map(c => showC(c.holds)) } };
|
|
141
|
+
}
|
|
142
|
+
const drop = alive.findIndex(c => out.failed?.goal.startsWith(`${c.name} @`));
|
|
143
|
+
if (drop < 0)
|
|
144
|
+
return undefined;
|
|
145
|
+
alive = alive.filter((_, i) => i !== drop);
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|