Computer Science 60
Principles of Computer Science
Fall 2008


Assignment 8: Prolog - yes! [100 points]
Due Sunday, November 2 by 11:59 PM


Formatting and commenting... Please use the standard commenting convention of your name, file name, and date in each file. In addition, be sure to include a comment for each function that you implement. Finally, please be sure to name your files and functions as noted by the problems.... The grading procedure for this assignment is partially automated and files and functions whose spellings differ from those below will cause our scripts to "barf".

Part 0: Getting started... [0 points]


First, you will want to be sure you can run prolog -- either from your computer or from one of the CS machines.
You can obtain a version named SWI prolog from for either Windows or Mac OS X. The Mac version requires you to have X11 (for the graphical interface) installed (if you have a recent enough Mac, you probably do). The location of the program after it installs on a Mac is /opt/local/bin/swipl. You'll need to run it from the command line (similar to the instructions below).

If you're on the Lab Macs    You will need to open a terminal window and change directory to the location of your part1.pl and part2.pl files -- you can download those from the top-level assignments page.

To do this, follow these steps:

Part 1: The Simpsons' family tree... [40 Points]

This part asks you to write predicates that define family relationships and answer questions about the Simpsons' family tree -- or, at least, the version of the family tree that appears in part1.pl.

You should download that part1.pl file -- you will need to change its name to part1.pl from part1.txt. In that file, below the rules for the family tree, you will see a place to write the following rules: You may also add any additional helper rules that you wish, including anything that we looked at in class. However, please don't change the facts about parenthood/relationships among the Simpsons!

Prolog reminders

Recall that "," is the symbol for AND, ";" is the SYMBOL for OR, and "\+" is the symbol for NOT, while \== is the symbol for "not equal." Please look at the class notes for other (admittedly, sometimes crazy) Prolog syntax. Another excellent resource is Learn Prolog Now!, a site that explains the language concisely and with a number of examples.

Please submit your solutions as part1.pl under assignment #8.

Prolog output formatting

Prof. Keller has found some of the settings that control how Prolog formats its output: the following describes a few of those settings you may want to change.

You may have noticed that when answer variables get bound to long lists, 
swipl shows the list with '...' after about 10 elements, 
which can be annoying.

To see the entire list, simply type

  w

following the output, for example:

?- range(1, 50, X).

X = [1, 2, 3, 4, 5, 6, 7, 8, 9|...] w

X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
     12, 13, 14, 15, 16, 17, 18, 19, 20, 
     21, 22, 23, 24, 25, 26, 27, 28, 29, 
     30, 31, 32, 33, 34, 35, 36, 37, 38, 
     39, 40, 41, 42, 43, 44, 45, 46, 47, 
     48, 49, 50]

Yes


To change the limit on the number of elements once and for all, copy the 
following into your source file:

:- set_prolog_flag(toplevel_print_options, 
     [quoted(true), portray(true), attributes(portray), 
     max_depth(999), priority(699)]).

You may use any other value for max_depth, including 0, which will 
place no limit on the depth of the expression. The default value of 
max_depth is set at 10. The values of the other attributes are just 
the default ones.


For longer term use, you may also put this incantation in your initialization 
file, which resides in a system-dependent place. See the manual for info on this:

http://gollem.science.uva.nl/SWI-Prolog/Manual/initfile.html



Part 2: Lists in Prolog [60 Points]


Here, you will write four Prolog predicates that express relationships about lists. Please submit these rules in one file called part2.pl. This link has a starting point with placeholders. Again, you'll need to change the extension from txt to pl in this case. You may create and use any helper rules that you like. In particular, some of the rules that we saw in class may be helpful here!

Extra credit: up to + 20 points

You may recall that in scheme we wrote a function called powerset which took as input a list S (imagined to be a set of distinct items) and then returned the powerset of S: the set of all subsets S. The scheme version used an algorithm based on "weirdo analysis" in its implementation.

For optional extra credit in prolog, implement an even-better version! Write a rule called subset(List1, List2) which takes two lists List1 and List2 as input and infers whether List1 is a subset of List2. (Notice that since we're using lists to represent sets, we're implicitly assuming that any given element appears at most once in a list. Keep in mind, however, that you should not assume anything about the order in which the elements are given in the lists.) No weirdo analysis is needed here... . Your predicate will simply describe to prolog how to infer that one set is a subset of another!

You may wish to use one or more helper rules. Your subset rule must work when it is given two actual lists and it must also work when the first argument is a variable and the second argument is an actual list. It need not work in the case that the second argument is a variable (although it's not hard to take care of that case either).

For example:

| ?- subset([1, 2], [2, 1, 3]).
yes

| ?- subset([1, 2], [1]).
no

| ?- subset(X, [1, 2]).
X = [] ;

X = [1] ;

X = [1,2] ;

X = [2] ;

X = [2,1] ;

Notice in the last example, the same subset appeared more than once (as [1, 2] and [2, 1]). This is fine, as long as your rule eventually stops and all correct answers are produced at least once. Moreover, the order in which the subsets are found and the order of the actual elements in the lists is unimportant. Submit your code at the bottom of your part2.pl file.