gdp-wired 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +237 -2
- package/dist/core/check.d.ts +3 -0
- package/dist/core/check.js +31 -0
- package/dist/core/compose.d.ts +24 -0
- package/dist/core/compose.js +121 -0
- package/dist/core/dsl.d.ts +268 -0
- package/dist/core/dsl.js +526 -0
- package/dist/core/machine.d.ts +145 -0
- package/dist/core/machine.js +516 -0
- package/dist/core/solver.d.ts +41 -0
- package/dist/core/solver.js +182 -0
- package/dist/kernel/kernel.d.ts +88 -0
- package/dist/kernel/kernel.js +593 -0
- package/dist/kernel/prove.d.ts +43 -0
- package/dist/kernel/prove.js +347 -0
- package/package.json +79 -4
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { type C, type E, type Law, type Machine, type State } from './machine.js';
|
|
2
|
+
import { type Goal, type Loop, type StepLaw, type Tree } from '../kernel/kernel.js';
|
|
3
|
+
export type TermLike = Term | number;
|
|
4
|
+
export declare const termOf: (t: TermLike) => E;
|
|
5
|
+
export declare class Term {
|
|
6
|
+
readonly e: E;
|
|
7
|
+
constructor(e: E);
|
|
8
|
+
plus(b: TermLike): Term;
|
|
9
|
+
minus(b: TermLike): Term;
|
|
10
|
+
times(k: number): Term;
|
|
11
|
+
mul(b: TermLike): Term;
|
|
12
|
+
lt(b: TermLike): Cond;
|
|
13
|
+
atMost(b: TermLike): Cond;
|
|
14
|
+
gt(b: TermLike): Cond;
|
|
15
|
+
atLeast(b: TermLike): Cond;
|
|
16
|
+
eq(b: TermLike): Cond;
|
|
17
|
+
ne(b: TermLike): Cond;
|
|
18
|
+
}
|
|
19
|
+
export declare class Cond {
|
|
20
|
+
readonly c: C;
|
|
21
|
+
constructor(c: C);
|
|
22
|
+
and(...xs: Cond[]): Cond;
|
|
23
|
+
or(...xs: Cond[]): Cond;
|
|
24
|
+
not(): Cond;
|
|
25
|
+
implies(b: Cond): Cond;
|
|
26
|
+
iff(b: Cond): Cond;
|
|
27
|
+
}
|
|
28
|
+
export declare const lit: (v: number) => Term;
|
|
29
|
+
export declare const yes: Cond;
|
|
30
|
+
export declare const no: Cond;
|
|
31
|
+
export declare const all: (...cs: Cond[]) => Cond;
|
|
32
|
+
export declare const any: (...cs: Cond[]) => Cond;
|
|
33
|
+
export declare const not: (c: Cond) => Cond;
|
|
34
|
+
export declare const min: (a: TermLike, b: TermLike) => Term;
|
|
35
|
+
export declare const max: (a: TermLike, b: TermLike) => Term;
|
|
36
|
+
export declare const when: (c: Cond, then: TermLike, otherwise: TermLike) => Term;
|
|
37
|
+
export declare const choose: (c: Cond, then: Cond, otherwise: Cond) => Cond;
|
|
38
|
+
export declare const count: (c: Cond) => Term;
|
|
39
|
+
export declare const sum: (first: TermLike, ...rest: TermLike[]) => Term;
|
|
40
|
+
type Rand = (k: number) => number;
|
|
41
|
+
type Raw = number | boolean | string | null | undefined;
|
|
42
|
+
export declare abstract class Kind<V> {
|
|
43
|
+
readonly _value: V;
|
|
44
|
+
abstract vars(name: string): string[];
|
|
45
|
+
abstract assume(name: string): Cond[];
|
|
46
|
+
abstract value(name: string, prefix?: string): V;
|
|
47
|
+
abstract encode(name: string, raw: Raw): Record<string, number>;
|
|
48
|
+
abstract random(name: string, rand: Rand, bound: number): Record<string, number>;
|
|
49
|
+
}
|
|
50
|
+
export declare const nat: Kind<Term>;
|
|
51
|
+
export declare const upTo: (maxValue: number) => Kind<Term>;
|
|
52
|
+
export declare class Bool extends Cond {
|
|
53
|
+
readonly n: Term;
|
|
54
|
+
constructor(n: Term);
|
|
55
|
+
}
|
|
56
|
+
export declare const bool: Kind<Bool>;
|
|
57
|
+
export declare class Choice<K extends string> {
|
|
58
|
+
readonly n: Term;
|
|
59
|
+
readonly values: readonly K[];
|
|
60
|
+
constructor(n: Term, values: readonly K[]);
|
|
61
|
+
is(k: K): Cond;
|
|
62
|
+
match(cases: {
|
|
63
|
+
[k in K]: () => Cond;
|
|
64
|
+
}): Cond;
|
|
65
|
+
match(cases: {
|
|
66
|
+
[k in K]: () => TermLike;
|
|
67
|
+
}): Term;
|
|
68
|
+
}
|
|
69
|
+
export declare const oneOf: <const K extends string>(...values: K[]) => Kind<Choice<K>>;
|
|
70
|
+
export declare class Maybe<V> {
|
|
71
|
+
private readonly present;
|
|
72
|
+
private readonly inner;
|
|
73
|
+
constructor(present: Term, inner: V);
|
|
74
|
+
get isSome(): Cond;
|
|
75
|
+
get isNone(): Cond;
|
|
76
|
+
match(cases: {
|
|
77
|
+
none: () => Cond;
|
|
78
|
+
some: (value: V) => Cond;
|
|
79
|
+
}): Cond;
|
|
80
|
+
match(cases: {
|
|
81
|
+
none: () => TermLike;
|
|
82
|
+
some: (value: V) => TermLike;
|
|
83
|
+
}): Term;
|
|
84
|
+
}
|
|
85
|
+
export declare const maybe: <V>(of: Kind<V>) => Kind<Maybe<V>>;
|
|
86
|
+
export type MapWrite = {
|
|
87
|
+
readonly map: string;
|
|
88
|
+
readonly key: E;
|
|
89
|
+
readonly value: E;
|
|
90
|
+
};
|
|
91
|
+
export declare class MapRef {
|
|
92
|
+
readonly name: string;
|
|
93
|
+
readonly init: number;
|
|
94
|
+
constructor(name: string, init: number);
|
|
95
|
+
at(key: TermLike): Term;
|
|
96
|
+
set(key: TermLike, value: TermLike): MapWrite;
|
|
97
|
+
bump(key: TermLike, c?: Cond, by?: TermLike): MapWrite;
|
|
98
|
+
}
|
|
99
|
+
export type Forall = {
|
|
100
|
+
readonly forall: (key: Term) => Cond;
|
|
101
|
+
};
|
|
102
|
+
export declare const forall: (f: (key: Term) => Cond) => Forall;
|
|
103
|
+
export type Relation = {
|
|
104
|
+
readonly when: Cond;
|
|
105
|
+
readonly holds: Cond;
|
|
106
|
+
};
|
|
107
|
+
export declare const relation: (holds: Cond, whenever?: Cond) => Relation;
|
|
108
|
+
type Kinds = Record<string, Kind<unknown>>;
|
|
109
|
+
type ValuesOf<K extends Kinds> = {
|
|
110
|
+
[n in keyof K]: K[n] extends Kind<infer V> ? V : never;
|
|
111
|
+
};
|
|
112
|
+
type Obj = object;
|
|
113
|
+
type Update<S extends Obj, M extends Obj> = Partial<{
|
|
114
|
+
[k in keyof S]: TermLike;
|
|
115
|
+
} & {
|
|
116
|
+
[k in keyof M]: MapWrite;
|
|
117
|
+
}>;
|
|
118
|
+
type Spec = {
|
|
119
|
+
name: string;
|
|
120
|
+
params: [string, Kind<unknown>][];
|
|
121
|
+
inputs: [string, Kind<unknown>][];
|
|
122
|
+
state: [string, number | ((p: never) => TermLike)][];
|
|
123
|
+
maps: [string, number][];
|
|
124
|
+
flags: ((env: never) => Record<string, Cond>)[];
|
|
125
|
+
step?: (env: never) => Record<string, TermLike | MapWrite | undefined>;
|
|
126
|
+
pre: ((p: never) => Cond)[];
|
|
127
|
+
assume: ((env: never) => Cond)[];
|
|
128
|
+
record?: string[];
|
|
129
|
+
sums: [string, (r: never, p: never) => TermLike][];
|
|
130
|
+
queries: [string, (r: never, v: never) => TermLike][];
|
|
131
|
+
keep?: (env: never) => Cond;
|
|
132
|
+
relations?: (env: never) => Relation[];
|
|
133
|
+
prune?: {
|
|
134
|
+
order: string;
|
|
135
|
+
after: (p: never) => TermLike;
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
export declare class Rule<P extends Obj = {}, I extends Obj = {}, S extends Obj = {}, M extends Obj = {}, F extends Obj = {}, R extends Obj = {}, Su extends Obj = {}, Q extends Obj = {}> {
|
|
139
|
+
private readonly spec;
|
|
140
|
+
constructor(spec: Spec);
|
|
141
|
+
private with;
|
|
142
|
+
get name(): string;
|
|
143
|
+
params<K extends Kinds>(kinds: K): Rule<P & ValuesOf<K>, I, S, M, F, R, Su, Q>;
|
|
144
|
+
inputs<K extends Kinds>(kinds: K): Rule<P, I & ValuesOf<K>, S, M, F, R, Su, Q>;
|
|
145
|
+
state<T extends Record<string, number | ((p: P) => TermLike)>>(init: T): Rule<P, I, S & {
|
|
146
|
+
[k in keyof T]: Term;
|
|
147
|
+
}, M, F, R, Su, Q>;
|
|
148
|
+
maps<T extends Record<string, number>>(init: T): Rule<P, I, S, M & {
|
|
149
|
+
[k in keyof T]: MapRef;
|
|
150
|
+
}, F, R, Su, Q>;
|
|
151
|
+
flags<T extends Record<string, Cond>>(f: (env: P & I & S & M & F & Su & Q) => T): Rule<P, I, S, M, F & {
|
|
152
|
+
[k in keyof T]: Cond;
|
|
153
|
+
}, R, Su, Q>;
|
|
154
|
+
step(f: (env: P & I & S & M & F & Su & Q) => Update<S, M>): Rule<P, I, S, M, F, R, Su, Q>;
|
|
155
|
+
pre(f: (p: P) => Cond): Rule<P, I, S, M, F, R, Su, Q>;
|
|
156
|
+
assume(f: (env: P & I & S & M & F & Su & Q) => Cond): Rule<P, I, S, M, F, R, Su, Q>;
|
|
157
|
+
log<const N extends keyof I & string>(...record: N[]): Rule<P, I, S, M, F, Pick<I, N>, Su, Q>;
|
|
158
|
+
sums<T extends Record<string, (r: R, p: P) => TermLike>>(sums: T): Rule<P, I, S, M, F, R, Su & {
|
|
159
|
+
[k in keyof T]: Term;
|
|
160
|
+
}, Q>;
|
|
161
|
+
queries<T extends Record<string, (r: R, v: P & I) => TermLike>>(queries: T): Rule<P, I, S, M, F, R, Su, Q & {
|
|
162
|
+
[k in keyof T]: Term;
|
|
163
|
+
}>;
|
|
164
|
+
keep(f: (env: P & I & S & M & F & Su & Q) => Cond): Rule<P, I, S, M, F, R, Su, Q>;
|
|
165
|
+
relations(f: (env: P & I & Su & Q) => Relation[]): Rule<P, I, S, M, F, R, Su, Q>;
|
|
166
|
+
prune(order: keyof R & string, after: (p: P) => TermLike): Rule<P, I, S, M, F, R, Su, Q>;
|
|
167
|
+
law(name: string, f: (env: P & S & M & Su) => Cond | Forall): Law;
|
|
168
|
+
stepLaw(name: string, then: (before: P & I & S & M & F & Su & Q, after: S & M & Su) => Cond | Forall, when?: (before: P & I & S & M & F & Su & Q) => Cond): StepLaw;
|
|
169
|
+
stepsLaw(name: string, k: number, then: (first: P & I & S & M & F & Su & Q, last: S & M & Su, requests: (I & Q)[]) => Cond | Forall, when?: (before: P & I & S & M & F & Su & Q, j: number) => Cond | undefined): StepLaw;
|
|
170
|
+
decide(name: string, given: (env: P & I) => Cond | Cond[], then: (env: P & I) => Cond): Decision;
|
|
171
|
+
private kindAssumptions;
|
|
172
|
+
private env;
|
|
173
|
+
get machine(): Machine;
|
|
174
|
+
encodeParams(values: {
|
|
175
|
+
[k in keyof P]: Raw;
|
|
176
|
+
}): Record<string, number>;
|
|
177
|
+
encodeInput(values: {
|
|
178
|
+
[k in keyof I]: Raw;
|
|
179
|
+
}): Record<string, number>;
|
|
180
|
+
randomParams(rand: Rand, bound: number): Record<string, number>;
|
|
181
|
+
randomInput(rand: Rand, bound: number): Record<string, number>;
|
|
182
|
+
get orderedBy(): string | undefined;
|
|
183
|
+
interpreter(): {
|
|
184
|
+
init: (params: {
|
|
185
|
+
[x: string]: number;
|
|
186
|
+
}) => State;
|
|
187
|
+
queries: (params: {
|
|
188
|
+
[x: string]: number;
|
|
189
|
+
}, st: State, input: {
|
|
190
|
+
[x: string]: number;
|
|
191
|
+
}) => {
|
|
192
|
+
[x: string]: number;
|
|
193
|
+
};
|
|
194
|
+
step: (params: {
|
|
195
|
+
[x: string]: number;
|
|
196
|
+
}, st: State, input: {
|
|
197
|
+
[x: string]: number;
|
|
198
|
+
}) => State;
|
|
199
|
+
};
|
|
200
|
+
compiled(): {
|
|
201
|
+
init: (params: object) => State & {
|
|
202
|
+
head?: number;
|
|
203
|
+
};
|
|
204
|
+
fork: (st: State) => State;
|
|
205
|
+
step: (params: object, st: State, input: object) => State & {
|
|
206
|
+
head?: number;
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
export declare const rule: (name: string) => Rule;
|
|
211
|
+
type AnyOf<R, K extends 'decide' | 'step' | 'law'> = R extends Rule<infer P, infer I, infer S, infer M, infer F, any, infer Su, infer Q> ? K extends 'decide' ? P & I : K extends 'law' ? P & S & M & Su : P & I & S & M & F & Su & Q : never;
|
|
212
|
+
export type DecisionEnv<R> = AnyOf<R, 'decide'>;
|
|
213
|
+
export type StepEnv<R> = AnyOf<R, 'step'>;
|
|
214
|
+
export type LawEnv<R> = AnyOf<R, 'law'>;
|
|
215
|
+
export type Decision = {
|
|
216
|
+
name: string;
|
|
217
|
+
rule: Rule<Obj>;
|
|
218
|
+
given: C[];
|
|
219
|
+
then: C;
|
|
220
|
+
goal: Goal;
|
|
221
|
+
};
|
|
222
|
+
export declare class LoopRule<P extends Obj = {}, I extends Obj = {}, S extends Obj = {}> {
|
|
223
|
+
private readonly spec;
|
|
224
|
+
constructor(spec: {
|
|
225
|
+
name: string;
|
|
226
|
+
params: [string, Kind<unknown>][];
|
|
227
|
+
inputs: [string, Kind<unknown>][];
|
|
228
|
+
state: [string, number | ((p: never) => TermLike)][];
|
|
229
|
+
while?: (env: never) => Cond;
|
|
230
|
+
body?: (env: never) => Record<string, TermLike | undefined>;
|
|
231
|
+
pre: ((p: never) => Cond)[];
|
|
232
|
+
assume: ((env: never) => Cond)[];
|
|
233
|
+
rank?: (env: never) => TermLike | TermLike[];
|
|
234
|
+
});
|
|
235
|
+
private with;
|
|
236
|
+
params<K extends Kinds>(kinds: K): LoopRule<P & ValuesOf<K>, I, S>;
|
|
237
|
+
inputs<K extends Kinds>(kinds: K): LoopRule<P, I & ValuesOf<K>, S>;
|
|
238
|
+
state<T extends Record<string, number | ((p: P) => TermLike)>>(init: T): LoopRule<P, I, S & {
|
|
239
|
+
[k in keyof T]: Term;
|
|
240
|
+
}>;
|
|
241
|
+
while(f: (env: P & S) => Cond): LoopRule<P, I, S>;
|
|
242
|
+
body(f: (env: P & I & S) => Partial<{
|
|
243
|
+
[k in keyof S]: TermLike;
|
|
244
|
+
}>): LoopRule<P, I, S>;
|
|
245
|
+
pre(f: (p: P) => Cond): LoopRule<P, I, S>;
|
|
246
|
+
assume(f: (env: P & I & S) => Cond): LoopRule<P, I, S>;
|
|
247
|
+
rank(f: (env: P & S) => TermLike | TermLike[]): LoopRule<P, I, S>;
|
|
248
|
+
law(name: string, f: (env: P & S) => Cond): Law;
|
|
249
|
+
private env;
|
|
250
|
+
get loop(): Loop;
|
|
251
|
+
get rankTerms(): E[];
|
|
252
|
+
randomParams(rand: Rand, bound: number): Record<string, number>;
|
|
253
|
+
randomInput(rand: Rand, bound: number): Record<string, number>;
|
|
254
|
+
}
|
|
255
|
+
export declare const loop: (name: string) => LoopRule;
|
|
256
|
+
export type Verdict = {
|
|
257
|
+
proven: boolean;
|
|
258
|
+
checked: boolean;
|
|
259
|
+
stuck?: string;
|
|
260
|
+
goal?: string;
|
|
261
|
+
witness?: Record<string, number>;
|
|
262
|
+
trees?: Record<string, Tree>;
|
|
263
|
+
};
|
|
264
|
+
export declare function proveRule(r: Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj>, laws: Law[]): Verdict;
|
|
265
|
+
export declare function proveSteps(r: Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj>, laws: Law[], stepLaws: StepLaw[]): Verdict;
|
|
266
|
+
export type { StepLaw };
|
|
267
|
+
export declare const proveDecision: (d: Decision) => Verdict;
|
|
268
|
+
export declare const proveLoop: (l: LoopRule<Obj, Obj, Obj>, laws: Law[]) => Verdict;
|