
// Test2.java
// Andrew Davison, ad@coe.psu.ac.th, Feb 2020

/* Create two clauses defining member(), and then test it with the
   goals ?- mem(M, ["j", 'i', 'm'])
   and   ?- mem("p", ["j", 'i', 'm']).
*/


import java.util.*;


public class Test2
{

  public static void main(String[] args)
  {
    Trail t = new Trail();

    // clause 1: member(X, [X|R])
    Term v_x = new Var("X", t);
    Term v_r = new Var("R", t);
    Functor lhs1 = new Functor("mem", v_x,
                      new Functor("cons", v_x, v_r) );
    Clause c1 = new Clause(lhs1);


    /* clause 2: member(X, [Y|R]) :-
                     member(X, R).
    */
    Term v_y = new Var("Y", t);
    Functor rhs2 = new Functor("mem", v_x, v_r);
    Functor lhs2 = new Functor("mem", v_x,
                     new Functor("cons", v_y, v_r) );
    Clause c2 = new Clause(lhs2, rhs2);

    // initialize engine
    List<Clause> cls = Arrays.asList(c1, c2);
    Engine eng = new Engine(cls, t);


    // ----------------------------------------
    // 1st goal: ?- mem(M, ["j", 'i', 'm']).
    Functor f_nil = new Functor("nil");
    Functor fj = new Functor("j");
    Functor fi = new Functor("i");
    Functor fm = new Functor("m");
    Var v_m = new Var("M", t);

    Functor goal1 = new Functor("mem", v_m,
        new Functor("cons", fj,
             new Functor("cons", fi, 
               new Functor("cons", fm, f_nil))));

    List<Var> vs = Arrays.asList(v_m);

    eng.solve(goal1, vs);


    // ----------------------------------------
    // 2nd goal: ?- mem("p", ["j", 'i', 'm']).
    Functor fp = new Functor("p");
    Functor goal2 = new Functor("mem", fp,
        new Functor("cons", fj,
             new Functor("cons", fi, 
               new Functor("cons", fm, f_nil))));

    eng.solve(goal2, null);

  }  // end of main()

}  // end of Test2 class

