AI File
AI File
Page
Sr. No. Program Description Signature
No.
1 Introduction to Prolog 3
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:
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:
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).
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")).
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
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.
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".
head :- body1.
head :- 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?
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:
teaches(Teacher, Student) :-
lectures(Teacher, Course),
studies(Student, Course).
?- teaches(codd, Student).
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,
water_jug(X,Y):-
X < 4,
Y > 3,
water_jug(X,Y):-
X > 4,
Y > 3,
water_jug(X,Y):-
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(N * N),
write(' board'),
length(Queens, N),
queens(Board, 0, Queens).
Col is Col0+1,
functor(Vars, f, N),
constraints(0, _, _, _) :- !.
queens([], _, []).
Row is Row0+1,
Query:-
?- queens(5,Queens).