Chapter 2: Rules and fact

Faiz ul haque Zeya
The fact are represented as
fact(param,...)
For example
brother(James,Tim).
indicates that James is the brother of Tim.
The first example is a set of facts related to brother as

brother(james,tim).
brother(alison,howard).
brother(bob,steve).
brother(pat,ben).
Consult the file and the following message will be displayed.
1 ?-
% c:/brother.pl compiled 0.00 sec, 1,192 bytes
1 ?-
The following questions can be asked.
1 ?- brother(X,steve).

X = bob

Yes
whose answers are in constant. The query posted is "who is the brother of steve" and the answer is bob which satisfies the facts above. The other types of questions are like
1 ?-
| brother(bob,steve).

Yes
2
whose answers are in yes or no.
There are rules in prolog which are used to describe relations between variables. Rules are define as pred(X,Y):- parent(X,Y).
pred(X,Y):- parent(X,Z),pred(Z,Y).
These first rules says that X is pred of Y if X is parent of Y. The second rules says that X is pred of Y if X is parent of some Z and that Z is pred of Y.
The following file can be consulted.

parent(abc,xyz).
parent(xyz,klm).
parent(jhk,klm).
parent(iop.klm).
pred(X,Y):- parent(X,Y).
pred(X,Y):- parent(X,Z),pred(Z,Y).

The follwoing types of questions can be asked.
1 ?- pred(abc,xyz).

Yes
which answer in yes or no and other
7 ?-
pred(X,xyz).

X = abc
Yes
Exercises
  1. Write facts about sibling, rule of sibling and demonstrate few goals(queries) related to it.
  2. Write rules about successor and facts about its.
  3. Write the rule that an apple can be defined as red and is fruit.