28 if (params::poly_unification) {
30 to_do.push_back(UPair(term1, term2));
40 if (outcome != UnificationOutcome::Succeed) {
61 if (t1_is_var && t1v == t2v) {}
62 else if (!t1_is_var && !t2_is_var) {
63 if (t1f == t2f && t1a == t2a) {
64 for (
size_t i = 0; i < t1a; i++) {
65 UnificationOutcome outcome =
unify_terms((*t1)[i], (*t2)[i]);
66 if (outcome != UnificationOutcome::Succeed) {
72 return UnificationOutcome::ConflictFail;
75 else if (!t1_is_var) {
79 return UnificationOutcome::OccursFail;
85 return UnificationOutcome::Succeed;
89 const vector<Term*>& t2s) {
90 if (t1s.size() != t2s.size())
91 return UnificationOutcome::ConflictFail;
92 if (params::poly_unification) {
95 for (
Term* term1 : t1s) {
96 to_do.push_back(UPair(term1, *i));
102 auto i = t2s.begin();
103 for (
Term* term1 : t1s) {
104 UnificationOutcome outcome =
unify_terms(term1, *i);
105 if (outcome != UnificationOutcome::Succeed) {
111 return UnificationOutcome::Succeed;
117 cerr <<
"ERROR" <<
": ";
118 cerr <<
"You're trying to unify non-compatible literals." << endl;
119 cerr <<
"ERROR" <<
": " << *l1 << endl;
120 cerr <<
"ERROR" <<
": " << *l2 << endl;
122 const vector<Term*>& args1 = l1->
get_args();
123 const vector<Term*>& args2 = l2->
get_args();
129 while (
to_do.size() > 0) {
131 UPair upair(
to_do.back());
132 Term* t1(upair.first);
133 Term* t2(upair.second);
149 if (!t1_is_var && t2_is_var) {
150 to_do.push_back(UPair(t2, t1));
160 if (!t1_is_var && !t2_is_var) {
165 return UnificationOutcome::ConflictFail;
169 size_t n_args = t1->
arity();
170 for (
size_t i = 0; i < n_args; i++) {
171 to_do.push_back(UPair((*t1)[i], (*t2)[i]));
180 if (t1_is_var && !contains) {
187 if (t1_is_var && !t2_is_var && contains) {
189 return UnificationOutcome::OccursFail;
192 return UnificationOutcome::Succeed;
200ostream& operator<<(ostream& out,
const UnificationOutcome& o) {
202 case UnificationOutcome::Succeed:
203 out <<
"Unification: success.";
205 case UnificationOutcome::ConflictFail:
206 out <<
"Unification: failed due to conflict.";
208 case UnificationOutcome::OccursFail:
209 out <<
"Unification: failed due to occurs check.";
218ostream& operator<<(ostream& out,
const Unifier& u) {
Basic representation of functions.
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.
bool subbed_equal(Term *) const
Equality test, taking substitution into account.
Term * skip_leading_variables_for_unification(bool &, Variable *&, Function *&, size_t &) const
It may be that there is a chain of substitutions attached to a variable.
bool contains_variable(Variable *) const
Taking substitution into account, does the term include the variable passed?
Arity arity() const
Self-explanatory access function.
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.
UnificationOutcome unify(Term *, Term *)
Implementation of unification for a pair of Terms.
string to_string(bool subbed=false) const
Make a nice string representation.
UnificationOutcome operator()(Term *, Term *)
Implementation of unification for a pair of Terms.
vector< UPair > to_do
Queue used by complete_unification method.
UnificationOutcome unify_terms(Term *, Term *)
Main method implementing the traditional unification algorithm.
UnificationOutcome complete_unification()
Implementation of unification: for the polynomial-time approach. All the real work happens here.
Substitution s
Build the resulting substitution here.
Basic representation of variables.
void substitute(Term *t)
Self-explanatory, but be careful!