polydat 0.7.0

Polydat — a variates construction engine
Documentation
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! Fuzz coverage for the `for` construct (for_traversal.md §2).
//!
//! Two generators feed the front end:
//!
//! - **Well-formed programs.** A grammar-directed generator emits
//!   random `for` forms — producer bindings, inline traversals,
//!   traversals over producers, nesting, every body statement kind,
//!   `where` predicates with `{name}` placeholders, `order` specs with
//!   every strategy, zip groups, string and generator sources, comments
//!   and odd whitespace. Invariants: lex and parse succeed, the
//!   element names match what was emitted, the pretty-printer round
//!   trips to a fixed point, and the compiler lowers every form to a
//!   child program or producer entry, or errors in a sentence, and
//!   never panics.
//! - **Mutated programs.** Random byte-level edits of well-formed
//!   programs. Invariants: no stage panics, and every error is a
//!   sentence rather than a leaked backtrace.
//!
//! Seeds and iteration counts follow the other fuzzers:
//! `FUZZ_SEED` and `FUZZ_ITERATIONS`.
//!
//! The `#[ignore]`d `superfuzz_for_syntax` enumerates the generator's
//! dimensions instead of drawing them: the top-level form (producer,
//! producer with a derived producer, traversal over a comprehension,
//! over a producer, over a derived head), the nesting depth 0 to 2,
//! the seven clause source kinds, the clause shape (single or one of
//! three zip forms), the `where` shape (none, one comparison, `&&`,
//! `||`), the comparison operator, the `order` spec (none, or each of
//! nine strategies bare or with `/n`), the derived head's clauses, the
//! seven body statement kinds, and plain or commented layout. A
//! combination fixes each of those choices everywhere the generator
//! makes it, and the traversal nests to exactly the planned depth; the
//! remaining choices (names, literals, extra statements) come from the
//! combination's seed. Each combination's program goes through the
//! well-formed invariants, the engine sweep, and one mutation.
//!
//! The combinations run in the stratified order of `common::superfuzz`,
//! which reaches every value of every dimension, depths included,
//! within the first 19 combinations. The run stops at 1000
//! combinations or 60 seconds, whichever comes first;
//! `SUPERFUZZ_MAX_COMBINATIONS` and `SUPERFUZZ_MAX_SECONDS` change the
//! limits (0 keeps the default, a large number lifts the limit).
//! `SUPERFUZZ_SEEDS` is the number of seeds run per combination
//! (default 1), `FUZZ_SEED` is the base seed, and `FUZZ_ENGINE_SWEEP`
//! is the stride over combinations of the engine sweep (default 1,
//! every combination; 0 turns it off).

use polydat::dsl::ast::{Expr, ForSourceKind, PolydatFile, Statement};
use polydat::dsl::pprint::pp_file;

// ---------------------------------------------------------------------------
// PRNG (splitmix64, same as fuzz_type_adapters)
// ---------------------------------------------------------------------------

struct Rng(u64);

impl Rng {
    fn new(seed: u64) -> Self {
        Rng(seed.wrapping_mul(0x9E37_79B9_7F4A_7C15).wrapping_add(1))
    }
    fn next_u64(&mut self) -> u64 {
        self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = self.0;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }
    fn range(&mut self, n: usize) -> usize {
        if n == 0 {
            0
        } else {
            (self.next_u64() as usize) % n
        }
    }
    fn coin(&mut self, pct: usize) -> bool {
        self.range(100) < pct
    }
    fn pick<'a>(&mut self, items: &[&'a str]) -> &'a str {
        items[self.range(items.len())]
    }
}

// ---------------------------------------------------------------------------
// Well-formed program generator
// ---------------------------------------------------------------------------

/// A generated comprehension: its canonical text and the element
/// names it dispenses, in declaration order.
struct Comp {
    text: String,
    names: Vec<String>,
}

struct Gen {
    rng: Rng,
    counter: usize,
    /// Producer wires bound so far at the current scope depth, with
    /// their element names, usable by `for <producer> { ... }` and by
    /// derivations.
    producers: Vec<(String, Vec<String>)>,
    /// The superfuzz's fixed choices, or `None` for the random
    /// generator.
    plan: Option<ForPlan>,
}

/// One combination of the superfuzz's dimensions. Each field fixes a
/// choice the random generator otherwise draws, everywhere it draws
/// it, so the value is present in the program whenever the construct
/// is.
#[derive(Clone, Copy, Debug)]
struct ForPlan {
    /// Index into [`FORMS`].
    form: usize,
    /// Nesting of `for` blocks under the planned traversal,
    /// `0..=MAX_DEPTH`.
    depth: usize,
    /// Clause source kind, `0..SOURCE_KINDS`.
    source: usize,
    /// 0 a single variable, 1 a parenthesized zip group, 2
    /// `zip_truncate`, 3 `zip_cycle`.
    clause: usize,
    /// 0 no `where`, 1 one comparison, 2 two joined by `&&`, 3 by `||`.
    predicate: usize,
    /// Index into [`CMP`].
    cmp: usize,
    /// 0 no `order`, `1..=9` a bare strategy, `10..=18` a strategy with
    /// a `/n` parameter.
    order: usize,
    /// A derived head's clauses: 0 `where`, 1 `order`, 2 both.
    derive: usize,
    /// Index into [`BODY_KINDS`].
    body: usize,
    /// 0 plain layout, 1 comments, a tight brace, and a spaced comma.
    trivia: usize,
}

/// The planned top-level form: a producer binding, a producer and a
/// derived producer (each followed by a traversal over a
/// comprehension), a traversal over a comprehension, a traversal over
/// a producer, and a traversal over a derived head.
const FORMS: &[&str] = &[
    "producer",
    "producer+derived",
    "traversal",
    "traversal over producer",
    "traversal over derived head",
];
/// Clause source kinds [`Gen::source`] draws from.
const SOURCE_KINDS: usize = 7;
/// Clause shapes: a single variable and the three zip forms.
const CLAUSES: usize = 4;
/// `where` shapes: none, one comparison, `&&`, `||`.
const PREDICATES: usize = 4;
/// `order` shapes: none, each strategy bare, each with `/n`.
const ORDERS: usize = 1 + 2 * STRATEGIES.len();
/// Derived-head shapes: `where`, `order`, both.
const DERIVES: usize = 3;
/// The deepest `for` nesting the generator emits.
const MAX_DEPTH: usize = 2;
/// Body statement kinds, as arms of [`Gen::body_statement_kind`]: hash,
/// string, extern, const, cursor, producer binding, `mod`. A nested
/// traversal is the depth dimension.
const BODY_KINDS: &[usize] = &[0, 2, 3, 4, 5, 6, 8];

const STRATEGIES: &[&str] = &[
    "lex",
    "reverse_lex",
    "diagonal",
    "antidiagonal",
    "extrema",
    "shells",
    "halton",
    "sobol",
    "lhs",
];
const CMP: &[&str] = &["==", "!=", "<", ">", "<=", ">="];
const WORDS: &[&str] = &["load", "verify", "read", "warm", "cold"];

impl Gen {
    fn new(seed: u64) -> Self {
        Gen {
            rng: Rng::new(seed),
            counter: 0,
            producers: Vec::new(),
            plan: None,
        }
    }

    fn planned(seed: u64, plan: ForPlan) -> Self {
        Gen {
            plan: Some(plan),
            ..Gen::new(seed)
        }
    }

    fn name(&mut self, prefix: &str) -> String {
        self.counter += 1;
        format!("{prefix}{}", self.counter)
    }

    fn cmp(&mut self) -> &'static str {
        match self.plan {
            Some(p) => CMP[p.cmp],
            None => self.rng.pick(CMP),
        }
    }

    /// A trivia choice: the plan's, or a coin at `pct`.
    fn trivia(&mut self, pct: usize) -> bool {
        match self.plan {
            Some(p) => p.trivia == 1,
            None => self.rng.coin(pct),
        }
    }

    /// An `order` suffix. `always` asks for one even where the plan
    /// says none, as a derived head with an order clause does.
    fn order_suffix(&mut self, always: bool) -> Option<String> {
        let (strat, param) = match self.plan {
            Some(p) => match p.order {
                0 if always => (STRATEGIES[0], false),
                0 => return None,
                o if o <= STRATEGIES.len() => (STRATEGIES[o - 1], false),
                o => (STRATEGIES[o - 1 - STRATEGIES.len()], true),
            },
            None => {
                if !always && !self.rng.coin(40) {
                    return None;
                }
                let strat = self.rng.pick(STRATEGIES);
                (strat, self.rng.coin(50))
            }
        };
        Some(if param {
            format!(" order {strat}/{}", 1 + self.rng.range(9))
        } else {
            format!(" order {strat}")
        })
    }

    /// One clause source in the text parser's surface.
    fn source(&mut self) -> String {
        let kind = match self.plan {
            Some(p) => p.source,
            None => self.rng.range(SOURCE_KINDS),
        };
        match kind {
            0 => {
                let lo = self.rng.range(5);
                format!("{lo}..{}", lo + 1 + self.rng.range(6))
            }
            1 => {
                let n = 2 + self.rng.range(4);
                (0..n)
                    .map(|i| (i * 10 + self.rng.range(10)).to_string())
                    .collect::<Vec<_>>()
                    .join(",")
            }
            2 => {
                let n = 2 + self.rng.range(3);
                (0..n)
                    .map(|_| self.rng.pick(WORDS).to_string())
                    .collect::<Vec<_>>()
                    .join(",")
            }
            3 => format!(
                "partitions(\"*/{}\", {})",
                2 + self.rng.range(6),
                100 * (1 + self.rng.range(1000))
            ),
            4 => format!(
                "partitions('{}%,{}%,*')",
                10 + self.rng.range(30),
                10 + self.rng.range(30)
            ),
            5 => format!("{}..{}", 1 + self.rng.range(3), 5 + self.rng.range(20)),
            _ => {
                let n = 2 + self.rng.range(3);
                (0..n)
                    .map(|i| format!("{}.{}", i, self.rng.range(10)))
                    .collect::<Vec<_>>()
                    .join(",")
            }
        }
    }

    fn comprehension(&mut self) -> Comp {
        let mut clauses = Vec::new();
        let mut names = Vec::new();
        let n = 1 + self.rng.range(3);
        for _ in 0..n {
            let zip = match self.plan {
                Some(p) => p.clause > 0,
                None => self.rng.coin(15),
            };
            if zip {
                // Parallel-iter (zip) group.
                let k = 2 + self.rng.range(2);
                let vars: Vec<String> = (0..k).map(|_| self.name("z")).collect();
                let size = 2 + self.rng.range(4);
                let srcs: Vec<String> = (0..k).map(|_| format!("0..{size}")).collect();
                let form = match self.plan {
                    Some(p) => p.clause - 1,
                    None => self.rng.range(3),
                };
                let rhs = match form {
                    0 => format!("({})", srcs.join(", ")),
                    1 => format!("zip_truncate({})", srcs.join(", ")),
                    _ => format!("zip_cycle({})", srcs.join(", ")),
                };
                clauses.push(format!("({}) in {rhs}", vars.join(", ")));
                names.extend(vars);
            } else {
                let v = self.name("k");
                let src = self.source();
                clauses.push(format!("{v} in {src}"));
                names.push(v);
            }
        }
        let spaced = self.trivia(20);
        let mut text = clauses.join(if spaced { " , " } else { ", " });
        let has_where = match self.plan {
            Some(p) => p.predicate > 0,
            None => self.rng.coin(40),
        };
        if has_where {
            let a = names[self.rng.range(names.len())].clone();
            let cmp = self.cmp();
            let lit = self.rng.range(50);
            let mut pred = format!("{{{a}}} {cmp} {lit}");
            let compound = match self.plan {
                Some(p) => p.predicate >= 2,
                None => self.rng.coin(40),
            };
            if compound {
                let b = names[self.rng.range(names.len())].clone();
                let and = match self.plan {
                    Some(p) => p.predicate == 2,
                    None => self.rng.coin(50),
                };
                let join = if and { "&&" } else { "||" };
                let cmp = self.cmp();
                pred = format!("{pred} {join} {{{b}}} {cmp} {}", self.rng.range(50));
            }
            text.push_str(&format!(" where {pred}"));
        }
        if let Some(order) = self.order_suffix(false) {
            text.push_str(&order);
        }
        Comp { text, names }
    }

    /// A random body statement that references the scope's element
    /// names. `depth` bounds nesting of further `for` blocks.
    fn body_statement(
        &mut self,
        names: &[String],
        depth: usize,
        out: &mut Vec<Expected>,
    ) -> String {
        let kind = self.rng.range(9);
        self.body_statement_kind(kind, names, depth, out)
    }

    /// The body statement of arm `kind`: 0 and 1 hash, 2 string, 3
    /// extern, 4 const, 5 cursor, 6 producer binding, 7 a nested
    /// traversal while `depth` allows, and `mod` otherwise.
    fn body_statement_kind(
        &mut self,
        kind: usize,
        names: &[String],
        depth: usize,
        out: &mut Vec<Expected>,
    ) -> String {
        let pick = |g: &mut Gen| -> String {
            if names.is_empty() || g.rng.coin(30) {
                "cycle".into()
            } else {
                names[g.rng.range(names.len())].clone()
            }
        };
        match kind {
            0 | 1 => {
                let n = self.name("v");
                let a = pick(self);
                format!("{n} := hash({a})")
            }
            2 => {
                let n = self.name("s");
                let a = pick(self);
                let b = pick(self);
                format!("{n} := \"{{{a}}}-{{{b}}}\"")
            }
            3 => format!("extern e{} : u64 = {}", self.counter, self.rng.range(1000)),
            4 => format!("const c{} := {}", self.counter, self.rng.range(1000)),
            5 => {
                let a = pick(self);
                format!(
                    "cursor rows{} = range(0, {}) over {a}",
                    self.counter,
                    100 + self.rng.range(1000)
                )
            }
            6 => {
                // Producer binding inside a body.
                let c = self.comprehension();
                let n = self.name("prod");
                out.push(Expected::Producer {
                    names: c.names.clone(),
                });
                self.producers.push((n.clone(), c.names.clone()));
                format!("{n} := for {}", c.text)
            }
            7 if depth > 0 => self.traversal(depth - 1, out),
            _ => {
                let n = self.name("w");
                let a = pick(self);
                format!("{n} := mod({a}, {})", 1 + self.rng.range(100))
            }
        }
    }

    /// A `for` traversal statement with a random body.
    fn traversal(&mut self, depth: usize, out: &mut Vec<Expected>) -> String {
        let use_producer = !self.producers.is_empty() && self.rng.coin(30);
        let (head, names) = if use_producer {
            let (p, names) = self.producers[self.rng.range(self.producers.len())].clone();
            out.push(Expected::TraversalOverProducer {
                producer: p.clone(),
            });
            if self.rng.coin(40) {
                // A derived head: `for base where ... order ... {`.
                (self.derivation_text(&p, &names), names)
            } else {
                (p, names)
            }
        } else {
            let c = self.comprehension();
            out.push(Expected::Traversal {
                names: c.names.clone(),
            });
            (c.text, c.names)
        };
        self.traversal_body(head, &names, depth, out)
    }

    /// A traversal over the comprehension, producer, or derived head
    /// `head` with element names `names`. A planned body holds the
    /// plan's body statement, then a nested traversal while `depth`
    /// allows, so the planned depth is always reached, then up to two
    /// random statements.
    fn traversal_body(
        &mut self,
        head: String,
        names: &[String],
        depth: usize,
        out: &mut Vec<Expected>,
    ) -> String {
        let saved = self.producers.len();
        let mut body = Vec::new();
        match self.plan {
            Some(p) => {
                body.push(self.body_statement_kind(BODY_KINDS[p.body], names, depth, out));
                if depth > 0 {
                    body.push(self.traversal(depth - 1, out));
                }
                for _ in 0..self.rng.range(3) {
                    body.push(self.body_statement(names, depth, out));
                }
            }
            None => {
                let n = 1 + self.rng.range(4);
                for _ in 0..n {
                    body.push(self.body_statement(names, depth, out));
                }
            }
        }
        self.producers.truncate(saved);
        let comment = if self.trivia(25) { " // traverse" } else { "" };
        let brace = if self.trivia(20) { "{" } else { " {" };
        format!(
            "for {head}{brace}{comment}\n{}\n}}",
            body.iter()
                .map(|s| format!("    {s}"))
                .collect::<Vec<_>>()
                .join("\n")
        )
    }

    /// `base where <pred>`, `base order <spec>`, or both, over a
    /// producer with the given element names.
    fn derivation_text(&mut self, base: &str, names: &[String]) -> String {
        let mut text = base.to_string();
        let which = match self.plan {
            Some(p) => p.derive,
            None => self.rng.range(DERIVES),
        };
        if which != 1 && !names.is_empty() {
            let a = names[self.rng.range(names.len())].clone();
            let cmp = self.cmp();
            text.push_str(&format!(" where {{{a}}} {cmp} {}", self.rng.range(50)));
        }
        if (which != 0 || names.is_empty())
            && let Some(order) = self.order_suffix(true)
        {
            text.push_str(&order);
        }
        text
    }

    /// A top-level producer binding, and a derived producer over it
    /// when `derived` says so (the plan's choice, or a coin).
    fn top_producer(
        &mut self,
        lines: &mut Vec<String>,
        out: &mut Vec<Expected>,
        derived: Option<bool>,
    ) -> (String, Vec<String>) {
        let c = self.comprehension();
        let name = self.name("sweep");
        out.push(Expected::Producer {
            names: c.names.clone(),
        });
        self.producers.push((name.clone(), c.names.clone()));
        let comment = if self.trivia(25) { "   # sweep" } else { "" };
        lines.push(format!("{name} := for {}{comment}", c.text));
        if derived.unwrap_or_else(|| self.rng.coin(50)) {
            // A derived producer over the one just bound. Its
            // element names resolve at compile time, so the
            // parsed form reports none.
            let derived = self.name("derived");
            let text = self.derivation_text(&name, &c.names);
            out.push(Expected::Producer { names: Vec::new() });
            self.producers.push((derived.clone(), c.names.clone()));
            lines.push(format!("{derived} := for {text}"));
        }
        (name, c.names)
    }

    /// The program of a plan: the planned form, whose traversal nests
    /// `depth` deep, then an optional plain binding.
    fn planned_program(&mut self, p: ForPlan) -> (String, Vec<Expected>) {
        let mut out = Vec::new();
        let mut lines = vec!["input cycle: u64".to_string()];
        if self.rng.coin(50) {
            lines.push(format!("extern seed: u64 = {}", self.rng.range(100)));
        }
        let comprehension_traversal = |g: &mut Gen, out: &mut Vec<Expected>| {
            let c = g.comprehension();
            out.push(Expected::Traversal {
                names: c.names.clone(),
            });
            g.traversal_body(c.text, &c.names, p.depth, out)
        };
        match p.form {
            0 | 1 => {
                self.top_producer(&mut lines, &mut out, Some(p.form == 1));
                let stmt = comprehension_traversal(self, &mut out);
                lines.push(stmt);
            }
            2 => {
                let stmt = comprehension_traversal(self, &mut out);
                lines.push(stmt);
            }
            _ => {
                let (name, names) = self.top_producer(&mut lines, &mut out, Some(false));
                out.push(Expected::TraversalOverProducer {
                    producer: name.clone(),
                });
                let head = if p.form == 4 {
                    self.derivation_text(&name, &names)
                } else {
                    name
                };
                let stmt = self.traversal_body(head, &names, p.depth, &mut out);
                lines.push(stmt);
            }
        }
        if self.rng.coin(30) {
            let plain = self.name("plain");
            lines.push(format!("{plain} := hash(cycle)"));
        }
        (lines.join("\n") + "\n", out)
    }

    /// A whole program: an input, a few top-level statements, one to
    /// three `for` forms. A planned generator makes the plan's program.
    fn program(&mut self) -> (String, Vec<Expected>) {
        if let Some(p) = self.plan {
            return self.planned_program(p);
        }
        let mut out = Vec::new();
        let mut lines = vec!["input cycle: u64".to_string()];
        if self.rng.coin(50) {
            lines.push(format!("extern seed: u64 = {}", self.rng.range(100)));
        }
        let n = 1 + self.rng.range(3);
        for _ in 0..n {
            match self.rng.range(3) {
                0 => {
                    self.top_producer(&mut lines, &mut out, None);
                }
                _ => {
                    let depth = self.rng.range(3);
                    let stmt = self.traversal(depth, &mut out);
                    lines.push(stmt);
                }
            }
            if self.rng.coin(30) {
                let plain = self.name("plain");
                lines.push(format!("{plain} := hash(cycle)"));
            }
        }
        (lines.join("\n") + "\n", out)
    }
}

/// What the generator emitted, in document order, for checking the
/// parsed AST against.
#[derive(Debug)]
enum Expected {
    Producer { names: Vec<String> },
    Traversal { names: Vec<String> },
    TraversalOverProducer { producer: String },
}

// ---------------------------------------------------------------------------
// Checks
// ---------------------------------------------------------------------------

fn parse(src: &str) -> Result<PolydatFile, String> {
    let tokens = polydat::dsl::lexer::lex(src)?;
    polydat::dsl::parser::parse(tokens)
}

/// Collect every `for` form in document order.
fn collect_for_forms(stmts: &[Statement], out: &mut Vec<Expected>) {
    for s in stmts {
        match s {
            Statement::Binding(b) => {
                if let Expr::For(src) = &b.value {
                    out.push(Expected::Producer {
                        names: src.element_names(),
                    });
                }
            }
            Statement::For(f) => {
                match &f.source.kind {
                    ForSourceKind::Producer(p) => out.push(Expected::TraversalOverProducer {
                        producer: p.clone(),
                    }),
                    ForSourceKind::Derived { base, .. } => {
                        out.push(Expected::TraversalOverProducer {
                            producer: base.clone(),
                        })
                    }
                    ForSourceKind::Comprehension(_) => out.push(Expected::Traversal {
                        names: f.source.element_names(),
                    }),
                }
                collect_for_forms(&f.body, out);
            }
            _ => {}
        }
    }
}

fn same(a: &Expected, b: &Expected) -> bool {
    match (a, b) {
        (Expected::Producer { names: x }, Expected::Producer { names: y }) => x == y,
        (Expected::Traversal { names: x }, Expected::Traversal { names: y }) => x == y,
        (
            Expected::TraversalOverProducer { producer: x },
            Expected::TraversalOverProducer { producer: y },
        ) => x == y,
        _ => false,
    }
}

fn panic_text(p: &Box<dyn std::any::Any + Send>) -> String {
    p.downcast_ref::<&str>()
        .map(|s| s.to_string())
        .or_else(|| p.downcast_ref::<String>().cloned())
        .unwrap_or_else(|| "<non-string panic>".into())
}

/// Count traversals and producers reachable from a program, recursing
/// into child programs.
fn count_lowered(p: &polydat::kernel::PolydatProgram, n: &mut usize) {
    *n += p.producers().len();
    for t in p.traversals() {
        *n += 1;
        count_lowered(&t.program, n);
    }
}

fn cryptic(msg: &str) -> bool {
    let m = msg.to_lowercase();
    msg.trim().is_empty()
        || m.contains("panic")
        || m.contains("index out of bounds")
        || m.contains("unreachable")
}

fn run_wellformed_pass(seed: u64, iterations: usize) -> Vec<String> {
    const MAX: usize = 8;
    let mut failures = Vec::new();
    // Invariant 6 is sampled — a sweep compiles the program once more
    // per tier, two of them through Cranelift, and a `for` body is a
    // child program compiled in its own right on top of that.
    // `FUZZ_ENGINE_SWEEP` is the stride; 0 turns it off, 1 sweeps every
    // program.
    let stride = env_u64("FUZZ_ENGINE_SWEEP", 8) as usize;
    let mut rng = Rng::new(seed);
    for i in 0..iterations {
        if failures.len() >= MAX {
            failures.push(format!("[seed {seed:#x}] … stopping after {MAX} failures"));
            break;
        }
        let mut g = Gen::new(rng.next_u64());
        let (source, expected) = g.program();
        let tag = format!("[seed {seed:#x}] iteration {i}: ");
        let repro = format!(
            "reproduce: FUZZ_SEED={seed} FUZZ_ITERATIONS={} cargo test -p polydat --test suite fuzz_for_syntax::wellformed",
            i + 1
        );
        let sweep = stride != 0 && i % stride == 0;
        failures.extend(check_wellformed(&source, &expected, sweep, &tag, &repro));
    }
    failures
}

/// Invariants 1 to 6 over one well-formed program and the `for` forms
/// the generator emitted for it. `sweep` runs invariant 6, the
/// engine sweep. Each failure starts with `tag` and ends with `repro`.
fn check_wellformed(
    source: &str,
    expected: &[Expected],
    sweep: bool,
    tag: &str,
    repro: &str,
) -> Vec<String> {
    let mut failures = Vec::new();
    // Invariant 1: the front end accepts what the grammar generator
    // produced, without panicking.
    let parsed = match std::panic::catch_unwind(|| parse(source)) {
        Ok(Ok(f)) => f,
        Ok(Err(e)) => {
            failures.push(format!(
                "{tag}parse rejected a well-formed program:\n  {e}\n  source:\n{source}\n  {}",
                repro
            ));
            return failures;
        }
        Err(p) => {
            failures.push(format!(
                "{tag}front end panicked: {}\n  source:\n{source}\n  {}",
                panic_text(&p),
                repro
            ));
            return failures;
        }
    };

    // Invariant 2: every for form is present, in order, with the
    // element names the comprehension declared.
    let mut got = Vec::new();
    collect_for_forms(&parsed.statements, &mut got);
    if got.len() != expected.len() || !got.iter().zip(expected).all(|(a, b)| same(a, b)) {
        failures.push(format!("{tag}for forms differ.\n  expected: {expected:?}\n  got:      {got:?}\n  source:\n{source}\n  {}", repro));
        return failures;
    }

    // Invariant 3: the pretty-printer reaches a fixed point in one
    // step, and its output parses to the same for forms.
    let printed = pp_file(&parsed);
    match parse(&printed) {
        Ok(again) => {
            let reprinted = pp_file(&again);
            if reprinted != printed {
                failures.push(format!("{tag}pretty-printer is not a fixed point.\n  first:\n{printed}\n  second:\n{reprinted}\n  {}", repro));
                return failures;
            }
            let mut got2 = Vec::new();
            collect_for_forms(&again.statements, &mut got2);
            if got2.len() != got.len() || !got2.iter().zip(&got).all(|(a, b)| same(a, b)) {
                failures.push(format!("{tag}reparse of printed form changed the for forms.\n  printed:\n{printed}\n  {}", repro));
                return failures;
            }
        }
        Err(e) => {
            failures.push(format!(
                "{tag}printed form does not parse: {e}\n  printed:\n{printed}\n  {}",
                repro
            ));
            return failures;
        }
    }

    // Invariant 4: the compiler never panics. It either lowers every
    // for form (one child program per traversal, one producer entry
    // per binding) or returns a sentence naming the offending form.
    match std::panic::catch_unwind(|| polydat::dsl::compile_polydat_interpreter(source)) {
        Ok(Ok(k)) => {
            let mut lowered = 0;
            count_lowered(k.program(), &mut lowered);
            if lowered != expected.len() {
                failures.push(format!("{tag}compiled program lowered {lowered} for forms but the source has {}.\n  source:\n{source}\n  {}", expected.len(), repro));
            }
        }
        Ok(Err(e)) => {
            if cryptic(&e.to_string()) || !e.to_string().contains("for ") {
                failures.push(format!("{tag}compiler error is cryptic or does not name the for form: {e}\n  source:\n{source}\n  {}", repro));
            }
        }
        Err(p) => failures.push(format!(
            "{tag}compiler panicked: {}\n  source:\n{source}\n  {}",
            panic_text(&p),
            repro
        )),
    }

    // Invariant 5: a compiled program's traversals activate and cycle
    // without panicking, and two hosts of the same source produce the
    // same trace (T1). Bounded so a wide comprehension stays cheap.
    let run = || -> Result<Vec<String>, String> {
        let mut k = polydat::dsl::compile_polydat_interpreter(source).map_err(|e| e.to_string())?;
        k.set_inputs(&[3]);
        let mut trace = Vec::new();
        run_traversals_bounded(&mut k, 0, &mut trace)?;
        Ok(trace)
    };
    let first = std::panic::catch_unwind(run);
    match first {
        Err(p) => failures.push(format!(
            "{tag}traversal runtime panicked: {}\n  source:\n{source}\n  {}",
            panic_text(&p),
            repro
        )),
        Ok(Err(e)) => {
            if cryptic(&e) {
                failures.push(format!(
                    "{tag}cryptic runtime error: {e}\n  source:\n{source}\n  {}",
                    repro
                ));
            }
        }
        Ok(Ok(trace_a)) => {
            if let Ok(Ok(trace_b)) = std::panic::catch_unwind(run)
                && trace_a != trace_b
            {
                failures.push(format!(
                    "{tag}T1 violated, two hosts diverged.\n  source:\n{source}\n  {}",
                    repro
                ));
            }
            // Invariant 6: and the same on every engine, when the
            // caller asks for the sweep.
            if sweep {
                for detail in engine_trace_failures(source) {
                    failures.push(format!("{tag}{detail}\n  source:\n{source}\n  {repro}"));
                }
            }
        }
    }
    failures
}

/// Invariant 6 — a composed program traverses the same on every engine.
///
/// Invariant 5 above runs one source twice and compares the traces,
/// which is T1: two hosts of the same program agree. Both runs were the
/// interpreter's, so nothing here ever asked a compiled tier what a
/// `for` does — and a traversal is where the engines have the most to
/// disagree about, since a body is a child program compiled in its own
/// right and an activation binds its coordinates through the same
/// kernel surface the parent uses.
///
/// The interpreter with no cones is the oracle; every other engine
/// walks the same bounded traversals and must produce the same trace.
/// A tier that declines the program says so (`KernelError::Refused`)
/// and is skipped; a tier that accepts it and answers differently is
/// the finding.
fn engine_trace_failures(source: &str) -> Vec<String> {
    use polydat::{Engine, JitMode, KernelError, Provenance};
    let mut out = Vec::new();

    // A program whose value is not a function of its inputs is not
    // comparable by value — `random`, a clock, a counter.
    let deterministic = match polydat::dsl::compile_polydat_interpreter(source) {
        Ok(k) => k.program().is_deterministic(),
        Err(_) => return out,
    };
    if !deterministic {
        return out;
    }

    let trace_on = |engine: Engine| -> Result<Result<Vec<String>, String>, String> {
        std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let mut k = match polydat::dsl::compile_polydat_with(source, engine) {
                Ok(k) => k,
                Err(KernelError::Refused { .. }) => return Err("refused".to_string()),
                Err(e) => return Err(format!("compile: {e}")),
            };
            k.set_inputs(&[3]);
            let mut trace = Vec::new();
            run_traversals_bounded(k.as_mut(), 0, &mut trace)?;
            Ok(trace)
        }))
        .map_err(|p| panic_text(&p))
    };

    let want = match trace_on(Engine::Interpreter(JitMode::Off)) {
        Ok(Ok(t)) => t,
        // The oracle itself declined or failed; invariants 4 and 5
        // judged that, and there is nothing to compare against.
        _ => return out,
    };

    let mut engines = vec![
        Engine::Interpreter(JitMode::Auto),
        Engine::Closures(Provenance::Raw),
        Engine::Closures(Provenance::Auto),
    ];
    if cfg!(feature = "jit") {
        engines.push(Engine::Native(Provenance::Raw));
        engines.push(Engine::PureNative(Provenance::Auto));
    }

    for engine in engines {
        match trace_on(engine) {
            Ok(Ok(got)) if got == want => {}
            // Declined, or failed the way the oracle would have.
            Ok(Err(_)) => {}
            Ok(Ok(got)) => out.push(format!(
                "I6: {engine} traverses differently from the interpreter:\n  \
                 interpreter: {want:?}\n  {engine}: {got:?}"
            )),
            // Pure native cannot carry a `None`, and one reaching it is
            // "a panic naming the extern, the tripwire of the rule,
            // never a wrong value" (engines.md §3.3). Whether an extern
            // is ever set is the host's, not the program's, so this
            // cannot be decided at build and refused there — the
            // tripwire *is* the decline, and the message says to run
            // the program on `native` instead. Not a finding: it is
            // the rule holding.
            Err(p) if p.contains("cannot carry a `None`") => {}
            Err(p) => out.push(format!(
                "I6: {engine} panicked traversing a program the interpreter walked: {p}"
            )),
        }
    }
    out
}

/// Activate every traversal of `k` (recursing into activations' own
/// traversals), running at most a few activations and cycles of each,
/// and append every pulled output's display form to `trace`.
fn run_traversals_bounded(
    k: &mut dyn polydat::Kernel,
    depth: usize,
    trace: &mut Vec<String>,
) -> Result<(), String> {
    const MAX_ACTIVATIONS: usize = 6;
    const MAX_CYCLES: u64 = 3;
    if depth > 4 {
        return Ok(());
    }
    let n = k.traversals().len();
    for t in 0..n {
        let stream = k.traverse(t)?;
        let outputs: Vec<String> = stream
            .traversal()
            .program
            .own_output_names()
            .iter()
            .map(|s| s.to_string())
            .collect();
        for a in 0..stream.len().min(MAX_ACTIVATIONS) {
            let mut act = stream.activation(a)?;
            for c in 0..act.cycle_count().min(MAX_CYCLES) {
                let kernel = act.cycle(c);
                for name in &outputs {
                    trace.push(format!(
                        "{t}/{a}/{c} {name}={}",
                        kernel.pull(name).to_display_string()
                    ));
                }
            }
            act.cycle(0);
            run_traversals_bounded(act.kernel.as_mut(), depth + 1, trace)?;
        }
    }
    Ok(())
}

/// Byte-level mutations of well-formed programs.
fn mutate(rng: &mut Rng, src: &str) -> String {
    let mut chars: Vec<char> = src.chars().collect();
    let edits = 1 + rng.range(4);
    const INSERT: &[char] = &[
        '{', '}', '(', ')', '[', ']', ',', '"', '\'', '.', 'f', 'o', 'r', ' ', '\n', '=', ':', '/',
        '#', '<', '&', '|', '_', '0',
    ];
    for _ in 0..edits {
        if chars.is_empty() {
            break;
        }
        let at = rng.range(chars.len());
        match rng.range(4) {
            0 => {
                chars.remove(at);
            }
            1 => chars.insert(at, INSERT[rng.range(INSERT.len())]),
            2 => chars[at] = INSERT[rng.range(INSERT.len())],
            _ => {
                // Duplicate a slice: exercises repeated `for` and unbalanced braces.
                let end = (at + 1 + rng.range(12)).min(chars.len());
                let slice: Vec<char> = chars[at..end].to_vec();
                for (k, c) in slice.into_iter().enumerate() {
                    chars.insert(end + k, c);
                }
            }
        }
    }
    chars.into_iter().collect()
}

fn run_mutation_pass(seed: u64, iterations: usize) -> Vec<String> {
    const MAX: usize = 8;
    let mut failures = Vec::new();
    let mut rng = Rng::new(seed ^ 0x5EED_F0F0);
    for i in 0..iterations {
        if failures.len() >= MAX {
            failures.push(format!("[seed {seed:#x}] … stopping after {MAX} failures"));
            break;
        }
        let mut g = Gen::new(rng.next_u64());
        let (clean, _) = g.program();
        let source = mutate(&mut rng, &clean);
        let tag = format!("[seed {seed:#x}] iteration {i}: ");
        let repro = format!(
            "reproduce: FUZZ_SEED={seed} FUZZ_ITERATIONS={} cargo test -p polydat --test suite fuzz_for_syntax::mutated",
            i + 1
        );
        failures.extend(check_mutant(&source, &tag, &repro));
    }
    failures
}

/// The mutant invariants over one damaged program. Each failure starts
/// with `tag` and ends with `repro`.
fn check_mutant(source: &str, tag: &str, repro: &str) -> Vec<String> {
    let mut failures = Vec::new();
    // Invariant: no stage panics, and every error is a sentence.
    let outcome = std::panic::catch_unwind(|| {
        let parsed = parse(source)?;
        let printed = pp_file(&parsed);
        // A parse that succeeds must print to something that parses.
        parse(&printed).map_err(|e| {
            format!("printed form of a parsed mutant does not parse: {e}\n  printed:\n{printed}")
        })?;
        polydat::dsl::compile_polydat_interpreter(source).map_err(|e| e.to_string())
    });
    match outcome {
        Err(p) => failures.push(format!(
            "{tag}panic on mutant: {}\n  source:\n{source}\n  {}",
            panic_text(&p),
            repro
        )),
        Ok(Err(e)) => {
            if cryptic(&e) || e.to_string().contains("printed form of a parsed mutant") {
                failures.push(format!("{tag}{e}\n  source:\n{source}\n  {}", repro));
            }
        }
        Ok(Ok(k)) => {
            // A mutant that compiles must have lowered exactly the
            // for forms it still contains.
            if let Ok(f) = parse(source) {
                let mut forms = Vec::new();
                collect_for_forms(&f.statements, &mut forms);
                let mut lowered = 0;
                count_lowered(k.program(), &mut lowered);
                if lowered != forms.len() {
                    failures.push(format!("{tag}mutant has {} for forms but {lowered} were lowered.\n  source:\n{source}\n  {}", forms.len(), repro));
                }
            }
        }
    }
    failures
}

fn env_u64(name: &str, default: u64) -> u64 {
    std::env::var(name)
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(default)
}

#[test]
fn wellformed_for_programs_parse_print_and_are_declined() {
    let seed = env_u64("FUZZ_SEED", 0xF0A_5EED);
    let iterations = env_u64("FUZZ_ITERATIONS", 400) as usize;
    let failures = run_wellformed_pass(seed, iterations);
    assert!(
        failures.is_empty(),
        "fuzz invariants violated ({} failures):\n\n{}",
        failures.len(),
        failures.join("\n---\n")
    );
}

#[test]
fn mutated_for_programs_never_panic() {
    let seed = env_u64("FUZZ_SEED", 0xF0A_5EED);
    let iterations = env_u64("FUZZ_ITERATIONS", 600) as usize;
    let failures = run_mutation_pass(seed, iterations);
    assert!(
        failures.is_empty(),
        "fuzz invariants violated ({} failures):\n\n{}",
        failures.len(),
        failures.join("\n---\n")
    );
}

#[test]
fn generator_smoke() {
    // The generator itself must produce every form across a modest
    // sample, or the fuzz above is weaker than it looks.
    let mut rng = Rng::new(7);
    let (
        mut producers,
        mut traversals,
        mut over_producer,
        mut nested,
        mut wheres,
        mut orders,
        mut zips,
    ) = (0, 0, 0, 0, 0, 0, 0);
    let mut derived = 0;
    for _ in 0..200 {
        let mut g = Gen::new(rng.next_u64());
        let (src, expected) = g.program();
        for e in &expected {
            match e {
                Expected::Producer { .. } => producers += 1,
                Expected::Traversal { .. } => traversals += 1,
                Expected::TraversalOverProducer { .. } => over_producer += 1,
            }
        }
        if src.matches("for ").count() > 1 && src.contains("    for ") {
            nested += 1;
        }
        if src.contains(" where ") {
            wheres += 1;
        }
        if src.contains(" order ") {
            orders += 1;
        }
        if src.contains("zip_") || src.contains(") in (") {
            zips += 1;
        }
        if src.contains(":= for sweep")
            || src.contains("for sweep") && (src.contains(" where ") || src.contains(" order "))
        {
            derived += 1;
        }
    }
    assert!(
        producers > 20
            && traversals > 20
            && over_producer > 5
            && nested > 5
            && wheres > 20
            && orders > 20
            && zips > 5
            && derived > 5,
        "generator coverage too thin: producers={producers} traversals={traversals} over_producer={over_producer} nested={nested} wheres={wheres} orders={orders} zips={zips} derived={derived}"
    );
}

#[test]
fn every_planned_form_parses_and_matches() {
    // One program per value of each dimension, through the parse and
    // shape invariants, so a plan the generator cannot honour fails
    // here rather than only in the manual superfuzz.
    let mut failures = Vec::new();
    for i in 0..ORDERS {
        let plan = ForPlan {
            form: i % FORMS.len(),
            depth: i % (MAX_DEPTH + 1),
            source: i % SOURCE_KINDS,
            clause: i % CLAUSES,
            predicate: i % PREDICATES,
            cmp: i % CMP.len(),
            order: i,
            derive: i % DERIVES,
            body: i % BODY_KINDS.len(),
            trivia: i % 2,
        };
        let (source, expected) = Gen::planned(i as u64, plan).program();
        let tag = format!("{plan:?}: ");
        failures.extend(check_wellformed(&source, &expected, false, &tag, ""));
    }
    assert!(failures.is_empty(), "{}", failures.join("\n---\n"));
}

/// MANUAL — every dimension of the generator under the shared budget:
///
/// ```text
/// cargo nextest run -p polydat --run-ignored only -E 'test(/superfuzz_for_syntax/)' --no-capture
/// ```
#[test]
#[ignore = "manual superfuzz — up to a minute by default; run with `--run-ignored only`"]
fn superfuzz_for_syntax() {
    use super::common::superfuzz;
    let base = env_u64("FUZZ_SEED", 0xF0A_5EED);
    let seeds = superfuzz::seeds_per_combination();
    // Every combination sweeps the engines unless `FUZZ_ENGINE_SWEEP`
    // sets a stride over combinations; 0 turns the sweep off.
    let stride = env_u64("FUZZ_ENGINE_SWEEP", 1);
    superfuzz::run(
        "superfuzz_for_syntax",
        &[
            ("form", FORMS.len()),
            ("depth", MAX_DEPTH + 1),
            ("source", SOURCE_KINDS),
            ("clause", CLAUSES),
            ("where", PREDICATES),
            ("cmp", CMP.len()),
            ("order", ORDERS),
            ("derive", DERIVES),
            ("body", BODY_KINDS.len()),
            ("trivia", 2),
        ],
        |v, index| {
            let plan = ForPlan {
                form: v[0],
                depth: v[1],
                source: v[2],
                clause: v[3],
                predicate: v[4],
                cmp: v[5],
                order: v[6],
                derive: v[7],
                body: v[8],
                trivia: v[9],
            };
            let sweep = stride != 0 && index % stride == 0;
            let mut failures = Vec::new();
            for k in 0..seeds {
                let seed = superfuzz::seed_for(base, index, k);
                let (source, expected) = Gen::planned(seed, plan).program();
                let tag = format!("seed {seed:#x}: ");
                failures.extend(check_wellformed(&source, &expected, sweep, &tag, ""));
                let mutant = mutate(&mut Rng::new(seed ^ 0x5EED_F0F0), &source);
                let tag = format!("seed {seed:#x} mutant: ");
                failures.extend(check_mutant(&mutant, &tag, ""));
            }
            failures
        },
    );
}