Advertisement
logicmoo

MandC

Aug 16th, 2018
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.36 KB | None | 0 0
  1. //.h file code:
  2.  
  3. #include <string>
  4. #include <vector>
  5. #include <iostream>
  6.  
  7. //JAVA TO C++ CONVERTER NOTE: Forward class declarations:
  8. class Atom;
  9. class Structure;
  10. class Term;
  11. class Var;
  12. class Trail;
  13. class Prog1Pred;
  14.  
  15. class JSxx
  16. {
  17.  
  18. public:
  19.     static int DEBUGGING;
  20.  
  21.     static Atom* ATOM_dot;
  22.     static Atom* ATOM_nil;
  23.     static Structure* NIL;
  24.     static Structure* CUT;
  25.  
  26.     static void indent(const int& n);
  27.  
  28.     static void main(std::vector<std::string>& args);
  29.     static Structure* CC(std::vector<Term> &args);
  30.  
  31. };
  32.     class TermVarMapping
  33.     {
  34.     private:
  35.         std::vector<Var*> varvar;
  36.         std::vector<std::string> vartext;
  37.         int size = 0;
  38.     public:
  39.         TermVarMapping(std::vector<Var*>& vv, std::vector<std::string>& vt, const int& vs);
  40.         void showanswer();
  41.     };
  42.  
  43.     class Term
  44.     {
  45.     public:
  46.         virtual bool unify(Trail* p, Term* p2) = 0;
  47.         virtual bool unify2(Trail* p, Structure* p2) = 0;
  48.         virtual Term* copy(Trail* p) = 0;
  49.         virtual void reset() = 0;
  50.         void debug();
  51.         virtual PrintStream* print(PrintStream* str) = 0;
  52.     };
  53.  
  54.     class Trail
  55.     {
  56.     private:
  57.         Term* head;
  58.         Trail* tcdr;
  59.         Trail* sofar;
  60.     public:
  61.         virtual ~Trail()
  62.         {
  63.             delete head;
  64.             delete tcdr;
  65.             delete sofar;
  66.         }
  67.  
  68.     private:
  69.         Trail(Term* h, Trail* t);
  70.     public:
  71.         Trail();
  72.         Trail* Note();
  73.         void Push(Term* x);
  74.         void Undo(Trail* whereto);
  75.     };
  76.  
  77.  
  78.     class Atom
  79.     {
  80.     private:
  81.         std::string atomname;
  82.     public:
  83.         Atom(const std::string& s);
  84.         virtual PrintStream* print(PrintStream* str);
  85.  
  86.         bool eqatom(Atom* t);
  87.     };
  88.  
  89.    class Structure : public Term
  90.    {
  91.     private:
  92.         int arity = 0;
  93.         Atom* fsym;
  94.         std::vector<Term*> args;
  95.     public:
  96.         virtual ~Structure()
  97.         {
  98.             delete fsym;
  99.         }
  100.  
  101.         Structure(const std::string& f);
  102.         Structure(Atom* f);
  103.         Structure(Atom* f, Term* a1);
  104.         Structure(Atom* f, Term* a1, Term* a2);
  105.         Structure(const std::string& f, Term* a1, Term* a2);
  106.         Structure(Atom* f, Term* a1, Term* a2, Term* a3);
  107.         Structure(Atom* f, std::vector<Term> &a);
  108.  
  109.  
  110.         PrintStream* print(PrintStream* str) override;
  111.  
  112.         virtual PrintStream* print(PrintStream* str, const std::string& open, const std::string& mid, const std::string& close);
  113.  
  114.         void reset() override;
  115.         bool unify(Trail* mach, Term* t) final override;
  116.         Term* copy(Trail* mach) final override;
  117.         Structure* copyNonvar(Trail* mach);
  118.     private:
  119.         Structure(Trail* mach, Structure* p);
  120.  
  121.     public:
  122.         bool unify2(Trail* mach, Structure* t) override;
  123.    };
  124.  
  125.     class Var : public Term
  126.     {
  127.     private:
  128.         Term* instance;
  129.         int varno = 0;
  130.         static int timestamp;
  131.     public:
  132.         virtual ~Var()
  133.         {
  134.             delete instance;
  135.         }
  136.  
  137.         Var();
  138.         PrintStream* print(PrintStream* str) override;
  139.         void reset() override;
  140.         bool unify2(Trail* mach, Structure* t) override;
  141.         bool unify(Trail* mach, Term* t) override;
  142.  
  143.         Term* copy(Trail* mach) override;
  144.     };
  145.  
  146.     class Goal
  147.     {
  148.     private:
  149.         Structure* head;
  150.         Goal* tail;
  151.  
  152.         Trail* mach;
  153.  
  154.     public:
  155.         virtual ~Goal()
  156.         {
  157.             delete head;
  158.             delete tail;
  159.             delete mach;
  160.         }
  161.  
  162.         Goal(Structure* h);
  163.         Goal(Structure* h, Goal* t);
  164.  
  165.         Goal* copy(Trail* mach);
  166.         Goal* append(Goal* l);
  167.  
  168.         virtual PrintStream* print(PrintStream* str);
  169.         /* returns a new Goal if coroutining or null if cutted */
  170.         virtual Prog1Pred* solve(Prog1Pred* p, const int& level, TermVarMapping* map);
  171.     };
  172.  
  173.  
  174.     class Clause
  175.     {
  176.     public:
  177.         Term* head;
  178.         Goal* tail;
  179.         virtual ~Clause()
  180.         {
  181.             delete head;
  182.             delete tail;
  183.         }
  184.  
  185.         Clause(Structure* h, Goal* t);
  186.         Clause(Structure* h, Structure* t);
  187.         Clause(Structure* h);
  188.  
  189.         Clause* copy(Trail* mach);
  190.  
  191.         virtual PrintStream* print(PrintStream* str);
  192.  
  193.         Clause* append(Structure* l);
  194.  
  195.     };
  196.  
  197.     class Prog1Pred
  198.     {
  199.     public:
  200.         Clause* pcar;
  201.         Prog1Pred* pcdr;
  202.         virtual ~Prog1Pred()
  203.         {
  204.             delete pcar;
  205.             delete pcdr;
  206.         }
  207.  
  208.         Prog1Pred(Clause* h);
  209.         Prog1Pred(Clause* h, Prog1Pred* t);
  210.         Prog1Pred(Clause* h, Clause* t);
  211.         Prog1Pred(Clause* h, Clause* h2, Clause* t);
  212.  
  213. /* returns the newly appended Prog1Pred*/
  214.         Prog1Pred* append(Clause* l);
  215.  
  216.         /* returns itself*/
  217.         Prog1Pred* prepend(Clause* h);
  218.     };
  219.  
  220.     static int test_program_append();
  221. }
  222.  
  223. //.cpp file code:
  224.  
  225. using namespace std;
  226.  
  227. int JSxx::DEBUGGING = 1;
  228. Atom* JSxx::ATOM_dot = new Atom(".");
  229. Atom* JSxx::ATOM_nil = new Atom("[]");
  230. Structure* JSxx::NIL = new Structure(ATOM_nil);
  231. Structure* JSxx::CUT = new Structure(new Atom("!"));
  232.  
  233. void JSxx::indent(const int& n)
  234. {
  235.     for(int i = 0; i < n; i++)
  236.     {
  237.         cout << "    ";
  238.     }
  239. }
  240.  
  241. void JSxx::main(std::vector<wstring>& args)
  242. {
  243.     test_program_append();
  244. }
  245.  
  246. Structure* JSxx::CC(vector<Term> &args)
  247. {
  248.     return new Structure(ATOM_dot,args);
  249. }
  250.  
  251. TermVarMapping::TermVarMapping(std::vector<Var*>& vv, std::vector<wstring>& vt, const int& vs)
  252. {
  253.     this->varvar = vv;
  254.     this->vartext = vt;
  255.     this->size = vs;
  256. }
  257.  
  258. void TermVarMapping::showanswer()
  259. {
  260.     if(size == 0)
  261.     {
  262.         cout << "yes\n";
  263.     } else
  264.     {
  265.         for(int i = 0; i < size; i++)
  266.         {
  267.             cout << vartext[i];
  268.             cout << " = ";
  269.             cout << varvar[i];
  270.             cout << "\n";
  271.         }
  272.     }
  273. }
  274.  
  275. void Term::debug()
  276. {
  277.     print(System::out);
  278. }
  279.  
  280. Trail::Trail(Term* h, Trail* t)
  281. {
  282.     this->head = h;
  283.     this->tcdr = t;
  284. }
  285.  
  286. Trail::Trail()
  287. {
  288.     this->head = nullptr;
  289.     this->tcdr = nullptr;
  290. }
  291.  
  292. Trail* Trail::Note()
  293. {
  294.     return (sofar);
  295. }
  296.  
  297. void Trail::Push(Term* x)
  298. {
  299.     sofar = new Trail(x, sofar);
  300. }
  301.  
  302. void Trail::Undo(Trail* whereto)
  303. {
  304.     for(; sofar != whereto; sofar = sofar->tcdr)
  305.     {
  306.         sofar->head->reset();
  307.     }
  308. }
  309.  
  310. Atom::Atom(const wstring& s)
  311. {
  312.     this->atomname = s;
  313. }
  314.  
  315. PrintStream* Atom::print(PrintStream* str)
  316. {
  317.     str->print(atomname);
  318.     return str;
  319. }
  320.  
  321. bool Atom::eqatom(Atom* t)
  322. {
  323.     return atomname == atomname;
  324. }
  325.  
  326. Structure::Structure(const wstring& f)
  327. {
  328.     this->fsym = new Atom(f);
  329.     this->arity = 0;
  330.     this->args.clear();
  331. }
  332.  
  333. Structure::Structure(Atom* f)
  334. {
  335.     this->fsym = f;
  336.     this->arity = 0;
  337.     this->args.clear();
  338. }
  339.  
  340. Structure::Structure(Atom* f, Term* a1)
  341. {
  342.     this->fsym = f;
  343.     this->arity = 1;
  344.     this->args = std::vector<Term*>{ a1 };
  345.     args[0] = a1;
  346. }
  347.  
  348. Structure::Structure(Atom* f, Term* a1, Term* a2)
  349. {
  350.     this->fsym = f;
  351.     this->arity = 2;
  352.     this->args = std::vector<Term*>{ a1, a2 };
  353. }
  354.  
  355. Structure::Structure(const wstring& f, Term* a1, Term* a2)
  356. {
  357.     this->fsym = new Atom(f);
  358.     this->arity = 2;
  359.     this->args = std::vector<Term*>{ a1, a2 };
  360. }
  361.  
  362. Structure::Structure(Atom* f, Term* a1, Term* a2, Term* a3)
  363. {
  364.     this->fsym = f;
  365.     this->arity = 3;
  366.     this->args = std::vector<Term*>{ a1, a2, a3 };
  367. }
  368.  
  369. Structure::Structure(Atom* f, vector<Term> &a)
  370. {
  371.     this->fsym = f;
  372.     this->args = a::clone();
  373.     this->arity = args.size();
  374. }
  375.  
  376. PrintStream* Structure::print(PrintStream* str)
  377. {
  378.     if(fsym == ATOM_dot && arity == 2)
  379.     {
  380.         return print(str,"[","|","]");
  381.     } else
  382.     {
  383.         return print(str,"(",",",")");
  384.     }
  385. }
  386.  
  387. PrintStream* Structure::print(PrintStream* str, const wstring& open, const wstring& mid, const wstring& close)
  388. {
  389.     fsym->print(str);
  390.     if(arity > 0)
  391.     {
  392.         str->print(open);
  393.         for(int i = 0; i < arity;)
  394.         {
  395.             args[i]->print(str);
  396.             if(++i < arity)
  397.             {
  398.                 str->print(mid);
  399.             }
  400.         }
  401.         str->print(close);
  402.     }
  403.     return str;
  404. }
  405.  
  406. void Structure::reset()
  407. {
  408. }
  409.  
  410. bool Structure::unify(Trail* mach, Term* t)
  411. {
  412.     return (t->unify2(mach,this));
  413. }
  414.  
  415. Term* Structure::copy(Trail* mach)
  416. {
  417.     return (copyNonvar(mach));
  418. }
  419.  
  420. Structure* Structure::copyNonvar(Trail* mach)
  421. {
  422.     return new Structure(mach,this);
  423. }
  424.  
  425. Structure::Structure(Trail* mach, Structure* p)
  426. {
  427.     this->fsym = p->fsym;
  428.     this->arity = p->arity;
  429.     this->args = p->arity == 0 ? nullptr : std::vector<Term*>(p->arity);
  430.     for(int i = 0; i < arity; i++)
  431.     {
  432.         args[i] = p->args[i]->copy(mach);
  433.     }
  434. }
  435.  
  436. bool Structure::unify2(Trail* mach, Structure* t)
  437. {
  438.     if(!(fsym->eqatom(t->fsym) && arity == t->arity))
  439.     {
  440.         return (false);
  441.     }
  442.     for(int i = 0; i < arity; i++)
  443.     {
  444.         if(!args[i]->unify(mach,t->args[i]))
  445.         {
  446.             return (false);
  447.         }
  448.     }
  449.     return (true);
  450. }
  451.  
  452. int Var::timestamp = 0;
  453.  
  454. Var::Var()
  455. {
  456.     this->instance = this;
  457.     this->varno = ++timestamp;
  458. }
  459.  
  460. PrintStream* Var::print(PrintStream* str)
  461. {
  462.     if(instance != this)
  463.     {
  464.         return instance->print(str);
  465.     } else
  466.     {
  467.         str->print("_");
  468.         str->print(varno);
  469.         return str;
  470.     }
  471. }
  472.  
  473. void Var::reset()
  474. {
  475.     instance = this;
  476. }
  477.  
  478. bool Var::unify2(Trail* mach, Structure* t)
  479. {
  480.     return (this->unify(mach, t));
  481. }
  482.  
  483. bool Var::unify(Trail* mach, Term* t)
  484. {
  485.     if(instance != this)
  486.     {
  487.         return (instance->unify(mach,t));
  488.     }
  489.     mach->Push(this);
  490.     instance = t;
  491.     return (true);
  492. }
  493.  
  494. Term* Var::copy(Trail* mach)
  495. {
  496.     if(instance == this)
  497.     {
  498.         mach->Push(this);
  499.         instance = new Var();
  500.     }
  501.     return (instance);
  502. }
  503.  
  504. Goal::Goal(Structure* h)
  505. {
  506.     this->head = h;
  507.     this->tail = nullptr;
  508.     mach = new Trail();
  509. }
  510.  
  511. Goal::Goal(Structure* h, Goal* t)
  512. {
  513.     this->head = h;
  514.     this->tail = t;
  515.     mach = new Trail();
  516. }
  517.  
  518. Goal* Goal::copy(Trail* mach)
  519. {
  520.     Goal tempVar(head->copyNonvar(mach), tail == nullptr ? nullptr : tail->copy(mach));
  521.     return (&tempVar);
  522. }
  523.  
  524. Goal* Goal::append(Goal* l)
  525. {
  526.     Goal tempVar(head, tail == nullptr ? nullptr : tail->append(l));
  527.     return (&tempVar);
  528. }
  529.  
  530. PrintStream* Goal::print(PrintStream* str)
  531. {
  532.     head->print(str);
  533.     if(tail != nullptr)
  534.     {
  535.         str->print(" ; ");
  536.         tail->print(str);
  537.     }
  538.     return str;
  539. }
  540.  
  541. Prog1Pred* Goal::solve(Prog1Pred* p, const int& level, TermVarMapping* map)
  542. {
  543.  
  544.     PrintStream* str = System::out;
  545.     if(DEBUGGING != 0)
  546.     {
  547.         indent(level);
  548.         str->print("solve@");
  549.         str->print(level);
  550.         str->print(": ");
  551.         this->print(str);
  552.         str->print("\n");
  553.     }
  554.  
  555.     Trail* trail = (mach == nullptr)?new Trail():mach;
  556.     Prog1Pred* retQ = p;
  557.     for(Prog1Pred* q = p; q != nullptr; q = q->pcdr)
  558.     {
  559.         Trail* t = trail->Note();
  560.         retQ = q;
  561.         Clause* next = q->pcar;
  562.         Clause* c = next->copy(trail);
  563.         trail->Undo(t);
  564.         if(DEBUGGING != 0)
  565.         {
  566.             indent(level);
  567.             str->print("  try:");
  568.             c->print(str);
  569.             str->print("\n");
  570.         }
  571.         if(head->unify(trail,c->head))
  572.         {
  573.             Goal* gdash = ((c->tail == nullptr) ? tail : c->tail->append(tail));
  574.             if(gdash == nullptr)
  575.             {
  576.                 map->showanswer();
  577.             } else
  578.             {
  579.                retQ = gdash->solve(p, level + 1, map);
  580.             }
  581.         } else
  582.         {
  583.             if(DEBUGGING != 0)
  584.             {
  585.                 indent(level);
  586.                 str->print(" nomatch.\n");
  587.             }
  588.         }
  589.         trail->Undo(t);
  590.     }
  591.     return retQ;
  592. }
  593.  
  594. Clause::Clause(Structure* h, Goal* t)
  595. {
  596.     this->head = h;
  597.     this->tail = t;
  598. }
  599.  
  600. Clause::Clause(Structure* h, Structure* t)
  601. {
  602.     this->head = h;
  603.     this->tail = new Goal(t);
  604. }
  605.  
  606. Clause::Clause(Structure* h)
  607. {
  608.     this->head = h;
  609.     this->tail = nullptr;
  610. }
  611.  
  612. Clause* Clause::copy(Trail* mach)
  613. {
  614.     Clause tempVar(head->copyNonvar(mach), tail == nullptr ? nullptr : tail->copy(mach));
  615.     return (&tempVar);
  616. }
  617.  
  618. PrintStream* Clause::print(PrintStream* str)
  619. {
  620.     head->print(str);
  621.     str->print(" :- ");
  622.     if(tail == nullptr)
  623.     {
  624.         str->print("true");
  625.     } else
  626.     {
  627.         tail->print(str);
  628.     }
  629.     return str;
  630. }
  631.  
  632. Clause* Clause::append(Structure* l)
  633. {
  634.     if(tail == nullptr)
  635.     {
  636.         tail = new Goal(l);
  637.     } else
  638.     {
  639.         Goal tempVar(l);
  640.         tail = tail->append(&tempVar);
  641.     }
  642.     return this;
  643. }
  644.  
  645. Prog1Pred::Prog1Pred(Clause* h)
  646. {
  647.     this->pcar = h;
  648.     this->pcdr = nullptr;
  649. }
  650.  
  651. Prog1Pred::Prog1Pred(Clause* h, Prog1Pred* t)
  652. {
  653.     this->pcar = h;
  654.     this->pcdr = t;
  655. }
  656.  
  657. Prog1Pred::Prog1Pred(Clause* h, Clause* t)
  658. {
  659.     this->pcar = h;
  660.     this->pcdr = new Prog1Pred(t);
  661. }
  662.  
  663. Prog1Pred::Prog1Pred(Clause* h, Clause* h2, Clause* t)
  664. {
  665.     this->pcar = h;
  666.     this->pcdr = new Prog1Pred(h2, t);
  667. }
  668.  
  669. Prog1Pred* Prog1Pred::append(Clause* l)
  670. {
  671.     if(pcdr == nullptr)
  672.     {
  673.         return pcdr = new Prog1Pred(l);
  674.     } else
  675.     {
  676.         return pcdr->append(l);
  677.     }
  678. }
  679.  
  680. Prog1Pred* Prog1Pred::prepend(Clause* h)
  681. {
  682.     pcdr = new Prog1Pred(pcar, pcdr);
  683.     pcar = h;
  684.     return this;
  685. }
  686.  
  687. int <missing_class_definition>::test_program_append()
  688. {
  689. /*Psuedovars for source translation */
  690.     Var* VAR_X = new Var();
  691.     Var* VAR_L = new Var();
  692.     Var* VAR_M = new Var();
  693.     Var* VAR_N = new Var();
  694.     Var* VAR_I = new Var();
  695.     Var* VAR_J = new Var();
  696.  
  697.  
  698.     Atom* APPEND3 = new Atom("append_3");
  699.  
  700.  
  701.  
  702. /*
  703. Test normally:
  704.  
  705.    append_3([],X,X).
  706.    append([X|L],M,[X|N]):- append(L,M,N).
  707. */
  708.     Structure tempVar(APPEND3, NIL, VAR_X, VAR_X);
  709.     Clause* c1 = new Clause(&tempVar);
  710.     Structure tempVar2(APPEND3, CC(VAR_X, VAR_L),VAR_M, CC(VAR_X,VAR_N));
  711.     Structure tempVar3(APPEND3,VAR_L,VAR_M,VAR_N);
  712.     Clause* c2 = new Clause(&tempVar2, &tempVar3);
  713.  
  714.     Prog1Pred* test_program_normally = new Prog1Pred(c1, c2);
  715.  
  716.  
  717. /*
  718. Test reversed:
  719.  
  720.    append([X|LL],M,[X|N]):- append(LL,M,N).
  721.    append_3([],X,X).
  722. */
  723.     Prog1Pred* test_program_reversed = new Prog1Pred(c2, c1);
  724.  
  725.  
  726. /*
  727.   Test Cut:
  728.  
  729.     append_3([],X,X):- !.
  730.     append([X|LL],M,[X|N]):- append(LL,M,N).
  731. */
  732.  
  733.     Trail tempVar4();
  734.     Clause* c3 = c1->copy(&tempVar4);
  735.     c3->append(CUT);
  736.  
  737.     Prog1Pred* test_program_cut = new Prog1Pred(c3, c2);
  738.  
  739.  
  740.  
  741.     std::vector<Var*> varvar = { VAR_I, VAR_J };
  742.     std::vector<wstring> varname = { "I", "J" };
  743.     TermVarMapping* var_name_map = new TermVarMapping(varvar, varname, 2);
  744.  
  745.     /*
  746.        ?- append_3(I,J,[1,2,3]).
  747.     */
  748.     Structure tempVar5(APPEND3, VAR_I, VAR_J, CC(new Structure("1"), CC(new Structure("2"), CC(new Structure("3"), NIL))));
  749.     Goal* g1 = new Goal(&tempVar5);
  750.  
  751.     cout << "=======Append with normal clause order:\n";
  752.     cout << g1->solve(test_program_normally, 0, var_name_map);
  753.  
  754.     cout << "\n=======Append with reversed clause order:\n";
  755.     cout << g1->solve(test_program_reversed, 0, var_name_map);
  756.  
  757.     cout << "\n=======Append with a cut:\n";
  758.     cout << g1->solve(test_program_cut, 0, var_name_map);
  759.  
  760.     return (0);
  761. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement