Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //.h file code:
- #include <string>
- #include <vector>
- #include <iostream>
- //JAVA TO C++ CONVERTER NOTE: Forward class declarations:
- class Atom;
- class Structure;
- class Term;
- class Var;
- class Trail;
- class Prog1Pred;
- class JSxx
- {
- public:
- static int DEBUGGING;
- static Atom* ATOM_dot;
- static Atom* ATOM_nil;
- static Structure* NIL;
- static Structure* CUT;
- static void indent(const int& n);
- static void main(std::vector<std::string>& args);
- static Structure* CC(std::vector<Term> &args);
- };
- class TermVarMapping
- {
- private:
- std::vector<Var*> varvar;
- std::vector<std::string> vartext;
- int size = 0;
- public:
- TermVarMapping(std::vector<Var*>& vv, std::vector<std::string>& vt, const int& vs);
- void showanswer();
- };
- class Term
- {
- public:
- virtual bool unify(Trail* p, Term* p2) = 0;
- virtual bool unify2(Trail* p, Structure* p2) = 0;
- virtual Term* copy(Trail* p) = 0;
- virtual void reset() = 0;
- void debug();
- virtual PrintStream* print(PrintStream* str) = 0;
- };
- class Trail
- {
- private:
- Term* head;
- Trail* tcdr;
- Trail* sofar;
- public:
- virtual ~Trail()
- {
- delete head;
- delete tcdr;
- delete sofar;
- }
- private:
- Trail(Term* h, Trail* t);
- public:
- Trail();
- Trail* Note();
- void Push(Term* x);
- void Undo(Trail* whereto);
- };
- class Atom
- {
- private:
- std::string atomname;
- public:
- Atom(const std::string& s);
- virtual PrintStream* print(PrintStream* str);
- bool eqatom(Atom* t);
- };
- class Structure : public Term
- {
- private:
- int arity = 0;
- Atom* fsym;
- std::vector<Term*> args;
- public:
- virtual ~Structure()
- {
- delete fsym;
- }
- Structure(const std::string& f);
- Structure(Atom* f);
- Structure(Atom* f, Term* a1);
- Structure(Atom* f, Term* a1, Term* a2);
- Structure(const std::string& f, Term* a1, Term* a2);
- Structure(Atom* f, Term* a1, Term* a2, Term* a3);
- Structure(Atom* f, std::vector<Term> &a);
- PrintStream* print(PrintStream* str) override;
- virtual PrintStream* print(PrintStream* str, const std::string& open, const std::string& mid, const std::string& close);
- void reset() override;
- bool unify(Trail* mach, Term* t) final override;
- Term* copy(Trail* mach) final override;
- Structure* copyNonvar(Trail* mach);
- private:
- Structure(Trail* mach, Structure* p);
- public:
- bool unify2(Trail* mach, Structure* t) override;
- };
- class Var : public Term
- {
- private:
- Term* instance;
- int varno = 0;
- static int timestamp;
- public:
- virtual ~Var()
- {
- delete instance;
- }
- Var();
- PrintStream* print(PrintStream* str) override;
- void reset() override;
- bool unify2(Trail* mach, Structure* t) override;
- bool unify(Trail* mach, Term* t) override;
- Term* copy(Trail* mach) override;
- };
- class Goal
- {
- private:
- Structure* head;
- Goal* tail;
- Trail* mach;
- public:
- virtual ~Goal()
- {
- delete head;
- delete tail;
- delete mach;
- }
- Goal(Structure* h);
- Goal(Structure* h, Goal* t);
- Goal* copy(Trail* mach);
- Goal* append(Goal* l);
- virtual PrintStream* print(PrintStream* str);
- /* returns a new Goal if coroutining or null if cutted */
- virtual Prog1Pred* solve(Prog1Pred* p, const int& level, TermVarMapping* map);
- };
- class Clause
- {
- public:
- Term* head;
- Goal* tail;
- virtual ~Clause()
- {
- delete head;
- delete tail;
- }
- Clause(Structure* h, Goal* t);
- Clause(Structure* h, Structure* t);
- Clause(Structure* h);
- Clause* copy(Trail* mach);
- virtual PrintStream* print(PrintStream* str);
- Clause* append(Structure* l);
- };
- class Prog1Pred
- {
- public:
- Clause* pcar;
- Prog1Pred* pcdr;
- virtual ~Prog1Pred()
- {
- delete pcar;
- delete pcdr;
- }
- Prog1Pred(Clause* h);
- Prog1Pred(Clause* h, Prog1Pred* t);
- Prog1Pred(Clause* h, Clause* t);
- Prog1Pred(Clause* h, Clause* h2, Clause* t);
- /* returns the newly appended Prog1Pred*/
- Prog1Pred* append(Clause* l);
- /* returns itself*/
- Prog1Pred* prepend(Clause* h);
- };
- static int test_program_append();
- }
- //.cpp file code:
- using namespace std;
- int JSxx::DEBUGGING = 1;
- Atom* JSxx::ATOM_dot = new Atom(".");
- Atom* JSxx::ATOM_nil = new Atom("[]");
- Structure* JSxx::NIL = new Structure(ATOM_nil);
- Structure* JSxx::CUT = new Structure(new Atom("!"));
- void JSxx::indent(const int& n)
- {
- for(int i = 0; i < n; i++)
- {
- cout << " ";
- }
- }
- void JSxx::main(std::vector<wstring>& args)
- {
- test_program_append();
- }
- Structure* JSxx::CC(vector<Term> &args)
- {
- return new Structure(ATOM_dot,args);
- }
- TermVarMapping::TermVarMapping(std::vector<Var*>& vv, std::vector<wstring>& vt, const int& vs)
- {
- this->varvar = vv;
- this->vartext = vt;
- this->size = vs;
- }
- void TermVarMapping::showanswer()
- {
- if(size == 0)
- {
- cout << "yes\n";
- } else
- {
- for(int i = 0; i < size; i++)
- {
- cout << vartext[i];
- cout << " = ";
- cout << varvar[i];
- cout << "\n";
- }
- }
- }
- void Term::debug()
- {
- print(System::out);
- }
- Trail::Trail(Term* h, Trail* t)
- {
- this->head = h;
- this->tcdr = t;
- }
- Trail::Trail()
- {
- this->head = nullptr;
- this->tcdr = nullptr;
- }
- Trail* Trail::Note()
- {
- return (sofar);
- }
- void Trail::Push(Term* x)
- {
- sofar = new Trail(x, sofar);
- }
- void Trail::Undo(Trail* whereto)
- {
- for(; sofar != whereto; sofar = sofar->tcdr)
- {
- sofar->head->reset();
- }
- }
- Atom::Atom(const wstring& s)
- {
- this->atomname = s;
- }
- PrintStream* Atom::print(PrintStream* str)
- {
- str->print(atomname);
- return str;
- }
- bool Atom::eqatom(Atom* t)
- {
- return atomname == atomname;
- }
- Structure::Structure(const wstring& f)
- {
- this->fsym = new Atom(f);
- this->arity = 0;
- this->args.clear();
- }
- Structure::Structure(Atom* f)
- {
- this->fsym = f;
- this->arity = 0;
- this->args.clear();
- }
- Structure::Structure(Atom* f, Term* a1)
- {
- this->fsym = f;
- this->arity = 1;
- this->args = std::vector<Term*>{ a1 };
- args[0] = a1;
- }
- Structure::Structure(Atom* f, Term* a1, Term* a2)
- {
- this->fsym = f;
- this->arity = 2;
- this->args = std::vector<Term*>{ a1, a2 };
- }
- Structure::Structure(const wstring& f, Term* a1, Term* a2)
- {
- this->fsym = new Atom(f);
- this->arity = 2;
- this->args = std::vector<Term*>{ a1, a2 };
- }
- Structure::Structure(Atom* f, Term* a1, Term* a2, Term* a3)
- {
- this->fsym = f;
- this->arity = 3;
- this->args = std::vector<Term*>{ a1, a2, a3 };
- }
- Structure::Structure(Atom* f, vector<Term> &a)
- {
- this->fsym = f;
- this->args = a::clone();
- this->arity = args.size();
- }
- PrintStream* Structure::print(PrintStream* str)
- {
- if(fsym == ATOM_dot && arity == 2)
- {
- return print(str,"[","|","]");
- } else
- {
- return print(str,"(",",",")");
- }
- }
- PrintStream* Structure::print(PrintStream* str, const wstring& open, const wstring& mid, const wstring& close)
- {
- fsym->print(str);
- if(arity > 0)
- {
- str->print(open);
- for(int i = 0; i < arity;)
- {
- args[i]->print(str);
- if(++i < arity)
- {
- str->print(mid);
- }
- }
- str->print(close);
- }
- return str;
- }
- void Structure::reset()
- {
- }
- bool Structure::unify(Trail* mach, Term* t)
- {
- return (t->unify2(mach,this));
- }
- Term* Structure::copy(Trail* mach)
- {
- return (copyNonvar(mach));
- }
- Structure* Structure::copyNonvar(Trail* mach)
- {
- return new Structure(mach,this);
- }
- Structure::Structure(Trail* mach, Structure* p)
- {
- this->fsym = p->fsym;
- this->arity = p->arity;
- this->args = p->arity == 0 ? nullptr : std::vector<Term*>(p->arity);
- for(int i = 0; i < arity; i++)
- {
- args[i] = p->args[i]->copy(mach);
- }
- }
- bool Structure::unify2(Trail* mach, Structure* t)
- {
- if(!(fsym->eqatom(t->fsym) && arity == t->arity))
- {
- return (false);
- }
- for(int i = 0; i < arity; i++)
- {
- if(!args[i]->unify(mach,t->args[i]))
- {
- return (false);
- }
- }
- return (true);
- }
- int Var::timestamp = 0;
- Var::Var()
- {
- this->instance = this;
- this->varno = ++timestamp;
- }
- PrintStream* Var::print(PrintStream* str)
- {
- if(instance != this)
- {
- return instance->print(str);
- } else
- {
- str->print("_");
- str->print(varno);
- return str;
- }
- }
- void Var::reset()
- {
- instance = this;
- }
- bool Var::unify2(Trail* mach, Structure* t)
- {
- return (this->unify(mach, t));
- }
- bool Var::unify(Trail* mach, Term* t)
- {
- if(instance != this)
- {
- return (instance->unify(mach,t));
- }
- mach->Push(this);
- instance = t;
- return (true);
- }
- Term* Var::copy(Trail* mach)
- {
- if(instance == this)
- {
- mach->Push(this);
- instance = new Var();
- }
- return (instance);
- }
- Goal::Goal(Structure* h)
- {
- this->head = h;
- this->tail = nullptr;
- mach = new Trail();
- }
- Goal::Goal(Structure* h, Goal* t)
- {
- this->head = h;
- this->tail = t;
- mach = new Trail();
- }
- Goal* Goal::copy(Trail* mach)
- {
- Goal tempVar(head->copyNonvar(mach), tail == nullptr ? nullptr : tail->copy(mach));
- return (&tempVar);
- }
- Goal* Goal::append(Goal* l)
- {
- Goal tempVar(head, tail == nullptr ? nullptr : tail->append(l));
- return (&tempVar);
- }
- PrintStream* Goal::print(PrintStream* str)
- {
- head->print(str);
- if(tail != nullptr)
- {
- str->print(" ; ");
- tail->print(str);
- }
- return str;
- }
- Prog1Pred* Goal::solve(Prog1Pred* p, const int& level, TermVarMapping* map)
- {
- PrintStream* str = System::out;
- if(DEBUGGING != 0)
- {
- indent(level);
- str->print("solve@");
- str->print(level);
- str->print(": ");
- this->print(str);
- str->print("\n");
- }
- Trail* trail = (mach == nullptr)?new Trail():mach;
- Prog1Pred* retQ = p;
- for(Prog1Pred* q = p; q != nullptr; q = q->pcdr)
- {
- Trail* t = trail->Note();
- retQ = q;
- Clause* next = q->pcar;
- Clause* c = next->copy(trail);
- trail->Undo(t);
- if(DEBUGGING != 0)
- {
- indent(level);
- str->print(" try:");
- c->print(str);
- str->print("\n");
- }
- if(head->unify(trail,c->head))
- {
- Goal* gdash = ((c->tail == nullptr) ? tail : c->tail->append(tail));
- if(gdash == nullptr)
- {
- map->showanswer();
- } else
- {
- retQ = gdash->solve(p, level + 1, map);
- }
- } else
- {
- if(DEBUGGING != 0)
- {
- indent(level);
- str->print(" nomatch.\n");
- }
- }
- trail->Undo(t);
- }
- return retQ;
- }
- Clause::Clause(Structure* h, Goal* t)
- {
- this->head = h;
- this->tail = t;
- }
- Clause::Clause(Structure* h, Structure* t)
- {
- this->head = h;
- this->tail = new Goal(t);
- }
- Clause::Clause(Structure* h)
- {
- this->head = h;
- this->tail = nullptr;
- }
- Clause* Clause::copy(Trail* mach)
- {
- Clause tempVar(head->copyNonvar(mach), tail == nullptr ? nullptr : tail->copy(mach));
- return (&tempVar);
- }
- PrintStream* Clause::print(PrintStream* str)
- {
- head->print(str);
- str->print(" :- ");
- if(tail == nullptr)
- {
- str->print("true");
- } else
- {
- tail->print(str);
- }
- return str;
- }
- Clause* Clause::append(Structure* l)
- {
- if(tail == nullptr)
- {
- tail = new Goal(l);
- } else
- {
- Goal tempVar(l);
- tail = tail->append(&tempVar);
- }
- return this;
- }
- Prog1Pred::Prog1Pred(Clause* h)
- {
- this->pcar = h;
- this->pcdr = nullptr;
- }
- Prog1Pred::Prog1Pred(Clause* h, Prog1Pred* t)
- {
- this->pcar = h;
- this->pcdr = t;
- }
- Prog1Pred::Prog1Pred(Clause* h, Clause* t)
- {
- this->pcar = h;
- this->pcdr = new Prog1Pred(t);
- }
- Prog1Pred::Prog1Pred(Clause* h, Clause* h2, Clause* t)
- {
- this->pcar = h;
- this->pcdr = new Prog1Pred(h2, t);
- }
- Prog1Pred* Prog1Pred::append(Clause* l)
- {
- if(pcdr == nullptr)
- {
- return pcdr = new Prog1Pred(l);
- } else
- {
- return pcdr->append(l);
- }
- }
- Prog1Pred* Prog1Pred::prepend(Clause* h)
- {
- pcdr = new Prog1Pred(pcar, pcdr);
- pcar = h;
- return this;
- }
- int <missing_class_definition>::test_program_append()
- {
- /*Psuedovars for source translation */
- Var* VAR_X = new Var();
- Var* VAR_L = new Var();
- Var* VAR_M = new Var();
- Var* VAR_N = new Var();
- Var* VAR_I = new Var();
- Var* VAR_J = new Var();
- Atom* APPEND3 = new Atom("append_3");
- /*
- Test normally:
- append_3([],X,X).
- append([X|L],M,[X|N]):- append(L,M,N).
- */
- Structure tempVar(APPEND3, NIL, VAR_X, VAR_X);
- Clause* c1 = new Clause(&tempVar);
- Structure tempVar2(APPEND3, CC(VAR_X, VAR_L),VAR_M, CC(VAR_X,VAR_N));
- Structure tempVar3(APPEND3,VAR_L,VAR_M,VAR_N);
- Clause* c2 = new Clause(&tempVar2, &tempVar3);
- Prog1Pred* test_program_normally = new Prog1Pred(c1, c2);
- /*
- Test reversed:
- append([X|LL],M,[X|N]):- append(LL,M,N).
- append_3([],X,X).
- */
- Prog1Pred* test_program_reversed = new Prog1Pred(c2, c1);
- /*
- Test Cut:
- append_3([],X,X):- !.
- append([X|LL],M,[X|N]):- append(LL,M,N).
- */
- Trail tempVar4();
- Clause* c3 = c1->copy(&tempVar4);
- c3->append(CUT);
- Prog1Pred* test_program_cut = new Prog1Pred(c3, c2);
- std::vector<Var*> varvar = { VAR_I, VAR_J };
- std::vector<wstring> varname = { "I", "J" };
- TermVarMapping* var_name_map = new TermVarMapping(varvar, varname, 2);
- /*
- ?- append_3(I,J,[1,2,3]).
- */
- Structure tempVar5(APPEND3, VAR_I, VAR_J, CC(new Structure("1"), CC(new Structure("2"), CC(new Structure("3"), NIL))));
- Goal* g1 = new Goal(&tempVar5);
- cout << "=======Append with normal clause order:\n";
- cout << g1->solve(test_program_normally, 0, var_name_map);
- cout << "\n=======Append with reversed clause order:\n";
- cout << g1->solve(test_program_reversed, 0, var_name_map);
- cout << "\n=======Append with a cut:\n";
- cout << g1->solve(test_program_cut, 0, var_name_map);
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement