0% found this document useful (0 votes)
65 views6 pages

Rules in Prolog: Student Name Student Roll # Program Section

The document describes rules in Prolog. It defines rules as consisting of two parts: (1) a predicate similar to a fact, and (2) other clauses that must be true for the rule to be true, separated by ":-". It provides examples of valid and invalid rules. The document also contains examples demonstrating querying a knowledge base with facts and rules to deduce relationships. Lab tasks involve determining syntactically correct rules and querying a family relationship program to fill a table.

Uploaded by

every one
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views6 pages

Rules in Prolog: Student Name Student Roll # Program Section

The document describes rules in Prolog. It defines rules as consisting of two parts: (1) a predicate similar to a fact, and (2) other clauses that must be true for the rule to be true, separated by ":-". It provides examples of valid and invalid rules. The document also contains examples demonstrating querying a knowledge base with facts and rules to deduce relationships. Lab tasks involve determining syntactically correct rules and querying a family relationship program to fill a table.

Uploaded by

every one
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Student Name

Student Roll #

Program

Section

RULES IN PROLOG
Lab-03

___________________________________________________________________________
7
Artificial Intelligence-Lab [COSC-3212]
OBJECTIVES:

 Querying the Knowledge Base


 Logical Operators in Prolog
 Variables & Name
 Rules in Prolog

1. QUERYING THE KNOWLEDGE BASE


From the given facts we can query the prolog about the facts. For example:

Question: Is there any females in the family.


Query: ?-female(X).

Question: Who are sophia's parents.


Query: ?-Parent(X, sophia).

Question: Charles-I is the parent of whom.


Query: ?-Parent(Charles-I, Y).

2. LOGICAL OPERATORS IN PROLOG

Prolog expressions are comprised of the following truth-functional symbols.

3. VARIABLES & NAMES

Predicate names, function names, and the names for objects must begin with a lowercase
letter.
mother_of
male
female
greater_than
socrates

4. RULES IN PROLOG

A rule can be viewed as an extension of a fact with added conditions that also have to be
satisfied for it to be true. Or more precisely “a rule is a predicate expression that uses
logical implication (:-) to describe a relationship among facts.” It consists of two parts:
___________________________________________________________________________
8
Artificial Intelligence-Lab [COSC-3212]
1. The first part is similar to a fact (a predicate with atoms).

2. The second part consists of other clauses (facts or rules which are separated by
commas) which must all be true for the rule itself to be true.

These two parts are separated by ":-". You may interpret this operator as "if" in English.

Thus a PROLOG rule takes the form:

left_hand_side :- right_hand_side .

This sentence is interpreted as: left_hand_side if right_hand_side. The left_hand_side is


restricted to a single, positive, literal, which means it must consist of a positive atomic
expression. It cannot be negated and it cannot contain logical connectives.

This notation is known as a Horn clause. In Horn clause logic, the left hand side of the
clause is the conclusion, and must be a single positive literal. The right hand side contains the
premises.

Examples of valid rules:

friends(X,Y) :- likes(X,Y),likes(Y,X). /* X and Y are friends


if they like each
other */

hates(X,Y) :- not(likes(X,Y)). /* X hates Y if X does


not like Y. */

enemies(X,Y) :- not(likes(X,Y)),not(likes(Y,X)). /* X and Y are enemies


if they don't like
each other */

Examples of invalid rules:


left_of(X,Y) :- right_of(Y,X) /* Missing a period */

likes(X,Y),likes(Y,X) :- friends(X,Y). /* LHS is not a single literal */

not(likes(X,Y)) :- hates(X,Y). /* LHS cannot be negated */

See the following examples:

EXAMPLE-I:

___________________________________________________________________________
9
Artificial Intelligence-Lab [COSC-3212]
Program 1. A Program with Facts only

sunny.
father(john, peter).
father(john, mary).
mother(susan, peter).
Goals/Queries and their results:
?- sunny. /* The response is yes because the fact "sunny." is present. */

True
?- rainy. /* There is an error because there is no predicate "rainy". */

Erroneous result.
?- father(john, mary).

True
?- mother(susan, mary). /* This cannot be deduced. */

False
?- father(john, susan). /* This cannot be deduced. */

False

EXAMPLE-2:
Program 2. A program describes the relationships of the members in a family.

Take Rule 3 as an example. It means that "grandfather(X, Y)" is true if both


"father(X, Z)" and "parent (Z, X)" are true. The comma between the two
conditions can be considered as a logical-AND operator.
___________________________________________________________________________
10
Artificial Intelligence-Lab [COSC-3212]
You may see that both Rules 1 and 2 start with "parent(X, Y)". When will
"parent(X, Y)" be true? The answer is any one of these two rules is true. This
means that "parent(X, Y)" is true when "father(X, Y)" is true, or
"mother(X, Y)" is true.

As you can see from Rules 3 to 5, predicates that only appear in rules but not
facts (in this case, parent) can also form conditions of other rules.

5. LAB TASKS

5.1. LAB TASK 1

Indicate whether the following are syntactically correct Rules by answering in Yes or No.

Correct Syntax of Rules Yes/No


a :- b, c, d:- e f.

happy(X):- a , b.

happy(X):- hasmoney(X) & has_friends(X).

fun(fish):- blue(betty), bike(yamaha).

5.2. LAB TASK 2


Perform following queries on Program 2 and fill the Table I given below with accurate
information.
Table I

Query Result Description

?- parent(susan, mary).

?- parent(ray, peter).

___________________________________________________________________________
11
Artificial Intelligence-Lab [COSC-3212]
?- yeye(X, susan).

?- mama(amy, X).

?- gunggung(X, Y).

___________________________________________________________________________
12
Artificial Intelligence-Lab [COSC-3212]

You might also like