polydat-grammar 0.6.1

The Polydat language: lexer, parser, AST, and pretty-printer, without the runtime
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
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! Metadata algebra (comprehension_forms.md §10.7).
//!
//! Every well-formed comprehension AST node carries a four-field
//! [`Metadata`] bundle computed bottom-up from its children's
//! metadata and its own scalar parameters. The bundle is a
//! monoid: propagation composes under composition, and every
//! field is either a closed enum (capability bit) or a
//! closed-form numeric/symbolic descriptor.
//!
//! This module owns:
//!
//! - [`Metadata`] — the four-field bundle.
//! - [`IndexFn`] — closed-form addressing schemes (six variants
//!   covering cartesian, zip Strict/Truncate, zip Cycle, union,
//!   continuous, hybrid).
//! - [`NaturalOrder`] — how a node enumerates by default.
//! - [`Materialization`] — streaming or sized-barrier
//!   classification (comprehension_forms.md §6.2).
//! - [`Comprehension::metadata`] — propagation entry point.
//!
//! The propagation rules are total, constant-time per node, and
//! cannot fail. Dependent-source cartesians produce
//! `index_addressable = None`; this is the **only** place
//! metadata propagation consults child-internal information
//! beyond the published bundles — and it does so at the
//! cartesian node, by walking the children's source expressions
//! for back-references to earlier-axis names.

use serde::{Deserialize, Serialize};

use super::ast::Comprehension;
use super::cardinality::{CardinalityClass, Hybrid, Interval, ProductMeasure};
use super::source::Source;
use super::strategy::{StrategyName, ZipMode};

/// The metadata bundle carried by every well-formed AST node.
///
/// Computed bottom-up; never mutated after propagation. Each
/// field is a closed enum or a closed-form descriptor — no
/// callbacks, no fail-able analyses.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Metadata {
    /// Cardinality class (comprehension_forms.md §6.1).
    pub cardinality: CardinalityClass,

    /// Closed-form bijection from `0..|c|` to the node's
    /// dispensed tuples. `None` when the node has no
    /// addressable index space (raw filter output, dependent
    /// cartesian, a truncated `Lex` order over either). An order
    /// other than an untruncated `Lex` is a one-axis `Lattice` of its
    /// selection: position `i` is the input's tuple at the `i`-th
    /// selected position.
    pub index_addressable: Option<IndexFn>,

    /// How this node enumerates by default.
    pub natural_order: NaturalOrder,

    /// Streaming-vs-barrier classification (comprehension_forms.md §6.2).
    pub materialization: Materialization,
}

/// Closed-form addressing schemes (comprehension_forms.md §10.7.1).
///
/// Six variants. Each describes the bijection from a
/// `0..cardinality` index range to the node's tuple shape.
/// `Continuous` and `Hybrid` carry the cardinality's
/// interval+measure descriptors directly so the R2 push-down
/// rule (comprehension_forms.md §10.2) dispatches on them without
/// recomputing.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum IndexFn {
    /// Discrete cartesian. `axis_sizes[i]` is the i-th axis's
    /// element count. Multi-index `(i₀, i₁, …)` maps to the
    /// per-axis tuple at those positions.
    Lattice {
        /// Element count per axis.
        axis_sizes: Vec<u64>,
    },

    /// Zip Strict / Truncate. One index `i ∈ 0..length` maps
    /// to the per-child tuple at position i.
    Lockstep {
        /// The common length.
        length: u64,
    },

    /// Zip Cycle. Modular addressing — index `i` maps to each
    /// child at `i mod child.cardinality`. At least one child
    /// must be bounded (the cycling target). The index range is
    /// [`cycle_length`] of the sizes: the longest child's, or empty
    /// when any child is empty.
    Modular {
        /// Element count per child.
        axis_sizes: Vec<u64>,
    },

    /// Union of index-addressable children. Index `i ∈
    /// 0..Σsegment_sizes` maps to segment k where k is the
    /// smallest such that `Σ₀^k segment_sizes > i`, position
    /// `i - Σ₀^{k-1} segment_sizes` within that segment.
    Concatenation {
        /// Element count per segment, in order.
        segment_sizes: Vec<u64>,
    },

    /// Continuous K-D box. Strategy push-down rules (Halton /
    /// Sobol / Lhs / Extrema on Continuous) draw from this
    /// directly; the discrete-to-continuous mapping is
    /// strategy-specific.
    Continuous {
        /// The interval of each axis.
        intervals: Vec<Interval>,
        /// The measure drawn from.
        measure: ProductMeasure,
    },

    /// Mixed discrete × continuous cartesian. Discrete axes get
    /// integer indexing; continuous axes get measure-weighted
    /// sampling. Strategy push-down dispatches per-axis.
    Hybrid {
        /// Element count per discrete axis.
        discrete_axes: Vec<u64>,
        /// The interval of each continuous axis.
        continuous_axes: Vec<Interval>,
        /// The measure over the continuous axes.
        measure: ProductMeasure,
    },
}

impl IndexFn {
    /// `true` if this index function carries any continuous
    /// axis. Used by per-strategy V4 checks to reject
    /// strategies that don't accept continuous inputs.
    pub fn has_continuous_axis(&self) -> bool {
        matches!(self, IndexFn::Continuous { .. } | IndexFn::Hybrid { .. })
    }

    /// `true` if this index function is a multi-axis Lattice
    /// (discrete cartesian with ≥2 axes). Required by
    /// lattice-geometric strategies (Extrema / Shells /
    /// Diagonal / Antidiagonal) for non-degenerate behavior.
    pub fn is_multi_axis_lattice(&self) -> bool {
        matches!(self, IndexFn::Lattice { axis_sizes } if axis_sizes.len() >= 2)
    }
}

/// Natural enumeration order (comprehension_forms.md §10.7.1).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum NaturalOrder {
    /// Lex order — rightmost axis varies fastest. Produced by
    /// cartesian, single-axis clause, and `order(_, Lex, _)`.
    Lex,

    /// Lockstep — zip's natural order. One tuple per i, all
    /// children at position i.
    Lockstep,

    /// Sequential — union's natural order. Drain child 0,
    /// then child 1, etc.
    Sequential,

    /// Strategy-driven — produced by `order(_, non-Lex, _)`.
    /// The wrapped strategy determines the emission order.
    Strategy(StrategyName),

    /// A continuous source that no sampling order wraps. V8
    /// refuses to dispense it.
    PendingSampling,
}

/// Streaming-vs-barrier classification (comprehension_forms.md §6.2).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Materialization {
    /// O(operator-local state) per pull; no input materialized.
    Streaming,

    /// Holds a finite working set; size declared at compile
    /// time. The two natural barriers (comprehension_forms.md §6.3):
    /// `zip(Cycle)` shorter children + non-Lex `order`.
    BoundedBarrier {
        /// Tuples the barrier holds at most.
        working_set_size: u64,
    },

    /// Working set is unbounded. Always V6-rejected per spec
    /// §5; this variant exists for representational
    /// completeness but should never propagate through to a
    /// valid AST's metadata.
    UnboundedBarrier,
}

impl Comprehension {
    /// Compute this node's metadata bundle (comprehension_forms.md
    /// §10.7.2).
    ///
    /// Bottom-up: every child's metadata is computed first,
    /// then this node's. Constant-time per node above the
    /// child cost. Total — never fails, never partial.
    ///
    /// For non-leaf nodes the metadata is recomputed on every
    /// call (no caching at this layer); consumers that need
    /// memoization wrap it externally. The propagation cost is
    /// O(N) in the nodes, and the optimizer re-propagates after
    /// each rewrite.
    pub fn metadata(&self) -> Metadata {
        match self {
            Comprehension::Clause { source, .. } => clause_metadata(source),
            Comprehension::Cartesian { children } => cartesian_metadata(children),
            Comprehension::Zip { children, mode } => zip_metadata(children, *mode),
            Comprehension::Union { children } => union_metadata(children),
            Comprehension::Filter { child, .. } => filter_metadata(child),
            Comprehension::Order {
                child,
                strategy,
                truncation,
                ..
            } => order_metadata(child, *strategy, *truncation),
        }
    }
}

fn clause_metadata(source: &Source) -> Metadata {
    let cardinality = source.cardinality();
    let (index_addressable, natural_order) = match &cardinality {
        CardinalityClass::Bounded(n) => (
            Some(IndexFn::Lattice {
                axis_sizes: vec![*n],
            }),
            NaturalOrder::Lex,
        ),
        CardinalityClass::Continuous { intervals, measure } => (
            Some(IndexFn::Continuous {
                intervals: intervals.clone(),
                measure: measure.clone(),
            }),
            NaturalOrder::PendingSampling,
        ),
        // BoundedAtMost / Unbounded / ContinuousAtMost — no
        // closed-form addressing function exists.
        _ => (None, NaturalOrder::Lex),
    };
    Metadata {
        cardinality,
        index_addressable,
        natural_order,
        materialization: Materialization::Streaming,
    }
}

fn cartesian_metadata(children: &[Comprehension]) -> Metadata {
    // First detect dependent sources: any child whose source
    // expression references an earlier child's coordinate name.
    // Dependent → index_addressable = None.
    let dependent = detect_dependent_sources(children);

    let child_meta: Vec<Metadata> = children.iter().map(|c| c.metadata()).collect();
    let cardinality = combine_cartesian_cardinality(&child_meta);

    let index_addressable = if dependent {
        None
    } else {
        combine_cartesian_index_fn(&child_meta)
    };

    let natural_order = if matches!(
        cardinality,
        CardinalityClass::Continuous { .. } | CardinalityClass::Hybrid(_)
    ) {
        NaturalOrder::PendingSampling
    } else {
        NaturalOrder::Lex
    };

    Metadata {
        cardinality,
        index_addressable,
        natural_order,
        materialization: Materialization::Streaming,
    }
}

fn zip_metadata(children: &[Comprehension], mode: ZipMode) -> Metadata {
    let child_meta: Vec<Metadata> = children.iter().map(|c| c.metadata()).collect();
    let cardinality = combine_zip_cardinality(&child_meta, mode);
    let index_addressable = combine_zip_index_fn(&child_meta, mode);

    let materialization = match mode {
        ZipMode::Strict | ZipMode::Truncate => Materialization::Streaming,
        ZipMode::Cycle => cycle_materialization(&cycle_operands(&child_meta)),
    };

    Metadata {
        cardinality,
        index_addressable,
        natural_order: NaturalOrder::Lockstep,
        materialization,
    }
}

/// The tuple count of a `zip(Cycle)` over operands of these counts:
/// the longest operand's, or zero when any operand is empty. Every
/// tuple binds every operand's names, and an empty operand has no
/// tuple to cycle.
pub fn cycle_length(counts: &[u64]) -> u64 {
    if counts.contains(&0) {
        0
    } else {
        counts.iter().copied().max().unwrap_or(0)
    }
}

/// `true` when metadata alone shows the operand yields no tuple.
fn known_empty(m: &Metadata) -> bool {
    matches!(
        m.cardinality,
        CardinalityClass::Bounded(0) | CardinalityClass::BoundedAtMost(0)
    )
}

/// How a `zip(Cycle)` holds one operand while it cycles (comprehension_forms.md §6.2,
/// §6.3).
///
/// Cycling re-emits an operand's earlier tuples once it is exhausted
/// and a longer operand is not. An index-addressable operand is read
/// at `i mod |operand|` directly and holds nothing; one operand that
/// is not addressable streams, and is restarted when it runs out
/// before the zip does; every other operand that is not addressable
/// is buffered in full. An operand found empty empties the zip, so an
/// executor checks the indexed operands' lengths and drains the
/// buffered operands in ascending bound before it holds any tuple,
/// and stops at the first empty one.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum CycleOperand {
    /// Read at `i mod |operand|` through its index function.
    Indexed,
    /// Pulled once per tuple, and restarted when it runs out before
    /// the zip does.
    Streamed,
    /// Held in full and replayed. A bound of zero marks an operand
    /// known empty: the executor drains it first and holds nothing.
    Buffered {
        /// Tuples the buffer holds at most; `None` when the operand's
        /// count is unknown before it is evaluated.
        bound: Option<u64>,
    },
}

/// The plan a `zip(Cycle)` over operands with these bundles executes:
/// an operand known empty is buffered with bound zero; any other
/// addressable discrete operand is [`CycleOperand::Indexed`]; of the
/// rest, the first whose count is unknown streams, or when every count
/// is known, the first with the largest bound; every other operand is
/// buffered.
pub fn cycle_operands(children: &[Metadata]) -> Vec<CycleOperand> {
    let indexed = |m: &Metadata| {
        !known_empty(m)
            && m.index_addressable
                .as_ref()
                .is_some_and(|idx| !idx.has_continuous_axis())
    };
    let bound = |m: &Metadata| match &m.cardinality {
        CardinalityClass::Bounded(n) | CardinalityClass::BoundedAtMost(n) => Some(*n),
        _ => None,
    };
    let rest: Vec<usize> = (0..children.len())
        .filter(|&i| !indexed(&children[i]) && !known_empty(&children[i]))
        .collect();
    let streamed = rest
        .iter()
        .copied()
        .find(|&i| bound(&children[i]).is_none())
        .or_else(|| {
            rest.iter()
                .copied()
                .rev()
                .max_by_key(|&i| bound(&children[i]))
        });
    children
        .iter()
        .enumerate()
        .map(|(i, m)| {
            if indexed(m) {
                CycleOperand::Indexed
            } else if Some(i) == streamed {
                CycleOperand::Streamed
            } else {
                CycleOperand::Buffered { bound: bound(m) }
            }
        })
        .collect()
}

/// `true` when `plan` holds an operand known empty, so the zip yields
/// no tuple and buffers nothing.
pub fn cycle_plan_is_empty(plan: &[CycleOperand]) -> bool {
    plan.contains(&CycleOperand::Buffered { bound: Some(0) })
}

/// A `zip(Cycle)`'s working set under `plan`: the buffered operands'
/// bounds summed, unbounded when one of them has no bound, and
/// streaming when nothing is buffered or an operand is known empty.
pub fn cycle_materialization(plan: &[CycleOperand]) -> Materialization {
    if cycle_plan_is_empty(plan) {
        return Materialization::Streaming;
    }
    let mut total: u64 = 0;
    let mut buffered = false;
    for operand in plan {
        if let CycleOperand::Buffered { bound } = operand {
            buffered = true;
            match bound {
                Some(n) => total = total.saturating_add(*n),
                None => return Materialization::UnboundedBarrier,
            }
        }
    }
    if buffered {
        Materialization::BoundedBarrier {
            working_set_size: total,
        }
    } else {
        Materialization::Streaming
    }
}

fn union_metadata(children: &[Comprehension]) -> Metadata {
    let child_meta: Vec<Metadata> = children.iter().map(|c| c.metadata()).collect();
    let cardinality = combine_union_cardinality(&child_meta);
    let index_addressable = combine_union_index_fn(&child_meta);
    Metadata {
        cardinality,
        index_addressable,
        natural_order: NaturalOrder::Sequential,
        materialization: Materialization::Streaming,
    }
}

fn filter_metadata(child: &Comprehension) -> Metadata {
    let child_meta = child.metadata();
    let cardinality = match &child_meta.cardinality {
        // Filtering nothing keeps exactly nothing.
        CardinalityClass::Bounded(0) | CardinalityClass::BoundedAtMost(0) => {
            CardinalityClass::Bounded(0)
        }
        CardinalityClass::Bounded(n) | CardinalityClass::BoundedAtMost(n) => {
            CardinalityClass::BoundedAtMost(*n)
        }
        CardinalityClass::Unbounded => CardinalityClass::Unbounded,
        CardinalityClass::Continuous { intervals, measure }
        | CardinalityClass::ContinuousAtMost {
            intervals,
            measure_at_most: measure,
        } => CardinalityClass::ContinuousAtMost {
            intervals: intervals.clone(),
            measure_at_most: measure.clone(),
        },
        CardinalityClass::Hybrid(h) => CardinalityClass::Hybrid(h.clone()),
    };
    Metadata {
        cardinality,
        index_addressable: None, // filter destroys the bijection
        natural_order: child_meta.natural_order,
        materialization: child_meta.materialization,
    }
}

fn order_metadata(
    child: &Comprehension,
    strategy: StrategyName,
    truncation: Option<u64>,
) -> Metadata {
    let child_meta = child.metadata();
    let cardinality = order_cardinality(child, &child_meta.cardinality, strategy, truncation);

    // An order's output is addressed through its selection: position `i`
    // is the input's tuple at the `i`-th selected position, so the
    // output is one axis as long as the selection (comprehension_forms.md
    // §3.6). The axis is sized by the order's count, or its bound.
    let selected = || match &cardinality {
        CardinalityClass::Bounded(n) | CardinalityClass::BoundedAtMost(n) => {
            Some(IndexFn::Lattice {
                axis_sizes: vec![*n],
            })
        }
        _ => None,
    };
    let (index_addressable, natural_order, materialization) = match strategy {
        StrategyName::Lex => (
            // An untruncated `Lex` passes its input through, positions
            // and all. A truncated one selects a prefix of an addressable
            // input's positions; over any other input it counts the
            // tuples as they stream and addresses nothing.
            match truncation {
                None => child_meta.index_addressable,
                Some(_) => child_meta.index_addressable.and_then(|_| selected()),
            },
            NaturalOrder::Lex,
            child_meta.materialization, // counter wrapper at most
        ),
        non_lex => {
            // Over an addressable input the strategy selects positions
            // and holds only its selection (R2); over any other input
            // the input is buffered in full first.
            let materialization = match &child_meta.index_addressable {
                Some(_) => Materialization::BoundedBarrier {
                    working_set_size: strategy_working_set(
                        non_lex,
                        &child_meta.index_addressable,
                        truncation,
                    ),
                },
                None => match &child_meta.cardinality {
                    CardinalityClass::Bounded(n) | CardinalityClass::BoundedAtMost(n) => {
                        Materialization::BoundedBarrier {
                            working_set_size: *n,
                        }
                    }
                    _ => Materialization::UnboundedBarrier,
                },
            };
            (selected(), NaturalOrder::Strategy(non_lex), materialization)
        }
    };

    Metadata {
        cardinality,
        index_addressable,
        natural_order,
        materialization,
    }
}

// ---- cardinality combinators ----

fn combine_cartesian_cardinality(children: &[Metadata]) -> CardinalityClass {
    let mut has_continuous = false;
    let mut has_discrete = false;
    let mut counts: Vec<Count> = Vec::new();
    let mut discrete_axes: Vec<u64> = Vec::new();
    let mut continuous_intervals: Vec<Interval> = Vec::new();
    let mut continuous_measures: Vec<ProductMeasure> = Vec::new();

    for m in children {
        match &m.cardinality {
            CardinalityClass::Bounded(n) | CardinalityClass::BoundedAtMost(n) => {
                has_discrete = true;
                discrete_axes.push(*n); // the count, or its upper bound
                counts.extend(Count::of(&m.cardinality));
            }
            CardinalityClass::Unbounded => {
                has_discrete = true;
                discrete_axes.push(0);
                counts.push(Count::Unknown);
            }
            CardinalityClass::Continuous { intervals, measure }
            | CardinalityClass::ContinuousAtMost {
                intervals,
                measure_at_most: measure,
            } => {
                has_continuous = true;
                continuous_intervals.extend(intervals.iter().cloned());
                continuous_measures.push(measure.clone());
            }
            CardinalityClass::Hybrid(h) => {
                has_continuous = true;
                has_discrete = true;
                discrete_axes.extend(h.discrete_axes.iter().copied());
                continuous_intervals.extend(h.continuous_axes.iter().cloned());
                continuous_measures.push(h.measure.clone());
            }
        }
    }

    if has_continuous && has_discrete {
        CardinalityClass::Hybrid(Hybrid {
            discrete_axes,
            continuous_axes: continuous_intervals,
            measure: simplify_measures(continuous_measures),
        })
    } else if has_continuous {
        CardinalityClass::Continuous {
            intervals: continuous_intervals,
            measure: simplify_measures(continuous_measures),
        }
    } else {
        cartesian_count(&counts).class()
    }
}

fn combine_cartesian_index_fn(children: &[Metadata]) -> Option<IndexFn> {
    // All children must be addressable for the cartesian to be.
    let all_addressable = children.iter().all(|m| m.index_addressable.is_some());
    if !all_addressable {
        return None;
    }

    let mut all_discrete = true;
    let mut all_continuous = true;
    let mut discrete_axes: Vec<u64> = Vec::new();
    let mut continuous_intervals: Vec<Interval> = Vec::new();
    let mut continuous_measures: Vec<ProductMeasure> = Vec::new();

    for m in children {
        match m.index_addressable.as_ref().unwrap() {
            IndexFn::Lattice { axis_sizes } => {
                all_continuous = false;
                discrete_axes.extend(axis_sizes.iter().copied());
            }
            IndexFn::Continuous { intervals, measure } => {
                all_discrete = false;
                continuous_intervals.extend(intervals.iter().cloned());
                continuous_measures.push(measure.clone());
            }
            IndexFn::Hybrid {
                discrete_axes: d,
                continuous_axes: c,
                measure,
            } => {
                all_discrete = false;
                all_continuous = false;
                discrete_axes.extend(d.iter().copied());
                continuous_intervals.extend(c.iter().cloned());
                continuous_measures.push(measure.clone());
            }
            // Lockstep / Modular / Concatenation — these don't
            // combine as cartesian axes (they're 1-D index
            // spaces of their own), and a cartesian of a zip or a
            // union has no addressing scheme here, so it has none.
            IndexFn::Lockstep { .. } | IndexFn::Modular { .. } | IndexFn::Concatenation { .. } => {
                return None;
            }
        }
    }

    if all_discrete {
        Some(IndexFn::Lattice {
            axis_sizes: discrete_axes,
        })
    } else if all_continuous {
        Some(IndexFn::Continuous {
            intervals: continuous_intervals,
            measure: simplify_measures(continuous_measures),
        })
    } else {
        Some(IndexFn::Hybrid {
            discrete_axes,
            continuous_axes: continuous_intervals,
            measure: simplify_measures(continuous_measures),
        })
    }
}

fn combine_zip_cardinality(children: &[Metadata], mode: ZipMode) -> CardinalityClass {
    // A continuous child is a V7 failure; its count is unknown here.
    let counts: Vec<Count> = children
        .iter()
        .map(|m| Count::of(&m.cardinality).unwrap_or(Count::Unknown))
        .collect();
    match mode {
        ZipMode::Strict => strict_zip_count(&counts),
        ZipMode::Truncate => truncate_zip_count(&counts),
        ZipMode::Cycle => cycle_zip_count(&counts),
    }
    .class()
}

// ---- tuple counts ----

/// A discrete tuple count as metadata knows it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Count {
    /// Exactly this many.
    Exact(u64),
    /// Between zero and this many.
    AtMost(u64),
    /// No known bound.
    Unknown,
}

impl Count {
    /// The count a discrete class states, `None` for a continuous one.
    /// At most zero is exactly zero.
    fn of(class: &CardinalityClass) -> Option<Self> {
        Some(match class {
            CardinalityClass::Bounded(n) | CardinalityClass::BoundedAtMost(n @ 0) => {
                Count::Exact(*n)
            }
            CardinalityClass::BoundedAtMost(n) => Count::AtMost(*n),
            CardinalityClass::Unbounded => Count::Unknown,
            _ => return None,
        })
    }

    fn bound(self) -> Option<u64> {
        match self {
            Count::Exact(n) | Count::AtMost(n) => Some(n),
            Count::Unknown => None,
        }
    }

    /// `n` exactly when every count it was combined from is exact, at
    /// most `n` otherwise.
    fn combined(n: u64, counts: &[Count]) -> Self {
        if counts.iter().all(|c| matches!(c, Count::Exact(_))) {
            Count::Exact(n)
        } else {
            Count::AtMost(n)
        }
    }

    fn class(self) -> CardinalityClass {
        match self {
            Count::Exact(n) | Count::AtMost(n @ 0) => CardinalityClass::Bounded(n),
            Count::AtMost(n) => CardinalityClass::BoundedAtMost(n),
            Count::Unknown => CardinalityClass::Unbounded,
        }
    }
}

/// A cartesian's count: exactly zero when an operand is exactly empty,
/// whatever the others; unknown when an operand's count is; otherwise
/// the product of the operands' counts, exact when every one is.
fn cartesian_count(counts: &[Count]) -> Count {
    if counts.contains(&Count::Exact(0)) {
        return Count::Exact(0);
    }
    let Some(bounds) = counts.iter().map(|c| c.bound()).collect::<Option<Vec<_>>>() else {
        return Count::Unknown;
    };
    Count::combined(bounds.into_iter().fold(1, u64::saturating_mul), counts)
}

/// A truncating zip's count: the shortest operand's. Exactly zero when
/// an operand is exactly empty; exact when every operand is; otherwise
/// at most the least bound, since an operand at most `m` or of unknown
/// count may end at any length before `m`. Unknown only when no operand
/// has a bound.
fn truncate_zip_count(counts: &[Count]) -> Count {
    if counts.contains(&Count::Exact(0)) {
        return Count::Exact(0);
    }
    match counts.iter().filter_map(|c| c.bound()).min() {
        Some(m) => Count::combined(m, counts),
        None => Count::Unknown,
    }
}

/// A strict zip's count when it yields: every operand's, so an exact
/// operand's count exactly; otherwise at most the least bound. Operands
/// that end apart fail the zip instead.
fn strict_zip_count(counts: &[Count]) -> Count {
    if let Some(exact) = counts.iter().find(|c| matches!(c, Count::Exact(_))) {
        return *exact;
    }
    match counts.iter().filter_map(|c| c.bound()).min() {
        Some(m) => Count::AtMost(m),
        None => Count::Unknown,
    }
}

/// A cycle zip's count ([`cycle_length`]): exactly zero when an operand
/// is exactly empty; unknown when an operand's count is; otherwise the
/// longest operand's, exact when every operand is exact, and at most
/// that when one is at most, since it may be empty at open.
fn cycle_zip_count(counts: &[Count]) -> Count {
    if counts.contains(&Count::Exact(0)) {
        return Count::Exact(0);
    }
    let Some(bounds) = counts.iter().map(|c| c.bound()).collect::<Option<Vec<_>>>() else {
        return Count::Unknown;
    };
    Count::combined(bounds.into_iter().max().unwrap_or(0), counts)
}

/// A union's count: the sum of its operands', exact when every one is,
/// unknown when one is.
fn union_count(counts: &[Count]) -> Count {
    let Some(bounds) = counts.iter().map(|c| c.bound()).collect::<Option<Vec<_>>>() else {
        return Count::Unknown;
    };
    Count::combined(bounds.into_iter().fold(0, u64::saturating_add), counts)
}

/// An order's count. `Lex` and the strategies that truncate by tuple
/// count (`reverse_lex`, `diagonal`, `antidiagonal`, `halton`, `sobol`,
/// `lhs`, `shuffle`) keep `min(count, n)` of a discrete input; `extrema`
/// and `shells` truncate by whole strata or shells, so they keep at
/// most the input's count. Over a continuous space a sampling strategy
/// draws `n` points, exactly `n` unless a filter in the space or a
/// discrete axis of inexact count may leave fewer; `extrema` takes
/// the strata of the box, each continuous axis contributing its two
/// ends.
fn order_cardinality(
    child: &Comprehension,
    child_class: &CardinalityClass,
    strategy: StrategyName,
    truncation: Option<u64>,
) -> CardinalityClass {
    let strata = matches!(strategy, StrategyName::Extrema | StrategyName::Shells);
    let Some(count) = Count::of(child_class) else {
        // A continuous space, sampled by a non-`Lex` order with a count
        // (V8); any other order over one is invalid and keeps its class.
        let Some(n) = truncation.filter(|_| !matches!(strategy, StrategyName::Lex)) else {
            return child_class.clone();
        };
        let mut space = SampledSpace::default();
        space.collect(child);
        if space.discrete.contains(&Count::Exact(0)) {
            return CardinalityClass::Bounded(0);
        }
        return if matches!(strategy, StrategyName::Extrema) {
            // `n` strata of the box: at most all of its corners.
            let mut axes = space.discrete;
            axes.extend(std::iter::repeat_n(Count::Exact(2), space.continuous));
            match cartesian_count(&axes) {
                Count::Exact(m) | Count::AtMost(m) => Count::AtMost(m),
                Count::Unknown => Count::Unknown,
            }
        } else if !space.filtered && space.discrete.iter().all(|c| matches!(c, Count::Exact(_))) {
            Count::Exact(n)
        } else {
            Count::AtMost(n)
        }
        .class();
    };
    match (count, truncation) {
        (Count::Exact(0), _) | (_, None) => count,
        (Count::Exact(c) | Count::AtMost(c), Some(_)) if strata => Count::AtMost(c),
        (Count::Exact(c), Some(n)) => Count::Exact(c.min(n)),
        (Count::AtMost(c), Some(n)) => Count::AtMost(c.min(n)),
        (Count::Unknown, Some(_)) if strata => Count::Unknown,
        (Count::Unknown, Some(n)) => Count::AtMost(n),
    }
    .class()
}

/// The axes an order over a continuous space samples, walked as the
/// runtime walks them: clauses through cartesians and filters, any
/// other node one discrete axis of its tuples.
#[derive(Default)]
struct SampledSpace {
    /// Each discrete axis's count.
    discrete: Vec<Count>,
    /// How many continuous axes.
    continuous: usize,
    /// Whether a filter sits between the order and its clauses.
    filtered: bool,
}

impl SampledSpace {
    fn collect(&mut self, c: &Comprehension) {
        match c {
            Comprehension::Clause { source, .. } => match source.cardinality() {
                CardinalityClass::Continuous { .. } => self.continuous += 1,
                class => self
                    .discrete
                    .push(Count::of(&class).unwrap_or(Count::Unknown)),
            },
            Comprehension::Cartesian { children } => {
                children.iter().for_each(|child| self.collect(child));
            }
            Comprehension::Filter { child, .. } => {
                self.filtered = true;
                self.collect(child);
            }
            other => self
                .discrete
                .push(Count::of(&other.metadata().cardinality).unwrap_or(Count::Unknown)),
        }
    }
}

fn combine_zip_index_fn(children: &[Metadata], mode: ZipMode) -> Option<IndexFn> {
    let all_addressable = children.iter().all(|m| m.index_addressable.is_some());
    if !all_addressable {
        return None;
    }
    let counts: Vec<u64> = children
        .iter()
        .filter_map(|m| match &m.cardinality {
            CardinalityClass::Bounded(n) | CardinalityClass::BoundedAtMost(n) => Some(*n),
            _ => None,
        })
        .collect();
    if counts.len() != children.len() {
        return None;
    }
    match mode {
        ZipMode::Strict | ZipMode::Truncate => {
            let length = match mode {
                ZipMode::Strict => counts[0],
                ZipMode::Truncate => *counts.iter().min().unwrap(),
                ZipMode::Cycle => unreachable!(),
            };
            Some(IndexFn::Lockstep { length })
        }
        ZipMode::Cycle => Some(IndexFn::Modular { axis_sizes: counts }),
    }
}

fn combine_union_cardinality(children: &[Metadata]) -> CardinalityClass {
    // A continuous child is a V9 failure; its count is unknown here.
    let counts: Vec<Count> = children
        .iter()
        .map(|m| Count::of(&m.cardinality).unwrap_or(Count::Unknown))
        .collect();
    union_count(&counts).class()
}

fn combine_union_index_fn(children: &[Metadata]) -> Option<IndexFn> {
    let all_addressable = children.iter().all(|m| m.index_addressable.is_some());
    if !all_addressable {
        return None;
    }
    let segment_sizes: Vec<u64> = children
        .iter()
        .filter_map(|m| match &m.cardinality {
            CardinalityClass::Bounded(n) | CardinalityClass::BoundedAtMost(n) => Some(*n),
            _ => None,
        })
        .collect();
    if segment_sizes.len() != children.len() {
        return None;
    }
    Some(IndexFn::Concatenation { segment_sizes })
}

// ---- supporting helpers ----

fn simplify_measures(measures: Vec<ProductMeasure>) -> ProductMeasure {
    match measures.len() {
        0 => ProductMeasure::Uniform,
        1 => measures.into_iter().next().unwrap(),
        _ => ProductMeasure::Product(measures),
    }
}

/// Strategy-specific working-set size for use as
/// `BoundedBarrier.working_set_size` over an addressable input: the
/// selection the strategy holds, since it reads tuples only at the
/// positions it selects (R2).
fn strategy_working_set(
    strategy: StrategyName,
    input: &Option<IndexFn>,
    truncation: Option<u64>,
) -> u64 {
    match (strategy, input, truncation) {
        // Halton / Sobol / Shuffle over an index-addressable
        // input + truncation: O(n) draws.
        (StrategyName::Halton, Some(_), Some(n))
        | (StrategyName::Sobol, Some(_), Some(n))
        | (StrategyName::Shuffle, Some(_), Some(n))
        | (StrategyName::ReverseLex, Some(_), Some(n)) => n,
        // Lhs: O(n * dim).
        (StrategyName::Lhs, Some(idx), Some(n)) => {
            let dim = lattice_dim(idx).max(1);
            n.saturating_mul(dim as u64)
        }
        // Extrema (comprehension_forms.md §3.6) and Shells rank every multi-index of
        // the input's index space before keeping the first strata or
        // shells, so they hold the whole index space.
        (StrategyName::Extrema, Some(idx), Some(_))
        | (StrategyName::Shells, Some(idx), Some(_)) => index_fn_cardinality(idx),
        // Diagonal / Antidiagonal walk the diagonals in order and stop
        // at `n`.
        (StrategyName::Diagonal, Some(_), Some(n))
        | (StrategyName::Antidiagonal, Some(_), Some(n)) => n,
        // No truncation: fall back to the input's cardinality.
        (_, Some(idx), None) => index_fn_cardinality(idx),
        // No addressable input: we can't compute a closed form;
        // use the naïve "input cardinality" placeholder so the
        // metadata still has a number (consumers should treat
        // this as a conservative upper bound).
        (_, None, Some(n)) => n,
        (_, None, None) => 0,
        // Lex with truncation over addressable input — counter
        // wrapper, working set equals output size.
        (StrategyName::Lex, Some(_), Some(n)) => n,
    }
}

fn lattice_dim(idx: &IndexFn) -> usize {
    match idx {
        IndexFn::Lattice { axis_sizes } => axis_sizes.len(),
        IndexFn::Continuous { intervals, .. } => intervals.len(),
        IndexFn::Hybrid {
            discrete_axes,
            continuous_axes,
            ..
        } => discrete_axes.len() + continuous_axes.len(),
        IndexFn::Lockstep { .. } | IndexFn::Modular { .. } | IndexFn::Concatenation { .. } => 1,
    }
}

fn index_fn_cardinality(idx: &IndexFn) -> u64 {
    match idx {
        IndexFn::Lattice { axis_sizes } => axis_sizes
            .iter()
            .copied()
            .fold(1u64, |a, b| a.saturating_mul(b)),
        IndexFn::Lockstep { length } => *length,
        IndexFn::Modular { axis_sizes } => cycle_length(axis_sizes),
        IndexFn::Concatenation { segment_sizes } => segment_sizes
            .iter()
            .copied()
            .fold(0u64, |a, b| a.saturating_add(b)),
        // Continuous index has no integer cardinality.
        IndexFn::Continuous { .. } | IndexFn::Hybrid { .. } => 0,
    }
}

/// Walk children's source expressions for back-references to
/// earlier-axis coordinate names. Used by cartesian metadata
/// propagation to detect dependent sources (comprehension_forms.md §3.2).
fn detect_dependent_sources(children: &[Comprehension]) -> bool {
    let mut prior_names: Vec<String> = Vec::new();
    for child in children {
        // First check if the child references any prior name in
        // its source(s).
        for name in collect_source_name_references(child) {
            if prior_names.contains(&name) {
                return true;
            }
        }
        // Then add this child's coordinates to the prior set.
        for n in child.coordinate_names() {
            if !prior_names.contains(&n) {
                prior_names.push(n);
            }
        }
    }
    false
}

/// The names the sources of a comprehension subtree read when they are
/// evaluated ([`Source::names_read`]), a composed name's leaves
/// included.
fn collect_source_name_references(c: &Comprehension) -> std::collections::BTreeSet<String> {
    c.source_names_read()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::comprehension::source::{LiteralValue, Source};

    fn clause(name: &str, vs: &[i64]) -> Comprehension {
        Comprehension::clause(
            name,
            Source::Literal {
                values: vs.iter().map(|n| LiteralValue::Int(*n)).collect(),
            },
        )
    }

    fn continuous_clause(name: &str) -> Comprehension {
        Comprehension::clause(
            name,
            Source::ContinuousInterval {
                interval: Interval::closed(0.0, 1.0),
                measure: ProductMeasure::Uniform,
            },
        )
    }

    #[test]
    fn clause_metadata_for_bounded_source() {
        let m = clause("k", &[1, 2, 3]).metadata();
        assert_eq!(m.cardinality, CardinalityClass::Bounded(3));
        assert_eq!(
            m.index_addressable,
            Some(IndexFn::Lattice {
                axis_sizes: vec![3]
            })
        );
        assert_eq!(m.natural_order, NaturalOrder::Lex);
        assert_eq!(m.materialization, Materialization::Streaming);
    }

    #[test]
    fn clause_metadata_for_continuous_source() {
        let m = continuous_clause("alpha").metadata();
        assert!(matches!(m.cardinality, CardinalityClass::Continuous { .. }));
        assert!(matches!(
            m.index_addressable,
            Some(IndexFn::Continuous { .. })
        ));
        assert_eq!(m.natural_order, NaturalOrder::PendingSampling);
        assert_eq!(m.materialization, Materialization::Streaming);
    }

    #[test]
    fn cartesian_metadata_combines_lattice_axes() {
        let c =
            Comprehension::cartesian(vec![clause("k", &[1, 2]), clause("limit", &[10, 20, 30])]);
        let m = c.metadata();
        assert_eq!(m.cardinality, CardinalityClass::Bounded(6));
        assert_eq!(
            m.index_addressable,
            Some(IndexFn::Lattice {
                axis_sizes: vec![2, 3]
            })
        );
        assert_eq!(m.natural_order, NaturalOrder::Lex);
    }

    #[test]
    fn cartesian_metadata_for_hybrid() {
        let c =
            Comprehension::cartesian(vec![clause("k", &[1, 2, 3, 4]), continuous_clause("theta")]);
        let m = c.metadata();
        match m.cardinality {
            CardinalityClass::Hybrid(h) => {
                assert_eq!(h.discrete_axes, vec![4]);
                assert_eq!(h.continuous_axes.len(), 1);
            }
            other => panic!("expected Hybrid, got {other:?}"),
        }
        assert!(matches!(m.index_addressable, Some(IndexFn::Hybrid { .. })));
        assert_eq!(m.natural_order, NaturalOrder::PendingSampling);
    }

    #[test]
    fn dependent_cartesian_produces_none_addressable() {
        // clause replicas references {k} from the prior clause.
        let dependent = Comprehension::cartesian(vec![
            clause("k", &[1, 2, 3]),
            Comprehension::clause(
                "replicas",
                Source::Generator {
                    expr: "range(0, 2 * {k})".into(),
                    cardinality_hint: Some(6),
                },
            ),
        ]);
        let m = dependent.metadata();
        assert!(m.index_addressable.is_none());
    }

    #[test]
    fn zip_strict_produces_lockstep_index_fn() {
        let c = Comprehension::zip(
            vec![clause("x", &[1, 2, 3]), clause("y", &[10, 20, 30])],
            ZipMode::Strict,
        );
        let m = c.metadata();
        assert_eq!(m.index_addressable, Some(IndexFn::Lockstep { length: 3 }));
        assert_eq!(m.natural_order, NaturalOrder::Lockstep);
        assert_eq!(m.materialization, Materialization::Streaming);
    }

    #[test]
    fn zip_cycle_produces_modular_index_fn_and_barrier() {
        let c = Comprehension::zip(
            vec![clause("k", &[1, 2, 3, 4, 5]), clause("color", &[1, 2, 3])],
            ZipMode::Cycle,
        );
        let m = c.metadata();
        match m.index_addressable {
            Some(IndexFn::Modular { axis_sizes }) => {
                assert_eq!(axis_sizes, vec![5, 3]);
            }
            other => panic!("expected Modular, got {other:?}"),
        }
        // Both operands are addressable: cycling reads the shorter one
        // at `i mod 3` and buffers nothing.
        assert_eq!(m.materialization, Materialization::Streaming);
    }

    fn unknown_count(name: &str) -> Comprehension {
        Comprehension::clause(
            name,
            Source::Generator {
                expr: "values({n})".into(),
                cardinality_hint: None,
            },
        )
    }

    /// With an operand of unknown count, that operand streams and every
    /// finite operand that is not addressable is buffered in full;
    /// addressable ones are indexed.
    #[test]
    fn zip_cycle_with_an_unknown_count_buffers_every_finite_unaddressable_operand() {
        let c = Comprehension::zip(
            vec![
                unknown_count("tick"),
                Comprehension::filter(clause("a", &(0..1000).collect::<Vec<_>>()), "{a} > 1"),
                Comprehension::filter(clause("b", &[1, 2, 3]), "{b} > 0"),
                clause("color", &[1, 2, 3]),
            ],
            ZipMode::Cycle,
        );
        let plan = cycle_operands(
            &match &c {
                Comprehension::Zip { children, .. } => children,
                _ => unreachable!(),
            }
            .iter()
            .map(Comprehension::metadata)
            .collect::<Vec<_>>(),
        );
        assert_eq!(
            plan,
            vec![
                CycleOperand::Streamed,
                CycleOperand::Buffered { bound: Some(1000) },
                CycleOperand::Buffered { bound: Some(3) },
                CycleOperand::Indexed,
            ]
        );
        assert_eq!(
            c.metadata().materialization,
            Materialization::BoundedBarrier {
                working_set_size: 1003
            }
        );
    }

    /// With every count known, the largest operand that is not
    /// addressable streams and the others are buffered.
    #[test]
    fn zip_cycle_streams_the_largest_unaddressable_operand() {
        let c = Comprehension::zip(
            vec![
                Comprehension::filter(clause("a", &[1, 2]), "{a} > 0"),
                Comprehension::filter(clause("b", &[1, 2, 3, 4]), "{b} > 0"),
                clause("k", &(0..100).collect::<Vec<_>>()),
            ],
            ZipMode::Cycle,
        );
        assert_eq!(
            c.metadata().materialization,
            Materialization::BoundedBarrier {
                working_set_size: 2
            }
        );
    }

    /// An operand known empty empties the zip: no tuple, no index, and
    /// nothing held, in any position and beside operands of unknown
    /// count.
    #[test]
    fn zip_cycle_with_an_operand_known_empty_is_empty() {
        let operands = || {
            vec![
                unknown_count("tick"),
                Comprehension::filter(clause("a", &[1, 2, 3]), "{a} > 1"),
                clause("color", &[1, 2, 3]),
            ]
        };
        let empties = [
            clause("e", &[]),
            Comprehension::filter(clause("e", &[]), "{e} > 1"),
        ];
        for empty in &empties {
            for at in 0..=3 {
                let mut children = operands();
                children.insert(at, empty.clone());
                let plan = cycle_operands(
                    &children
                        .iter()
                        .map(Comprehension::metadata)
                        .collect::<Vec<_>>(),
                );
                assert_eq!(plan[at], CycleOperand::Buffered { bound: Some(0) });
                assert!(cycle_plan_is_empty(&plan));
                let m = Comprehension::zip(children, ZipMode::Cycle).metadata();
                assert_eq!(m.cardinality, CardinalityClass::Bounded(0));
                assert_eq!(m.materialization, Materialization::Streaming);
            }
        }
        let addressable = Comprehension::zip(
            vec![clause("k", &[1, 2, 3]), clause("e", &[])],
            ZipMode::Cycle,
        );
        let m = addressable.metadata();
        assert_eq!(
            m.index_addressable,
            Some(IndexFn::Modular {
                axis_sizes: vec![3, 0]
            })
        );
        assert_eq!(
            index_fn_cardinality(m.index_addressable.as_ref().unwrap()),
            0
        );
        assert_eq!(cycle_length(&[3, 0]), 0);
        assert_eq!(cycle_length(&[3, 5]), 5);
    }

    /// An operand that may be empty at open makes the zip's count an
    /// upper bound.
    #[test]
    fn zip_cycle_over_an_operand_at_most_counts_at_most() {
        let c = Comprehension::zip(
            vec![
                clause("k", &[1, 2, 3, 4, 5]),
                Comprehension::filter(clause("a", &[1, 2]), "{a} > 1"),
            ],
            ZipMode::Cycle,
        );
        assert_eq!(c.metadata().cardinality, CardinalityClass::BoundedAtMost(5));
    }

    /// The tuple counts an operand of count `c` may have: an exact
    /// count its own, an at-most count every count up to its bound, an
    /// unknown count small and large ones.
    fn witnesses(c: Count) -> Vec<u64> {
        match c {
            Count::Exact(n) => vec![n],
            Count::AtMost(n) => (0..=n).collect(),
            Count::Unknown => (0..=7).chain([1000]).collect(),
        }
    }

    /// Assert `claim` holds for every count the operator yields over
    /// the operands' witnesses, and is tight: exact means every yield is
    /// that count, at most means none exceeds the bound and one meets
    /// it, and unknown means some yield exceeds any bound the operands
    /// state.
    fn assert_describes(claim: Count, yields: &[u64], what: &str) {
        let Some(&max) = yields.iter().max() else {
            return; // the operator never yields, as a strict zip of unequal operands
        };
        match claim {
            Count::Exact(n) => assert!(yields.iter().all(|&y| y == n), "{what}: {yields:?}"),
            Count::AtMost(n) => {
                assert!(max <= n, "{what}: {yields:?} exceed {n}");
                assert_eq!(max, n, "{what}: the bound is not tight");
                assert!(n > 0, "{what}: at most zero is exactly zero");
                assert!(
                    yields.iter().any(|&y| y != n),
                    "{what}: always {n}, so exact"
                );
            }
            Count::Unknown => assert!(max >= 1000, "{what}: bounded by {max}"),
        }
    }

    const KINDS: [Count; 5] = [
        Count::Exact(0),
        Count::Exact(3),
        Count::Exact(5),
        Count::AtMost(4),
        Count::Unknown,
    ];

    /// Every combination of the operands' witnesses.
    fn combinations(counts: &[Count]) -> Vec<Vec<u64>> {
        counts.iter().fold(vec![Vec::new()], |acc, c| {
            acc.iter()
                .flat_map(|prefix| {
                    witnesses(*c).into_iter().map(move |w| {
                        let mut next = prefix.clone();
                        next.push(w);
                        next
                    })
                })
                .collect()
        })
    }

    /// Each operator's count over every pair and triple of kinds holds
    /// for, and is tight over, what the operator yields.
    #[test]
    fn every_kind_combination_counts_what_the_operator_yields() {
        let mut shapes: Vec<Vec<Count>> = Vec::new();
        for a in KINDS {
            for b in KINDS {
                shapes.push(vec![a, b]);
                for c in KINDS {
                    shapes.push(vec![a, b, c]);
                }
            }
        }
        for counts in &shapes {
            let combos = combinations(counts);
            let product: Vec<u64> = combos.iter().map(|c| c.iter().product()).collect();
            assert_describes(
                cartesian_count(counts),
                &product,
                &format!("cartesian {counts:?}"),
            );
            let sum: Vec<u64> = combos.iter().map(|c| c.iter().sum()).collect();
            assert_describes(union_count(counts), &sum, &format!("union {counts:?}"));
            let shortest: Vec<u64> = combos.iter().map(|c| *c.iter().min().unwrap()).collect();
            assert_describes(
                truncate_zip_count(counts),
                &shortest,
                &format!("truncate {counts:?}"),
            );
            let cycled: Vec<u64> = combos.iter().map(|c| cycle_length(c)).collect();
            assert_describes(
                cycle_zip_count(counts),
                &cycled,
                &format!("cycle {counts:?}"),
            );
            let strict: Vec<u64> = combos
                .iter()
                .filter(|c| c.iter().all(|&n| n == c[0]))
                .map(|c| c[0])
                .collect();
            assert_describes(
                strict_zip_count(counts),
                &strict,
                &format!("strict {counts:?}"),
            );
        }
    }

    fn filtered(c: Comprehension) -> Comprehension {
        Comprehension::filter(c, "true")
    }

    /// The combinators report a filtered operand's bound as a bound: a
    /// product and a truncating zip over one are at most, not exactly,
    /// their count.
    #[test]
    fn an_operand_at_most_makes_a_combination_at_most() {
        let at_most = || filtered(clause("k", &[1, 2, 3, 4, 5, 6, 7, 8, 9]));
        let colors = || clause("c", &[1, 2]);
        let product = Comprehension::cartesian(vec![at_most(), colors()]);
        assert_eq!(
            product.metadata().cardinality,
            CardinalityClass::BoundedAtMost(18)
        );
        let zip = Comprehension::zip(vec![at_most(), colors()], ZipMode::Truncate);
        assert_eq!(
            zip.metadata().cardinality,
            CardinalityClass::BoundedAtMost(2)
        );
        let zip = Comprehension::zip(vec![unknown_count("u"), colors()], ZipMode::Truncate);
        assert_eq!(
            zip.metadata().cardinality,
            CardinalityClass::BoundedAtMost(2)
        );
        let product = Comprehension::cartesian(vec![unknown_count("u"), clause("e", &[])]);
        assert_eq!(product.metadata().cardinality, CardinalityClass::Bounded(0));
        let empty = filtered(clause("e", &[]));
        assert_eq!(empty.metadata().cardinality, CardinalityClass::Bounded(0));
    }

    /// An order keeps `min(count, n)` under a strategy that truncates by
    /// tuple count, at most its input's count under one that truncates
    /// by strata, and `n` samples of a continuous space unless a filter
    /// may leave fewer.
    #[test]
    fn an_order_counts_by_its_strategy() {
        let order = |c, s, t| Comprehension::order(c, s, t).metadata().cardinality;
        let ks = || clause("k", &[1, 2, 3, 4, 5, 6]);
        for s in [
            StrategyName::Lex,
            StrategyName::ReverseLex,
            StrategyName::Diagonal,
            StrategyName::Halton,
            StrategyName::Sobol,
            StrategyName::Lhs,
            StrategyName::Shuffle,
        ] {
            assert_eq!(
                order(ks(), s, Some(4)),
                CardinalityClass::Bounded(4),
                "{s:?}"
            );
            assert_eq!(
                order(ks(), s, Some(9)),
                CardinalityClass::Bounded(6),
                "{s:?}"
            );
            assert_eq!(order(ks(), s, None), CardinalityClass::Bounded(6), "{s:?}");
            assert_eq!(
                order(filtered(ks()), s, Some(4)),
                CardinalityClass::BoundedAtMost(4),
                "{s:?}"
            );
            assert_eq!(
                order(unknown_count("u"), s, Some(4)),
                CardinalityClass::BoundedAtMost(4),
                "{s:?}"
            );
        }
        for s in [StrategyName::Extrema, StrategyName::Shells] {
            assert_eq!(
                order(ks(), s, Some(1)),
                CardinalityClass::BoundedAtMost(6),
                "{s:?}"
            );
            assert_eq!(order(ks(), s, None), CardinalityClass::Bounded(6), "{s:?}");
            assert_eq!(
                order(clause("e", &[]), s, Some(1)),
                CardinalityClass::Bounded(0)
            );
        }
        let space = || Comprehension::cartesian(vec![clause("k", &[1, 2]), continuous_clause("u")]);
        assert_eq!(
            order(space(), StrategyName::Halton, Some(5)),
            CardinalityClass::Bounded(5)
        );
        assert_eq!(
            order(filtered(space()), StrategyName::Halton, Some(5)),
            CardinalityClass::BoundedAtMost(5)
        );
        assert_eq!(
            order(space(), StrategyName::Extrema, Some(1)),
            CardinalityClass::BoundedAtMost(4)
        );
        let empty_axis = Comprehension::cartesian(vec![clause("k", &[]), continuous_clause("u")]);
        assert_eq!(
            order(empty_axis, StrategyName::Sobol, Some(5)),
            CardinalityClass::Bounded(0)
        );
    }

    /// Two operands of unknown count: one streams, the other has no
    /// bound to buffer.
    #[test]
    fn zip_cycle_with_two_unknown_counts_is_unbounded() {
        let c = Comprehension::zip(vec![unknown_count("x"), unknown_count("y")], ZipMode::Cycle);
        assert_eq!(
            c.metadata().materialization,
            Materialization::UnboundedBarrier
        );
    }

    #[test]
    fn union_produces_concatenation_index_fn() {
        let a = Comprehension::cartesian(vec![clause("k", &[1, 2]), clause("limit", &[10])]);
        let b = Comprehension::cartesian(vec![clause("k", &[3, 4]), clause("limit", &[20])]);
        let u = Comprehension::union(vec![a, b]);
        let m = u.metadata();
        assert_eq!(m.cardinality, CardinalityClass::Bounded(4));
        assert_eq!(
            m.index_addressable,
            Some(IndexFn::Concatenation {
                segment_sizes: vec![2, 2]
            })
        );
        assert_eq!(m.natural_order, NaturalOrder::Sequential);
    }

    #[test]
    fn filter_destroys_addressability() {
        let inner =
            Comprehension::cartesian(vec![clause("k", &[1, 2]), clause("limit", &[10, 20])]);
        let filtered = Comprehension::filter(inner, "{k} > 0");
        let m = filtered.metadata();
        assert_eq!(m.cardinality, CardinalityClass::BoundedAtMost(4));
        assert_eq!(m.index_addressable, None);
    }

    /// An untruncated `Lex` order passes its input's addressing through;
    /// a truncated one selects a prefix of its input's positions, one
    /// axis as long as the prefix, and over a filter it addresses
    /// nothing.
    #[test]
    fn lex_order_inherits_addressability_untruncated() {
        let inner =
            Comprehension::cartesian(vec![clause("k", &[1, 2]), clause("limit", &[10, 20])]);
        let whole = Comprehension::order(inner.clone(), StrategyName::Lex, None).metadata();
        assert_eq!(whole.cardinality, CardinalityClass::Bounded(4));
        assert_eq!(
            whole.index_addressable,
            Some(IndexFn::Lattice {
                axis_sizes: vec![2, 2]
            })
        );
        assert_eq!(whole.natural_order, NaturalOrder::Lex);
        let prefix = Comprehension::order(inner.clone(), StrategyName::Lex, Some(3)).metadata();
        assert_eq!(prefix.cardinality, CardinalityClass::Bounded(3));
        assert_eq!(
            prefix.index_addressable,
            Some(IndexFn::Lattice {
                axis_sizes: vec![3]
            })
        );
        assert_eq!(prefix.natural_order, NaturalOrder::Lex);
        let streamed = Comprehension::order(
            Comprehension::filter(inner, "{k} > 1"),
            StrategyName::Lex,
            Some(3),
        )
        .metadata();
        assert_eq!(streamed.index_addressable, None);
    }

    /// Any other order addresses its output through its selection: one
    /// axis as long as the selection, over which the next order holds
    /// only its own selection.
    #[test]
    fn non_lex_order_addresses_its_selection() {
        let inner =
            Comprehension::cartesian(vec![clause("k", &[1, 2]), clause("limit", &[10, 20])]);
        let ordered = Comprehension::order(inner, StrategyName::Halton, Some(2));
        let m = ordered.metadata();
        assert_eq!(
            m.index_addressable,
            Some(IndexFn::Lattice {
                axis_sizes: vec![2]
            })
        );
        let reordered = Comprehension::order(ordered.clone(), StrategyName::Shuffle, None);
        let r = reordered.metadata();
        assert_eq!(r.cardinality, CardinalityClass::Bounded(2));
        assert_eq!(
            r.index_addressable,
            Some(IndexFn::Lattice {
                axis_sizes: vec![2]
            })
        );
        assert_eq!(
            r.materialization,
            Materialization::BoundedBarrier {
                working_set_size: 2
            }
        );
        match m.natural_order {
            NaturalOrder::Strategy(StrategyName::Halton) => {}
            other => panic!("expected Strategy(Halton), got {other:?}"),
        }
        assert_eq!(
            m.materialization,
            Materialization::BoundedBarrier {
                working_set_size: 2
            }
        );
    }

    #[test]
    fn continuous_sampling_yields_bounded_cardinality() {
        let inner =
            Comprehension::cartesian(vec![continuous_clause("alpha"), continuous_clause("beta")]);
        let ordered = Comprehension::order(inner, StrategyName::Halton, Some(100));
        let m = ordered.metadata();
        assert_eq!(m.cardinality, CardinalityClass::Bounded(100));
        assert_eq!(
            m.materialization,
            Materialization::BoundedBarrier {
                working_set_size: 100
            }
        );
    }

    #[test]
    fn metadata_propagation_is_idempotent() {
        let c = Comprehension::order(
            Comprehension::filter(
                Comprehension::cartesian(vec![clause("k", &[1, 2, 3]), clause("limit", &[10, 20])]),
                "{k} * {limit} > 5",
            ),
            StrategyName::Halton,
            Some(5),
        );
        let m1 = c.metadata();
        let m2 = c.metadata();
        assert_eq!(m1, m2);
    }

    #[test]
    fn has_continuous_axis_classifier() {
        let lat = IndexFn::Lattice {
            axis_sizes: vec![3, 4],
        };
        assert!(!lat.has_continuous_axis());

        let cont = IndexFn::Continuous {
            intervals: vec![Interval::closed(0.0, 1.0)],
            measure: ProductMeasure::Uniform,
        };
        assert!(cont.has_continuous_axis());
    }

    #[test]
    fn multi_axis_lattice_classifier() {
        assert!(
            IndexFn::Lattice {
                axis_sizes: vec![3, 4]
            }
            .is_multi_axis_lattice()
        );
        assert!(
            !IndexFn::Lattice {
                axis_sizes: vec![3]
            }
            .is_multi_axis_lattice()
        );
        assert!(
            !IndexFn::Continuous {
                intervals: vec![Interval::closed(0.0, 1.0), Interval::closed(0.0, 1.0)],
                measure: ProductMeasure::Uniform,
            }
            .is_multi_axis_lattice()
        );
    }
}