Skip to main content

datalog_structural_model/ir/head/
form.rs

1//! [`HeadForm`] — the logical shape of a rule head.
2
3use std::fmt;
4
5use crate::DatalogFeature;
6
7/// Which logical form a [`RuleHead`](super::RuleHead) has.
8///
9/// The form is a pure function of the head with fixed precedence: falsum,
10/// then existential, then disjunctive (more than one disjunct), then
11/// conjunctive (more than one atom), then atomic. A one-disjunct disjunction
12/// classifies by its conjunction and a one-atom conjunction is atomic, so the
13/// classification depends on content, not on the constructor used.
14#[derive(
15    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
16)]
17pub enum HeadForm {
18    /// Exactly one atom and no existential variable: plain Datalog.
19    Atomic,
20    /// One disjunct of several atoms, no existential variable.
21    Conjunctive,
22    /// At least one existentially quantified head variable.
23    Existential,
24    /// Several disjuncts, no existential variable.
25    Disjunctive,
26    /// The head is `false`: a body match is an inconsistency witness.
27    Inconsistency,
28}
29
30impl HeadForm {
31    /// Every form, in declaration order.
32    pub const ALL: &'static [Self] = &[
33        Self::Atomic,
34        Self::Conjunctive,
35        Self::Existential,
36        Self::Disjunctive,
37        Self::Inconsistency,
38    ];
39
40    /// The capability a profile must declare to accept this form; `None` for
41    /// the atomic form every Datalog profile accepts.
42    #[must_use]
43    pub const fn required_feature(self) -> Option<DatalogFeature> {
44        match self {
45            Self::Atomic => None,
46            Self::Conjunctive => Some(DatalogFeature::ConjunctiveHeads),
47            Self::Existential => Some(DatalogFeature::ExistentialHeads),
48            Self::Disjunctive => Some(DatalogFeature::DisjunctiveHeads),
49            Self::Inconsistency => Some(DatalogFeature::InconsistencyConstraints),
50        }
51    }
52
53    /// Stable kebab-case name.
54    #[must_use]
55    pub const fn as_str(self) -> &'static str {
56        match self {
57            Self::Atomic => "atomic",
58            Self::Conjunctive => "conjunctive",
59            Self::Existential => "existential",
60            Self::Disjunctive => "disjunctive",
61            Self::Inconsistency => "inconsistency",
62        }
63    }
64}
65
66impl fmt::Display for HeadForm {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        f.write_str(self.as_str())
69    }
70}