Connect++ 0.4.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.
 
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.
 
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.
 
SimplificationResult simplify ()
 Simplify a clause by dealing with $true, $false, complementary literals, and duplicates.
 
void random_reorder ()
 Randomly re-order the literals.
 
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 87 of file Clause.cpp.

87 {
88 for (size_t i = 0; i < c.size(); i++)
89 if (c[i] == l) {
90#ifdef DEBUGMESSAGES
91 cerr << "Don't duplicate Literals in a clause!" << endl;
92#endif
93 return;
94 }
95 c.push_back(l);
96}

◆ begin()

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

Definition at line 192 of file Clause.hpp.

192{ return c.begin(); }

◆ cbegin()

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

Definition at line 190 of file Clause.hpp.

190{ return c.cbegin(); }

◆ cend()

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

Definition at line 191 of file Clause.hpp.

191{ 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 135 of file Clause.cpp.

135 {
136 if (c.size() == 0) {
137#ifdef DEBUGMESSAGES
138 cerr << "Can't drop_literal from empty clause" << endl;
139#endif
140 return;
141 }
142 if (l > c.size() - 1) {
143#ifdef DEBUGMESSAGES
144 cerr << "drop_literal argument is out of range" << endl;
145#endif
146 return;
147 }
148 if (c.size() == 1) {
149 c.pop_back();
150 return;
151 }
152 c[l] = c.back();
153 c.pop_back();
154}

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

193{ 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 156 of file Clause.cpp.

156 {
157 Literal error_lit;
158 if (c.size() == 0) {
159#ifdef DEBUGMESSAGES
160 cerr << "Can't extract_literal from empty clause" << endl;
161#endif
162 return error_lit;
163 }
164 if (l > c.size() - 1) {
165#ifdef DEBUGMESSAGES
166 cerr << "extract_literal argument is out of range" << endl;
167#endif
168 return error_lit;
169 }
170 Literal lit(c[l]);
171 if (c.size() == 1) {
172 c.pop_back();
173 return lit;
174 }
175 c[l] = c.back();
176 c.pop_back();
177 return lit;
178}
Basic representation of literals, bundling together (pointers to) a Predicate, a collection of argume...
Definition Literal.hpp:50

◆ 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_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()

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

98 {
99#ifdef DEBUGMESSAGES
100 if (c.size() == 0)
101 cerr << "You are making a copy of an empty clause" << endl;
102#endif
103 vector<Literal> new_lits;
104 unordered_map<Variable*,Term*> new_vars;
105 for (size_t i = 0; i < c.size(); i++)
106 new_lits.push_back(c[i].make_copy_with_new_vars_helper(vi, ti, new_vars));
107 return Clause(new_lits);
108}
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 238 of file Clause.cpp.

238 {
239 commas::comma com(c.size());
240 string s ("\\textcolor{blue}{\\{} ");
241 for (const Literal& l : c) {
242 s += l.make_LaTeX(subbed);
243 s += com();
244 }
245 s += "\\textcolor{blue}{\\}}";
246 return s;
247}
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 180 of file Clause.cpp.

180 {
181#ifdef DEBUGMESSAGES
182 if (i > c.size() - 1)
183 cerr << "You're out of range accessing a clause" << endl;
184#endif
185 return c[i];
186}

◆ random_reorder()

void Clause::random_reorder ( )

Randomly re-order the literals.

Definition at line 188 of file Clause.cpp.

188 {
189 // Make a permutation.
190 vector<size_t> new_order;
191 size_t s = c.size();
192 for (size_t i = 0; i < s; i++)
193 new_order.push_back(i);
194 std::shuffle(new_order.begin(), new_order.end(), d);
195 // Re-order.
196 vector<Literal> new_c = c;
197 for (size_t i = 0; i < s; i++) {
198 c[i] = new_c[new_order[i]];
199 }
200}
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 }

◆ simplify()

SimplificationResult Clause::simplify ( )

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

Definition at line 110 of file Clause.cpp.

110 {
111 // Do this first in case someone has made a clause with
112 // both $true and $false in it.
113 vector<Literal> new_clause;
114 bool clause_is_true = false;
115 for (Literal& lit : c) {
116 if (lit.is_false())
117 continue;
118 if (lit.is_true()) {
119 clause_is_true = true;
120 continue;
121 }
122 new_clause.push_back(lit);
123 }
124 c = new_clause;
125 if (clause_is_true)
126 return SimplificationResult::Tautology;
128 if (c.size() == 0)
129 return SimplificationResult::Contradiction;
131 return SimplificationResult::Tautology;
132 return SimplificationResult::OK;
133}
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 213 of file Clause.cpp.

213 {
214 commas::comma com(c.size());
215 string result("[ ");
216 for (const Literal& l : c) {
217 result += l.to_prolog_string();
218 result += com();
219 }
220 result += " ]";
221 return result;
222}

◆ to_string()

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

Convert to a string.

Parameters
subbedImplement substitutions if true.

Definition at line 202 of file Clause.cpp.

202 {
203 commas::comma com(c.size());
204 string result("{ ");
205 for (const Literal& l : c) {
206 result += l.to_string(subbed);
207 result += com();
208 }
209 result += " }";
210 return result;
211}

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

224 {
225 commas::comma com(c.size());
226 string result("( ");
227 for (const Literal& l : c) {
228 result += l.to_tptp_string();
229 string s = com();
230 if (s == string(", "))
231 s = string(" | ");
232 result += s;
233 }
234 result += " )";
235 return result;
236}

Friends And Related Symbol Documentation

◆ operator<<

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

Definition at line 249 of file Clause.cpp.

249 {
250 out << "{ ";
251 size_t s = cl.c.size();
252 size_t i = 1;
253 for (const Literal& l : cl.c) {
254 out << l.to_string();
255 if (i < s)
256 out << " " << unicode_symbols::LogSym::or_sym << " ";
257 i++;
258 }
259 out << " }";
260 return out;
261}

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: