CS 327: Artificial Intelligence

Assignment 7: Playing with Prolog

This assignment is technically due at 5 PM on Tuesday, 2/27/01. I strongly recommend finishing it early, before the weekend if possible, as it is not a long assignment. Go to lab during class time on Friday and try it. Get it out of the way so that you're free to prepare for the exam next Wednesday if you need to :)

I will be checking my e-mail intermittently while I'm gone.

To start up Prolog, type "pl" at any UNIX prompt.

Problem 1: (10 points)

Write Prolog clauses that define the predicate sorted(L), which is true if and only if list L is sorted in ascending order. Here is some sample output:

?- sorted([1,2,3,4,7]).

Yes
?- sorted([2,3,4,1,7]).

No

Here are two hints:

Hint 1:
The Prolog predicate "<" checks if one quantity is less than another. For example:

?- <(3,5).

Yes
?- <(5,3).

No

Hint 2:
In class, we used the Prolog header/tail notation only on the left side of an implication. It can also can be used on the right side, or both. For example, here is a Prolog predicate that is true if and only if Y is the rest of list X:

- [user].
|    rest(X,Y) :- [A | B] = X, B = Y.
|    % user compiled 0.01 sec, 380 bytes

Yes
?- rest([a,b,c],Q).

Q = [b, c] ;

No
?- rest([a,b,c,d,e],Q).

Q = [b, c, d, e] ;

No

Make sure to name your program sorted.pl, and submit it using hsp.

To give you a sense of scale: my solution consisted of two lines of Prolog code. Yours should be roughly the same (2 plus or minus 5).

Problem 2: (10 points)

Take the wumpus code found in /Accounts/courses/cs327/prolog/wumpus.pl, and try it out. Copy it to your own directory, then in Prolog type

consult('wumpus.pl').
deadWumpus(X).

Prolog will start showing you varying ways to solve the problem. When it shows you the first one, hit the "w" key to get read of the "..." and give you the full output. Hit a semicolon to see more results, and an "a" to abort the program.

YOUR GOAL:
Copy wumpus.pl to a new program, called pitwumpus.pl. Modify it so that there is a pit in location (2,3), and modify the code so that the hero always avoids this pit. Remember, this is a simple version of the wumpus world where all the agent is trying to do is get from the starting location at (2,2) to the sword at (3,3). pick it up, then walk back to (1,1) to hit the wumpus. This is intended to be straightforward - don't add any breezes or anything like that. My solution consisted of two small modifications to the code.

Submit pitwumpus.pl using hsp.