gdp-wired 0.33.1 → 0.34.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.
@@ -1904,7 +1904,8 @@ export function findLeadsTo(m, laws, l, sample, opts = {}) {
1904
1904
  const ms = until - clock();
1905
1905
  if (ms > 300) {
1906
1906
  const { stay, cover, helps } = livenessParts(m, lv), inf = inferLaws(sample.rule ?? m, laws, [stay, cover, ...helps], { ms });
1907
- const kept = inf.found.filter(f => !f.law.forall && !f.law.keys?.length), more = kept.map(f => f.law);
1907
+ // (laws about every key too: a liveness about every key needs those of its key's cells)
1908
+ const kept = inf.found.filter(f => !f.law.some?.length), more = kept.map(f => f.law);
1908
1909
  if (inf.proven && more.length) {
1909
1910
  const again = withDeadline(until + 5000, () => proveLiveness(m, [...laws, ...more], lv));
1910
1911
  if (again.proven && again.checked)
@@ -1,6 +1,7 @@
1
1
  import { type C, type E, type Law, type Recurrence, type Machine, type Rand, type Request, type State, type Sum } from './machine.js';
2
2
  import { type Fold, type Goal, type Liveness, type Loop, type Recur, type StepLaw, type Tree } from '../kernel/kernel.js';
3
3
  import { type Report } from '../kernel/prove.js';
4
+ import { findRing } from './live.js';
4
5
  export type TermLike = Term | number;
5
6
  export declare const termOf: (t: TermLike) => E;
6
7
  export declare class Term {
@@ -267,6 +268,15 @@ export type MapWrite = {
267
268
  };
268
269
  };
269
270
  export declare const everyKey: (f: (key: Term, key2: Term, key3: Term) => Cond) => Cond;
271
+ export type LeastDef = {
272
+ where: (key: Term) => Cond;
273
+ by: (key: Term) => (IntLike | {
274
+ desc: IntLike;
275
+ })[];
276
+ };
277
+ export declare const desc: (t: IntLike) => {
278
+ desc: IntLike;
279
+ };
270
280
  export declare const someKey: (f: (key: Term) => Cond, witness?: TermLike) => Cond;
271
281
  export type ExistsWitness<V> = V | number | {
272
282
  start: V | number;
@@ -532,6 +542,7 @@ type Spec = {
532
542
  record?: string[];
533
543
  sums: [string, (r: never, p: never) => TermLike][];
534
544
  queries: [string, (r: never, v: never) => TermLike][];
545
+ leasts?: [string, (env: never) => LeastDef][];
535
546
  keep?: (env: never) => Cond;
536
547
  relations?: (env: never) => Relation[];
537
548
  prune?: {
@@ -612,6 +623,9 @@ export declare class Rule<P extends Obj = {}, I extends Obj = {}, S extends Obj
612
623
  queries<T extends Record<string, (r: R, v: P & I) => TermLike>>(queries: T): Rule<P, I, S, M, F, R, Su, Q & {
613
624
  [k in keyof T]: Term;
614
625
  }>;
626
+ least<T extends Record<string, (env: P & I & S & M & Su & Q) => LeastDef>>(defs: T): Rule<P, I, S, M, F, R, Su, Q & {
627
+ [k in keyof T]: Maybe<Term>;
628
+ }>;
615
629
  keep(f: (env: P & I & S & M & F & Su & Q) => Cond): Rule<P, I, S, M, F, R, Su, Q>;
616
630
  relations(f: (env: P & I & Su & Q) => Relation[]): Rule<P, I, S, M, F, R, Su, Q>;
617
631
  prune(order: keyof R & string, after: (p: P) => TermLike): Rule<P, I, S, M, F, R, Su, Q>;
@@ -628,16 +642,16 @@ export declare class Rule<P extends Obj = {}, I extends Obj = {}, S extends Obj
628
642
  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;
629
643
  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;
630
644
  leadsTo(name: string, spec: {
631
- from: (env: P & S & M & Su) => Cond;
632
- to: (env: P & S & M & Su) => Cond;
633
- rank?: (env: P & S & M & Su) => TermLike | TermLike[];
634
- helpful: ((env: P & S & M & Su) => Record<string, TermLike>) | {
635
- when?: (env: P & S & M & Su) => Cond;
636
- request: (env: P & S & M & Su) => Record<string, TermLike>;
645
+ from: (env: P & S & M & Su, key: Term) => Cond;
646
+ to: (env: P & S & M & Su, key: Term) => Cond;
647
+ rank?: (env: P & S & M & Su, key: Term) => TermLike | TermLike[];
648
+ helpful: ((env: P & S & M & Su, key: Term) => Record<string, TermLike>) | {
649
+ when?: (env: P & S & M & Su, key: Term) => Cond;
650
+ request: (env: P & S & M & Su, key: Term) => Record<string, TermLike>;
637
651
  }[];
638
652
  fairness?: 'weak' | 'strong';
639
653
  levels?: number[];
640
- }): Liveness;
654
+ }): KeyedLiveness;
641
655
  decide(name: string, given: (env: P & I) => Cond | Cond[], then: (env: P & I) => Cond): Decision;
642
656
  relate(name: string, given: (a: P & I, b: P & I) => Cond | Cond[], then: (a: P & I, b: P & I) => Cond): Decision;
643
657
  private kindAssumptions;
@@ -843,6 +857,7 @@ export type Verdict = {
843
857
  witnesses?: Record<string, string>;
844
858
  k?: number;
845
859
  assumes?: Assumes;
860
+ rank?: E | E[];
846
861
  };
847
862
  export type Assumes = {
848
863
  states: 'reachable';
@@ -878,9 +893,16 @@ export declare function proveRule(r: Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj
878
893
  export declare function proveSteps(r: Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj>, laws: Law[], stepLaws: StepLaw[], opts?: ProveOptions): Verdict;
879
894
  export type { StepLaw };
880
895
  export type { Liveness };
881
- export declare function proveLeadsTo(r: Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj>, laws: Law[], l: Liveness, opts?: {
896
+ export type KeyedLiveness = Liveness & {
897
+ forall?: string;
898
+ };
899
+ export declare const keyed: <R extends Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj>>(r: R, l: KeyedLiveness) => R;
900
+ export declare function proveLeadsTo(r: Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj>, laws: Law[], l: KeyedLiveness, opts?: {
882
901
  ms?: number;
883
902
  }): Verdict;
903
+ export declare function refuteLeadsTo(r: Rule<Obj, Obj, Obj, Obj, Obj, Obj, Obj, Obj>, laws: Law[], l: KeyedLiveness, opts?: {
904
+ ms?: number;
905
+ }): ReturnType<typeof findRing>;
884
906
  export declare const proveDecision: (d: Decision, opts?: {
885
907
  ms?: number;
886
908
  }) => Verdict;
package/dist/core/dsl.js CHANGED
@@ -27,7 +27,7 @@ import { findBoundedRun } from "./bmc.js";
27
27
  import { conjunction, proveKInduction } from "./kind.js";
28
28
  import { cycleLemmas, findLeadsTo, findRank } from "./cycles.js";
29
29
  import { findNeverEnds } from "./nonterm.js";
30
- import { leadsByTerms } from "./live.js";
30
+ import { findRing, leadsByTerms } from "./live.js";
31
31
  import { freshLaws } from "./fresh.js";
32
32
  // --- terms and conditions -------------------------------------------------------------------------------------------
33
33
  const naturalConst = (v) => {
@@ -832,6 +832,8 @@ export const everyKey = (f) => {
832
832
  const ok = (picked) => (picked.length === names.length ? g(...picked).ok : everyOk(scope?.keys, k => ok([...picked, keyTerm(k)])));
833
833
  return new Cond({ op: 'all', key: names[0], ...(names.length > 1 ? { keys: names.slice(1) } : {}), c: g(...names.map(v)).c }, ok([]));
834
834
  };
835
+ // A part of a least key's order compared the other way (the greatest first).
836
+ export const desc = (t) => ({ desc: t });
835
837
  // Some key where f holds, in a law or a step law's conclusion (¬∀k. ¬f(k)): proven through a witness, a term naming the
836
838
  // key, given here or found by the prover among a few candidates (kernel/prove.ts).
837
839
  export const someKey = (f, witness) => {
@@ -1866,6 +1868,16 @@ export class Rule {
1866
1868
  queries(queries) {
1867
1869
  return this.with({ queries: [...this.spec.queries, ...Object.entries(queries)] });
1868
1870
  }
1871
+ // Ordered collections, as a keyed map and the least key the step reads: `name`, a Maybe (none where no key meets
1872
+ // `where`), the key where `where` holds that is least by `by`, compared part by part (`desc(t)`: the greatest first).
1873
+ // The runtimes find it in the state before the step (the smaller key where all tie); the kernel knows it only by the
1874
+ // assumption it adds: where some, `where` holds there and it is at most every key `where` holds at, by `by`; where
1875
+ // none, `where` holds nowhere. A priority queue, a sorted map's first entry, a list's first item by insertion:
1876
+ // .least({best: ({Asks}) => ({where: k => Asks.at(k).has, by: k => [Asks.at(k).price, Asks.at(k).seq]})})
1877
+ // .step(({best, Asks}) => ({Asks: Asks.set(best.inner, ...)})) (best.isSome, best.inner)
1878
+ least(defs) {
1879
+ return this.with({ leasts: [...(this.spec.leasts ?? []), ...Object.entries(defs)] });
1880
+ }
1869
1881
  keep(f) { return this.with({ keep: f }); }
1870
1882
  relations(f) { return this.with({ relations: f }); }
1871
1883
  // Requests come in nondecreasing `order`; a record whose order plus `after` is at most the request's is dropped
@@ -1999,12 +2011,19 @@ export class Rule {
1999
2011
  // infinitely often (an event that keeps coming back is taken). Its regions need not stay; a helpful request may move to
2000
2012
  // the region of one of a higher `levels` (found where left out) instead of reaching `to` or lowering the rank:
2001
2013
  // job.leadsTo('every job ends', {..., fairness: 'strong', helpful: [{when: waiting, request: fetch}, {when: active, request: fail}]})
2014
+ // About every key: callbacks that take a second parameter, the key (one for every key, the same in every callback):
2015
+ // outbox.leadsTo('every row is published', {from: ({Rows}, k) => Rows.at(k).eq(1), to: ({Pub}, k) => Pub.at(k).eq(1), helpful: (_, k) => ({act: 1, row: k})})
2016
+ // The key is a fresh param no step reads (`forall`), which proveLeadsTo adds to the rule (`keyed`): a run of the rule
2017
+ // with it is a run of the rule without it, at every value of the key, so the one-machine argument at that param is
2018
+ // the law at every key, each key's helpful requests fair on their own (fairness per key).
2002
2019
  leadsTo(name, spec) {
2003
2020
  const env = this.env({ flags: false, inputs: false, queries: false });
2004
- const req = (f) => Object.fromEntries(Object.entries(f(env)).map(([n, t]) => [n, termOf(t)]));
2005
- const r = spec.rank ? spec.rank(env) : [], rank = Array.isArray(r) ? r.map(termOf) : termOf(r);
2006
- const base = { name, from: spec.from(env).c, to: spec.to(env).c, rank, ...(spec.fairness === 'strong' ? { fairness: 'strong' } : {}), ...(spec.levels ? { levels: spec.levels } : {}) };
2007
- return this.divides(typeof spec.helpful === 'function' ? { ...base, request: req(spec.helpful) } : { ...base, helpful: spec.helpful.map(h => ({ ...(h.when ? { when: h.when(env).c } : {}), request: req(h.request) })) });
2021
+ const fs = [spec.from, spec.to, spec.rank, ...(typeof spec.helpful === 'function' ? [spec.helpful] : spec.helpful.flatMap(h => [h.when, h.request]))];
2022
+ const forall = fs.some(f => f && f.length > 1) ? `key$${++keyCounter}` : undefined, key = (forall ? v(forall) : undefined);
2023
+ const req = (f) => Object.fromEntries(Object.entries(f(env, key)).map(([n, t]) => [n, termOf(t)]));
2024
+ const r = spec.rank ? spec.rank(env, key) : [], rank = Array.isArray(r) ? r.map(termOf) : termOf(r);
2025
+ const base = { name, from: spec.from(env, key).c, to: spec.to(env, key).c, rank, ...(spec.fairness === 'strong' ? { fairness: 'strong' } : {}), ...(spec.levels ? { levels: spec.levels } : {}), ...(forall ? { forall } : {}) };
2026
+ return this.divides(typeof spec.helpful === 'function' ? { ...base, request: req(spec.helpful) } : { ...base, helpful: spec.helpful.map(h => ({ ...(h.when ? { when: h.when(env, key).c } : {}), request: req(h.request) })) });
2008
2027
  }
2009
2028
  // A decision: whenever the params and the request meet `given`, `then` holds. Decisions don't read the state; the
2010
2029
  // functions' facts are assumed at the arguments the decision reads them at, and a carried list's equations (each
@@ -2094,6 +2113,8 @@ export class Rule {
2094
2113
  delete out[part];
2095
2114
  out[n] = l;
2096
2115
  }
2116
+ for (const [n] of this.spec.leasts ?? [])
2117
+ out[n] = maybe(nat).value(n);
2097
2118
  }
2098
2119
  if (parts.state !== false) {
2099
2120
  for (const [n] of this.spec.state)
@@ -2151,7 +2172,7 @@ export class Rule {
2151
2172
  names() {
2152
2173
  const s = this.spec;
2153
2174
  return [...s.params.map(([n]) => [n, 'a param']), ...(s.fns ?? []).map(([n]) => [n, 'a function']), ...s.inputs.map(([n]) => [n, 'an input']), ...(s.carried ?? []).map(n => [n, 'a carried map']),
2154
- ...s.state.map(([n]) => [n, 'a state variable']), ...s.maps.map(([n]) => [n, 'a map']), ...s.sums.map(([n]) => [n, 'a sum']), ...s.queries.map(([n]) => [n, 'a query']),
2175
+ ...s.state.map(([n]) => [n, 'a state variable']), ...s.maps.map(([n]) => [n, 'a map']), ...s.sums.map(([n]) => [n, 'a sum']), ...s.queries.map(([n]) => [n, 'a query']), ...(s.leasts ?? []).map(([n]) => [n, 'a least key']),
2155
2176
  ...[...(s.cells ?? []), ...(s.items ?? [])].filter(([n, k]) => !k.vars(n).includes(n)).map(([n]) => [n, 'a map of a kind']),
2156
2177
  ...(s.nodes ?? []).map(([n]) => [n, 'a table of nodes']),
2157
2178
  ];
@@ -2226,7 +2247,19 @@ export class Rule {
2226
2247
  params.push(...pre0.vars);
2227
2248
  inputs.push(...as0.vars);
2228
2249
  const pre = [...this.kindAssumptions('params'), ...pre0.cs];
2229
- const assume = [...this.kindAssumptions('inputs'), ...(s.lists2 ?? []).flatMap(([n]) => env[n].equations().map(c => hoist.cond(c.c))), ...as0.cs.map(c => hoist.cond(c))];
2250
+ // The least keys: inputs the runtimes find, assumed least.
2251
+ const leasts = {}, leastAssume = [];
2252
+ for (const [n, f] of s.leasts ?? []) {
2253
+ const d = f(env), key = `k$${++boundCounter}`, best = env[n];
2254
+ const parts = (k) => d.by(k).map(b => (typeof b === 'object' && !(b instanceof Term) && !(b instanceof Int) && 'desc' in b ? { t: Int.of(b.desc), desc: true } : { t: Int.of(b), desc: false }));
2255
+ const zero = (e) => e.op === 'const' && e.value === 0;
2256
+ leasts[n] = { some: `${n}$some`, key, where: d.where(v(key)).c, by: parts(v(key)).map(b => ({ e: b.t.pos.e, ...(zero(b.t.neg.e) ? {} : { neg: b.t.neg.e }), ...(b.desc ? { desc: true } : {}) })) };
2257
+ inputs.push(`${n}$some`, n);
2258
+ const atMost = (a, b) => (a.length === 0 ? yes
2259
+ : ((x, y) => (a.length === 1 ? x.atMost(y) : x.lt(y).or(x.eq(y).and(atMost(a.slice(1), b.slice(1))))))(a[0].desc ? b[0].t : a[0].t, a[0].desc ? a[0].t : b[0].t));
2260
+ leastAssume.push(best.present.atMost(1), best.isSome.implies(d.where(best.inner)), best.isNone.implies(everyKey(k => d.where(k).not())), best.isSome.implies(everyKey(k => d.where(k).implies(atMost(parts(best.inner), parts(k))))));
2261
+ }
2262
+ const assume = [...this.kindAssumptions('inputs'), ...leastAssume.map(c => hoist.cond(c.c)), ...(s.lists2 ?? []).flatMap(([n]) => env[n].equations().map(c => hoist.cond(c.c))), ...as0.cs.map(c => hoist.cond(c))];
2230
2263
  placed();
2231
2264
  // Functions, and their facts: each function's own (named after it), then the rule's.
2232
2265
  // The functions a function reads come first, each named <function>.<use>.
@@ -2246,6 +2279,7 @@ export class Rule {
2246
2279
  ...(assume.length ? { assume: assume.length === 1 ? assume[0] : { op: 'and', xs: assume } } : {}),
2247
2280
  ...(Object.keys(fns).length ? { fns } : {}),
2248
2281
  ...(facts.length ? { facts } : {}),
2282
+ ...(Object.keys(leasts).length ? { leasts } : {}),
2249
2283
  };
2250
2284
  if (s.record) {
2251
2285
  const kinds = new Map(s.inputs);
@@ -3028,8 +3062,10 @@ function searchedUntil(r, laws, stepLaws, v, opts, until) {
3028
3062
  // What a verdict proven with these found laws rests on: the states reachable from init (each found law is proven so).
3029
3063
  const reachableBy = (found) => ({ states: 'reachable', laws: found.map(f => f.text) });
3030
3064
  const withAssumes = (found) => (found.length ? { assumes: reachableBy(found) } : {});
3065
+ // The rule a keyed liveness is proven on: the rule with its key as one more param (a natural), any other rule as it is.
3066
+ export const keyed = (r, l) => (l.forall ? r.params({ [l.forall]: nat }) : r);
3031
3067
  export function proveLeadsTo(r, laws, l, opts = {}) {
3032
- r = r.withSumsOf(laws, l);
3068
+ r = keyed(r, l).withSumsOf(laws, l);
3033
3069
  noting(r, laws, l);
3034
3070
  if (laws.some(law => law.some?.length))
3035
3071
  throw new Error(`${r.machine.name}: a law with "there exists" goes to proveRule or proveSteps; a liveness takes plain laws (the ones its proof printed, Verdict.witnesses)`);
@@ -3061,12 +3097,19 @@ export function proveLeadsTo(r, laws, l, opts = {}) {
3061
3097
  }
3062
3098
  }
3063
3099
  const helpers = [...fresh, ...(f.laws ?? []).map((law, i) => ({ law, text: f.found?.laws[i] ?? law.name }))];
3064
- return { proven: f.proven, checked: f.checked, ...(f.goal ? { goal: f.goal, stuck: f.stuck } : {}), ...(f.witness ? { witness: f.witness } : {}), ...(f.why ? { why: f.why } : {}),
3100
+ return { proven: f.proven, checked: f.checked, ...(f.proven && f.rank ? { rank: f.rank } : {}), ...(f.goal ? { goal: f.goal, stuck: f.stuck } : {}), ...(f.witness ? { witness: f.witness } : {}), ...(f.why ? { why: f.why } : {}),
3065
3101
  ...(f.found ? { found: { rank: f.found.rank, ...(helpers.length ? { laws: helpers } : {}) } } : {}), ...(f.proven ? withAssumes(helpers) : {}) };
3066
3102
  }
3067
3103
  const out = proveLiveness(r.machine, withKinds(r, laws), l);
3068
3104
  return verdictOf(out);
3069
3105
  }
3106
+ // A leads-to refuted by a fair run that never gets there (core/live.ts's findRing, checked by refutedBy): a stem from
3107
+ // the start, then round a ring of sets each request of which the kernel proves allowed and into the next set, fair to
3108
+ // each helpful request (taken in the ring, or never allowed in a set). Undefined where none is found within `ms`.
3109
+ export function refuteLeadsTo(r, laws, l, opts = {}) {
3110
+ const k = keyed(r, l);
3111
+ return findRing(k.machine, withKinds(k, laws), l, { params: (rand, bound) => k.randomParams(rand, bound), input: (rand, bound, ps) => k.randomInput(rand, bound, ps) }, opts);
3112
+ }
3070
3113
  // `ms`: the budget (the prover's own per goal, 20 s or a second a hypothesis, where none is given), on the clock the
3071
3114
  // prover reads (the counted one inside a counted search): past it the verdict is unknown.
3072
3115
  export const proveDecision = (d, opts = {}) => {
@@ -289,10 +289,10 @@ export function ruleSnap(r, m, st, sorts) {
289
289
  leaves[v.name] = d[v.name];
290
290
  for (const md of m.maps) {
291
291
  const cells = {};
292
- for (const [k, c] of Object.entries((d[md.name] ?? {}))) {
293
- if (!c.has)
294
- continue;
295
- const key = md.key === 'str' ? idText(Number(k)) ?? `#${k}` : k;
292
+ // (a list: its items by their places in its order, 0 first, as the code's list holds them)
293
+ const held = Object.entries((d[md.name] ?? {})).filter(([, c]) => c.has).sort(([a], [b]) => Number(a) - Number(b));
294
+ for (const [i, [k, c]] of held.entries()) {
295
+ const key = md.ordered ? String(i) : md.key === 'str' ? idText(Number(k)) ?? `#${k}` : k;
296
296
  cells[key] = Object.fromEntries(md.fields.map(f => [f.name, c[f.name]]));
297
297
  }
298
298
  maps[md.name] = cells;
@@ -13,6 +13,7 @@ export declare function sortsOf(m: Model, given?: Record<string, 'nat' | 'int'>)
13
13
  sorts: Sorts;
14
14
  notes: string[];
15
15
  };
16
+ export declare const leastName: (m: Model, n: string) => string;
16
17
  export type Printer = {
17
18
  sorts: Sorts;
18
19
  m: Model;
@@ -229,6 +229,11 @@ export function sortsOf(m, given = {}) {
229
229
  const cellNums = m.maps.flatMap(d => d.fields.filter(f => f.sort.k === 'num').map(f => `${d.name}.${f.name}`));
230
230
  for (const v of nums(m.state))
231
231
  sorts.set(v.name, given[v.name] ?? 'nat');
232
+ // (a least key, and the key its order is read at: naturals, a map's keys)
233
+ for (const l of m.leasts ?? []) {
234
+ sorts.set(l.name, 'nat');
235
+ sorts.set(l.key, 'nat');
236
+ }
232
237
  for (const c of cellNums)
233
238
  sorts.set(c, given[c] ?? 'nat');
234
239
  // A list's length is a natural; its items naturals until one written may be below 0.
@@ -289,6 +294,12 @@ function refusedBelowZero(m, x, ctx) {
289
294
  return refutes([...linCond(I.not(own[0]), ctx), ...lt(lv(x), lk(0))]);
290
295
  }));
291
296
  }
297
+ // --- printing -------------------------------------------------------------------------------------------------------
298
+ // A least key's names as the printed rule reads them: the key `<name>.inner`, whether there is one `<name>.isSome`.
299
+ export const leastName = (m, n) => {
300
+ const l = m.leasts?.find(x => x.name === n || x.some === n);
301
+ return !l ? n : l.name === n ? `${n}.inner` : `${l.name}.isSome`;
302
+ };
292
303
  const q = (s) => `'${s.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`;
293
304
  function pn(e, P, ctx, mode, recv = false) {
294
305
  const h = P.hoisted.get(I.keyOf(e));
@@ -297,7 +308,10 @@ function pn(e, P, ctx, mode, recv = false) {
297
308
  const nat = mode === 'nat';
298
309
  const R = (x, c = ctx) => pn(x, P, c, nat ? 'nat' : 'int', true), A = (x, c = ctx) => pn(x, P, c, nat || natOK(x, c) ? 'nat' : 'int');
299
310
  switch (e.op) {
300
- case 'var': return !nat && recv && P.sorts.get(e.name) === 'nat' ? `Int.of(${e.name})` : e.name;
311
+ case 'var': {
312
+ const n = leastName(P.m, e.name);
313
+ return !nat && recv && P.sorts.get(e.name) === 'nat' ? `Int.of(${n})` : n;
314
+ }
301
315
  case 'const': return recv ? (nat ? `lit(${e.v})` : `Int.of(${e.v})`) : String(e.v);
302
316
  case 'add': return e.a.op === 'const' ? `${R(e.b)}.plus(${A(e.a)})` : `${R(e.a)}.plus(${A(e.b)})`;
303
317
  case 'sub': return `${R(e.a)}.minus(${A(e.b)})`;
@@ -354,7 +368,7 @@ export function pb(c, P, ctx) {
354
368
  return h;
355
369
  switch (c.op) {
356
370
  case 'const': return c.v ? 'yes' : 'no';
357
- case 'var': return c.name;
371
+ case 'var': return leastName(P.m, c.name);
358
372
  case 'cmp': return pcmp(c.rel, c.a, c.b, P, ctx);
359
373
  case 'and': return `all(${c.xs.map(x => pb(x, P, ctx)).join(', ')})`;
360
374
  case 'or': return `any(${c.xs.map(x => pb(x, P, ctx)).join(', ')})`;
@@ -47,6 +47,18 @@ export type MapDecl = {
47
47
  js: 'Map' | 'record' | 'Set';
48
48
  untyped?: boolean;
49
49
  len?: number;
50
+ ordered?: true;
51
+ };
52
+ export type LeastRead = {
53
+ name: string;
54
+ some: string;
55
+ map: string;
56
+ key: string;
57
+ where: B;
58
+ by: {
59
+ e: N;
60
+ desc?: boolean;
61
+ }[];
50
62
  };
51
63
  export type Layout = {
52
64
  k: 'scalar';
@@ -181,6 +193,7 @@ export type Model = {
181
193
  fnFields?: string[];
182
194
  imports?: string[];
183
195
  kind?: string;
196
+ leasts?: LeastRead[];
184
197
  };
185
198
  type V = {
186
199
  t: 'num';
@@ -1,6 +1,6 @@
1
1
  import * as I from './extract-ir.js';
2
2
  import type { B, K, L, N } from './extract-ir.js';
3
- import { type MapDecl, type Model, Refusal, type Term, type Var } from './extract-read.js';
3
+ import { type LeastRead, type MapDecl, type Model, Refusal, type Term, type Var } from './extract-read.js';
4
4
  import { type Extraction, type Name } from './extract.js';
5
5
  import type { Rule } from './dsl.js';
6
6
  import { type Sorts } from './extract-print.js';
@@ -374,10 +374,12 @@ export type Leaf = {
374
374
  fields?: {
375
375
  name: string;
376
376
  float?: boolean;
377
+ choice?: boolean;
377
378
  }[];
378
379
  key?: 'num' | 'str';
379
380
  store?: string;
380
381
  tpl?: string;
382
+ ordered?: boolean;
381
383
  };
382
384
  export type PArg = {
383
385
  k: 'in';
@@ -487,6 +489,7 @@ export declare class Stepper {
487
489
  id: string;
488
490
  decl: MapDecl;
489
491
  }>;
492
+ readonly leasts: LeastRead[];
490
493
  frames: string[];
491
494
  fieldNames: Set<string>;
492
495
  readonly frozen: Set<string>;
@@ -580,6 +583,46 @@ export declare class Stepper {
580
583
  mapDeclOf(name: string, t: SType, at: {
581
584
  p: string;
582
585
  }): MapDecl;
586
+ literalsOf(r: SRec, field: string): string[] | undefined;
587
+ least(o: Extract<Obj, {
588
+ k: 'map';
589
+ }>, at: SX, where: (k: K) => B, by: (k: K) => {
590
+ e: N;
591
+ desc?: boolean;
592
+ }[]): {
593
+ key: N;
594
+ some: B;
595
+ held: B;
596
+ where: (k: K) => B;
597
+ };
598
+ holds(p: Path, c: B): boolean;
599
+ orderedObj(m: V, p: Path): [string, Extract<Obj, {
600
+ k: 'map';
601
+ }>] | undefined;
602
+ item(id: string, o: Extract<Obj, {
603
+ k: 'map';
604
+ }>, at: SX, last: boolean, by?: (k: K) => {
605
+ e: N;
606
+ desc?: boolean;
607
+ }[]): {
608
+ key: K;
609
+ some: B;
610
+ held: B;
611
+ v: V;
612
+ };
613
+ listCall(id: string, o0: Extract<Obj, {
614
+ k: 'map';
615
+ }>, name: string, x: SX, p: Path): [V, Path];
616
+ listDel(id: string, o: Extract<Obj, {
617
+ k: 'map';
618
+ }>, key: K, at: SX, p: Path): Path;
619
+ recOf(d: MapDecl, at: SX): SRec;
620
+ fieldOf(v: V, f: string, at: SX, p: Path): V;
621
+ minMax(n: 'min' | 'max', x: SX, p: Path): [V, Path] | undefined;
622
+ emptyOr(n: string, v: V, l: {
623
+ some: B;
624
+ held: B;
625
+ }, d: V | undefined, x: SX, p: Path): [V, Path];
583
626
  storeMap(store: string, tpl: string, kn: boolean, at: SX, p: Path): [string, Extract<Obj, {
584
627
  k: 'map';
585
628
  }>];
@@ -648,7 +691,7 @@ export declare class Stepper {
648
691
  dictCall(m: V, name: string, x: SX, p: Path): [V, Path];
649
692
  construct(rec: string, args: V[], kw: [string, V][], x: SX, p: Path): [V, Path];
650
693
  builtin(n: string, x: SX, p: Path): [V, Path];
651
- quotient(d: SX, p: Path, round: 'floor' | 'ceil' | 'trunc'): [V, Path];
694
+ quotient(d: SX, p: Path, round: 'floor' | 'ceil' | 'trunc' | 'even'): [V, Path];
652
695
  lib(q0: string, x: SX, p: Path): [V, Path];
653
696
  readonly edges: B[];
654
697
  readonly lists: MapDecl[];