Connect++ 0.6.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
Unifier.hpp
1/*
2
3Copyright © 2023-24 Sean Holden. All rights reserved.
4
5*/
6/*
7
8This file is part of Connect++.
9
10Connect++ is free software: you can redistribute it and/or modify it
11under the terms of the GNU General Public License as published by the
12Free Software Foundation, either version 3 of the License, or (at your
13option) any later version.
14
15Connect++ is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18more details.
19
20You should have received a copy of the GNU General Public License along
21with Connect++. If not, see <https://www.gnu.org/licenses/>.
22
23*/
24
25#ifndef UNIFIER_HPP
26#define UNIFIER_HPP
27
28#include<iostream>
29#include<string>
30#include<deque>
31
32#include "Substitution.hpp"
33#include "Literal.hpp"
34
35using std::deque;
36using std::pair;
37using std::ostream;
38using std::endl;
39using std::string;
40
45using UPair = pair<Term* ,Term*>;
50enum class UnificationOutcome {Succeed, ConflictFail, OccursFail};
55ostream& operator<<(ostream&, const UnificationOutcome&);
89class Unifier {
90private:
98 vector<UPair> to_do;
103 UnificationOutcome complete_unification();
104public:
108 Unifier() : s(), to_do() {}
115 Unifier(const Unifier&) = delete;
116 Unifier(const Unifier&&) = delete;
117 Unifier& operator=(const Unifier&) = delete;
118 Unifier& operator=(const Unifier&&) = delete;
122 Substitution get_substitution() const { return s; }
129 UnificationOutcome operator()(Term*, Term*);
136 UnificationOutcome unify(Term*, Term*);
141 UnificationOutcome unify_terms(Term*, Term*);
150 UnificationOutcome operator()(const vector<Term*>&,
151 const vector<Term*>&);
158 UnificationOutcome operator()(Literal*, Literal*);
168 inline UnificationOutcome operator()(const Literal& l1, const Literal& l2) {
169 if (params::poly_unification) {
170 to_do.clear();
171 size_t s = l1.get_arity();
172 for (size_t i = 0; i < s; i++) {
173 to_do.push_back(UPair(l1[i], l2[i]));
174 }
175 return complete_unification();
176 }
177 else {
178 auto i = l1.get_args().begin();
179 for (Term* term1 : l2.get_args()) {
180 UnificationOutcome outcome = unify_terms(term1, *i);
181 if (outcome != UnificationOutcome::Succeed) {
182 backtrack();
183 return outcome;
184 }
185 i++;
186 }
187 return UnificationOutcome::Succeed;
188 }
189 }
194 void backtrack();
200 string to_string(bool subbed = false) const {
201 return s.to_string(subbed);
202 }
203
204 friend ostream& operator<<(ostream&, const Unifier&);
205};
206
207#endif
Basic representation of literals, bundling together (pointers to) a Predicate, a collection of argume...
Definition Literal.hpp:50
const vector< Term * > & get_args() const
Basic get method.
Definition Literal.hpp:83
Arity get_arity() const
Basic get method.
Definition Literal.hpp:75
General representation of a substitution.
string to_string(bool=false) const
Make a string representation.
General representation of terms.
Definition Term.hpp:62
Wrap up various applications of unificiation into a single class: all the unification you need to do ...
Definition Unifier.hpp:89
void backtrack()
Apply the backtracking process to the substitution that has been constructed.
Definition Unifier.cpp:195
UnificationOutcome unify(Term *, Term *)
Implementation of unification for a pair of Terms.
Definition Unifier.cpp:38
string to_string(bool subbed=false) const
Make a nice string representation.
Definition Unifier.hpp:200
UnificationOutcome operator()(const Literal &l1, const Literal &l2)
Definition Unifier.hpp:168
Substitution get_substitution() const
Trivial get methods for the result.
Definition Unifier.hpp:122
UnificationOutcome operator()(Term *, Term *)
Implementation of unification for a pair of Terms.
Definition Unifier.cpp:27
Unifier()
There isn't really much to initialize.
Definition Unifier.hpp:108
Unifier(const Unifier &)=delete
You really only need one Unifier!
vector< UPair > to_do
Queue used by complete_unification method.
Definition Unifier.hpp:98
UnificationOutcome unify_terms(Term *, Term *)
Main method implementing the traditional unification algorithm.
Definition Unifier.cpp:48
UnificationOutcome complete_unification()
Implementation of unification: for the polynomial-time approach. All the real work happens here.
Definition Unifier.cpp:127
Substitution s
Build the resulting substitution here.
Definition Unifier.hpp:94