lemma-engine 0.11.0

A pure, declarative language for business rules.
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
//! Flat, id-indexed storage for expression trees.
//!
//! Nodes reference children by [`ExprId`], never by pointer. A table holds exactly one
//! tree: every child id is below its parent's id, and the root is the last node, so the
//! table order is a post-order of the tree. Trees of any depth are stored, walked,
//! compared and dropped without recursion.

use crate::parsing::source::Source;
use serde::Serialize;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
pub struct ExprId(u32);

impl ExprId {
    #[must_use]
    pub fn index(self) -> usize {
        self.0 as usize
    }

    #[must_use]
    pub fn from_index(index: usize) -> Self {
        Self(u32::try_from(index).expect("BUG: expression table exceeds u32::MAX nodes"))
    }
}

/// Node payload that names its child ids.
pub trait ExprKind: Clone + PartialEq {
    /// Calls `f` on each child id in left-to-right source order.
    fn for_each_child(&self, f: impl FnMut(ExprId));

    /// Mutable child ids, in the same order as [`ExprKind::for_each_child`].
    fn children_mut(&mut self) -> Vec<&mut ExprId>;
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ExprNode<K> {
    pub kind: K,
    pub source_location: Option<Source>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ExprTable<K> {
    nodes: Vec<ExprNode<K>>,
}

impl<K> Default for ExprTable<K> {
    fn default() -> Self {
        Self { nodes: Vec::new() }
    }
}

impl<K> ExprTable<K> {
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            nodes: Vec::with_capacity(capacity),
        }
    }

    /// Moves every node into a new table sized exactly; `self` keeps its capacity for reuse.
    pub fn take_compact(&mut self) -> Self {
        Self {
            nodes: self.nodes.drain(..).collect(),
        }
    }
}

impl<K: ExprKind> ExprTable<K> {
    /// Appends a node. Its children must already be in the table.
    pub fn push(&mut self, kind: K, source_location: Option<Source>) -> ExprId {
        let id = ExprId::from_index(self.nodes.len());
        kind.for_each_child(|child| {
            assert!(
                child < id,
                "BUG: child ExprId {} pushed after its parent {}",
                child.0,
                id.0
            );
        });
        self.nodes.push(ExprNode {
            kind,
            source_location,
        });
        id
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Drops every node pushed after the first `len`, undoing a speculative parse.
    pub fn truncate(&mut self, len: usize) {
        assert!(
            len <= self.nodes.len(),
            "BUG: truncate to {len} beyond table length {}",
            self.nodes.len()
        );
        self.nodes.truncate(len);
    }

    #[must_use]
    pub fn node(&self, id: ExprId) -> &ExprNode<K> {
        self.nodes.get(id.index()).unwrap_or_else(|| {
            panic!(
                "BUG: ExprId {} out of range (len {})",
                id.0,
                self.nodes.len()
            )
        })
    }

    #[must_use]
    pub fn kind(&self, id: ExprId) -> &K {
        &self.node(id).kind
    }

    #[must_use]
    pub fn source(&self, id: ExprId) -> Option<&Source> {
        self.node(id).source_location.as_ref()
    }

    /// Every node id, children before their parents.
    pub fn ids(&self) -> impl Iterator<Item = ExprId> {
        (0..self.nodes.len()).map(ExprId::from_index)
    }

    pub fn nodes(&self) -> impl Iterator<Item = &ExprNode<K>> {
        self.nodes.iter()
    }

    pub fn kinds_mut(&mut self) -> impl Iterator<Item = &mut K> {
        self.nodes.iter_mut().map(|node| &mut node.kind)
    }

    pub fn kind_mut(&mut self, id: ExprId) -> &mut K {
        let len = self.nodes.len();
        &mut self
            .nodes
            .get_mut(id.index())
            .unwrap_or_else(|| panic!("BUG: ExprId {} out of range (len {len})", id.0))
            .kind
    }

    pub fn set_source(&mut self, id: ExprId, source: Source) {
        let len = self.nodes.len();
        self.nodes
            .get_mut(id.index())
            .unwrap_or_else(|| panic!("BUG: ExprId {} out of range (len {len})", id.0))
            .source_location = Some(source);
    }

    /// Structural equality of two subtrees, ignoring source locations and ids.
    #[must_use]
    pub fn structurally_equal(&self, left: ExprId, other: &Self, right: ExprId) -> bool {
        let mut lefts = vec![left];
        let mut rights = vec![right];
        while let (Some(a), Some(b)) = (lefts.pop(), rights.pop()) {
            let kind_a = self.kind(a);
            let kind_b = other.kind(b);
            if shape(kind_a) != shape(kind_b) {
                return false;
            }
            kind_a.for_each_child(|child| lefts.push(child));
            kind_b.for_each_child(|child| rights.push(child));
            if lefts.len() != rights.len() {
                return false;
            }
        }
        true
    }
}

fn shape<K: ExprKind>(kind: &K) -> K {
    let mut shaped = kind.clone();
    for child in shaped.children_mut() {
        *child = ExprId(0);
    }
    shaped
}

/// A table holding one tree, and that tree's root (its last node).
#[derive(Debug, Clone, Serialize)]
pub struct ExprTree<K> {
    pub table: ExprTable<K>,
    pub root: ExprId,
}

impl<K: ExprKind> ExprTree<K> {
    /// `table` as one tree rooted at its last node.
    #[must_use]
    pub fn new(table: ExprTable<K>, root: ExprId) -> Self {
        assert_eq!(
            root.index() + 1,
            table.len(),
            "BUG: expression tree root must be the last node of its table"
        );
        Self { table, root }
    }

    /// A tree of one node. `kind` must not name children.
    #[must_use]
    pub fn leaf(kind: K, source_location: Option<Source>) -> Self {
        let mut table = ExprTable::with_capacity(1);
        let root = table.push(kind, source_location);
        Self::new(table, root)
    }

    #[must_use]
    pub fn root_kind(&self) -> &K {
        self.table.kind(self.root)
    }

    #[must_use]
    pub fn root_source(&self) -> Option<&Source> {
        self.table.source(self.root)
    }

    #[must_use]
    pub fn at(&self, id: ExprId) -> ExprRef<'_, K> {
        ExprRef {
            table: &self.table,
            id,
        }
    }

    #[must_use]
    pub fn root(&self) -> ExprRef<'_, K> {
        self.at(self.root)
    }

    /// Every node is reachable from the root through exactly one parent.
    #[cfg(test)]
    pub(crate) fn assert_is_tree(&self) {
        let mut parents = vec![0usize; self.table.len()];
        for id in self.table.ids() {
            self.table
                .kind(id)
                .for_each_child(|child| parents[child.index()] += 1);
        }
        for (index, count) in parents.iter().enumerate() {
            let expected = usize::from(index != self.root.index());
            assert_eq!(
                *count, expected,
                "node {index} has {count} parents, expected {expected}"
            );
        }
    }
}

impl<K: ExprKind> PartialEq for ExprTree<K> {
    fn eq(&self, other: &Self) -> bool {
        self.table
            .structurally_equal(self.root, &other.table, other.root)
    }
}

impl<K: ExprKind> Eq for ExprTree<K> {}

/// A node viewed through its table.
#[derive(Debug)]
pub struct ExprRef<'a, K> {
    pub table: &'a ExprTable<K>,
    pub id: ExprId,
}

impl<K> Clone for ExprRef<'_, K> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<K> Copy for ExprRef<'_, K> {}

impl<'a, K: ExprKind> ExprRef<'a, K> {
    #[must_use]
    pub fn kind(self) -> &'a K {
        self.table.kind(self.id)
    }

    #[must_use]
    pub fn source(self) -> Option<&'a Source> {
        self.table.source(self.id)
    }

    #[must_use]
    pub fn child(self, id: ExprId) -> Self {
        Self {
            table: self.table,
            id,
        }
    }

    /// Pushes this node's children onto a depth-first stack so they pop in source order.
    pub fn push_children(self, pending: &mut Vec<Self>) {
        let start = pending.len();
        self.kind()
            .for_each_child(|child| pending.push(self.child(child)));
        pending[start..].reverse();
    }
}

#[cfg(test)]
mod tests {
    use crate::parsing::source::SourceType;
    use crate::ResourceLimits;
    use std::path::{Path, PathBuf};

    const CORPUS_DIRS: [&str; 4] = [
        "documentation/examples",
        "benches/specs",
        "tests/fixtures",
        "../cli/tests/integrations",
    ];

    fn lemma_files(root: &Path) -> Vec<PathBuf> {
        let mut files = Vec::new();
        let mut pending = vec![root.to_path_buf()];
        while let Some(dir) = pending.pop() {
            for entry in std::fs::read_dir(&dir).expect("corpus directory must be readable") {
                let path = entry.expect("corpus entry must be readable").path();
                if path.is_dir() {
                    pending.push(path);
                } else if path.extension().is_some_and(|ext| ext == "lemma") {
                    files.push(path);
                }
            }
        }
        files.sort();
        files
    }

    fn parse_rule_expression(source: &str, rule_name: &str) -> crate::parsing::ast::Expression {
        let specs = crate::parsing::parse(source, SourceType::Volatile, &ResourceLimits::default())
            .expect("fixture must parse")
            .into_flattened_specs();
        specs
            .iter()
            .flat_map(|spec| spec.rules.iter())
            .find(|rule| rule.name == rule_name)
            .expect("BUG: fixture must declare the named rule")
            .expression
            .clone()
    }

    #[test]
    fn parsed_expressions_are_trees() {
        let manifest = Path::new(env!("CARGO_MANIFEST_DIR"));
        let mut checked = 0usize;
        for dir in CORPUS_DIRS {
            for path in lemma_files(&manifest.join(dir)) {
                let source = std::fs::read_to_string(&path).expect("corpus file must be readable");
                let specs = crate::parsing::parse(
                    &source,
                    SourceType::Volatile,
                    &ResourceLimits::default(),
                )
                .unwrap_or_else(|error| panic!("{} must parse: {error}", path.display()))
                .into_flattened_specs();
                for spec in &specs {
                    for rule in &spec.rules {
                        rule.expression.assert_is_tree();
                        for clause in &rule.unless_clauses {
                            clause.condition.assert_is_tree();
                            clause.result.assert_is_tree();
                        }
                        checked += 1;
                    }
                }
            }
        }
        assert!(checked > 100, "corpus too small: {checked} rules");
    }

    #[test]
    fn identifier_list_command_is_a_tree() {
        let expression = parse_rule_expression(
            r#"
spec item
data score: number

spec scores
uses item
rule average:
  item list -> avg score
"#,
            "average",
        );
        expression.assert_is_tree();
        assert_eq!(
            expression.table.len(),
            3,
            "ListType, score Reference, ListCommand — no orphan Reference for item"
        );
    }

    #[test]
    fn identifier_range_list_is_a_tree() {
        let expression = parse_rule_expression(
            r#"
spec item
data score: number

spec scores
uses item
rule empty:
  item range list -> count
"#,
            "empty",
        );
        expression.assert_is_tree();
        assert_eq!(
            expression.table.len(),
            2,
            "ListType then Count command — no orphan Reference for item"
        );
    }

    #[test]
    fn qualified_list_type_is_a_tree() {
        let expression = parse_rule_expression(
            r#"
spec row
data score: number

spec outer
uses r: row
rule counted:
  r.score list -> count
"#,
            "counted",
        );
        expression.assert_is_tree();
        assert_eq!(
            expression.table.len(),
            2,
            "ListType then Count — no orphan qualified Reference"
        );
    }
}