male(james1). male(charles1). male(charles2). male(james2). male(george1). female(catherine). female(elizabeth). female(sophia). parent(james1, charles1). parent(james1, elizabeth). parent(charles1, charles2). parent(charles1, catherine). parent(charles1, james2). parent(elizabeth, sophia). parent(sophia, george1).
family.plconsult(family).consultparent(charles1, X). X = charles2 ; X = catherine ; X = james2 ; fail.
; after each responsemother(X, Y) :- female(X), parent(X, Y).
:- as “provided that” or
“if”mother(sophia, george1). true. mother(X, sophia). X = elizabeth ; fail. mother(X, charles1). fail.
[][H | T][john | [mary | [pat | []]]] is [john,
mary, pat]member(X,[X|_]). member(X,[_|T]) :- member(X,T).
member(mary, [john, mary, pat]). true ; fail member(ann, [john, mary, pat]). fail ; member(X, [john, mary, pat]). X = john ; X = mary ; X = pat ; fail.
append(X, Y, Z) means “It is true that Z
is X appended with Y”append([], Ys, Ys). append([X | Xs], Ys, [X | Zs]) :- append(Xs, Ys, Zs).
append([john], [mary, pat], Z). Z = [john, mary, pat]. append(X, Y, [john, mary, pat]). X = [], Y = [john, mary, pat] ; X = [john], Y = [mary, pat] ; X = [john, mary], Y = [pat] ; X = [john, mary, pat], Y = [] ; fail.
extends(employee, object). extends(manager, employee). extends(string, object). subtype1(X, Y) :- extends(X, Y). subtype1(array(X), array(Y)) :- subtype1(X, Y). subtype(X, Y) :- subtype1(X, Y). subtype(X, Y) :- subtype1(X, Z), subtype(Z, Y).
subtype(array(manager), array(object)). true ; fail.
array means. charles1 or manager means
eitherarray (arity 1) or charles1 (arity 0)Xfun(X,
const):-
T1, ..., Tn. (H,
Ti are terms)
:- is omitted
This lab requires SWI Prolog.
Download and install for your platform (Linux, Mac OS X, WIndows). In my
favorite operating system, all I had to do is type sudo apt-get install
swi-prolog. Your mileage may vary.
Download the family.pl file and put it into a
directory such as /home/me/cs152/lab19.
Start SWI Prolog:
In the Prolog shell, type
consult(family).
This loads the family.pl facts into Prolog. Note the period at the end of the command.
If you get a message that the file was not found, perhaps you are not in the right directory. Run the cd command, like this:
cd('/home/me/cs152/lab7/').
Note that there is no space after cd, the quotes are single quotes, and there is a period at the end of the command.
At least these were the right steps 18 months ago. In your lab report, tell me what worked for you.
charles1 and the
parents of george1. Which queries did you issue, and what was
the result?grandparent predicate. Add it to
family.pl. Reload the file again with consult.
What is the rule defining your predicate? Which query yields the
grandparents of sophia? The grandchildren of
james1? What are the results? sibling predicate in the same way. What query did
you use to test it?family.pl:
ancestor(X, Y) :- parent(X, Y). ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
Which query yields the ancestors of sophia? The descendants
of james1? What are the results?
trace.
(Remember the period at the end.)
Now issue the query
ancestor(X, sophia).
Each time execution halts with a ? prompt, hit Enter. Each time you get another answer, type a semicolon (;) as you did previously.
What happens?
Turn debugging off by typing
nodebug.
ancestor to
ancestor(X, Y) :- ancestor(Z, Y), parent(X, Z).
Intuitively, this should not change the behavior of ancestor.
Reload family and try out
ancestor(X, sophia).
Be sure to enumerate all answers.
What happens?
(Hit a to abort.)
parent facts in family.pl. In prolog, a line
starting with % is a comment.