Você está na página 1de 7

PRACTICAL # 02

SWI PROLOGUE
TOOLS: SWI PROLOG V7.3.2
OBJECT: VARIABLES, OPERATORS & BUILT-IN PREDICATES IN PROLOGUE.
2.1

VARIABLES IN PROLOG:

2.1.1 General Variables


In Prolog, variables enable you to write general facts and rules and ask general questions. In natural
language, you use variables in sentences all the time. A typical general statement in English could be
Bill likes the same thing as Kim.
To represent a variable in Prolog, the first character of the name must be an upper-case letter or an
underscore. For example, in the following line, Thing is a variable.
likes(bill, Thing):- likes(kim, Thing).
Here you can notice that the object bill is mentioned without quotation marks.
Objects can be written with either Quotation Marks,
For instance,
Bill
Harry
serra

OR

You can mention them without quotation marks, using a lower case initial letter, for instance,
john
harry
helen
Like other programming languages the variables are used to temporarily store certain values. They are
used in place of unknown values.
2.1.2 Anonymous Variables
Anonymous variables enable you to unclutter your programs. If you only need certain information from
a query, you can use anonymous variables to ignore the values you don't need. In Prolog, the
anonymous variable is represented by a lone underscore ("_").
For example,
Parent(Huma,Nabeela).
AISHA JUNEJO

PRACTICAL # 02

Parent(Ahad,Uzma).
Parent(Najeeb, Saim).
In order to know the names of all the parents, regardless of who the children are, we can run the query,
?- Parent(WHO, _ ).
This will yield the following Answer
WHO = Huma;
WHO = Ahad;
WHO = Najeeb.

2.2

RULES IN PROLOG:

What You Can Infer from Given Facts


Rules are dependent relations; they allow Prolog to infer one piece of information from another. A rule
becomes true if a given set of conditions is proven to be true. Each rule depends upon proving its
conditions to be true.
In Prolog, all rules have two parts: a head and a body separated by the special :- token.
head :- body
The head is the fact that would be true if some number of conditions were true. This is also known as
the conclusion or the dependent relation.
The body is the set of conditions that must be true so that Prolog can prove that the head of the rule is
true.
A rule is an expression that indicates that the truth of a particular fact depends upon one or more other
facts. The syntax can be
predicate1(arg1, arg2, ... argN):- predicate2(arg1, arg2, ... argN), predicate3(arg1, arg2, ...
argN), ,predicateN(arg1, arg2, ... argN).
This can be read as predicate1 is true only if predicate2 to predicateN are true. Or It can be:
predicate1(arg1, arg2, ... argN):- predicate2(arg1, arg2, ... argN); predicate3(arg1, arg2, ...
argN); ;predicateN(arg1, arg2, ... argN).
This can be read as predicate1 is true if any one of predicate2 to predicateN is true (as ; represents OR in
Prolog).
The left hand side before :- of a rule 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.
Again taking the example of the program above, you can add a rule in the Clauses like
parent(X,Y):- child(Y,X).
This can be read as if Y is child of X then X is the parent of Y.
AISHA JUNEJO

PRACTICAL # 02

Similarly we can also have


Grandchild(X,Y):- child(Y,Z), child(Z,X).
This can be read as if Y is child of Z and Z is child of X then Y is Grandchild of X. It can also be
Grandchild(X,Y):-parent(Z,Y),parent(X,Z).

EXAMPLES:
Not:
X and Y are Enemies if X does not like Y and Y does not like X.
X and Y are enemies if they dont like each other.
enemies(X,Y) :- not(likes(X,Y)),not(likes(Y,X)).

OR

X will not eat if X is not hungry.


not(eat(X)) :- not(hungry(X)).
2.2.1 Like-Hate Case Study
Declarative Sentences
Kashif likes music.
Kashif likes cricket.
Sara likes chips.
Sara likes mobile.
Kashif hates hockey.
Kashif hates colddrinks.
Sara hates driving.
Sara hates parties.
Sara hates everything Kashif likes.
Kashif likes everything sara likes.

PROLOG facts
likes(kashif, music).
likes(kashif, cricket).
likes(sara, chips).
likes(sara, mobile).
AISHA JUNEJO

PRACTICAL # 02

hates(kashif, hockey).
hates(kashif, colddrinks).
hates(sara, driving).
hates(sara, parties).
PROLOG Rules
hates(sara,X):-likes(kashif,X).
likes(kashif,X):-likes(sara,X).

2.3

ARITHMETIC OPERATORS

Prolog can perform all four basic arithmetic operations (addition, subtraction, multiplication, and
division) between integral and real values; the type of the result is determined according to the
following table.
Operand 1

Operator

Operand 2

Result

integral

+, -, *

Integral

integral

real

+, -, *

Integral

real

integral

+, -, *

Real

real

real

+, -, *

Real

real

integral or real

integral or real

real

integral

Div

Integral

integral

Integral

Mod

Integral

integral

In case of mixed integral arithmetic, involving both signed and unsigned quantities, the result is signed.
The size of the result will be that of the larger of the two operands.

2.4

RELATIONAL OPERATORS

Following is the list of relational operators used in PROLOG:


Relational Operator

Meaning

>

Greater Than

<

Less Than

>=

Greater Than or Equals To

<=

Less Than or Equals To

Equals To

<> or ><

Not Equals To
AISHA JUNEJO

PRACTICAL # 02

Note that Prolog's "=" relation is equality (not assignment); it is the same as the "==" relation in C.

2.5

BUILT-IN PREDICATES IN PROLOG

In many programming languages there are some built-in functions available. For example in JAVA there
Math.sin(), Math.cos(), Math.abs(), System.out.println() etc. are built-in functions. Similarly in PROLOG
predicates are like functions and there are some built-in predicates available in PROLOG some of them
are explained below.

2.5.1 write(value, value1,..)


It prints the values concatenated with each other at output screen. E.g. write (Hello World, 1, A, B, b)
will result in:
Hello World1ab b

(assuming A and B are bound to a and b respectively).

2.5.2 nl:It generates a line feed (new line). The same can be achieved by using \n in write.
2.5.3 read:This is used to read integer, real, and character values respectively.
2.5.4 consult(filename)
You are not bound to store all the facts within the file of your program. They can be saved in a separate
file and then your program can load them using consult. Remember that the file used as the database to
store the facts should contain only facts and no program sections. For example, the database file can be
C:\Facts.txt, so you can use consult(Facts).
The facts that are placed in external files should be declared under Facts or Database section instead of
Predicates section.
2.5.5 fail:If this predicate is encountered in any rule, that rule automatically fails.

2.6

EXAMPLE PROGRAM

Take 3 numbers from the user and add/subtract them. Store the result in some variable
add(A, B, C, Res):sub(A, B, C, Res):-

Res is A+B+C.
Res is A-B-C.

Goals to run:

AISHA JUNEJO

PRACTICAL # 02

2.7

EXERCISE
1. Cricket case study

There is a cricket team of 5 players; capabilities (properties) of each of the player are given in
following table.
Player

Name

Batsman

Bowler

Fielder

Salman

Afridi

Malik

Shoaib
Tanveer

Write the facts from the above table, create the rule for allRounder and run following goals in
PROLOG.

Is Afridi All-rounder?
Is Shoaib all-rounder?
Who are all-rounders in the team?
Is Salman a fielder and a bowler?
Is Tanveer not a batsman?
Is Afridi not a bowler?
AISHA JUNEJO

PRACTICAL # 02

2. Write Visual Prolog rules that convey the meaning of these natural-language sentences:
A person is hungry if that person's stomach is empty.
Everybody likes a job if it's fun and it pays well.
Sally likes french fries if they're cooked.
Everybody owns a car who buys one, pays for it, and keeps it.
3. Given with following fact and rules, create a prolog program and run the following queries:
Facts

Mubeen is Sairas father


Asad is Omers and Jawerias father
Bilal is Asads father
Shahid is Madeehas father
Saira is Madeehas mother
Aleena is Mubeens and Jawerias mother
Madeeha is Asmas mother

Rules

A persons parent is their mother or father


A persons grand father is the father of one of their parents
A persons grand mother is the mother one of their parents

Make Queries for following. Check each in separate Goal sections and show results

Who is the father of Saira?


Is Asad the parent of Jaweria?
Who is the Grandmother of Jaweria?
Who is the Grandfather of whom?

4. Create a basic calculator prolog program for making basic calculations (add, subtract, multiply,
divide, average).
5. Continue calculator program with addition of using read(), write() predicate, for taking numeric
input from user

AISHA JUNEJO