Prolog Programming
Prolog is a logic programming language. It has important role in artificial
intelligence.
Unlike many other programming languages, Prolog is intended primarily as a
declarative programming language.
In prolog, logic is expressed as relations (called as Facts and Rules).
3 Important things in PROLOG Programming
When we program in ProLog we need to provide following three things
– Declaring some facts about objects and their relationships
– Defining some rules about objects and their relationships
– Asking questions about objects and their relationships
Problem solving in PROLOG
Insert facts and rules into the database
Ask questions (queries) based on the contents of the database
Facts
Used to represent unchanging information about objects and their
relationships.
Only facts in the PROLOG database can be used for problem solving.
Insert facts into the database by, typing the facts into a file and loading (consulting)
the file into a running PROLOG system
Queries
Retrieve information from the database by entering QUERIES
A query is a pattern that PROLOG is asked to match against the database
A query will cause PROLOG to
● look at the database
● try to find a match for the query pattern
● execute the body of the matching head
● return an answer
Examples (Facts)
English PROLOG
“A dog is a mammal” isa(dog, mammal).
“A sparrow is a bird” isa(sparrow, bird).
Examples (Rules)
English PROLOG
“Something is an animal animal(X) :- isa(X,mammal).
if it is a mammal or a bird” animal(X) :- isa(X, bird).
Examples (Queries)
English PROLOG
“is a sparrow an animal?” ?- animal(sparrow).
answer: “yes” yes
“is a table an animal?” ?- animal(table).
answer: “no” no
“what is a dog?” ?- isa(dog, X).
answer: “a mammal” X = mammal
Syntax and Basic Fields :
In prolog, We declare some facts. These facts constitute the Knowledge Base of the system.
We can query against the Knowledge Base. We get output as affirmative if our query is
already in the knowledge Base or it is implied by Knowledge Base, otherwise we get output
as negative. So, Knowledge Base can be considered similar to database, against which we
can query.
Prolog facts are expressed in definite pattern.
Facts contain entities and their relation.
Entities are written within the parenthesis separated by comma (, ).
Their relation is expressed at the start and outside the parenthesis.
Every fact/rule ends with a dot (.). So, a typical prolog fact goes as follows :
Example: Output of prolog programming:
Advantages :
1. Easy to build database. Doesn’t need a lot of programming effort.
2. Pattern matching is easy. Search is recursion based.
3. It has built in list handling. Makes it easier to play with any algorithm involving lists.
Disadvantages :
1. LISP (another logic programming language) dominates over prolog with respect to I/O
features.
2. Sometimes input and output is not easy.
Applications :
Prolog is highly used in artificial intelligence(AI). Prolog is also used for pattern matching
over natural language parse trees.
Example
English Lanuage:
a. Ram likes mango.
b. Seema is a girl.
c. Bill likes Cindy.
d. Rose is red.
e. John owns gold.
PL Program:
likes(ram ,mango).
girl(seema).
red(rose).
likes(bill ,cindy).
owns(john ,gold).
Output:
?-likes(ram,What).
What= mango
?-likes(Who,cindy).
Who= bill
?-red(What).
What= rose
?-owns(Who,What).
Who= john
What= gold.