30 to_do.push_front(UPair(term1, term2));
35 const vector<Term*>& t2s) {
36 if (t1s.size() != t2s.size())
37 return UnificationOutcome::ConflictFail;
40 for (
Term* term1 : t1s) {
41 to_do.push_front(UPair(term1, *i));
49 cerr <<
"ERROR" <<
": ";
50 cerr <<
"You're trying to unify non-compatible literals." << endl;
51 cerr <<
"ERROR" <<
": " << *l1 << endl;
52 cerr <<
"ERROR" <<
": " << *l2 << endl;
54 const vector<Term*>& args1 = l1->
get_args();
55 const vector<Term*>& args2 = l2->
get_args();
62 cerr <<
"ERROR" <<
": ";
63 cerr <<
"You're trying to unify non-compatible literals." << endl;
64 cerr <<
"ERROR" <<
": " << l1 << endl;
65 cerr <<
"ERROR" <<
": " << l2 << endl;
67 const vector<Term*>& args1 = l1.
get_args();
68 const vector<Term*>& args2 = l2.
get_args();
74 while (
to_do.size() > 0) {
76 UPair upair(
to_do.front());
77 Term* t1(upair.first);
78 Term* t2(upair.second);
98 if (t1_is_fun && t2_is_var) {
99 to_do.push_back(UPair(t2, t1));
105 return UnificationOutcome::OccursFail;
114 if (t1_is_fun && t2_is_fun) {
119 return UnificationOutcome::ConflictFail;
123 size_t n_args = t1->
arity();
124 for (
size_t i = 0; i < n_args; i++) {
125 to_do.push_back(UPair((*t1)[i], (*t2)[i]));
131 return UnificationOutcome::Succeed;
139ostream& operator<<(ostream& out,
const UnificationOutcome& o) {
141 case UnificationOutcome::Succeed:
142 out <<
"Unification: success.";
144 case UnificationOutcome::ConflictFail:
145 out <<
"Unification: failed due to conflict.";
147 case UnificationOutcome::OccursFail:
148 out <<
"Unification: failed due to occurs check.";
157ostream& operator<<(ostream& out,
const Unifier& u) {
Basic representation of literals, bundling together (pointers to) a Predicate, a collection of argume...
const vector< Term * > & get_args() const
Basic get method.
bool is_compatible_with(const Literal *) const
Literals can only be unified if the polarity and actual predicate are the same.
void backtrack() const
Remove a substitution everywhere.
void push_back(Variable *, Term *)
Add a pair to a substitution.
void clear()
Clear a substitution.
General representation of terms.
Function * get_subbed_f() const
Taking substitution into account, what function do we actually have?
Variable * get_v() const
Self-explanatory access function.
bool subbed_equal(Term *) const
Equality test, taking substitution into account.
Term * skip_leading_variables() const
It may be that there is a chain of substitutions attached to a variable.
bool subbed_is_variable() const
Is this term a variable, taking substitution into accoumt?
Arity get_subbed_arity() const
Taking substitution into account, what arity do we actually have?
bool contains_variable(Variable *) const
Taking substitution into account, does the term include the variable passed?
Arity arity() const
Self-explanatory access function.
bool subbed_is_function() const
Is this term a function, taking substitution into accoumt?
Wrap up various applications of unificiation into a single class: all the unification you need to do ...
void backtrack()
Apply the backtracking process to the substitution that has been constructed.
string to_string(bool subbed=false) const
Make a nice string representation.
UnificationOutcome operator()(Term *, Term *)
Implementation of unification for a pair of Terms.
UnificationOutcome complete_unification()
Implementation of unification: all the real work happens here.
deque< UPair > to_do
Queue used by complete_unification method.
Substitution s
Build the resulting substitution here.
Basic representation of variables.
Variable * subbed_variable() const
If the variable is unsubbed then return "this"; otherwise a pointer to the variable at the end of a c...
void substitute(Term *t)
Self-explanatory, but be careful!