@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.
- package/COOKBOOK.md +1394 -0
- package/README.md +4 -0
- package/dist/closed.d.ts +60 -37
- package/dist/closed.d.ts.map +1 -1
- package/dist/closed.js +159 -40
- package/dist/closed.js.map +1 -1
- package/dist/count.d.ts +64 -17
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +37 -14
- package/dist/count.js.map +1 -1
- package/dist/db.d.ts +15 -7
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +13 -5
- package/dist/db.js.map +1 -1
- package/dist/exhume.d.ts +12 -11
- package/dist/exhume.d.ts.map +1 -1
- package/dist/exhume.js +7 -6
- package/dist/exhume.js.map +1 -1
- package/dist/face.d.ts +79 -19
- package/dist/face.d.ts.map +1 -1
- package/dist/face.js +14 -15
- package/dist/face.js.map +1 -1
- package/dist/fields.d.ts +158 -129
- package/dist/fields.d.ts.map +1 -1
- package/dist/fields.js +80 -81
- package/dist/fields.js.map +1 -1
- package/dist/index.d.ts +21 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -12
- package/dist/index.js.map +1 -1
- package/dist/lower.d.ts +6 -3
- package/dist/lower.d.ts.map +1 -1
- package/dist/lower.js +48 -17
- package/dist/lower.js.map +1 -1
- package/dist/marshal.d.ts +36 -21
- package/dist/marshal.d.ts.map +1 -1
- package/dist/marshal.js +41 -25
- package/dist/marshal.js.map +1 -1
- package/dist/native.js +2 -2
- package/dist/query/atom.d.ts +332 -174
- package/dist/query/atom.d.ts.map +1 -1
- package/dist/query/atom.js +72 -172
- package/dist/query/atom.js.map +1 -1
- package/dist/query/lower.d.ts +295 -107
- package/dist/query/lower.d.ts.map +1 -1
- package/dist/query/lower.js +860 -388
- package/dist/query/lower.js.map +1 -1
- package/dist/query/predicate.d.ts +69 -88
- package/dist/query/predicate.d.ts.map +1 -1
- package/dist/query/predicate.js +118 -72
- package/dist/query/predicate.js.map +1 -1
- package/dist/query/run.d.ts +19 -16
- package/dist/query/run.d.ts.map +1 -1
- package/dist/query/run.js +31 -24
- package/dist/query/run.js.map +1 -1
- package/dist/query/scope.d.ts +139 -123
- package/dist/query/scope.d.ts.map +1 -1
- package/dist/query/scope.js +71 -115
- package/dist/query/scope.js.map +1 -1
- package/dist/query/select.d.ts +102 -80
- package/dist/query/select.d.ts.map +1 -1
- package/dist/query/select.js +39 -34
- package/dist/query/select.js.map +1 -1
- package/dist/relation.d.ts +33 -36
- package/dist/relation.d.ts.map +1 -1
- package/dist/relation.js +15 -16
- package/dist/relation.js.map +1 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +19 -8
- package/dist/schema.js.map +1 -1
- package/dist/spec.d.ts +6 -5
- package/dist/spec.d.ts.map +1 -1
- package/dist/spec.js.map +1 -1
- package/dist/statements.d.ts +56 -31
- package/dist/statements.d.ts.map +1 -1
- package/dist/statements.js +38 -13
- package/dist/statements.js.map +1 -1
- package/package.json +4 -3
- package/src/closed.ts +271 -93
- package/src/count.ts +112 -18
- package/src/db.ts +22 -12
- package/src/exhume.ts +12 -11
- package/src/face.ts +114 -22
- package/src/fields.ts +261 -217
- package/src/index.ts +50 -59
- package/src/lower.ts +62 -20
- package/src/marshal.ts +48 -30
- package/src/native.ts +2 -2
- package/src/query/atom.ts +480 -376
- package/src/query/lower.ts +1341 -542
- package/src/query/predicate.ts +198 -161
- package/src/query/run.ts +35 -25
- package/src/query/scope.ts +188 -218
- package/src/query/select.ts +168 -93
- package/src/relation.ts +38 -44
- package/src/schema.ts +22 -12
- package/src/spec.ts +6 -5
- package/src/statements.ts +69 -26
- package/dist/brand.d.ts +0 -59
- package/dist/brand.d.ts.map +0 -1
- package/dist/brand.js +0 -47
- package/dist/brand.js.map +0 -1
- package/src/brand.ts +0 -82
package/src/query/atom.ts
CHANGED
|
@@ -1,303 +1,307 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
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)
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* and `
|
|
12
|
-
*
|
|
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
26
|
|
|
15
27
|
import * as errors from "@superbuilders/errors"
|
|
16
|
-
import type { IntervalValue } from "#
|
|
17
|
-
import { phantom } from "#brand.ts"
|
|
18
|
-
import type { OneOf } from "#face.ts"
|
|
19
|
-
import type { FieldData, FieldValue } from "#fields.ts"
|
|
20
|
-
import type { PredicateData } from "#query/predicate.ts"
|
|
28
|
+
import type { AnyField, Infer, IntervalValue } from "#fields.ts"
|
|
21
29
|
import type {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
30
|
+
Duration,
|
|
31
|
+
EnvShape,
|
|
32
|
+
JoinOk,
|
|
25
33
|
MaskParam,
|
|
26
34
|
Param,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
TermContribution,
|
|
35
|
+
ParamValueAt,
|
|
36
|
+
SetParam,
|
|
37
|
+
ShapeOf,
|
|
31
38
|
Var
|
|
32
39
|
} from "#query/scope.ts"
|
|
33
|
-
import { isTerm
|
|
40
|
+
import { isTerm } from "#query/scope.ts"
|
|
34
41
|
import type { AnyRelation, FieldsShape, Relation } from "#relation.ts"
|
|
35
42
|
|
|
36
|
-
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
* equality condition — the three-confinement law's rule-level OR, spelled
|
|
42
|
-
* for the caller as one binding).
|
|
43
|
-
*/
|
|
44
|
-
type BindingTerm =
|
|
45
|
-
| { readonly kind: "term"; readonly value: AnyTerm }
|
|
43
|
+
/** One atom-binding position as runtime data. */
|
|
44
|
+
type BindingTermData =
|
|
45
|
+
| { readonly kind: "var"; readonly name: string }
|
|
46
|
+
| { readonly kind: "param"; readonly name: string }
|
|
47
|
+
| { readonly kind: "setParam"; readonly name: string }
|
|
46
48
|
| { readonly kind: "literal"; readonly value: unknown }
|
|
47
|
-
| { readonly kind: "oneOf"; readonly values: readonly unknown[] }
|
|
48
49
|
|
|
49
|
-
/** One resolved binding: the field's name, its
|
|
50
|
+
/** One resolved binding: the field's name, its descriptor, and the term. */
|
|
50
51
|
interface BindingEntry {
|
|
51
52
|
readonly field: string
|
|
52
|
-
readonly data:
|
|
53
|
-
readonly term:
|
|
53
|
+
readonly data: AnyField
|
|
54
|
+
readonly term: BindingTermData
|
|
54
55
|
}
|
|
55
56
|
|
|
56
|
-
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
| { readonly kind: "predicate"; readonly pred: PredicateData }
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* One atom value (positive or negated — negation is a position in the
|
|
63
|
-
* rule, not a kind of atom, exactly as the IR reuses `Atom` unchanged).
|
|
64
|
-
* The phantom carries the params object the atom's bindings contribute.
|
|
65
|
-
*/
|
|
66
|
-
interface MatchAtom<P extends ParamsRecord> {
|
|
67
|
-
readonly item: "atom"
|
|
68
|
-
readonly negated: boolean
|
|
69
|
-
readonly source: AtomSourceData
|
|
57
|
+
/** One EDB atom as runtime data (either polarity — polarity is the rule item's). */
|
|
58
|
+
interface AtomData {
|
|
59
|
+
readonly relation: AnyRelation
|
|
70
60
|
readonly bindings: readonly BindingEntry[]
|
|
71
|
-
readonly [phantom]?: P
|
|
72
61
|
}
|
|
73
62
|
|
|
63
|
+
/** One comparison operator name (mirrors `ir::CmpOp`). */
|
|
64
|
+
type CmpKind = "eq" | "ne" | "lt" | "le" | "gt" | "ge" | "pointIn" | "allen"
|
|
65
|
+
|
|
74
66
|
/** One comparison side as runtime data. */
|
|
75
|
-
type
|
|
76
|
-
| { readonly kind: "
|
|
67
|
+
type CmpTermData =
|
|
68
|
+
| { readonly kind: "var"; readonly name: string }
|
|
69
|
+
| { readonly kind: "param"; readonly name: string }
|
|
70
|
+
| { readonly kind: "setParam"; readonly name: string }
|
|
71
|
+
| { readonly kind: "measure"; readonly name: string }
|
|
77
72
|
| { readonly kind: "literal"; readonly value: unknown }
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
| { readonly kind: "lt" }
|
|
90
|
-
| { readonly kind: "le" }
|
|
91
|
-
| { readonly kind: "gt" }
|
|
92
|
-
| { readonly kind: "ge" }
|
|
93
|
-
| { readonly kind: "allen"; readonly mask: MaskData }
|
|
94
|
-
| { readonly kind: "pointIn" }
|
|
95
|
-
|
|
96
|
-
/** One comparison condition value. */
|
|
97
|
-
interface ComparisonItem<P extends ParamsRecord> {
|
|
98
|
-
readonly item: "cmp"
|
|
99
|
-
readonly op: CmpOpData
|
|
100
|
-
readonly lhs: CmpTerm
|
|
101
|
-
readonly rhs: CmpTerm
|
|
102
|
-
readonly [phantom]?: P
|
|
73
|
+
|
|
74
|
+
/** The `allen` mask position as runtime data. */
|
|
75
|
+
type MaskData = { readonly kind: "literal"; readonly mask: number } | { readonly kind: "param"; readonly name: string }
|
|
76
|
+
|
|
77
|
+
/** One comparison condition as runtime data (`mask` present exactly for `allen`). */
|
|
78
|
+
interface CmpData {
|
|
79
|
+
readonly kind: "cmp"
|
|
80
|
+
readonly op: CmpKind
|
|
81
|
+
readonly mask: MaskData | undefined
|
|
82
|
+
readonly lhs: CmpTermData
|
|
83
|
+
readonly rhs: CmpTermData
|
|
103
84
|
}
|
|
104
85
|
|
|
105
|
-
/**
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
* rules; the surface admits exactly what the IR admits.
|
|
109
|
-
*/
|
|
110
|
-
interface ConditionTreeItem<P extends ParamsRecord> {
|
|
111
|
-
readonly item: "tree"
|
|
86
|
+
/** One condition-tree node as runtime data (`ir::ConditionTree`). */
|
|
87
|
+
interface TreeData {
|
|
88
|
+
readonly kind: "tree"
|
|
112
89
|
readonly op: "and" | "or"
|
|
113
|
-
readonly children: readonly
|
|
114
|
-
readonly [phantom]?: P
|
|
90
|
+
readonly children: readonly CondData[]
|
|
115
91
|
}
|
|
116
92
|
|
|
117
|
-
/** Any condition
|
|
118
|
-
type
|
|
93
|
+
/** Any condition node as runtime data. */
|
|
94
|
+
type CondData = CmpData | TreeData
|
|
95
|
+
|
|
96
|
+
/** One aggregate's runtime description (select vocabulary, over var NAMES). */
|
|
97
|
+
type AggData =
|
|
98
|
+
| { readonly op: "count" }
|
|
99
|
+
| { readonly op: "countDistinct"; readonly over: string }
|
|
100
|
+
| { readonly op: "fold"; readonly fold: "sum" | "min" | "max"; readonly over: string | { readonly duration: string } }
|
|
101
|
+
| { readonly op: "arg"; readonly direction: "argMax" | "argMin"; readonly over: string; readonly key: string }
|
|
102
|
+
| { readonly op: "pack"; readonly over: string }
|
|
103
|
+
|
|
104
|
+
/** One classified select entry as runtime data. */
|
|
105
|
+
type SelectEntryData =
|
|
106
|
+
| { readonly kind: "var"; readonly over: string }
|
|
107
|
+
| { readonly kind: "measure"; readonly over: string }
|
|
108
|
+
| { readonly kind: "aggregate"; readonly agg: AggData }
|
|
109
|
+
|
|
110
|
+
/** One answer column: its name (the row object key) and its entry. */
|
|
111
|
+
interface SelectColumn {
|
|
112
|
+
readonly name: string
|
|
113
|
+
readonly entry: SelectEntryData
|
|
114
|
+
}
|
|
119
115
|
|
|
120
|
-
/**
|
|
121
|
-
type
|
|
116
|
+
/** One body item of a rule, in written order. */
|
|
117
|
+
type RuleItem =
|
|
118
|
+
| { readonly kind: "atom"; readonly atom: AtomData }
|
|
119
|
+
| { readonly kind: "negated"; readonly atom: AtomData }
|
|
120
|
+
| { readonly kind: "idb"; readonly rec: RecData; readonly vars: readonly string[] }
|
|
121
|
+
| { readonly kind: "cond"; readonly cond: CondData }
|
|
122
122
|
|
|
123
123
|
/**
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* `sum`/`min`/`max`; every other position is unwritable here exactly as
|
|
128
|
-
* the IR rejects it typed. A ray has no finite measure — the engine's
|
|
129
|
-
* `MeasureOfRay` execution error; exclude rays first (`allen` against a
|
|
130
|
-
* bounded window).
|
|
124
|
+
* One use of a parameter inside a rule, in written order: the census the
|
|
125
|
+
* query-level registry folds (first use mints the dense `ParamId`, first
|
|
126
|
+
* FIELD-ANCHORED use types the wire).
|
|
131
127
|
*/
|
|
132
|
-
interface
|
|
133
|
-
readonly
|
|
134
|
-
readonly
|
|
128
|
+
interface ParamUse {
|
|
129
|
+
readonly name: string
|
|
130
|
+
readonly shape: "value" | "set" | "mask"
|
|
131
|
+
readonly anchor: AnyField | "measure" | undefined
|
|
132
|
+
readonly op: "binding" | CmpKind
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** One complete rule as runtime data. */
|
|
136
|
+
interface RuleData {
|
|
137
|
+
readonly items: readonly RuleItem[]
|
|
138
|
+
readonly select: readonly SelectColumn[]
|
|
139
|
+
/** Variable name → the field descriptor its FIRST positive binding carries (the runtime env). */
|
|
140
|
+
readonly varFields: Readonly<Record<string, AnyField>>
|
|
141
|
+
readonly paramUses: readonly ParamUse[]
|
|
135
142
|
}
|
|
136
143
|
|
|
137
144
|
/**
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
* IR's membership typing rule: an element-typed term at an interval field
|
|
142
|
-
* is point membership; an interval-typed term is value equality).
|
|
145
|
+
* One recursive predicate's runtime description — identity keys the dense
|
|
146
|
+
* `PredId` at lowering. `rules` is appended by `rec.rule(...)` and sealed
|
|
147
|
+
* (frozen) when the program's output is declared.
|
|
143
148
|
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
| Param<string, V>
|
|
149
|
-
| ParamSet<string, V>
|
|
150
|
-
| (V extends IntervalValue ? PointTermInput : never)
|
|
149
|
+
interface RecData {
|
|
150
|
+
readonly name: string
|
|
151
|
+
readonly rules: RuleData[]
|
|
152
|
+
}
|
|
151
153
|
|
|
152
|
-
/**
|
|
153
|
-
|
|
154
|
+
/**
|
|
155
|
+
* What a binding position of field `F` accepts: a bare structural literal
|
|
156
|
+
* of the field's value type, a var/param/∈-set-param term — and, when the
|
|
157
|
+
* field is interval-typed, a bare point literal (the IR's membership
|
|
158
|
+
* typing rule: an element-typed term at an interval field is point
|
|
159
|
+
* membership; an interval-typed term is value equality).
|
|
160
|
+
*/
|
|
161
|
+
type BindingInput<F extends AnyField> =
|
|
162
|
+
| Infer<F>
|
|
163
|
+
| (F extends { readonly kind: "interval" } ? bigint : never)
|
|
164
|
+
| Var<string>
|
|
165
|
+
| Param<string>
|
|
166
|
+
| SetParam<string>
|
|
154
167
|
|
|
155
168
|
/**
|
|
156
|
-
* The `match` bindings record: per field, a term of that
|
|
157
|
-
* unmentioned fields are wildcards (absence IS
|
|
158
|
-
* no wildcard variant to spell).
|
|
169
|
+
* The `match`/`not` bindings record: per field, a term or literal of that
|
|
170
|
+
* field's structural type; unmentioned fields are wildcards (absence IS
|
|
171
|
+
* the wildcard — the IR has no wildcard variant to spell).
|
|
159
172
|
*/
|
|
160
|
-
type
|
|
161
|
-
readonly [K in keyof
|
|
173
|
+
type MatchShape<F extends FieldsShape> = {
|
|
174
|
+
readonly [K in keyof F]?: BindingInput<F[K]>
|
|
162
175
|
}
|
|
163
176
|
|
|
164
|
-
/**
|
|
165
|
-
|
|
177
|
+
/**
|
|
178
|
+
* The var binding's judgment against the incoming rule environment: a name
|
|
179
|
+
* already bound must land on a domain-equal field.
|
|
180
|
+
*/
|
|
181
|
+
type EnvJoinOk<Env extends EnvShape, F extends FieldsShape, K, N extends string> = N extends keyof Env
|
|
182
|
+
? JoinOk<Env[N], F[K & keyof F]>
|
|
183
|
+
: true
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The var binding's judgment against its OWN record's siblings: two
|
|
187
|
+
* bindings of one var name inside a single bindings record are the same
|
|
188
|
+
* join the environment check judges across atoms, so every same-named
|
|
189
|
+
* sibling must be domain-equal too. Without this arm two FIRST occurrences
|
|
190
|
+
* of one name (a record the environment has not seen yet) would meet no
|
|
191
|
+
* check at all — the intra-atom join would silently cross domains.
|
|
192
|
+
*/
|
|
193
|
+
type SiblingJoinOk<F extends FieldsShape, B, K extends keyof B, N extends string> = false extends {
|
|
194
|
+
[K2 in Exclude<keyof B & keyof F, K>]: B[K2] extends Var<N> ? JoinOk<F[K2], F[K & keyof F]> : true
|
|
195
|
+
}[Exclude<keyof B & keyof F, K>]
|
|
196
|
+
? false
|
|
197
|
+
: true
|
|
166
198
|
|
|
167
|
-
/**
|
|
168
|
-
|
|
169
|
-
|
|
199
|
+
/**
|
|
200
|
+
* The per-property join judgment of a bindings record: a var binding must
|
|
201
|
+
* be domain-equal to the rule environment's binding of the name AND to
|
|
202
|
+
* every same-named sibling of its own record (a cross-domain reuse maps
|
|
203
|
+
* the property to `never` — the compile error the old value brand carried,
|
|
204
|
+
* now structural).
|
|
205
|
+
*/
|
|
206
|
+
type BindingOk<Env extends EnvShape, F extends FieldsShape, B, K extends keyof B> =
|
|
207
|
+
B[K] extends Var<infer N extends string>
|
|
208
|
+
? [EnvJoinOk<Env, F, K, N>, SiblingJoinOk<F, B, K, N>] extends [true, true]
|
|
209
|
+
? true
|
|
210
|
+
: false
|
|
211
|
+
: true
|
|
212
|
+
|
|
213
|
+
/** The validated bindings record (intersect with the inferred `B` — errors land on the offending property). */
|
|
214
|
+
type CheckBindings<Env extends EnvShape, F extends FieldsShape, B> = {
|
|
215
|
+
readonly [K in keyof B]: K extends keyof F ? (BindingOk<Env, F, B, K> extends true ? B[K] : never) : never
|
|
170
216
|
}
|
|
171
217
|
|
|
172
|
-
/**
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
if (value[term] === "maskParam") {
|
|
176
|
-
throw errors.new(
|
|
177
|
-
`${context}: an Allen-mask param is not a field-typed value — masks live in allen() conditions only`
|
|
178
|
-
)
|
|
179
|
-
}
|
|
180
|
-
return Object.freeze({ kind: "term" as const, value })
|
|
181
|
-
}
|
|
182
|
-
if (isOneOf(value)) {
|
|
183
|
-
return Object.freeze({ kind: "oneOf" as const, values: Object.freeze([...value.literals]) })
|
|
184
|
-
}
|
|
185
|
-
return Object.freeze({ kind: "literal" as const, value })
|
|
218
|
+
/** The environment a bindings record contributes: var name → the bound field's descriptor. */
|
|
219
|
+
type BindEnv<F extends FieldsShape, B> = {
|
|
220
|
+
readonly [K in keyof B & keyof F as B[K] extends Var<infer N extends string> ? N : never]: F[K]
|
|
186
221
|
}
|
|
187
222
|
|
|
223
|
+
/** The params-object fragments a bindings record contributes (one union member per param use). */
|
|
224
|
+
type BindParams<F extends FieldsShape, B> = {
|
|
225
|
+
[K in keyof B & keyof F]: B[K] extends Param<infer P extends string>
|
|
226
|
+
? { readonly [Q in P]: ParamValueAt<F[K]> }
|
|
227
|
+
: B[K] extends SetParam<infer P extends string>
|
|
228
|
+
? { readonly [Q in P]: readonly ParamValueAt<F[K]>[] }
|
|
229
|
+
: never
|
|
230
|
+
}[keyof B & keyof F]
|
|
231
|
+
|
|
188
232
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* the
|
|
233
|
+
* One comparison VALUE: op plus its operands, raw — the runtime
|
|
234
|
+
* representation carries the operand types, so `.where`'s environment
|
|
235
|
+
* check and the params inference both read the value itself (no phantom).
|
|
236
|
+
* `mask` is populated exactly for `allen`.
|
|
192
237
|
*/
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
for (const [fieldName, value] of Object.entries(bindings)) {
|
|
200
|
-
if (value === undefined) {
|
|
201
|
-
continue
|
|
202
|
-
}
|
|
203
|
-
const declared = fields.find(function byName(candidate) {
|
|
204
|
-
return candidate.name === fieldName
|
|
205
|
-
})
|
|
206
|
-
if (declared === undefined) {
|
|
207
|
-
throw errors.new(`${context} has no field ${fieldName}`)
|
|
208
|
-
}
|
|
209
|
-
entries.push(
|
|
210
|
-
Object.freeze({
|
|
211
|
-
field: fieldName,
|
|
212
|
-
data: declared.field,
|
|
213
|
-
term: bindingTermOf(`${context}.${fieldName}`, value)
|
|
214
|
-
})
|
|
215
|
-
)
|
|
216
|
-
}
|
|
217
|
-
return Object.freeze(entries)
|
|
238
|
+
interface Cmp<Op extends CmpKind, L, R, M = undefined> {
|
|
239
|
+
readonly cond: "cmp"
|
|
240
|
+
readonly op: Op
|
|
241
|
+
readonly lhs: L
|
|
242
|
+
readonly rhs: R
|
|
243
|
+
readonly mask: M
|
|
218
244
|
}
|
|
219
245
|
|
|
220
|
-
/**
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
*/
|
|
226
|
-
function match<Name extends string, Fields extends FieldsShape, const B extends MatchInput<Fields>>(
|
|
227
|
-
relation: Relation<Name, Fields>,
|
|
228
|
-
bindings: B
|
|
229
|
-
): MatchAtom<BindingsParams<B>> {
|
|
230
|
-
return Object.freeze({
|
|
231
|
-
item: "atom" as const,
|
|
232
|
-
negated: false,
|
|
233
|
-
source: Object.freeze({ kind: "relation" as const, relation }),
|
|
234
|
-
bindings: resolveBindings(
|
|
235
|
-
`relation ${relation.name}`,
|
|
236
|
-
relation.data.fields,
|
|
237
|
-
Object.fromEntries(Object.entries(bindings))
|
|
238
|
-
)
|
|
239
|
-
})
|
|
246
|
+
/** One condition-tree VALUE (`and`/`or` over comparisons and nested trees). */
|
|
247
|
+
interface Tree<Ch extends readonly AnyTreeChild[]> {
|
|
248
|
+
readonly cond: "tree"
|
|
249
|
+
readonly op: "and" | "or"
|
|
250
|
+
readonly children: Ch
|
|
240
251
|
}
|
|
241
252
|
|
|
242
253
|
/**
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
* variable bound only inside the atom plus a rule-level OR — which is
|
|
249
|
-
* exactly the shape the safety rule rejects, and semantically wrong for
|
|
250
|
-
* negation anyway (¬∃(f = a ∨ f = b) is a CONJUNCTION of negated atoms,
|
|
251
|
-
* ¬∃(f = a) ∧ ¬∃(f = b), never one negated atom's OR).
|
|
254
|
+
* One negated-atom VALUE — negation is a position in the rule (anti-join
|
|
255
|
+
* over sets, no null trick): a binding satisfies it iff NO fact matches.
|
|
256
|
+
* Its variables must be positively bound in the rule — environment
|
|
257
|
+
* membership at `.where` IS the safety rule, a compile error before it is
|
|
258
|
+
* the engine's refusal.
|
|
252
259
|
*/
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
for (const binding of atom.bindings) {
|
|
258
|
-
if (binding.term.kind === "oneOf") {
|
|
259
|
-
const source = atom.source.kind === "relation" ? atom.source.relation.name : `predicate ${atom.source.pred.name}`
|
|
260
|
-
throw errors.new(
|
|
261
|
-
`negated ${source} atom binds ${binding.field} with oneOf(...) — ¬∃(${binding.field} = a ∨ ${binding.field} = b) means ¬∃(${binding.field} = a) ∧ ¬∃(${binding.field} = b): write one not(match(...)) per literal, or bind a paramSet`
|
|
262
|
-
)
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return Object.freeze({
|
|
266
|
-
item: "atom" as const,
|
|
267
|
-
negated: true,
|
|
268
|
-
source: atom.source,
|
|
269
|
-
bindings: atom.bindings
|
|
270
|
-
})
|
|
260
|
+
interface NotAtom<F extends FieldsShape, B> {
|
|
261
|
+
readonly cond: "not"
|
|
262
|
+
readonly relation: Relation<string, F>
|
|
263
|
+
readonly bindings: B
|
|
271
264
|
}
|
|
272
265
|
|
|
273
|
-
/**
|
|
274
|
-
|
|
275
|
-
return Object.freeze({ item: "cmp" as const, op: Object.freeze(op), lhs, rhs })
|
|
276
|
-
}
|
|
266
|
+
/** Any comparison value. */
|
|
267
|
+
type AnyCmp = Cmp<CmpKind, unknown, unknown, unknown>
|
|
277
268
|
|
|
278
|
-
/**
|
|
279
|
-
|
|
280
|
-
return typeof value === "object" && value !== null && "measure" in value && !isTerm(value)
|
|
281
|
-
}
|
|
269
|
+
/** Any condition-tree child (trees hold comparisons and trees — never atoms). */
|
|
270
|
+
type AnyTreeChild = AnyCmp | Tree<readonly AnyTreeChild[]>
|
|
282
271
|
|
|
283
|
-
/**
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
272
|
+
/** Any negated-atom value. */
|
|
273
|
+
type AnyNotAtom = NotAtom<FieldsShape, unknown>
|
|
274
|
+
|
|
275
|
+
/** Any `.where` input: a comparison, a condition tree, or a negated atom. */
|
|
276
|
+
type AnyCond = AnyCmp | Tree<readonly AnyTreeChild[]> | AnyNotAtom
|
|
277
|
+
|
|
278
|
+
/** What `eq`'s right side accepts (`ParamSet` is `Eq`-only — the IR's rule). */
|
|
279
|
+
type EqRight = Var<string> | Param<string> | SetParam<string> | bigint | string | boolean | Uint8Array | IntervalValue
|
|
280
|
+
|
|
281
|
+
/** What `ne`'s right side accepts. */
|
|
282
|
+
type NeRight = Var<string> | Param<string> | bigint | string | boolean | Uint8Array | IntervalValue
|
|
283
|
+
|
|
284
|
+
/** One side of an order comparison: orderable terms only (the IR's comparison rules). */
|
|
285
|
+
type OrderSide = Var<string> | Param<string> | Duration<string> | bigint
|
|
286
|
+
|
|
287
|
+
/** The point side of `pointIn`/`covers`. */
|
|
288
|
+
type PointSide = Var<string> | Param<string> | bigint
|
|
289
|
+
|
|
290
|
+
/** The interval side of `pointIn`/`covers`/`allen`. */
|
|
291
|
+
type IntervalSide = Var<string> | Param<string> | IntervalValue
|
|
292
|
+
|
|
293
|
+
/** Builds one comparison value. */
|
|
294
|
+
function comparison<Op extends CmpKind, L, R, M>(op: Op, lhs: L, rhs: R, mask: M): Cmp<Op, L, R, M> {
|
|
295
|
+
return Object.freeze({ cond: "cmp", op, lhs, rhs, mask })
|
|
292
296
|
}
|
|
293
297
|
|
|
294
298
|
/**
|
|
295
299
|
* Rejects a comparison with no term side: it is constant-valued, the
|
|
296
|
-
* engine's own validation refuses it, and the lowering has no
|
|
300
|
+
* engine's own validation refuses it, and the lowering has no anchored
|
|
297
301
|
* position to type the literals by — fail here with the same verdict.
|
|
298
302
|
*/
|
|
299
|
-
function assertTermSide(op: string, lhs:
|
|
300
|
-
if (lhs
|
|
303
|
+
function assertTermSide(op: string, lhs: unknown, rhs: unknown): void {
|
|
304
|
+
if (!isTerm(lhs) && !isTerm(rhs)) {
|
|
301
305
|
throw errors.new(
|
|
302
306
|
`${op}: a comparison without a variable or parameter side is constant-valued — write the query you mean`
|
|
303
307
|
)
|
|
@@ -305,100 +309,73 @@ function assertTermSide(op: string, lhs: CmpTerm, rhs: CmpTerm): void {
|
|
|
305
309
|
}
|
|
306
310
|
|
|
307
311
|
/**
|
|
308
|
-
* The equality
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
312
|
+
* The equality comparison (`ir::CmpOp::Eq`) — a bound variable against a
|
|
313
|
+
* variable (var-to-var unification, domain-equal by the environment
|
|
314
|
+
* check), a param (typed by the variable), an ∈-set param (`Eq`-only, the
|
|
315
|
+
* IR's set rule), or a bare literal of the variable's own value type.
|
|
316
|
+
* Prefer direct placement in `match` where punning applies.
|
|
313
317
|
*/
|
|
314
|
-
function
|
|
315
|
-
left
|
|
316
|
-
right: R
|
|
317
|
-
): ComparisonItem<ParamsShape<TermContribution<R>>> {
|
|
318
|
-
return comparison(Object.freeze({ kind: "eq" as const }), cmpTermOf(left), cmpTermOf(right))
|
|
318
|
+
function eq<L extends Var<string>, const R extends EqRight>(left: L, right: R): Cmp<"eq", L, R> {
|
|
319
|
+
return comparison("eq", left, right, undefined)
|
|
319
320
|
}
|
|
320
321
|
|
|
321
322
|
/** Typed disequality (`ir::CmpOp::Ne`). "Not in set" has no operator — write a negated atom. */
|
|
322
|
-
function ne<
|
|
323
|
-
left
|
|
324
|
-
right: R
|
|
325
|
-
): ComparisonItem<ParamsShape<TermContribution<R>>> {
|
|
326
|
-
return comparison(Object.freeze({ kind: "ne" as const }), cmpTermOf(left), cmpTermOf(right))
|
|
323
|
+
function ne<L extends Var<string>, const R extends NeRight>(left: L, right: R): Cmp<"ne", L, R> {
|
|
324
|
+
return comparison("ne", left, right, undefined)
|
|
327
325
|
}
|
|
328
326
|
|
|
329
|
-
/**
|
|
330
|
-
* One side of an order comparison: a u64/i64-typed var or param, a bigint
|
|
331
|
-
* literal, or the measure (`duration(v)`) — order operators are legal for
|
|
332
|
-
* the orderable types only, never intervals/bytes/strings/bools (the IR's
|
|
333
|
-
* comparison rules; each refusal is the engine's own typed diagnostic).
|
|
334
|
-
*/
|
|
335
|
-
type OrderInput = Var<bigint> | Param<string, bigint> | bigint | Duration
|
|
336
|
-
|
|
337
327
|
/** The shared order-comparison constructor. */
|
|
338
|
-
function order<const L extends
|
|
339
|
-
op:
|
|
328
|
+
function order<Op extends "lt" | "le" | "gt" | "ge", const L extends OrderSide, const R extends OrderSide>(
|
|
329
|
+
op: Op,
|
|
340
330
|
left: L,
|
|
341
331
|
right: R
|
|
342
|
-
):
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
assertTermSide(op, lhs, rhs)
|
|
346
|
-
return comparison(Object.freeze({ kind: op }), lhs, rhs)
|
|
332
|
+
): Cmp<Op, L, R> {
|
|
333
|
+
assertTermSide(op, left, right)
|
|
334
|
+
return comparison(op, left, right, undefined)
|
|
347
335
|
}
|
|
348
336
|
|
|
349
|
-
/** Strict less-than (`ir::CmpOp::Lt`). */
|
|
350
|
-
function lt<const L extends
|
|
351
|
-
left: L,
|
|
352
|
-
right: R
|
|
353
|
-
): ComparisonItem<ParamsShape<TermContribution<L> | TermContribution<R>>> {
|
|
337
|
+
/** Strict less-than (`ir::CmpOp::Lt`) — orderable sides only, never intervals/bytes/strings/bools. */
|
|
338
|
+
function lt<const L extends OrderSide, const R extends OrderSide>(left: L, right: R): Cmp<"lt", L, R> {
|
|
354
339
|
return order("lt", left, right)
|
|
355
340
|
}
|
|
356
341
|
|
|
357
342
|
/** Less-or-equal (`ir::CmpOp::Le`). */
|
|
358
|
-
function le<const L extends
|
|
359
|
-
left: L,
|
|
360
|
-
right: R
|
|
361
|
-
): ComparisonItem<ParamsShape<TermContribution<L> | TermContribution<R>>> {
|
|
343
|
+
function le<const L extends OrderSide, const R extends OrderSide>(left: L, right: R): Cmp<"le", L, R> {
|
|
362
344
|
return order("le", left, right)
|
|
363
345
|
}
|
|
364
346
|
|
|
365
347
|
/** Strict greater-than (`ir::CmpOp::Gt`). */
|
|
366
|
-
function gt<const L extends
|
|
367
|
-
left: L,
|
|
368
|
-
right: R
|
|
369
|
-
): ComparisonItem<ParamsShape<TermContribution<L> | TermContribution<R>>> {
|
|
348
|
+
function gt<const L extends OrderSide, const R extends OrderSide>(left: L, right: R): Cmp<"gt", L, R> {
|
|
370
349
|
return order("gt", left, right)
|
|
371
350
|
}
|
|
372
351
|
|
|
373
352
|
/** Greater-or-equal (`ir::CmpOp::Ge`). */
|
|
374
|
-
function ge<const L extends
|
|
375
|
-
left: L,
|
|
376
|
-
right: R
|
|
377
|
-
): ComparisonItem<ParamsShape<TermContribution<L> | TermContribution<R>>> {
|
|
353
|
+
function ge<const L extends OrderSide, const R extends OrderSide>(left: L, right: R): Cmp<"ge", L, R> {
|
|
378
354
|
return order("ge", left, right)
|
|
379
355
|
}
|
|
380
356
|
|
|
381
|
-
/**
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
357
|
+
/**
|
|
358
|
+
* Point membership as a predicate (`ir::CmpOp::PointIn`), membership
|
|
359
|
+
* spelling: `pointIn(t, w)` holds iff `w.start ≤ t < w.end`. The IR
|
|
360
|
+
* orders the operands interval-left, point-right; the value stores them
|
|
361
|
+
* that way whatever the surface spelling.
|
|
362
|
+
*/
|
|
363
|
+
function pointIn<const P extends PointSide, const I extends IntervalSide>(point: P, interval: I): Cmp<"pointIn", I, P> {
|
|
364
|
+
assertTermSide("pointIn", point, interval)
|
|
365
|
+
return comparison("pointIn", interval, point, undefined)
|
|
366
|
+
}
|
|
386
367
|
|
|
387
368
|
/**
|
|
388
|
-
* Point membership
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
*
|
|
369
|
+
* Point membership, coverage spelling — `covers(w, t)` is `pointIn(t, w)`
|
|
370
|
+
* with the interval written first (the IR's own operand order; a literal
|
|
371
|
+
* `span(...)` left operand is legal and tags by the point sibling's
|
|
372
|
+
* element domain — the bug-hunt fix, now also a type-level guarantee).
|
|
373
|
+
* Interval ⊇ interval is NOT this operator; that predicate is
|
|
374
|
+
* `allen(a, ALLEN.covers, b)`.
|
|
393
375
|
*/
|
|
394
|
-
function covers<const I extends
|
|
395
|
-
interval
|
|
396
|
-
point
|
|
397
|
-
): ComparisonItem<ParamsShape<TermContribution<I> | TermContribution<T>>> {
|
|
398
|
-
const lhs = cmpTermOf(interval)
|
|
399
|
-
const rhs = cmpTermOf(point)
|
|
400
|
-
assertTermSide("covers", lhs, rhs)
|
|
401
|
-
return comparison(Object.freeze({ kind: "pointIn" as const }), lhs, rhs)
|
|
376
|
+
function covers<const I extends IntervalSide, const P extends PointSide>(interval: I, point: P): Cmp<"pointIn", I, P> {
|
|
377
|
+
assertTermSide("covers", interval, point)
|
|
378
|
+
return comparison("pointIn", interval, point, undefined)
|
|
402
379
|
}
|
|
403
380
|
|
|
404
381
|
/**
|
|
@@ -444,46 +421,22 @@ const ALLEN = Object.freeze({
|
|
|
444
421
|
* THE interval-pair comparison (`ir::CmpOp::Allen`): two interval terms of
|
|
445
422
|
* one element type, satisfied iff the pair's classification is in the
|
|
446
423
|
* 13-bit mask — a literal built from the `ALLEN` constants, or a mask
|
|
447
|
-
* parameter (
|
|
424
|
+
* parameter (`r.maskParam`). Vacuous masks (empty/full) are the engine's
|
|
448
425
|
* two distinct typed rejections; nothing is pre-judged here beyond the
|
|
449
426
|
* representable bit range.
|
|
450
427
|
*/
|
|
451
|
-
function allen<
|
|
452
|
-
const A extends IntervalInput,
|
|
453
|
-
const M extends number | MaskParam<string>,
|
|
454
|
-
const B extends IntervalInput
|
|
455
|
-
>(
|
|
428
|
+
function allen<const A extends IntervalSide, const M extends number | MaskParam<string>, const B extends IntervalSide>(
|
|
456
429
|
left: A,
|
|
457
430
|
mask: M,
|
|
458
431
|
right: B
|
|
459
|
-
):
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
if (typeof maskValue === "number") {
|
|
465
|
-
if (!Number.isInteger(maskValue) || maskValue < 0 || maskValue > ALLEN_ALL_BITS) {
|
|
466
|
-
throw errors.new(
|
|
467
|
-
`allen mask ${maskValue} is not a 13-bit mask — build masks from the ALLEN constants (bumbledb allen.rs: bits above the low 13 are unrepresentable)`
|
|
468
|
-
)
|
|
469
|
-
}
|
|
470
|
-
return comparison(
|
|
471
|
-
Object.freeze({
|
|
472
|
-
kind: "allen" as const,
|
|
473
|
-
mask: Object.freeze({ kind: "literal" as const, mask: maskValue })
|
|
474
|
-
}),
|
|
475
|
-
lhs,
|
|
476
|
-
rhs
|
|
432
|
+
): Cmp<"allen", A, B, M> {
|
|
433
|
+
assertTermSide("allen", left, right)
|
|
434
|
+
if (typeof mask === "number" && (!Number.isInteger(mask) || mask < 0 || mask > ALLEN_ALL_BITS)) {
|
|
435
|
+
throw errors.new(
|
|
436
|
+
`allen mask ${mask} is not a 13-bit mask — build masks from the ALLEN constants (bumbledb allen.rs: bits above the low 13 are unrepresentable)`
|
|
477
437
|
)
|
|
478
438
|
}
|
|
479
|
-
return comparison(
|
|
480
|
-
Object.freeze({
|
|
481
|
-
kind: "allen" as const,
|
|
482
|
-
mask: Object.freeze({ kind: "param" as const, param: maskValue })
|
|
483
|
-
}),
|
|
484
|
-
lhs,
|
|
485
|
-
rhs
|
|
486
|
-
)
|
|
439
|
+
return comparison("allen", left, right, mask)
|
|
487
440
|
}
|
|
488
441
|
|
|
489
442
|
/**
|
|
@@ -492,14 +445,8 @@ function allen<
|
|
|
492
445
|
* nesting under `or`, and the empty combination keeps the IR's algebraic
|
|
493
446
|
* reading (`And([])` is true).
|
|
494
447
|
*/
|
|
495
|
-
function and<const C extends readonly
|
|
496
|
-
|
|
497
|
-
): ConditionTreeItem<ParamsShape<ItemParams<C[number]>>> {
|
|
498
|
-
return Object.freeze({
|
|
499
|
-
item: "tree" as const,
|
|
500
|
-
op: "and" as const,
|
|
501
|
-
children: Object.freeze([...children])
|
|
502
|
-
})
|
|
448
|
+
function and<const C extends readonly AnyTreeChild[]>(...children: C): Tree<C> {
|
|
449
|
+
return Object.freeze({ cond: "tree", op: "and", children: Object.freeze(children) })
|
|
503
450
|
}
|
|
504
451
|
|
|
505
452
|
/**
|
|
@@ -508,49 +455,206 @@ function and<const C extends readonly AnyCondition[]>(
|
|
|
508
455
|
* to DNF rules engine-side (OR is data or it is nothing). `Or([])` keeps
|
|
509
456
|
* its algebraic reading (false: the rule denotes nothing).
|
|
510
457
|
*/
|
|
511
|
-
function or<const C extends readonly
|
|
512
|
-
|
|
513
|
-
): ConditionTreeItem<ParamsShape<ItemParams<C[number]>>> {
|
|
514
|
-
return Object.freeze({
|
|
515
|
-
item: "tree" as const,
|
|
516
|
-
op: "or" as const,
|
|
517
|
-
children: Object.freeze([...children])
|
|
518
|
-
})
|
|
458
|
+
function or<const C extends readonly AnyTreeChild[]>(...children: C): Tree<C> {
|
|
459
|
+
return Object.freeze({ cond: "tree", op: "or", children: Object.freeze(children) })
|
|
519
460
|
}
|
|
520
461
|
|
|
521
462
|
/**
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
*
|
|
463
|
+
* Negation — anti-join over sets: `not(Rel, { field: r.var("x"), ... })`
|
|
464
|
+
* rejects every binding some matching fact extends. A negated atom binds
|
|
465
|
+
* nothing, only rejects: every variable it names must be positively bound
|
|
466
|
+
* in the rule, which `.where`'s environment check makes a COMPILE error
|
|
467
|
+
* (the engine's safety refusal stands behind it).
|
|
525
468
|
*/
|
|
526
|
-
function
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
return Object.freeze({ measure: over })
|
|
469
|
+
function not<Name extends string, F extends FieldsShape, const B extends MatchShape<F>>(
|
|
470
|
+
relation: Relation<Name, F>,
|
|
471
|
+
bindings: B
|
|
472
|
+
): NotAtom<F, B> {
|
|
473
|
+
const value: NotAtom<F, B> = { cond: "not", relation, bindings }
|
|
474
|
+
return Object.freeze(value)
|
|
533
475
|
}
|
|
534
476
|
|
|
477
|
+
/** Whether a var name is bound in the environment at an orderable (u64/i64) field. */
|
|
478
|
+
type OrderVarOk<Env extends EnvShape, N extends string> = N extends keyof Env
|
|
479
|
+
? Env[N]["kind"] extends "u64" | "i64"
|
|
480
|
+
? true
|
|
481
|
+
: false
|
|
482
|
+
: false
|
|
483
|
+
|
|
484
|
+
/** Whether a var name is bound at an interval field. */
|
|
485
|
+
type IntervalVarOk<Env extends EnvShape, N extends string> = N extends keyof Env
|
|
486
|
+
? Env[N]["kind"] extends "interval"
|
|
487
|
+
? true
|
|
488
|
+
: false
|
|
489
|
+
: false
|
|
490
|
+
|
|
491
|
+
/** One order-comparison side's judgment against the environment. */
|
|
492
|
+
type OrderSideOk<Env extends EnvShape, T> =
|
|
493
|
+
T extends Var<infer N extends string>
|
|
494
|
+
? OrderVarOk<Env, N>
|
|
495
|
+
: T extends Duration<infer N extends string>
|
|
496
|
+
? IntervalVarOk<Env, N>
|
|
497
|
+
: true
|
|
498
|
+
|
|
499
|
+
/** One point side's judgment. */
|
|
500
|
+
type PointSideOk<Env extends EnvShape, T> = T extends Var<infer N extends string> ? OrderVarOk<Env, N> : true
|
|
501
|
+
|
|
502
|
+
/** One interval side's judgment. */
|
|
503
|
+
type IntervalSideOk<Env extends EnvShape, T> = T extends Var<infer N extends string> ? IntervalVarOk<Env, N> : true
|
|
504
|
+
|
|
505
|
+
/** The `eq`/`ne` judgment: left var bound; right joins it (domain-equal var, param, or an exact-type literal). */
|
|
506
|
+
type EqOk<Env extends EnvShape, L, R> =
|
|
507
|
+
L extends Var<infer N extends string>
|
|
508
|
+
? N extends keyof Env
|
|
509
|
+
? R extends Var<infer M extends string>
|
|
510
|
+
? M extends keyof Env
|
|
511
|
+
? JoinOk<Env[N], Env[M]>
|
|
512
|
+
: false
|
|
513
|
+
: R extends Param<string> | SetParam<string>
|
|
514
|
+
? true
|
|
515
|
+
: [R] extends [Infer<Env[N]>]
|
|
516
|
+
? true
|
|
517
|
+
: false
|
|
518
|
+
: false
|
|
519
|
+
: false
|
|
520
|
+
|
|
521
|
+
/** One negated-atom binding's judgment: a var must be positively bound (safety) AND domain-equal. */
|
|
522
|
+
type NotBindingOk<Env extends EnvShape, F extends AnyField, T> =
|
|
523
|
+
T extends Var<infer N extends string> ? (N extends keyof Env ? JoinOk<Env[N], F> : false) : true
|
|
524
|
+
|
|
525
|
+
/** The whole negated atom's judgment. */
|
|
526
|
+
type NotOk<Env extends EnvShape, F extends FieldsShape, B> = false extends {
|
|
527
|
+
[K in keyof B]: NotBindingOk<Env, K extends keyof F ? F[K] : never, B[K]>
|
|
528
|
+
}[keyof B]
|
|
529
|
+
? false
|
|
530
|
+
: true
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* One condition's judgment against the rule environment — the type-level
|
|
534
|
+
* twin of the engine's comparison roster: domain-equal joins, orderable
|
|
535
|
+
* order sides (an interval var under a non-`pointIn` op is exactly here
|
|
536
|
+
* refused), kind-correct `pointIn`/`covers`/`allen` sides, and negated-atom
|
|
537
|
+
* safety. The leading `[AnyTreeChild] extends [C]` arm is the recursion's
|
|
538
|
+
* base case: at an UNRESOLVED constraint (the whole condition union — or a
|
|
539
|
+
* tree's child union, which is the union itself) the judgment is vacuously
|
|
540
|
+
* true — without it the constraint instantiation recurses into itself.
|
|
541
|
+
*/
|
|
542
|
+
type CondOkBool<Env extends EnvShape, C> = [AnyTreeChild] extends [C]
|
|
543
|
+
? true
|
|
544
|
+
: C extends Cmp<infer Op, infer L, infer R, unknown>
|
|
545
|
+
? Op extends "eq" | "ne"
|
|
546
|
+
? EqOk<Env, L, R>
|
|
547
|
+
: Op extends "lt" | "le" | "gt" | "ge"
|
|
548
|
+
? [OrderSideOk<Env, L>, OrderSideOk<Env, R>] extends [true, true]
|
|
549
|
+
? true
|
|
550
|
+
: false
|
|
551
|
+
: Op extends "pointIn"
|
|
552
|
+
? [IntervalSideOk<Env, L>, PointSideOk<Env, R>] extends [true, true]
|
|
553
|
+
? true
|
|
554
|
+
: false
|
|
555
|
+
: Op extends "allen"
|
|
556
|
+
? [IntervalSideOk<Env, L>, IntervalSideOk<Env, R>] extends [true, true]
|
|
557
|
+
? true
|
|
558
|
+
: false
|
|
559
|
+
: false
|
|
560
|
+
: C extends Tree<infer Ch extends readonly AnyTreeChild[]>
|
|
561
|
+
? false extends CondOkBool<Env, Ch[number]>
|
|
562
|
+
? false
|
|
563
|
+
: true
|
|
564
|
+
: C extends NotAtom<infer F extends FieldsShape, infer B>
|
|
565
|
+
? NotOk<Env, F, B>
|
|
566
|
+
: false
|
|
567
|
+
|
|
568
|
+
/** The validated `.where` argument (intersect with the inferred condition type). */
|
|
569
|
+
type CheckCond<Env extends EnvShape, C> = CondOkBool<Env, C> extends true ? C : never
|
|
570
|
+
|
|
571
|
+
/** The `eq`/`ne` params contribution: the param typed by the left variable's field. */
|
|
572
|
+
type EqParams<Env extends EnvShape, L, R> =
|
|
573
|
+
L extends Var<infer N extends string>
|
|
574
|
+
? R extends Param<infer P extends string>
|
|
575
|
+
? { readonly [Q in P]: Infer<Env[N & keyof Env]> }
|
|
576
|
+
: R extends SetParam<infer P extends string>
|
|
577
|
+
? { readonly [Q in P]: readonly Infer<Env[N & keyof Env]>[] }
|
|
578
|
+
: never
|
|
579
|
+
: never
|
|
580
|
+
|
|
581
|
+
/** An order side's params contribution (order params are always `bigint`). */
|
|
582
|
+
type OrderSideParams<T> = T extends Param<infer P extends string> ? { readonly [Q in P]: bigint } : never
|
|
583
|
+
|
|
584
|
+
/** An interval side's params contribution. */
|
|
585
|
+
type IntervalSideParams<T> = T extends Param<infer P extends string> ? { readonly [Q in P]: IntervalValue } : never
|
|
586
|
+
|
|
587
|
+
/** The mask position's params contribution. */
|
|
588
|
+
type MaskParams<M> = M extends MaskParam<infer P extends string> ? { readonly [Q in P]: number } : never
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* One condition's params-object fragments (a union; the rule builder folds
|
|
592
|
+
* them into the inferred `Params` record) — every param typed by its use.
|
|
593
|
+
* The leading arm is the same base case as {@link CondOkBool}'s: the
|
|
594
|
+
* unresolved constraint contributes nothing.
|
|
595
|
+
*/
|
|
596
|
+
type CondParams<Env extends EnvShape, C> = [AnyTreeChild] extends [C]
|
|
597
|
+
? never
|
|
598
|
+
: C extends Cmp<infer Op, infer L, infer R, infer M>
|
|
599
|
+
? Op extends "eq" | "ne"
|
|
600
|
+
? EqParams<Env, L, R>
|
|
601
|
+
: Op extends "lt" | "le" | "gt" | "ge"
|
|
602
|
+
? OrderSideParams<L> | OrderSideParams<R>
|
|
603
|
+
: Op extends "pointIn"
|
|
604
|
+
? IntervalSideParams<L> | OrderSideParams<R>
|
|
605
|
+
: Op extends "allen"
|
|
606
|
+
? IntervalSideParams<L> | IntervalSideParams<R> | MaskParams<M>
|
|
607
|
+
: never
|
|
608
|
+
: C extends Tree<infer Ch extends readonly AnyTreeChild[]>
|
|
609
|
+
? CondParams<Env, Ch[number]>
|
|
610
|
+
: C extends NotAtom<infer F extends FieldsShape, infer B>
|
|
611
|
+
? BindParams<F, B>
|
|
612
|
+
: never
|
|
613
|
+
|
|
614
|
+
/** The flattened params record one bindings record contributes. */
|
|
615
|
+
type BindParamsShape<F extends FieldsShape, B> = ShapeOf<BindParams<F, B>>
|
|
616
|
+
|
|
617
|
+
/** The flattened params record one condition contributes. */
|
|
618
|
+
type CondParamsShape<Env extends EnvShape, C> = ShapeOf<CondParams<Env, C>>
|
|
619
|
+
|
|
535
620
|
export type {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
621
|
+
AggData,
|
|
622
|
+
AnyCmp,
|
|
623
|
+
AnyCond,
|
|
624
|
+
AnyNotAtom,
|
|
625
|
+
AnyTreeChild,
|
|
626
|
+
AtomData,
|
|
627
|
+
BindEnv,
|
|
539
628
|
BindingEntry,
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
629
|
+
BindingInput,
|
|
630
|
+
BindingTermData,
|
|
631
|
+
BindParams,
|
|
632
|
+
BindParamsShape,
|
|
633
|
+
CheckBindings,
|
|
634
|
+
CheckCond,
|
|
635
|
+
Cmp,
|
|
636
|
+
CmpData,
|
|
637
|
+
CmpKind,
|
|
638
|
+
CmpTermData,
|
|
639
|
+
CondData,
|
|
640
|
+
CondOkBool,
|
|
641
|
+
CondParams,
|
|
642
|
+
CondParamsShape,
|
|
643
|
+
IntervalSide,
|
|
644
|
+
IntervalVarOk,
|
|
548
645
|
MaskData,
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
646
|
+
MatchShape,
|
|
647
|
+
NotAtom,
|
|
648
|
+
OrderSide,
|
|
649
|
+
OrderVarOk,
|
|
650
|
+
ParamUse,
|
|
651
|
+
PointSide,
|
|
652
|
+
RecData,
|
|
653
|
+
RuleData,
|
|
654
|
+
RuleItem,
|
|
655
|
+
SelectColumn,
|
|
656
|
+
SelectEntryData,
|
|
657
|
+
Tree,
|
|
658
|
+
TreeData
|
|
555
659
|
}
|
|
556
|
-
export { ALLEN, allen, and, covers,
|
|
660
|
+
export { ALLEN, allen, and, comparison, covers, eq, ge, gt, le, lt, ne, not, or, pointIn }
|