Skip to main content

datalog_structural_model/termination/
criterion.rs

1//! [`TerminationCriterion`] — the versioned sufficient condition proved.
2
3use std::fmt;
4
5/// Version of the evidence law a [`TerminationCriterion`] proves. A change to
6/// what a criterion accepts or how it reports bumps the version.
7#[derive(
8    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
9)]
10#[serde(transparent)]
11pub struct LawVersion(u32);
12
13impl LawVersion {
14    /// Wrap a law version number.
15    #[must_use]
16    pub const fn new(version: u32) -> Self {
17        Self(version)
18    }
19
20    /// The version number.
21    #[must_use]
22    pub const fn get(self) -> u32 {
23        self.0
24    }
25}
26
27impl fmt::Display for LawVersion {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(f, "v{}", self.0)
30    }
31}
32
33/// A sufficient criterion for the termination of bottom-up evaluation.
34///
35/// Failing a criterion is not a proof of non-termination.
36#[derive(
37    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
38)]
39#[non_exhaustive]
40pub enum TerminationCriterion {
41    /// No value-inventing edge of the position graph lies on a cycle.
42    WeaklyAcyclic,
43}
44
45impl TerminationCriterion {
46    /// Stable kebab-case name, independent of presentation.
47    #[must_use]
48    pub const fn label(self) -> &'static str {
49        match self {
50            Self::WeaklyAcyclic => "weakly-acyclic",
51        }
52    }
53
54    /// Version of the law this criterion currently proves.
55    #[must_use]
56    pub const fn law_version(self) -> LawVersion {
57        match self {
58            Self::WeaklyAcyclic => LawVersion::new(1),
59        }
60    }
61}
62
63impl fmt::Display for TerminationCriterion {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        write!(f, "{} {}", self.label(), self.law_version())
66    }
67}