0% found this document useful (0 votes)
110 views

AI File

The document provides an introduction to Prolog including: 1) Prolog is a declarative programming language where the programmer specifies goals and Prolog works out how to achieve them, unlike most common procedural languages. 2) Some applications of Prolog include intelligent databases, natural language understanding, expert systems, and problem solving. 3) Prolog programs specify relationships between objects and their properties through facts and rules. Facts describe explicit relationships and rules define implicit relationships.

Uploaded by

academy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

AI File

The document provides an introduction to Prolog including: 1) Prolog is a declarative programming language where the programmer specifies goals and Prolog works out how to achieve them, unlike most common procedural languages. 2) Some applications of Prolog include intelligent databases, natural language understanding, expert systems, and problem solving. 3) Prolog programs specify relationships between objects and their properties through facts and rules. Facts describe explicit relationships and rules define implicit relationships.

Uploaded by

academy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

INDEX

Page
Sr. No. Program Description Signature
No.

1 Introduction to Prolog 3

To calculate the factorial of a


2 given positive integer.
8

To calculate the sum of two given


3 numbers.
9

To find the maximum between


4 two given numbers.
10

5 To implement depth first search. 11

To solve water jug problem using


6 AI Technique.
13

To solve any problem using


7 Breadth First Search. (N-Queens 15
Problem)
INTRODUCTION TO PROLOG
What is Prolog?
 Prolog = Programmation en Logique (Programming in Logic).
 Prolog is a declarative programming language unlike most common programming languages.
 In a declarative language
o the programmer specifies a goal to be achieved
o the Prolog system works out how to achieve it
 relational databases owe something to Prolog
 traditional programming languages are said to be procedural
 procedural programmer must specify in detail how to solve a problem:

o mix ingredients;
o beat until smooth;
o bake for 20 minutes in a moderate oven;
o remove tin from oven;
o put on bench;
o close oven;
o turn off oven;

 in purely declarative languages, the programmer only states what the problem is and leaves the
rest to the language system.

Applications of Prolog
Some applications of Prolog are:

 intelligent data base retrieval


 natural language understanding
 expert systems
 specification language
 machine learning
 robot planning
 automated reasoning
 problem solving

Relations
 Prolog programs specify relationships among objects and properties of objects.
 When we say, "John owns the book", we are declaring the ownership relationship between two
objects: John and the book.
 When we ask, "Does John own the book?" we are trying to find out about a relationship.
 Relationships can also rules such as:

Two people are sisters if


they are both female and
they have the same parents.

 A rule allows us to find out about a relationship even if the relationship isn't explicitly stated as a
fact.

Programming in Prolog
 declare facts describing explicit relationships between objects and properties objects might have
(e.g. Mary likes pizza, grass has_colour green, Fido is a dog, Mizuki taught Paul Japanese )
 define rules defining implicit relationships between objects (e.g. the sister rule above) and/or
rules defining implicit object properties (e.g. X is a parent if there is a Y such that Y is a child of
X).

One then uses the system by:

 asking questions above relationships between objects, and/or about object properties (e.g. does
Mary like pizza? is Joe a parent?)

Facts
 Properties of objects, or relationships between objects;
 "Dr Turing lectures in course 9020", is written in Prolog as:

lectures(turing, 9020).

 Notice that:
o names of properties/relationships begin with lower case letters.
o the relationship name appears as the first term
o objects appear as comma-separated arguments within parentheses.
o A period "." must end a fact.
o objects also begin with lower case letters. They also can begin with digits (like 9020),
and can be strings of characters enclosed in quotes (as in reads (fred, "War and
Peace")).

lectures(turing, 9020). is also called a predicate.

Facts about a hypothetical computer science department:


% lectures(X, Y): person X lectures in course Y
lectures(turing, 9020).
lectures(codd, 9311).
lectures(backus, 9021).
lectures(ritchie, 9201).
lectures(minsky, 9414).
lectures(codd, 9314).

% studies(X, Y): person X studies in course Y


studies(fred, 9020).
studies(jack, 9311).
studies(jill, 9314).
studies(jill, 9414).
studies(henry, 9414).
studies(henry, 9314).

%year(X, Y): person X is in year Y


year(fred, 1).
year(jack, 2).
year(jill, 2).
year(henry, 4).

Together, these facts form Prolog's database.

Queries

 Once we have a database of facts (and, soon, rules) we can ask questions about the stored
information.
 Suppose we want to know if Turing lectures in course 9020. We can ask:

% prolog -s facts03
(multi-line welcome message) facts03 loaded into Prolog
?- lectures(turing, 9020). "?-" is Prolog's prompt
true. output from Prolog
?- <control-D> hold down control & press D
% to leave Prolog

 Notice that:
o In SWI Prolog, queries are terminated by a full stop.
o To answer this query, Prolog consults its database to see if this is a known fact.
o In example dialogues with Prolog, the text in italics is what the user types.

Variables

 Suppose we want to ask, "What course does Turing teach"?


 This could be written as:

Is there a course, X, that Turing teaches?

 The variable X stands for an object that the questioner does not know about yet.
 To answer the question, Prolog has to find out the value of X, if it exists.
 As long as we do not know the value of a variable it is said to be unbound.
 When a value is found, the variable is said to bound to that value.
 The name of a variable must begin with a capital letter or an underscore character, "_".
 To ask Prolog to find the course that Turing teaches, enter this:

?- lectures(turing, Course).
Course = 9020 output from Prolog
 To ask Prolog to find the course that Codd teaches, enter this:
?- lectures(codd , Course).
Course = 9311 ; type ";" to get next solution
Course = 9314
?-

If Prolog can tell that there are no more solutions, it just gives you the ?- prompt for a new
query, as here. If Prolog can't tell, it will let you type ; again, and then if there is no further
solution, report false.

 Prolog can find all possible ways to answer a query, unless you explicitly tell it not to.

Conjunctions of Goals in Queries


 How do we ask, "Does Turing teach Fred"?
 This means finding out if Turing lectures in a course that Fred studies.

?- lectures(turing, Course), studies(fred, Course).

 i.e. "Turing lectures in course, Course and Fred studies (the same) Course".
 The question consists of two goals.
 To answer this question, Prolog must find a single value for Course, that satisfies both goals.
 Read the comma, ",", as and.
 However, note that Prolog will evaluate the two goals left-to-right. In pure logic, P1 ∧ P2 is the
same as P2 ∧ P1. In Prolog, there is the practical consideration of which goal should be evaluated
first - the code might be more efficient one way or the other. In particular, in "P1, P2", if P1 fails,
then P2 does not need to be evaluated at all. This is sometimes referred to as a "conditional-and".

Disjunctions of Goals in Queries


 What about or (i.e. disjunction)? It turns out explicit ors aren't needed much in Prolog. There is a
way to write or: (";")

 The reason ors aren't needed much is that

head :- body1.
head :- body2.

has the same effect as

head :- body1 ; body2.

 Avoid using “;” if you can, at least until you have learned how to manage without it. While some
uses of “;” are harmless, others can make your code hard to follow.

Backtracking in Prolog
 Who does Codd teach?

?- lectures(codd, Course), studies(Student, Course).


Course = 9311
Student = jack ;

Course = 9314
Student = jill ;

Course = 9314
Student = henry ;

 Prolog solves this problem by proceeding left to right and then backtracking.
 When given the initial query, Prolog starts by trying to solve

lectures(codd, Course)

 There are six lectures clauses, but only two have codd as their first argument.
 Prolog uses the first clause that refers to codd: lectures(codd, 9311).
 With Course = 9311, it tries to satisfy the next goal, studies(Student, 9311).
 It finds the fact studies(jack, 9311). and hence the first solution: (Course = 9311,
Student = jack)

Rules
 The previous question can be restated as a general rule:

One person, Teacher, teaches another person, Student if


Teacher lectures in a course, Course and
Student studies Course.

 In Prolog this is written as:

teaches(Teacher, Student) :-
lectures(Teacher, Course),
studies(Student, Course).

?- teaches(codd, Student).

 Facts are unit clauses and rules are non-unit clauses.

Clause Syntax

 ":-" means "if" or "is implied by". Also called the neck symbol.
 The left hand side of the neck is called the head.
 The right hand side of the neck is called the body.
 The comma, ",", separating the goals, stands for and.
 Another rule, using the predefined predicate ">".

more_advanced(S1, S2) :-
year(S1, Year1),
year(S2, Year2),
Year1 > Year2.
PRACTICAL – 01
Aim: - To calculate the factorial of a given positive integer.

Program Code:-

factorial(0,1).

factorial(X,Y):-

X>0,

X1 is X-1,

factorial(X1,Y1),

Y is X * Y1.

Query:-
?- factorial(10,X).
PRACTICAL – 02
Aim: - To calculate the sum of two given numbers.

Program Code:-

sum(X,Y,Z):-

Z is X+Y.

Query:-
?- sum(203,546,X).
PRACTICAL – 03
Aim: - To find the maximum between two given numbers.

Program Code:-

max(X,Y,Z):-

X>=Y,

Z is X.

max(X,Y,Z):-

Y>X,

Z is Y.

Query:-
?- max(100,234,X).

?- max(263,11,X).

?- max(192,192,X).
PRACTICAL – 04
Aim: - To implement depth first search.

Program Code:-

dfs(X,Y,R) :-

dfs(X,Y,[X],R).

dfs(X,Y,_,[mov(X,Y)]) :-

go(X,Y).

dfs(X,Y,V,[mov(X,Z)|R]) :-

go(X,Z),

\+ member(Z,V),

dfs(Z,Y,[Z|V],R),

Z \= Y.

go(X,Y) :- path(X,Y).

go(X,Y) :- path(Y,X).

path(a,b).

path(a,c).

path(b,d).

path(b,e).

path(c,e).

path(c,f).

path(e,g).

path(e,h).

path(f,i).
path(h,j).

path(i,j).

Query:-
?- dfs(a,j,Path).
PRACTICAL – 05
Aim: - To solve water jug problem using AI Technique.

Program Code:-

water_jug(X,Y):-

X > 4,

Y < 3,

write('4L water jug overflowed.'),nl.

water_jug(X,Y):-

X < 4,

Y > 3,

write('3L water jug overflowed.'),nl.

water_jug(X,Y):-

X > 4,

Y > 3,

write('Both water jug overflowed.'),nl.

water_jug(X,Y):-

(X=:=0,Y=:=0,nl,write('4L:0 & 3L:3 (Action: Fill 3L


jug.)'),YY is 3,water_jug(X,YY));

(X=:=0,Y=:=0,nl,write('4L:4 & 3L:0 (Action: Fill 4L


jug.)'),XX is 4,water_jug(XX,Y));

(X=:=2,Y=:=0,nl,write('4L:2 & 3L:0 (Action: Goal state


reached.)'));

(X=:=4,Y=:=0,nl,write('4L:1 & 3L:3 (Action: Pour water


from 4L to 3L jug.)'),XX is X-3,YY is 3,water_jug(XX,YY));
(X=:=0,Y=:=3,nl,write('4L:3 & 3L:0 (Action: Pour water
from 3L to 4L jug.)'),XX is 3,YY is 0,water_jug(XX,YY));

(X=:=1,Y=:=3,nl,write('4L:1 & 3L:0 (Action: Empty 3L


jug.)'),YY is 0,water_jug(X,YY));

(X=:=3,Y=:=0,nl,write('4L:3 & 3L:3 (Action: Fill 3L


jug.)'),YY is 3,water_jug(X,YY));

(X=:=3,Y=:=3,nl,write('4L:4 & 3L:2 (Action: Pour water


from 3L to 4L jug until 4L jug is full.)'),XX is X+1,YY is Y-
1,water_jug(XX,YY));

(X=:=1,Y=:=0,nl,write('4L:0 & 3L:1 (Action: Pour water


from 4L to 3L jug.)'),XX is Y,YY is X,water_jug(XX,YY));

(X=:=0,Y=:=1,nl,write('4L:4 & 3L:1 (Action: Fill 4L


jug.)'),XX is 4,water_jug(XX,Y));

(X=:=4,Y=:=1,nl,write('4L:2 & 3L:3 (Action: Pour water


from 4L to 3L jug until 3L jug is full.)'),XX is X-2,YY is
Y+2,water_jug(XX,YY));

(X=:=2,Y=:=3,nl,write('4L:2 & 3L:0 (Action: Empty 3L


jug.)'),YY is 0,water_jug(X,YY));

(X=:=4,Y=:=2,nl,write('4L:0 & 3L:2 (Action: Empty 4L


jug.)'),XX is 0,water_jug(XX,Y));

(X=:=0,Y=:=2,nl,write('4L:2 & 3L:0 (Action: Pour water


from 3L to 4L jug.)'),XX is Y,YY is 0,water_jug(XX,YY)).

Query:-
?- water_jug(0,0).
PRACTICAL – 06
Aim: - To solve any problem using Breadth First Search. (N-Queens Problem)

Program Code:-

queens(N, Queens) :-

write('The column number for each consecutive row for


'),

write(N * N),

write(' board'),

length(Queens, N),

board(Queens, Board, 0, N, _, _),

queens(Board, 0, Queens).

board([], [], N, N, _, _).

board([_|Queens], [Col-Vars|Board], Col0, N,[_|VR], VC) :-

Col is Col0+1,

functor(Vars, f, N),

constraints(N, Vars, VR, VC),

board(Queens, Board, Col, N, VR, [_|VC]).

constraints(0, _, _, _) :- !.

constraints(N, Row, [R|Rs], [C|Cs]) :-

arg(N, Row, R-C),


M is N-1,

constraints(M, Row, Rs, Cs).

queens([], _, []).

queens([C|Cs], Row0, [Col|Solution]) :-

Row is Row0+1,

select(Col-Vars, [C|Cs], Board),

arg(Row, Vars, Row-Row),

queens(Board, Row, Solution).

Query:-
?- queens(5,Queens).

You might also like