Connect++ 0.6.0
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.
 
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 &, const Clause &)
 

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 221 of file Clause.hpp.

221{ return c.begin(); }

◆ cbegin()

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

Definition at line 219 of file Clause.hpp.

219{ return c.cbegin(); }

◆ cend()

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

Definition at line 220 of file Clause.hpp.

220{ 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 155 of file Clause.cpp.

155 {
156 if (c.size() == 0) {
157#ifdef DEBUGMESSAGES
158 cerr << "Can't drop_literal from empty clause" << endl;
159#endif
160 return;
161 }
162 if (l > c.size() - 1) {
163#ifdef DEBUGMESSAGES
164 cerr << "drop_literal argument is out of range" << endl;
165#endif
166 return;
167 }
168 if (c.size() == 1) {
169 c.pop_back();
170 return;
171 }
172 c[l] = c.back();
173 c.pop_back();
174}

◆ 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 222 of file Clause.hpp.

222{ 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 176 of file Clause.cpp.

176 {
177 Literal error_lit;
178 if (c.size() == 0) {
179#ifdef DEBUGMESSAGES
180 cerr << "Can't extract_literal from empty clause" << endl;
181#endif
182 return error_lit;
183 }
184 if (l > c.size() - 1) {
185#ifdef DEBUGMESSAGES
186 cerr << "extract_literal argument is out of range" << endl;
187#endif
188 return error_lit;
189 }
190 Literal lit(c[l]);
191 if (c.size() == 1) {
192 c.pop_back();
193 return lit;
194 }
195 c[l] = c.back();
196 c.pop_back();
197 return lit;
198}
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 200 of file Clause.cpp.

200 {
201 Literal error_lit;
202 if (c.size() == 0) {
203#ifdef DEBUGMESSAGES
204 cerr << "Can't extract_literal from empty clause" << endl;
205#endif
206 return error_lit;
207 }
208 if (l > c.size() - 1) {
209#ifdef DEBUGMESSAGES
210 cerr << "extract_literal argument is out of range" << endl;
211#endif
212 return error_lit;
213 }
214 Literal lit(c[l]);
215 return lit;
216}

◆ 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_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 120 of file Clause.cpp.

120 {
121 size_t s = c.size();
122 unordered_map<Variable*,Term*> new_vars;
123 _l.make_copy_with_new_vars_replace_helper(vi, ti, new_vars);
124 // NOTE: make sure _c actually has room in it, unless you want
125 // horror!
126 for (size_t i = 0; i < s; i++)
127 _c.c[i].make_copy_with_new_vars_replace_helper(vi, ti, new_vars);
128}
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:58

◆ 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}
Representation of clauses.
Definition Clause.hpp:52

◆ 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 273 of file Clause.cpp.

273 {
274 commas::comma com(c.size());
275 string s ("\\textcolor{blue}{\\{} ");
276 for (const Literal& l : c) {
277 s += l.make_LaTeX(subbed);
278 s += com();
279 }
280 s += "\\textcolor{blue}{\\}}";
281 return s;
282}
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 218 of file Clause.cpp.

218 {
219#ifdef DEBUGMESSAGES
220 if (i > c.size() - 1)
221 cerr << "You're out of range accessing a clause" << endl;
222#endif
223 return c[i];
224}

◆ random_reorder()

void Clause::random_reorder ( )

Randomly re-order the literals.

Definition at line 226 of file Clause.cpp.

226 {
227 // Make a permutation.
228 vector<size_t> new_order;
229 size_t s = c.size();
230 for (size_t i = 0; i < s; i++)
231 new_order.push_back(i);
232 std::shuffle(new_order.begin(), new_order.end(), d);
233 // Re-order.
234 vector<Literal> new_c = c;
235 for (size_t i = 0; i < s; i++) {
236 c[i] = new_c[new_order[i]];
237 }
238}
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 240 of file Clause.cpp.

240 {
241 std::reverse(c.begin(), c.end());
242}

◆ simplify()

SimplificationResult Clause::simplify ( )

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

Definition at line 130 of file Clause.cpp.

130 {
131 // Do this first in case someone has made a clause with
132 // both $true and $false in it.
133 vector<Literal> new_clause;
134 bool clause_is_true = false;
135 for (Literal& lit : c) {
136 if (lit.is_false())
137 continue;
138 if (lit.is_true()) {
139 clause_is_true = true;
140 continue;
141 }
142 new_clause.push_back(lit);
143 }
144 c = new_clause;
145 if (clause_is_true)
146 return SimplificationResult::Tautology;
148 if (c.size() == 0)
149 return SimplificationResult::Contradiction;
151 return SimplificationResult::Tautology;
152 return SimplificationResult::OK;
153}
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 248 of file Clause.cpp.

248 {
249 commas::comma com(c.size());
250 string result("[ ");
251 for (const Literal& l : c) {
252 result += l.to_prolog_string();
253 result += com();
254 }
255 result += " ]";
256 return result;
257}

◆ to_string()

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

Convert to a string.

Parameters
subbedImplement substitutions if true.

Definition at line 244 of file Clause.cpp.

244 {
245 return vector_to_string(c, subbed, "{ ", " }", ", ");
246}

◆ 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 259 of file Clause.cpp.

259 {
260 commas::comma com(c.size());
261 string result("( ");
262 for (const Literal& l : c) {
263 result += l.to_tptp_string();
264 string s = com();
265 if (s == string(", "))
266 s = string(" | ");
267 result += s;
268 }
269 result += " )";
270 return result;
271}

Friends And Related Symbol Documentation

◆ operator<<

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

Definition at line 284 of file Clause.cpp.

284 {
285 out << "{ ";
286 size_t s = cl.c.size();
287 size_t i = 1;
288 for (const Literal& l : cl.c) {
289 out << l.to_string();
290 if (i < s)
291 out << " " << unicode_symbols::LogSym::or_sym << " ";
292 i++;
293 }
294 out << " }";
295 return out;
296}

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: