Connect++ 0.6.1
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
Clause Class Reference

Representation of clauses. More...

#include <Clause.hpp>

Collaboration diagram for Clause:

Public Member Functions

 Clause (const vector< Literal > &new_lits)
 Construction is just taking a new vector of Literals.
 
size_t size () const
 Straightforward get method.
 
bool empty () const
 Straightforward get method.
 
void clear ()
 Straightforward reset method.
 
bool is_positive () const
 A clause is positive if it has no negated literals.
 
bool is_negative () const
 A clause is negative if it has only negated literals.
 
void remove_duplicates ()
 Get rid of identical literals.
 
bool has_complementary_literals () const
 Check whether the clause contains complementary literals.
 
bool is_ground () const
 Is this a ground clause?
 
void add_lit (const Literal &)
 Add a literal, making sure you don't duplicate.
 
void drop_literal (LitNum)
 Get rid of the specified Literal.
 
Literal extract_literal (LitNum)
 Get rid of and return the specified Literal.
 
Literal get_literal (LitNum) const
 Get and return the specified Literal.
 
Literaloperator[] (size_t)
 Direct read of the specified Literal.
 
Clause make_copy_with_new_vars (VariableIndex &, TermIndex &) const
 Make a copy of an entire clause, introducing new variables.
 
Clause make_copy_with_new_unique_vars (VariableIndex &, TermIndex &) const
 Make a copy of an entire clause, introducing new variables.
 
void make_copy_with_new_vars (Literal &, Clause &, VariableIndex &, TermIndex &)
 Make a copy of an entire clause and literal, introducing new variables.
 
SimplificationResult simplify ()
 Simplify a clause by dealing with $true, $false, complementary literals, and duplicates.
 
void random_reorder ()
 Randomly re-order the literals.
 
void reverse ()
 Reverse the order of the literals in the clause.
 
string to_string (bool=false) const
 Convert to a string.
 
string to_prolog_string () const
 Convert to a string that can be read by Prolog.
 
string to_tptp_string () const
 Convert to a string that is compatible with the TPTP.
 
string make_LaTeX (bool=false) const
 Convert the clause to a LaTeX representation.
 
vector< Literal >::const_iterator cbegin () const
 
vector< Literal >::const_iterator cend () const
 
vector< Literal >::iterator begin ()
 
vector< Literal >::iterator end ()
 

Private Attributes

vector< Literalc
 A Clause is just a vector of Literals.
 

Static Private Attributes

static std::mt19937 d
 Randomness for random reordering.
 

Friends

ostream & operator<< (ostream &out, const Clause &cl)
 

Detailed Description

Representation of clauses.

Essentially straightforward as they are just vectors of Literals. Other classes do all the heavy lifting.

Definition at line 52 of file Clause.hpp.

Constructor & Destructor Documentation

◆ Clause() [1/2]

Clause::Clause ( )
inline

Definition at line 63 of file Clause.hpp.

63: c() {}
vector< Literal > c
A Clause is just a vector of Literals.
Definition Clause.hpp:57

◆ Clause() [2/2]

Clause::Clause ( const vector< Literal > & new_lits)
inline

Construction is just taking a new vector of Literals.

Parameters
new_litsReference to new vector of Literals.

Definition at line 69 of file Clause.hpp.

69 : c(new_lits) {
70 #ifdef DEBUGMESSAGES
71 if (new_lits.size() == 0)
72 cerr << "Why are you constructing an empty clause?" << endl;
73 #endif
74 }

Member Function Documentation

◆ add_lit()

void Clause::add_lit ( const Literal & l)

Add a literal, making sure you don't duplicate.

Yes, yes a set<> would be a good idea, but vectors are easy, nicer if you want indexes amd nicer for the cache.

Parameters
lReference to Literal to add

Definition at line 96 of file Clause.cpp.

96 {
97 for (size_t i = 0; i < c.size(); i++)
98 if (c[i] == l) {
99#ifdef DEBUGMESSAGES
100 cerr << "Don't duplicate Literals in a clause!" << endl;
101#endif
102 return;
103 }
104 c.push_back(l);
105}

◆ begin()

vector< Literal >::iterator Clause::begin ( )
inline

Definition at line 236 of file Clause.hpp.

236{ return c.begin(); }

◆ cbegin()

vector< Literal >::const_iterator Clause::cbegin ( ) const
inline

Definition at line 234 of file Clause.hpp.

234{ return c.cbegin(); }

◆ cend()

vector< Literal >::const_iterator Clause::cend ( ) const
inline

Definition at line 235 of file Clause.hpp.

235{ return c.cend(); }

◆ clear()

void Clause::clear ( )
inline

Straightforward reset method.

Definition at line 86 of file Clause.hpp.

86{ c.clear(); }

◆ drop_literal()

void Clause::drop_literal ( LitNum l)

Get rid of the specified Literal.

Parameters
lIndex of literal to remove.

Definition at line 168 of file Clause.cpp.

168 {
169 if (c.size() == 0) {
170#ifdef DEBUGMESSAGES
171 cerr << "Can't drop_literal from empty clause" << endl;
172#endif
173 return;
174 }
175 if (l > c.size() - 1) {
176#ifdef DEBUGMESSAGES
177 cerr << "drop_literal argument is out of range" << endl;
178#endif
179 return;
180 }
181 if (c.size() == 1) {
182 c.pop_back();
183 return;
184 }
185 c[l] = c.back();
186 c.pop_back();
187}

◆ empty()

bool Clause::empty ( ) const
inline

Straightforward get method.

Definition at line 82 of file Clause.hpp.

82{ return c.empty(); }

◆ end()

vector< Literal >::iterator Clause::end ( )
inline

Definition at line 237 of file Clause.hpp.

237{ return c.end(); }

◆ extract_literal()

Literal Clause::extract_literal ( LitNum l)

Get rid of and return the specified Literal.

Parameters
lIndex of literal to remove.

Definition at line 189 of file Clause.cpp.

189 {
190 Literal error_lit;
191 if (c.size() == 0) {
192#ifdef DEBUGMESSAGES
193 cerr << "Can't extract_literal from empty clause" << endl;
194#endif
195 return error_lit;
196 }
197 if (l > c.size() - 1) {
198#ifdef DEBUGMESSAGES
199 cerr << "extract_literal argument is out of range" << endl;
200#endif
201 return error_lit;
202 }
203 Literal lit(c[l]);
204 if (c.size() == 1) {
205 c.pop_back();
206 return lit;
207 }
208 c[l] = c.back();
209 c.pop_back();
210 return lit;
211}
Basic representation of literals, bundling together (pointers to) a Predicate, a collection of argume...
Definition Literal.hpp:50

◆ get_literal()

Literal Clause::get_literal ( LitNum l) const

Get and return the specified Literal.

Parameters
lIndex of literal to return.

Definition at line 213 of file Clause.cpp.

213 {
214 Literal error_lit;
215 if (c.size() == 0) {
216#ifdef DEBUGMESSAGES
217 cerr << "Can't extract_literal from empty clause" << endl;
218#endif
219 return error_lit;
220 }
221 if (l > c.size() - 1) {
222#ifdef DEBUGMESSAGES
223 cerr << "extract_literal argument is out of range" << endl;
224#endif
225 return error_lit;
226 }
227 Literal lit(c[l]);
228 return lit;
229}

◆ has_complementary_literals()

bool Clause::has_complementary_literals ( ) const

Check whether the clause contains complementary literals.

This does not take account of substitutions as it is mainly used for simplification before any substitutions happen.

Definition at line 74 of file Clause.cpp.

74 {
75 size_t s = c.size();
76 if (s < 2)
77 return false;
78 for (size_t i = 0; i < s - 1; i++) {
79 for (size_t j = i+1; j < s; j++) {
80 if (c[i].is_complement_of(c[j]))
81 return true;
82 }
83 }
84 return false;
85}

◆ is_ground()

bool Clause::is_ground ( ) const

Is this a ground clause?

Do not attempt to follow substitutions.

Definition at line 87 of file Clause.cpp.

87 {
88 for (const Literal& l : c) {
89 if (!l.is_ground()) {
90 return false;
91 }
92 }
93 return true;
94}

◆ is_negative()

bool Clause::is_negative ( ) const

A clause is negative if it has only negated literals.

Definition at line 42 of file Clause.cpp.

42 {
43#ifdef DEBUGMESSAGES
44 if (c.size() == 0)
45 cerr << "Don't check an empty clause for negativity" << endl;
46#endif
47 for (size_t i = 0; i < c.size(); i++) {
48 if (c[i].is_positive())
49 return false;
50 }
51 return true;
52}
bool is_positive() const
A clause is positive if it has no negated literals.
Definition Clause.cpp:30

◆ is_positive()

bool Clause::is_positive ( ) const

A clause is positive if it has no negated literals.

Definition at line 30 of file Clause.cpp.

30 {
31#ifdef DEBUGMESSAGES
32 if (c.size() == 0)
33 cerr << "Don't check an empty clause for positivity" << endl;
34#endif
35 for (size_t i = 0; i < c.size(); i++) {
36 if (c[i].is_negative())
37 return false;
38 }
39 return true;
40}
bool is_negative() const
A clause is negative if it has only negated literals.
Definition Clause.cpp:42

◆ make_copy_with_new_unique_vars()

Clause Clause::make_copy_with_new_unique_vars ( VariableIndex & vi,
TermIndex & ti ) const

Make a copy of an entire clause, introducing new variables.

Straightforward because the heavy lifting is done elsewhere.

This version uses unique variables, that do not get recycled when backtracking, and are thus suitable for caching copies of clauses.

Parameters
viReference to the VariableIndex
tiReference to the TermIndex

Definition at line 120 of file Clause.cpp.

120 {
121#ifdef DEBUGMESSAGES
122 if (c.size() == 0)
123 cerr << "You are making a copy of an empty clause" << endl;
124#endif
125 size_t s = c.size();
126 vector<Literal> new_lits;
127 unordered_map<Variable*,Term*> new_vars;
128 for (size_t i = 0; i < s; i++)
129 new_lits.push_back(c[i].make_copy_with_new_unique_vars_helper(vi, ti, new_vars));
130 return Clause(new_lits);
131}
Representation of clauses.
Definition Clause.hpp:52

◆ make_copy_with_new_vars() [1/2]

void Clause::make_copy_with_new_vars ( Literal & _l,
Clause & _c,
VariableIndex & vi,
TermIndex & ti )

Make a copy of an entire clause and literal, introducing new variables.

Straightforward because the heavy lifting is done elsewhere.

Parameters
_lNew literal.
_cNew clause,
viReference to the VariableIndex
tiReference to the TermIndex

Definition at line 133 of file Clause.cpp.

133 {
134 size_t s = c.size();
135 unordered_map<Variable*,Term*> new_vars;
136 _l.make_copy_with_new_vars_replace_helper(vi, ti, new_vars);
137 // NOTE: make sure _c actually has room in it, unless you want
138 // horror!
139 for (size_t i = 0; i < s; i++)
140 _c.c[i].make_copy_with_new_vars_replace_helper(vi, ti, new_vars);
141}
void make_copy_with_new_vars_replace_helper(VariableIndex &, TermIndex &, unordered_map< Variable *, Term * > &)
Helper function for making copies with new variables.
Definition Literal.cpp:68

◆ make_copy_with_new_vars() [2/2]

Clause Clause::make_copy_with_new_vars ( VariableIndex & vi,
TermIndex & ti ) const

Make a copy of an entire clause, introducing new variables.

Straightforward because the heavy lifting is done elsewhere.

Parameters
viReference to the VariableIndex
tiReference to the TermIndex

Definition at line 107 of file Clause.cpp.

107 {
108#ifdef DEBUGMESSAGES
109 if (c.size() == 0)
110 cerr << "You are making a copy of an empty clause" << endl;
111#endif
112 size_t s = c.size();
113 vector<Literal> new_lits;
114 unordered_map<Variable*,Term*> new_vars;
115 for (size_t i = 0; i < s; i++)
116 new_lits.push_back(c[i].make_copy_with_new_vars_helper(vi, ti, new_vars));
117 return Clause(new_lits);
118}

◆ make_LaTeX()

string Clause::make_LaTeX ( bool subbed = false) const

Convert the clause to a LaTeX representation.

Assumes you are in Math Mode.

Parameters
subbedimplement substitution if true.

Definition at line 286 of file Clause.cpp.

286 {
287 commas::comma com(c.size());
288 string s ("\\textcolor{blue}{\\{} ");
289 for (const Literal& l : c) {
290 s += l.make_LaTeX(subbed);
291 s += com();
292 }
293 s += "\\textcolor{blue}{\\}}";
294 return s;
295}
Simple function object for putting commas in lists.

◆ operator[]()

Literal & Clause::operator[] ( size_t i)

Direct read of the specified Literal.

There is no check on whether you've given a sensible index, so behave yourself!

Parameters
iIndex of required Literal

Definition at line 231 of file Clause.cpp.

231 {
232#ifdef DEBUGMESSAGES
233 if (i > c.size() - 1)
234 cerr << "You're out of range accessing a clause" << endl;
235#endif
236 return c[i];
237}

◆ random_reorder()

void Clause::random_reorder ( )

Randomly re-order the literals.

Definition at line 239 of file Clause.cpp.

239 {
240 // Make a permutation.
241 vector<size_t> new_order;
242 size_t s = c.size();
243 for (size_t i = 0; i < s; i++)
244 new_order.push_back(i);
245 std::shuffle(new_order.begin(), new_order.end(), d);
246 // Re-order.
247 vector<Literal> new_c = c;
248 for (size_t i = 0; i < s; i++) {
249 c[i] = new_c[new_order[i]];
250 }
251}
static std::mt19937 d
Randomness for random reordering.
Definition Clause.hpp:61

◆ remove_duplicates()

void Clause::remove_duplicates ( )

Get rid of identical literals.

This does not take account of substitutions as it is mainly used for simplification before any substitutions happen.

Definition at line 54 of file Clause.cpp.

54 {
55 size_t s = c.size();
56 if (s < 2)
57 return;
58 vector<Literal> new_c;
59 for (size_t i = 0; i < s - 1; i++) {
60 bool found = false;
61 for (size_t j = i+1; j < s; j++ ) {
62 if (c[i] == c[j]) {
63 found = true;
64 break;
65 }
66 }
67 if (!found)
68 new_c.push_back(c[i]);
69 }
70 new_c.push_back(c[s - 1]);
71 c = new_c;
72 }

◆ reverse()

void Clause::reverse ( )

Reverse the order of the literals in the clause.

Definition at line 253 of file Clause.cpp.

253 {
254 std::reverse(c.begin(), c.end());
255}

◆ simplify()

SimplificationResult Clause::simplify ( )

Simplify a clause by dealing with $true, $false, complementary literals, and duplicates.

Definition at line 143 of file Clause.cpp.

143 {
144 // Do this first in case someone has made a clause with
145 // both $true and $false in it.
146 vector<Literal> new_clause;
147 bool clause_is_true = false;
148 for (Literal& lit : c) {
149 if (lit.is_false())
150 continue;
151 if (lit.is_true()) {
152 clause_is_true = true;
153 continue;
154 }
155 new_clause.push_back(lit);
156 }
157 c = new_clause;
158 if (clause_is_true)
159 return SimplificationResult::Tautology;
161 if (c.size() == 0)
162 return SimplificationResult::Contradiction;
164 return SimplificationResult::Tautology;
165 return SimplificationResult::OK;
166}
bool has_complementary_literals() const
Check whether the clause contains complementary literals.
Definition Clause.cpp:74
void remove_duplicates()
Get rid of identical literals.
Definition Clause.cpp:54

◆ size()

size_t Clause::size ( ) const
inline

Straightforward get method.

Definition at line 78 of file Clause.hpp.

78{ return c.size(); }

◆ to_prolog_string()

string Clause::to_prolog_string ( ) const

Convert to a string that can be read by Prolog.

Does not apply substitutions.

Definition at line 261 of file Clause.cpp.

261 {
262 commas::comma com(c.size());
263 string result("[ ");
264 for (const Literal& l : c) {
265 result += l.to_prolog_string();
266 result += com();
267 }
268 result += " ]";
269 return result;
270}

◆ to_string()

string Clause::to_string ( bool subbed = false) const

Convert to a string.

Parameters
subbedImplement substitutions if true.

Definition at line 257 of file Clause.cpp.

257 {
258 return vector_to_string(c, subbed, "{ ", " }", ", ");
259}

◆ to_tptp_string()

string Clause::to_tptp_string ( ) const

Convert to a string that is compatible with the TPTP.

Does not apply substitutions.

Definition at line 272 of file Clause.cpp.

272 {
273 commas::comma com(c.size());
274 string result("( ");
275 for (const Literal& l : c) {
276 result += l.to_tptp_string();
277 string s = com();
278 if (s == string(", "))
279 s = string(" | ");
280 result += s;
281 }
282 result += " )";
283 return result;
284}

Friends And Related Symbol Documentation

◆ operator<<

ostream & operator<< ( ostream & out,
const Clause & cl )
friend

Definition at line 297 of file Clause.cpp.

297 {
298 out << "{ ";
299 size_t s = cl.c.size();
300 size_t i = 1;
301 for (const Literal& l : cl.c) {
302 out << l.to_string();
303 if (i < s)
304 out << " " << unicode_symbols::LogSym::or_sym << " ";
305 i++;
306 }
307 out << " }";
308 return out;
309}
string to_string(bool=false) const
Full conversion of Literal to string.
Definition Literal.cpp:176

Member Data Documentation

◆ c

vector<Literal> Clause::c
private

A Clause is just a vector of Literals.

Definition at line 57 of file Clause.hpp.

◆ d

std::mt19937 Clause::d
staticprivate

Randomness for random reordering.

Definition at line 61 of file Clause.hpp.


The documentation for this class was generated from the following files: