Prolog, Prolog Programming IN AI. Prolog is an Artificial Intelligence programming language. Prolog is a logic programming language associated with artificial intelligence and computational linguistics. Its applications include natural language understanding and expert systems. Prolog is notably a so-called nonprocedural, or declarative, language.
2. Introduction
Prolog
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). Core heart of prolog lies at
the logic being applied. Formulation or
Computation is carried out by running a
query over these relations.
4
Prolog
3. Prolog program is simply based on predicate
logic known as Horn clause. In prolog, we
compose the program using facts and rules and
we pose a query on query prompt about the
facts and rules we inserted.
5
Prolog Programming Basics
Horn Clause : Horn clause consists of head (left hand
side) and body (right hand side). Head can have 0 or 1
predicate and body can have list of predicates. That
means LHS has only single literal and RHS can have
more than one literals.
head:- body.
4. What is Prolog?
» Prolog or PROgramming in LOGics is a logical and
declarative programming language. It is one major example
of the fifth generation language that supports the
declarative programming paradigm. This is particularly
suitable for programs that involve symbolic or non-
numeric computation. This is the main reason to use
Prolog as the programming language in Artificial
Intelligence, where symbol manipulation and inference
manipulation are the fundamental tasks.
» In Prolog, we need not mention the way how one problem
can be solved, we just need to mention what the problem
is, so that Prolog automatically solves it. However, in Prolog
we are supposed to give clues as the solution method.
6
5. Programming languages are of two kinds:
Procedural (BASIC, Fortran, C++, Pascal,
Java);
Declarative (LISP, Prolog, ML).
In procedural programming, we tell the
computer how to solve a problem.
In declarative programming, we tell the
computer what problem we want solved.
SWI-Prolog offers a comprehensive free Prolog
environment. Since its start in 1987, SWI-Prolog
development has been driven by the needs of real
world applications. SWI-Prolog is widely used in
research and education as well as commercial
applications.
Prolog was originally designed to be an AI language,
and it is very well suited for expert systems, planning
systems and similar AI applications.
Logic Programming: Prolog
SWI - Prolog
The logic programming language PROLOG (Programmation en Logique)
was conceived by Alain Colmerauer at the University of Aix-Marseille,
France, where the language was first implemented in 1973. PROLOG was
further developed by the logician Robert Kowalski, a member of the AI group
at the University of Edinburgh.
7. Rules are extinctions of facts
that contain conditional
clauses. To satisfy a rule
these conditions should be
met.
Rules
And to run a prolog
program, we need some
questions, and those
questions can be answered
by the given facts and rules
Questions
Facts are statements
that describe object
properties or relations
between objects.
The fact is predicate that
is true, for example, if we
say, “Tom is the son of
Jack”, then this is a fact.
Facts
Prolog language basically has three different elements −
Facts Rules Queries
9. Prolog
» Example:
» “Arslan owns the book”
» Owns (arslan, book) relationship(object1, object2)
» The relationship has a specific order, arslan own the
book, but the book dose not owns arslan, and this
relationship and its representation above called fact.
11
10. English Predicate Calculus Prolog
If → :-
Not ~ Not
Or V ;
and ^ ,
female, Male, X, Y, mother_of, _father, Pro34
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. Rules for
forming names and predicate calculus are the same.
Using the following truth-functional symbols, the Prolog
expressions are comprised. These symbols have the same
interpretation as in the predicate calculus.
Symbols
Variables
11. 13
A fact is like a predicate expression. It is used to provide a declarative statement about the
problem. In a Prolog expression, when a variable occurs, it is assumed to be universally
quantified. Facts are specified in the form of the head. Head is known as the clause head. It will
take in the same way as the goal entered at the prompt by the user.
Facts
cat(momo). /* momo is a cat */
dog(pulu). /* pulu is a dog */
likes(arslan, jolie). /* Arslan likes Jolie*/
not(likes(Jolie, pasta)). /* Jolie does not like pasta */
For example:
Queries In Prolog, the query is the action of asking the program about the information which is available
within its database. When a Prolog program is loaded, we will get the query prompt,
?- 'It is sunny'. yes
?-
?- 'It is cold'.
no
?-
no
?-
?- ’momo is cat'. yes
?-
?- ’momo is dog'.
12. Rules extend the logic program capabilities. Rules are used
to provide the decision-making process in Prolog. Rules are
specified in the form:
head:- t1, t2, t3,….., tk. Where k>=1
The head is known as the clause of the head.
:- is known as the clause neck. It
is read as 'if'. The body of the
clause is specified by t1, t2, t3,…,
tk. It contains one or more
components, and it can be
separated using the commas. A
rule will read as 'head is true if
t1, t2, t3,…., tk are all true'.
Rules
In the following program, first two lines indicate the facts and last two lines indicate the rules:
dog(pulu). large(pulu).
cat(momo). large(momo).
large_animal(A) :- dog(A),large(A).
large_animal(C) :- cat(C),large(C).
The above rules mean that 'large_animal(A) is true if dog(A) is true, and large(A) is true, etc.'
The last line means that 'large_animal(C) is true if cat(C) is true, and large(C) is true.
Facts and Rules make up
the knowledge base.
13. likes(tarzan, jane).
likes(jane, tarzan).
likes(terk, jane).
facts
Arguments / objects / items
.dot is necessary to end the statement
relation
friends(X,Y):- likes(X,Y), likes(Y,X).
Predicate name
head body
variables
Horn Clause
15. Queries
Rules
facts
Example 1 : Below food table shows the facts, rules, goals and their English meanings.
Queries / Goals
?- food(pizza). // Is pizza a food?
?- meal(X), lunch(X). // Which food is meal and lunch?
?- dinner(sandwich). // Is sandwich a dinner?
Facts English meanings
food(burger). // burger is a food
food(sandwich). // sandwich is a food
food(pizza). // pizza is a food
lunch(sandwich). // sandwich is a lunch
dinner(pizza). // pizza is a dinner
Rule
meal(X) :- food(X).
// Every food is a meal OR
Anything is a meal if it is a food
16. Queries
Rules
facts
Example 2 : Below student-professor relation table shows the facts, rules, goals and their English meanings.
.
Facts English meanings
studies(charlie, csc135). // charlie studies csc135
studies(olivia, csc135). // olivia studies csc135
studies(jack, csc131). // jack studies csc131
studies(arthur, csc134).
likes('John', car(bmw))
// arthur studies csc134
// Read as : john likes bmw car
gives(john, chocolate, jane). // Read as : john gives chocolate to jane
Rules
professor(X, Y) :-
teaches(X, C), studies(Y, C).
// X is a professor of Y if X teaches
C and Y studies C.
Queries / Goals
?- studies(charlie, What).
// charlie studies what? OR
What does charlie study?
?- professor(kirke, Students).
// Who are the students of
professor kirke.
17. 19
Lists and Sequence in Prolog
In Prolog, the list builder uses brackets[...]. A list is referred by the
notation [A | B] in which, A is the first element, and whose tail is B.
The following example shows the three definitions, where the first
element of the list is refereed by the 'car', the tail of the list is referred
by 'cdr', list constructor is referred by the 'cons'.
car([A | B], A).
cdr([A | B], B).
cons[A, B, [A | S]).
Where,
•A is the head(car) of [A | B].
•B is the tail(car) of [A | B].
•Put A at the head and B as the tail constructs the list [A | S].
The predicate 'member/2' definition is described as follows:
member(A, [A | S]).
member(A, [B | S]) :- member(A, S).
The clauses can be read as follows:
•A is a list member whose first element is A.
•A is a list member whose tail is S if A is a member of S.
18. The looping facility is contained in most of the programming
languages. Looping is used to enable a set of instructions to
be repeatedly executed either a fixed number of times or
until a given condition met. Prolog has no looping facility,
but we can obtain a similar effect. Using this effect, we can
evaluate a sequence of goals repeatedly. In various ways,
this can be done like built-in predicates, recursion,
backtracking, or a combination of these.
In the above example, we define the loop predicate in
terms of itself. The second clause read as 'to loop from N,
first write the N's value, then subtract one to provide S,
then loop from M'. This process will terminate using
the first clause: 'when the argument is 0, then stop or do
nothing'. Here the first clause is called as terminated
condition.
Looping a fixed number of times
To execute the instruction a fixed number of times, many
programming languages provide 'for loop'. In Prolog, there
is no such facility available directly, but using recursion, we
can obtain a similar effect, which is shown in the following
programs:
Loops in Prolog
Example 1:
This program outputs the integer value from a specified
value down to 1.
loop(0).
loop(N) :- N>0, write('value of N is: '), write(N), nl.
S is N-1, loop(S).
?- loop(4).
value of N is: 4
value of N is: 3
value of N is: 2
value of N is: 1
yes
19. Here are some simple clauses.
The following queries yield the specified answers.
How do you add the following facts?
likes(mary,food).
likes(mary,juice).
likes(john,juice).
likes(john,mary).
| ?- likes(mary,food).
yes.
| ?- likes(john,juice).
yes.
| ?- likes(john,food).
no.
John likes anything that Mary likes
John likes anyone who likes juice
John likes anyone who likes themselves
Example 3 :
20. Some Applications of Prolog
Prolog is used in various
domains. It plays a vital role in
automation system. Following
are some other important
fields where Prolog is used −
Prolog is used in various
domains. It plays a vital role in
automation system. Following
are some other important
fields where Prolog is used −
• Intelligent Database Retrieval
• Natural Language Understanding
• Specification Language
• Machine Learning
• Robot Planning
• Automation System
• Problem Solving
• Expert Systems
What is
Prolog used
for
Good at
▪ Grammars and Language processing,
▪ Knowledge representation and reasoning,
▪ Unification,
▪ Pattern matching,
▪ Planning and Search.
i.e. Prolog is good at Symbolic AI.
?
21. 23
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.
2. Sometimes input and output is
not easy.
Disadvantages :
1. LISP (another logic
programming language)
dominates over prolog with
respect to I/O features
3. Repetitive number crunching,
Representing complex data
structures,
Input/output (interfaces).