Skip to main content

datalog_structural_model/ir/error/
variants.rs

1//! The [`ProgramValidationError`] variants.
2
3use crate::ir::symbols::VariableSymbol;
4use crate::position::{HeadAtomPath, LiteralPath, RuleIndex, RuleSite, TermPath};
5use crate::stratify::UnstratifiableCycle;
6use crate::termination::ArgumentPosition;
7
8/// Program validation failures. Every failure tied to a location names the
9/// rule and, where narrower, the literal and term inside it.
10#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
11#[serde(deny_unknown_fields)]
12pub enum ProgramValidationError {
13    /// Head variables are missing from the positive body, `BIND` outputs, and
14    /// aggregate exports, and are not existentially quantified.
15    UnsafeRule {
16        /// Rule whose head is unsafe.
17        rule: RuleIndex,
18        /// The first head atom with missing variables.
19        atom: HeadAtomPath,
20        /// Its missing variables.
21        variables: Vec<VariableSymbol>,
22    },
23    /// An existentially quantified head variable occurs in no head atom.
24    UnusedExistentialVariable {
25        /// Rule whose head quantifies the variable.
26        rule: RuleIndex,
27        /// The first unused existential variable.
28        variable: VariableSymbol,
29    },
30    /// An existentially quantified head variable is also bound by the body,
31    /// so it would denote a body value rather than a witness.
32    ExistentialVariableBoundInBody {
33        /// Rule whose head quantifies the variable.
34        rule: RuleIndex,
35        /// The first such existential variable.
36        variable: VariableSymbol,
37    },
38    /// A negated body literal references variables no positive body literal
39    /// binds (negation-as-failure requires a ground negated atom).
40    UnsafeNegation {
41        /// Rule containing the literal.
42        rule: RuleIndex,
43        /// The first offending negated literal.
44        literal: LiteralPath,
45        /// Its unbound variables.
46        variables: Vec<VariableSymbol>,
47    },
48    /// A [`Literal::Filter`](crate::Literal::Filter) references unbound
49    /// variables. Filters guard bindings; they cannot introduce them.
50    UnsafeFilter {
51        /// Rule containing the literal.
52        rule: RuleIndex,
53        /// The first offending filter.
54        literal: LiteralPath,
55        /// Its unbound variables.
56        variables: Vec<VariableSymbol>,
57    },
58    /// A [`Literal::Bind`](crate::Literal::Bind) references a variable not
59    /// bound earlier, or its output shadows an already-bound variable.
60    UnsafeBind {
61        /// Rule containing the literal.
62        rule: RuleIndex,
63        /// The first offending `BIND`.
64        literal: LiteralPath,
65        /// Unbound expression references or the colliding output name.
66        variables: Vec<VariableSymbol>,
67    },
68    /// A [`Literal::Aggregate`](crate::Literal::Aggregate) violates
69    /// aggregation safety: unbound `input_var` or group key, non-fresh
70    /// `result_var`, or an unsafe inner negation, filter, or `BIND`.
71    UnsafeAggregate {
72        /// Rule containing the literal.
73        rule: RuleIndex,
74        /// The first offending aggregate.
75        literal: LiteralPath,
76        /// Offending variables.
77        variables: Vec<VariableSymbol>,
78    },
79    /// An aggregate's inner body contains another aggregate. Lift the inner
80    /// aggregate into its own rule.
81    NestedAggregateInInnerBody {
82        /// Rule containing the literal.
83        rule: RuleIndex,
84        /// Path to the nested aggregate.
85        literal: LiteralPath,
86    },
87    /// The program contains a recursive cycle through negation or
88    /// aggregation.
89    UnstratifiableCycle(UnstratifiableCycle),
90    /// A negative literal under
91    /// [`EqualityMode::SameAs`](crate::EqualityMode::SameAs); equality
92    /// rewriting and negation-as-failure are mutually exclusive.
93    NegationUnderEqualityMode {
94        /// Rule containing the literal.
95        rule: RuleIndex,
96        /// The negative literal.
97        literal: LiteralPath,
98    },
99    /// An aggregate literal under
100    /// [`EqualityMode::SameAs`](crate::EqualityMode::SameAs); aggregation
101    /// needs a complete extension that equality rewriting keeps changing.
102    AggregationUnderEqualityMode {
103        /// Rule containing the literal.
104        rule: RuleIndex,
105        /// The aggregate literal.
106        literal: LiteralPath,
107    },
108    /// A body literal carries a [`Term::Skolem`](crate::Term::Skolem).
109    /// SKOLEM mints IRIs and is only well-defined in the head.
110    SkolemInBodyAtom {
111        /// Rule containing the literal.
112        rule: RuleIndex,
113        /// The body literal.
114        literal: LiteralPath,
115        /// The Skolem term inside it.
116        term: TermPath,
117    },
118    /// A [`Term::TripleTerm`](crate::Term::TripleTerm) nests deeper than
119    /// [`ValidationLimits::triple_term_nesting`](crate::ValidationLimits::triple_term_nesting).
120    TripleTermNestingExceeded {
121        /// Rule containing the term.
122        rule: RuleIndex,
123        /// Head or body literal containing the term.
124        site: RuleSite,
125        /// The outermost term whose nesting exceeds the limit.
126        term: TermPath,
127        /// Observed nesting depth (always greater than `limit`).
128        depth: usize,
129        /// Configured limit at the time of validation.
130        limit: usize,
131    },
132    /// A Skolem argument is neither a variable nor a constant.
133    InvalidSkolemArguments {
134        /// Rule containing the term.
135        rule: RuleIndex,
136        /// Head or body literal containing the term.
137        site: RuleSite,
138        /// The Skolem term.
139        term: TermPath,
140    },
141    /// The program has more rules than
142    /// [`ValidationLimits::rules`](crate::ValidationLimits::rules).
143    RuleCountExceeded {
144        /// Number of rules in the program.
145        count: usize,
146        /// Configured limit.
147        limit: usize,
148    },
149    /// A rule body has more literals than
150    /// [`ValidationLimits::body_literals`](crate::ValidationLimits::body_literals).
151    BodyLengthExceeded {
152        /// The rule.
153        rule: RuleIndex,
154        /// Its literal count, aggregate inner bodies included.
155        length: usize,
156        /// Configured limit.
157        limit: usize,
158    },
159    /// Invented values nest deeper than
160    /// [`ValidationLimits::skolem_depth`](crate::ValidationLimits::skolem_depth),
161    /// as measured by [`WeakAcyclicity::skolem_depth`](crate::WeakAcyclicity::skolem_depth).
162    SkolemDepthExceeded {
163        /// The smallest position reaching `depth`.
164        position: ArgumentPosition,
165        /// Depth reached.
166        depth: usize,
167        /// Configured limit.
168        limit: usize,
169    },
170}