datalog_structural_model/stratify/
cycle.rs1use std::fmt;
5
6use crate::ir::PredicateSymbol;
7use crate::position::{LiteralPath, RuleIndex};
8
9#[derive(
11 Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
12)]
13#[serde(deny_unknown_fields)]
14pub enum StrictDependencyKind {
15 Negation,
17 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#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
33#[serde(deny_unknown_fields)]
34pub struct StrictDependency {
35 pub producer: RuleIndex,
37 pub consumer: RuleIndex,
39 pub literal: LiteralPath,
41 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#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
57#[serde(deny_unknown_fields)]
58pub struct UnstratifiableCycle {
59 pub rules: Vec<RuleIndex>,
62 pub predicates: Vec<PredicateSymbol>,
64 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}