
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.prologconsult('family.prolog').consultparent(charles1, X). X = charles2 ; X = catherine ; X = james2.
; after each responsemother(X, Y) :- female(X), parent(X, Y).
:- as “provided that” or “if”mother(sophia, george1). true. mother(X, sophia). X = elizabeth. mother(X, charles1). false.
class(person). extends(student, person). extends(employee, person). extends(manager, employee). type(X) :- class(X). type(X) :- extends(X, _). subtype(X, Y) :- extends(X, Y). subtype(X, Y) :- extends(X, Z), subtype(Z, Y). subtype(array(X), array(Y)) :- subtype(X, Y), type(X), type(Y).
subtype(array(manager), array(person)). true.
array means. charles1 or person means eitherarray (arity 1) or charles1 (arity 0)Xparent(X, sophia):- T1, ..., Tn. (H, Ti are terms)
:- is omitted
This lab requires SWI Prolog. In Linux, sudo apt-get install swi-prolog. Read their documentation for Mac OS X and Windows instructions, or just install in a Linux virtual machine.
Note: Didn't install the software before the lab? Try to use https://swish.swi-prolog.org/
Download the family.prolog file and put it into your home directory.
Start SWI Prolog:
In the Prolog shell, type
consult('family.prolog').
This loads the family.prolog 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/lab17/').
Note that there is no space after cd, the quotes are single quotes, and there is a period at the end of the command.
charles1 and the parents of george1. Which queries did you issue, and what was the result?grandparent predicate. Add it to family.prolog. 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.prolog
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.guitracer.and then run a
trace. To switch back to the command line tracer, use noguitracer.