AI Prolog PDF
Prolog
Clause
Prolog programs describe
relations, defined by means of clauses. Pure Prolog is restricted to Horn clauses. There are two
types of clauses: facts and rules.
A rule is of the form Head
:- Body
and is read as "Head is true if Body is true". For example
animal(X) :- cat(X).
mean for all
X, X animal if X is cat.
Fact is a clause
without body. For example ‘Tom is
a cat.’ is a fact and represented
in prolog as
cat(‘Tom’).
It may also be represented as
cat(tom):- true.
Term
A term in prolog may be
Atom: An atom is a general-purpose name with no inherent meaning. Examples of atoms include x , red , 'Taco' , 'some atom’ .
Number: It can be floats or integers. For example 5, 2.5, 0.5, -2 etc
Variable: It is denoted by a string consisting of letters, numbers and underscore
characters, and beginning with an upper-case letter or underscore. It represents
placeholders for arbitrary terms.
A variable can become instantiated through unification.
_ denotes anonymous variable. It does not represent same value every
where it occurs
with in a predicate definition.
Compound term: It is composed
of an atom called a "functor" and a number
of "arguments", which
are again terms For example
likes_movie(ravi, dangal).
The number of arguments is called the term's arity.
Evaluation
Execution of a Prolog program
is initiated by the user's posting
of a single goal, called the query. Execution strategy is called chronological backtracking.
Example
mother_child(trude,
sally). father_child(tom, sally). father_child(tom, erica). father_child(mike, tom).
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y). parent_child(X, Y) :- father_child(X, Y).
parent_child(X,
Y) :- mother_child(X, Y).
This results
in the following query being evaluated as true:
?- sibling(sally, erica).
True
The process of evaluation
is shown below
father_child(Z, sally)
father_child(tom, sally)
true
This results
in the following query being evaluated as true:
?- father_child(X, Y).
X=tom, Y=sally
X=tom, Y=erica
X=mike, Y=tom
The process of evaluation
is shown below
r(a). s(b,c). m(b). n(a). q(X):-m(X). q(X):-n(X). p(X,Y):-q(X),r(Y). p(X,Y):-r(X),s(X,Y). |
?-p(a,Y) Y=a |
Loop and Recursion
Iterative algorithms can be
implemented by means of recursive predicates. Order of rules is important.
For example
program to check whether a number is natural number or not
natural(X):-X=<0,!,fail. natural(1):-true,!. natural(X):-Y is X-1,natural(Y). |
Anonymous Variable
The name of every
anonymous variable is _.
An ordinary variable
stands for the same variable
within one clause
and bound to same value. On the contrary, every occurrence of _denotes a distinct variable.
For example
fly(X) :- green(X), dragon(X).
and happy(X)
:- pass_exam(X,Y).
here, X in fly(X) and
happy(X)
are not related.
However, the
occurrance of X in the definition of fly(X)
refer to the same thing. In the following example , the variable represented by _ refers
to different people
balanced_child(X) :-brother(X,_), sister(X,_).
List
Prolog supports
a data-structure known as
list. List always
starts and end with square
brackets, and each of the items it contains is separated
by a comma.
Syntex [item1,item2,….]
[Head|Tail] [item1,item2,…|others] Item may be another list.
For example
[a,b,c] , [a], [[a,b],c,[d,[e,f]]]
[a,b,c] unifies with [Head|Tail] resulting in Head=a
and Tail=[b,c]
[a] unifies
with [H|T] resulting
in H=a and T=[] [a,b,c] unifies with [a|T] resulting in
T=[b,c] [a,b,c] doesn't unify with [b|T]
[] doesn't unify with [H|T]
[] unifies with []. Two empty lists always match
Q: Do the following
pairs of lists unify ?
Ans:
[a,d,z,c] and [H|T]
Yes H
= a and T = [d,z,c] [apple,pear,grape] and [A,pear|Rest]
Yes A = apple and Rest = [grape] [a|Rest] and [a,b,c]
Yes Rest = [b,c] [a,[]] and [A,B|Rest]
Yes A = a, B = [] and Rest = [] [One] and [two|[]]
Yes One = two [one] and [Two]
Yes Two = one [a,b,X] and [a,b,c,d]
No X can not represent the two atoms
c,d
To
check membership
member(X,Y) checks X is member of list Y?
member(X,[X|_]). |
member(X,[H|T]):-member(X,T) |
?- member(a,[b,a,c]) True |
?- member(a,[[a,b],c,d]) False |
To check
concatenation
concatenation(L1,L2,L3) check L3 is concatenation of L1 and L2?
concatenation([],L,L). concatenation([X|L1],L2,[X|L3]):- concatenation(L1,L2,L3). |
concatenation([],L,L). concatenation([X|L1],L2,[X|L3]):- concatenation(L1,L2,L3). |
concatenation([1,2],[3,4],[1,2,3,4])
⬇
concatenation([2],[3,4],[2,3,4])
⬇
concatenation([],[3,4],[3,4])
⬇
To add a element
in the front of a list
add(X,L,[X|L]). |
?-add(a,[b,c],[a,b,c]) true |
?-add(a,[b,c],X) X=[a,b,c] |
To delete an
element from the front of a list
del(X,[X|Tail],Tail). del(X,[Y|Tail],[Y|Tail]):-del(X,Tail,Tail). |
?-del(a,[a,b,c],[b,c]) True |
?-del(a,[b,a,c],[b,c]) False |
?-del(a,[a,b,c],X) X=[b,c] |
To find out
S is sublist of list L
sublist(S,L):-concatenation(L1,L2,L),concatenation(S,L3,L2). |
?-
sublist([a,b],[d,a,e,b,c]) False |
?- sublist([a,b],[d,a,b,c]) True |
To find out length of a
list
len([],0). len([_|T],N):-len(T,N1), N is N1+1. |
?- len([a,b,c],4) False |
?- len([a,b,c],X) X=3 |
Monkey and Banana Problem
There is a monkey at the door of a room
In the middle of the room
a banana hangs from the ceiling
The monkey wants it, but can not jump high enough from the floor. At the window of the room there is a box
that the monkey can use.
The monkey can perform the following actions.
·
Walk on the floor
·
Climb the box
·
Push the box around(if it is beside the box)
·
Grasp the banana
if it is standing on the box directly under the banana. Solution:
We define the
state as 4 tuple. (monkey_at, on, box_at, has_banana)
First parameter
specify the position of monkey - door/middle
Second parameter specifies whether monkey is on floor or on box Third
parameter specifies position of box – window/middle
Forth parameter specifies whether
monkey has banana
or not
move(state(middle,onbox,middle,hasnot),grasp,state(middle,onbox,middle,has)).
move(state(P,onfloor,P,H),climb,state(P,onbox,P,H)). move(state(P1,onfloor,P1,H),push(P1,P2),state(P2,onfloor,P2,H)). move(state(P1,onfloor,B,H),walk(P1,P2),state(P2,onfloor,B,H)). canget(state(_,_,_,has)):-write("yes can get"). canget(State1):-move(State1,Op,State2),canget(State2). |
?- canget(state(middle,onbox,middle,has)) True |
?- canget(state(middle,onbox,middle,has)) True |
Arithmetic operators
Supports +,-,*, /, mod
is operator forces evaluation
?- X is 3/2
Relational operators
>, >=,
=<,=:=,=\=
To find gcd of two
numbers.
gcd(X,X,X). gcd(X,Y,D):-X<Y, Y1 is Y-X
, gcd(X,Y1,D). gcd(X,Y,D):-X>Y, X1 is
X-Y , gcd(X1,Y,D). |
?-gcd(15,40,X) X=5 |
?-gcd(5,4,1) True |
Cuts for controlling backtracking
The cut, in
Prolog, is a goal,
written as !, which always succeeds, but cannot be backtracked past. It is used to prevent unwanted
backtracking, for example, to prevent extra solutions being found by Prolog.
For example
C:-P, Q, R, !, S, T, U.
C:- V.
A:- B,C,D.
?-A
·
Backtracking done within
the goal list P,Q,R
·
As soon as the
cut is reached
ü
All alternatives of P,Q,R are suppressed.
ü
The clause C:-V will also be discarded
ü
No effect within
A:-B, C, D, that is backtracking within
B,C,D remains active.
To find out maximum of 2
numbers.
max(X,Y,X):-X>=Y,!.
max(X,Y,Y).
Adding an element to a list without duplication.
member(X,[X|_]).
member(X,[_|T]):-member(X,T).
add(X,L,L):-member(X,L),!.
add(X,L,[X|L]).
Negation as failure
(use of !)
Example 1:
‘Rohan likes all jewellery
except rings’ likes(rohan,X):- ring(X),!,fail. likes(rohan,X):-jewellery(X).
Example 2:
Different predicate.
different(X,X):-!,fail. different(X,Y).
Difference between
fail and cut
·
Fail predicate forces
backtracking whereas Cut predicate prevents
backtracking.
·
Fail predicate always fail whereas
Cut predicate always
succeed.
Comments
Post a Comment
Kaushikmadhav77@gmail.com