Module-4-Prolog Material
Module-4-Prolog Material
What is Prolog
o Prolog stands for programming in logic. In the logic programming paradigm,
prolog language is most widely available. 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 find a solution. A logical relationship
describes the relationships which hold for the given application.
o 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.
o In 1981, a Japanese computer Project of 5th generation was announced. Prolog
features are 'Logical variable', which means that they behave like uniform data
structure, a backtracking strategy to search for proofs, a pattern-matching facility,
mathematical variable, and input and out are interchangeable.
o To deduce the answer, there will be more than one way. In such case, the run
time system will be asked to find another solution. To generate another solution,
use the backtracking strategy. Prolog is a weakly typed language with static
scope rules and dynamic type checking.
o Prolog is a declarative language that means we can specify what problem we
want to solve rather than how to solve it.
o Prolog is used in some areas like database, natural language processing, artificial
intelligence, but it is pretty useless in some areas like a numerical algorithm or
instance graphics.
o In artificial intelligence applications, prolog is used. The artificial intelligence
applications can be automated reasoning systems, natural language interfaces,
and expert systems. The expert system consists of an interface engine and a
database of facts. The prolog's run time system provides the service of an
interface engine.
o A basic logic programming environment has no literal values. An identifier with
upper case letters and other identifiers denote variables. Identifiers that start with
lower-case letters denote data values. The basic Prolog elements are typeless.
The most implementations of prolog have been enhanced to include integer
value, characters, and operations. The Mechanism of prolog describes the tuples
and lists.
o Functional programming language and prolog have some similarities like Hugs. A
logic program is used to consist of relation definition. A functional programming
language is used to consist of a sequence of function definitions. Both the logical
programming and functional programming rely heavily on recursive definitions.
Applications of Prolog
o Specification Language
o Robot Planning
o Natural language understanding
o Machine Learning
o Problem Solving
o Intelligent Database retrieval
o Expert System
o Automated Reasoning
Starting Prolog
Prolog system is straightforward. From one person to other person, the precise details
of Prolog will vary. Prolog will produce a number of lines of headings in the starting,
which is followed by a line. It contains just
?-
The above symbol shows the system prompt. The prompt is used to show that the
Prolog system is ready to specify one or more goals of sequence to the user. Using a full
stop, we can terminate the sequence of goals.
Example
Welcome to Javatpoint
Example of Prolog
yes
?- prompt shows the sequence of goal which is entered by the user. The user will not
type the prompt. Prolog system will automatically generate this prompt. It means that it
is ready to receive a sequence of goals.
Halt and statistics are the two other built-in predicates. In almost all Prolog versions,
these predicates are provided as standard.
o ?-Halt.
The above command is used to terminate the Prolog system.
o ?-statistics.
This command will cause the Prolog system statistics. This statistics feature is
mainly used to experienced user. In statistics, the following things will generate:
The above output ends with Yes. Yes, specify that the goal has succeeded, as halt,
statistics, and many other built-in predicates always do. When they evaluate, their values
produce, which lies in the side-effects.
'Query' is a sequence of one or more goals. These goals are entered by the user at the
prompt. In this tutorial, we are generally using the 'sequence of goals' term.
Types of Prolog
Prolog is used to provide the Tuples, lists, numbers, atoms, and patterns. In this section,
we can define the type of objects that are passed as arguments.
Simple Types
This type of Prolog is implementation-dependent. The following table shows the
implementation of a simple type of prolog:
TYPE VALUES
Variables Variables
Integer Integers
The Boolean constants are not passed as an argument. Variables describe the character
string. The character strings start with a capital letter or upper case letter. Atoms are
constants that have no numerical value. All the atoms start with a lower case letter or
small letter.
Composition Types
The distinction between data and program are blurred in prolog. In the argument, data
is often passed to predicates. In prolog, the most common data structure is lists. Lists
are much like the stack in which we can only sequentially access the lists of elements,
and much like the array in which we have a list of elements sequentially.
Type Predicates
The user has to determine the parameter type because prolog is a weakly typed
language. To determine the parameter type in prolog, the following built-in predicate
can be used.
PREDICATE CHECKS IF
atom(A) A is an atom
var(V) V is a variable
integer(I) I is an Integer
T =L T is a term, L is a list
Symbols
Using the following truth-functional symbols, the Prolog expressions are comprised. These
symbols have the same interpretation as in the predicate calculus.
If > :-
Not ~ Not
Or V ;
and ^ ,
Variable
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.
Example
1. female
2. Male
3. X
4. y
5. mother_of
6. _father
7. Pro34
Facts
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.
For example:
1. holi.
2. cat(bengal). /* bengal is a cat */
3. dog(rottweiler). /* rottweiler is a dog */
4. likes(Jolie, Kevin). /* Jolie likes Kevin */
5. likes(A, Kevin). /* Everyone likes Kevin */
6. likes(Jolie, B). /* Jolie likes everybody */
7. likes(B, Jolie), likes(Jolie, B). /* Everybody likes Jolie and Jolie likes everybody */
8. likes(Jolie, Kevin); likes(Jolie, Ray). /* Jolie likes Kevin or Jolie likes Ray */
9. not(likes(Jolie, pasta)). /* Jolie does not like pasta */
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,
1. ?-
After this, we can ask about the information to the run time system. Using the above
simple database, we can ask a question to the program like
1. ?- 'It is sunny'.
1. yes
2. ?-
The system responds to the query with yes if the database information is consistent to
answer the query. Using the available database information, we can also check that the
program is capable of proving the query true. No indicates that the fact is not deducible
based on the available information.
The system answers no to the query if the database does not have sufficient
information.
1. ?- 'It is cold'.
2. no
3. ?-
1. Numbers
In Prolog, all versions allow the use of integers. Any sequence of numbers from 0 to 9 is
written as numbers. The numbers are preceded by + or - sign.
For example,
19M
426
1. 278
2. -64
3. +8
4. 057
The use of numbers with decimal points is allowed by most versions of Prolog. Just like
the integer, they are written. It contains a single decimal point. But this decimal point
cannot exist before an optional plus(+) or minus(-) sign.
For example:
1. 4.32
2. -7.91
3. +8.45.
2. Atoms
Atoms do not have any numerical value. Atoms are like the constants. The atoms can be
written in three different ways.
a. Any sequence of upper case or lower case letters, underscores, numerals, starting with
a lower case letter. For example,
1. jackson
2. toady_is_Saturday
3. smart_jack
4. x64_YZ
5. but not
6. Weekend
7. Toady-is-Saturday
8. 64xyz
b. In single quotes, any sequence of characters can be enclosed. It includes lower case
letters and spaces. For example,
1. 'today-is-Saturday'
2. 'Today is Saturday'
3. '64xyz'
c. Any sequence of one or more characters includes = @ > < # & + - * /. For example,
1. <
2. ++-
3. <=
4. -++
3. Variable
A variable is a name that is used to stand for a term. The variable name can be any
sequence of one or more upper case letters or lower case letters, underscore, and
numerals. It can begin with an underscore or lower case letters.
For example:
1. A
2. jack
3. Javatpoint_X
4. _987X
5.
6. but not
7.
8. 82_XYZ
9. Javatpoint-X
10. jack
Prolog Clauses
In Prolog, the program contains a sequence of one or more clauses. The clauses can run
over many lines. Using a dot character, a clause can be terminated. This dot character is
followed by at least one 'white space' character. The clauses are of two types: facts and
rules.
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. The head of a clause
must be a compound term or an atom. Compound terms and atoms are collectively
called as call terms.
Predicates in Prolog
There was a simple program that has five clauses. Head is a compound term for each
first three clauses with functor parent. It has two arguments with 2 arity.
1. parent(emma, robert).
2. parent(A, B) :- father(A, B).
3. parent(A, B) :- mother(A, B).
4. father(kevin, mary).
5. mother(anne, mary).
For example:
1. parent(kevin).
2. parent(A) :- son(A, B).
3. /* A is parent if A has a son B */