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

Basic representation of literals, bundling together (pointers to) a Predicate, a collection of arguments each a (pointer to a) Term, an arity and a polarity. More...

#include <Literal.hpp>

Collaboration diagram for Literal:

Public Member Functions

 Literal (Predicate *, const vector< Term * > &, Arity, bool)
 Construct a Literal from existing material.
 
Predicateget_pred () const
 Basic get method.
 
Arity get_arity () const
 Basic get method.
 
bool get_polarity () const
 Basic get method.
 
const vector< Term * > & get_args () const
 Basic get method.
 
bool is_positive () const
 Basic manipulation - entirely self-explanatory.
 
bool is_negative () const
 Basic manipulation - entirely self-explanatory.
 
void make_positive ()
 Basic manipulation - entirely self-explanatory.
 
void make_negative ()
 Basic manipulation - entirely self-explanatory.
 
void invert ()
 Basic manipulation - entirely self-explanatory.
 
void make_empty ()
 Basic manipulation - entirely self-explanatory.
 
bool is_empty () const
 Basic manipulation - entirely self-explanatory.
 
void clear ()
 Basic manipulation - entirely self-explanatory.
 
size_t get_pred_as_index () const
 Turn a Literal into an array index.
 
bool is_true () const
 Is this equivalent to $true?
 
bool is_false () const
 Is this equivalent to $false?
 
bool contains_variable (Variable *) const
 Does this Literal contain the specified variable?
 
bool is_compatible_with (const Literal *) const
 Literals can only be unified if the polarity and actual predicate are the same.
 
bool is_complement_of (const Literal &) const
 Test whether one literal is exactly the same as another, with the exception of the polarity.
 
string get_small_lit () const
 Get the predicate and polarity as a string.
 
string to_string (bool=false) const
 Full conversion of Literal to string.
 
string to_prolog_string () const
 Convert to a string in a format readable by Prolog.
 
string to_tptp_string () const
 Convert to a string in a format compatible with the TPTP.
 
string make_LaTeX (bool=false) const
 Make a useable LaTeX version.
 
bool operator== (const Literal &) const
 Equality without taking account of substutions.
 
bool subbed_equal (Literal *) const
 Equality, taking account of substitutions.
 
Literal make_copy_with_new_vars (VariableIndex &, TermIndex &) const
 Make a copy of the Literal but with a new set of variables.
 
Literal make_copy_with_new_vars_helper (VariableIndex &, TermIndex &, unordered_map< Variable *, Term * > &) const
 Helper function for make_copy_with_new_vars.
 
void replace_variable (Variable *, Variable *, TermIndex *)
 Replace one variable with another throughout.
 
void replace_variable_with_term (Term *, Variable *, TermIndex *)
 Replace one variable with a term throughout.
 

Private Attributes

Predicatepred
 
vector< Term * > args
 
Arity arity
 
bool polarity
 

Friends

ostream & operator<< (ostream &, const Literal &)
 

Detailed Description

Basic representation of literals, bundling together (pointers to) a Predicate, a collection of arguments each a (pointer to a) Term, an arity and a polarity.

Definition at line 50 of file Literal.hpp.

Constructor & Destructor Documentation

◆ Literal() [1/2]

Literal::Literal ( )
inline

Definition at line 57 of file Literal.hpp.

57: pred(nullptr), args(), arity(0), polarity(true) {}

◆ Literal() [2/2]

Literal::Literal ( Predicate * new_p,
const vector< Term * > & new_args,
Arity new_ar,
bool new_pol )

Construct a Literal from existing material.

Parameters
new_pPointer to a Predicate
new_argsReference to a vector of pointers to Terms
new_arNew value for arity
new_polSet true for positive literal, false for negative (negated).

Definition at line 28 of file Literal.cpp.

32: pred(new_p)
33, args(new_args)
34, arity(new_ar)
35, polarity(new_pol)
36{
37 #ifdef DEBUGMESSAGES
38 if (new_p == nullptr) {
39 cerr << "Why would you construct an empty predicate?" << endl;
40 }
41 #endif
42 if (new_p->get_arity() != new_args.size()) {
43 cerr << "Number of arguments doesn't match predicate arity" << endl;
44 cerr << "Are you mixing up your predicate names?" << endl;
45 }
46}
Arity get_arity() const
Basic get method.
Definition Predicate.hpp:90

Member Function Documentation

◆ clear()

void Literal::clear ( )
inline

Basic manipulation - entirely self-explanatory.

Definition at line 115 of file Literal.hpp.

115{ pred = nullptr; args.clear(), arity = 0; polarity = true; }

◆ contains_variable()

bool Literal::contains_variable ( Variable * v) const

Does this Literal contain the specified variable?

Parameters
vPointer to Variable to search for.

Definition at line 105 of file Literal.cpp.

105 {
106 for (size_t i = 0; i < args.size(); i++) {
107 // Note that this takes account of substitutions.
108 if (args[i]->contains_variable(v))
109 return true;
110 }
111 return false;
112}
bool contains_variable(Variable *) const
Does this Literal contain the specified variable?
Definition Literal.cpp:105

◆ get_args()

const vector< Term * > & Literal::get_args ( ) const
inline

Basic get method.

Definition at line 83 of file Literal.hpp.

83{ return args; }

◆ get_arity()

Arity Literal::get_arity ( ) const
inline

Basic get method.

Definition at line 75 of file Literal.hpp.

75{ return arity; }

◆ get_polarity()

bool Literal::get_polarity ( ) const
inline

Basic get method.

Definition at line 79 of file Literal.hpp.

79{ return polarity; }

◆ get_pred()

Predicate * Literal::get_pred ( ) const
inline

Basic get method.

Definition at line 71 of file Literal.hpp.

71{ return pred; }

◆ get_pred_as_index()

size_t Literal::get_pred_as_index ( ) const
inline

Turn a Literal into an array index.

This is straightforward: positive literals have even indices and negative literals odd.

Definition at line 122 of file Literal.hpp.

122 {
123 size_t index = pred->get_ID() << 1;
124 if (!polarity)
125 index |= 0x00000001;
126 return index;
127 };
ID get_ID() const
Basic get method.
Definition Predicate.hpp:82

◆ get_small_lit()

string Literal::get_small_lit ( ) const

Get the predicate and polarity as a string.

Definition at line 142 of file Literal.cpp.

142 {
143 string result;
144 if (!polarity)
145 result += unicode_symbols::LogSym::neg;
146 result += pred->to_string();
147 return result;
148}
string to_string() const
Converting to a string just gives you the name.
Definition Predicate.cpp:27

◆ invert()

void Literal::invert ( )
inline

Basic manipulation - entirely self-explanatory.

Definition at line 103 of file Literal.hpp.

103{ polarity = !polarity; }

◆ is_compatible_with()

bool Literal::is_compatible_with ( const Literal * l) const

Literals can only be unified if the polarity and actual predicate are the same.

One does not generally expect literals to be differentiated by arity, but it's possible so we check that too.

Parameters
lPointer to Literal to compare with.

Definition at line 67 of file Literal.cpp.

67 {
68 #ifdef DEBUGMESSAGES
69 if (pred == nullptr || l->pred == nullptr) {
70 cerr << "You shouldn't be checking compatibility of empty Literals" << endl;
71 }
72 #endif
73 return (pred == l->pred &&
74 polarity == l->polarity &&
75 arity == l->arity);
76}

◆ is_complement_of()

bool Literal::is_complement_of ( const Literal & l) const

Test whether one literal is exactly the same as another, with the exception of the polarity.

This also requires that the argument lists are the same.

Parameters
lLiteral to compare with.

Definition at line 78 of file Literal.cpp.

78 {
79 return (pred == l.pred &&
80 args == l.args &&
81 arity == l.arity &&
82 !polarity == l.polarity);
83}

◆ is_empty()

bool Literal::is_empty ( ) const
inline

Basic manipulation - entirely self-explanatory.

Definition at line 111 of file Literal.hpp.

111{ return (pred == nullptr); }

◆ is_false()

bool Literal::is_false ( ) const

Is this equivalent to $false?

Definition at line 95 of file Literal.cpp.

95 {
96 return ((polarity &&
97 (pred->get_name() == string("$false")) &&
98 (pred->get_arity() == 0))
99 ||
100 (!polarity &&
101 (pred->get_name() == string("$true")) &&
102 (pred->get_arity() == 0)));
103}
string get_name() const
Basic get method.
Definition Predicate.hpp:86

◆ is_negative()

bool Literal::is_negative ( ) const
inline

Basic manipulation - entirely self-explanatory.

Definition at line 91 of file Literal.hpp.

91{ return !polarity; }

◆ is_positive()

bool Literal::is_positive ( ) const
inline

Basic manipulation - entirely self-explanatory.

Definition at line 87 of file Literal.hpp.

87{ return polarity; }

◆ is_true()

bool Literal::is_true ( ) const

Is this equivalent to $true?

Definition at line 85 of file Literal.cpp.

85 {
86 return ((polarity &&
87 (pred->get_name() == string("$true")) &&
88 (pred->get_arity() == 0))
89 ||
90 (!polarity &&
91 (pred->get_name() == string("$false")) &&
92 (pred->get_arity() == 0)));
93}

◆ make_copy_with_new_vars()

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

Make a copy of the Literal but with a new set of variables.

All the heavy lifting is done by the Term class. Essentially just initializes an unordered_map and calls the corresponging helper.

Parameters
viReference to the VariableIndex
tiReference to the TermIndex

Definition at line 58 of file Literal.cpp.

58 {
59 #ifdef DEBUGMESSAGES
60 if (pred == nullptr)
61 cerr << "You shouldn't be making copies of empty Literals" << endl;
62 #endif
63 unordered_map<Variable*,Term*> new_vars;
64 return make_copy_with_new_vars_helper(vi, ti, new_vars);
65}
Literal make_copy_with_new_vars_helper(VariableIndex &, TermIndex &, unordered_map< Variable *, Term * > &) const
Helper function for make_copy_with_new_vars.
Definition Literal.cpp:48

◆ make_copy_with_new_vars_helper()

Literal Literal::make_copy_with_new_vars_helper ( VariableIndex & vi,
TermIndex & ti,
unordered_map< Variable *, Term * > & new_vars ) const

Helper function for make_copy_with_new_vars.

Iterates over the args using the corresponding method in the Term class to do the real work.

Parameters
viReference to the VariableIndex
tiReference to the TermIndex
new_varsReference to an unordered_map from pointers to Variables, to pointers to Terms. Initially empty. On completion contains a map from variables, to the terms corresponding to the new variables made to replace them.

Definition at line 48 of file Literal.cpp.

50 {
51 vector<Term*> new_args;
52 for (size_t i = 0; i < arity; i++)
53 new_args.push_back(args[i]->make_copy_with_new_vars_helper(vi, ti, new_vars));
54 Literal result(pred, new_args, arity, polarity);
55 return result;
56}
Basic representation of literals, bundling together (pointers to) a Predicate, a collection of argume...
Definition Literal.hpp:50

◆ make_empty()

void Literal::make_empty ( )
inline

Basic manipulation - entirely self-explanatory.

Definition at line 107 of file Literal.hpp.

107{ pred = nullptr; }

◆ make_LaTeX()

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

Make a useable LaTeX version.

Assumes you are typesetting in Math Mode.

Parameters
subbedImplement substitutions if true.

Definition at line 209 of file Literal.cpp.

209 {
210 string s;
211 if (!polarity)
212 s += "\\neg ";
213 s += pred->make_LaTeX();
214 if (arity > 0) {
215 s += "(";
216 commas::comma com(arity);
217 for (Term* p : args) {
218 s += p->make_LaTeX(subbed);
219 s += com();
220 }
221 s += ")";
222 }
223 return s;
224}
string make_LaTeX() const
Make a useable LaTeX version.
Definition Predicate.cpp:32
General representation of terms.
Definition Term.hpp:62
Simple function object for putting commas in lists.

◆ make_negative()

void Literal::make_negative ( )
inline

Basic manipulation - entirely self-explanatory.

Definition at line 99 of file Literal.hpp.

99{ polarity = false; }

◆ make_positive()

void Literal::make_positive ( )
inline

Basic manipulation - entirely self-explanatory.

Definition at line 95 of file Literal.hpp.

95{ polarity = true; }

◆ operator==()

bool Literal::operator== ( const Literal & l) const

Equality without taking account of substutions.

Parameters
lLiteral to compare with.

Definition at line 114 of file Literal.cpp.

114 {
115 #ifdef DEBUGMESSAGES
116 if (pred == nullptr || l.pred == nullptr) {
117 cerr << "You shouldn't be checking equality of empty Literals" << endl;
118 }
119 #endif
120 return (pred == l.pred &&
121 args == l.args &&
122 arity == l.arity &&
123 polarity == l.polarity);
124}

◆ replace_variable()

void Literal::replace_variable ( Variable * new_var,
Variable * old_var,
TermIndex * ti )

Replace one variable with another throughout.

Iterate over the args using the replace_variable_in_term method in TermIndex to do the real work.

Parameters
new_varPointer to new Variable
old_varPointer to old Variable
tiPointer to the TermIndex

Definition at line 226 of file Literal.cpp.

227 {
228#ifdef DEBUGMESSAGES
229 if (pred == nullptr)
230 cerr << "You shouldn't be replacing variables in an empty Literal" << endl;
231#endif
232 for (size_t i = 0; i < arity; i++) {
233 args[i] = ti->replace_variable_in_term(new_var, old_var, args[i]);
234 }
235}
Term * replace_variable_in_term(Variable *, Variable *, Term *)
Replace a variable in a term with an alternative, maintaining the structure of the TermIndex.
Definition TermIndex.cpp:93

◆ replace_variable_with_term()

void Literal::replace_variable_with_term ( Term * new_term,
Variable * old_var,
TermIndex * ti )

Replace one variable with a term throughout.

Iterate over the args using the replace_variable_in_term_with_term method in TermIndex to do the real work.

Parameters
new_termPointer to new Term
old_varPointer to old Variable
tiPointer to the TermIndex

Definition at line 237 of file Literal.cpp.

238 {
239#ifdef DEBUGMESSAGES
240 if (pred == nullptr)
241 cerr << "You shouldn't be subbing terms in an empty Literal" << endl;
242#endif
243 for (size_t i = 0; i < arity; i++) {
244 args[i] = ti->replace_variable_in_term_with_term(new_term, old_var, args[i]);
245 }
246}
Term * replace_variable_in_term_with_term(Term *, Variable *, Term *)
Minor variation on replace_variable_in_term.

◆ subbed_equal()

bool Literal::subbed_equal ( Literal * l) const

Equality, taking account of substitutions.

Parameters
lPointer to Literal to compare with

Definition at line 126 of file Literal.cpp.

126 {
127 #ifdef DEBUGMESSAGES
128 if (pred == nullptr || l->pred == nullptr) {
129 cerr << "You shouldn't be checking subbed equality of empty Literals" << endl;
130 }
131 #endif
132 if (!is_compatible_with(l)) {
133 return false;
134 }
135 for (size_t i = 0; i < arity; i++) {
136 if (!(args[i]->subbed_equal((l->args)[i])))
137 return false;
138 }
139 return true;
140}
bool is_compatible_with(const Literal *) const
Literals can only be unified if the polarity and actual predicate are the same.
Definition Literal.cpp:67
bool subbed_equal(Literal *) const
Equality, taking account of substitutions.
Definition Literal.cpp:126

◆ to_prolog_string()

string Literal::to_prolog_string ( ) const

Convert to a string in a format readable by Prolog.

Does not apply substitutions.

Definition at line 159 of file Literal.cpp.

159 {
160 string result;
161 if (!polarity)
162 result += "-";
163 string name = pred->get_name();
164 if (name == "=")
165 name = "equal";
166 result += name;
167 if (arity > 0) {
168 result += "(";
169 commas::comma com(args.size());
170 for (Term* p : args) {
171 result += p->to_prolog_string();
172 result += com();
173 }
174 result += ")";
175 }
176 return result;
177}

◆ to_string()

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

Full conversion of Literal to string.

Parameters
subbedImplement substitutions if true.

Definition at line 150 of file Literal.cpp.

150 {
151 string result("Empty literal");
152 if (!is_empty()) {
153 result = get_small_lit();
154 result += vector_to_string(args, subbed);
155 }
156 return result;
157}
bool is_empty() const
Basic manipulation - entirely self-explanatory.
Definition Literal.hpp:111
string get_small_lit() const
Get the predicate and polarity as a string.
Definition Literal.cpp:142

◆ to_tptp_string()

string Literal::to_tptp_string ( ) const

Convert to a string in a format compatible with the TPTP.

Does not apply substitutions.

Definition at line 179 of file Literal.cpp.

179 {
180 string result;
181 string name = pred->get_name();
182 if (name != "=") {
183 if (!polarity)
184 result += "~";
185 result += name;
186 if (arity > 0) {
187 result += "(";
188 commas::comma com(args.size());
189 for (Term* p : args) {
190 result += p->to_prolog_string();
191 result += com();
192 }
193 result += ")";
194 }
195 }
196 else {
197 result += "( ";
198 result += args[0]->to_prolog_string();
199 if (polarity)
200 result += " = ";
201 else
202 result += " != ";
203 result += args[1]->to_prolog_string();
204 result += ")";
205 }
206 return result;
207}

Friends And Related Symbol Documentation

◆ operator<<

ostream & operator<< ( ostream & out,
const Literal & l )
friend

Definition at line 248 of file Literal.cpp.

248 {
249 if (l.is_empty())
250 out << "Empty literal";
251 else {
252 if (!l.polarity)
253 out << unicode_symbols::LogSym::neg;
254 out << l.pred->to_string();
255 out << vector_to_string(l.args);
256 }
257 return out;
258}

Member Data Documentation

◆ args

vector<Term*> Literal::args
private

Definition at line 53 of file Literal.hpp.

◆ arity

Arity Literal::arity
private

Definition at line 54 of file Literal.hpp.

◆ polarity

bool Literal::polarity
private

Definition at line 55 of file Literal.hpp.

◆ pred

Predicate* Literal::pred
private

Definition at line 52 of file Literal.hpp.


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