Professional Documents
Culture Documents
Variables:
• Variable is a string.
• The string can be a combination of lower case or upper case letters. The string can also contain
underscore characters that begin with an underscore or an upper-case letter.
• Anything that begins with an uppercase is considered a variable in prolog.
• Eg:female, Male, X, Y, _female, Ma_le
fi
fi
fi
fi
Rules:
• Rules are implicit relationship between objects.
• Rules are conditionally true. So, when associate condition is true, then the predicate is true.
• Rules are used when you want to say that a fact depends on a group of facts.
• Syntax: rule_name(object1, object2, ...) :- fact/rule(object1, object2, …)
• Rules always end with a full stop.
• Eg:
- Lily is happy if she dances.
- happy(lily) :- dances(lily).
- John likes X if X likes cricket.
- likes(john,X) :- likes(X,cricket).
Queries:
• Queries are some questions on the relationships between objects and object properties.
• They allow us to ask questions to the system.
• The system responds to the query with yes if the database information is consistent to answer the query.
Predicate:
• A relation identi er is referred to as a predicate
• Eg:
- Car is blue
- is(car, blue)
• Predicate express a relationship.
• The element within the parenthesis are the arguments of the predicate, which may be objects or
variable.
• The word before the parenthesis is the name of relation.
Example:
Diana and Charles have two children- William, Harry.
Program to assert gender relationship along with gender.
parent(diana,william).
parent(charles,william).
parent(diana,harry).
parent(charles,harry).
female(diana).
male(charles).
male(harry).
male(william).
fi
Directed Graphs:
Goals in Prolog:
• To nd the goal, Prolog searches all the clauses from top to bottom. Prolog searches in the database
until one is found.
• The goal succeeds if the Prolog matches a clause, and it is a fact.
• Prolog continues to search the database for further matches if it is not a rule. The goal fails if the Prolog
reaches the end of the database.
Recursion in Prolog:
• Any function which calls itself is called recursive function.
• In Prolog, recursion appears when a predicate contain a goal that refers to itself. This simply means a
program calls itself typically until some nal point is reached.
• A recursive de nition always has at least two parts.
• A rst fact that act like a stopping condition and a rule that calls itself simpli ed.
• At each level the rst fact is checked. If the fact is true then the recursion ends. If not the recursion
continue.
• A recursive rule must never call itself with the same arguments. If that happens then the program will
never end.
List in Prolog:
• it is a nite sequence of elements.
• A list in PROLOG is a structure of the form: [t1,t2,t3 … tn]
• The elements in list organised in two section head and tail.
• The direct access to only one element call head , while the rest forms the list called the Tail.
• [Head|Tail] : where Head is a single element, while Tail is list.
fi
fi
fi
fi
fi
fi
Prolog Programs:
Tower of Hanoi:
move(1,X,Y,_) :-
write('Move top disk from '), write(X), write(' to ‘), write(Y), nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).