gdp-wired 0.46.0 → 0.48.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.
Files changed (182) hide show
  1. package/README.md +18 -5
  2. package/dist/core/carry.d.ts +15 -0
  3. package/dist/core/carry.js +651 -0
  4. package/dist/core/cli.d.ts +4 -1
  5. package/dist/core/cli.js +31 -11
  6. package/dist/core/count-cases.d.ts +24 -0
  7. package/dist/core/count-cases.js +320 -0
  8. package/dist/core/count-cert-grow.d.ts +4 -0
  9. package/dist/core/count-cert-grow.js +37 -0
  10. package/dist/core/count-cert-transport.d.ts +3 -0
  11. package/dist/core/count-cert-transport.js +58 -0
  12. package/dist/core/count-completion.d.ts +9 -0
  13. package/dist/core/count-completion.js +27 -0
  14. package/dist/core/count-cubes.d.ts +8 -0
  15. package/dist/core/count-cubes.js +69 -0
  16. package/dist/core/count-demand.d.ts +37 -0
  17. package/dist/core/count-demand.js +361 -0
  18. package/dist/core/count-facts.d.ts +52 -0
  19. package/dist/core/count-facts.js +247 -0
  20. package/dist/core/count-focus-proof.d.ts +8 -0
  21. package/dist/core/count-focus-proof.js +140 -0
  22. package/dist/core/count-focused.d.ts +22 -0
  23. package/dist/core/count-focused.js +676 -0
  24. package/dist/core/count-ranges.d.ts +5 -0
  25. package/dist/core/count-ranges.js +65 -0
  26. package/dist/core/count-recovery/action-guard.d.ts +5 -0
  27. package/dist/core/count-recovery/action-guard.js +51 -0
  28. package/dist/core/count-recovery/boolean-core.d.ts +16 -0
  29. package/dist/core/count-recovery/boolean-core.js +258 -0
  30. package/dist/core/count-recovery/carried-case.d.ts +2 -0
  31. package/dist/core/count-recovery/carried-case.js +24 -0
  32. package/dist/core/count-recovery/conditional-check-cache.d.ts +23 -0
  33. package/dist/core/count-recovery/conditional-check-cache.js +130 -0
  34. package/dist/core/count-recovery/conditional-cuts.d.ts +6 -0
  35. package/dist/core/count-recovery/conditional-cuts.js +318 -0
  36. package/dist/core/count-recovery/conditional-proof.d.ts +19 -0
  37. package/dist/core/count-recovery/conditional-proof.js +306 -0
  38. package/dist/core/count-recovery/derivability-closure.d.ts +14 -0
  39. package/dist/core/count-recovery/derivability-closure.js +533 -0
  40. package/dist/core/count-recovery/empty-count-source.d.ts +2 -0
  41. package/dist/core/count-recovery/empty-count-source.js +40 -0
  42. package/dist/core/count-recovery/field-bound-lemmas.d.ts +23 -0
  43. package/dist/core/count-recovery/field-bound-lemmas.js +640 -0
  44. package/dist/core/count-recovery/field-case.d.ts +5 -0
  45. package/dist/core/count-recovery/field-case.js +89 -0
  46. package/dist/core/count-recovery/field-demand.d.ts +46 -0
  47. package/dist/core/count-recovery/field-demand.js +47 -0
  48. package/dist/core/count-recovery/field-only.d.ts +3 -0
  49. package/dist/core/count-recovery/field-only.js +18 -0
  50. package/dist/core/count-recovery/fold-path.d.ts +6 -0
  51. package/dist/core/count-recovery/fold-path.js +35 -0
  52. package/dist/core/count-recovery/frame-lemmas.d.ts +9 -0
  53. package/dist/core/count-recovery/frame-lemmas.js +120 -0
  54. package/dist/core/count-recovery/guard-cases.d.ts +12 -0
  55. package/dist/core/count-recovery/guard-cases.js +138 -0
  56. package/dist/core/count-recovery/key-closure.d.ts +14 -0
  57. package/dist/core/count-recovery/key-closure.js +62 -0
  58. package/dist/core/count-recovery/leaf-allowance.d.ts +4 -0
  59. package/dist/core/count-recovery/leaf-allowance.js +7 -0
  60. package/dist/core/count-recovery/lift-count-proxies.d.ts +6 -0
  61. package/dist/core/count-recovery/lift-count-proxies.js +175 -0
  62. package/dist/core/count-recovery/live-dependencies.d.ts +6 -0
  63. package/dist/core/count-recovery/live-dependencies.js +76 -0
  64. package/dist/core/count-recovery/member-inclusions.d.ts +11 -0
  65. package/dist/core/count-recovery/member-inclusions.js +168 -0
  66. package/dist/core/count-recovery/member-premises.d.ts +29 -0
  67. package/dist/core/count-recovery/member-premises.js +114 -0
  68. package/dist/core/count-recovery/natural-cases.d.ts +6 -0
  69. package/dist/core/count-recovery/natural-cases.js +59 -0
  70. package/dist/core/count-recovery/ops.d.ts +6 -0
  71. package/dist/core/count-recovery/ops.js +8 -0
  72. package/dist/core/count-recovery/pre-state-source.d.ts +8 -0
  73. package/dist/core/count-recovery/pre-state-source.js +18 -0
  74. package/dist/core/count-recovery/rebase-path.d.ts +9 -0
  75. package/dist/core/count-recovery/rebase-path.js +70 -0
  76. package/dist/core/count-recovery/rename-proof.d.ts +2 -0
  77. package/dist/core/count-recovery/rename-proof.js +102 -0
  78. package/dist/core/count-recovery/row-cases.d.ts +16 -0
  79. package/dist/core/count-recovery/row-cases.js +291 -0
  80. package/dist/core/count-recovery/scalar-guards.d.ts +7 -0
  81. package/dist/core/count-recovery/scalar-guards.js +291 -0
  82. package/dist/core/count-recovery/source-memo.d.ts +12 -0
  83. package/dist/core/count-recovery/source-memo.js +67 -0
  84. package/dist/core/count-select.d.ts +16 -0
  85. package/dist/core/count-select.js +24 -0
  86. package/dist/core/count-support.d.ts +598 -0
  87. package/dist/core/count-support.js +2080 -0
  88. package/dist/core/count-unions.d.ts +2 -0
  89. package/dist/core/count-unions.js +139 -0
  90. package/dist/core/count-witness-flags.d.ts +9 -0
  91. package/dist/core/count-witness-flags.js +37 -0
  92. package/dist/core/count-write-frames.d.ts +12 -0
  93. package/dist/core/count-write-frames.js +206 -0
  94. package/dist/core/counts.d.ts +20 -2
  95. package/dist/core/counts.js +807 -156
  96. package/dist/core/dsl.d.ts +50 -6
  97. package/dist/core/dsl.js +307 -65
  98. package/dist/core/effect-schema.d.ts +90 -0
  99. package/dist/core/effect-schema.js +500 -0
  100. package/dist/core/effect.d.ts +3 -7
  101. package/dist/core/effect.js +10 -65
  102. package/dist/core/exists.d.ts +1 -1
  103. package/dist/core/exists.js +80 -15
  104. package/dist/core/extract-check.d.ts +7 -1
  105. package/dist/core/extract-check.js +161 -19
  106. package/dist/core/extract-go-check.js +24 -4
  107. package/dist/core/extract-go.d.ts +7 -0
  108. package/dist/core/extract-go.js +29 -2
  109. package/dist/core/extract-read.d.ts +31 -9
  110. package/dist/core/extract-read.js +36 -11
  111. package/dist/core/extract-rs.js +58 -14
  112. package/dist/core/extract-shapes.d.ts +1 -0
  113. package/dist/core/extract-shapes.js +190 -27
  114. package/dist/core/extract-standalone.d.ts +3 -0
  115. package/dist/core/extract-standalone.js +265 -0
  116. package/dist/core/extract.d.ts +1 -0
  117. package/dist/core/extract.js +25 -5
  118. package/dist/core/field-laws.d.ts +2 -0
  119. package/dist/core/field-laws.js +100 -0
  120. package/dist/core/finite-frontiers.d.ts +5 -0
  121. package/dist/core/finite-frontiers.js +116 -0
  122. package/dist/core/finite-houdini.d.ts +52 -0
  123. package/dist/core/finite-houdini.js +460 -0
  124. package/dist/core/finite-model.d.ts +13 -0
  125. package/dist/core/finite-model.js +68 -0
  126. package/dist/core/finite-tiny.d.ts +72 -0
  127. package/dist/core/finite-tiny.js +1052 -0
  128. package/dist/core/infer.d.ts +1 -1
  129. package/dist/core/infer.js +32 -9
  130. package/dist/core/laws.d.ts +2 -1
  131. package/dist/core/laws.js +76 -19
  132. package/dist/core/lemma-cuts.d.ts +38 -0
  133. package/dist/core/lemma-cuts.js +365 -0
  134. package/dist/core/machine.d.ts +39 -2
  135. package/dist/core/machine.js +240 -35
  136. package/dist/core/mutate.d.ts +55 -0
  137. package/dist/core/mutate.js +210 -0
  138. package/dist/core/native-domain.d.ts +6 -0
  139. package/dist/core/native-domain.js +114 -0
  140. package/dist/core/native-law.d.ts +3 -0
  141. package/dist/core/native-law.js +174 -0
  142. package/dist/core/quorum-laws.d.ts +5 -0
  143. package/dist/core/quorum-laws.js +239 -0
  144. package/dist/core/relational.js +10 -2
  145. package/dist/core/relevance.d.ts +4 -0
  146. package/dist/core/relevance.js +79 -0
  147. package/dist/core/rs/Cargo.toml +1 -1
  148. package/dist/core/rs/src/main.rs +458 -9
  149. package/dist/core/runtime-wall.d.ts +3 -0
  150. package/dist/core/runtime-wall.js +4 -0
  151. package/dist/core/sample-step.d.ts +16 -0
  152. package/dist/core/sample-step.js +28 -0
  153. package/dist/core/source-check.d.ts +44 -0
  154. package/dist/core/source-check.js +126 -0
  155. package/dist/core/source-diagnostic.d.ts +1 -0
  156. package/dist/core/source-diagnostic.js +34 -0
  157. package/dist/core/support-budget.d.ts +2 -0
  158. package/dist/core/support-budget.js +6 -0
  159. package/dist/core/system.js +2 -0
  160. package/dist/core/wall.d.ts +6 -0
  161. package/dist/core/wall.js +23 -0
  162. package/dist/core/watch.js +4 -0
  163. package/dist/kernel/cdcl.d.ts +4 -0
  164. package/dist/kernel/cdcl.js +20 -5
  165. package/dist/kernel/kernel.d.ts +10 -0
  166. package/dist/kernel/kernel.js +56 -3
  167. package/dist/kernel/model-sequence.d.ts +2 -2
  168. package/dist/kernel/model-sequence.js +4 -3
  169. package/dist/kernel/model-values.d.ts +2 -0
  170. package/dist/kernel/model-values.js +60 -0
  171. package/dist/kernel/prove.d.ts +10 -0
  172. package/dist/kernel/prove.js +56 -8
  173. package/dist/kernel/prover-rows.d.ts +29 -0
  174. package/dist/kernel/prover-rows.js +114 -0
  175. package/dist/lean-kernel/emit.d.ts +39 -2
  176. package/dist/lean-kernel/emit.js +136 -24
  177. package/dist/lean-kernel/lean-terms.d.ts +1 -0
  178. package/dist/lean-kernel/lean-terms.js +13 -5
  179. package/dist/lean-kernel/library.js +4 -0
  180. package/dist/lean-kernel/qalpha.d.ts +7 -0
  181. package/dist/lean-kernel/qalpha.js +40 -0
  182. package/package.json +5 -1
@@ -38,11 +38,12 @@
38
38
  // across a set, one object (an array, a Map, a Date) in two places, aliasing that depends on the path, mixins of other
39
39
  // modules, other modules' code.
40
40
  import * as dsl from "./dsl.js";
41
+ import { attachSource } from "./source-check.js";
41
42
  import { compileFunction, walk } from "./machine.js";
42
43
  import * as I from "./extract-ir.js";
43
44
  import { PASS } from "./extract-ir.js";
44
45
  import { Refusal, placed, sourceOf, syntaxError, tooDeep } from "./extract-read.js";
45
- import { isFactory, isSlice, readClass, readFactory, readFunctions, readSlice } from "./extract-shapes.js";
46
+ import { isFactory, isSlice, readClass, readFactory, readFunctions, readSlice, readStandalone } from "./extract-shapes.js";
46
47
  import { kindOf, mapEmpty, mapKind, natOK, pb, pidx, pk, pnum, pterm, sortsOf } from "./extract-print.js";
47
48
  import { check } from "./extract-check.js";
48
49
  import { extractOrm } from "./extract-orm.js";
@@ -72,9 +73,11 @@ export function namesOf(m) {
72
73
  const arg = (a, at, method) => {
73
74
  if (a.k === 'input')
74
75
  out.push({ code: at, rule: a.name, role: 'input', method });
76
+ else if (a.k === 'maybe-bool')
77
+ out.push({ code: at, rule: a.name, none: a.missing, role: 'input', method });
75
78
  else if (a.k === 'rec')
76
79
  for (const [f, x] of a.fields)
77
- arg(x, `${at}.${f}`, method);
80
+ arg(x, m.standalone && at === '__gdp_destructured' ? f : `${at}.${f}`, method);
78
81
  };
79
82
  for (const e of m.events)
80
83
  e.args.forEach((a, i) => { const n = e.names?.[i]; if (n)
@@ -85,8 +88,12 @@ export function namesOf(m) {
85
88
  else if (r.k === 'maybe')
86
89
  out.push({ code: 'result', rule: r.name, role: 'result', method, none: r.missing });
87
90
  else
88
- for (const [f, n] of r.fields)
89
- out.push(f === 'failed' ? { code: 'failed', rule: n, role: 'failed' } : { code: f === 'value' ? 'result' : `result.${f}`, rule: n, role: 'result', method });
91
+ for (const [f, n] of r.fields) {
92
+ out.push({ code: `result.${f}`, rule: n, role: 'result', method });
93
+ // Preserve legacy class/Effect envelope aliases; standalone records expose only source field names.
94
+ if (!m.standalone && (f === 'value' || f === 'failed'))
95
+ out.push(f === 'failed' ? { code: 'failed', rule: n, role: 'failed' } : { code: 'result', rule: n, role: 'result', method });
96
+ }
90
97
  }
91
98
  if (m.event)
92
99
  out.push({ code: 'method', rule: m.event, role: 'method' });
@@ -98,17 +105,27 @@ export function extract(at, opts = {}) {
98
105
  try {
99
106
  ;
100
107
  ({ code, file } = sourceOf(at, opts.file, 'input.ts'));
108
+ if (opts.mode !== undefined && opts.mode !== 'standalone')
109
+ throw new Refusal(file, `unsupported extraction mode: ${String(opts.mode)}`);
101
110
  const bad = syntaxError(code, file);
102
111
  if (bad)
103
112
  throw new Refusal(file, bad);
104
113
  if (opts.orm) {
114
+ if (opts.mode === 'standalone')
115
+ throw new Refusal(file, 'standalone mode cannot use ORM');
105
116
  if (typeof at !== 'string' || at.includes('\n') || !/\.[cm]?[jt]sx?$/.test(at))
106
117
  throw new Refusal(file, 'ORM checking requires the original module on disk');
107
118
  return extractOrm(file, code, { ...opts, orm: opts.orm });
108
119
  }
109
120
  const fns = Array.isArray(opts.entry) ? opts.entry : opts.functions;
110
121
  const o = { ...opts, entry: (Array.isArray(opts.entry) ? opts.entry[0] : opts.entry) ?? fns?.[0] ?? '', ...(fns ? { functions: fns } : {}) };
111
- model = o.entry ? readModel(code, file, o, opts.shape) : inferred(code, file, o, opts.shape);
122
+ if (opts.mode === 'standalone') {
123
+ if (!o.entry || Array.isArray(opts.entry) || opts.functions || opts.shape || opts.orm)
124
+ throw new Refusal(file, 'standalone mode needs exactly one named function and no other shape');
125
+ model = readStandalone(code, placed(file), o);
126
+ }
127
+ else
128
+ model = o.entry ? readModel(code, file, o, opts.shape) : inferred(code, file, o, opts.shape);
112
129
  model.file = file;
113
130
  }
114
131
  catch (e) {
@@ -118,6 +135,8 @@ export function extract(at, opts = {}) {
118
135
  return { ok: false, refusals: [{ where: opts.file ?? (typeof at === 'string' && !at.includes('\n') ? at : 'input.ts'), what: 'the source nests too deeply to read' }], notes };
119
136
  throw e;
120
137
  }
138
+ if (model.standalone && Object.entries(model.standaloneDomains ?? {}).some(([n, xs]) => opts.domains?.[n] === 'nat' && xs.some(x => x < 0)))
139
+ return { ok: false, refusals: [{ where: file, what: 'standalone: a natural domain contradicts the declared negative numeric literals' }], notes, model };
121
140
  return { ...fromModel(model, file, opts.domains, notes, (rule, sorts) => check(code, file, model, rule, sorts, { ...opts.check, ...(opts.files ? { files: opts.files } : {}), ...(opts.domains ? { given: opts.domains } : {}) })), names: namesOf(model) };
122
141
  }
123
142
  function readModel(code, file, opts, shape) {
@@ -180,6 +199,7 @@ export function fromModel(model, file, domains, notes, run) {
180
199
  return { ok: false, source, model, sorts, evidence, refusals: [{ where: file, what: `${HARNESS_FAILED}${evidence.harness}` }], notes };
181
200
  if (evidence.mismatch)
182
201
  return { ok: false, source, model, sorts, evidence, refusals: [{ where: file, what: `the rule and the code differ: ${evidence.mismatch}` }], notes };
202
+ attachSource(rule, evidence, invariants);
183
203
  return { ok: true, rule, ...(invariants.length ? { invariants } : {}), source, model, sorts, evidence, refusals: [], notes };
184
204
  }
185
205
  // The check's own failure (twice): no verdict on the code, neither a difference nor a pass.
@@ -0,0 +1,2 @@
1
+ import { type Law, type Machine } from './machine.js';
2
+ export declare function fieldLaws(m: Machine, composed?: boolean): Law[];
@@ -0,0 +1,100 @@
1
+ // Untrusted candidates for maps of numeric fields. Boolean cubes lose comparisons
2
+ // between fields (and between two records), even when the transition reads them.
3
+ // Propose their shapes, not facts: counts.ts filters on reachable samples, and the
4
+ // ordinary counted induction/checker must prove every surviving candidate.
5
+ import { all, any, atMost, get, less, n, not, same, times, plus, tuple, walk, x } from "./machine.js";
6
+ export function fieldLaws(m, composed = true) {
7
+ const fields = new Set(), roles = new Map(), bounds = new Map();
8
+ const maps = m.maps ?? {}, source = [m.assume, m.step.flags];
9
+ const direct = (e) => e.op === 'get' && e.key.op === 'var' && e.map in maps;
10
+ const cells = (e) => {
11
+ const out = new Set();
12
+ walk(e, o => { const t = o; if (direct(t))
13
+ out.add(t.map); });
14
+ return [...out];
15
+ };
16
+ walk(source, o => {
17
+ if (o.op !== '=' && o.op !== '<' && o.op !== '<=')
18
+ return;
19
+ const c = o, a = cells(c.a), b = cells(c.b);
20
+ if (a.length && b.length)
21
+ for (const name of [...a, ...b])
22
+ fields.add(name);
23
+ if (c.op === '=' && direct(c.a) && c.a.key.op === 'var' && m.inputs.includes(c.a.key.name) && c.b.op === 'const')
24
+ roles.set(`${c.a.map}:${c.b.value}`, { map: c.a.map, value: c.b.value });
25
+ if (c.op === '<' && c.a.op === 'var' && m.inputs.includes(c.a.name) && c.b.op === 'var' && m.params.includes(c.b.name))
26
+ bounds.set(JSON.stringify(c.b), c.b);
27
+ });
28
+ // Different key domains need a sort analysis. Do not guess a shared domain.
29
+ if (fields.size < 2 || bounds.size !== 1)
30
+ return [];
31
+ const bound = [...bounds.values()][0], fs = [...fields].sort(), rs = [...roles.values()].filter(r => !fields.has(r.map));
32
+ const names = new Set([...m.params, ...m.inputs, ...Object.keys(m.state)]);
33
+ walk(m, o => { if (o.op === 'var')
34
+ names.add(o.name); });
35
+ const fresh = (base) => { let s = base; while (names.has(s))
36
+ s += '$'; names.add(s); return s; };
37
+ const ka = fresh('a$field'), kb = fresh('b$field'), kc = fresh('c$field'), a = x(ka), b = x(kb), c = x(kc), da = less(a, bound), db = less(b, bound);
38
+ const out = [], seen = new Set();
39
+ const propose = (name, guards, holds, pair = false) => {
40
+ const body = any(not(all(da, ...(pair ? [db] : []), ...guards)), holds), id = JSON.stringify(body);
41
+ if (seen.has(id))
42
+ return;
43
+ seen.add(id);
44
+ out.push({ name: `fields: ${name}`, forall: ka, ...(pair ? { keys: [kb] } : {}), holds: body });
45
+ };
46
+ const guards = [{ name: '', c: [] }, ...rs.map(r => ({ name: `${r.map}[a]=${r.value} -> `, c: [same(get(r.map, a), n(r.value))] }))];
47
+ for (const f of fs)
48
+ for (const g of fs) {
49
+ if (f === g)
50
+ continue;
51
+ for (const h of guards) {
52
+ if (f < g)
53
+ propose(`${h.name}${f}[a]=${g}[a]`, h.c, same(get(f, a), get(g, a)));
54
+ propose(`${h.name}${f}[a]<=${g}[a]`, h.c, atMost(get(f, a), get(g, a)));
55
+ }
56
+ // Equality of a record's first field can constrain the order of its second
57
+ // field across records. A role applies to the first record, not both.
58
+ for (const h of guards) {
59
+ const eq = same(get(f, a), get(f, b));
60
+ propose(`${h.name}${f}[a]=${f}[b] -> ${g}[b]<=${g}[a]`, [...h.c, eq], atMost(get(g, b), get(g, a)), true);
61
+ propose(`${h.name}${f}[a]=${f}[b] -> ${g}[a]=${g}[b]`, [...h.c, eq], same(get(g, a), get(g, b)), true);
62
+ }
63
+ }
64
+ // Versioned rows: two equal numeric fields identify the contents of a row.
65
+ // Quantify the member independently; no finite row is baked into the claim.
66
+ for (const [map, sorts] of Object.entries(m.relations ?? {})) {
67
+ if (sorts.length !== 2 || sorts[0] !== sorts[1])
68
+ continue;
69
+ for (let i = 0; i < fs.length; i++)
70
+ for (let j = i + 1; j < fs.length; j++) {
71
+ const f = fs[i], g = fs[j];
72
+ const guard = all(da, db, less(c, bound), same(get(f, a), get(f, b)), same(get(g, a), get(g, b)));
73
+ const holds = any(not(guard), same(get(map, tuple([a, c])), get(map, tuple([b, c]))));
74
+ out.push({ name: `fields: ${f}[a]=${f}[b] and ${g}[a]=${g}[b] -> ${map} rows agree`, forall: ka, keys: [kb, kc], holds });
75
+ }
76
+ }
77
+ // The numeric encoding base*row + member is a relation too. Discover it
78
+ // from reads, not map names; explicit tuple metadata keeps its old encoding.
79
+ if (composed) {
80
+ const rows = new Map();
81
+ walk([m.assume, m.step, m.counts], o => {
82
+ if (o.op !== 'get' || !(o.map in maps) || m.relations?.[o.map])
83
+ return;
84
+ const key = o.key;
85
+ if (key.op !== 'add' || key.a.op !== 'mul' || key.a.k <= 1 || key.a.a.op !== 'var' || key.b.op !== 'var' || key.a.a.name === key.b.name || !m.inputs.includes(key.a.a.name))
86
+ return;
87
+ const old = rows.get(o.map);
88
+ rows.set(o.map, old === undefined || old === key.a.k ? key.a.k : 0);
89
+ });
90
+ for (const [map, base] of rows)
91
+ if (base)
92
+ for (let i = 0; i < fs.length; i++)
93
+ for (let j = i + 1; j < fs.length; j++) {
94
+ const f = fs[i], g = fs[j], cell = (row) => get(map, plus(times(base, row), c));
95
+ const guard = all(da, db, less(c, bound), same(get(f, a), get(f, b)), same(get(g, a), get(g, b)));
96
+ out.push({ name: `fields: ${f}[a]=${f}[b] and ${g}[a]=${g}[b] -> ${map} rows agree`, forall: ka, keys: [kb, kc], holds: any(not(guard), same(cell(a), cell(b))) });
97
+ }
98
+ }
99
+ return out;
100
+ }
@@ -0,0 +1,5 @@
1
+ import { type C, type Law, type Machine, type State } from './machine.js';
2
+ type Env = Record<string, number>;
3
+ export declare function lawFrontiers(law: Law): C[][];
4
+ export declare function boundaryState(source: Machine, m: Machine, conditions: C[], p: Env, old: State, keys: Env): State | undefined;
5
+ export {};
@@ -0,0 +1,116 @@
1
+ // Untrusted finite-state proposals from the laws' own syntax. No proposal is
2
+ // evidence: finiteHoudini still checks the entire family and the actual step.
3
+ import { carriedView, compileC, evalE } from "./machine.js";
4
+ import { checkWall, WallExpired } from "./wall.js";
5
+ const flatten = (c) => c.op === 'and' ? c.xs.flatMap(flatten) : [c];
6
+ export function lawFrontiers(law) {
7
+ const disjuncts = law.holds.op === 'or' ? law.holds.xs : [law.holds];
8
+ const guards = disjuncts.flatMap(c => c.op === 'not' ? [c.x] : []);
9
+ const out = guards.length ? [guards.flatMap(flatten)] : [];
10
+ for (const guard of guards) {
11
+ const atoms = flatten(guard), others = guards.filter(c => c !== guard).flatMap(flatten);
12
+ if (atoms.length === 2)
13
+ for (let i = 0; i < 2; i++)
14
+ out.push([...others, atoms[i], { op: 'not', x: atoms[1 - i] }]);
15
+ }
16
+ for (const c of disjuncts)
17
+ for (const atom of flatten(c))
18
+ if (atom.op === '<=')
19
+ out.push([...guards.flatMap(flatten), { op: '=', a: atom.a, b: atom.b }]);
20
+ return [...new Map(out.map(xs => [JSON.stringify(xs), xs])).values()];
21
+ }
22
+ export function boundaryState(source, m, conditions, p, old, keys) {
23
+ if (source.history || source.leasts)
24
+ return undefined;
25
+ const st = { ...old, scalars: { ...old.scalars }, maps: { ...old.maps } }, env = { ...p, ...st.scalars, ...keys };
26
+ let cached;
27
+ const view = () => cached ??= carriedView(m, st, Object.fromEntries((m.inputMaps ?? []).map(k => [k, new Map()])), undefined, p);
28
+ const value = (e) => evalE(e, env, view(), m);
29
+ const natural = (n) => Number.isSafeInteger(n) && n >= 0;
30
+ const write = (e, n, depth) => {
31
+ checkWall('finite boundary write');
32
+ if (!natural(n) || depth > 16)
33
+ return false;
34
+ if (value(e) === n)
35
+ return true;
36
+ if (e.op === 'var' && e.name in source.state && !(e.name in keys)) {
37
+ st.scalars[e.name] = env[e.name] = n;
38
+ cached = undefined;
39
+ return true;
40
+ }
41
+ if (e.op === 'get') {
42
+ const key = value(e.key);
43
+ if (!natural(key))
44
+ return false;
45
+ if (e.map in (source.maps ?? {})) {
46
+ st.maps[e.map] = new Map(st.maps[e.map]);
47
+ st.maps[e.map].set(key, n);
48
+ cached = undefined;
49
+ return true;
50
+ }
51
+ const count = m.counts?.[e.map];
52
+ if (!count)
53
+ return false;
54
+ const prior = { ...env };
55
+ env[count.j] = count.base ? Math.floor(key / count.base) : key;
56
+ if (count.lo && count.base)
57
+ env[count.lo] = key % count.base;
58
+ const bound = value(count.below);
59
+ if (!natural(bound) || bound > 64 || n > bound) {
60
+ Object.assign(env, prior);
61
+ return false;
62
+ }
63
+ let ok = true;
64
+ for (let i = 0; i < bound && ok; i++) {
65
+ env[count.key] = i;
66
+ ok = write(count.body, Number(i < n), depth + 1);
67
+ }
68
+ for (const k of Object.keys(env))
69
+ if (!(k in prior))
70
+ delete env[k];
71
+ Object.assign(env, prior, st.scalars);
72
+ return ok;
73
+ }
74
+ if (e.op === 'if') {
75
+ if (e.then.op === 'const' && e.then.value === n)
76
+ return force(e.c, true, depth + 1);
77
+ if (e.else.op === 'const' && e.else.value === n)
78
+ return force(e.c, false, depth + 1);
79
+ }
80
+ if (e.op === 'add')
81
+ return write(e.a, n - value(e.b), depth + 1) || write(e.b, n - value(e.a), depth + 1);
82
+ if (e.op === 'sub')
83
+ return write(e.a, n + value(e.b), depth + 1);
84
+ if (e.op === 'mul' && e.k > 0 && n % e.k === 0)
85
+ return write(e.a, n / e.k, depth + 1);
86
+ return false;
87
+ };
88
+ const force = (c, wanted, depth = 0) => {
89
+ if (depth > 16)
90
+ return false;
91
+ if (compileC(c, m)(env, view()) === wanted)
92
+ return true;
93
+ if (c.op === 'not')
94
+ return force(c.x, !wanted, depth + 1);
95
+ if (c.op === 'and')
96
+ return wanted ? c.xs.every(x => force(x, true, depth + 1)) : c.xs.some(x => force(x, false, depth + 1));
97
+ if (c.op === 'or')
98
+ return wanted ? c.xs.some(x => force(x, true, depth + 1)) : c.xs.every(x => force(x, false, depth + 1));
99
+ if (c.op === 'flag' || c.op === 'all')
100
+ return false;
101
+ const a = value(c.a), b = value(c.b);
102
+ if (c.op === '=')
103
+ return wanted ? write(c.a, b, depth + 1) || write(c.b, a, depth + 1) : write(c.a, b + 1, depth + 1) || write(c.b, a + 1, depth + 1);
104
+ if (c.op === '<=')
105
+ return wanted ? write(c.a, b, depth + 1) || write(c.b, a, depth + 1) : write(c.a, b + 1, depth + 1) || write(c.b, a - 1, depth + 1);
106
+ return wanted ? write(c.a, b - 1, depth + 1) || write(c.b, a + 1, depth + 1) : write(c.a, b, depth + 1) || write(c.b, a, depth + 1);
107
+ };
108
+ try {
109
+ return conditions.every(c => force(c, true)) && conditions.every(c => compileC(c, m)(env, view())) ? st : undefined;
110
+ }
111
+ catch (error) {
112
+ if (error instanceof WallExpired)
113
+ throw error;
114
+ return undefined;
115
+ }
116
+ }
@@ -0,0 +1,52 @@
1
+ import { type Law, type Machine, type Request, type State } from './machine.js';
2
+ import { type TinyFiniteOptions, type TinyFiniteStatistics, type TinySeedStatistics } from './finite-tiny.js';
3
+ type Env = Record<string, number>;
4
+ type Samples = {
5
+ p: Env;
6
+ sts: State[];
7
+ }[];
8
+ type Truth = boolean | undefined;
9
+ export declare function finiteEvaluator(m: Machine, stop?: () => boolean): (law: Law, p: Env, st: State) => Truth;
10
+ export type FiniteDrop = {
11
+ round: number;
12
+ law: Law;
13
+ params: Env;
14
+ pre: State;
15
+ request: Request;
16
+ post: State;
17
+ };
18
+ export declare const finiteDump: {
19
+ onStart?: (value: {
20
+ machine: Machine;
21
+ caller: Law[];
22
+ candidates: Law[];
23
+ }) => void;
24
+ onDrop?: (drop: FiniteDrop) => void;
25
+ };
26
+ export type FiniteOptions = {
27
+ randomStates?: number;
28
+ requests?: number;
29
+ seed?: number;
30
+ targeted?: boolean;
31
+ wallMs?: number;
32
+ onFinish?: (row: {
33
+ timedOut: boolean;
34
+ ms: number;
35
+ dropped: number;
36
+ }) => void;
37
+ onDrop?: (drop: FiniteDrop) => void;
38
+ onRound?: (row: {
39
+ round: number;
40
+ alive: number;
41
+ dropped: number;
42
+ randomStates: number;
43
+ steps: number;
44
+ }) => void;
45
+ };
46
+ export declare function finiteHoudini(source: Machine, m: Machine, caller: Law[], candidates: Law[], samples: Samples, options?: FiniteOptions): Law[];
47
+ export declare function finiteTinyHoudini(source: Machine, m: Machine, caller: Law[], candidates: Law[], params: Env[], options?: FiniteOptions & TinyFiniteOptions & {
48
+ rounds?: number;
49
+ onTiny?: (round: number, stats: TinyFiniteStatistics) => void;
50
+ onSeed?: (round: number, stats: TinySeedStatistics) => void;
51
+ }): Law[];
52
+ export {};