[2 problems, 60 points total]
This assignment consists of one Prolog program and one Java program with a textual answer embedded as comments.. To simplify handling, please submit them as if they were two separate assignments, numbered 8 and 9, respectively.
8. [30 points]
This problem entails coding information and queries into predicate logic using Prolog. Your file should be a program which will load and run in Prolog. (The executable on turing is called 'bp'. As always, you may need to ask some questions about the formulation of certain types of information. The names are in lower-case for convenience in working with Prolog; to capitalize them would require that we include them in single quotes.)
a. Code the following predicates about local restaurants in Prolog:
serves predicate:
american restaurants serve salad, steak, sandwiches, burgers, and fried chicken.
burger_place restaurants serve burgers, fries, and salad.
cajun restaurants serve rice, beans, gumbo, and sausage.
chinese restaurants serve eggrolls, rice, shrimp, soup, and noodles.
indian restaurants serve papadam, bagan_bharta, rice, tandoori, and naan.
italian restaurants serve salad, pasta, cioppino, snapper, and bread.
japanese restaurants serve sashimi, rice, tempura, and noodles.
mediterranean restaurants serve gyros, humus, pita, and falafel.
mexican restaurants serve tacos, beans, rice, enchiladas, and snapper.
pizza_place restaurants serve pizza, salad, garlic_bread
seafood_place restaurants serve salad, shrimp, snapper, bouillabaisse, and clams.
thai restaurants serve eggrolls, rice, noodles, and pad_thai.
[Note: There are two ways in which you can define a predicate such as serves: One on hand, you can enumerate each relationship:
serves(american, salad).
serves(american, steak).
serves(american, sandwiches).
etc.
A more concise way is to use lists in an auxiliary predicate, in conjunction with the built-in member predicate which enumerates the relationships.
servesAll(american, [salad, steak, sandwiches, burgers, fried_chicken]).serves(Kind, Dish) :- servesAll(Kind, Dishes), member(Dish, Dishes).
(end of note)]
dish predicate:
vegetarian dishes:
beans, bagan_bharta, enchiladas, falafel, humus, pizza, salad, soup, tempura, and all starch dishes
meat dishes:
burgers, enchiladas, gyros, pad_thai, pizza, steak, sandwiches, fried_chicken, tacos, tandoori
seafood dishes:
eggrolls, snapper, cioppino, sashimi, shrimp, bouillabaisse, tempura
starch dishes:
naan, papadam, bread, rice, noodles, pita, garlic_bread, pasta, fries
cuisine and location predicate:
arrufos is an italian restaurant in claremont.
bamboo_yuan is a chinese restaurant in claremont.
bombay is an indian restaurant in ontario.
chilis is an american restaurant in laverne.
don_salsa is a mexican restaurant in claremont.
el_gato is a mexican restaurant in upland.
full_house is a chinese restaurant in upland.
islands is a burger place in montclair.
kishi is a japanese restaurant in upland.
nirvana is an indian restaurant in montclair.
nogi is a japanese restaurant in claremont.
orchid_garden is a thai restaurant in laverne.
pizza_n_such is a pizza_place restaurant in claremont.
rosas is an italian restaurant in ontario.
sacas is a mediterranean restaurant in claremont.
sanamluang_cafe is a thai restaurant in pomona.
shrimp_house is a seafood_place restaurant in claremont.
tutti_mangia is an italian restaurant in claremont.
warehouse is a pizza_place restaurant in laverne.
yiannis is a mediterranean restaurant in claremont.
county predicate:
claremont is in los_angeles_county.
laverne is in los_angeles_county.
montclair is in san_bernardino_county.
ontario is in san_bernardino_county.
pomona is in los_angeles_county.
upland is in san_bernardino_county.
b. Code the following queries in Prolog and show their execution relative to the database above. In some cases, you may find it helpful to define auxiliary predicates to assist.
Each answer should be encoded as a predicate which gives the whole set of entities as a list. The following is an example of how this would be done for the first query:
/* 1. Which restaurants are in claremont? */ /* inClaremont(Restaurant) is true iff Restaurant is in claremont */ inClaremont(Restaurant) :- restaurant(Restaurant, _, claremont). /* ans1(Restaurants) gives the set of restaurants in claremont */ ans1(Restaurants) :- setof(Restaurant, inClaremont(Restaurant), Restaurants).
The answer to the query ans1(X) would be:
X = [arrufos,bamboo_yuan,don_salsa,nogi,pizza_n_such,sacas,shrimp_house, tutti_mangia,yiannis]
9. [30 points]
Devise an appropriate loop invariant (for the point marked by /* */) and use it to show that the following function returns the cube of its argument, provided that the argument is non-negative.
static long cube(long N) { long i, x, y, z; x = 0; y = 1; z = 6; for( i = 0; /* */ i < N; i++ ) { x = x + y; y = y + z; z = z + 6; } return x; }
Devise an appropriate energy function and use it to show that the program terminates for arguments N >= 0. What invariant do you need to show that your function really is an energy function?
(This is how Babbage's difference engine might have computed cube. To see how the method is derived, execute the following in rex:
cubes = map((i) => i*i*i, from(0)); // sequence of all cubesfirst_diffs = diff(-, cubes); // first differences of cubessecond_diffs = diff(-, first_diffs); // second differences of cubesthird_diffs = diff(-, second_diffs); // third differences of cube
Then look at each of the sequences defined.)
The proof of invariance should be inserted as a comment in the .java file.
Once you have your invariant, include it as the argument to an assert predicate in the code which is included in the loop test as shown below and test its validity by running the code on various input values, say 0 to 25, checking that the invariant never fails:
assert(.... your invariant ....) && i < N
Here the definition of assert is:
static boolean assert(boolean PredicateValue) { if( PredicateValue ) return true;System.err.println("*** assertion failed ***"); System.exit(1); return false; }
Thus a true assertion will have no effect on the test which follows, while a false assertion will terminate the program immediately, printing a message.