Skip to main content

datalog_structural_model/termination/
edge.rs

1//! Positions of the position graph and its value-inventing edges.
2
3use std::fmt;
4
5use crate::ir::PredicateSymbol;
6use crate::position::{AtomSlot, RuleIndex, RuleSite};
7
8/// One argument slot of one atom occurrence: a node of the position graph.
9///
10/// Positions are refined by occurrence: a head slot feeds a body slot only
11/// when the head and body atoms can denote the same fact, so constants that
12/// keep two patterns apart also keep their positions apart.
13#[derive(
14    Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
15)]
16#[serde(deny_unknown_fields)]
17pub struct ArgumentPosition {
18    /// The atom's predicate.
19    pub predicate: PredicateSymbol,
20    /// The top-level argument slot.
21    pub slot: AtomSlot,
22    /// The rule containing the atom.
23    pub rule: RuleIndex,
24    /// The head, or the positive body literal holding the atom.
25    pub site: RuleSite,
26}
27
28impl ArgumentPosition {
29    /// Slot `slot` of the `predicate` atom at `site` of `rule`.
30    #[must_use]
31    pub fn new(
32        predicate: PredicateSymbol,
33        slot: AtomSlot,
34        rule: RuleIndex,
35        site: RuleSite,
36    ) -> Self {
37        Self {
38            predicate,
39            slot,
40            rule,
41            site,
42        }
43    }
44}
45
46impl fmt::Display for ArgumentPosition {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        write!(
49            f,
50            "{}[{}] ({}, {})",
51            self.predicate.as_str(),
52            self.slot.get(),
53            self.rule,
54            self.site
55        )
56    }
57}
58
59/// How a rule head builds a value that need not occur in its body.
60#[derive(
61    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
62)]
63pub enum InventionKind {
64    /// A `SKOLEM(...)` head term mints a value from its arguments.
65    Skolem,
66    /// A head triple term wraps body values into a larger term.
67    TripleTerm,
68    /// A `BIND` expression computes a value from body values.
69    Bind,
70    /// An aggregate folds body values into a result value.
71    Aggregate,
72    /// An existential head variable stands for a witness that depends on
73    /// the rule's frontier variables.
74    Existential,
75}
76
77impl fmt::Display for InventionKind {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        f.write_str(match self {
80            Self::Skolem => "Skolem",
81            Self::TripleTerm => "triple-term",
82            Self::Bind => "BIND",
83            Self::Aggregate => "aggregate",
84            Self::Existential => "existential",
85        })
86    }
87}
88
89/// A special (value-inventing) edge of the position graph: a value bound at
90/// body position `from` feeds a value invented at head position `to` of the
91/// same rule.
92///
93/// The derived order — `from`, `to`, then `kind` — is the canonical order
94/// used to pick the reported violation.
95#[derive(
96    Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
97)]
98#[serde(deny_unknown_fields)]
99pub struct SpecialEdge {
100    /// Body position the input value is bound at.
101    pub from: ArgumentPosition,
102    /// Head position receiving the invented value.
103    pub to: ArgumentPosition,
104    /// The inventing construct.
105    pub kind: InventionKind,
106}
107
108impl fmt::Display for SpecialEdge {
109    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110        write!(f, "{} edge from {} to {}", self.kind, self.from, self.to)
111    }
112}