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

Basic representation of variables. More...

#include <Variable.hpp>

Collaboration diagram for Variable:

Public Member Functions

 Variable (const Variable &)=delete
 Explicitly disallow copying as this is a very bad idea.
 
 Variable (const Variable &&)=delete
 
Variableoperator= (const Variable &)=delete
 
Variableoperator= (const Variable &&)=delete
 
ID get_id () const
 Self-explanatory access function.
 
string get_name () const
 Self-explanatory access function.
 
string to_string (bool=false) const
 Convert to a (coloured) string.
 
string to_prolog_string (bool=false) const
 Convert to a Prolog string.
 
bool is_subbed () const
 Is this variable currently substituted?
 
Termget_subbed_term () const
 Get the current substitution, or nullptr.
 
void remove_substitution ()
 Self-explanatory, but be careful!
 
void substitute (Term *t)
 Self-explanatory, but be careful!
 
bool subbed_is_function () const
 Is the variable now a function, taking substitution into account?
 
bool subbed_is_variable () const
 Is the variable still in fact a variable, taking substitution into account?
 
Variablesubbed_variable () const
 If the variable is unsubbed then return "this"; otherwise a pointer to the variable at the end of a chain of substitutions of variables for variables.
 
bool contains_variable (Variable *) const
 Does the substitution turn the variable into something that includes the variable passed?
 
Functionget_subbed_f () const
 If there is a chain of substitutions ending in a function, find that function.
 
Arity get_subbed_arity () const
 If there is a chain of substitutions ending in a function, find that function's arity.
 
Termskip_leading_variables () const
 It may be that there is a chain of substitutions attached to this variable: skip the leading ones.
 
string make_LaTeX (bool=false) const
 Convert the Variable to a useable LaTeX representation.
 
string make_Graphviz (bool=false) const
 Convert the Variable to a useable Graphviz representation.
 

Private Member Functions

 Variable ()
 Constructors: you can only construct un-substituted variables.
 
 Variable (ID new_id)
 
 Variable (ID new_id, const string &new_name)
 

Private Attributes

ID id
 
string name
 
Termsubstitution
 

Friends

class VariableIndex
 
ostream & operator<< (ostream &out, const Variable &v)
 This is mainly for debugging. If you want actual representations then use Variable::to_string or similar.
 

Detailed Description

Basic representation of variables.

This way of treating variables makes substitution and the removal thereof extremely easy. BUT, you have to be somewhat careful, because once substituted you may no longer have a variable.

A Variable has an id and a name. Substitution is by way of setting the substitution pointer from nullptr to the address of a Term.

Some of the implementation is in Term.cpp in order to allow the compiler to do things in the right order.

You should never try to construct one of these yourself. Only do it by way of the VariableIndex.

Definition at line 58 of file Variable.hpp.

Constructor & Destructor Documentation

◆ Variable() [1/4]

Variable::Variable ( )
inlineprivate

Constructors: you can only construct un-substituted variables.

These are private because in order to enforce sharing the only place you should ever construct one is in the VariableIndex, which is a friend.

Definition at line 71 of file Variable.hpp.

71: id(0), name(), substitution(nullptr) {}

◆ Variable() [2/4]

Variable::Variable ( ID new_id)
inlineprivate

Definition at line 72 of file Variable.hpp.

72: id(new_id), name(), substitution(nullptr) {}

◆ Variable() [3/4]

Variable::Variable ( ID new_id,
const string & new_name )
inlineprivate

Definition at line 73 of file Variable.hpp.

74 : id(new_id), name(new_name), substitution(nullptr) {}

◆ Variable() [4/4]

Variable::Variable ( const Variable & )
delete

Explicitly disallow copying as this is a very bad idea.

You should never have to do this, and by explicitly disallowing it we help the compiler to find our errors for us.

Member Function Documentation

◆ contains_variable()

bool Variable::contains_variable ( Variable * v2) const

Does the substitution turn the variable into something that includes the variable passed?

Note that this acts on the eventual term, so if you look for something that has itself been subbed then the answer is "no".

Parameters
v2Pointer to Variable to search for.

Definition at line 395 of file Term.cpp.

395 {
396 if (substitution == nullptr)
397 return this==v2;
398 else
399 return substitution->contains_variable(v2);
400}
bool contains_variable(Variable *) const
Taking substitution into account, does the term include the variable passed?
Definition Term.cpp:143

◆ get_id()

ID Variable::get_id ( ) const
inline

Self-explanatory access function.

Definition at line 90 of file Variable.hpp.

90{ return id; }

◆ get_name()

string Variable::get_name ( ) const
inline

Self-explanatory access function.

Only return the variable name – do not follow substitutions.

Definition at line 96 of file Variable.hpp.

96{ return name; }

◆ get_subbed_arity()

Arity Variable::get_subbed_arity ( ) const

If there is a chain of substitutions ending in a function, find that function's arity.

Definition at line 411 of file Term.cpp.

411 {
412 if (substitution == nullptr) {
413 cerr << "There is no arity to be found..." << endl;
414 return 0;
415 }
416 else
417 return substitution->get_subbed_arity();
418}
Arity get_subbed_arity() const
Taking substitution into account, what arity do we actually have?
Definition Term.cpp:160

◆ get_subbed_f()

Function * Variable::get_subbed_f ( ) const

If there is a chain of substitutions ending in a function, find that function.

Definition at line 402 of file Term.cpp.

402 {
403 if (substitution == nullptr) {
404 cerr << "There is no function name to be found..." << endl;
405 return nullptr;
406 }
407 else
408 return substitution->get_subbed_f();
409}
Function * get_subbed_f() const
Taking substitution into account, what function do we actually have?
Definition Term.cpp:153

◆ get_subbed_term()

Term * Variable::get_subbed_term ( ) const
inline

Get the current substitution, or nullptr.

Definition at line 116 of file Variable.hpp.

116{ return substitution; }

◆ is_subbed()

bool Variable::is_subbed ( ) const
inline

Is this variable currently substituted?

Definition at line 112 of file Variable.hpp.

112{ return (substitution != nullptr); }

◆ make_Graphviz()

string Variable::make_Graphviz ( bool subbed = false) const

Convert the Variable to a useable Graphviz representation.

Parameters
subbedInclude effect of substitution if true.

Definition at line 364 of file Term.cpp.

364 {
365 if (subbed && substitution != nullptr)
366 return substitution->make_Graphviz(subbed);
367 else {
368 return name;
369 }
370}
string make_Graphviz(bool=false) const
Make a useable Graphviz representation.
Definition Term.cpp:239

◆ make_LaTeX()

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

Convert the Variable to a useable LaTeX representation.

Note that this makes a limited attempt to deal with all situations. Anonymous variables will work fine, but if your named variables have symbols that need an escape or similar it may not work as expected.

Assumes that the output will be typeset in math mode. Parameter implements substitutions if true.

Parameters
subbedInclude effect of substitution if true.

Definition at line 353 of file Term.cpp.

353 {
354 if (subbed && substitution != nullptr)
355 return substitution->make_LaTeX(subbed);
356 else {
357 string s ("\\text{");
358 s += latex_escape_characters(name);
359 s += "}";
360 return s;
361 }
362}
string make_LaTeX(bool=false) const
Make a useable LaTeX representation.
Definition Term.cpp:222

◆ remove_substitution()

void Variable::remove_substitution ( )
inline

Self-explanatory, but be careful!

As long as your Term* pointers are only generated by the TermIndex, then you don't have to worry about memory allocation or de-allocation.

If you did a "new Term" anywhere yourself then you must live with the consequences.

Definition at line 127 of file Variable.hpp.

127{ substitution = nullptr; }

◆ skip_leading_variables()

Term * Variable::skip_leading_variables ( ) const

It may be that there is a chain of substitutions attached to this variable: skip the leading ones.

When doing unification we actually want a pointer either to the final variable in a chain or to the first term that's a function. This returns it.

Definition at line 420 of file Term.cpp.

420 {
421 if (substitution != nullptr) {
422 return substitution->skip_leading_variables();
423 }
424 cerr << "Stop it! You're trying to skip something inappropriate!" << endl;
425 return nullptr;
426}
Term * skip_leading_variables() const
It may be that there is a chain of substitutions attached to a variable.
Definition Term.cpp:262

◆ subbed_is_function()

bool Variable::subbed_is_function ( ) const

Is the variable now a function, taking substitution into account?

Note that this it false if the variable is unsubstituted.

Definition at line 372 of file Term.cpp.

372 {
373 if (substitution == nullptr)
374 return false;
375 else
376 return substitution->subbed_is_function();
377}
bool subbed_is_function() const
Is this term a function, taking substitution into accoumt?
Definition Term.cpp:120

◆ subbed_is_variable()

bool Variable::subbed_is_variable ( ) const

Is the variable still in fact a variable, taking substitution into account?

Note that this it true if the variable is unsubstituted.

Definition at line 379 of file Term.cpp.

379 {
380 if (substitution == nullptr)
381 return true;
382 else
383 return substitution->subbed_is_variable();
384}
bool subbed_is_variable() const
Is this term a variable, taking substitution into accoumt?
Definition Term.cpp:127

◆ subbed_variable()

Variable * Variable::subbed_variable ( ) const

If the variable is unsubbed then return "this"; otherwise a pointer to the variable at the end of a chain of substitutions of variables for variables.

You should probably only call this if you know the substitution is a chain of variables. (Otherwise you're getting a nullptr and an error message.)

Definition at line 386 of file Term.cpp.

386 {
387 if (substitution == nullptr) {
388 Variable* p = const_cast<Variable*>(this);
389 return p;
390 }
391 else
392 return substitution->subbed_variable();
393}
Variable * subbed_variable() const
Taking substitution into account, what variable do we actually have?
Definition Term.cpp:134
Basic representation of variables.
Definition Variable.hpp:58

◆ substitute()

void Variable::substitute ( Term * t)
inline

Self-explanatory, but be careful!

See the documentation for remove_substitution.

Parameters
tPointer to Term to substitute.

Definition at line 135 of file Variable.hpp.

135{ substitution = t; }

◆ to_prolog_string()

string Variable::to_prolog_string ( bool subbed = false) const

Convert to a Prolog string.

Apply substitutions if argument is true.

Definition at line 346 of file Term.cpp.

346 {
347 if (subbed && substitution != nullptr) {
348 return substitution->to_prolog_string(true);
349 }
350 return name;
351}
string to_prolog_string(bool=false) const
Convert to a string that can be read by Prolog.
Definition Term.cpp:202

◆ to_string()

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

Convert to a (coloured) string.

Parameters
subbedInclude effect of substitution if true.

Definition at line 336 of file Term.cpp.

336 {
337 colour_string::ColourString cs(params::use_colours);
338 string s = cs(name).lblue();
339 if (subbed && substitution != nullptr) {
340 s += unicode_symbols::LogSym::subbed;
341 s += substitution->to_string(subbed);
342 }
343 return s;
344}
string to_string(bool=false, bool=false) const
Make a string representation of the Term, taking into account any substitutions that have been made.
Definition Term.cpp:180
Simple addition of colour to strings and ostreams.

Friends And Related Symbol Documentation

◆ operator<<

ostream & operator<< ( ostream & out,
const Variable & v )
friend

This is mainly for debugging. If you want actual representations then use Variable::to_string or similar.

Definition at line 33 of file Variable.cpp.

33 {
34 out << "Var: " << setw(params::output_width) << v.name
35 <<" ID: " << setw(params::output_width) << v.id;
36 if (v.substitution != nullptr)
37 out << " Subbed";
38 return out;
39}

◆ VariableIndex

friend class VariableIndex
friend

Definition at line 211 of file Variable.hpp.

Member Data Documentation

◆ id

ID Variable::id
private

Definition at line 60 of file Variable.hpp.

◆ name

string Variable::name
private

Definition at line 61 of file Variable.hpp.

◆ substitution

Term* Variable::substitution
private

Definition at line 62 of file Variable.hpp.


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