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/cs252/lp1lab.
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/cs252/lp1lab/').
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.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.Save this Prolog.scala file. In a shell window, type
scala -i Prolog.scala
When you get a prompt, type these commands:
import Prolog._
val member = new Predicate
member('X, 'X::'Xs)!
member('X, 'Y::'Ys) :- member('X, 'Ys)
member('X, 'john :: 'mary :: 'nil)?
more
more
What happens?
As you can see, the implementor of this interpreter cleverly overloaded
operators ! (fact), :- (provided that) and
? (query).
Note the single quote that is used for variables. These are Scala "atoms".
Look inside the source code.
? operator defined?! and not . for
the end of fact?'array('object). What is your program, and what does it print?