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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! The walk's shorter forms change no answer (the review of
//! 2026-11-23). A quantified group is walked exactly, one walk per
//! history, or in a form that has fewer walks in flight: the set walk
//! (one arrival per node), the merge under an acyclic promise, the
//! search by total under a cost budget. Each asks what the group
//! reads before it runs, and a read that tells two walks to one node
//! apart takes the exact walk: the same question written with a
//! count, which is always exact, must answer the same.
//!
//! Beside them: two walks of two totals are two walks under a budget,
//! a budget weighs the legs of a group nested inside it, each level
//! keeping its own total, a tie cut at the quantifier bound is said,
//! and `edges` refuses what is written on a group and not read.
use std::path::{Path, PathBuf};
const REFS: &str = "ref /links/*::from --> /nodes/*[::name = $];\n\
ref /links/*::to --> /nodes/*[::name = $];\n";
/// A graph by its edges, each `(from, to, fare)`; the nodes are the
/// names in order of first mention.
fn graph(edges: &[(&str, &str, u32)]) -> String {
let mut names: Vec<&str> = Vec::new();
for (a, b, _) in edges {
for n in [a, b] {
if !names.contains(n) {
names.push(n);
}
}
}
let nodes: Vec<String> = names
.iter()
.map(|n| format!(r#"{{"name": "{n}"}}"#))
.collect();
let links: Vec<String> = edges
.iter()
.map(|(a, b, f)| format!(r#"{{"from": "{a}", "to": "{b}", "kind": "x", "fare": {f}}}"#))
.collect();
format!(
r#"{{"nodes": [{}], "links": [{}]}}"#,
nodes.join(", "),
links.join(", ")
)
}
fn fixture(tag: &str, edges: &[(&str, &str, u32)]) -> PathBuf {
let dir = std::env::temp_dir().join(format!("quarb-walkforms-{}-{tag}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("g.json"), graph(edges)).unwrap();
let edge = "edge /links/*: ::from -- ::to as ::kind weights(::fare<_EUR>)";
std::fs::write(dir.join("plain.model"), format!("{REFS}{edge};\n")).unwrap();
std::fs::write(
dir.join("acyclic.model"),
format!("{REFS}{edge} acyclic;\n"),
)
.unwrap();
dir
}
fn run(dir: &Path, pre: &[&str], model: &str, query: &str) -> Result<Vec<String>, String> {
let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
.env("QUARB_LOCALE", "C")
.current_dir(dir)
.args(pre)
.args(["--model", model, query, "g.json"])
.stdin(std::process::Stdio::null())
.output()
.unwrap();
match out.status.success() {
true => Ok(String::from_utf8(out.stdout)
.unwrap()
.lines()
.map(str::to_string)
.collect()),
false => Err(String::from_utf8_lossy(&out.stderr).to_string()),
}
}
fn lines(dir: &Path, model: &str, query: &str) -> Vec<String> {
run(dir, &[], model, query).unwrap_or_else(|e| panic!("{query}: {e}"))
}
fn refused(dir: &Path, pre: &[&str], model: &str, query: &str) -> String {
match run(dir, pre, model, query) {
Ok(out) => panic!("{query}: expected a refusal, got {out:?}"),
Err(e) => e,
}
}
const FROM_A: &str = r#"/nodes/*[::name = "A"]"#;
/// X is one step from A and two through B; Y is one step past X.
const TWO_WAYS: &[(&str, &str, u32)] =
&[("A", "X", 5), ("A", "B", 1), ("B", "X", 1), ("X", "Y", 1)];
/// A step's condition that reads the repetition tells the two
/// arrivals at X apart: the second repetition admits X alone, so Y is
/// reached only through the arrival of the second repetition. The
/// set walk kept the first arrival and lost Y.
#[test]
fn a_steps_condition_on_the_repetition_takes_the_exact_walk() {
let d = fixture("iter", TWO_WAYS);
let body = r#"->*[$iter != 2 || ::name = "X"]"#;
let counted = lines(
&d,
"plain.model",
&format!("{FROM_A}({body}){{1;5}} @| collapse | ::name"),
);
assert_eq!(counted, ["X", "B", "Y"]);
assert_eq!(
lines(
&d,
"plain.model",
&format!("{FROM_A}({body})+ @| collapse | ::name")
),
counted
);
assert_eq!(
lines(
&d,
"plain.model",
&format!(r#"{FROM_A}({body})+[::name = "Y"]?::name"#)
),
["Y"]
);
// and under a budget the search by total is not taken: the
// lightest walk to Y within two repetitions goes by X at 5
for fact in ["$iter", "$steps"] {
assert_eq!(
lines(
&d,
"plain.model",
&format!(r#"{FROM_A}(->*[{fact} <= 2]){{;300_EUR}}[::name = "Y"]? | $total"#)
),
["6_EUR"],
"{fact}"
);
}
}
/// The search by total settles a node at its least total whatever
/// the walk recorded. A guard that reads the record is asked of the
/// dearer walk too: the walk whose first node is X costs 6, and the
/// lightest walk to Y, 3, goes by B.
#[test]
fn a_guard_on_what_the_walk_recorded_takes_the_exact_walk() {
let d = fixture("capsa", TWO_WAYS);
for braces in ["{1;5}{;300_EUR}", "{;300_EUR}"] {
assert_eq!(
lines(
&d,
"plain.model",
&format!(r#"{FROM_A}(->* .(::name)){braces}[::name = "Y" && $.1 = "X"]? | $total"#)
),
["6_EUR"],
"{braces}"
);
}
// a guard on the node alone is still searched, and answers the
// lightest
assert_eq!(
lines(
&d,
"plain.model",
&format!(r#"{FROM_A}(->* .(::name)){{;300_EUR}}[::name = "Y"]? | $total"#)
),
["3_EUR"]
);
}
/// An acyclic promise changes no answer: two histories of two and
/// three steps reach Y by one edge in one outer repetition, and a
/// guard on the count of steps tells them apart. The promise's merge
/// folded them and the guard was asked of one.
#[test]
fn a_guard_on_the_step_count_is_not_merged_under_the_promise() {
let d = fixture("promise", TWO_WAYS);
for q in [
format!("{FROM_A}((->x){{1;2}}){{2}}[$steps = 3]?::name"),
format!("{FROM_A}((->x){{1;2}}){{2}}[$steps = 3] @| collapse | ::name"),
] {
let plain = lines(&d, "plain.model", &q);
assert_eq!(plain, ["Y"], "{q}");
assert_eq!(lines(&d, "acyclic.model", &q), plain, "{q}");
}
}
/// Two walks cross A, B, C, D to E by the edge from D: one costs 4,
/// one 12. They are two walks under a budget, whose totals differ;
/// folded, a higher ceiling lost the cheaper one.
#[test]
fn two_walks_of_two_totals_are_two_under_a_budget() {
let d = fixture(
"totals",
&[
("A", "C", 5),
("A", "B", 1),
("B", "C", 1),
("C", "B", 1),
("C", "D", 1),
("B", "D", 5),
("D", "E", 1),
],
);
let mut totals = lines(
&d,
"plain.model",
&format!(r#"{FROM_A}(->*){{;300_EUR}}[::name = "E"] | $total"#),
);
totals.sort();
assert_eq!(totals, ["12_EUR", "4_EUR", "7_EUR", "7_EUR"]);
// the lightest is 4 in every form
for braces in ["{;300_EUR}", "{1;9}{;300_EUR}", "{4}{;300_EUR}"] {
assert_eq!(
lines(
&d,
"plain.model",
&format!(r#"{FROM_A}(->*){braces}[::name = "E"]? | $total"#)
),
["4_EUR"],
"{braces}"
);
}
// and a ceiling of 11 or of 12 keeps it
for ceiling in [11, 12] {
let found = lines(
&d,
"plain.model",
&format!(r#"{FROM_A}(->*){{;{ceiling}_EUR}}[::name = "E"] | $total"#),
);
assert!(found.contains(&"4_EUR".to_string()), "{ceiling}: {found:?}");
}
}
/// A budget weighs every leg walked under it, those of a group nested
/// inside among them (the user, 2026-11-23, of the refusal 0.74 gave
/// such a form: "OK, make impl enqueue it for 0.75"). The nested walk
/// begins costs of its own and the walk around it keeps its own
/// beside: `$total` reads the nearest group's, `$total2` the group's
/// around it. Until 0.74 the outer budget bounded nothing and
/// `$total` read the first leg.
#[test]
fn a_budget_weighs_the_legs_of_a_group_nested_inside() {
let d = fixture(
"nested",
&[
("A", "B", 10),
("B", "C", 10),
("C", "D", 10),
("D", "E", 10),
("E", "F", 10),
],
);
let run = |q: &str| lines(&d, "plain.model", &format!("{FROM_A}{q}"));
// each nested form answers as the same walk written in one group
for (nested, flat) in [
("((->x){1}){;15_EUR}::name", "(->x){;15_EUR}::name"),
("((->x){1}){;25_EUR} | $total", "(->x){;25_EUR} | $total"),
(
"((->x){1}){20_EUR;35_EUR} | %(n = ::name; t = $total)",
"(->x){20_EUR;35_EUR} | %(n = ::name; t = $total)",
),
(
"(->x (->x){1}){;45_EUR} | $total",
"(->x ->x){;45_EUR} | $total",
),
] {
let answer = run(flat);
assert!(!answer.is_empty(), "{flat}");
assert_eq!(run(nested), answer, "{nested}");
}
assert_eq!(run("((->x){1}){;15_EUR}::name"), ["A", "B"]);
// the inner group takes one step or two, and the outer ceiling
// ends the walk that would take the third leg
assert_eq!(
run("(->x (->x){1;2}){;35_EUR} | %(n = ::name; t = $total)"),
[
r#"%(n = "A"; t = 0_EUR)"#,
r#"%(n = "C"; t = 20_EUR)"#,
r#"%(n = "D"; t = 30_EUR)"#,
]
);
// A budget at each level: the inner walk begins anew in every
// repetition of the outer one, which sums them all. C is two
// repetitions of one leg, 10_EUR under the inner brace and
// 20_EUR under the outer.
assert_eq!(
run(
"((->x .p(%(i = $total; o = $total2))){1;}{;15_EUR}){;25_EUR} \
| %(n = ::name; p = $.p ?? null)"
),
[
r#"%(n = "A"; p = null)"#,
r#"%(n = "B"; p = %(i = 10_EUR; o = 10_EUR))"#,
r#"%(n = "C"; p = %(i = 10_EUR; o = 20_EUR))"#,
]
);
// the outer total in the inner group's guard, and the outer
// braces' costs as a list: a count brace's steps, a budget's cost
assert_eq!(
run("((->x){1;2}[$total2 >= 20_EUR]){;45_EUR} | %(n = ::name; t = $total)"),
[
r#"%(n = "A"; t = 0_EUR)"#,
r#"%(n = "C"; t = 20_EUR)"#,
r#"%(n = "D"; t = 30_EUR)"#,
r#"%(n = "E"; t = 40_EUR)"#,
]
);
assert_eq!(
run("((->x .o(@total2)){1;2}){1}{;35_EUR} | %(n = ::name; o = $.o)"),
[
r#"%(n = "B"; o = @(1; 10_EUR))"#,
r#"%(n = "C"; o = @(2; 20_EUR))"#,
]
);
// a budget on the inner group alone weighs its own, as before
assert_eq!(run("((->x){;15_EUR}){1}::name"), ["A", "B"]);
}
/// A repetition of a group in which no step is walked adds no walk,
/// and the walk does not go on from it (the user, 2026-11-25: "yes,
/// first reading"). A group nested inside may admit a walk of no
/// steps: a quantifier around it repeated the standing path to the
/// quantifier bound and was refused there. The empty repetition
/// fills the quantifier's floor all the same, the pattern read as a
/// regular expression over the edges, each sequence of edges one
/// walk; it is never counted, so `$iter` is the repetitions that
/// walked a step, zero at the start.
#[test]
fn a_repetition_that_walks_no_step_adds_no_walk() {
let d = fixture("nostep", &[("A", "B", 10), ("B", "C", 10), ("C", "D", 10)]);
let run = |q: &str| lines(&d, "plain.model", &format!("{FROM_A}{q}"));
let all = ["A", "B", "C", "D"];
// as before the rule
assert_eq!(run("(->x)*::name"), all);
assert_eq!(run("((->x)+)+::name"), ["B", "C", "D"]);
assert_eq!(run("((->x){1;})*::name"), all);
assert_eq!(run("((->x)*){1;3}::name"), all);
assert_eq!(run("((->x)*){2}::name"), all);
// refused at the quantifier bound until the rule
assert_eq!(run("((->x)*)+::name"), all);
assert_eq!(run("((->x)*)*::name"), all);
assert_eq!(run("((->x){;2})+::name"), all);
// The counters: the division with the fewest repetitions, the
// empty ones not counted. (`{2}` read 2 at every node, the start
// among them, two empty repetitions.)
assert_eq!(
run("((->x)*){2} | %(n = ::name; i = $iter; s = $steps)"),
[
r#"%(n = "A"; i = 0; s = 0)"#,
r#"%(n = "B"; i = 1; s = 1)"#,
r#"%(n = "C"; i = 1; s = 2)"#,
r#"%(n = "D"; i = 1; s = 3)"#,
]
);
// What an empty repetition pushed goes with it: the start
// carries no push, and B is reached by one repetition alone.
// (The start answered twice, with 1 and with 2.)
assert_eq!(
run("((->x)* .p($iter)){1;2} | %(n = ::name; p = $.p ?? null)"),
[
r#"%(n = "A"; p = null)"#,
r#"%(n = "B"; p = 1)"#,
r#"%(n = "C"; p = 1)"#,
r#"%(n = "D"; p = 1)"#,
r#"%(n = "C"; p = 2)"#,
r#"%(n = "D"; p = 2)"#,
]
);
// a budget at each level with no floor inside answers as its
// floored form does
assert_eq!(
run("((->x){;15_EUR}){;25_EUR}::name"),
run("((->x){1;}{;15_EUR}){;25_EUR}::name")
);
assert_eq!(run("((->x){;15_EUR}){;25_EUR}::name"), ["A", "B", "C"]);
// the nearest and the farthest
assert_eq!(run("((->x)*)+?::name"), ["A"]);
assert_eq!(run("((->x)*)+!::name"), ["D"]);
// The quantifier bound is for a walk that really grows past it:
// a chain of forty links under an exact walk is refused as ever.
let names: Vec<String> = (0..=40).map(|i| format!("N{i}")).collect();
let chain: Vec<(&str, &str, u32)> = names
.windows(2)
.map(|w| (w[0].as_str(), w[1].as_str(), 1))
.collect();
let long = fixture("nostep-long", &chain);
let e = refused(
&long,
&[],
"plain.model",
r#"/nodes/*[::name = "N0"]((->x)*)+ @| count"#,
);
assert!(e.contains("QUANTIFIER_BOUND"), "{e}");
}
/// A walk that ties with the answer and is cut at the quantifier
/// bound leaves the tie unsaid: the search by total refused only a
/// cut below the answer's total. B is 10 from A; D is 10 too, two
/// legs of no weight further.
#[test]
fn a_tie_cut_at_the_quantifier_bound_is_refused() {
let d = fixture("tie", &[("A", "B", 10), ("B", "C", 0), ("C", "D", 0)]);
let q = format!(r#"{FROM_A}(->*){{;300_EUR}}[::name = "B" || ::name = "D"]? | ::name"#);
assert_eq!(lines(&d, "plain.model", &q), ["B", "D"]);
let e = refused(&d, &["--quantifier-bound", "2"], "plain.model", &q);
assert!(e.contains("QUANTIFIER_BOUND"), "{e}");
}
/// The cycle refusal names the node the walk began at. The ground is
/// kept sorted, and its first node was printed: a walk from the
/// third node was said to begin at the first.
#[test]
fn the_cycle_refusal_names_the_walks_start() {
let d = fixture("cycle", &[("C", "A", 1), ("A", "B", 1), ("B", "C", 1)]);
// the nodes in order of first mention: C, A, B
let e = refused(
&d,
&[],
"acyclic.model",
r#"/nodes/*[::name = "B"](->x){1;5}::name"#,
);
assert!(
e.contains("the walk from /nodes/3 returns to /nodes/3 after 3 steps"),
"{e}"
);
}
/// `edges` reads the alternatives of a group and nothing written on
/// the group: a guard there was dropped in silence, and every edge
/// answered.
#[test]
fn edges_refuses_what_is_written_on_a_group() {
let d = fixture("edges", TWO_WAYS);
assert_eq!(
lines(&d, "plain.model", "/nodes/* @| edges((->x|<-x)) | count"),
lines(&d, "plain.model", "/nodes/* @| edges(--x) | count")
);
for group in [
r#"(->x|<-x)[::name = "B"]"#,
"(->x|<-x){1}{;5_EUR}",
"(->x|<-x)?",
] {
let e = refused(
&d,
&[],
"plain.model",
&format!("/nodes/* @| edges({group}) | count"),
);
assert!(e.contains("nothing written on the group"), "{group}: {e}");
}
// a condition on the step is read
assert_eq!(
lines(
&d,
"plain.model",
r#"/nodes/* @| edges(->x[::name = "B"]) | count"#
),
["1"]
);
}