pulsatrix
Loading...
Searching...
No Matches
datalog_atom.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstddef>
9#include <string>
10#include <vector>
11
13
15
24class Atom {
25public:
26 Atom(std::string predicate_name, std::vector<Term> terms)
27 : predicate_name_(std::move(predicate_name)), terms_(std::move(terms)) {}
28
29 [[nodiscard]] const std::string& predicate_name() const { return predicate_name_; }
30 [[nodiscard]] const std::vector<Term>& terms() const { return terms_; }
31 [[nodiscard]] std::size_t arity() const { return terms_.size(); }
32
34 [[nodiscard]] bool is_ground() const {
35 for (const Term& term : terms_) {
36 if (!term.is_constant()) return false;
37 }
38 return true;
39 }
40
41 [[nodiscard]] bool operator==(const Atom& other) const {
42 return predicate_name_ == other.predicate_name_ && terms_ == other.terms_;
43 }
44 [[nodiscard]] bool operator!=(const Atom& other) const { return !(*this == other); }
45
46private:
47 std::string predicate_name_;
48 std::vector<Term> terms_;
49};
50
52struct AtomHash {
53 [[nodiscard]] std::size_t operator()(const Atom& atom) const {
54 std::size_t h = std::hash<std::string>{}(atom.predicate_name());
55 for (const Term& term : atom.terms()) {
56 h ^= (TermHash{}(term) + 0x9e3779b9U + (h << 6) + (h >> 2));
57 }
58 return h;
59 }
60};
61
62} // namespace pulsatrix::datalog
An atom is a predicate name applied to a tuple of Terms, e.g. edge(a, b) or ancestor(X,...
Definition datalog_atom.hpp:24
const std::vector< Term > & terms() const
Definition datalog_atom.hpp:30
bool operator==(const Atom &other) const
Definition datalog_atom.hpp:41
std::size_t arity() const
Definition datalog_atom.hpp:31
const std::string & predicate_name() const
Definition datalog_atom.hpp:29
bool operator!=(const Atom &other) const
Definition datalog_atom.hpp:44
Atom(std::string predicate_name, std::vector< Term > terms)
Definition datalog_atom.hpp:26
bool is_ground() const
True iff every term in this atom is a constant (no variables).
Definition datalog_atom.hpp:34
A Datalog term: either a constant (e.g. a) or a variable (e.g. X) – never a compound/function-symbol ...
Definition datalog_term.hpp:38
Function-symbol-free Datalog term – a constant or a variable, never a compound term....
Definition datalog_atom.hpp:14
Hash functor for Atom, for use in std::unordered_map/std::unordered_set.
Definition datalog_atom.hpp:52
std::size_t operator()(const Atom &atom) const
Definition datalog_atom.hpp:53
Hash functor for Term, for use in std::unordered_map/std::unordered_set.
Definition datalog_term.hpp:71