CSE 341
section, July 15, 1999

Answers and additional notes are provided at the bottom of this page with in-line links.

Jump to a section of this page...

  1. Prolog? Refresh my memory...
  2. from the real world to Prolog and back
  3. example rules
  4. Do you know these predicates?

Prolog? Refresh my memory...

computation model

terminology

Don't forget the closed-world assumption!


from the real world to Prolog and back

WA, OR, and CA are on the west coast, whereas RI, NY, NC, VA (and some others) are on the east coast. RI, NY, WA, and OR are also northern states. What are the northern east coast states?

Carrots, broccoli, spinach, and corn are vegetables. Broccoli and spinach are green, corn is yellow, and carrots are orange. Apples, oranges, and bananas are fruit. Apples can be green, yellow, or red. Oranges are, well, orange. Bananas are yellow. Which of these foods are/can be yellow?


example rules

fraction(Num,Den). /* or fraction(_,_). */
lt(fraction(Num1,Den1),fraction(Num2,Den2)) :-
    Num1 * Den2 < Num2 * Den1.


length([], 0).
length([H|T], L) :-
    length(T, M),
    L is M + 1.

Do you know these predicates?

one(X,[X|_]).
one(X,[_|Y]) :- one(X,Y).


two([]).
two([_]).
two([A,B|T]) :- A =< B, two([B|T]).


three([], 0).
three([First|Rest], S) :-
    three(Rest, RS),
    S is First + RS.


answers and notes

The closed-world assumption, put briefly: "If a predicate can't be proved true, it's assumed to be false."

sample database for first paragraph:

westCoast(wa).
westCoast(or).
westCoast(ca).
eastCoast(ri).
eastCoast(ny).
eastCoast(nc).
eastCoast(va).
northern(ri).
northern(ny).
northern(wa).
northern(or).
sample query for question "What are the northern east coast states?":

northern(State), eastCoast(State).

sample database for first paragraph:

vegetable(carrots).
vegetable(broccoli).
vegetable(spinach).
vegetable(corn).
green(broccoli).
green(spinach).
yellow(corn).
orange(carrots).
fruit(apples).
fruit(oranges).
fruit(bananas).
green(apples).
yellow(apples).
red(apples).
orange(oranges).
yellow(bananas).
sample query for question "Which of these foods are/can be yellow?":

yellow(Food).

Predicate one might more appropriately be called isMember, because it is true if the first argument is an element of the list which is the second argument.

Predicate two could be called inOrder, because it is true if the given list is in sorted (increasing) order.

Predicate three could be called sumList, because it is true if the second argument is the length of the list given as the first argument.


Ken Yasuhara <yasuhara@cs.washington.edu>
Last modified: Thu Jul 15 17:44:28 PDT 1999