%% -*- prolog -*-

father(vader, luke).
father(soldier,vader).
father(vader,leia).

mother(amidala, luke).
mother(amidala,leia).

grandfather(G, C) :- father(G, X), parent(X, C).

parent(P,C) :- father(P,C).
parent(P,C) :- mother(P,C).

ancestor(A,C) :- parent(A,C).
ancestor(A,C) :- parent(A,X), ancestor(X,C).

%% define Peano Arithmetic in Prolog

plus(z,X,X).			% 0+x=x
plus(s(X),Y,s(Z)) :- plus(X,Y,Z). % (x+1)+y=(z+1) if x+y=z

sub(X,Y,Z) :- plus(Y,Z,X).

mult(z,_,z).			% 0*x=0
mult(s(X),Y,Z) :- 		% (x+1)*y=z if y+w=z and x*y=w
	plus(Y,W,Z), mult(X,Y,W).

%%% avoid overly complex base cases like:
%% mult(s(z),X,X).
%% mult(X,z,z).
%% mult(X,s(z),X).

lessthan(X,Y) :- plus(X,s(_),Y).

%%% In Prolog, write list [a,b,c,d],
%%% shorthand for .(a,.(b,.(c,.(d,[]))))
%%% can also abbreviate [a,b,c,...] as [a,b,c|...]

%% define append relation
app([],X,X).
app([E|X],Y,[E|Z]) :- app(X,Y,Z).

