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

//! [`ComprehensionSpec`] — author-friendly serde-deserializable
//! form, plus its conversion into the algebra-layer AST.
//!
//! The friendly form has a **single `for` verb** (per user
//! direction) whose RHS shape determines the constructor:
//!
//! - `for: "k in 1..10"` — single clause / inline cartesian
//! - `for: ["k in 1..10", "limit in [1, 2, 3]"]` — list of
//!   clauses, cartesian (or union if names repeat)
//! - `for: [["k in 10", "limit in 1..5"], ["k in 100", "limit
//!   in 1..50"]]` — explicit union of cartesian sub-spaces
//!
//! With optional modifiers:
//!
//! - `where: "..."` — filter predicate
//! - `order: "halton/50"` — traversal order spec
//!
//! Conversion delegates to the existing legacy parsers + the
//! [`super::legacy_convert`] bridge. This module owns only the
//! serde surface and the shape-routing logic.

use serde::{Deserialize, Serialize};

use crate::iteration::comprehension::ast::Comprehension as AlgebraAst;
use crate::iteration::comprehension::ast_legacy::{
    Clause as LegacyClause, Comprehension as LegacyAst,
};
use crate::iteration::comprehension::parse::{
    comprehension_from_subspaces, parse_clause_list, parse_order_spec,
};

use super::legacy_convert::{legacy_to_algebra, ConvertError};

/// The friendly, serde-deserializable comprehension surface.
///
/// Field names match the YAML / JSON keys 1:1. The
/// [`r#for`](Self::r#for) field carries the only required input
/// — the clause specification — in any of the three accepted
/// shapes (see [`ForSpec`]).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComprehensionSpec {
    /// The clause specification.  See [`ForSpec`] for the
    /// accepted shapes.  `r#for` because `for` is a reserved
    /// keyword in Rust — serde renames it for YAML / JSON.
    #[serde(rename = "for")]
    pub r#for: ForSpec,
    /// Optional filter predicate (the `where` clause). String
    /// form, evaluated against bound coordinates at iteration
    /// time.
    #[serde(default, rename = "where", skip_serializing_if = "Option::is_none")]
    pub r#where: Option<String>,
    /// Optional traversal-order spec. See
    /// [`crate::iteration::comprehension::parse::parse_order_spec`] for
    /// the accepted syntax (`lex`, `halton/50`,
    /// `shells(origin=center, depth=3)`, etc.).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub order: Option<String>,
}

/// The three accepted shapes of the `for` field.
///
/// Routes through the legacy parser:
///
/// - [`ForSpec::Inline`] → one call to `parse_clause_list`,
///   then the structural-detection rule (`comprehension_from_subspaces`).
/// - [`ForSpec::ClauseList`] → one `parse_clause_list` per
///   entry; each entry becomes its own sub-space (so name
///   repetition across entries triggers Union per the rule).
/// - [`ForSpec::UnionOfClauseLists`] → one `parse_clause_list`
///   per inner list; each inner list is one sub-space.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ForSpec {
    /// `for: "k in 1..10, limit in [1, 2, 3]"` — one inline
    /// string, possibly multi-clause.
    Inline(String),
    /// `for: ["k in 1..10", "limit in [1, 2, 3]"]` — each
    /// entry is one clause's text.
    ClauseList(Vec<String>),
    /// `for: [["k in 10", "limit in 1..5"], …]` — each inner
    /// list is one sub-space (cartesian over those clauses).
    UnionOfClauseLists(Vec<Vec<String>>),
}

/// Errors produced when converting a [`ComprehensionSpec`] to
/// the algebra-layer AST.
#[derive(Debug, Clone)]
pub enum SpecConvertError {
    /// `parse_clause_list` failed on one of the input strings.
    ParseClause { input: String, message: String },
    /// `parse_order_spec` failed on the `order` field.
    ParseOrder { input: String, message: String },
    /// Legacy AST failed self-validation.
    LegacyValidate { errors: Vec<String> },
    /// Conversion from legacy AST to algebra AST failed.
    Convert(ConvertError),
}

impl std::fmt::Display for SpecConvertError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SpecConvertError::ParseClause { input, message } => {
                write!(f, "failed to parse clause(s) {input:?}: {message}")
            }
            SpecConvertError::ParseOrder { input, message } => {
                write!(f, "failed to parse order {input:?}: {message}")
            }
            SpecConvertError::LegacyValidate { errors } => {
                write!(f, "legacy AST validation failed: {}", errors.join("; "))
            }
            SpecConvertError::Convert(e) => {
                write!(f, "algebra conversion failed: {e}")
            }
        }
    }
}

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

impl From<ConvertError> for SpecConvertError {
    fn from(e: ConvertError) -> Self {
        SpecConvertError::Convert(e)
    }
}

/// Parse a single inline `for`-clause string — **possibly
/// multi-clause**, e.g. `"k in 1..10, limit in [1, 2, 3]"` — into the
/// algebra-layer [`AlgebraAst`].
///
/// The canonical entry for consumers that hold only the for-clause text
/// (no separate `where` / `order`): both **scenario- and phase-level**
/// `for_each` in a host route through this, so the comprehension grammar
/// has a single owner (polydat) rather than ad-hoc `var in expr` splits
/// scattered in the runtime.
pub fn parse_inline(spec: &str) -> Result<AlgebraAst, SpecConvertError> {
    ComprehensionSpec {
        r#for: ForSpec::Inline(spec.to_string()),
        r#where: None,
        order: None,
    }
    .into_algebra()
}

impl ComprehensionSpec {
    /// Convert this spec into the algebra-layer
    /// [`AlgebraAst`].  Routes the `for` shape through the
    /// legacy parser, builds a legacy AST (applying `where` /
    /// `order` modifiers), and runs the [`legacy_to_algebra`]
    /// bridge.
    pub fn into_algebra(self) -> Result<AlgebraAst, SpecConvertError> {
        let legacy = self.into_legacy()?;
        let algebra = legacy_to_algebra(&legacy)?;
        Ok(algebra)
    }

    /// Build the intermediate legacy AST.  Exposed for tests
    /// and for any consumer that still needs the legacy shape
    /// (e.g., during incremental cutover).
    pub fn into_legacy(self) -> Result<LegacyAst, SpecConvertError> {
        let subspaces = self.r#for.into_subspaces()?;
        let mut legacy = comprehension_from_subspaces(subspaces);
        if let Some(predicate) = self.r#where {
            legacy = legacy.with_filter(predicate);
        }
        if let Some(order_text) = self.order {
            let order =
                parse_order_spec(&order_text).map_err(|msg| SpecConvertError::ParseOrder {
                    input: order_text.clone(),
                    message: msg,
                })?;
            legacy = legacy.with_order(order);
        }
        legacy
            .validate()
            .map_err(|errs| SpecConvertError::LegacyValidate { errors: errs })?;
        Ok(legacy)
    }
}

impl ForSpec {
    /// Lower a `ForSpec` to the `Vec<Vec<Clause>>` shape that
    /// [`comprehension_from_subspaces`] expects.
    fn into_subspaces(self) -> Result<Vec<Vec<LegacyClause>>, SpecConvertError> {
        match self {
            ForSpec::Inline(text) => {
                // One inline string = one sub-space per clause
                // (matches `parse_comprehension_text`'s
                // convention so the union-detection rule sees
                // per-clause boundaries).
                let clauses = parse_clause_list(&text).map_err(|message| {
                    SpecConvertError::ParseClause {
                        input: text.clone(),
                        message,
                    }
                })?;
                Ok(clauses.into_iter().map(|c| vec![c]).collect())
            }
            ForSpec::ClauseList(entries) => {
                // Each entry is one clause's text — one
                // sub-space per entry (same convention).
                let mut subspaces = Vec::with_capacity(entries.len());
                for entry in entries {
                    let clauses = parse_clause_list(&entry).map_err(|message| {
                        SpecConvertError::ParseClause {
                            input: entry.clone(),
                            message,
                        }
                    })?;
                    for c in clauses {
                        subspaces.push(vec![c]);
                    }
                }
                Ok(subspaces)
            }
            ForSpec::UnionOfClauseLists(groups) => {
                // Each inner list is one sub-space (cartesian
                // over those clauses).
                let mut subspaces = Vec::with_capacity(groups.len());
                for group in groups {
                    let mut subspace_clauses = Vec::with_capacity(group.len());
                    for entry in group {
                        let clauses = parse_clause_list(&entry).map_err(|message| {
                            SpecConvertError::ParseClause {
                                input: entry.clone(),
                                message,
                            }
                        })?;
                        subspace_clauses.extend(clauses);
                    }
                    subspaces.push(subspace_clauses);
                }
                Ok(subspaces)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::iteration::comprehension::strategy::StrategyName;

    #[test]
    fn inline_single_clause() {
        let yaml = r#"
            for: "k in 1..10"
        "#;
        let spec: ComprehensionSpec = serde_yaml::from_str(yaml).unwrap();
        let algebra = spec.into_algebra().unwrap();
        // Single-clause cartesian collapses to the bare Clause.
        assert!(matches!(algebra, AlgebraAst::Clause { .. }));
    }

    #[test]
    fn inline_multi_clause_cartesian() {
        let yaml = r#"
            for: "k in 1..10, limit in [10, 100, 1000]"
        "#;
        let spec: ComprehensionSpec = serde_yaml::from_str(yaml).unwrap();
        let algebra = spec.into_algebra().unwrap();
        match algebra {
            AlgebraAst::Cartesian { children } => assert_eq!(children.len(), 2),
            other => panic!("expected Cartesian, got {other:?}"),
        }
    }

    #[test]
    fn clause_list_form_cartesian() {
        let yaml = r#"
            for:
              - "k in 1..10"
              - "limit in [10, 100, 1000]"
        "#;
        let spec: ComprehensionSpec = serde_yaml::from_str(yaml).unwrap();
        let algebra = spec.into_algebra().unwrap();
        match algebra {
            AlgebraAst::Cartesian { children } => assert_eq!(children.len(), 2),
            other => panic!("expected Cartesian, got {other:?}"),
        }
    }

    #[test]
    fn union_of_clause_lists() {
        let yaml = r#"
            for:
              - ["k in 10",  "limit in [1, 2, 3]"]
              - ["k in 100", "limit in [10, 20, 30]"]
        "#;
        let spec: ComprehensionSpec = serde_yaml::from_str(yaml).unwrap();
        let algebra = spec.into_algebra().unwrap();
        match algebra {
            AlgebraAst::Union { children } => assert_eq!(children.len(), 2),
            other => panic!("expected Union, got {other:?}"),
        }
    }

    #[test]
    fn where_clause_wraps_with_filter() {
        let yaml = r#"
            for: "k in 1..10"
            where: "{k} > 5"
        "#;
        let spec: ComprehensionSpec = serde_yaml::from_str(yaml).unwrap();
        let algebra = spec.into_algebra().unwrap();
        assert!(matches!(algebra, AlgebraAst::Filter { .. }));
    }

    #[test]
    fn order_clause_wraps_with_order() {
        let yaml = r#"
            for: "k in 1..10, limit in 1..100"
            order: "halton/50"
        "#;
        let spec: ComprehensionSpec = serde_yaml::from_str(yaml).unwrap();
        let algebra = spec.into_algebra().unwrap();
        match algebra {
            AlgebraAst::Order {
                strategy: StrategyName::Halton,
                truncation: Some(50),
                ..
            } => {}
            other => panic!("expected Order(Halton, Some(50)), got {other:?}"),
        }
    }

    #[test]
    fn where_and_order_compose() {
        let yaml = r#"
            for: "k in 1..10, limit in 1..100"
            where: "{k} * {limit} <= 100"
            order: "lex/20"
        "#;
        let spec: ComprehensionSpec = serde_yaml::from_str(yaml).unwrap();
        let algebra = spec.into_algebra().unwrap();
        // Order wraps Filter wraps Cartesian
        match algebra {
            AlgebraAst::Order { child, strategy: StrategyName::Lex, truncation: Some(20), .. } => {
                assert!(matches!(*child, AlgebraAst::Filter { .. }));
            }
            other => panic!("expected Order(Lex, Some(20)) wrapping Filter, got {other:?}"),
        }
    }

    #[test]
    fn json_input_round_trips() {
        let json = r#"
            {
                "for": ["k in 1..10", "limit in [10, 100]"],
                "where": "{k} > 0",
                "order": "halton/20"
            }
        "#;
        let spec: ComprehensionSpec = serde_json::from_str(json).unwrap();
        let algebra = spec.into_algebra().unwrap();
        assert!(matches!(algebra, AlgebraAst::Order { .. }));
    }

    #[test]
    fn malformed_clause_surfaces_error() {
        let yaml = r#"
            for: "this is not a valid clause"
        "#;
        let spec: ComprehensionSpec = serde_yaml::from_str(yaml).unwrap();
        let err = spec.into_algebra().unwrap_err();
        assert!(matches!(err, SpecConvertError::ParseClause { .. }));
    }

    #[test]
    fn malformed_order_surfaces_error() {
        let yaml = r#"
            for: "k in 1..10"
            order: "(((not valid"
        "#;
        let spec: ComprehensionSpec = serde_yaml::from_str(yaml).unwrap();
        let err = spec.into_algebra().unwrap_err();
        assert!(matches!(err, SpecConvertError::ParseOrder { .. }));
    }

    #[test]
    fn unparseable_source_inside_for_falls_back_to_generator() {
        // parse_source now treats unrecognized text as a
        // Source::Generator that the runtime evaluator resolves
        // against the Polydat Kernel chain — matching the legacy
        // grammar's permissive accept-anything behavior.
        let yaml = r#"
            for: "k in something-weird"
        "#;
        let spec: ComprehensionSpec = serde_yaml::from_str(yaml).unwrap();
        let algebra = spec.into_algebra().expect("permissive accept");
        match algebra {
            AlgebraAst::Clause { source, .. } => match source {
                crate::iteration::comprehension::source::Source::Generator { expr, .. } => {
                    assert_eq!(expr, "something-weird");
                }
                other => panic!("expected Generator, got {other:?}"),
            },
            other => panic!("expected Clause, got {other:?}"),
        }
    }
}