Skip to main content

datalog_structural_model/termination/
violation.rs

1//! [`TerminationViolation`] — the cyclic special edge that defeats a criterion.
2
3use std::{error::Error, fmt};
4
5use super::criterion::{LawVersion, TerminationCriterion};
6use super::edge::{ArgumentPosition, SpecialEdge};
7use crate::error_code::ErrorCode;
8
9/// A value-inventing edge whose endpoints share a strongly connected
10/// component of the position graph, so a recursive program can keep
11/// inventing values. This defeats the criterion; it does not prove that
12/// every evaluation diverges.
13#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
14#[serde(deny_unknown_fields)]
15pub struct TerminationViolation {
16    #[serde(deserialize_with = "super::criterion::deserialize_current_law")]
17    law_version: LawVersion,
18    criterion: TerminationCriterion,
19    edge: Box<SpecialEdge>,
20    // bounded: positions of one component of the finite program.
21    component: Vec<ArgumentPosition>,
22}
23
24impl TerminationViolation {
25    pub(super) fn new(
26        criterion: TerminationCriterion,
27        edge: SpecialEdge,
28        mut component: Vec<ArgumentPosition>,
29    ) -> Self {
30        component.sort();
31        Self {
32            law_version: criterion.law_version(),
33            criterion,
34            edge: Box::new(edge),
35            component,
36        }
37    }
38
39    /// The law that produced this violation. Decoding requires the current law.
40    #[must_use]
41    pub fn law_version(&self) -> LawVersion {
42        self.law_version
43    }
44
45    /// The criterion that was not met.
46    #[must_use]
47    pub fn criterion(&self) -> TerminationCriterion {
48        self.criterion
49    }
50
51    /// The first cyclic special edge in canonical order.
52    #[must_use]
53    pub fn edge(&self) -> &SpecialEdge {
54        &self.edge
55    }
56
57    /// Positions of the edge's strongly connected component, sorted.
58    #[must_use]
59    pub fn component(&self) -> &[ArgumentPosition] {
60        &self.component
61    }
62
63    /// Code of every violation of [`TerminationCriterion::WeaklyAcyclic`].
64    pub const CODE_NOT_WEAKLY_ACYCLIC: ErrorCode = ErrorCode::new("not-weakly-acyclic");
65
66    /// Stable kebab-case code of this violation.
67    #[must_use]
68    pub fn code(&self) -> ErrorCode {
69        match self.criterion {
70            TerminationCriterion::WeaklyAcyclic => Self::CODE_NOT_WEAKLY_ACYCLIC,
71        }
72    }
73}
74
75impl fmt::Display for TerminationViolation {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        write!(
78            f,
79            "[{}] {} {} is not met: {} lies on a cycle through positions ",
80            self.code(),
81            self.criterion.label(),
82            self.law_version,
83            self.edge
84        )?;
85        for (index, position) in self.component.iter().enumerate() {
86            if index > 0 {
87                f.write_str("; ")?;
88            }
89            position.fmt(f)?;
90        }
91        Ok(())
92    }
93}
94
95impl Error for TerminationViolation {}