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