datalog_structural_model/ir/error/
display.rs1use std::error::Error;
5use std::fmt;
6
7use super::variants::ProgramValidationError;
8use crate::ir::symbols::VariableSymbol;
9use crate::stratify::StratificationError;
10
11impl From<StratificationError> for ProgramValidationError {
12 fn from(err: StratificationError) -> Self {
13 let StratificationError::UnstratifiableCycle(cycle) = err;
14 Self::UnstratifiableCycle(cycle)
15 }
16}
17
18struct VariableList<'a>(&'a [VariableSymbol]);
19
20impl fmt::Display for VariableList<'_> {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 for (index, var) in self.0.iter().enumerate() {
23 if index > 0 {
24 f.write_str(", ")?;
25 }
26 f.write_str(var.as_str())?;
27 }
28 Ok(())
29 }
30}
31
32impl fmt::Display for ProgramValidationError {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 write!(f, "[{}] ", self.code())?;
35 match self {
36 Self::UnsafeRule {
37 rule,
38 atom,
39 variables,
40 } => write!(
41 f,
42 "{rule} is unsafe at {atom}; head variables not bound by the body: {}",
43 VariableList(variables)
44 ),
45 Self::UnusedExistentialVariable { rule, variable } => write!(
46 f,
47 "{rule}: existential variable {} occurs in no head atom",
48 variable.as_str()
49 ),
50 Self::ExistentialVariableBoundInBody { rule, variable } => write!(
51 f,
52 "{rule}: existential variable {} is also bound by the body",
53 variable.as_str()
54 ),
55 Self::UnsafeNegation {
56 rule,
57 literal,
58 variables,
59 } => write!(
60 f,
61 "{rule}, {literal}: negated literal uses variables not bound by the positive \
62 body: {}",
63 VariableList(variables)
64 ),
65 Self::UnsafeFilter {
66 rule,
67 literal,
68 variables,
69 } => write!(
70 f,
71 "{rule}, {literal}: filter uses variables not bound by the positive body: {}",
72 VariableList(variables)
73 ),
74 Self::UnsafeBind {
75 rule,
76 literal,
77 variables,
78 } => write!(
79 f,
80 "{rule}, {literal}: unsafe BIND (unbound expression reference or colliding \
81 output name): {}",
82 VariableList(variables)
83 ),
84 Self::UnsafeAggregate {
85 rule,
86 literal,
87 variables,
88 } => write!(
89 f,
90 "{rule}, {literal}: unsafe aggregate (unbound input or group key, colliding \
91 result variable, or unsafe inner literal): {}",
92 VariableList(variables)
93 ),
94 Self::NestedAggregateInInnerBody { rule, literal } => write!(
95 f,
96 "{rule}, {literal}: aggregate nested inside an aggregate's inner body; lift it \
97 into its own rule"
98 ),
99 Self::UnstratifiableCycle(cycle) => write!(f, "program contains a {cycle}"),
100 Self::NegationUnderEqualityMode { rule, literal } => write!(
101 f,
102 "{rule}, {literal}: negative literal while equality mode is `SameAs`"
103 ),
104 Self::AggregationUnderEqualityMode { rule, literal } => write!(
105 f,
106 "{rule}, {literal}: aggregate literal while equality mode is `SameAs`"
107 ),
108 Self::SkolemInBodyAtom {
109 rule,
110 literal,
111 term,
112 } => write!(
113 f,
114 "{rule}, {literal}, {term}: SKOLEM term in a body literal; SKOLEM is only \
115 allowed in the head"
116 ),
117 Self::TripleTermNestingExceeded {
118 rule,
119 site,
120 term,
121 depth,
122 limit,
123 } => write!(
124 f,
125 "{rule}, {site}, {term}: triple term nested {depth} deep; limit is {limit}"
126 ),
127 Self::InvalidSkolemArguments { rule, site, term } => write!(
128 f,
129 "{rule}, {site}, {term}: Skolem arguments must be variables or constants"
130 ),
131 Self::RuleCountExceeded { count, limit } => {
132 write!(f, "program has {count} rules; limit is {limit}")
133 }
134 Self::BodyLengthExceeded {
135 rule,
136 length,
137 limit,
138 } => write!(f, "{rule}: body has {length} literals; limit is {limit}"),
139 Self::SkolemDepthExceeded {
140 position,
141 depth,
142 limit,
143 } => write!(
144 f,
145 "invented values reach depth {depth} at {position}; limit is {limit}"
146 ),
147 }
148 }
149}
150
151impl Error for ProgramValidationError {}