mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
CNF.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <iostream>
5#include <mutable/mutable-config.hpp>
7#include <vector>
8
9
10namespace m {
11
12namespace cnf {
13
15struct M_EXPORT Predicate
16{
17 private:
18 uintptr_t literal_;
19
20 explicit Predicate(uintptr_t l) : literal_(l) { }
21
22 public:
24 static Predicate Positive(const ast::Expr *e) { return Predicate(reinterpret_cast<uintptr_t>(e) | 0x0UL); }
26 static Predicate Negative(const ast::Expr *e) { return Predicate(reinterpret_cast<uintptr_t>(e) | 0x1UL); }
28 static Predicate Create(const ast::Expr *e, bool is_negative) { return is_negative ? Negative(e) : Positive(e); }
29
31 bool negative() const { return literal_ & 0x1UL; }
32
34 ast::Expr & expr() { return *reinterpret_cast<ast::Expr*>(literal_ & ~0b11UL); }
36 const ast::Expr & expr() const { return *reinterpret_cast<const ast::Expr*>(literal_ & ~0b11UL); }
38 const ast::Expr & operator*() const { return expr(); }
40 const ast::Expr * operator->() const { return &expr(); }
41
43 bool is_equi() const {
44 auto binary = cast<const ast::BinaryExpr>(&expr());
45 if (not binary) return false;
46 if (not negative() and binary->tok != TK_EQUAL) return false; // `=` is ok
47 if (negative() and binary->tok != TK_BANG_EQUAL) return false; // negated `!=` is ok
48 return is<const ast::Designator>(binary->lhs) and is<const ast::Designator>(binary->rhs);
49 }
51 bool can_be_null() const { return expr().can_be_null(); }
52
55 Predicate operator!() const { return Predicate(literal_ ^ 0x1UL); }
56
59 bool operator==(Predicate other) const {
60 return this->negative() == other.negative() and this->expr() == other.expr();
61 }
64 bool operator!=(Predicate other) const { return not operator==(other); }
65
68 bool operator<(Predicate other) const { return this->literal_ < other.literal_; }
69
71 void to_sql(std::ostream &out) const;
72
74 friend std::ostream & M_EXPORT operator<<(std::ostream &out, const Predicate &pred);
75 friend M_EXPORT std::string to_string(const Predicate &pred) {
76 std::ostringstream oss;
77 oss << pred;
78 return oss.str();
79 }
80
81 void dump(std::ostream &out) const;
82 void dump() const;
83};
84
86struct M_EXPORT Clause : public std::vector<Predicate>
87{
88 using std::vector<Predicate>::vector; // c'tor
89
90 bool operator<=(const Clause &other) const;
91 bool operator>=(const Clause &other) const { return other <= *this; }
92 bool operator==(const Clause &other) const { return *this >= other and *this <= other; }
93 bool operator!=(const Clause &other) const { return not operator==(other); }
94
97 Schema required;
98 for (auto &P : *this)
99 required |= P->get_required();
100 return required;
101 }
102
105 bool is_equi() const {
106 if (size() != 1) return false;
107 auto &literal = operator[](0);
108 return literal.is_equi();
109 }
111 bool can_be_null() const {
112 for (auto &P : *this)
113 if (P.can_be_null()) return true;
114 return false;
115 }
116
118 void to_sql(std::ostream &out) const;
119
121 friend std::ostream & M_EXPORT operator<<(std::ostream &out, const Clause &clause);
122 friend M_EXPORT std::string to_string(const Clause &clause) {
123 std::ostringstream oss;
124 oss << clause;
125 return oss.str();
126 }
127
128 void dump(std::ostream &out) const;
129 void dump() const;
130};
131
133struct M_EXPORT CNF : public std::vector<Clause>
134{
135 using std::vector<Clause>::vector; // c'tor
136
139 Schema required;
140 for (auto &clause : *this)
141 required |= clause.get_required();
142 return required;
143 }
144
147 bool is_equi() const {
148 if (size() == 0)
149 return false;
150 for (auto &clause : *this)
151 if (not clause.is_equi()) return false;
152 return true;
153 }
155 bool can_be_null() const {
156 for (auto &clause : *this)
157 if (clause.can_be_null()) return true;
158 return false;
159 }
160
161 bool operator<=(const CNF &other) const;
162 bool operator>=(const CNF &other) const { return other <= *this; }
163 bool operator==(const CNF &other) const { return *this >= other and *this <= other; }
164 bool operator!=(const CNF &other) const { return not operator==(other); }
165
167 void to_sql(std::ostream &out) const;
168
170 friend M_EXPORT std::ostream & operator<<(std::ostream &out, const CNF &cnf);
171 friend M_EXPORT std::string to_string(const CNF &cnf) {
172 std::ostringstream oss;
173 oss << cnf;
174 return oss.str();
175 }
176
177 void dump(std::ostream &out) const;
178 void dump() const;
179};
180
183Clause M_EXPORT operator||(const Clause &lhs, const Clause &rhs);
184
187CNF M_EXPORT operator&&(const Clause &lhs, const Clause &rhs);
188
190CNF M_EXPORT operator&&(const CNF &lhs, const CNF &rhs);
191
194CNF M_EXPORT operator||(const CNF &lhs, const CNF &rhs);
195
198CNF M_EXPORT operator!(const Clause &clause);
199
202CNF M_EXPORT operator!(const CNF &cnf);
203
205CNF M_EXPORT to_CNF(const ast::Expr &e);
207CNF M_EXPORT get_CNF(const ast::Clause &c);
208
209}
210
211}
212
213namespace std {
214
215template<>
216struct hash<m::cnf::Predicate>
217{
218 std::size_t operator()(m::cnf::Predicate P) const {
219 return P.expr().hash() ^ (-P.negative());
220 }
221};
222
223}
and(sizeof(T)==4) U64x1 reinterpret_to_U64(m
Definition: WasmAlgo.cpp:266
CNF M_EXPORT get_CNF(const ast::Clause &c)
Converts the Boolean Expr of c to a CNF.
CNF M_EXPORT operator&&(const Clause &lhs, const Clause &rhs)
Returns the logical and of two cnf::Clauses, i.e. a CNF with the two cnf::Clauses lhs and rhs.
Definition: CNF.cpp:45
CNF M_EXPORT operator!(const Clause &clause)
Returns the logical negation of a cnf::Clause.
Definition: CNF.cpp:78
Clause M_EXPORT operator||(const Clause &lhs, const Clause &rhs)
Returns the logical or of two cnf::Clauses, i.e. the disjunction of the Predicates of lhs and rhs.
Definition: CNF.cpp:36
CNF M_EXPORT to_CNF(const ast::Expr &e)
Converts the Boolean Expr e to a CNF.
Definition: CNF.cpp:298
‍mutable namespace
Definition: Backend.hpp:10
STL namespace.
A Schema represents a sequence of identifiers, optionally with a prefix, and their associated types.
Definition: Schema.hpp:39
An expression.
Definition: AST.hpp:39
virtual uint64_t hash() const =0
Computes a hash of this, considering only syntactic properties.
A CNF represents a conjunction of cnf::Clauses.
Definition: CNF.hpp:134
Schema get_required() const
Returns a Schema instance containing all required definitions (of Attributes and other Designators).
Definition: CNF.hpp:138
bool can_be_null() const
Returns true iff this CNF formula is nullable, i.e.
Definition: CNF.hpp:155
bool operator==(const CNF &other) const
Definition: CNF.hpp:163
bool is_equi() const
Returns true iff this CNF formula is an equi-predicate, i.e.
Definition: CNF.hpp:147
friend M_EXPORT std::string to_string(const CNF &cnf)
Definition: CNF.hpp:171
bool operator>=(const CNF &other) const
Definition: CNF.hpp:162
bool operator!=(const CNF &other) const
Definition: CNF.hpp:164
friend M_EXPORT std::ostream & operator<<(std::ostream &out, const CNF &cnf)
Print a textual representation of cnf to out.
A cnf::Clause represents a disjunction of Predicates.
Definition: CNF.hpp:87
bool operator==(const Clause &other) const
Definition: CNF.hpp:92
bool operator!=(const Clause &other) const
Definition: CNF.hpp:93
friend M_EXPORT std::string to_string(const Clause &clause)
Definition: CNF.hpp:122
Schema get_required() const
Returns a Schema instance containing all required definitions (of Attributes and other Designators).
Definition: CNF.hpp:96
bool operator>=(const Clause &other) const
Definition: CNF.hpp:91
bool can_be_null() const
Returns true iff this cnf::Clause formula is nullable, i.e.
Definition: CNF.hpp:111
friend std::ostream &M_EXPORT operator<<(std::ostream &out, const Clause &clause)
Print a textual representation of clause to out.
bool is_equi() const
Returns true iff this cnf::Clause formula is an equi-predicate, i.e.
Definition: CNF.hpp:105
A Predicate contains a Expr of Boolean type in either positive or negative form.
Definition: CNF.hpp:16
bool is_equi() const
Returns true iff this Predicate is an equi-predicate, i.e.
Definition: CNF.hpp:43
const ast::Expr & expr() const
Returns the Expr within this Predicate.
Definition: CNF.hpp:36
const ast::Expr * operator->() const
Returns the Expr within this Predicate.
Definition: CNF.hpp:40
Predicate operator!() const
Returns a negated version of this Predicate, i.e. if this Predicate is positive, the returned Predica...
Definition: CNF.hpp:55
bool operator<(Predicate other) const
Compare Predicates by the location of their referenced Expr in memory and their sign.
Definition: CNF.hpp:68
const ast::Expr & operator*() const
Returns the Expr within this Predicate.
Definition: CNF.hpp:38
bool can_be_null() const
Returns true iff this Predicate is nullable, i.e.
Definition: CNF.hpp:51
uintptr_t literal_
pointer to Expr; LSB is 1 iff literal is negated
Definition: CNF.hpp:18
bool operator!=(Predicate other) const
Returns true iff other is not equal to this.
Definition: CNF.hpp:64
static Predicate Create(const ast::Expr *e, bool is_negative)
Creates a Predicate from e.
Definition: CNF.hpp:28
static Predicate Negative(const ast::Expr *e)
Creates a negative Predicate from e.
Definition: CNF.hpp:26
bool operator==(Predicate other) const
Returns true iff other is equal to this.
Definition: CNF.hpp:59
Predicate(uintptr_t l)
Definition: CNF.hpp:20
friend std::ostream &M_EXPORT operator<<(std::ostream &out, const Predicate &pred)
Print a textual representation of pred to out.
static Predicate Positive(const ast::Expr *e)
Creates a positive Predicate from e.
Definition: CNF.hpp:24
ast::Expr & expr()
Returns the Expr within this Predicate.
Definition: CNF.hpp:34
bool negative() const
Returns true iff this Predicate is negative.
Definition: CNF.hpp:31
friend M_EXPORT std::string to_string(const Predicate &pred)
Definition: CNF.hpp:75
std::size_t operator()(m::cnf::Predicate P) const
Definition: CNF.hpp:218