gdp-wired 0.2.0 → 0.4.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.
@@ -1,10 +1,11 @@
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';
1
+ import { type C, type E, type Law, type Machine, type Request, type State } from './machine.js';
2
+ import { type Goal, type Liveness, type Loop, type StepLaw, type Tree } from '../kernel/kernel.js';
3
3
  export type TermLike = Term | number;
4
4
  export declare const termOf: (t: TermLike) => E;
5
5
  export declare class Term {
6
6
  readonly e: E;
7
- constructor(e: E);
7
+ readonly val?: number | undefined;
8
+ constructor(e: E, val?: number | undefined);
8
9
  plus(b: TermLike): Term;
9
10
  minus(b: TermLike): Term;
10
11
  times(k: number): Term;
@@ -18,7 +19,8 @@ export declare class Term {
18
19
  }
19
20
  export declare class Cond {
20
21
  readonly c: C;
21
- constructor(c: C);
22
+ readonly ok?: boolean | undefined;
23
+ constructor(c: C, ok?: boolean | undefined);
22
24
  and(...xs: Cond[]): Cond;
23
25
  or(...xs: Cond[]): Cond;
24
26
  not(): Cond;
@@ -33,7 +35,10 @@ export declare const any: (...cs: Cond[]) => Cond;
33
35
  export declare const not: (c: Cond) => Cond;
34
36
  export declare const min: (a: TermLike, b: TermLike) => Term;
35
37
  export declare const max: (a: TermLike, b: TermLike) => Term;
36
- export declare const when: (c: Cond, then: TermLike, otherwise: TermLike) => Term;
38
+ export declare function when(c: Cond, then: TermLike, otherwise: TermLike): Term;
39
+ export declare function when(c: Cond, then: Decimal, otherwise: Decimal | number): Decimal;
40
+ export declare function when(c: Cond, then: Decimal | number, otherwise: Decimal): Decimal;
41
+ export declare function when(c: Cond, then: IntLike, otherwise: IntLike): Int;
37
42
  export declare const choose: (c: Cond, then: Cond, otherwise: Cond) => Cond;
38
43
  export declare const count: (c: Cond) => Term;
39
44
  export declare const sum: (first: TermLike, ...rest: TermLike[]) => Term;
@@ -46,6 +51,50 @@ export declare abstract class Kind<V> {
46
51
  abstract value(name: string, prefix?: string): V;
47
52
  abstract encode(name: string, raw: Raw): Record<string, number>;
48
53
  abstract random(name: string, rand: Rand, bound: number): Record<string, number>;
54
+ sample(_rand: Rand, _bound: number): Raw;
55
+ }
56
+ export type IntLike = Int | Term | number;
57
+ export declare class Int {
58
+ readonly pos: Term;
59
+ readonly neg: Term;
60
+ constructor(pos: Term, neg: Term);
61
+ static of(x: IntLike): Int;
62
+ plus(b: IntLike): Int;
63
+ minus(b: IntLike): Int;
64
+ negate(): Int;
65
+ times(k: number): Int;
66
+ lt(b: IntLike): Cond;
67
+ atMost(b: IntLike): Cond;
68
+ gt(b: IntLike): Cond;
69
+ atLeast(b: IntLike): Cond;
70
+ eq(b: IntLike): Cond;
71
+ ne(b: IntLike): Cond;
72
+ min(b: IntLike): Int;
73
+ max(b: IntLike): Int;
74
+ get isNegative(): Cond;
75
+ get abs(): Term;
76
+ get clamped(): Term;
77
+ }
78
+ export type DecimalLike = Decimal | IntLike;
79
+ export declare class Decimal {
80
+ readonly int: Int;
81
+ readonly places: number;
82
+ constructor(int: Int, places: number);
83
+ lift(b: DecimalLike): Int;
84
+ private with;
85
+ plus(b: DecimalLike): Decimal;
86
+ minus(b: DecimalLike): Decimal;
87
+ negate(): Decimal;
88
+ times(k: number): Decimal;
89
+ lt(b: DecimalLike): Cond;
90
+ atMost(b: DecimalLike): Cond;
91
+ gt(b: DecimalLike): Cond;
92
+ atLeast(b: DecimalLike): Cond;
93
+ eq(b: DecimalLike): Cond;
94
+ ne(b: DecimalLike): Cond;
95
+ min(b: DecimalLike): Decimal;
96
+ max(b: DecimalLike): Decimal;
97
+ get isNegative(): Cond;
49
98
  }
50
99
  export declare const nat: Kind<Term>;
51
100
  export declare const upTo: (maxValue: number) => Kind<Term>;
@@ -83,35 +132,112 @@ export declare class Maybe<V> {
83
132
  }): Term;
84
133
  }
85
134
  export declare const maybe: <V>(of: Kind<V>) => Kind<Maybe<V>>;
135
+ export declare const intern: ((text: string) => number) & {
136
+ load(entries: [string, number][]): void;
137
+ };
138
+ export declare const interned: () => [string, number][];
139
+ export declare const globMatch: (pattern: string, text: string) => boolean;
140
+ export declare class Str {
141
+ readonly id: Term;
142
+ readonly length: Term;
143
+ private readonly bits;
144
+ constructor(id: Term, length: Term, bits: ReadonlyMap<string, Term>);
145
+ like(pattern: string): Cond;
146
+ get isEmpty(): Cond;
147
+ eq(b: Str | string): Cond;
148
+ ne(b: Str | string): Cond;
149
+ }
150
+ export declare const str: (opts?: {
151
+ like?: readonly string[];
152
+ maxLength?: number;
153
+ }) => Kind<Str>;
154
+ export declare const int: Kind<Int>;
155
+ export declare const decimal: (places: number) => Kind<Decimal>;
86
156
  export type MapWrite = {
87
157
  readonly map: string;
88
158
  readonly key: E;
89
159
  readonly value: E;
160
+ readonly concrete?: {
161
+ key?: number;
162
+ value?: number;
163
+ };
164
+ } | {
165
+ readonly map: string;
166
+ readonly each: string;
167
+ readonly value: E;
168
+ readonly concrete?: {
169
+ at: (key: Term) => TermLike;
170
+ };
90
171
  };
172
+ export declare const everyKey: (f: (key: Term) => Cond) => Cond;
173
+ export declare const someKey: (f: (key: Term) => Cond, witness?: TermLike) => Cond;
91
174
  export declare class MapRef {
92
175
  readonly name: string;
93
176
  readonly init: number;
177
+ private readonly cells;
94
178
  constructor(name: string, init: number);
95
179
  at(key: TermLike): Term;
96
180
  set(key: TermLike, value: TermLike): MapWrite;
97
181
  bump(key: TermLike, c?: Cond, by?: TermLike): MapWrite;
182
+ each(f: (key: Term) => TermLike): MapWrite;
183
+ has(key: TermLike): Cond;
184
+ subsetOf(other: MapRef): Cond;
185
+ disjointFrom(other: MapRef): Cond;
186
+ sameAs(other: MapRef): Cond;
187
+ within(n: number): Cond;
188
+ sizeBelow(n: number): Term;
189
+ }
190
+ export type ListOp = {
191
+ readonly when: Cond;
192
+ readonly at: (k: Term) => TermLike;
193
+ readonly len: TermLike;
194
+ };
195
+ export declare class ListRef {
196
+ readonly name: string;
197
+ readonly len: Term;
198
+ private readonly cells;
199
+ constructor(name: string, len: Term);
200
+ at(i: TermLike): Term;
201
+ has(x: TermLike): Cond;
202
+ sorted(): Cond;
203
+ push(x: TermLike, when?: Cond): ListOp;
204
+ pop(when?: Cond): ListOp;
205
+ set(i: TermLike, x: TermLike, when?: Cond): ListOp;
206
+ insertAt(i: TermLike, x: TermLike, when?: Cond): ListOp;
207
+ removeAt(i: TermLike, when?: Cond): ListOp;
208
+ apply(...ops: ListOp[]): Record<string, TermLike | MapWrite>;
209
+ within(): Forall;
210
+ increasing(): Forall;
98
211
  }
99
212
  export type Forall = {
100
- readonly forall: (key: Term) => Cond;
213
+ readonly forall: (key: Term, ...more: Term[]) => Cond;
101
214
  };
102
- export declare const forall: (f: (key: Term) => Cond) => Forall;
215
+ export declare function forall(f: (key: Term, key2: Term, key3: Term, key4: Term) => Cond): Forall;
103
216
  export type Relation = {
104
217
  readonly when: Cond;
105
218
  readonly holds: Cond;
106
219
  };
107
220
  export declare const relation: (holds: Cond, whenever?: Cond) => Relation;
221
+ export declare function nameQuantifiers(m: Machine): Machine;
222
+ export type FnDef = {
223
+ arity: number;
224
+ js: string | ((...args: number[]) => number);
225
+ lean?: string;
226
+ facts?: (f: (...args: TermLike[]) => Term) => Record<string, Cond | Forall>;
227
+ };
228
+ export declare const fn: (arity: number, js: FnDef["js"], opts?: {
229
+ lean?: string;
230
+ facts?: FnDef["facts"];
231
+ }) => FnDef;
232
+ export declare const divBy: (c: number) => FnDef;
233
+ export declare const modBy: (c: number) => FnDef;
108
234
  type Kinds = Record<string, Kind<unknown>>;
109
235
  type ValuesOf<K extends Kinds> = {
110
236
  [n in keyof K]: K[n] extends Kind<infer V> ? V : never;
111
237
  };
112
238
  type Obj = object;
113
239
  type Update<S extends Obj, M extends Obj> = Partial<{
114
- [k in keyof S]: TermLike;
240
+ [k in keyof S]: S[k] extends Decimal ? DecimalLike : S[k] extends Int ? IntLike : TermLike;
115
241
  } & {
116
242
  [k in keyof M]: MapWrite;
117
243
  }>;
@@ -121,6 +247,11 @@ type Spec = {
121
247
  inputs: [string, Kind<unknown>][];
122
248
  state: [string, number | ((p: never) => TermLike)][];
123
249
  maps: [string, number][];
250
+ lists?: string[];
251
+ signed?: [string, number | undefined][];
252
+ carried?: string[];
253
+ fns?: [string, FnDef][];
254
+ facts?: ((env: never) => Record<string, Cond | Forall>)[];
124
255
  flags: ((env: never) => Record<string, Cond>)[];
125
256
  step?: (env: never) => Record<string, TermLike | MapWrite | undefined>;
126
257
  pre: ((p: never) => Cond)[];
@@ -148,6 +279,25 @@ export declare class Rule<P extends Obj = {}, I extends Obj = {}, S extends Obj
148
279
  maps<T extends Record<string, number>>(init: T): Rule<P, I, S, M & {
149
280
  [k in keyof T]: MapRef;
150
281
  }, F, R, Su, Q>;
282
+ ints<T extends Record<string, number | ((p: P) => IntLike)>>(init: T): Rule<P, I, S & {
283
+ [k in keyof T]: Int;
284
+ }, M, F, R, Su, Q>;
285
+ decimals<T extends Record<string, number | string | ((p: P) => DecimalLike)>>(places: number, init: T): Rule<P, I, S & {
286
+ [k in keyof T]: Decimal;
287
+ }, M, F, R, Su, Q>;
288
+ private signedState;
289
+ lists<const N extends string>(...names: N[]): Rule<P, I, S & {
290
+ [k in N as `${k}_len`]: Term;
291
+ }, M & {
292
+ [k in N]: ListRef;
293
+ }, F, R, Su, Q>;
294
+ fns<T extends Record<string, FnDef>>(defs: T): Rule<P & {
295
+ [k in keyof T]: (...args: TermLike[]) => Term;
296
+ }, I, S, M, F, R, Su, Q>;
297
+ facts(f: (p: P) => Record<string, Cond | Forall>): Rule<P, I, S, M, F, R, Su, Q>;
298
+ carries<const N extends string>(...names: N[]): Rule<P, I & {
299
+ [k in N]: MapRef;
300
+ }, S, M, F, R, Su, Q>;
151
301
  flags<T extends Record<string, Cond>>(f: (env: P & I & S & M & F & Su & Q) => T): Rule<P, I, S, M, F & {
152
302
  [k in keyof T]: Cond;
153
303
  }, R, Su, Q>;
@@ -165,20 +315,64 @@ export declare class Rule<P extends Obj = {}, I extends Obj = {}, S extends Obj
165
315
  relations(f: (env: P & I & Su & Q) => Relation[]): Rule<P, I, S, M, F, R, Su, Q>;
166
316
  prune(order: keyof R & string, after: (p: P) => TermLike): Rule<P, I, S, M, F, R, Su, Q>;
167
317
  law(name: string, f: (env: P & S & M & Su) => Cond | Forall): Law;
318
+ private lawOf;
168
319
  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
320
  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;
321
+ leadsTo(name: string, spec: {
322
+ from: (env: P & S & M & Su) => Cond;
323
+ to: (env: P & S & M & Su) => Cond;
324
+ rank: (env: P & S & M & Su) => TermLike | TermLike[];
325
+ helpful: ((env: P & S & M & Su) => Record<string, TermLike>) | {
326
+ when?: (env: P & S & M & Su) => Cond;
327
+ request: (env: P & S & M & Su) => Record<string, TermLike>;
328
+ }[];
329
+ }): Liveness;
170
330
  decide(name: string, given: (env: P & I) => Cond | Cond[], then: (env: P & I) => Cond): Decision;
331
+ relate(name: string, given: (a: P & I, b: P & I) => Cond | Cond[], then: (a: P & I, b: P & I) => Cond): Decision;
171
332
  private kindAssumptions;
333
+ private signedValue;
172
334
  private env;
335
+ private names;
336
+ private whatIs;
337
+ private distinct;
173
338
  get machine(): Machine;
339
+ crossCheck(laws: Law[], stepLaws?: StepLaw[], opts?: {
340
+ trials?: number;
341
+ depth?: number;
342
+ bound?: number;
343
+ rand?: Rand;
344
+ }): {
345
+ checked: number;
346
+ failure?: string;
347
+ };
348
+ private verdict;
349
+ private crossStep;
350
+ compare<St extends Record<string, number>>(reference: {
351
+ init: (params: {
352
+ [k in keyof P]: Raw;
353
+ }) => St;
354
+ step: (params: {
355
+ [k in keyof P]: Raw;
356
+ }, st: St, request: {
357
+ [k in keyof I]: Raw;
358
+ }) => St;
359
+ }, opts?: Draws<{
360
+ [k in keyof I]: Raw;
361
+ }> & {
362
+ params?: (rand: Rand) => {
363
+ [k in keyof P]: Raw;
364
+ };
365
+ depth?: number;
366
+ }): Agreement;
174
367
  encodeParams(values: {
175
368
  [k in keyof P]: Raw;
176
369
  }): Record<string, number>;
177
370
  encodeInput(values: {
178
- [k in keyof I]: Raw;
179
- }): Record<string, number>;
371
+ [k in keyof I]: I[k] extends MapRef ? ReadonlySet<number> | readonly number[] | ReadonlyMap<number, number> : Raw;
372
+ }): Request;
180
373
  randomParams(rand: Rand, bound: number): Record<string, number>;
181
- randomInput(rand: Rand, bound: number): Record<string, number>;
374
+ randomInput(rand: Rand, bound: number): Request;
375
+ decode(st: State): Record<string, number>;
182
376
  get orderedBy(): string | undefined;
183
377
  interpreter(): {
184
378
  init: (params: {
@@ -186,16 +380,12 @@ export declare class Rule<P extends Obj = {}, I extends Obj = {}, S extends Obj
186
380
  }) => State;
187
381
  queries: (params: {
188
382
  [x: string]: number;
189
- }, st: State, input: {
190
- [x: string]: number;
191
- }) => {
383
+ }, st: State, input: Request) => {
192
384
  [x: string]: number;
193
385
  };
194
386
  step: (params: {
195
387
  [x: string]: number;
196
- }, st: State, input: {
197
- [x: string]: number;
198
- }) => State;
388
+ }, given: State, input: Request) => State;
199
389
  };
200
390
  compiled(): {
201
391
  init: (params: object) => State & {
@@ -219,6 +409,60 @@ export type Decision = {
219
409
  then: C;
220
410
  goal: Goal;
221
411
  };
412
+ type Result<T> = {
413
+ [k in keyof T]: T[k] extends Cond ? Cond : T[k] extends Decimal ? Decimal : T[k] extends Int ? Int : Term;
414
+ };
415
+ type Value<T> = {
416
+ [k in keyof T]: T[k] extends Cond ? boolean : number;
417
+ };
418
+ export declare class Fun<P extends Obj = {}, I extends Obj = {}, O extends Obj = {}> {
419
+ private readonly r;
420
+ private readonly out?;
421
+ constructor(r: Rule<P, I>, out?: ((env: never) => Record<string, TermLike | Cond | Int | Decimal>) | undefined);
422
+ get name(): string;
423
+ params<K extends Kinds>(kinds: K): Fun<P & ValuesOf<K>, I, O>;
424
+ inputs<K extends Kinds>(kinds: K): Fun<P, I & ValuesOf<K>, O>;
425
+ fns<T extends Record<string, FnDef>>(defs: T): Fun<P & {
426
+ [k in keyof T]: (...args: TermLike[]) => Term;
427
+ }, I, O>;
428
+ facts(f: (p: P) => Record<string, Cond | Forall>): Fun<P, I, O>;
429
+ pre(f: (p: P) => Cond): Fun<P, I, O>;
430
+ assume(f: (env: P & I) => Cond): Fun<P, I, O>;
431
+ returns<T extends Record<string, TermLike | Cond | Int | Decimal>>(f: (env: P & I) => T): Fun<P, I, Result<T>>;
432
+ private result;
433
+ law(name: string, given: (env: P & I, out: O) => Cond | Cond[], then: (out: O, env: P & I) => Cond): Decision;
434
+ relate(name: string, given: (a: P & I, b: P & I) => Cond | Cond[], then: (outA: O, outB: O, a: P & I, b: P & I) => Cond): Decision;
435
+ compare(reference: (values: {
436
+ [k in keyof I]: Raw;
437
+ }, params: {
438
+ [k in keyof P]: Raw;
439
+ }) => Partial<Value<O>>, opts?: Draws<{
440
+ [k in keyof I]: Raw;
441
+ }> & {
442
+ params?: {
443
+ [k in keyof P]: Raw;
444
+ };
445
+ }): Agreement;
446
+ get rule(): Rule<P, I>;
447
+ private compiledRule?;
448
+ run(values: {
449
+ [k in keyof I]: Raw;
450
+ }, params?: {
451
+ [k in keyof P]?: Raw;
452
+ }): Value<O>;
453
+ }
454
+ export declare const fun: (name: string) => Fun;
455
+ export type Agreement = {
456
+ checked: number;
457
+ failure?: string;
458
+ };
459
+ type Draws<T> = {
460
+ trials?: number;
461
+ bound?: number;
462
+ rand?: Rand;
463
+ edges?: T[];
464
+ draw?: (rand: Rand) => T;
465
+ };
222
466
  export declare class LoopRule<P extends Obj = {}, I extends Obj = {}, S extends Obj = {}> {
223
467
  private readonly spec;
224
468
  constructor(spec: {
@@ -259,10 +503,21 @@ export type Verdict = {
259
503
  stuck?: string;
260
504
  goal?: string;
261
505
  witness?: Record<string, number>;
506
+ why?: string;
262
507
  trees?: Record<string, Tree>;
263
508
  };
264
509
  export declare function proveRule(r: Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj>, laws: Law[]): Verdict;
265
510
  export declare function proveSteps(r: Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj>, laws: Law[], stepLaws: StepLaw[]): Verdict;
266
511
  export type { StepLaw };
512
+ export type { Liveness };
513
+ export declare function proveLeadsTo(r: Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj>, laws: Law[], l: Liveness): Verdict;
267
514
  export declare const proveDecision: (d: Decision) => Verdict;
515
+ export declare function crossCheckDecision(d: Decision, opts?: {
516
+ trials?: number;
517
+ bound?: number;
518
+ rand?: Rand;
519
+ }): {
520
+ checked: number;
521
+ failure?: string;
522
+ };
268
523
  export declare const proveLoop: (l: LoopRule<Obj, Obj, Obj>, laws: Law[]) => Verdict;