%%%%%%%%%%%%%%%
%
%   orl(C, Ain, Bin, Aout, Bout) - Separate a conjunction into the
%   conjuncts that are disjunctions (Bout) and the others (Aout).
%
%%%%%%%%%%%%%%%

orl(X and Y, Ain, Bin, Aout, Bout) :-
    !,
    orl(X, Ain, Bin, A1, B1),
    orl(Y, A1, B1, Aout, Bout).
orl(F1 or F2, Ain, Bin, Ain, Bout) :-
    !,
    andon(Bin, F1 or F2, Bout).
orl(F, Ain, Bin, Aout, Bin) :-
    andon(Ain, F, Aout).

%%%%%%%%%%%%%%%
%
%   andl(D, Ain, Bin, Aout, Bout) - Separate a disjunction (D) into the
%   disjuncts that are conjunctions (Bout) and the others (Aout).
%
%%%%%%%%%%%%%%%

andl(X or Y, Ain, Bin, Aout, Bout) :-
    !,
    andl(X, Ain, Bin, A1, B1),
    andl(Y, A1, B1, Aout, Bout).
andl(F1 and F2, Ain, Bin, Ain, Bout) :-
    !,
    oron(Bin, F1 and F2, Bout).
andl(F, Ain, Bin, Aout, Bin) :-
    oron(Ain, F, Aout).
%%%%%%%%%%%%%%%
%
%    ofc(X, F, Fin, Nin, Fout, Nout) - Separate a conjunction into the
%    conjuncts in which X occurs free (Fout) and those in which it
%    doesn't (Nout).
%
%%%%%%%%%%%%%%%

ofc(Z, G1 and G2, Fin, Nin, Fout, Nout) :-
    !,
    ofc(Z, G1, Fin, Nin, F1, N1),
    ofc(Z, G2, F1, N1, Fout, Nout).
ofc(Z, F, Fin, Nin, Fout, Nin) :-
    free(Z, F),
    !,
    andon(Fin, F, Fout).
ofc(_, F, Fin, Nin, Fin, Nout) :-
    andon(Nin, F, Nout).
/* 
   cnfass - convert to conjunctive normal form and left
   associate 'and' and 'or'.  Handles and, or, ~, imp, iff  
*/   

cnfass(X, Y) :-
   !, 
   cnf(X, X1), 
   assand(X1, [], X2), 
   recassor(X2, Y).
/* 
   dnfass - convert to disjunctive normal form and left
   associate 'or' or 'and'.  Handles or, and, ~, imp, iff  
*/   

dnfass(X, Y) :-
   !, 
   dnf(X, X1), 
   assor(X1, [], X2), 
   recassand(X2, Y).

/* 
   recassor - left associate the 'or' subtrees whose ancestors
   are all 'and'.  
*/

recassor(and(X, Y), and(X1, Y1)) :-
    !, 
    recassor(X, X1), 
    recassor(Y, Y1).
recassor(X, Y) :-
    assor(X, [], Y).

/* 
   recassand - left associate the 'and' subtrees whose ancestors
   are all 'or'.  
*/

recassand(or(X, Y), or(X1, Y1)) :-
    !, 
    recassand(X, X1), 
    recassand(Y, Y1).
recassand(X, Y) :-
    assand(X, [], Y).
%%%%%%%%%%%%%%%
%
%    nnfuq(Fin, Fout) -  convert to nnf then make quantified variables
%    unique.
%
%%%%%%%%%%%%%%%

nnfuq(Fin, Fout) :-
    !,
    nnf(Fin, F1),
    renamevars(F1, uu, [], _, Fout).

/*
    newvarslist(Fin,Fout) - It is assumed that Fin is a conjunction
    of disjunctions (a list of clauses).  For each conjunct, call 
    "newvars" to rename the variables.
*/

newvarslist(X and Y,X1 and Y1) :-
    !,
    newvarslist(X,X1),
    newvarslist(Y,Y1),
    !.
newvarslist(X,Y) :-
    newvars(X,Y),
    !.

