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

/* Based on the Prolog interpreter coded in C++ by Alan Mycroft.
   http://www.cl.cam.ac.uk/~am21/research/funnel/prolog.c
*/

/* Create two clauses defining append(), and then test it with the
   goal ?- append(I, J, [1,2,3])
*/


import java.util.*;


public class Test1
{

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

    // clause 1: append([], X, X).
    Functor f_nil = new Functor("nil");
    Term v_x = new Var("X", t);
    Functor lhs1 = new Functor("app", f_nil, v_x, v_x);
    Clause c1 = new Clause(lhs1);


    /* clause 2: append([X|L], M, [X|N]) :-
                     append(L, M, N).
    */
    Term v_l = new Var("L", t);
    Term v_m = new Var("M", t);
    Term v_n = new Var("N", t);
    Functor rhs2 = new Functor("app", v_l, v_m, v_n);
    Functor lhs2 = new Functor("app", 
        new Functor("cons", v_x, v_l), v_m,
                 new Functor("cons", v_x, v_n));
    Clause c2 = new Clause(lhs2, rhs2);


    // goal: ?- append(I, J, [1,2,3]).
    Functor n1 = new Functor("1");
    Functor n2 = new Functor("2");
    Functor n3 = new Functor("3");
    Var v_i = new Var("I", t);
    Var v_j = new Var("J", t);
    Functor goal = new Functor("app", v_i, v_j,
        new Functor("cons", n1,
             new Functor("cons", n2, 
               new Functor("cons", n3, f_nil))));


    List<Var> vs = Arrays.asList(v_i, v_j);


    System.out.println("Normal clause order:\n");
    List<Clause> cls = Arrays.asList(c1, c2);
    Engine eng = new Engine(cls, t);
    eng.solve(goal, vs);


    System.out.println("\nReversed clause order:\n");
    cls = Arrays.asList(c2, c1);
    eng = new Engine(cls, t);
    eng.solve(goal, vs);

  }  // end of main()

}  // end of Test1 class

