Prolog tutorial

4
Prolog Prolog, which stands for PROgramming in LOGic, is the most widely available language in the logic programming paradigm. Logic and therefore Prolog is based the mathematical notions of relations and logical inference. Prolog is a declarative language meaning that rather than describing how to compute a solution, a program consists of a data base of facts and logical relationships (rules) which describe the relationships which hold for the given application. Rather then running a program to obtain a solution, the user asks a question. When asked a question, the run time system searches through the data base of facts and rules to determine (by logical deduction) the answer. You will see that Prolog is quite dierent from other programming languages you have studied. First, Prolog has no types. In fact, the basic logic programming environment has no literal values as such. Identifiers starting with lower-case letters denote data values (almost like values in an enumerated type) while all other identifiers denote variables. Though the basic elements of Prolog are typeless, most implementations have been enhanced to include character and integer values and operations. Also, Prolog has mechanisms built in for describing tuples and lists. A Prolog program consists of a database of facts and rules, and queries (questions). Facts: The intuitive meaning of a fact is that we define a certain instance of a relation as being true , So facts are prior information(or knowledge base) stored in database. Rules: Rules extend the capabilities of a logic program. They are what give Prolog the ability to pursue its decision-making process. Rules are representation of logics in prolog , using facts Rules infer new information. Prolog Syntex: As every computer language, Prolog also have some rules regarding variables and constants. Variables: Variables are strings of letters, digits, and the underscore, starting with a capital letter or an underscore. Examples: X, Elephant, _4711, X_1_2, MyVariable. Constants: Numbers, begin with lowercase letter, or enclosed in single quotes. Examples: x, 25, ’this is rainy’. Facts : Two types of facts we will consider :- 1. Simple facts:- In Prolog we can make some statements by using factsFacts either consist of a particular item or a relation between items. For example we can represent the fact that it is sunny by writing the program : sunny. We can now ask a query of Prolog by asking ?- sunny. ?- is the Prolog prompt. To this query, Prolog will answer yes. sunny is true because (from above) Prolog matches it in its database of facts.

description

 

Transcript of Prolog tutorial

Page 1: Prolog tutorial

Prolog

Prolog, which stands for PROgramming in LOGic, is the most widely available language in

the logic programming paradigm. Logic and therefore Prolog is based the mathematical

notions of relations and logical inference. Prolog is a declarative language meaning that

rather than describing how to compute a solution, a program consists of a data base of facts

and logical relationships (rules) which describe the relationships which hold for the given

application. Rather then running a program to obtain a solution, the user asks a question.

When asked a question, the run time system searches through the data base of facts and rules

to determine (by logical deduction) the answer.

You will see that Prolog is quite different from other programming languages you have

studied. First, Prolog has no types. In fact, the basic logic programming environment has no

literal values as such. Identifiers starting with lower-case letters denote data values (almost

like values in an enumerated type) while all other identifiers denote variables. Though the

basic elements of Prolog are typeless, most implementations have been enhanced to include

character and integer values and operations. Also, Prolog has mechanisms built in for

describing tuples and lists.

A Prolog program consists of a database of facts and rules, and queries (questions).

Facts: The intuitive meaning of a fact is that we define a certain instance of a relation

as being true , So facts are prior information(or knowledge base) stored in database.

Rules: Rules extend the capabilities of a logic program. They are what give Prolog

the ability to pursue its decision-making process. Rules are representation of logics in

prolog , using facts Rules infer new information.

Prolog Syntex: As every computer language, Prolog also have some rules regarding

variables and constants.

Variables: Variables are strings of letters, digits, and the underscore, starting with a

capital letter or an underscore.

Examples: X, Elephant, _4711, X_1_2, MyVariable.

Constants: Numbers, begin with lowercase letter, or enclosed in single quotes.

Examples: x, 25, ’this is rainy’.

Facts : Two types of facts we will consider :-

1. Simple facts:- In Prolog we can make some statements by using factsFacts

either consist of a particular item or a relation between items. For example we

can represent the fact that it is sunny by writing the program :

sunny.

We can now ask a query of Prolog by asking

?- sunny.

?- is the Prolog prompt. To this query, Prolog will answer yes. sunny is true

because (from above) Prolog matches it in its database of facts.

Page 2: Prolog tutorial

2. Facts with arguments:- More complicated facts consist of a relation and the

items that this refers to. These items are called arguments. Facts can have

arbitrary number of arguments from zero upwards. A general model is shown

below:

relation(<argument1>,<argument2>,....,<argumentN> ).

The arguments can be any legal Prolog term. The basic Prolog terms are an

integer, an atom, a variable or a structure.

Example:-

likes(raman, music)

eats(john, pizza) **here both arguments started with lower letters(atoms).

Rules:- If conditions then consequence, written as:-

<head> :- <body>

where head and body are conditions and consequences.

<consequences> :- <preconditions>

For example :-

1. “ ram likes everything that shyam likes”, will be represented as:-

likes(ram,X) :- likes(shyam,X).

2. “ ram likes anyone who likes cricket”

likes(ram,X):- likes(X, cricket).

3. “ram likes anyone who like themselves”

likes(ram,X):- likes(X,X).

If a rule have more than one preconditions than, preconditions are separated by “,”.

'It is cold' :- 'It is winter', 'It is snowing'. siblingof(X,Y) :- parentof(Z,X), parentof(Z,Y), X Y.

brotherof(X,Y) :- parentof(Z,X), male(X), parentof(Z,Y), X Y.

Some Useful operators:-

+ / - / * / / addition / subtraction / multiplication / division

/\ / \/ / xor bitwise operators (and / or / xor)

% comment until end of line

= \= string equality & inequality

\ bitwise operators (bitwise inversion)

<< / >> bitwise operators (left shift / right shift / unsigned right shift)

** exponentiation (power)

; or operator

, and operator

\+ negation as failure if goal didn’t match knowledge base

Page 3: Prolog tutorial

START WITH G-PROLOG:-

To work with GNU-prolog(Gprlog follow these steps :-

1. Now create a “file_name.pl” file in linux using any text-editor , this file contain facts

and rules for query. Every fact and rule must be terminated by “.”. Save this file and

return to terminal.

2. Type ‘gprolog’ on Linux terminal and press Enter.

3. Now load file which contains facts and rules for your program, using following

syntax:-

[‘file_name’].

4. Now prolog will load that file and compile it, if it contains any error, it will be

displayed on terminal with corresponding line number.(If it shows any error again open

.pl file and remove that error).

5. After successful compilation throw your Query.

Try it your self:-

Example1. Consider the following connected graph:

Fig. 1.1

The edges can be represented in Prolog as facts:

edge(1,2).

edge(1,4).

edge(1,3).

edge(2,3).

edge(2,5).

edge(3,4).

edge(3,5).

edge(4,5).

To represent the fact that the edges are bi-directional we could either add eight more 'edge'

clauses (edge(2,1),etc.) or we could try a rule like:

connected(X,Y) :- edge(X,Y) ; edge(Y,X). % ”;” or operator

** Think about why we didn’t use a rule like( edge(X,Y):- edge(Y,X).)

Type all the described facts and rules in a file “file_name.pl”. Load this file and compile it.

Now, fire your query like:- ?- connected(3,2).

It will give result yes.

Page 4: Prolog tutorial

Task :-

Jack owns a dog.

Every dog owner is an animal lover.

No animal lover kills an animal.

Either Jack or Curiosity killed the cat, who is named Tuna.

Query:-Did Curiosity kill the cat?

Soln:-

cat(tera). %tera is a cat.

dog(tom). %tom is a dog

owns(jack,tom). % jack owns tom.

animal(Y):-dog(Y);cat(Y). % animal is either dog or cat.

animal_lover(X):- owns(X,Y),dog(Y).% dog owner is an animal

%lover

kill(X,Y):- (X=jack;X=curiosity),cat(Y),\+animal_lover(X).

%either jack(X) or curiosity(X) killed cat, Y is a cat , X is

not an animal Lover.

** Operator (\+)is used when“ \+ means negation as failure”.

//Prepared by Yagyavrat Sharma for EA C461 (AI) course