@bjornpagen/bumbledb 0.1.0 → 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.
Files changed (103) hide show
  1. package/COOKBOOK.md +1394 -0
  2. package/README.md +4 -0
  3. package/dist/closed.d.ts +60 -37
  4. package/dist/closed.d.ts.map +1 -1
  5. package/dist/closed.js +159 -40
  6. package/dist/closed.js.map +1 -1
  7. package/dist/count.d.ts +64 -17
  8. package/dist/count.d.ts.map +1 -1
  9. package/dist/count.js +37 -14
  10. package/dist/count.js.map +1 -1
  11. package/dist/db.d.ts +15 -7
  12. package/dist/db.d.ts.map +1 -1
  13. package/dist/db.js +13 -5
  14. package/dist/db.js.map +1 -1
  15. package/dist/exhume.d.ts +12 -11
  16. package/dist/exhume.d.ts.map +1 -1
  17. package/dist/exhume.js +7 -6
  18. package/dist/exhume.js.map +1 -1
  19. package/dist/face.d.ts +79 -19
  20. package/dist/face.d.ts.map +1 -1
  21. package/dist/face.js +14 -15
  22. package/dist/face.js.map +1 -1
  23. package/dist/fields.d.ts +158 -129
  24. package/dist/fields.d.ts.map +1 -1
  25. package/dist/fields.js +80 -81
  26. package/dist/fields.js.map +1 -1
  27. package/dist/index.d.ts +21 -21
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +13 -12
  30. package/dist/index.js.map +1 -1
  31. package/dist/lower.d.ts +6 -3
  32. package/dist/lower.d.ts.map +1 -1
  33. package/dist/lower.js +48 -17
  34. package/dist/lower.js.map +1 -1
  35. package/dist/marshal.d.ts +36 -21
  36. package/dist/marshal.d.ts.map +1 -1
  37. package/dist/marshal.js +41 -25
  38. package/dist/marshal.js.map +1 -1
  39. package/dist/native.js +2 -2
  40. package/dist/query/atom.d.ts +332 -174
  41. package/dist/query/atom.d.ts.map +1 -1
  42. package/dist/query/atom.js +72 -172
  43. package/dist/query/atom.js.map +1 -1
  44. package/dist/query/lower.d.ts +295 -107
  45. package/dist/query/lower.d.ts.map +1 -1
  46. package/dist/query/lower.js +860 -388
  47. package/dist/query/lower.js.map +1 -1
  48. package/dist/query/predicate.d.ts +69 -88
  49. package/dist/query/predicate.d.ts.map +1 -1
  50. package/dist/query/predicate.js +118 -72
  51. package/dist/query/predicate.js.map +1 -1
  52. package/dist/query/run.d.ts +19 -16
  53. package/dist/query/run.d.ts.map +1 -1
  54. package/dist/query/run.js +31 -24
  55. package/dist/query/run.js.map +1 -1
  56. package/dist/query/scope.d.ts +139 -123
  57. package/dist/query/scope.d.ts.map +1 -1
  58. package/dist/query/scope.js +71 -115
  59. package/dist/query/scope.js.map +1 -1
  60. package/dist/query/select.d.ts +102 -80
  61. package/dist/query/select.d.ts.map +1 -1
  62. package/dist/query/select.js +39 -34
  63. package/dist/query/select.js.map +1 -1
  64. package/dist/relation.d.ts +33 -36
  65. package/dist/relation.d.ts.map +1 -1
  66. package/dist/relation.js +15 -16
  67. package/dist/relation.js.map +1 -1
  68. package/dist/schema.d.ts.map +1 -1
  69. package/dist/schema.js +19 -8
  70. package/dist/schema.js.map +1 -1
  71. package/dist/spec.d.ts +6 -5
  72. package/dist/spec.d.ts.map +1 -1
  73. package/dist/spec.js.map +1 -1
  74. package/dist/statements.d.ts +56 -31
  75. package/dist/statements.d.ts.map +1 -1
  76. package/dist/statements.js +38 -13
  77. package/dist/statements.js.map +1 -1
  78. package/package.json +4 -3
  79. package/src/closed.ts +271 -93
  80. package/src/count.ts +112 -18
  81. package/src/db.ts +22 -12
  82. package/src/exhume.ts +12 -11
  83. package/src/face.ts +114 -22
  84. package/src/fields.ts +261 -217
  85. package/src/index.ts +50 -59
  86. package/src/lower.ts +62 -20
  87. package/src/marshal.ts +48 -30
  88. package/src/native.ts +2 -2
  89. package/src/query/atom.ts +480 -376
  90. package/src/query/lower.ts +1341 -542
  91. package/src/query/predicate.ts +198 -161
  92. package/src/query/run.ts +35 -25
  93. package/src/query/scope.ts +188 -218
  94. package/src/query/select.ts +168 -93
  95. package/src/relation.ts +38 -44
  96. package/src/schema.ts +22 -12
  97. package/src/spec.ts +6 -5
  98. package/src/statements.ts +69 -26
  99. package/dist/brand.d.ts +0 -59
  100. package/dist/brand.d.ts.map +0 -1
  101. package/dist/brand.js +0 -47
  102. package/dist/brand.js.map +0 -1
  103. package/src/brand.ts +0 -82
@@ -1,226 +1,322 @@
1
1
  /**
2
- * Query atoms and conditions (PRD-08) — the body vocabulary of a rule,
3
- * mirroring the engine IR variant for variant
2
+ * Atoms and conditions, STRUCTURAL edition — the body vocabulary of a
3
+ * rule, mirroring the engine IR variant for variant
4
4
  * (`bumbledb/crates/bumbledb/src/ir.rs`, the bijection target;
5
- * `docs/architecture/20-query-ir.md` normative): `match` is the named-field
6
- * atom (unmentioned fields ARE the wildcard no wildcard value exists),
7
- * `not` is negation-as-position (anti-join), `is`/`ne`/`lt`/`le`/`gt`/`ge`
8
- * are the comparison roster, `covers` is the IR's `PointIn` predicate,
9
- * `allen` the 13-bit-mask interval-pair comparison, `and`/`or` the input
10
- * condition-tree grammar (distributed to DNF by the engine's validation),
11
- * and `duration` the measure term. Nothing beyond the IR exists here: no
12
- * convenience operator fakes an unsupported comparison.
5
+ * `docs/architecture/20-query-ir.md` normative). A `match` binding record
6
+ * binds fields to vars, params, ∈-set params, or bare structural literals
7
+ * (unmentioned fields ARE the wildcard — no wildcard value exists);
8
+ * `not(Rel, {...})` is negation-as-position (anti-join); `eq`/`ne` and the
9
+ * order roster, `pointIn`/`covers` (both spellings of `ir::CmpOp::PointIn`,
10
+ * always lowered interval-left), `allen` (the 13-bit mask pair
11
+ * comparison), and `and`/`or` (the input condition-tree grammar) complete
12
+ * the roster. Nothing beyond the IR exists here — and the walls the engine
13
+ * enforces at prepare are TYPES first: a var joins only domain-equal
14
+ * fields (`JoinOk`, checked against the rule environment AND against the
15
+ * binding record's own same-named siblings — two first occurrences of one
16
+ * name inside one record are a join too), an
17
+ * interval-typed var under a non-`pointIn` comparison is unwritable, and a
18
+ * negated atom's variables must be positively bound (env membership IS the
19
+ * safety rule). Every condition value carries its operands raw — the
20
+ * runtime representation is the type-inference carrier, no phantoms.
21
+ *
22
+ * This module also owns the plain runtime DATA a built rule is made of
23
+ * (`RuleData`/`RecData` and friends): frozen values the lowering walks —
24
+ * pure data, so lowering stays a pure, stable function of the query value.
13
25
  */
14
- import type { IntervalValue } from "#brand.ts";
15
- import { phantom } from "#brand.ts";
16
- import type { OneOf } from "#face.ts";
17
- import type { FieldData, FieldValue } from "#fields.ts";
18
- import type { PredicateData } from "#query/predicate.ts";
19
- import type { AnyTerm, AnyVar, ItemParams, MaskParam, Param, ParamSet, ParamsRecord, ParamsShape, TermContribution, Var } from "#query/scope.ts";
26
+ import type { AnyField, Infer, IntervalValue } from "#fields.ts";
27
+ import type { Duration, EnvShape, JoinOk, MaskParam, Param, ParamValueAt, SetParam, ShapeOf, Var } from "#query/scope.ts";
20
28
  import type { AnyRelation, FieldsShape, Relation } from "#relation.ts";
21
- /**
22
- * One atom-binding position as runtime data: a scope term, a host literal
23
- * (tagged at lowering by the FIELD's structural type — a point-typed
24
- * literal at an interval field is the IR's membership typing rule), or an
25
- * `oneOf` literal set (lowered to a fresh variable plus a disjunctive
26
- * equality condition — the three-confinement law's rule-level OR, spelled
27
- * for the caller as one binding).
28
- */
29
- type BindingTerm = {
30
- readonly kind: "term";
31
- readonly value: AnyTerm;
29
+ /** One atom-binding position as runtime data. */
30
+ type BindingTermData = {
31
+ readonly kind: "var";
32
+ readonly name: string;
33
+ } | {
34
+ readonly kind: "param";
35
+ readonly name: string;
36
+ } | {
37
+ readonly kind: "setParam";
38
+ readonly name: string;
32
39
  } | {
33
40
  readonly kind: "literal";
34
41
  readonly value: unknown;
35
- } | {
36
- readonly kind: "oneOf";
37
- readonly values: readonly unknown[];
38
42
  };
39
- /** One resolved binding: the field's name, its description, and the term. */
43
+ /** One resolved binding: the field's name, its descriptor, and the term. */
40
44
  interface BindingEntry {
41
45
  readonly field: string;
42
- readonly data: FieldData;
43
- readonly term: BindingTerm;
46
+ readonly data: AnyField;
47
+ readonly term: BindingTermData;
44
48
  }
45
- /** Where an atom draws its facts: a stored relation or a scope predicate. */
46
- type AtomSourceData = {
47
- readonly kind: "relation";
49
+ /** One EDB atom as runtime data (either polarity polarity is the rule item's). */
50
+ interface AtomData {
48
51
  readonly relation: AnyRelation;
49
- } | {
50
- readonly kind: "predicate";
51
- readonly pred: PredicateData;
52
- };
53
- /**
54
- * One atom value (positive or negated — negation is a position in the
55
- * rule, not a kind of atom, exactly as the IR reuses `Atom` unchanged).
56
- * The phantom carries the params object the atom's bindings contribute.
57
- */
58
- interface MatchAtom<P extends ParamsRecord> {
59
- readonly item: "atom";
60
- readonly negated: boolean;
61
- readonly source: AtomSourceData;
62
52
  readonly bindings: readonly BindingEntry[];
63
- readonly [phantom]?: P;
64
53
  }
54
+ /** One comparison operator name (mirrors `ir::CmpOp`). */
55
+ type CmpKind = "eq" | "ne" | "lt" | "le" | "gt" | "ge" | "pointIn" | "allen";
65
56
  /** One comparison side as runtime data. */
66
- type CmpTerm = {
67
- readonly kind: "term";
68
- readonly value: AnyTerm;
57
+ type CmpTermData = {
58
+ readonly kind: "var";
59
+ readonly name: string;
69
60
  } | {
70
- readonly kind: "literal";
71
- readonly value: unknown;
61
+ readonly kind: "param";
62
+ readonly name: string;
63
+ } | {
64
+ readonly kind: "setParam";
65
+ readonly name: string;
72
66
  } | {
73
67
  readonly kind: "measure";
74
- readonly over: AnyVar;
68
+ readonly name: string;
69
+ } | {
70
+ readonly kind: "literal";
71
+ readonly value: unknown;
75
72
  };
76
- /** The `allen` mask position: a literal 13-bit mask or a mask parameter. */
73
+ /** The `allen` mask position as runtime data. */
77
74
  type MaskData = {
78
75
  readonly kind: "literal";
79
76
  readonly mask: number;
80
77
  } | {
81
78
  readonly kind: "param";
82
- readonly param: MaskParam<string>;
79
+ readonly name: string;
83
80
  };
84
- /** One comparison operator as runtime data (mirrors `ir::CmpOp`). */
85
- type CmpOpData = {
86
- readonly kind: "eq";
87
- } | {
88
- readonly kind: "ne";
81
+ /** One comparison condition as runtime data (`mask` present exactly for `allen`). */
82
+ interface CmpData {
83
+ readonly kind: "cmp";
84
+ readonly op: CmpKind;
85
+ readonly mask: MaskData | undefined;
86
+ readonly lhs: CmpTermData;
87
+ readonly rhs: CmpTermData;
88
+ }
89
+ /** One condition-tree node as runtime data (`ir::ConditionTree`). */
90
+ interface TreeData {
91
+ readonly kind: "tree";
92
+ readonly op: "and" | "or";
93
+ readonly children: readonly CondData[];
94
+ }
95
+ /** Any condition node as runtime data. */
96
+ type CondData = CmpData | TreeData;
97
+ /** One aggregate's runtime description (select vocabulary, over var NAMES). */
98
+ type AggData = {
99
+ readonly op: "count";
89
100
  } | {
90
- readonly kind: "lt";
101
+ readonly op: "countDistinct";
102
+ readonly over: string;
91
103
  } | {
92
- readonly kind: "le";
104
+ readonly op: "fold";
105
+ readonly fold: "sum" | "min" | "max";
106
+ readonly over: string | {
107
+ readonly duration: string;
108
+ };
93
109
  } | {
94
- readonly kind: "gt";
110
+ readonly op: "arg";
111
+ readonly direction: "argMax" | "argMin";
112
+ readonly over: string;
113
+ readonly key: string;
95
114
  } | {
96
- readonly kind: "ge";
115
+ readonly op: "pack";
116
+ readonly over: string;
117
+ };
118
+ /** One classified select entry as runtime data. */
119
+ type SelectEntryData = {
120
+ readonly kind: "var";
121
+ readonly over: string;
97
122
  } | {
98
- readonly kind: "allen";
99
- readonly mask: MaskData;
123
+ readonly kind: "measure";
124
+ readonly over: string;
100
125
  } | {
101
- readonly kind: "pointIn";
126
+ readonly kind: "aggregate";
127
+ readonly agg: AggData;
102
128
  };
103
- /** One comparison condition value. */
104
- interface ComparisonItem<P extends ParamsRecord> {
105
- readonly item: "cmp";
106
- readonly op: CmpOpData;
107
- readonly lhs: CmpTerm;
108
- readonly rhs: CmpTerm;
109
- readonly [phantom]?: P;
129
+ /** One answer column: its name (the row object key) and its entry. */
130
+ interface SelectColumn {
131
+ readonly name: string;
132
+ readonly entry: SelectEntryData;
110
133
  }
134
+ /** One body item of a rule, in written order. */
135
+ type RuleItem = {
136
+ readonly kind: "atom";
137
+ readonly atom: AtomData;
138
+ } | {
139
+ readonly kind: "negated";
140
+ readonly atom: AtomData;
141
+ } | {
142
+ readonly kind: "idb";
143
+ readonly rec: RecData;
144
+ readonly vars: readonly string[];
145
+ } | {
146
+ readonly kind: "cond";
147
+ readonly cond: CondData;
148
+ };
111
149
  /**
112
- * One condition-tree node (`ir::ConditionTree`): any boolean combination
113
- * of comparisons the engine's validation distributes nested OR to DNF
114
- * rules; the surface admits exactly what the IR admits.
150
+ * One use of a parameter inside a rule, in written order: the census the
151
+ * query-level registry folds (first use mints the dense `ParamId`, first
152
+ * FIELD-ANCHORED use types the wire).
115
153
  */
116
- interface ConditionTreeItem<P extends ParamsRecord> {
117
- readonly item: "tree";
118
- readonly op: "and" | "or";
119
- readonly children: readonly AnyCondition[];
120
- readonly [phantom]?: P;
154
+ interface ParamUse {
155
+ readonly name: string;
156
+ readonly shape: "value" | "set" | "mask";
157
+ readonly anchor: AnyField | "measure" | undefined;
158
+ readonly op: "binding" | CmpKind;
159
+ }
160
+ /** One complete rule as runtime data. */
161
+ interface RuleData {
162
+ readonly items: readonly RuleItem[];
163
+ readonly select: readonly SelectColumn[];
164
+ /** Variable name → the field descriptor its FIRST positive binding carries (the runtime env). */
165
+ readonly varFields: Readonly<Record<string, AnyField>>;
166
+ readonly paramUses: readonly ParamUse[];
121
167
  }
122
- /** Any condition value, whatever its params. */
123
- type AnyCondition = ComparisonItem<ParamsRecord> | ConditionTreeItem<ParamsRecord>;
124
- /** Any rule body item: an atom (either polarity) or a condition. */
125
- type AnyBodyItem = MatchAtom<ParamsRecord> | AnyCondition;
126
168
  /**
127
- * The measure of an interval-typed variable (`ir::Term::Measure`, surface
128
- * `Duration`): `|[s, e)| = e s`, u64 legal as one side of an order
129
- * comparison, as a projected select entry, and as the input of
130
- * `sum`/`min`/`max`; every other position is unwritable here exactly as
131
- * the IR rejects it typed. A ray has no finite measure — the engine's
132
- * `MeasureOfRay` execution error; exclude rays first (`allen` against a
133
- * bounded window).
169
+ * One recursive predicate's runtime description identity keys the dense
170
+ * `PredId` at lowering. `rules` is appended by `rec.rule(...)` and sealed
171
+ * (frozen) when the program's output is declared.
134
172
  */
135
- interface Duration {
136
- readonly measure: AnyVar;
137
- readonly [phantom]?: bigint;
173
+ interface RecData {
174
+ readonly name: string;
175
+ readonly rules: RuleData[];
138
176
  }
139
177
  /**
140
- * What a binding position of value type `V` accepts: a branded literal, a
141
- * disjunctive `oneOf` literal set, a `Var`/`Param`/`ParamSet` of the same
142
- * brand — and, when the field is interval-typed, a point-typed term (the
143
- * IR's membership typing rule: an element-typed term at an interval field
144
- * is point membership; an interval-typed term is value equality).
178
+ * What a binding position of field `F` accepts: a bare structural literal
179
+ * of the field's value type, a var/param/∈-set-param term — and, when the
180
+ * field is interval-typed, a bare point literal (the IR's membership
181
+ * typing rule: an element-typed term at an interval field is point
182
+ * membership; an interval-typed term is value equality).
145
183
  */
146
- type TermInput<V> = V | OneOf<V> | Var<V> | Param<string, V> | ParamSet<string, V> | (V extends IntervalValue ? PointTermInput : never);
147
- /** The point-typed terms an interval field position additionally accepts. */
148
- type PointTermInput = bigint | Var<bigint> | Param<string, bigint> | ParamSet<string, bigint>;
184
+ type BindingInput<F extends AnyField> = Infer<F> | (F extends {
185
+ readonly kind: "interval";
186
+ } ? bigint : never) | Var<string> | Param<string> | SetParam<string>;
149
187
  /**
150
- * The `match` bindings record: per field, a term of that field's brand;
151
- * unmentioned fields are wildcards (absence IS the wildcard — the IR has
152
- * no wildcard variant to spell).
188
+ * The `match`/`not` bindings record: per field, a term or literal of that
189
+ * field's structural type; unmentioned fields are wildcards (absence IS
190
+ * the wildcard — the IR has no wildcard variant to spell).
153
191
  */
154
- type MatchInput<Fields extends FieldsShape> = {
155
- readonly [K in keyof Fields]?: TermInput<FieldValue<Fields[K]>>;
192
+ type MatchShape<F extends FieldsShape> = {
193
+ readonly [K in keyof F]?: BindingInput<F[K]>;
156
194
  };
157
- /** The params object a bindings record contributes (used by `match`). */
158
- type BindingsParams<B> = ParamsShape<TermContribution<B[keyof B]>>;
159
195
  /**
160
- * Resolves a bindings record against an ordered field roster (shared by
161
- * relation atoms here and predicate atoms in `#query/predicate.ts`), in
162
- * the record's written order.
196
+ * The var binding's judgment against the incoming rule environment: a name
197
+ * already bound must land on a domain-equal field.
163
198
  */
164
- declare function resolveBindings(context: string, fields: ReadonlyArray<{
165
- readonly name: string;
166
- readonly field: FieldData;
167
- }>, bindings: Readonly<Record<string, unknown>>): readonly BindingEntry[];
199
+ type EnvJoinOk<Env extends EnvShape, F extends FieldsShape, K, N extends string> = N extends keyof Env ? JoinOk<Env[N], F[K & keyof F]> : true;
200
+ /**
201
+ * The var binding's judgment against its OWN record's siblings: two
202
+ * bindings of one var name inside a single bindings record are the same
203
+ * join the environment check judges across atoms, so every same-named
204
+ * sibling must be domain-equal too. Without this arm two FIRST occurrences
205
+ * of one name (a record the environment has not seen yet) would meet no
206
+ * check at all — the intra-atom join would silently cross domains.
207
+ */
208
+ type SiblingJoinOk<F extends FieldsShape, B, K extends keyof B, N extends string> = false extends {
209
+ [K2 in Exclude<keyof B & keyof F, K>]: B[K2] extends Var<N> ? JoinOk<F[K2], F[K & keyof F]> : true;
210
+ }[Exclude<keyof B & keyof F, K>] ? false : true;
168
211
  /**
169
- * The named-field atom the semantic twin of `query!`'s `Node(id: c)`:
170
- * fields bind vars, params, branded literals, `oneOf` sets, or (interval
171
- * fields) point terms; unmentioned fields are wildcards; a zero-binding
172
- * atom is a nonemptiness gate on the relation (IR-legal, so writable).
212
+ * The per-property join judgment of a bindings record: a var binding must
213
+ * be domain-equal to the rule environment's binding of the name AND to
214
+ * every same-named sibling of its own record (a cross-domain reuse maps
215
+ * the property to `never` the compile error the old value brand carried,
216
+ * now structural).
173
217
  */
174
- declare function match<Name extends string, Fields extends FieldsShape, const B extends MatchInput<Fields>>(relation: Relation<Name, Fields>, bindings: B): MatchAtom<BindingsParams<B>>;
218
+ type BindingOk<Env extends EnvShape, F extends FieldsShape, B, K extends keyof B> = B[K] extends Var<infer N extends string> ? [EnvJoinOk<Env, F, K, N>, SiblingJoinOk<F, B, K, N>] extends [true, true] ? true : false : true;
219
+ /** The validated bindings record (intersect with the inferred `B` — errors land on the offending property). */
220
+ type CheckBindings<Env extends EnvShape, F extends FieldsShape, B> = {
221
+ readonly [K in keyof B]: K extends keyof F ? (BindingOk<Env, F, B, K> extends true ? B[K] : never) : never;
222
+ };
223
+ /** The environment a bindings record contributes: var name → the bound field's descriptor. */
224
+ type BindEnv<F extends FieldsShape, B> = {
225
+ readonly [K in keyof B & keyof F as B[K] extends Var<infer N extends string> ? N : never]: F[K];
226
+ };
227
+ /** The params-object fragments a bindings record contributes (one union member per param use). */
228
+ type BindParams<F extends FieldsShape, B> = {
229
+ [K in keyof B & keyof F]: B[K] extends Param<infer P extends string> ? {
230
+ readonly [Q in P]: ParamValueAt<F[K]>;
231
+ } : B[K] extends SetParam<infer P extends string> ? {
232
+ readonly [Q in P]: readonly ParamValueAt<F[K]>[];
233
+ } : never;
234
+ }[keyof B & keyof F];
175
235
  /**
176
- * Negation anti-join over sets, no null trick: a binding satisfies the
177
- * negated atom iff NO fact matches it. Safety (every negated var bound
178
- * positively) is validated at `query()` construction with an error naming
179
- * the variable; the negated atom binds nothing, only rejects. An `oneOf`
180
- * binding is refused HERE, at construction: it lowers to a synthetic
181
- * variable bound only inside the atom plus a rule-level OR — which is
182
- * exactly the shape the safety rule rejects, and semantically wrong for
183
- * negation anyway (¬∃(f = a ∨ f = b) is a CONJUNCTION of negated atoms,
184
- * ¬∃(f = a) ∧ ¬∃(f = b), never one negated atom's OR).
236
+ * One comparison VALUE: op plus its operands, raw the runtime
237
+ * representation carries the operand types, so `.where`'s environment
238
+ * check and the params inference both read the value itself (no phantom).
239
+ * `mask` is populated exactly for `allen`.
185
240
  */
186
- declare function not<P extends ParamsRecord>(atom: MatchAtom<P>): MatchAtom<P>;
241
+ interface Cmp<Op extends CmpKind, L, R, M = undefined> {
242
+ readonly cond: "cmp";
243
+ readonly op: Op;
244
+ readonly lhs: L;
245
+ readonly rhs: R;
246
+ readonly mask: M;
247
+ }
248
+ /** One condition-tree VALUE (`and`/`or` over comparisons and nested trees). */
249
+ interface Tree<Ch extends readonly AnyTreeChild[]> {
250
+ readonly cond: "tree";
251
+ readonly op: "and" | "or";
252
+ readonly children: Ch;
253
+ }
187
254
  /**
188
- * The equality atom (`ir::CmpOp::Eq`)for binding a var to a param or
189
- * literal where punning inside `match` doesn't apply, and for var-to-var
190
- * unification. Prefer direct placement (`match(Account, { kind:
191
- * Kind.Savings })`); `is` exists for the var-to-param case. `ParamSet` is
192
- * legal here and under no other operator (the IR's `Eq`-only set rule).
255
+ * One negated-atom VALUEnegation is a position in the rule (anti-join
256
+ * over sets, no null trick): a binding satisfies it iff NO fact matches.
257
+ * Its variables must be positively bound in the rule — environment
258
+ * membership at `.where` IS the safety rule, a compile error before it is
259
+ * the engine's refusal.
193
260
  */
194
- declare function is<V, const R extends Var<V> | Param<string, V> | ParamSet<string, V> | V>(left: Var<V>, right: R): ComparisonItem<ParamsShape<TermContribution<R>>>;
195
- /** Typed disequality (`ir::CmpOp::Ne`). "Not in set" has no operator — write a negated atom. */
196
- declare function ne<V, const R extends Var<V> | Param<string, V> | V>(left: Var<V>, right: R): ComparisonItem<ParamsShape<TermContribution<R>>>;
261
+ interface NotAtom<F extends FieldsShape, B> {
262
+ readonly cond: "not";
263
+ readonly relation: Relation<string, F>;
264
+ readonly bindings: B;
265
+ }
266
+ /** Any comparison value. */
267
+ type AnyCmp = Cmp<CmpKind, unknown, unknown, unknown>;
268
+ /** Any condition-tree child (trees hold comparisons and trees — never atoms). */
269
+ type AnyTreeChild = AnyCmp | Tree<readonly AnyTreeChild[]>;
270
+ /** Any negated-atom value. */
271
+ type AnyNotAtom = NotAtom<FieldsShape, unknown>;
272
+ /** Any `.where` input: a comparison, a condition tree, or a negated atom. */
273
+ type AnyCond = AnyCmp | Tree<readonly AnyTreeChild[]> | AnyNotAtom;
274
+ /** What `eq`'s right side accepts (`ParamSet` is `Eq`-only — the IR's rule). */
275
+ type EqRight = Var<string> | Param<string> | SetParam<string> | bigint | string | boolean | Uint8Array | IntervalValue;
276
+ /** What `ne`'s right side accepts. */
277
+ type NeRight = Var<string> | Param<string> | bigint | string | boolean | Uint8Array | IntervalValue;
278
+ /** One side of an order comparison: orderable terms only (the IR's comparison rules). */
279
+ type OrderSide = Var<string> | Param<string> | Duration<string> | bigint;
280
+ /** The point side of `pointIn`/`covers`. */
281
+ type PointSide = Var<string> | Param<string> | bigint;
282
+ /** The interval side of `pointIn`/`covers`/`allen`. */
283
+ type IntervalSide = Var<string> | Param<string> | IntervalValue;
284
+ /** Builds one comparison value. */
285
+ declare function comparison<Op extends CmpKind, L, R, M>(op: Op, lhs: L, rhs: R, mask: M): Cmp<Op, L, R, M>;
197
286
  /**
198
- * One side of an order comparison: a u64/i64-typed var or param, a bigint
199
- * literal, or the measure (`duration(v)`) order operators are legal for
200
- * the orderable types only, never intervals/bytes/strings/bools (the IR's
201
- * comparison rules; each refusal is the engine's own typed diagnostic).
287
+ * The equality comparison (`ir::CmpOp::Eq`) a bound variable against a
288
+ * variable (var-to-var unification, domain-equal by the environment
289
+ * check), a param (typed by the variable), an ∈-set param (`Eq`-only, the
290
+ * IR's set rule), or a bare literal of the variable's own value type.
291
+ * Prefer direct placement in `match` where punning applies.
202
292
  */
203
- type OrderInput = Var<bigint> | Param<string, bigint> | bigint | Duration;
204
- /** Strict less-than (`ir::CmpOp::Lt`). */
205
- declare function lt<const L extends OrderInput, const R extends OrderInput>(left: L, right: R): ComparisonItem<ParamsShape<TermContribution<L> | TermContribution<R>>>;
293
+ declare function eq<L extends Var<string>, const R extends EqRight>(left: L, right: R): Cmp<"eq", L, R>;
294
+ /** Typed disequality (`ir::CmpOp::Ne`). "Not in set" has no operator — write a negated atom. */
295
+ declare function ne<L extends Var<string>, const R extends NeRight>(left: L, right: R): Cmp<"ne", L, R>;
296
+ /** Strict less-than (`ir::CmpOp::Lt`) — orderable sides only, never intervals/bytes/strings/bools. */
297
+ declare function lt<const L extends OrderSide, const R extends OrderSide>(left: L, right: R): Cmp<"lt", L, R>;
206
298
  /** Less-or-equal (`ir::CmpOp::Le`). */
207
- declare function le<const L extends OrderInput, const R extends OrderInput>(left: L, right: R): ComparisonItem<ParamsShape<TermContribution<L> | TermContribution<R>>>;
299
+ declare function le<const L extends OrderSide, const R extends OrderSide>(left: L, right: R): Cmp<"le", L, R>;
208
300
  /** Strict greater-than (`ir::CmpOp::Gt`). */
209
- declare function gt<const L extends OrderInput, const R extends OrderInput>(left: L, right: R): ComparisonItem<ParamsShape<TermContribution<L> | TermContribution<R>>>;
301
+ declare function gt<const L extends OrderSide, const R extends OrderSide>(left: L, right: R): Cmp<"gt", L, R>;
210
302
  /** Greater-or-equal (`ir::CmpOp::Ge`). */
211
- declare function ge<const L extends OrderInput, const R extends OrderInput>(left: L, right: R): ComparisonItem<ParamsShape<TermContribution<L> | TermContribution<R>>>;
212
- /** An interval-typed comparison side: a var, a param, or a `span` literal. */
213
- type IntervalInput = Var<IntervalValue> | Param<string, IntervalValue> | IntervalValue;
214
- /** A point-typed comparison side. */
215
- type PointInput = Var<bigint> | Param<string, bigint> | bigint;
303
+ declare function ge<const L extends OrderSide, const R extends OrderSide>(left: L, right: R): Cmp<"ge", L, R>;
304
+ /**
305
+ * Point membership as a predicate (`ir::CmpOp::PointIn`), membership
306
+ * spelling: `pointIn(t, w)` holds iff `w.start ≤ t < w.end`. The IR
307
+ * orders the operands interval-left, point-right; the value stores them
308
+ * that way whatever the surface spelling.
309
+ */
310
+ declare function pointIn<const P extends PointSide, const I extends IntervalSide>(point: P, interval: I): Cmp<"pointIn", I, P>;
216
311
  /**
217
- * Point membership as a predicate (`ir::CmpOp::PointIn`): `covers(iv, t)`
218
- * holds iff `iv.start t < iv.end` the predicate form of the membership
219
- * typing rule, for terms already bound elsewhere. Interval interval is
220
- * NOT this operator; that predicate is `allen(a, ALLEN.covers, b)`. The IR
221
- * orders the operands interval-left, point-right; so does this surface.
312
+ * Point membership, coverage spelling `covers(w, t)` is `pointIn(t, w)`
313
+ * with the interval written first (the IR's own operand order; a literal
314
+ * `span(...)` left operand is legal and tags by the point sibling's
315
+ * element domain the bug-hunt fix, now also a type-level guarantee).
316
+ * Interval interval is NOT this operator; that predicate is
317
+ * `allen(a, ALLEN.covers, b)`.
222
318
  */
223
- declare function covers<const I extends IntervalInput, const T extends PointInput>(interval: I, point: T): ComparisonItem<ParamsShape<TermContribution<I> | TermContribution<T>>>;
319
+ declare function covers<const I extends IntervalSide, const P extends PointSide>(interval: I, point: P): Cmp<"pointIn", I, P>;
224
320
  /**
225
321
  * The Allen coordinate system's named constants — the 13 basics in the
226
322
  * engine's palindromic bit order (bit i = basic i:
@@ -255,31 +351,93 @@ declare const ALLEN: Readonly<{
255
351
  * THE interval-pair comparison (`ir::CmpOp::Allen`): two interval terms of
256
352
  * one element type, satisfied iff the pair's classification is in the
257
353
  * 13-bit mask — a literal built from the `ALLEN` constants, or a mask
258
- * parameter (`$.allenParam`). Vacuous masks (empty/full) are the engine's
354
+ * parameter (`r.maskParam`). Vacuous masks (empty/full) are the engine's
259
355
  * two distinct typed rejections; nothing is pre-judged here beyond the
260
356
  * representable bit range.
261
357
  */
262
- declare function allen<const A extends IntervalInput, const M extends number | MaskParam<string>, const B extends IntervalInput>(left: A, mask: M, right: B): ComparisonItem<ParamsShape<TermContribution<A> | TermContribution<M> | TermContribution<B>>>;
358
+ declare function allen<const A extends IntervalSide, const M extends number | MaskParam<string>, const B extends IntervalSide>(left: A, mask: M, right: B): Cmp<"allen", A, B, M>;
263
359
  /**
264
360
  * Conjunction node of the input condition grammar (`ConditionTree::And`).
265
361
  * The rule's condition list is already a conjunction — `and` exists for
266
362
  * nesting under `or`, and the empty combination keeps the IR's algebraic
267
363
  * reading (`And([])` is true).
268
364
  */
269
- declare function and<const C extends readonly AnyCondition[]>(...children: C): ConditionTreeItem<ParamsShape<ItemParams<C[number]>>>;
365
+ declare function and<const C extends readonly AnyTreeChild[]>(...children: C): Tree<C>;
270
366
  /**
271
367
  * Disjunction node of the input condition grammar (`ConditionTree::Or`) —
272
368
  * the one place the surface admits a nested OR; validation distributes it
273
369
  * to DNF rules engine-side (OR is data or it is nothing). `Or([])` keeps
274
370
  * its algebraic reading (false: the rule denotes nothing).
275
371
  */
276
- declare function or<const C extends readonly AnyCondition[]>(...children: C): ConditionTreeItem<ParamsShape<ItemParams<C[number]>>>;
372
+ declare function or<const C extends readonly AnyTreeChild[]>(...children: C): Tree<C>;
373
+ /**
374
+ * Negation — anti-join over sets: `not(Rel, { field: r.var("x"), ... })`
375
+ * rejects every binding some matching fact extends. A negated atom binds
376
+ * nothing, only rejects: every variable it names must be positively bound
377
+ * in the rule, which `.where`'s environment check makes a COMPILE error
378
+ * (the engine's safety refusal stands behind it).
379
+ */
380
+ declare function not<Name extends string, F extends FieldsShape, const B extends MatchShape<F>>(relation: Relation<Name, F>, bindings: B): NotAtom<F, B>;
381
+ /** Whether a var name is bound in the environment at an orderable (u64/i64) field. */
382
+ type OrderVarOk<Env extends EnvShape, N extends string> = N extends keyof Env ? Env[N]["kind"] extends "u64" | "i64" ? true : false : false;
383
+ /** Whether a var name is bound at an interval field. */
384
+ type IntervalVarOk<Env extends EnvShape, N extends string> = N extends keyof Env ? Env[N]["kind"] extends "interval" ? true : false : false;
385
+ /** One order-comparison side's judgment against the environment. */
386
+ type OrderSideOk<Env extends EnvShape, T> = T extends Var<infer N extends string> ? OrderVarOk<Env, N> : T extends Duration<infer N extends string> ? IntervalVarOk<Env, N> : true;
387
+ /** One point side's judgment. */
388
+ type PointSideOk<Env extends EnvShape, T> = T extends Var<infer N extends string> ? OrderVarOk<Env, N> : true;
389
+ /** One interval side's judgment. */
390
+ type IntervalSideOk<Env extends EnvShape, T> = T extends Var<infer N extends string> ? IntervalVarOk<Env, N> : true;
391
+ /** The `eq`/`ne` judgment: left var bound; right joins it (domain-equal var, param, or an exact-type literal). */
392
+ type EqOk<Env extends EnvShape, L, R> = L extends Var<infer N extends string> ? N extends keyof Env ? R extends Var<infer M extends string> ? M extends keyof Env ? JoinOk<Env[N], Env[M]> : false : R extends Param<string> | SetParam<string> ? true : [R] extends [Infer<Env[N]>] ? true : false : false : false;
393
+ /** One negated-atom binding's judgment: a var must be positively bound (safety) AND domain-equal. */
394
+ type NotBindingOk<Env extends EnvShape, F extends AnyField, T> = T extends Var<infer N extends string> ? (N extends keyof Env ? JoinOk<Env[N], F> : false) : true;
395
+ /** The whole negated atom's judgment. */
396
+ type NotOk<Env extends EnvShape, F extends FieldsShape, B> = false extends {
397
+ [K in keyof B]: NotBindingOk<Env, K extends keyof F ? F[K] : never, B[K]>;
398
+ }[keyof B] ? false : true;
399
+ /**
400
+ * One condition's judgment against the rule environment — the type-level
401
+ * twin of the engine's comparison roster: domain-equal joins, orderable
402
+ * order sides (an interval var under a non-`pointIn` op is exactly here
403
+ * refused), kind-correct `pointIn`/`covers`/`allen` sides, and negated-atom
404
+ * safety. The leading `[AnyTreeChild] extends [C]` arm is the recursion's
405
+ * base case: at an UNRESOLVED constraint (the whole condition union — or a
406
+ * tree's child union, which is the union itself) the judgment is vacuously
407
+ * true — without it the constraint instantiation recurses into itself.
408
+ */
409
+ type CondOkBool<Env extends EnvShape, C> = [AnyTreeChild] extends [C] ? true : C extends Cmp<infer Op, infer L, infer R, unknown> ? Op extends "eq" | "ne" ? EqOk<Env, L, R> : Op extends "lt" | "le" | "gt" | "ge" ? [OrderSideOk<Env, L>, OrderSideOk<Env, R>] extends [true, true] ? true : false : Op extends "pointIn" ? [IntervalSideOk<Env, L>, PointSideOk<Env, R>] extends [true, true] ? true : false : Op extends "allen" ? [IntervalSideOk<Env, L>, IntervalSideOk<Env, R>] extends [true, true] ? true : false : false : C extends Tree<infer Ch extends readonly AnyTreeChild[]> ? false extends CondOkBool<Env, Ch[number]> ? false : true : C extends NotAtom<infer F extends FieldsShape, infer B> ? NotOk<Env, F, B> : false;
410
+ /** The validated `.where` argument (intersect with the inferred condition type). */
411
+ type CheckCond<Env extends EnvShape, C> = CondOkBool<Env, C> extends true ? C : never;
412
+ /** The `eq`/`ne` params contribution: the param typed by the left variable's field. */
413
+ type EqParams<Env extends EnvShape, L, R> = L extends Var<infer N extends string> ? R extends Param<infer P extends string> ? {
414
+ readonly [Q in P]: Infer<Env[N & keyof Env]>;
415
+ } : R extends SetParam<infer P extends string> ? {
416
+ readonly [Q in P]: readonly Infer<Env[N & keyof Env]>[];
417
+ } : never : never;
418
+ /** An order side's params contribution (order params are always `bigint`). */
419
+ type OrderSideParams<T> = T extends Param<infer P extends string> ? {
420
+ readonly [Q in P]: bigint;
421
+ } : never;
422
+ /** An interval side's params contribution. */
423
+ type IntervalSideParams<T> = T extends Param<infer P extends string> ? {
424
+ readonly [Q in P]: IntervalValue;
425
+ } : never;
426
+ /** The mask position's params contribution. */
427
+ type MaskParams<M> = M extends MaskParam<infer P extends string> ? {
428
+ readonly [Q in P]: number;
429
+ } : never;
277
430
  /**
278
- * The measure term surface `Duration`, IR `Measure`: the point-set
279
- * cardinality `end start` of an interval-typed variable, u64. See
280
- * {@link Duration} for the legal positions.
431
+ * One condition's params-object fragments (a union; the rule builder folds
432
+ * them into the inferred `Params` record) every param typed by its use.
433
+ * The leading arm is the same base case as {@link CondOkBool}'s: the
434
+ * unresolved constraint contributes nothing.
281
435
  */
282
- declare function duration<IV extends IntervalValue>(over: Var<IV>): Duration;
283
- export type { AnyBodyItem, AnyCondition, AtomSourceData, BindingEntry, BindingsParams, BindingTerm, CmpOpData, CmpTerm, ComparisonItem, ConditionTreeItem, Duration, IntervalInput, MaskData, MatchAtom, MatchInput, OrderInput, PointInput, PointTermInput, TermInput };
284
- export { ALLEN, allen, and, covers, duration, ge, gt, is, le, lt, match, ne, not, or, resolveBindings };
436
+ type CondParams<Env extends EnvShape, C> = [AnyTreeChild] extends [C] ? never : C extends Cmp<infer Op, infer L, infer R, infer M> ? Op extends "eq" | "ne" ? EqParams<Env, L, R> : Op extends "lt" | "le" | "gt" | "ge" ? OrderSideParams<L> | OrderSideParams<R> : Op extends "pointIn" ? IntervalSideParams<L> | OrderSideParams<R> : Op extends "allen" ? IntervalSideParams<L> | IntervalSideParams<R> | MaskParams<M> : never : C extends Tree<infer Ch extends readonly AnyTreeChild[]> ? CondParams<Env, Ch[number]> : C extends NotAtom<infer F extends FieldsShape, infer B> ? BindParams<F, B> : never;
437
+ /** The flattened params record one bindings record contributes. */
438
+ type BindParamsShape<F extends FieldsShape, B> = ShapeOf<BindParams<F, B>>;
439
+ /** The flattened params record one condition contributes. */
440
+ type CondParamsShape<Env extends EnvShape, C> = ShapeOf<CondParams<Env, C>>;
441
+ export type { AggData, AnyCmp, AnyCond, AnyNotAtom, AnyTreeChild, AtomData, BindEnv, BindingEntry, BindingInput, BindingTermData, BindParams, BindParamsShape, CheckBindings, CheckCond, Cmp, CmpData, CmpKind, CmpTermData, CondData, CondOkBool, CondParams, CondParamsShape, IntervalSide, IntervalVarOk, MaskData, MatchShape, NotAtom, OrderSide, OrderVarOk, ParamUse, PointSide, RecData, RuleData, RuleItem, SelectColumn, SelectEntryData, Tree, TreeData };
442
+ export { ALLEN, allen, and, comparison, covers, eq, ge, gt, le, lt, ne, not, or, pointIn };
285
443
  //# sourceMappingURL=atom.d.ts.map