pulsatrix
Loading...
Searching...
No Matches
datalog_term.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstddef>
9#include <functional>
10#include <string>
11
12namespace pulsatrix::datalog {
13
15enum class TermKind { Constant, Variable };
16
38class Term {
39public:
41 static Term make_constant(std::string value) { return Term(TermKind::Constant, std::move(value)); }
42
44 static Term make_variable(std::string name) { return Term(TermKind::Variable, std::move(name)); }
45
47 [[nodiscard]] TermKind kind() const { return kind_; }
48
50 [[nodiscard]] bool is_constant() const { return kind_ == TermKind::Constant; }
51
53 [[nodiscard]] bool is_variable() const { return kind_ == TermKind::Variable; }
54
56 [[nodiscard]] const std::string& value() const { return value_; }
57
58 [[nodiscard]] bool operator==(const Term& other) const {
59 return kind_ == other.kind_ && value_ == other.value_;
60 }
61 [[nodiscard]] bool operator!=(const Term& other) const { return !(*this == other); }
62
63private:
64 Term(TermKind kind, std::string value) : kind_(kind), value_(std::move(value)) {}
65
66 TermKind kind_;
67 std::string value_;
68};
69
71struct TermHash {
72 [[nodiscard]] std::size_t operator()(const Term& term) const {
73 return std::hash<std::string>{}(term.value()) ^ (static_cast<std::size_t>(term.kind()) << 1);
74 }
75};
76
77} // namespace pulsatrix::datalog
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
bool is_variable() const
True iff this term is a variable.
Definition datalog_term.hpp:53
bool operator==(const Term &other) const
Definition datalog_term.hpp:58
const std::string & value() const
The constant's value, or the variable's name – whichever this term is.
Definition datalog_term.hpp:56
bool operator!=(const Term &other) const
Definition datalog_term.hpp:61
TermKind kind() const
Whether this term is a constant or a variable.
Definition datalog_term.hpp:47
bool is_constant() const
True iff this term is a constant.
Definition datalog_term.hpp:50
static Term make_variable(std::string name)
Constructs a variable term with the given name (e.g. "X").
Definition datalog_term.hpp:44
static Term make_constant(std::string value)
Constructs a constant term with the given value (e.g. "a").
Definition datalog_term.hpp:41
Definition datalog_atom.hpp:14
TermKind
Which of the two (and only two) term alternatives this Term is.
Definition datalog_term.hpp:15
Hash functor for Term, for use in std::unordered_map/std::unordered_set.
Definition datalog_term.hpp:71
std::size_t operator()(const Term &term) const
Definition datalog_term.hpp:72