Unit 3:
Prolog Programming:
Prolog
Dr. Nancy Kumari
• Prolog is the most commonly used logic – Simple syntax
• Programming in LOGIC
• High- level interactive language.
• Logic programming language.
• Programming languages are of two kinds:
- Procedural-(we tell the computer how to solve
problem)-(BASIC, Pascal, C++,Java)
- Declarative –(what problem we want solved)-(LISP,
Prolog, ML).
- Used in – automated reasoning system
natural languages interfaces.
expert systems
Prolog
Dr. Nancy Kumari
Knowledge Base − This is one of the fundamental
parts of Logic Programming.
Facts, Rules and Queries − These are the building
blocks of logic programming. We will get some
detailed knowledge about facts and rules, and also
see some kind of queries that will be used in logic
programming.
Prolog Syntax
1. Between a pair of single quotes, a character is encapsulated.
2. Integer: An integer between -32768 and 32767 that is a full number.
3. Real: A peculiar character that is either positive or negative, followed by
numbers.
4. String: A collection of characters encased in a pair of double-quotes. Strings can
have up to 255 characters in them.
5. Symbol: A combination of letters (A to Z or a to z), numerals (0 to 9) and the
underscore(_) character.
6. Variables: A variable is a symbol that can have multiple values assigned to it at
different stages of the program’s execution.
7. Reserved terms: PROLOG features a few reserved words that should not be
substituted for user-defined names.
8. Arithmetic Operators: The basic arithmetic operators in PROLOG are +, -, *,
and /.
9. Relational Operators: PROLOG utilizes the relational operators,=, =>, >=, >=.
A relational operator in PROLOG can be either goal or subgoal. The relational
operator (=) resembles an assignment operator in appearance.
10.Terms have several different types: Numbers, Atoms, Variable, Array etc.
PROLOG Data Types & Data Structures
Clauses:
 This section contains all of the program’s information and
regulations. The same-named facts and rules must be
grouped together. A procedure is a set of sentences that
define a predicate. The user is requested to write in a goal
following a goal during execution.
Predicates:
 One or more clauses make up a predicate. Clauses
belonging to the same predicate must be in order. A
predicate can be declared as – in general. predicate name
(arg1, arg2, arg3,…, argn).
Domains:
 Even while PROLOG uses the same domains as in-built
domains, a user can choose problem-related meaningful
names for domains. User-defined domain names are
commonly used, but the PROLOG system does not
recognize them. As a result, the user provides information
about these user-defined domains in the domains section.
PROLOG Components
Database Handling in Prolog
Two types of databases :-static and dynamic.
● Static database is a part of the program that is complied along with it.
It does not change during execution of the program.
● Dynamic database can change dynamically at execution time and are of
two types.
Type1: created at each execution. It grows, shrinks and is deleted at the
end of program.
● This type of database is no longer available after program finishes its
execution and is called working memory.
Type2: Other type of dynamic databases are those which are stored in
files and called database files.
− These are consulted in any program whenever required.
− These types of databases are not part of any particular program and are
available for use in future by different programs using system defined
predicates called save and consult.
− While executing a Prolog program one can load database file(s) using
'consult' predicate. − These files can be updated dynamically at run time
and saved using 'save' predicate.
Prolog - Inputs and Outputs
We will see some techniques to handle inputs and outputs through
prolog.
We will use some built in predicates to do these tasks, and also see file
handling techniques.
Following topics will be discussed in detail −
Handling inputs and outputs
File handling using Prolog
Using some external file to read lines and terms
Character manipulation for input and output
Constructing and decomposing atoms
Consulting prolog files into other prolog program techniques.
Handling input and output
So far we have seen that we can write a program and the query on the
console to execute.
In some cases, we print something on the console, that are written in our
prolog code.
So here we will see that writing and reading tasks in more detail using
prolog. So this will be the input and output handling techniques.
Prolog - Built-In Predicates
In Prolog, we have seen the user defined predicates in most of the cases, but there are
some built-in-predicates. There are three types of built-in predicates as given below −
Identifying terms
Decomposing structures
Collecting all solutions
So this is the list of some predicates that are falls under the identifying terms group −
Predicate Description
var(X) succeeds if X is currently an un-instantiated variable.
novar(X) succeeds if X is not a variable, or already instantiated
atom(X) is true if X currently stands for an atom
number(X) is true if X currently stands for a number
integer(X) is true if X currently stands for an integer
float(X) is true if X currently stands for a real number.
atomic(X) is true if X currently stands for a number or an atom.
compound(X) is true if X currently stands for a structure.
ground(X) succeeds if X does not contain any un-instantiated
variables
Structure of PROLOG:
domains
/*…domain statements…*/
predicates
/*…predicate statements…*/
goal
/*…subgoall, subgoal, etc… */
clauses
/* …clauses (rules and facts)…*/
 Prolog programs are written using a syntax that is similar to natural
language. For example, a simple Prolog program might look like this:
man(john).
woman(mary).
capital_of(france, paris).
not(X,Y) :- man(X), woman(Y).
 In this example, the first three lines are facts, while the fourth line is a rule.
 The rule uses the not/2 predicate to state that if X is a man and Y is a
woman, then X is not Y.
% Facts
man(john).
woman(mary).
capital_of(france, paris).
% Rule
not(X,Y) :- man(X), woman(Y).
% Query 1
not(john, mary)?
% Query 2
capital_of(france, X)?
Basic Elements of Prolog
• A Program consist of clauses- facts, rules and questions.
• Some are always true(facts):
-Father(x, y).
• Some are dependent on others being true(rules):
-parent(Person1, Person2):-father(Person1, Person2)
• A knowledge base of facts
• teacher(x)
• teacher(y)
• father(x, z)
• mother(y, z)
• Queries
?-teacher(x) Yes
?-mother(x, z) No
?-doctor(x)
ERROR: Undefined procedure: doctor/1
? - prompt provided by
Prolog interpreter
Examples
Dr. Nancy Kumari
Facts-
•Tom is a cat
•Kunal loves to eat Pasta
•Hair is black
•Nawaz loves to play games
•Pratyusha is lazy.
Program -
cat(tom).
loves_to_eat(kunal,pasta).
of_color(hair,black).
loves_to_play_games(nawaz).
lazy(pratyusha).
Prolog - Operators
Dr. Nancy Kumari
calc :- X is 100 + 200,write('100 + 200 is '),write(X),nl,
Y is 400 - 150,write('400 - 150 is '),write(Y),nl,
Z is 10 * 300,write('10 * 300 is '),write(Z),nl,
A is 100 / 30,write('100 / 30 is '),write(A),nl,
B is 100 // 30,write('100 // 30 is '),write(B),nl,
C is 100 ** 2,write('100 ** 2 is '),write(C),nl,
D is 100 mod 30,write('100 mod 30 is '),write(D),nl.

Unit 3 Prolog.pptxUnit 3 Prolog.pptxUnit 3 Prolog.pptxUnit 3 Prolog.pptxUnit

  • 1.
  • 2.
    Prolog Dr. Nancy Kumari •Prolog is the most commonly used logic – Simple syntax • Programming in LOGIC • High- level interactive language. • Logic programming language. • Programming languages are of two kinds: - Procedural-(we tell the computer how to solve problem)-(BASIC, Pascal, C++,Java) - Declarative –(what problem we want solved)-(LISP, Prolog, ML). - Used in – automated reasoning system natural languages interfaces. expert systems
  • 3.
    Prolog Dr. Nancy Kumari KnowledgeBase − This is one of the fundamental parts of Logic Programming. Facts, Rules and Queries − These are the building blocks of logic programming. We will get some detailed knowledge about facts and rules, and also see some kind of queries that will be used in logic programming.
  • 4.
  • 5.
    1. Between apair of single quotes, a character is encapsulated. 2. Integer: An integer between -32768 and 32767 that is a full number. 3. Real: A peculiar character that is either positive or negative, followed by numbers. 4. String: A collection of characters encased in a pair of double-quotes. Strings can have up to 255 characters in them. 5. Symbol: A combination of letters (A to Z or a to z), numerals (0 to 9) and the underscore(_) character. 6. Variables: A variable is a symbol that can have multiple values assigned to it at different stages of the program’s execution. 7. Reserved terms: PROLOG features a few reserved words that should not be substituted for user-defined names. 8. Arithmetic Operators: The basic arithmetic operators in PROLOG are +, -, *, and /. 9. Relational Operators: PROLOG utilizes the relational operators,=, =>, >=, >=. A relational operator in PROLOG can be either goal or subgoal. The relational operator (=) resembles an assignment operator in appearance. 10.Terms have several different types: Numbers, Atoms, Variable, Array etc. PROLOG Data Types & Data Structures
  • 6.
    Clauses:  This sectioncontains all of the program’s information and regulations. The same-named facts and rules must be grouped together. A procedure is a set of sentences that define a predicate. The user is requested to write in a goal following a goal during execution. Predicates:  One or more clauses make up a predicate. Clauses belonging to the same predicate must be in order. A predicate can be declared as – in general. predicate name (arg1, arg2, arg3,…, argn). Domains:  Even while PROLOG uses the same domains as in-built domains, a user can choose problem-related meaningful names for domains. User-defined domain names are commonly used, but the PROLOG system does not recognize them. As a result, the user provides information about these user-defined domains in the domains section. PROLOG Components
  • 7.
    Database Handling inProlog Two types of databases :-static and dynamic. ● Static database is a part of the program that is complied along with it. It does not change during execution of the program. ● Dynamic database can change dynamically at execution time and are of two types. Type1: created at each execution. It grows, shrinks and is deleted at the end of program. ● This type of database is no longer available after program finishes its execution and is called working memory. Type2: Other type of dynamic databases are those which are stored in files and called database files. − These are consulted in any program whenever required. − These types of databases are not part of any particular program and are available for use in future by different programs using system defined predicates called save and consult. − While executing a Prolog program one can load database file(s) using 'consult' predicate. − These files can be updated dynamically at run time and saved using 'save' predicate.
  • 8.
    Prolog - Inputsand Outputs We will see some techniques to handle inputs and outputs through prolog. We will use some built in predicates to do these tasks, and also see file handling techniques. Following topics will be discussed in detail − Handling inputs and outputs File handling using Prolog Using some external file to read lines and terms Character manipulation for input and output Constructing and decomposing atoms Consulting prolog files into other prolog program techniques. Handling input and output So far we have seen that we can write a program and the query on the console to execute. In some cases, we print something on the console, that are written in our prolog code. So here we will see that writing and reading tasks in more detail using prolog. So this will be the input and output handling techniques.
  • 9.
    Prolog - Built-InPredicates In Prolog, we have seen the user defined predicates in most of the cases, but there are some built-in-predicates. There are three types of built-in predicates as given below − Identifying terms Decomposing structures Collecting all solutions So this is the list of some predicates that are falls under the identifying terms group − Predicate Description var(X) succeeds if X is currently an un-instantiated variable. novar(X) succeeds if X is not a variable, or already instantiated atom(X) is true if X currently stands for an atom number(X) is true if X currently stands for a number integer(X) is true if X currently stands for an integer float(X) is true if X currently stands for a real number. atomic(X) is true if X currently stands for a number or an atom. compound(X) is true if X currently stands for a structure. ground(X) succeeds if X does not contain any un-instantiated variables
  • 10.
    Structure of PROLOG: domains /*…domainstatements…*/ predicates /*…predicate statements…*/ goal /*…subgoall, subgoal, etc… */ clauses /* …clauses (rules and facts)…*/  Prolog programs are written using a syntax that is similar to natural language. For example, a simple Prolog program might look like this: man(john). woman(mary). capital_of(france, paris). not(X,Y) :- man(X), woman(Y).  In this example, the first three lines are facts, while the fourth line is a rule.  The rule uses the not/2 predicate to state that if X is a man and Y is a woman, then X is not Y. % Facts man(john). woman(mary). capital_of(france, paris). % Rule not(X,Y) :- man(X), woman(Y). % Query 1 not(john, mary)? % Query 2 capital_of(france, X)?
  • 11.
    Basic Elements ofProlog • A Program consist of clauses- facts, rules and questions. • Some are always true(facts): -Father(x, y). • Some are dependent on others being true(rules): -parent(Person1, Person2):-father(Person1, Person2) • A knowledge base of facts • teacher(x) • teacher(y) • father(x, z) • mother(y, z) • Queries ?-teacher(x) Yes ?-mother(x, z) No ?-doctor(x) ERROR: Undefined procedure: doctor/1 ? - prompt provided by Prolog interpreter
  • 12.
    Examples Dr. Nancy Kumari Facts- •Tomis a cat •Kunal loves to eat Pasta •Hair is black •Nawaz loves to play games •Pratyusha is lazy. Program - cat(tom). loves_to_eat(kunal,pasta). of_color(hair,black). loves_to_play_games(nawaz). lazy(pratyusha).
  • 13.
    Prolog - Operators Dr.Nancy Kumari calc :- X is 100 + 200,write('100 + 200 is '),write(X),nl, Y is 400 - 150,write('400 - 150 is '),write(Y),nl, Z is 10 * 300,write('10 * 300 is '),write(Z),nl, A is 100 / 30,write('100 / 30 is '),write(A),nl, B is 100 // 30,write('100 // 30 is '),write(B),nl, C is 100 ** 2,write('100 ** 2 is '),write(C),nl, D is 100 mod 30,write('100 mod 30 is '),write(D),nl.