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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! The position graph and [`Program::check_weak_acyclicity`].
use std::collections::{BTreeMap, BTreeSet};
use super::criterion::TerminationCriterion;
use super::edge::{ArgumentPosition, InventionKind, SpecialEdge};
use super::sources::VariableSources;
use super::verdict::WeakAcyclicity;
use super::violation::TerminationViolation;
use crate::ir::{Atom, Program, RuleHead, Term, VariableSymbol};
use crate::position::{AtomSlot, HeadAtomPath, RuleIndex, RuleSite};
use crate::scc::SuccessorGraph;
use crate::stratify::overlaps;
/// Nodes are predicate positions; an edge carries a value from a body
/// position to a head position, specially when the head invents a value.
#[derive(Debug, Default)]
struct PositionGraph {
// bounded: one node per distinct position of the finite program.
ids: BTreeMap<ArgumentPosition, usize>,
positions: Vec<ArgumentPosition>,
// bounded: edges are pairs of authored occurrences, never facts.
successors: Vec<Vec<(usize, bool)>>,
special: Vec<SpecialEdge>,
}
impl PositionGraph {
fn build(program: &Program) -> Self {
let mut graph = Self::default();
// bounded: one scope per rule of the finite program.
let scopes: Vec<_> = program
.rules
.iter()
.enumerate()
.map(|(index, rule)| VariableSources::of_body(RuleIndex::new(index), &rule.body))
.collect();
for (index, (rule, sources)) in program.rules.iter().zip(&scopes).enumerate() {
let head = HeadOccurrences::new(RuleIndex::new(index), &rule.head);
graph.add_head(&head, sources);
}
for (producer, rule) in program.rules.iter().enumerate() {
let head = HeadOccurrences::new(RuleIndex::new(producer), &rule.head);
graph.add_flows(&head, &scopes);
}
graph
}
fn node(&mut self, position: &ArgumentPosition) -> usize {
if let Some(&id) = self.ids.get(position) {
return id;
}
let id = self.positions.len();
self.ids.insert(position.clone(), id);
self.positions.push(position.clone());
self.successors.push(Vec::new());
id
}
fn edge(
&mut self,
from: &ArgumentPosition,
to: &ArgumentPosition,
kind: Option<InventionKind>,
) {
let source = self.node(from);
let target = self.node(to);
self.successors[source].push((target, kind.is_some()));
if let Some(kind) = kind {
self.special.push(SpecialEdge {
from: from.clone(),
to: to.clone(),
kind,
});
}
}
/// Edges inside one rule, from the body positions binding each head
/// variable to the head slot holding it. An existential variable has no
/// body source; like a Skolem term over the frontier, it gets a special
/// edge from every body position binding a frontier variable.
fn add_head(&mut self, head: &HeadOccurrences<'_>, sources: &VariableSources<'_>) {
for (path, atom) in head.head.atoms() {
for (slot, term) in atom.terms.iter().enumerate() {
let to = head.position(path, atom, slot);
self.node(&to);
let wrapper = match term {
Term::Variable(_) | Term::Constant(_) => None,
Term::TripleTerm(_) => Some(InventionKind::TripleTerm),
Term::Skolem { .. } => Some(InventionKind::Skolem),
};
let mut existential = false;
term.visit_variables(&mut |variable| {
existential |= head.head.is_existential(variable);
if let Some(kind) = sources.clock_invention(variable) {
// A clock may produce a fresh value at each firing,
// even without data arguments. Every positive body
// atom controls whether this firing occurs.
for body in &sources.atoms {
for slot in 0..body.atom.arity() {
self.edge(
&body.position(head.rule, slot),
&to,
Some(wrapper.unwrap_or(kind)),
);
}
}
}
for source in sources.of(variable) {
self.edge(&source.position, &to, wrapper.or(source.invention));
}
});
if existential {
for variable in head.frontier() {
for source in sources.of(&variable) {
self.edge(&source.position, &to, Some(InventionKind::Existential));
}
}
}
}
}
}
/// Ordinary edges from a head slot to the same slot of every positive
/// body atom, in any rule, whose pattern can match the head's facts.
fn add_flows(&mut self, head: &HeadOccurrences<'_>, scopes: &[VariableSources<'_>]) {
for (path, atom) in head.head.atoms() {
for (consumer, scope) in scopes.iter().enumerate() {
for body in scope.atoms.iter().filter(|body| overlaps(atom, body.atom)) {
for slot in 0..atom.arity() {
let from = head.position(path, atom, slot);
let to = body.position(RuleIndex::new(consumer), slot);
self.edge(&from, &to, None);
}
}
}
}
}
fn component_of(&self, sccs: &[BTreeSet<usize>]) -> Vec<usize> {
// bounded: one component id per position of the finite program.
let mut component = vec![0; self.positions.len()];
for (index, members) in sccs.iter().enumerate() {
for &node in members {
component[node] = index;
}
}
component
}
fn violation(
&self,
sccs: &[BTreeSet<usize>],
component: &[usize],
) -> Option<TerminationViolation> {
let edge = self
.special
.iter()
.filter(|edge| component[self.ids[&edge.from]] == component[self.ids[&edge.to]])
.min()?;
let members = &sccs[component[self.ids[&edge.from]]];
let positions = members
.iter()
.map(|&id| self.positions[id].clone())
.collect();
Some(TerminationViolation::new(
TerminationCriterion::WeaklyAcyclic,
edge.clone(),
positions,
))
}
/// Longest special-edge count over the acyclic condensation. Tarjan
/// emits sinks first, so walking components in reverse settles every
/// predecessor before its successors.
fn certificate(&self, sccs: &[BTreeSet<usize>], component: &[usize]) -> WeakAcyclicity {
let mut rank = vec![0_usize; sccs.len()];
for index in (0..sccs.len()).rev() {
for &node in &sccs[index] {
for &(target, special) in &self.successors[node] {
let target = component[target];
if target != index {
rank[target] = rank[target].max(rank[index] + usize::from(special));
}
}
}
}
let skolem_depth = rank.iter().copied().max().unwrap_or(0);
let deepest = (skolem_depth > 0)
.then(|| {
self.ids
.iter()
.find(|&(_, &id)| rank[component[id]] == skolem_depth)
.map(|(position, _)| position.clone())
})
.flatten();
WeakAcyclicity {
law_version: TerminationCriterion::WeaklyAcyclic.law_version(),
positions: self.positions.len(),
special_edges: self.special.len(),
skolem_depth,
deepest,
}
}
}
/// The head of one rule, whose atom slots are graph nodes.
struct HeadOccurrences<'a> {
rule: RuleIndex,
head: &'a RuleHead,
}
impl<'a> HeadOccurrences<'a> {
fn new(rule: RuleIndex, head: &'a RuleHead) -> Self {
Self { rule, head }
}
fn position(&self, path: HeadAtomPath, atom: &Atom, slot: usize) -> ArgumentPosition {
ArgumentPosition::new(
atom.predicate.clone(),
AtomSlot::new(slot),
self.rule,
RuleSite::Head(path),
)
}
/// Head variables that are not existential: the values a witness
/// depends on.
fn frontier(&self) -> impl Iterator<Item = VariableSymbol> + '_ {
self.head
.variables()
.into_iter()
.filter(|variable| !self.head.is_existential(variable))
}
}
impl SuccessorGraph for PositionGraph {
fn node_count(&self) -> usize {
self.positions.len()
}
fn successor(&self, node: usize, nth: usize) -> Option<usize> {
self.successors[node].get(nth).map(|&(target, _)| target)
}
}
impl Program {
/// Check [`TerminationCriterion::WeaklyAcyclic`] on the position graph.
///
/// Nodes are argument slots of atom occurrences. Inside a rule, a head
/// variable copied directly from a body slot adds an ordinary edge; a
/// head value built by a Skolem term, a triple term, a `BIND` or an
/// aggregate adds a special edge from each body slot feeding it. A
/// clock-dependent value also receives special edges from every positive
/// body slot controlling its firing, including later body atoms. A head
/// slot holding an existential variable is treated like a Skolem term over
/// the frontier: a special edge arrives from every body slot binding a
/// non-existential head variable. Every atom of a conjunctive,
/// existential or disjunctive head is its own occurrence. Across
/// rules, a head slot feeds the same slot of every positive body atom
/// whose complete pattern can match the head, so constants that keep two
/// patterns apart break the cycle. The program is weakly acyclic when no
/// special edge lies on a cycle, which bounds the nesting of invented
/// values and so the number of derivable facts.
///
/// The analysis reads only the program: its time and memory are bounded
/// by program size, never by data. A program without value invention is
/// always weakly acyclic.
///
/// # Errors
///
/// Returns the first cyclic special edge in canonical order, with the
/// positions of its component.
pub fn check_weak_acyclicity(&self) -> Result<WeakAcyclicity, TerminationViolation> {
let graph = PositionGraph::build(self);
let sccs = graph.strongly_connected_components();
let component = graph.component_of(&sccs);
match graph.violation(&sccs, &component) {
Some(violation) => Err(violation),
None => Ok(graph.certificate(&sccs, &component)),
}
}
}