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
//! Datalog [`Program`] — an ordered set of [`Rule`]s plus
//! validation and stratification entry points.
use super::equality_mode::EqualityMode;
use super::error::ProgramValidationError;
use super::head::{ExistentialFault, HeadFault};
use super::literal::Literal;
use super::rule::{LiteralFault, Rule};
use crate::limits::ValidationLimits;
use crate::position::{LiteralIndex, LiteralPath, RuleIndex};
use crate::stratify::{Stratification, stratify};
/// Datalog program as an ordered set of rules.
#[derive(Clone, Debug, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(try_from = "super::serde_input::ProgramInput")]
pub struct Program {
/// Program rules.
pub rules: Vec<Rule>,
}
impl Program {
/// Construct a program only when all safety and stratification checks pass.
/// # Errors
/// Returns the same structural failures as [`Self::validate`].
pub fn try_new(rules: Vec<Rule>) -> Result<Self, ProgramValidationError> {
let program = Self { rules };
program.validate()?;
Ok(program)
}
/// Validate all rules in the program for safety and
/// stratifiability.
///
/// Runs two checks:
///
/// 1. **Safety** — every rule must be range-restricted; head variables and
/// variables inside negated body literals must also appear in some
/// positive body literal.
/// 2. **Stratifiability** — the compatible-rule dependency graph must have no
/// cycle that includes a negation edge (see [`Program::stratify`]).
///
/// # Errors
///
/// Returns [`ProgramValidationError::UnsafeRule`] on a safety
/// failure, [`ProgramValidationError::UnsafeNegation`] when a
/// negated body literal references a variable that no positive
/// literal binds, or
/// [`ProgramValidationError::UnstratifiableCycle`] when the
/// program contains a recursive cycle through negation.
pub fn validate(&self) -> Result<(), ProgramValidationError> {
self.validate_within(EqualityMode::Default, &ValidationLimits::standard())
}
/// Validate the program under a specified equality-reasoning
/// mode (extension).
///
/// Runs the same safety / stratification / triple-term-nesting
/// checks as [`Self::validate`] and additionally rejects
/// negation and aggregation when
/// `equality_mode == EqualityMode::SameAs`. The two features
/// interact non-monotonically with `owl:sameAs` rewriting; see
/// [`EqualityMode`] for the semantic rationale.
///
/// # Errors
///
/// Returns the same errors as [`Self::validate`] plus
/// [`ProgramValidationError::NegationUnderEqualityMode`] or
/// [`ProgramValidationError::AggregationUnderEqualityMode`]
/// when the corresponding feature is used under
/// [`EqualityMode::SameAs`].
pub fn validate_with_equality_mode(
&self,
equality_mode: EqualityMode,
) -> Result<(), ProgramValidationError> {
self.validate_within(equality_mode, &ValidationLimits::standard())
}
/// Validate under an equality mode and explicit structural `limits`.
///
/// Checks the rule count, each rule's body length and triple-term
/// nesting against `limits` before the safety and stratification checks
/// of [`Self::validate_with_equality_mode`]. The Skolem depth limit
/// needs the termination analysis and is enforced by
/// [`Self::validate_for`].
///
/// # Errors
///
/// Returns [`ProgramValidationError::RuleCountExceeded`],
/// [`ProgramValidationError::BodyLengthExceeded`] or
/// [`ProgramValidationError::TripleTermNestingExceeded`] with the limit
/// and the value reached, or any error of
/// [`Self::validate_with_equality_mode`].
pub fn validate_within(
&self,
equality_mode: EqualityMode,
limits: &ValidationLimits,
) -> Result<(), ProgramValidationError> {
if self.rules.len() > limits.rules() {
return Err(ProgramValidationError::RuleCountExceeded {
count: self.rules.len(),
limit: limits.rules(),
});
}
if equality_mode == EqualityMode::SameAs {
check_equality_mode_compatibility(&self.rules)?;
}
for (index, rule) in self.rules.iter().enumerate() {
rule.validate(RuleIndex::new(index), limits)?;
}
// Stratification check — reject programs with recursion
// through negation.
self.stratify()?;
Ok(())
}
/// Compute the program's stratification.
///
/// Connects producers to consumers whose complete tuple patterns can
/// unify, collapses strongly connected components, and assigns each rule
/// to a stratum. Negative and aggregate dependencies require a strictly
/// earlier producer stratum. Pure EDB inputs are available before stratum
/// zero. Rule indices refer to this program's rule order.
///
/// # Errors
///
/// Returns [`ProgramValidationError::UnstratifiableCycle`]
/// when a cycle in the dependency graph contains a negation
/// edge.
pub fn stratify(&self) -> Result<Stratification, ProgramValidationError> {
stratify(self).map_err(ProgramValidationError::from)
}
}
/// Reject negation and aggregation under [`EqualityMode::SameAs`], naming
/// the first offending top-level body literal.
fn check_equality_mode_compatibility(rules: &[Rule]) -> Result<(), ProgramValidationError> {
for (index, rule) in rules.iter().enumerate() {
let rule_index = RuleIndex::new(index);
for (position, lit) in rule.body.iter().enumerate() {
let literal = LiteralPath::top(LiteralIndex::new(position));
match lit {
Literal::Negative(_) => {
return Err(ProgramValidationError::NegationUnderEqualityMode {
rule: rule_index,
literal,
});
}
Literal::Aggregate(_) => {
return Err(ProgramValidationError::AggregationUnderEqualityMode {
rule: rule_index,
literal,
});
}
Literal::Positive(_) | Literal::Filter(_) | Literal::Bind { .. } => {}
}
}
}
Ok(())
}
impl Rule {
/// Per-rule shape and safety checks, in reporting order.
fn validate(
&self,
rule: RuleIndex,
limits: &ValidationLimits,
) -> Result<(), ProgramValidationError> {
let length = self.literal_count();
if length > limits.body_literals() {
return Err(ProgramValidationError::BodyLengthExceeded {
rule,
length,
limit: limits.body_literals(),
});
}
self.validate_term_shapes(rule, limits)?;
match self.existential_fault() {
Some(ExistentialFault::Unused(variable)) => {
return Err(ProgramValidationError::UnusedExistentialVariable { rule, variable });
}
Some(ExistentialFault::BoundInBody(variable)) => {
return Err(ProgramValidationError::ExistentialVariableBoundInBody {
rule,
variable,
});
}
None => {}
}
if let Some(HeadFault { atom, variables }) = self.unsafe_head_variables() {
return Err(ProgramValidationError::UnsafeRule {
rule,
atom,
variables,
});
}
if let Some(LiteralFault { literal, variables }) = self.unsafe_negative_variables() {
return Err(ProgramValidationError::UnsafeNegation {
rule,
literal,
variables,
});
}
if let Some(LiteralFault { literal, variables }) = self.unsafe_filter_variables() {
return Err(ProgramValidationError::UnsafeFilter {
rule,
literal,
variables,
});
}
if let Some(LiteralFault { literal, variables }) = self.unsafe_bind_variables() {
return Err(ProgramValidationError::UnsafeBind {
rule,
literal,
variables,
});
}
if let Some(literal) = self.nested_aggregate_in_inner_body() {
return Err(ProgramValidationError::NestedAggregateInInnerBody { rule, literal });
}
if let Some(LiteralFault { literal, variables }) = self.unsafe_aggregate_variables() {
return Err(ProgramValidationError::UnsafeAggregate {
rule,
literal,
variables,
});
}
Ok(())
}
}