gdp-wired 0.22.0 → 0.24.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 +27 -2
- package/dist/core/bmc.d.ts +49 -0
- package/dist/core/bmc.js +509 -0
- package/dist/core/check.d.ts +1 -1
- package/dist/core/check.js +19 -6
- package/dist/core/cycles.d.ts +38 -0
- package/dist/core/cycles.js +531 -43
- package/dist/core/data.d.ts +25 -1
- package/dist/core/data.js +984 -215
- package/dist/core/discover.js +8 -3
- package/dist/core/dsl.d.ts +17 -3
- package/dist/core/dsl.js +81 -15
- package/dist/core/extract-check.d.ts +77 -0
- package/dist/core/extract-check.js +785 -0
- package/dist/core/extract-ir.d.ts +139 -0
- package/dist/core/extract-ir.js +198 -0
- package/dist/core/extract-print.d.ts +30 -0
- package/dist/core/extract-print.js +426 -0
- package/dist/core/extract-read.d.ts +298 -0
- package/dist/core/extract-read.js +1298 -0
- package/dist/core/extract-shapes.d.ts +13 -0
- package/dist/core/extract-shapes.js +993 -0
- package/dist/core/extract-xstate.d.ts +34 -0
- package/dist/core/extract-xstate.js +320 -0
- package/dist/core/extract.d.ts +31 -0
- package/dist/core/extract.js +249 -0
- package/dist/core/infer.js +518 -5
- package/dist/core/kind.d.ts +30 -0
- package/dist/core/kind.js +81 -0
- package/dist/core/nonterm.d.ts +34 -0
- package/dist/core/nonterm.js +610 -0
- package/dist/core/q.d.ts +17 -0
- package/dist/core/q.js +52 -0
- package/dist/core/rationals.d.ts +112 -0
- package/dist/core/rationals.js +281 -0
- package/dist/core/relation.d.ts +28 -0
- package/dist/core/relation.js +483 -0
- package/dist/core/solver.d.ts +5 -2
- package/dist/core/solver.js +65 -20
- package/dist/core/transfer.d.ts +3 -3
- package/dist/core/transfer.js +69 -42
- package/dist/core/xstate.d.ts +6 -0
- package/dist/core/xstate.js +70 -12
- package/dist/kernel/cdcl.d.ts +13 -0
- package/dist/kernel/cdcl.js +307 -236
- package/dist/kernel/kernel.d.ts +106 -0
- package/dist/kernel/kernel.js +342 -0
- package/dist/kernel/prove.js +14 -3
- package/dist/kernel/qprove.d.ts +18 -0
- package/dist/kernel/qprove.js +157 -0
- package/dist/kernel/reader.d.ts +14 -0
- package/dist/kernel/reader.js +102 -14
- package/dist/kernel/simplex.d.ts +4 -0
- package/dist/kernel/simplex.js +38 -11
- package/dist/lean-kernel/emit.d.ts +46 -1
- package/dist/lean-kernel/emit.js +125 -16
- package/dist/lean-kernel/library.js +4 -1
- package/dist/lean-kernel/qlean.d.ts +26 -0
- package/dist/lean-kernel/qlean.js +105 -0
- package/package.json +16 -3
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { type QC, type QE, type QLaw, type QMachine, type Tree } from '../kernel/kernel.js';
|
|
2
|
+
import { type Rat } from './q.js';
|
|
3
|
+
export type { Rat };
|
|
4
|
+
export type RatLike = RatTerm | number | string | bigint | Rat;
|
|
5
|
+
export declare class RatTerm {
|
|
6
|
+
readonly e: QE;
|
|
7
|
+
constructor(e: QE);
|
|
8
|
+
plus(b: RatLike): RatTerm;
|
|
9
|
+
minus(b: RatLike): RatTerm;
|
|
10
|
+
times(k: RatLike): RatTerm;
|
|
11
|
+
over(k: RatLike): RatTerm;
|
|
12
|
+
negate(): RatTerm;
|
|
13
|
+
lt(b: RatLike): RatCond;
|
|
14
|
+
atMost(b: RatLike): RatCond;
|
|
15
|
+
gt(b: RatLike): RatCond;
|
|
16
|
+
atLeast(b: RatLike): RatCond;
|
|
17
|
+
eq(b: RatLike): RatCond;
|
|
18
|
+
ne(b: RatLike): RatCond;
|
|
19
|
+
min(b: RatLike): RatTerm;
|
|
20
|
+
max(b: RatLike): RatTerm;
|
|
21
|
+
}
|
|
22
|
+
export declare class RatCond {
|
|
23
|
+
readonly c: QC;
|
|
24
|
+
constructor(c: QC);
|
|
25
|
+
and(...xs: RatCond[]): RatCond;
|
|
26
|
+
or(...xs: RatCond[]): RatCond;
|
|
27
|
+
not(): RatCond;
|
|
28
|
+
implies(b: RatCond): RatCond;
|
|
29
|
+
iff(b: RatCond): RatCond;
|
|
30
|
+
}
|
|
31
|
+
export declare const rlit: (x: RatLike) => RatTerm;
|
|
32
|
+
export declare const ryes: RatCond;
|
|
33
|
+
export declare const rno: RatCond;
|
|
34
|
+
export declare const rall: (...cs: RatCond[]) => RatCond;
|
|
35
|
+
export declare const rany: (...cs: RatCond[]) => RatCond;
|
|
36
|
+
export declare const rmin: (a: RatLike, b: RatLike) => RatTerm;
|
|
37
|
+
export declare const rmax: (a: RatLike, b: RatLike) => RatTerm;
|
|
38
|
+
export declare const rwhen: (c: RatCond, then: RatLike, otherwise: RatLike) => RatTerm;
|
|
39
|
+
export type RatKind = {
|
|
40
|
+
kind: 'rat';
|
|
41
|
+
nonneg: boolean;
|
|
42
|
+
};
|
|
43
|
+
export declare const rat: RatKind;
|
|
44
|
+
export declare const nonneg: RatKind;
|
|
45
|
+
type Terms<K extends string> = {
|
|
46
|
+
[k in K]: RatTerm;
|
|
47
|
+
};
|
|
48
|
+
type Init<P extends string> = RatLike | ((p: Terms<P>) => RatLike);
|
|
49
|
+
export type RatLaw<S extends string, P extends string> = QLaw & {
|
|
50
|
+
readonly __rat?: [S, P];
|
|
51
|
+
};
|
|
52
|
+
export declare class RatRule<P extends string = never, I extends string = never, S extends string = never> {
|
|
53
|
+
readonly name: string;
|
|
54
|
+
private readonly p;
|
|
55
|
+
private readonly i;
|
|
56
|
+
private readonly s;
|
|
57
|
+
private readonly stepFn?;
|
|
58
|
+
private readonly preFn?;
|
|
59
|
+
private readonly assumeFn?;
|
|
60
|
+
constructor(name: string, p?: Record<string, RatKind>, i?: Record<string, RatKind>, s?: Record<string, Init<P>>, stepFn?: ((x: Terms<P | I | S>) => Partial<Record<S, RatLike>>) | undefined, preFn?: ((x: Terms<P>) => RatCond) | undefined, assumeFn?: ((x: Terms<P | I | S>) => RatCond) | undefined);
|
|
61
|
+
params<Q extends string>(decl: Record<Q, RatKind>): RatRule<Q, I, S>;
|
|
62
|
+
inputs<J extends string>(decl: Record<J, RatKind>): RatRule<P, J, S>;
|
|
63
|
+
state<T extends string>(init: Record<T, Init<P>>): RatRule<P, I, T>;
|
|
64
|
+
step(fn: (x: Terms<P | I | S>) => Partial<Record<S, RatLike>>): RatRule<P, I, S>;
|
|
65
|
+
pre(fn: (x: Terms<P>) => RatCond): RatRule<P, I, S>;
|
|
66
|
+
assume(fn: (x: Terms<P | I | S>) => RatCond): RatRule<P, I, S>;
|
|
67
|
+
get paramNames(): P[];
|
|
68
|
+
get inputNames(): I[];
|
|
69
|
+
get stateNames(): S[];
|
|
70
|
+
get machine(): QMachine;
|
|
71
|
+
law(name: string, fn: (x: Terms<P | S>) => RatCond): RatLaw<S, P>;
|
|
72
|
+
run(params: Record<P, RatLike>, requests: Record<I, RatLike>[]): Record<S, string>[];
|
|
73
|
+
}
|
|
74
|
+
export declare const ratRule: (name: string) => RatRule;
|
|
75
|
+
export type QEnv = Record<string, Rat>;
|
|
76
|
+
export declare function evalQE(e: QE, env: QEnv): Rat;
|
|
77
|
+
export declare function evalQC(c: QC, env: QEnv): boolean;
|
|
78
|
+
export type QRuntime = {
|
|
79
|
+
init: (params: QEnv) => QEnv;
|
|
80
|
+
allowed: (params: QEnv, state: QEnv, input: QEnv) => boolean;
|
|
81
|
+
step: (params: QEnv, state: QEnv, input: QEnv) => QEnv;
|
|
82
|
+
};
|
|
83
|
+
export declare function interpretQ(m: QMachine): QRuntime;
|
|
84
|
+
export declare function compileQJs(m: QMachine): string;
|
|
85
|
+
export declare function loadQJs(m: QMachine): QRuntime;
|
|
86
|
+
export type QProof = {
|
|
87
|
+
trees: Record<string, {
|
|
88
|
+
start: Tree;
|
|
89
|
+
step: Tree;
|
|
90
|
+
}>;
|
|
91
|
+
};
|
|
92
|
+
export type QVerdict = {
|
|
93
|
+
proven: boolean;
|
|
94
|
+
checked: boolean;
|
|
95
|
+
goal?: string;
|
|
96
|
+
stuck?: 'sat' | 'unknown';
|
|
97
|
+
why?: string;
|
|
98
|
+
witness?: Record<string, string>;
|
|
99
|
+
run?: {
|
|
100
|
+
params: Record<string, string>;
|
|
101
|
+
requests: Record<string, string>[];
|
|
102
|
+
};
|
|
103
|
+
proof?: QProof;
|
|
104
|
+
};
|
|
105
|
+
export declare function refuteQ(m: QMachine, laws: QLaw[], depth?: number, until?: number): QVerdict | undefined;
|
|
106
|
+
export declare function proveQMachine(m: QMachine, laws: QLaw[], opts?: {
|
|
107
|
+
ms?: number;
|
|
108
|
+
}): QVerdict;
|
|
109
|
+
export declare function proveRatRule(r: RatRule<string, string, string> | QMachine, laws: QLaw[], opts?: {
|
|
110
|
+
depth?: number;
|
|
111
|
+
ms?: number;
|
|
112
|
+
}): QVerdict;
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// Rules over the rationals: params, inputs and state whose values are exact rationals (fees in fractions, tokens that
|
|
2
|
+
// refill by a fraction, ledgers in decimals), laws over them proven by linear arithmetic over ℚ, a decidable theory
|
|
3
|
+
// whose refutations are Farkas certificates. Nothing is scaled or encoded: 0.1 is 1/10, a third is 1/3, and the
|
|
4
|
+
// kernel's rows are the terms' own linear forms over a positive denominator.
|
|
5
|
+
//
|
|
6
|
+
// const split = ratRule('fee split')
|
|
7
|
+
// .params({amount: nonneg})
|
|
8
|
+
// .state({fee: 0, payout: 0})
|
|
9
|
+
// .step(({amount}) => ({fee: amount.times('0.025'), payout: amount.times('0.975')}))
|
|
10
|
+
// proveRatRule(split, [split.law('the parts sum to the whole', ({fee, payout, amount}) => fee.plus(payout).eq(amount))])
|
|
11
|
+
//
|
|
12
|
+
// Untrusted, like dsl.ts: everything here compiles to kernel.ts's QMachine and QLaw, which the kernel checks (and
|
|
13
|
+
// Lean again, Rationals.lean). The terms are linear: × and ÷ by a constant only; a product of two unknowns is refused
|
|
14
|
+
// (nonlinear arithmetic over the reals is decidable but not by these certificates, and with transcendental functions
|
|
15
|
+
// it isn't). A rule is all rationals: a natural is a rational with integrality, and integer constraints mixed with
|
|
16
|
+
// rational ones are mixed-integer arithmetic, which these certificates (no cut) don't refute; rules over the naturals
|
|
17
|
+
// and integers stay in dsl.ts, where the cut is the integrality rule.
|
|
18
|
+
import { checkQMachine, qObligations, qSubstC, qSubstE, qTrue } from "../kernel/kernel.js";
|
|
19
|
+
import { growQ } from "../kernel/qprove.js";
|
|
20
|
+
import { Q0, q, qadd, qcmp, qeq, qmax, qmin, qmul, qparse, qshow, qsub } from "./q.js";
|
|
21
|
+
const termOf = (x) => (x instanceof RatTerm ? x.e : { op: 'const', q: qparse(x) });
|
|
22
|
+
const constOf = (x, what) => {
|
|
23
|
+
if (x instanceof RatTerm) {
|
|
24
|
+
if (x.e.op === 'const')
|
|
25
|
+
return x.e.q;
|
|
26
|
+
throw new TypeError(`${what}: a product of two unknowns is nonlinear, outside linear arithmetic over ℚ (times and over take a constant)`);
|
|
27
|
+
}
|
|
28
|
+
return qparse(x);
|
|
29
|
+
};
|
|
30
|
+
// A rational-valued term: + and − exact, × and ÷ by a constant.
|
|
31
|
+
export class RatTerm {
|
|
32
|
+
e;
|
|
33
|
+
constructor(e) {
|
|
34
|
+
this.e = e;
|
|
35
|
+
}
|
|
36
|
+
plus(b) { return new RatTerm({ op: 'add', a: this.e, b: termOf(b) }); }
|
|
37
|
+
minus(b) { return new RatTerm({ op: 'sub', a: this.e, b: termOf(b) }); }
|
|
38
|
+
times(k) { return new RatTerm({ op: 'mul', k: constOf(k, 'times'), a: this.e }); }
|
|
39
|
+
over(k) {
|
|
40
|
+
const c = constOf(k, 'over');
|
|
41
|
+
if (c.num === 0n)
|
|
42
|
+
throw new RangeError('over: division by 0');
|
|
43
|
+
return new RatTerm({ op: 'mul', k: q(c.den, c.num), a: this.e });
|
|
44
|
+
}
|
|
45
|
+
negate() { return this.times(-1); }
|
|
46
|
+
lt(b) { return new RatCond({ op: '<', a: this.e, b: termOf(b) }); }
|
|
47
|
+
atMost(b) { return new RatCond({ op: '<=', a: this.e, b: termOf(b) }); }
|
|
48
|
+
gt(b) { return new RatCond({ op: '<', a: termOf(b), b: this.e }); }
|
|
49
|
+
atLeast(b) { return new RatCond({ op: '<=', a: termOf(b), b: this.e }); }
|
|
50
|
+
eq(b) { return new RatCond({ op: '=', a: this.e, b: termOf(b) }); }
|
|
51
|
+
ne(b) { return this.eq(b).not(); }
|
|
52
|
+
min(b) { return rmin(this, b); }
|
|
53
|
+
max(b) { return rmax(this, b); }
|
|
54
|
+
}
|
|
55
|
+
export class RatCond {
|
|
56
|
+
c;
|
|
57
|
+
constructor(c) {
|
|
58
|
+
this.c = c;
|
|
59
|
+
}
|
|
60
|
+
and(...xs) { return rall(this, ...xs); }
|
|
61
|
+
or(...xs) { return rany(this, ...xs); }
|
|
62
|
+
not() { return new RatCond({ op: 'not', x: this.c }); }
|
|
63
|
+
implies(b) { return rany(this.not(), b); }
|
|
64
|
+
iff(b) { return rall(this.implies(b), b.implies(this)); }
|
|
65
|
+
}
|
|
66
|
+
export const rlit = (x) => new RatTerm(termOf(x));
|
|
67
|
+
export const ryes = new RatCond(qTrue);
|
|
68
|
+
export const rno = ryes.not();
|
|
69
|
+
export const rall = (...cs) => (cs.length === 0 ? ryes : cs.reduce((a, b) => new RatCond({ op: 'and', a: a.c, b: b.c })));
|
|
70
|
+
export const rany = (...cs) => (cs.length === 0 ? rno : cs.reduce((a, b) => new RatCond({ op: 'or', a: a.c, b: b.c })));
|
|
71
|
+
export const rmin = (a, b) => new RatTerm({ op: 'min', a: termOf(a), b: termOf(b) });
|
|
72
|
+
export const rmax = (a, b) => new RatTerm({ op: 'max', a: termOf(a), b: termOf(b) });
|
|
73
|
+
export const rwhen = (c, then, otherwise) => new RatTerm({ op: 'if', c: c.c, then: termOf(then), else: termOf(otherwise) });
|
|
74
|
+
export const rat = { kind: 'rat', nonneg: false };
|
|
75
|
+
export const nonneg = { kind: 'rat', nonneg: true };
|
|
76
|
+
const v = (name) => new RatTerm({ op: 'var', name });
|
|
77
|
+
const terms = (names) => Object.fromEntries(names.map(n => [n, v(n)]));
|
|
78
|
+
const kindConds = (decl) => Object.entries(decl).filter(([, k]) => k.nonneg).map(([n]) => v(n).atLeast(0));
|
|
79
|
+
export class RatRule {
|
|
80
|
+
name;
|
|
81
|
+
p;
|
|
82
|
+
i;
|
|
83
|
+
s;
|
|
84
|
+
stepFn;
|
|
85
|
+
preFn;
|
|
86
|
+
assumeFn;
|
|
87
|
+
constructor(name, p = {}, i = {}, s = {}, stepFn, preFn, assumeFn) {
|
|
88
|
+
this.name = name;
|
|
89
|
+
this.p = p;
|
|
90
|
+
this.i = i;
|
|
91
|
+
this.s = s;
|
|
92
|
+
this.stepFn = stepFn;
|
|
93
|
+
this.preFn = preFn;
|
|
94
|
+
this.assumeFn = assumeFn;
|
|
95
|
+
}
|
|
96
|
+
params(decl) { return new RatRule(this.name, decl, this.i, this.s, this.stepFn, this.preFn, this.assumeFn); }
|
|
97
|
+
inputs(decl) { return new RatRule(this.name, this.p, decl, this.s, this.stepFn, this.preFn, this.assumeFn); }
|
|
98
|
+
state(init) { return new RatRule(this.name, this.p, this.i, init, this.stepFn, this.preFn, this.assumeFn); }
|
|
99
|
+
step(fn) { return new RatRule(this.name, this.p, this.i, this.s, fn, this.preFn, this.assumeFn); }
|
|
100
|
+
pre(fn) { return new RatRule(this.name, this.p, this.i, this.s, this.stepFn, fn, this.assumeFn); }
|
|
101
|
+
assume(fn) { return new RatRule(this.name, this.p, this.i, this.s, this.stepFn, this.preFn, fn); }
|
|
102
|
+
get paramNames() { return Object.keys(this.p); }
|
|
103
|
+
get inputNames() { return Object.keys(this.i); }
|
|
104
|
+
get stateNames() { return Object.keys(this.s); }
|
|
105
|
+
get machine() {
|
|
106
|
+
const names = [...this.paramNames, ...this.inputNames, ...this.stateNames];
|
|
107
|
+
if (new Set(names).size !== names.length)
|
|
108
|
+
throw new Error(`${this.name}: a name is declared twice`);
|
|
109
|
+
const all = terms(names), ps = terms(this.paramNames);
|
|
110
|
+
const state = Object.fromEntries(Object.entries(this.s).map(([n, init]) => [n, termOf(typeof init === 'function' ? init(ps) : init)]));
|
|
111
|
+
const out = this.stepFn ? this.stepFn(all) : {};
|
|
112
|
+
for (const n of Object.keys(out))
|
|
113
|
+
if (!(n in this.s))
|
|
114
|
+
throw new Error(`${this.name}: the step sets ${n}, which is not in the state`);
|
|
115
|
+
const step = Object.fromEntries(Object.entries(out).map(([n, t]) => [n, termOf(t)]));
|
|
116
|
+
const pre = rall(...kindConds(this.p), ...(this.preFn ? [this.preFn(ps)] : []));
|
|
117
|
+
const assume = rall(...kindConds(this.i), ...(this.assumeFn ? [this.assumeFn(all)] : []));
|
|
118
|
+
return { name: this.name, params: this.paramNames, inputs: this.inputNames, state, step, pre: pre.c, assume: assume.c };
|
|
119
|
+
}
|
|
120
|
+
// A law over the state and the params.
|
|
121
|
+
law(name, fn) { return { name, holds: fn(terms([...this.paramNames, ...this.stateNames])).c }; }
|
|
122
|
+
// The rule run exactly: the states after each request, from params (each value a number, decimal text or fraction).
|
|
123
|
+
run(params, requests) {
|
|
124
|
+
const r = interpretQ(this.machine), ps = Object.fromEntries(Object.entries(params).map(([n, x]) => [n, qparse(x)]));
|
|
125
|
+
let st = r.init(ps);
|
|
126
|
+
const out = [st];
|
|
127
|
+
for (const req of requests) {
|
|
128
|
+
const input = Object.fromEntries(Object.entries(req).map(([n, x]) => [n, qparse(x)]));
|
|
129
|
+
if (!r.allowed(ps, st, input))
|
|
130
|
+
throw new Error(`${this.name}: the request ${JSON.stringify(req)} is not allowed (the rule's assumption)`);
|
|
131
|
+
st = r.step(ps, st, input);
|
|
132
|
+
out.push(st);
|
|
133
|
+
}
|
|
134
|
+
return out.map(s => Object.fromEntries(Object.entries(s).map(([n, x]) => [n, qshow(x)])));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
export const ratRule = (name) => new RatRule(name);
|
|
138
|
+
export function evalQE(e, env) {
|
|
139
|
+
switch (e.op) {
|
|
140
|
+
case 'var': {
|
|
141
|
+
const x = env[e.name];
|
|
142
|
+
if (!x)
|
|
143
|
+
throw new Error(`no value for ${e.name}`);
|
|
144
|
+
return x;
|
|
145
|
+
}
|
|
146
|
+
case 'const': return q(e.q.num, e.q.den);
|
|
147
|
+
case 'add': return qadd(evalQE(e.a, env), evalQE(e.b, env));
|
|
148
|
+
case 'sub': return qsub(evalQE(e.a, env), evalQE(e.b, env));
|
|
149
|
+
case 'mul': return qmul(q(e.k.num, e.k.den), evalQE(e.a, env));
|
|
150
|
+
case 'min': return qmin(evalQE(e.a, env), evalQE(e.b, env));
|
|
151
|
+
case 'max': return qmax(evalQE(e.a, env), evalQE(e.b, env));
|
|
152
|
+
case 'if': return evalQC(e.c, env) ? evalQE(e.then, env) : evalQE(e.else, env);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export function evalQC(c, env) {
|
|
156
|
+
switch (c.op) {
|
|
157
|
+
case '<=': return qcmp(evalQE(c.a, env), evalQE(c.b, env)) <= 0;
|
|
158
|
+
case '<': return qcmp(evalQE(c.a, env), evalQE(c.b, env)) < 0;
|
|
159
|
+
case '=': return qeq(evalQE(c.a, env), evalQE(c.b, env));
|
|
160
|
+
case 'and': return evalQC(c.a, env) && evalQC(c.b, env);
|
|
161
|
+
case 'or': return evalQC(c.a, env) || evalQC(c.b, env);
|
|
162
|
+
case 'not': return !evalQC(c.x, env);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
export function interpretQ(m) {
|
|
166
|
+
return {
|
|
167
|
+
init: params => ({ ...Object.fromEntries(Object.entries(m.state).map(([n, e]) => [n, evalQE(e, params)])) }),
|
|
168
|
+
allowed: (params, state, input) => evalQC(m.assume ?? qTrue, { ...params, ...state, ...input }),
|
|
169
|
+
step: (params, state, input) => {
|
|
170
|
+
const env = { ...params, ...state, ...input };
|
|
171
|
+
return { ...state, ...Object.fromEntries(Object.entries(m.step).map(([n, e]) => [n, evalQE(e, env)])) };
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
// The same machine as generated JavaScript over bigint fractions (each value [num, den], reduced).
|
|
176
|
+
export function compileQJs(m) {
|
|
177
|
+
const lit = (r) => `[${r.num}n, ${r.den}n]`;
|
|
178
|
+
const e = (t) => {
|
|
179
|
+
switch (t.op) {
|
|
180
|
+
case 'var': return `v[${JSON.stringify(t.name)}]`;
|
|
181
|
+
case 'const': return lit(q(t.q.num, t.q.den));
|
|
182
|
+
case 'add': return `A(${e(t.a)}, ${e(t.b)})`;
|
|
183
|
+
case 'sub': return `S(${e(t.a)}, ${e(t.b)})`;
|
|
184
|
+
case 'mul': return `M(${lit(q(t.k.num, t.k.den))}, ${e(t.a)})`;
|
|
185
|
+
case 'min': return `(x => (y => C(x, y) <= 0 ? x : y)(${e(t.b)}))(${e(t.a)})`;
|
|
186
|
+
case 'max': return `(x => (y => C(x, y) <= 0 ? y : x)(${e(t.b)}))(${e(t.a)})`;
|
|
187
|
+
case 'if': return `(${c(t.c)} ? ${e(t.then)} : ${e(t.else)})`;
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
const c = (t) => {
|
|
191
|
+
switch (t.op) {
|
|
192
|
+
case '<=': return `(C(${e(t.a)}, ${e(t.b)}) <= 0)`;
|
|
193
|
+
case '<': return `(C(${e(t.a)}, ${e(t.b)}) < 0)`;
|
|
194
|
+
case '=': return `(C(${e(t.a)}, ${e(t.b)}) === 0)`;
|
|
195
|
+
case 'and': return `(${c(t.a)} && ${c(t.b)})`;
|
|
196
|
+
case 'or': return `(${c(t.a)} || ${c(t.b)})`;
|
|
197
|
+
case 'not': return `!${c(t.x)}`;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
const obj = (r) => `{${Object.entries(r).map(([n, t]) => `${JSON.stringify(n)}: ${e(t)}`).join(', ')}}`;
|
|
201
|
+
return `const G = (a, b) => { a = a < 0n ? -a : a; b = b < 0n ? -b : b; while (b) [a, b] = [b, a % b]; return a }
|
|
202
|
+
const N = (n, d) => { if (d < 0n) { n = -n; d = -d } const g = G(n, d) || 1n; return [n / g, d / g] }
|
|
203
|
+
const A = (a, b) => N(a[0] * b[1] + b[0] * a[1], a[1] * b[1]), S = (a, b) => N(a[0] * b[1] - b[0] * a[1], a[1] * b[1])
|
|
204
|
+
const M = (a, b) => N(a[0] * b[0], a[1] * b[1]), C = (a, b) => { const d = a[0] * b[1] - b[0] * a[1]; return d < 0n ? -1 : d > 0n ? 1 : 0 }
|
|
205
|
+
return {
|
|
206
|
+
init: v => (${obj(m.state)}),
|
|
207
|
+
allowed: v => ${c(m.assume ?? qTrue)},
|
|
208
|
+
step: v => ({...v.$state, ...${obj(m.step)}}),
|
|
209
|
+
}`;
|
|
210
|
+
}
|
|
211
|
+
export function loadQJs(m) {
|
|
212
|
+
// oxlint-disable-next-line typescript/no-implied-eval
|
|
213
|
+
const made = new Function(compileQJs(m))();
|
|
214
|
+
const to = (r) => Object.fromEntries(Object.entries(r).map(([n, x]) => [n, [x.num, x.den]]));
|
|
215
|
+
const from = (r) => Object.fromEntries(Object.entries(r).map(([n, [a, b]]) => [n, { num: a, den: b }]));
|
|
216
|
+
return {
|
|
217
|
+
init: params => from(made.init(to(params))),
|
|
218
|
+
allowed: (params, state, input) => made.allowed({ ...to(params), ...to(state), ...to(input) }),
|
|
219
|
+
step: (params, state, input) => from(made.step({ ...to(params), ...to(state), ...to(input), $state: to(state) })),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
const shown = (r) => Object.fromEntries(Object.entries(r).map(([n, x]) => [n, qshow(x)]));
|
|
223
|
+
// The machine unrolled d steps from the start: request j's inputs named <input>@j.
|
|
224
|
+
function unrolled(m, d) {
|
|
225
|
+
const hyps = [m.pre ?? qTrue];
|
|
226
|
+
let at = { ...m.state };
|
|
227
|
+
for (let j = 0; j < d; j++) {
|
|
228
|
+
const ren = { ...at, ...Object.fromEntries(m.inputs.map(i => [i, { op: 'var', name: `${i}@${j}` }])) };
|
|
229
|
+
hyps.push(qSubstC(m.assume ?? qTrue, ren));
|
|
230
|
+
at = { ...at, ...Object.fromEntries(Object.entries(m.step).map(([n, e]) => [n, qSubstE(e, ren)])) };
|
|
231
|
+
}
|
|
232
|
+
return { hyps, at };
|
|
233
|
+
}
|
|
234
|
+
// A run from the start that breaks a law, within `depth` requests: each unrolled goal decided over ℚ (exactly, so a
|
|
235
|
+
// model is a run), the run replayed by the interpreter, which must break the law too.
|
|
236
|
+
export function refuteQ(m, laws, depth = 3, until = performance.now() + 2_000) {
|
|
237
|
+
const run = interpretQ(m);
|
|
238
|
+
for (let d = 0; d <= depth; d++) {
|
|
239
|
+
const { hyps, at } = unrolled(m, d);
|
|
240
|
+
for (const law of laws) {
|
|
241
|
+
const goal = { name: `${law.name} @${d} steps`, hyps: [...hyps, { op: 'not', x: qSubstC(law.holds, at) }] };
|
|
242
|
+
const out = growQ(goal, { until });
|
|
243
|
+
if ('tree' in out || out.stuck !== 'sat' || !out.rational)
|
|
244
|
+
continue;
|
|
245
|
+
const val = (n) => out.rational[n] ?? Q0;
|
|
246
|
+
const params = Object.fromEntries(m.params.map(p => [p, val(p)]));
|
|
247
|
+
const requests = Array.from({ length: d }, (_, j) => Object.fromEntries(m.inputs.map(i => [i, val(`${i}@${j}`)])));
|
|
248
|
+
let st = run.init(params), ok = evalQC(m.pre ?? qTrue, params);
|
|
249
|
+
for (const req of requests) {
|
|
250
|
+
ok &&= run.allowed(params, st, req);
|
|
251
|
+
st = run.step(params, st, req);
|
|
252
|
+
}
|
|
253
|
+
if (ok && !evalQC(law.holds, { ...params, ...st }))
|
|
254
|
+
return { proven: false, checked: false, goal: goal.name, stuck: 'sat', witness: shown({ ...params, ...st }), run: { params: shown(params), requests: requests.map(shown) } };
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
// Each law by induction: its start and step goals grown by the search, the trees checked by the kernel.
|
|
260
|
+
export function proveQMachine(m, laws, opts = {}) {
|
|
261
|
+
const until = performance.now() + (opts.ms ?? 20_000), trees = {};
|
|
262
|
+
for (const law of laws) {
|
|
263
|
+
const goals = qObligations(m, laws, law), got = {};
|
|
264
|
+
for (const which of ['start', 'step']) {
|
|
265
|
+
const out = growQ(goals[which], { until });
|
|
266
|
+
if (!('tree' in out)) {
|
|
267
|
+
const w = out.rational ? shown(Object.fromEntries(Object.entries(out.rational).filter(([n]) => !n.includes('@')))) : undefined;
|
|
268
|
+
return { proven: false, checked: false, goal: goals[which].name, stuck: out.stuck, ...(out.why ? { why: out.why } : {}), ...(w ? { witness: w } : {}) };
|
|
269
|
+
}
|
|
270
|
+
got[which] = out.tree;
|
|
271
|
+
}
|
|
272
|
+
trees[law.name] = got;
|
|
273
|
+
}
|
|
274
|
+
return { proven: true, checked: checkQMachine(m, laws, trees), proof: { trees } };
|
|
275
|
+
}
|
|
276
|
+
// A rule's laws: refuted by a run from the start if one breaks them within `depth` requests, else proven by induction
|
|
277
|
+
// (or not, with the step that breaks one from where they all hold).
|
|
278
|
+
export function proveRatRule(r, laws, opts = {}) {
|
|
279
|
+
const m = r instanceof RatRule ? r.machine : r;
|
|
280
|
+
return refuteQ(m, laws, opts.depth ?? 3) ?? proveQMachine(m, laws, opts);
|
|
281
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Law } from './machine.js';
|
|
2
|
+
import type { Loop } from '../kernel/kernel.js';
|
|
3
|
+
import { type Lin } from './solver.js';
|
|
4
|
+
export type Path = {
|
|
5
|
+
rows: Lin[];
|
|
6
|
+
pins: Record<string, number>;
|
|
7
|
+
post: Record<string, Lin>;
|
|
8
|
+
};
|
|
9
|
+
export type Relation = {
|
|
10
|
+
paths: Path[];
|
|
11
|
+
ints: Map<string, [string, string]>;
|
|
12
|
+
nats: string[];
|
|
13
|
+
state: string[];
|
|
14
|
+
params: string[];
|
|
15
|
+
loc?: string;
|
|
16
|
+
from: number[];
|
|
17
|
+
to: number[];
|
|
18
|
+
exact: boolean;
|
|
19
|
+
};
|
|
20
|
+
export declare function relationOf(l: Loop, laws?: Law[], cap?: number): Relation | undefined;
|
|
21
|
+
export type Generators = {
|
|
22
|
+
names: string[];
|
|
23
|
+
vertices: number[][];
|
|
24
|
+
rays: number[][];
|
|
25
|
+
lines: number[][];
|
|
26
|
+
};
|
|
27
|
+
export declare function generators(rows: Lin[], names: string[], nats?: string[], cap?: number, until?: number): Generators | undefined;
|
|
28
|
+
export declare function houdini(rel: Relation, cands: Lin[][], locOf: (i: number) => [number, number], until: number): Lin[][] | undefined;
|