flowlog-build 0.3.3

Build-time FlowLog compiler for library mode.
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
//! Rule structures for FlowLog Datalog programs.
//!
//! A rule is `head :- p1, p2, ..., pn.`
//! - Head: a single derived relation
//! - Body: predicates that must all be satisfied

use super::{Factor, Head, HeadArg, Predicate};
use crate::common::{FileId, Ignored, Span};
use crate::parser::error::{ParseError, grammar_bug};
use crate::parser::primitive::ConstType;
use crate::parser::{Lexeme, Rule, span_of};
use pest::iterators::Pair;
use std::fmt;

/// A complete FlowLog rule.
///
/// Declared `pub` (not `pub(crate)`) because it leaks through
/// `Catalog::from_rule` / `Program::rules` signatures that external
/// callers (flowlog-compiler, integration tests) consume. The rule's
/// inherent methods stay `pub(crate)` — external code only passes rules
/// by reference without inspecting fields.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct FlowLogRule {
    head: Head,
    rhs: Vec<Predicate>,
    span: Ignored<Span>,
    /// Tombstone for the future cost-based optimizer: when true, the user
    /// supplied a `.plan` hint and the positive-atom order in `rhs` has
    /// already been permuted to match it — the optimizer must not reorder.
    plan_pinned: bool,
}

impl fmt::Display for FlowLogRule {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} :- {}.",
            self.head,
            self.rhs
                .iter()
                .map(|p| p.to_string())
                .collect::<Vec<_>>()
                .join(", ")
        )
    }
}

impl fmt::Debug for FlowLogRule {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} :- {}.",
            self.head,
            self.rhs
                .iter()
                .map(|p| format!("{p:?}"))
                .collect::<Vec<_>>()
                .join(", ")
        )
    }
}

impl FlowLogRule {
    /// Construct a rule.
    #[must_use]
    pub(crate) fn new(head: Head, rhs: Vec<Predicate>) -> Self {
        Self {
            head,
            rhs,
            span: Ignored(Span::DUMMY),
            plan_pinned: false,
        }
    }

    #[must_use]
    #[inline]
    pub(crate) fn positive_atom_count(&self) -> usize {
        self.rhs
            .iter()
            .filter(|p| matches!(p, Predicate::PositiveAtom(_)))
            .count()
    }

    /// Whether the rule's positive-atom order is pinned by a user `.plan`.
    /// Future cost-based optimizer reads this to decide whether to
    /// reorder; today no caller.
    #[must_use]
    #[inline]
    #[allow(dead_code)]
    pub(crate) fn plan_pinned(&self) -> bool {
        self.plan_pinned
    }

    /// Reorder the positive atoms in `rhs` according to a 0-based
    /// permutation, leaving negations / comparisons / fn-calls in their
    /// original global slots. Marks the rule as `plan_pinned`.
    ///
    /// `perm` must be a permutation of `0..positive_atom_count()`. The
    /// caller validates; debug builds re-check the length only.
    pub(crate) fn permute_positive_atoms(&mut self, perm: &[usize]) {
        let pos_indices: Vec<usize> = self
            .rhs
            .iter()
            .enumerate()
            .filter_map(|(i, p)| matches!(p, Predicate::PositiveAtom(_)).then_some(i))
            .collect();

        debug_assert_eq!(perm.len(), pos_indices.len());

        // Cycle-decomposition in place: for each cycle of `perm`, rotate
        // the positive atoms via `Vec::swap`. Zero clones, one bitmap
        // allocation. Correctness sketch: applying the swaps along each
        // cycle resolves all but the last element automatically, since by
        // then the cycle's other slots already hold their final values.
        let n = perm.len();
        let mut visited = vec![false; n];
        for start in 0..n {
            if visited[start] {
                continue;
            }
            let mut current = start;
            while !visited[current] {
                visited[current] = true;
                let next = perm[current];
                if next != current && !visited[next] {
                    self.rhs.swap(pos_indices[current], pos_indices[next]);
                }
                current = next;
            }
        }

        self.plan_pinned = true;
    }

    /// Source location this rule was parsed from.
    #[must_use]
    #[inline]
    pub(crate) fn span(&self) -> Span {
        self.span.0
    }

    /// Rule head.
    #[must_use]
    #[inline]
    pub(crate) fn head(&self) -> &Head {
        &self.head
    }

    /// Rule body (right-hand side predicates).
    #[must_use]
    #[inline]
    pub(crate) fn rhs(&self) -> &[Predicate] {
        &self.rhs
    }

    #[inline]
    pub(crate) fn head_mut(&mut self) -> &mut Head {
        &mut self.head
    }

    #[inline]
    pub(crate) fn rhs_mut(&mut self) -> &mut [Predicate] {
        &mut self.rhs
    }

    /// Replace the rule body wholesale. Used by the equality-assignment
    /// desugaring pass, which removes assignment literals after substituting
    /// their bound variable into the head and remaining predicates.
    #[inline]
    pub(crate) fn set_rhs(&mut self, rhs: Vec<Predicate>) {
        self.rhs = rhs;
    }

    /// Extract constants from a fact's head.
    ///
    /// Panics if any head argument is not a simple constant.
    #[must_use]
    pub(crate) fn extract_constants_from_head(&self) -> Vec<ConstType> {
        let args = self.head.head_arguments();
        let mut out = Vec::with_capacity(args.len());
        for arg in args {
            let HeadArg::Arith(arith) = arg else {
                panic!("Fact head must contain only constants: {self}");
            };
            let Factor::Const(c) = arith.init() else {
                panic!("Fact head must contain only constants: {self}");
            };
            assert!(
                arith.is_const(),
                "Fact head must contain only constants: {self}"
            );
            out.push(c.clone());
        }
        out
    }

    /// Expand a multi-head / multi-body rule into one rule per
    /// (head, body) pair. Parenthesised `(A ; B)` disjunctions inside
    /// a conjunction distribute by cross-product.
    pub(crate) fn expand_from_parsed_rule(
        parsed_rule: Pair<Rule>,
        file: FileId,
    ) -> Result<Vec<Self>, ParseError> {
        let rule_span = span_of(&parsed_rule, file);
        let mut inner = parsed_rule.into_inner();

        let rule_heads_node = inner
            .next()
            .ok_or_else(|| grammar_bug("rule missing heads"))?;
        let mut heads: Vec<Head> = Vec::new();
        for h in rule_heads_node.into_inner() {
            heads.push(Head::from_parsed_rule(h, file)?);
        }

        let rule_bodies_node = inner
            .next()
            .ok_or_else(|| grammar_bug("rule missing bodies"))?;
        let bodies = expand_rule_bodies(rule_bodies_node, file)?;

        let mut out = Vec::with_capacity(heads.len() * bodies.len());
        for head in &heads {
            for body in &bodies {
                out.push(Self {
                    head: head.clone(),
                    rhs: body.clone(),
                    span: Ignored(rule_span),
                    plan_pinned: false,
                });
            }
        }
        Ok(out)
    }
}

pub(crate) fn parse_plan_indices(
    pair: Pair<Rule>,
    file: FileId,
) -> Result<(Span, Vec<usize>), ParseError> {
    let span = span_of(&pair, file);
    let mut raw_indices = Vec::new();
    for child in pair.into_inner() {
        match child.as_rule() {
            // Soufflé `.plan N:(…)` carries an overload index. FlowLog
            // expands multi-clause rules at parse time, so the version
            // has no clause to bind to — parse and discard.
            Rule::plan_version => continue,
            Rule::plan_index => {
                let parsed: usize = child
                    .as_str()
                    .parse()
                    .map_err(|_| grammar_bug("plan_index is not a valid integer"))?;
                raw_indices.push(parsed);
            }
            other => {
                return Err(grammar_bug(format!(
                    "plan_directive unexpected child rule {other:?}"
                )));
            }
        }
    }
    Ok((span, raw_indices))
}

pub(crate) fn apply_indices_to_rule(
    rule: &mut FlowLogRule,
    span: Span,
    raw_indices: &[usize],
) -> Result<(), ParseError> {
    let k = rule.positive_atom_count();
    if raw_indices.len() != k {
        return Err(ParseError::PlanArityMismatch {
            span,
            expected: k,
            found: raw_indices.len(),
        });
    }
    let mut seen = vec![false; k];
    let mut perm: Vec<usize> = Vec::with_capacity(k);
    for &idx in raw_indices {
        if idx == 0 || idx > k {
            return Err(ParseError::PlanIndexOutOfRange {
                span,
                index: idx,
                max: k,
            });
        }
        let zero = idx - 1;
        if seen[zero] {
            return Err(ParseError::PlanDuplicateIndex { span, index: idx });
        }
        seen[zero] = true;
        perm.push(zero);
    }
    rule.permute_positive_atoms(&perm);
    Ok(())
}

/// State-machine consumer for top-level programs and loop/fixpoint blocks:
/// `plan_target_start` was set by the caller when the most recent rule
/// clause emitted its first rule, and `.take()` here both consumes it and
/// makes a subsequent `.plan` an orphan.
pub(crate) fn consume_plan_directive(
    pair: Pair<Rule>,
    file: FileId,
    rules: &mut [FlowLogRule],
    plan_target_start: &mut Option<usize>,
) -> Result<(), ParseError> {
    let (span, raw_indices) = parse_plan_indices(pair, file)?;
    let start = plan_target_start
        .take()
        .ok_or(ParseError::PlanOrphan { span })?;
    for rule in &mut rules[start..] {
        apply_indices_to_rule(rule, span, &raw_indices)?;
    }
    Ok(())
}

fn expand_rule_bodies(node: Pair<Rule>, file: FileId) -> Result<Vec<Vec<Predicate>>, ParseError> {
    let mut alternatives = Vec::new();
    for predicates_node in node.into_inner() {
        alternatives.extend(expand_predicates(predicates_node, file)?);
    }
    Ok(alternatives)
}

/// Expand one comma-separated `predicates` node, distributing any
/// nested `(A ; B)` groups by cross-product.
fn expand_predicates(node: Pair<Rule>, file: FileId) -> Result<Vec<Vec<Predicate>>, ParseError> {
    let mut acc: Vec<Vec<Predicate>> = vec![Vec::new()];
    for pred_node in node.into_inner() {
        let inner = pred_node
            .into_inner()
            .next()
            .ok_or_else(|| grammar_bug("predicate missing inner"))?;
        if matches!(inner.as_rule(), Rule::disjunction_group) {
            let bodies_node = inner
                .into_inner()
                .next()
                .ok_or_else(|| grammar_bug("disjunction_group missing inner rule_bodies"))?;
            let nested = expand_rule_bodies(bodies_node, file)?;
            let mut next = Vec::with_capacity(acc.len() * nested.len());
            for prefix in &acc {
                for alt in &nested {
                    let mut combined = prefix.clone();
                    combined.extend(alt.iter().cloned());
                    next.push(combined);
                }
            }
            acc = next;
        } else {
            let p = Predicate::from_inner(inner, file)?;
            for conjunction in &mut acc {
                conjunction.push(p.clone());
            }
        }
    }
    Ok(acc)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::AggregationOperator;
    use crate::parser::logic::{Aggregation, Arithmetic, Factor};
    use crate::parser::primitive::ConstType;

    fn head_const(v: ConstType) -> HeadArg {
        HeadArg::Arith(Arithmetic::new(Factor::Const(v), vec![]))
    }
    fn head_named(name: &str, args: Vec<HeadArg>) -> Head {
        Head::new(name.into(), args)
    }

    #[test]
    fn extract_constants() {
        let head = head_named(
            "facts",
            vec![
                head_const(ConstType::Int(42)),
                head_const(ConstType::Text("hello".into())),
            ],
        );
        let r = FlowLogRule::new(head, vec![]);
        let c = r.extract_constants_from_head();
        assert_eq!(c, vec![ConstType::Int(42), ConstType::Text("hello".into())]);
    }

    #[test]
    #[should_panic(expected = "Fact head must contain only constants")]
    fn extract_constants_panics_on_var() {
        let head = head_named(
            "invalid",
            vec![head_const(ConstType::Int(1)), HeadArg::Var("X".into())],
        );
        let _ = FlowLogRule::new(head, vec![]).extract_constants_from_head();
    }

    #[test]
    #[should_panic(expected = "Fact head must contain only constants")]
    fn extract_constants_panics_on_aggregation() {
        let agg = Aggregation::new(
            AggregationOperator::Sum,
            Arithmetic::new(Factor::Var("X".into()), vec![]),
        );
        let head = head_named(
            "invalid",
            vec![head_const(ConstType::Int(1)), HeadArg::Aggregation(agg)],
        );
        let _ = FlowLogRule::new(head, vec![]).extract_constants_from_head();
    }
}