Lab2

advertisement
Facts and Rules
Lab #2
LAB #2
FACTS AND RULES
OBJECTIVE
Study the basic structure of Prolog program.
THEORY

A Prolog program is made up of clauses, which conceptually are two types of
phrases: facts and rules.

Facts are relations or properties that you, the programmer, know to be true.

Rules are dependent relations; they allow Prolog to infer one piece of information
from another.

Facts have the general form:
property(object1, object2, ..., objectN)
or
relation(object1, object2, ..., objectN)
where a property is a property of the objects and a relation is a relation between the
objects.

Each fact given in a program consists of either a relation that affects one or more
objects or a property of one or more objects. For example, in the Prolog fact
likes(tom, baseball).
the relation is likes, and the objects are tom and baseball; Tom likes baseball.
Also, in the fact
left_handed(benjamin)
the property is left_handed and the object is benjamin; in other words, Benjamin
is left-handed.

Rules have the general form Head:- Body, which looks like this in a program:
relation(object,object,...,object):relation(object,...,object),
.
.
relation(object,...,object).

For a rule to succeed, Prolog must satisfy all of its subgoals, creating a consistent
set of variable bindings. If one subgoal fails, Prolog backs up and looks for
CSC470: Artificial Intelligence
7
Facts and Rules
Lab #2
alternatives to earlier subgoals, then proceeds forward with different variable
values. This is called backtracking.

The :- ("if") in Prolog should not be confused with the IF used in other languages;
a Prolog rule is in the form of a then/if conditional, while IF statements in other
languages are in the form of an if/then conditional.

You are free to choose names for the relations and objects in your programs,
subject to the following constraints:

Object names must begin with a lower-case letter, followed by any number of
characters; characters are upper-case or lower-case letters, digits, and
underscores.

Properties and relation names must start with a lower-case letter, followed by
any combination of letters, digits, and underscore characters.
EXAMPLE PROGRAMS
Program 1
PREDICATES
CLAUSES
nondeterm likes(symbol,symbol)
likes(ellen,tennis).
likes(tom,baseball).
likes(mark,tennis).
likes(john,football).
likes(eric,swimming).
likes(bill,Activity):-
likes(tom, Activity).
GOAL
likes(bill, baseball).
Program 2
PREDICATES
nondeterm can_buy(symbol, symbol)
person(symbol)
nondeterm car(symbol)
symbol)
for_sale(symbol)
CLAUSES
can_buy(X,Y):-
nondeterm
likes(symbol,
person(X),
car(Y),
likes(X,Y),
for_sale(Y).
person(kelly).
CSC470: Artificial Intelligence
8
Facts and Rules
Lab #2
person(judy).
person(ellen).
person(mark).
car(lemon).
car(hot_rod).
likes(kelly, hot_rod).
likes(judy, pizza).
likes(ellen, tennis).
likes(mark, tennis).
for_sale(pizza).
for_sale(lemon).
for_sale(hot_rod).
GOAL
can_buy(Who,What).
EXERCISES
1. Type the above examples in Visual Prolog environment and execute them
individually.
2. List all the facts and rules used in the above examples.
3. Explain the working of each example.
4. Collect some facts and rules and make your own program. Query it.
CSC470: Artificial Intelligence
9
Download