Prolog
Prolog
g
Facts have some simple rules of syntax:
• Facts should always begin with a lowercase letter and end with a full stop.
• The facts themselves can consist of any letter or number combination, as well as
the underscore _ character.
• However, names containing the characters -,+,*,/, or other mathematical
operators should be avoided.
•Facts syntax:
relation(object1,object2,..).
eats(fred,meat).
variable
X.(mother(mary, X) male(X))
Logical Not:
likes(ram,apple).
?- \+likes(ram,apple).
false.
?- likes(ram,apple).
true.
Logical and
likes(ram,apple).
likes(ram,mango).
?- likes(ram,mango),likes(ram,apple).
true.
?- likes(ram,mango),likes(ram,banana).
false.
Logical OR
likes(ram,apple).
likes(ram,mango).
?- likes(ram,mango);likes(ram,banana).
true
Claus
•eIt is group of words contain subject and predicate.
• Eg. Charlie runs.
• resolution is inference mechanism. we have two sentences (1)
All women like shopping. (2) Olivia is a woman.
Now we ask query 'Who likes shopping'. So, by resolving above
sentences we can have one new sentence Olivia likes shopping.
Example for applying
rules
eats(dog,vegitable).
eats(dog,meat).
eats(lion,meat).
carnivore(X):-
eats(X,vegitable),ea
ts(X,meat).
?- carnivore(dog).
true.
?- carnivore(lion).
false.
Example
Unification
• The idea is similar to that of unification in logic: we have two terms and
we want to see if they can be made to represent the same structure.
• The expression A=B is true if A and B are terms and unify (look identical)
arity
•?- a =
a. true
•?- a = b.
false
•Identify price of
hat. price(pen,20).
price(pencil,10).
price(hat,30).
•?- price(hat,X).
X = 30.
Other additional example:
max(X,Y,X):-X>Y.
?- max(11,10,Max).
Max = 11.
min(X,Y,X):-X<Y.
?- min(9,10,Min).
Min = 9.