1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! The frontier-NFA compiler (ADR-0478 Decision 1): the plan-time
//! Thompson construction that lowers the one `Quantified` element of a
//! path pattern — the element every var-length spelling lowers to since
//! ADR-0471, so every spelling reaches this compiler — into a small NFA
//! over interned rel-type ids: the shape the frontier BFS runs on, the
//! production form of the research prototype's hand-built `Pattern`
//! (`graph/qpp.rs`, ADR-0476).
//!
//! Model (the prototype's, generalized):
//!
//! - states `0..states`, a `start` state, and rel-consuming transitions
//! `(from --type-set--> to)` carrying the per-hop direction tag
//! (`Direction::Outgoing/Incoming/Both`, honored per transition, never
//! globally — the prototype is undirected-only);
//! - `accepting[s]`: the walk can complete at `s` (the final state, or a
//! state that epsilon-reaches it); each transition's `accepts` flag is
//! that table hoisted onto the transition — the prototype's hot-loop
//! shape, derived from the one table, never a second fact;
//! - epsilon edges — rel-free skips — exist only where bounded
//! quantifiers unroll with optional copies (or a zero-copy
//! pass-through); the prototype's two families compile with none;
//! - hop bounds `{min, max}` over the whole run, `max = None` running to
//! pair-saturation fixpoint.
//!
//! Compile families:
//!
//! - **flat** (`-[r:T*m..n]-`, the `(A|B)+` union): one self-loop state
//! over the interned type union — the prototype's `Pattern::hops`;
//! - **group** (`((a)-[:A]-(b)-[:B]-(c)){m,n}`): the chained cycle, state
//! *i* --step *i*--> *i+1* with the last step closing back to the entry
//! state, hop bounds `rounds × steps.len()` — the prototype's
//! `Pattern::rounds`;
//! - **concatenation context** (plain sibling rels around the quantified
//! element, the scan+segment classes): one more transition in the same
//! chain, the bounds offset by the fixed context hops;
//! - **nested quantifiers** (a var-length rel inside a group — the front
//! end quantifies top level only, `quantify_var_length`, so a nested
//! group spelling keeps its `Relationship` element): bounded ones
//! unroll structurally, exact copies with the optional tail skipped by
//! epsilon — never cycles.
//!
//! The soundness rule every form shares: the global hop interval may pin
//! the count of at most ONE cycle, and only when every traversal of that
//! cycle is the same fixed length (n rounds are then exactly n·L hops,
//! so the interval recovers the round count). A nested unbounded loop,
//! or a variable-length round under an unbounded outer quantifier,
//! would leave some instance's count unpinned — one instance skimping
//! compensated by another's excess — so both decline and the per-row DFS
//! keeps the pattern, answers identical.
//!
//! Every decline is structural, never an assert: state count above the
//! 64-state bitmask ceiling (the prototype's `assert!` is a decline
//! here), selectors, subpath variables, predicates on inner rels,
//! non-static type lists, a type name the store never interned, and
//! anything that breaks the node/hop alternation. An *empty* type list is
//! not a decline: it is the untyped hop, and it compiles as the any-type
//! set (see `NfaTransition::types`). The two admission
//! declines are switch-gated (ADR-0519): under `auto` an interior or
//! inner node carrying admission facts declines exactly as before (the
//! v1 NFA has no node-admission surface; the boundary nodes' admission
//! is the caller's), and so does a quantified group's own `WHERE`; under
//! the forced `nfa` rung both gap classes compile instead —
//!
//! - **class K, per-state guards**: an interior node's labels, label
//! expression, and property map become the guard of the state its
//! position occupies — the node pattern itself, checked at every
//! arrival by the walker's own `node_ok` (exact pair-level semantics:
//! a node either satisfies the position's pattern or not). A group's
//! boundary positions alias — the body's first and last nodes share
//! their state with the enclosing walk's nodes — so the guarded group
//! compiles to a **product form**: the boundary is split into an
//! `open` state (every round's start, the body's first pattern) and a
//! `close` state (every round's end, the body's last pattern),
//! stitched into the cycle by epsilon edges — the Thompson
//! construction's own glue, consuming neither hop nor rel — and a
//! guarded post-group node takes its own `exit` state, entered by
//! epsilon from the close once the group is finished; so every state
//! carries one pattern and the per-state check stays exact.
//! - **class F, transition guards**: a quantified group whose `WHERE`
//! references its own pattern variables (the fraud f03 shape)
//! compiles with the predicate riding the body's round-closing
//! transition, read at each arrival against the referenced variables'
//! **last bindings** — the arrival node, the departure node, or the
//! first-arrival chain's most recent cell at the variable's state.
//! The compile admits only plain one-hop-rel bodies here (the unroll
//! path's copy stitching has no guard positions), and the serving
//! gates keep every predicate-guarded clause on the DFS
//! (`frontier_match`): the pair-dedup answers these shapes
//! approximately, so no consumer proof covers serving them.
use crateGraph;
use cratePatternEngine;
use ;
use Builder;
use ;
/// The state ceiling: the frontier's dedup set is one `u64` bitmask per
/// node slot (the prototype's `seen: Vec<u64>`), so an NFA that would
/// need more than 64 states declines — ADR-0478 Decision 1 turns the
/// prototype's `assert!(states <= 64)` into a structural decline.
pub const STATE_CEILING: u32 = 64;
/// The pool a frontier run answers in — the family's shared vocabulary,
/// set once by the matcher on the contract
/// (`FrontierContract::pool`, the typed field) and read by every driver:
/// `Walk` for the bounded WALK spelling (the frontier's native semantics
/// for in-bounds arrivals, the re-arrival rule's walk-length check for
/// its below-min candidates, ADR-0478 §3), `Default` for everything else
/// (the default pool's trail exactness is the rule's jurisdiction,
/// ADR-0478 §3).
pub
/// One rel-consuming transition: the prototype's shape (from, to, the
/// interned type union, the hoisted accepts flag) plus the per-hop
/// direction tag the production transitions carry.
pub
/// A quantified path pattern as a small NFA plus hop bounds — the
/// production form of the prototype's `Pattern` (`graph/qpp.rs`).
pub
/// The one-cycle shape fact the compile requires and the ladder rung's
/// cheap pre-gate (`frontier_run::try_frontier_rows`) shares: exactly one
/// top-level `Quantified` element — a juxtaposed second group (the parser
/// allows `(x)((a)-->(b))*((c)-->(d))*`) would be a second cycle the hop
/// interval cannot pin. One pass over the elements, never the graph.
pub
/// Compile `pattern` into the frontier NFA, or `None` on any shape out
/// of the vocabulary above — the decline the caller turns into "keep the
/// per-row DFS". `engine` is the rung the caller resolved
/// ([`PatternEngine::Nfa`] compiles the gap classes; any other value the
/// v1 vocabulary) — the compiler never reads the switch itself.
pub
/// The epsilon edges indexed by from state.
/// Each state's epsilon closure over the from-indexed skip list: the
/// state itself plus everything reachable through skips.
/// The plain one-hop rel of the vocabulary: a static type disjunction
/// (the `(A|B)` union is one type list, not alternation IR), no
/// predicates, exactly one hop. Direction and the variable name ride the
/// transition and the row binding — neither is constrained here.
///
/// An empty type list is the untyped hop (`-[*1..]-`), and it compiles:
/// the transition carries no interned id, and `arms` resolves that empty
/// list to `RelTypes::Any` — the store's own encoding for "every type
/// matches", which is what an untyped hop means. Nothing has to
/// enumerate the interned vocabulary, so the decline this used to carry
/// (LABS-1434) is gone.
/// The quantifier admissibility every quantifier consults — the spine's
/// own gate and both nested spellings' (the chain's nested group arm and
/// the nested var-length rel's normalized form), one spelling: no
/// subpath variable, no per-iteration WHERE (the forced rung admits the
/// spine's own — it compiles to a transition guard; every nested one
/// keeps the decline), and `max >= min` when bounded. Boundedness itself
/// is the caller's fact (the spine may run unbounded; a nested quantifier
/// may not).
/// The quantified form of a nested var-length rel — the same
/// normalization the front end applies to top-level var-length rels
/// (`quantify_var_length`, ADR-0471): one clamped plain rel between two
/// implicit anonymous boundary nodes, the `*m..n` bounds moved onto the
/// quantifier. The front end quantifies top level only (a var-length
/// rel nested inside a group keeps its `Relationship` element), so the
/// compiler applies the normalization here — one quantifier code path
/// for both spellings, and [`quantified_admissible`] reads it like any
/// other quantifier.