Skip to main content

datalog_structural_model/stratify/
cycle.rs

1//! [`UnstratifiableCycle`] — the component and the strict edge inside it
2//! that make a program unstratifiable.
3
4use std::fmt;
5
6use crate::ir::PredicateSymbol;
7use crate::position::{LiteralPath, RuleIndex};
8
9/// Why a dependency must cross strata.
10#[derive(
11    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
12)]
13#[serde(deny_unknown_fields)]
14pub enum StrictDependencyKind {
15    /// The consumer literal is negated.
16    Negation,
17    /// The consumer literal sits inside an aggregate's inner body.
18    Aggregation,
19}
20
21impl fmt::Display for StrictDependencyKind {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        f.write_str(match self {
24            Self::Negation => "negation",
25            Self::Aggregation => "aggregation",
26        })
27    }
28}
29
30/// A producer-to-consumer dependency that requires a strictly earlier
31/// producer stratum.
32#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
33#[serde(deny_unknown_fields)]
34pub struct StrictDependency {
35    /// Rule whose head can match the consumer literal.
36    pub producer: RuleIndex,
37    /// Rule whose body contains the consumer literal.
38    pub consumer: RuleIndex,
39    /// The consumer literal.
40    pub literal: LiteralPath,
41    /// Negation or aggregation.
42    pub kind: StrictDependencyKind,
43}
44
45impl fmt::Display for StrictDependency {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        write!(
48            f,
49            "{} edge from {} into {}, {}",
50            self.kind, self.producer, self.consumer, self.literal
51        )
52    }
53}
54
55/// A strongly connected rule component containing a strict dependency.
56#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
57#[serde(deny_unknown_fields)]
58pub struct UnstratifiableCycle {
59    /// Rules of the component, sorted.
60    // bounded: rules of one component of the finite program.
61    pub rules: Vec<RuleIndex>,
62    /// Head predicates of those rules, sorted and unique.
63    pub predicates: Vec<PredicateSymbol>,
64    /// The offending strict edge inside the component.
65    pub edge: StrictDependency,
66}
67
68impl fmt::Display for UnstratifiableCycle {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.write_str("recursive cycle through ")?;
71        self.edge.kind.fmt(f)?;
72        f.write_str(" among rules ")?;
73        for (index, rule) in self.rules.iter().enumerate() {
74            if index > 0 {
75                f.write_str(", ")?;
76            }
77            write!(f, "{}", rule.get())?;
78        }
79        f.write_str(" (predicates ")?;
80        for (index, predicate) in self.predicates.iter().enumerate() {
81            if index > 0 {
82                f.write_str(", ")?;
83            }
84            f.write_str(predicate.as_str())?;
85        }
86        write!(f, "); offending {}", self.edge)
87    }
88}