You are on page 1of 5

Introduction:

• Prolog is a logic programming language.


• Prolog is a declarative language, which means that a program consists of data based on the facts and
rules (Logical relationship) rather than computing how to nd a solution.
• In prolog, logic is expressed as relations (called as Facts and Rules).
• To obtain the solution, the user asks a question rather than running a program. When a user asks a
question, then to determine the answer, the run time system searches through the database of facts
and rules.
• Prolog is case-sensitive.
• Prolog is used some areas like natural language processing, arti cial intelligence, expert systems,
automated reasoning, etc.

Basic Prolog Program:


Prolog Symbols:
Using the following truth-functional symbols, the Prolog expressions are comprised. These symbols have
the same interpretation as in the predicate calculus.

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

A basic Prolog program consists of facts, rules and queries.


Fact:
• Fact can be de ned as an explicit relationship between objects, and the properties these objects might
have.
• Facts are unconditionally true in nature.
• The Prolog system uses the facts to work out how to accomplish the solution by searching through the
space of possible solutions.
• An object is the name of element of certain type.
• A Relation is a name that de nes the way in which a collection of objects or variables belong together.
• Syntax of fact : relation(object1, object2….)
• Use small case in prolog facts. In prolog anything you write in capital
or any word starting with capital is considered as variable.
• Eg:
- Tom is a cat
- cat(tom)

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.

Knowledge Base is a collection of facts and rules.

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.

Example: Database consists of following facts.


boy(bob).
boy(johan).
girl(hezal).
likes(bob,hezal).

Arithmetic Operators in Prolog:


• Prolog also has arithmetic operators like +, -, *, / and also the usual collection of functions like sqrt, exp,
cos.
• Writing "2+3" in Prolog is not an instruction to carry out the addition. It represents "the addition of 2
and 3”.
• In Prolog = is used for string assignment and hence N=1+1 will print 1+1 not 2.
• To assign value to variable "is" is used – N is 1. Sum is N+10.
fi

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).

You might also like