%%% ============================ %%% CMPU-181, Spring 2013 %%% April 3, 2013 %%% ============================ %%% swipl <--- starts the Prolog interpreter %%% halt. <--- terminates swipl %%% The proposition A is true: a. %%% If A is true, then so is B: b :- a. %% ======================= %% A theory of ancestry %% ======================= %% anc(X,Y) -- X is an ancestor of Y %% par(X,Y) -- X is a parent of Y %% ----------------------- %% Rules in prolog employ UNIVERSALLY QUANTIFIED variables: %% for all X and Y: if X is a parent of Y, then X is an ancestor of Y %% par(X,Y) => anc(X,Y) anc(X,Y) :- par(X,Y). %% for all X, Y and Z: if X is an ancestor of Y, and Y is a parent of Z, %% then X is an ancestor of Z. %% anc(X,Y) AND par(Y,Z) => anc(X,Z) anc(X,Z) :- anc(X,Y), par(Y,Z). %% some facts about particular people par(amb,bert). %% Amberley is a parent of Bertrand par(bert,kate). %% Bertrand is a parent of Kate %% NOTE: X, Y and Z are upper-case VARIABLES %% amb, bert, kate are lower-case CONSTANTS %% anc(amb,kate) ??? %% anc(amb,bert) ??? %% anc(X,kate) <--- i.e., is there a value for X that makes anc(X,kate) true? %% Variables in a *query* are EXISTENTIALLY QUANTIFIED... %% "Does there EXIST a value for X such that ...?" %% anc(amb,Y) %% LIST CONCATENATION %% --------------------------------------- %% conc(Xs,Ys,Zs) holds if Zs is the concatenation of Xs and Ys. %% Base Case: The first list is empty %% For all Ys, if you concatenate [] onto Ys, you end up with Ys. conc([], Ys, Ys). %% Recursive Case: The first list is non-empty conc([X|Xs], Ys, [X|Zs]) :- conc(Xs,Ys,Zs).