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
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! The clause form → algebra AST converter.
//!
//! Reuses the crate-internal text parser
//! for structural shape
//! recognition, then converts the
//! flat-struct AST to the new algebra-layer operator-tree
//! [`crate::comprehension::ast::Comprehension`].
//!
//! Source-string typing is handled by
//! [`super::source_parser::parse_source`] — the clauses AST
//! carries source expressions as raw strings; the algebra
//! layer requires typed [`crate::comprehension::source::Source`]
//! values at AST construction time so the validator and
//! metadata propagator can do their work statically.
//!
//! This converter is the "single bridge" the audit calls for:
//! every clauses AST funnels through here on the way to the
//! algebra layer. nb-workload's parser remains responsible for
//! turning YAML / text into clauses ASTs; polydat owns the
//! conversion onward.

use crate::comprehension::ast::Comprehension as AlgebraAst;
use crate::comprehension::clause_ast::{
    Clause as ClauseForm, ClauseSource as ClauseSourceForm, Comprehension as ClauseAst,
    ComprehensionMode as ModeForm, Subspace as SubspaceForm, TraversalOrder as OrderForm,
    ZipMode as ZipModeForm,
};
use crate::comprehension::strategy::{StrategyName, ZipMode as AlgebraZipMode};

use super::source_parser::{SourceParseError, parse_source};

/// Errors produced when converting a clauses AST to algebra.
#[derive(Debug, Clone, PartialEq)]
pub enum ConvertError {
    /// A clause's source string didn't parse to a typed `Source`.
    SourceParse {
        /// The clause's element name.
        clause_var: String,
        /// The source text.
        source: String,
        /// Why it did not parse.
        cause: SourceParseError,
    },
    /// An empty cartesian or empty union mode.
    EmptyComprehension,
    /// A union sub-space was empty.
    EmptyUnionSubspace,
    /// A parallel clause's vars and exprs had mismatched lengths
    /// (should be caught by the parser, but defensive here).
    ParallelArityMismatch {
        /// Names bound.
        vars: usize,
        /// Expressions given.
        exprs: usize,
    },
}

impl std::fmt::Display for ConvertError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ConvertError::SourceParse {
                clause_var,
                source,
                cause,
            } => write!(
                f,
                "clause {clause_var:?} source {source:?} failed to parse: {cause}"
            ),
            ConvertError::EmptyComprehension => f.write_str("comprehension has no clauses"),
            ConvertError::EmptyUnionSubspace => f.write_str("union has empty sub-space"),
            ConvertError::ParallelArityMismatch { vars, exprs } => {
                write!(f, "parallel clause vars={vars} != exprs={exprs}")
            }
        }
    }
}

impl std::error::Error for ConvertError {}

/// Convert the flat parse form to the algebra-layer
/// [`AlgebraAst`].
///
/// Handles:
/// - `mode` → cartesian / union
/// - `filter` → wrapping `Filter` node
/// - `order` → wrapping `Order` node
/// - `Clause::Single` source → typed `Source` via
///   [`parse_source`]
/// - `Clause::Parallel` source → algebra `Zip` of single-var
///   clauses (the algebra layer represents parallel iteration
///   as zip; the clauses parallel-clause shape is an inline
///   form of the same thing)
// The bridge runs one way. The algebra is what everything
// downstream reads: the evaluator consumes it directly and the
// fixtures walk it natively. This is the forward direction,
// `clauses_to_algebra`, which `ComprehensionSpec::into_algebra`
// calls to turn parser output into algebra shape.
pub fn clauses_to_algebra(clauses: &ClauseAst) -> Result<AlgebraAst, ConvertError> {
    let body = match &clauses.mode {
        ModeForm::Cartesian(clauses) => convert_cartesian(clauses)?,
        ModeForm::Union(subspaces) => convert_union(subspaces)?,
    };

    let with_filter = if let Some(pred) = &clauses.filter {
        AlgebraAst::filter(body, pred.clone())
    } else {
        body
    };

    let with_order = if let Some(order) = &clauses.order {
        let (strategy, truncation, seed) = convert_order(order)?;
        AlgebraAst::order_seeded(with_filter, strategy, truncation, seed)
    } else {
        with_filter
    };

    Ok(with_order)
}

fn convert_cartesian(clauses: &[ClauseForm]) -> Result<AlgebraAst, ConvertError> {
    if clauses.is_empty() {
        return Err(ConvertError::EmptyComprehension);
    }
    let algebra_children: Vec<AlgebraAst> = clauses
        .iter()
        .map(convert_clause)
        .collect::<Result<_, _>>()?;
    if algebra_children.len() == 1 {
        // Single clause = the clause itself (R0a I2 would
        // eliminate the singleton cartesian anyway; produce
        // the canonical form upfront).
        Ok(algebra_children.into_iter().next().unwrap())
    } else {
        Ok(AlgebraAst::cartesian(algebra_children))
    }
}

fn convert_union(subspaces: &[SubspaceForm]) -> Result<AlgebraAst, ConvertError> {
    if subspaces.is_empty() {
        return Err(ConvertError::EmptyComprehension);
    }
    let algebra_children: Vec<AlgebraAst> = subspaces
        .iter()
        .map(|s| {
            if s.is_empty() {
                Err(ConvertError::EmptyUnionSubspace)
            } else {
                convert_cartesian(&s.clauses)
            }
        })
        .collect::<Result<_, _>>()?;
    if algebra_children.len() == 1 {
        Ok(algebra_children.into_iter().next().unwrap())
    } else {
        Ok(AlgebraAst::union(algebra_children))
    }
}

fn convert_clause(clause: &ClauseForm) -> Result<AlgebraAst, ConvertError> {
    match &clause.source {
        ClauseSourceForm::Single(source_str) => {
            let var = clause
                .single_var()
                .unwrap_or_else(|| clause.first_var())
                .to_string();
            let source = parse_source(source_str).map_err(|cause| ConvertError::SourceParse {
                clause_var: var.clone(),
                source: source_str.clone(),
                cause,
            })?;
            Ok(AlgebraAst::clause(var, source))
        }
        ClauseSourceForm::Parallel { mode, exprs } => {
            if clause.vars.len() != exprs.len() {
                return Err(ConvertError::ParallelArityMismatch {
                    vars: clause.vars.len(),
                    exprs: exprs.len(),
                });
            }
            // Parallel iteration in the clause form is zip in the algebra.
            // Build a single-var clause per (var, expr) pair,
            // wrap in a Zip with the converted mode.
            let mut children = Vec::with_capacity(clause.vars.len());
            for (var, expr) in clause.vars.iter().zip(exprs.iter()) {
                let source = parse_source(expr).map_err(|cause| ConvertError::SourceParse {
                    clause_var: var.clone(),
                    source: expr.clone(),
                    cause,
                })?;
                children.push(AlgebraAst::clause(var.clone(), source));
            }
            let zip_mode = convert_zip_mode(*mode);
            Ok(AlgebraAst::zip(children, zip_mode))
        }
    }
}

fn convert_zip_mode(clauses: ZipModeForm) -> AlgebraZipMode {
    match clauses {
        ZipModeForm::Strict => AlgebraZipMode::Strict,
        ZipModeForm::Truncate => AlgebraZipMode::Truncate,
        ZipModeForm::Cycle => AlgebraZipMode::Cycle,
    }
}

/// Convert a clauses [`OrderForm`] into the algebra's
/// strategy, truncation, and seed.
///
/// The clauses `Custom { function }` form is rejected: the strategy
/// set is closed (comprehension_forms.md §3.6, §14.4).
pub(crate) fn convert_order(
    order: &OrderForm,
) -> Result<(StrategyName, Option<u64>, Option<u64>), ConvertError> {
    let triple = match order {
        OrderForm::Lex { count } => (StrategyName::Lex, count.map(|n| n as u64), None),
        OrderForm::ReverseLex { count } => {
            (StrategyName::ReverseLex, count.map(|n| n as u64), None)
        }
        OrderForm::Diagonal { count } => (StrategyName::Diagonal, count.map(|n| n as u64), None),
        OrderForm::Antidiagonal { count } => {
            (StrategyName::Antidiagonal, count.map(|n| n as u64), None)
        }
        OrderForm::Extrema { strata } => (StrategyName::Extrema, strata.map(|n| n as u64), None),
        OrderForm::Shells { depth, .. } => (StrategyName::Shells, depth.map(|n| n as u64), None),
        OrderForm::Halton { count } => (StrategyName::Halton, count.map(|n| n as u64), None),
        OrderForm::Sobol { count } => (StrategyName::Sobol, count.map(|n| n as u64), None),
        OrderForm::Lhs { count, seed } => (StrategyName::Lhs, count.map(|n| n as u64), *seed),
        OrderForm::Shuffle { count, seed } => {
            (StrategyName::Shuffle, count.map(|n| n as u64), *seed)
        }
    };
    Ok(triple)
}

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

    fn clause(var: &str, source: &str) -> ClauseForm {
        ClauseForm::new(var, source)
    }

    #[test]
    fn cartesian_single_clause_collapses_to_clause() {
        let clauses = ClauseAst {
            mode: ModeForm::Cartesian(vec![clause("k", "1..10")]),
            filter: None,
            order: None,
        };
        let algebra = clauses_to_algebra(&clauses).unwrap();
        match algebra {
            AlgebraAst::Clause { name, source } => {
                assert_eq!(name, "k");
                assert!(matches!(
                    source,
                    Source::IntRange {
                        lo: 1,
                        hi: 10,
                        step: 1
                    }
                ));
            }
            other => panic!("expected Clause, got {other:?}"),
        }
    }

    #[test]
    fn multi_clause_cartesian_becomes_algebra_cartesian() {
        let clauses = ClauseAst {
            mode: ModeForm::Cartesian(vec![
                clause("k", "1..10"),
                clause("limit", "[10, 100, 1000]"),
            ]),
            filter: None,
            order: None,
        };
        let algebra = clauses_to_algebra(&clauses).unwrap();
        match algebra {
            AlgebraAst::Cartesian { children } => {
                assert_eq!(children.len(), 2);
                // First clause: int range
                match &children[0] {
                    AlgebraAst::Clause { name, source } => {
                        assert_eq!(name, "k");
                        assert!(matches!(
                            source,
                            Source::IntRange {
                                lo: 1,
                                hi: 10,
                                step: 1
                            }
                        ));
                    }
                    other => panic!("expected Clause, got {other:?}"),
                }
                // Second clause: literal list
                match &children[1] {
                    AlgebraAst::Clause { name, source } => {
                        assert_eq!(name, "limit");
                        match source {
                            Source::Literal { values } => {
                                assert_eq!(values.len(), 3);
                                assert_eq!(values[0], LiteralValue::Int(10));
                            }
                            other => panic!("expected Literal, got {other:?}"),
                        }
                    }
                    other => panic!("expected Clause, got {other:?}"),
                }
            }
            other => panic!("expected Cartesian, got {other:?}"),
        }
    }

    #[test]
    fn filter_wraps_body() {
        let clauses = ClauseAst {
            mode: ModeForm::Cartesian(vec![clause("k", "1..10")]),
            filter: Some("{k} > 5".to_string()),
            order: None,
        };
        let algebra = clauses_to_algebra(&clauses).unwrap();
        assert!(matches!(algebra, AlgebraAst::Filter { .. }));
    }

    #[test]
    fn order_lex_with_count_round_trips() {
        let clauses = ClauseAst {
            mode: ModeForm::Cartesian(vec![clause("k", "1..10")]),
            filter: None,
            order: Some(OrderForm::Lex { count: Some(5) }),
        };
        let algebra = clauses_to_algebra(&clauses).unwrap();
        match algebra {
            AlgebraAst::Order {
                strategy: StrategyName::Lex,
                truncation: Some(5),
                ..
            } => {}
            other => panic!("expected Order(Lex, Some(5)), got {other:?}"),
        }
    }

    #[test]
    fn order_halton_with_count() {
        let clauses = ClauseAst {
            mode: ModeForm::Cartesian(vec![clause("k", "1..10"), clause("limit", "1..100")]),
            filter: None,
            order: Some(OrderForm::Halton { count: Some(20) }),
        };
        let algebra = clauses_to_algebra(&clauses).unwrap();
        match algebra {
            AlgebraAst::Order {
                strategy: StrategyName::Halton,
                truncation: Some(20),
                ..
            } => {}
            other => panic!("expected Order(Halton, Some(20)), got {other:?}"),
        }
    }

    #[test]
    fn union_of_subspaces() {
        let clauses = ClauseAst {
            mode: ModeForm::Union(vec![
                SubspaceForm::new(vec![clause("k", "10"), clause("limit", "[1, 2, 3]")]),
                SubspaceForm::new(vec![clause("k", "100"), clause("limit", "[10, 20, 30]")]),
            ]),
            filter: None,
            order: None,
        };
        let algebra = clauses_to_algebra(&clauses).unwrap();
        match algebra {
            AlgebraAst::Union { children } => assert_eq!(children.len(), 2),
            other => panic!("expected Union, got {other:?}"),
        }
    }

    #[test]
    fn parallel_clause_becomes_zip() {
        let parallel = ClauseForm::parallel(["x", "y"], ["1..3", "10..30"]);
        let clauses = ClauseAst {
            mode: ModeForm::Cartesian(vec![parallel]),
            filter: None,
            order: None,
        };
        let algebra = clauses_to_algebra(&clauses).unwrap();
        // After the singleton-cartesian elide, the Zip
        // surfaces at the top level.
        match algebra {
            AlgebraAst::Zip {
                children,
                mode: AlgebraZipMode::Strict,
            } => {
                assert_eq!(children.len(), 2);
            }
            other => panic!("expected Zip, got {other:?}"),
        }
    }

    #[test]
    fn unparseable_source_falls_back_to_generator() {
        // parse_source now treats unrecognized text as a
        // Generator expression (runtime evaluates). So
        // "totally nonsense" round-trips through algebra as
        // a Source::Generator. No conversion error.
        let clauses = ClauseAst {
            mode: ModeForm::Cartesian(vec![clause("k", "totally nonsense")]),
            filter: None,
            order: None,
        };
        let algebra = clauses_to_algebra(&clauses).unwrap();
        match algebra {
            AlgebraAst::Clause { source, .. } => match source {
                crate::comprehension::source::Source::Generator { expr, .. } => {
                    assert_eq!(expr, "totally nonsense");
                }
                other => panic!("expected Generator, got {other:?}"),
            },
            other => panic!("expected Clause, got {other:?}"),
        }
    }

    /// The authored seed of a seeded order reaches the algebra; the
    /// other strategies lower without one.
    #[test]
    fn a_seeded_order_keeps_its_seed_in_the_algebra() {
        let clauses = ClauseAst {
            mode: ModeForm::Cartesian(vec![clause("k", "1..10")]),
            filter: None,
            order: Some(OrderForm::Shuffle {
                count: Some(3),
                seed: Some(42),
            }),
        };
        let algebra = clauses_to_algebra(&clauses).unwrap();
        assert!(
            matches!(
                algebra,
                AlgebraAst::Order {
                    strategy: StrategyName::Shuffle,
                    truncation: Some(3),
                    seed: Some(42),
                    ..
                }
            ),
            "{algebra:?}"
        );
        assert_eq!(
            convert_order(&OrderForm::Lhs {
                count: None,
                seed: Some(7)
            })
            .unwrap(),
            (StrategyName::Lhs, None, Some(7))
        );
        assert_eq!(
            convert_order(&OrderForm::Halton { count: Some(4) }).unwrap(),
            (StrategyName::Halton, Some(4), None)
        );
    }
}