Skip to main content

datalog_structural_model/
capability.rs

1//! Finite sets of explicitly supported Datalog language features.
2use crate::{Aggregator, BindingKind, FilterKind, HeadForm};
3use std::collections::BTreeSet;
4
5/// A language feature required by a rule program.
6/// Positive flat atoms require no extension capabilities.
7#[derive(
8    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
9)]
10pub enum DatalogFeature {
11    /// Stratified negation-as-failure.
12    StratifiedNegation,
13    /// Structured triple terms in rule arguments.
14    TripleTerms,
15    /// Deterministic head-only Skolem generation.
16    SkolemGeneration,
17    /// An exact built-in filter operation.
18    Filter(FilterKind),
19    /// An exact built-in binding operation.
20    Binding(BindingKind),
21    /// An exact aggregation operation.
22    Aggregate(Aggregator),
23    /// Heads that conjoin several atoms.
24    ConjunctiveHeads,
25    /// Heads that quantify variables existentially.
26    ExistentialHeads,
27    /// Heads that disjoin several conjunctions.
28    DisjunctiveHeads,
29    /// Falsum heads: inconsistency constraints.
30    InconsistencyConstraints,
31}
32
33/// Language feature set. This describes a contract; it does not certify an engine.
34#[derive(Clone, Debug, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
35#[serde(transparent)]
36pub struct DatalogCapabilities(BTreeSet<DatalogFeature>);
37impl DatalogCapabilities {
38    /// Pure positive Datalog over flat terms, with no extension operations.
39    #[must_use]
40    pub fn positive() -> Self {
41        Self::default()
42    }
43    /// Declare one additional supported feature.
44    #[must_use]
45    pub fn with(mut self, feature: DatalogFeature) -> Self {
46        self.0.insert(feature);
47        self
48    }
49    /// Inspect one exact feature.
50    #[must_use]
51    pub fn contains(&self, feature: &DatalogFeature) -> bool {
52        self.0.contains(feature)
53    }
54    /// Inspect features in stable structural order.
55    pub fn iter(&self) -> impl Iterator<Item = &DatalogFeature> {
56        self.0.iter()
57    }
58    /// All features representable by this model, without claiming engine support.
59    #[must_use]
60    pub fn all_model_features() -> Self {
61        let mut result = Self::positive()
62            .with(DatalogFeature::StratifiedNegation)
63            .with(DatalogFeature::TripleTerms)
64            .with(DatalogFeature::SkolemGeneration);
65        for form in HeadForm::ALL {
66            if let Some(feature) = form.required_feature() {
67                result.0.insert(feature);
68            }
69        }
70        for kind in FilterKind::ALL {
71            result.0.insert(DatalogFeature::Filter(*kind));
72        }
73        for kind in BindingKind::ALL {
74            result.0.insert(DatalogFeature::Binding(*kind));
75        }
76        for kind in [
77            Aggregator::Count,
78            Aggregator::Sum,
79            Aggregator::Min,
80            Aggregator::Max,
81            Aggregator::Avg,
82        ] {
83            result.0.insert(DatalogFeature::Aggregate(kind));
84        }
85        result
86    }
87    pub(crate) fn insert(&mut self, feature: DatalogFeature) {
88        self.0.insert(feature);
89    }
90    pub(crate) fn missing_from(&self, supported: &Self) -> Self {
91        Self(self.0.difference(&supported.0).copied().collect())
92    }
93    pub(crate) fn is_empty(&self) -> bool {
94        self.0.is_empty()
95    }
96}