SlideShare a Scribd company logo
Introduction to Prolog
Different programming languages to
implement AI problem
 AIML (Artificial Intelligence Markup Language)
 IPL (Information Processing Language)
 LISP
 PROLOG (Programming in Logic)
 Planner
 Haskell
 Wolfram Language
History of prolog
 developed in 1972
 by Alain Colmerauer and P. Roussel
 at the University of Marseilles in France.
Prolog
 Prolog is programming language.
 Prolog (pro + log = programming in logic)
 Prolog = logic + programming
The Family Tree
Who is father of Roy?
Bob is male.
May is Female.
Bo is Jill’s brother.
Kath is Bo’s sister.
Bo is father of Ali.
Ron is brother of Ali.
Ali is sister of Ron.
Bo is husband of Joy.
Joy is Bo’s wife.
Ben id Jill’s husband.
Jill is Ben’s wife.
Bo is father of Roy.
Jeff is kath’s husband.
Kath is Jeff’s wife.
Tim is brother of Roy.
Beth is daughter of Jeff and Kath.
Tim is son of Jeff and Kath .
Declarative v/s Imperative
Declarative Language Imperative Language
Programmer specifies what is to be
computed
Programmer specifies how this to be
computed
A set of statements A sequence of commands
Can be divided in to Functional
Language and Logic Programming
Divided in to procedural Language and
Object Oriented Language
Logic Programming (PROLOG)
Functional Language (Haskell)
Procedural Language (C , PASCAL)
Object Oriented Language (C++, Java)
Why Prolog in Intelligent?
Because
..
It approaches the
problems in the
same way that the
human does.
Applications for prolog
‱ Expert Systems
‱ Natural Language Processing
‱ Robotics
‱ Gaming and Simulation
Facts, Objects and Predicates
Collection of knowledge - database
 The main part of prolog program consist of collection
of knowledge about specific subject.
 This collection is known as database.
 Database can be expressed in facts and rules.
Knowledge Base
Bob is male.
Bo is father of Ron.
Statements like this is known as facts.
Natural Language Translation in Prolog
Bob is male. Male(bob).
May is Female. Female(May).
Bo is Jill’s brother. Brother(Bo,Jill).
Bo is husband of Joy. Husband(Bo,Joy).
Jill is Ben’s wife. Wife(Jill,Ben).
Jeff is kath’s husband Husband(Jeff,Kath).
Beth is daughter of Jeff and Kath. Daughter(Beth,Jeff,Kath).
Expressing Facts
 English- The right speaker is dead.
Prolog- is (right_speaker, dead)
 English- Bill is an employee.
Prolog- employee(Bill)
 English- Bob is married to Marry.
Prolog- married_to(bob,mary)
 English- The speaker is defective.
Prolog- defective(speaker)
 English- Tom is student.
Prolog- student(Tom)
 English- Ganesh is son of Shiva and Parvati.
Prolog- son(Ganesh, Shiva, Parvati)
Some Facts about “Fact”
 Full stop “.” at the end of every fact.
 Functors must begin with a small letter.
 There must never be a space between the functor and the
opening bracket!
 The number of argument in a fact is called “arity”.
 Eg. Female (mary) is an instance of female/1.
 Where female is functor and 1 is arity.
 Husband(Jeff,Kath) is an instance of Husband/2.
 Daughter(Beth,Jeff,Kath) is an instance of daughter/3.
 Arity is useful to say female/1 is different than female/2.
clause
Clause
Facts Rules
clauses
 In Prolog, rules (and facts) are called clauses.
 Clause in Prolog is a unit of information in a Prolog
program ending with a full stop ("."). A clause may
be a fact, like:
likes(mary, pizza).
food(pizza).
 Notice the period at the end of clause.
 The above clauses are also known as base clause.
predicates
Facts Rules
Predicates Periods
Clause
Predicate
 Has_a(bill,computer).
 The entire expression before the period (.) is known
as predicate.
 A predicate is a function with a value of true or false.
 Predicate express a property or a relationship.
 Here “has_a” is name of predicate.
Relations
clauses
Facts Rules
Predicates Periods
Relations Arguments
Relations
 is (reight_speaker,dead)
 The Word “is” is the relation in the example.
 A relation is a name that defines the way in which a
collection of objects (or objects and variables
referring to objects) belong together.
Fact Relation
has_a(bill,computer). has_a
is_a(collie,dog). is_a
likes(sue,chocolate). likes
Argument
 Employee(bill)
 Eligible(mary)
 Marital_status(joyce,married)
 The elements within the parenthesis are arguments
of the predicate.
Arguments
clauses
Facts Rules
Predicates Periods
Relations Arguments
Object Variable
Object and variable
 Is(right_speaker,dead)
 An object is the name of an element of a certain type.
 It represents an entity or a property of an entity in
the real world.
 Here right_speaker and dead are object
 Likes(X,Y)
 Here X and Y are variable with specific data type.
Several Things about clauses
 A given relation can have any number of objects. A predicate
can have any number of arguments, including zero. The
number of argument is called the arity of the predicate.
 An object name can represent a physical entity (Bob,
automobiles) or an abstract concept (defective). It can be a
noun, adverb or adjective. Indeed, it can any sequence of
characters.
 Objects are always singular. Ex, automobiles is considered as
singular object.
 Certain names are reserved in Prolog and should not be used
for object names. Ex, abs, not, fail ,if , display
 The prolog expression does not have to contain all the words
of the English expression.
Think and give Answer

 Likes( tom, janet)
 Likes( janet , tom)
 Both predicates are same????
First prolog program
Parts of Prolog Program
 A standard prolog program consist of 4 parts.
1. Domains
2. Predicates
3. Clauses
4. goal
Structure of Prolog Program
/* comments */
% A full line comment
domains
// 
.. //
predicates
//



.//
clauses
//



.//
Goal
//




..//
Turbo Prolog domain Type
 Char : Single character enclosed between single
quotation mars. Ex: ‘A’ , ’X’ , ’W’
 Integer: Integer from -32768 to +32768
Ex : 1, 735,1001
 Real : floating point number
Ex: 2.54, 6,78
 Symbol : character sequence of letters, numbers and
underscores with the first character as lower case
letter. Ex: status, disease
 File: symbolic file name. Ex, file.txt
Domains
 Frank is a male who is 45 years old.
person(Frank,male,45)
person(symbol , symbol , integer)
domains
Name , gender=symbol
Age=integer
domains
 The domains section is where you declare any
domains you're using that aren't Prolog's standard
domains.
 Domains enable you to give distinctive names to
different kinds of data that would otherwise look
alike.
 It is sometimes useful to declare a domain when you
want to clarify portions of the predicates section.
Declaring your own domains helps document the
predicates that you define by giving a useful name to
the argument type.
Predicates
 Frank is a male who is 45 years old.
person(Frank,male,45)
person(symbol , symbol , integer)
predicates
person(symbol , symbol , integer)
 Frank is a male who is 45 years old.
person(Frank,male,45)
person(symbol , symbol , integer)
Predicates
 If you define your own predicate in
the clauses section of a Prolog program,
you must declare it in a predicates section,
or Prolog won't know what you're talking about.
 When you declare a predicate, you
tell Prolog which domains the arguments of that
predicate belong to.
 You don't need to declare any of Prolog's built-
in predicates that you use in your program
clauses : An instance of predicate
 Frank is a male who is 45 years old.
person(Frank,male,45)
clauses
person(frank,male,45)
person(sue,female,26)
person(bob,male,39)
clauses
 The clauses section is where you put all the facts and
rules that make up your program.
 Clauses for a given predicate must be placed
together in the clauses section; a sequence of clauses
defining a predicate is called a procedure.
A simple Family Tree without Rules
Goal:
1. female(sophia)
2. Parent(charles, james)
3. Male(james)
4. Parent(george, charles)
5. Female(charles)
domains
mname , fname = symbol
Predicates
male(mname)
female(fname)
clauses
male(james).
male(charles).
male(charles).
male(james).
male(george).
female(catherine).
female(elizabeth).
female(sophia).
parent(charles, james).
parent(elizabeth, james).
parent(charles2, charles).
parent(catherine, charles).
parent(james, charles).
parent(sophia, elizabeth).
parent(george, sophia).
A simple Prolog Program
domains
disease , indication=symbol
predicates
sympton(disease,indication)
clauses
sympton(chicken_pox,high_fever).
sympton(chicken_pox, chills).
sympton(flu,chills).
sympton(cold,mild_body_ache).
sympton(cold, runny_nose).
sympton(flu,runny_nose).
sympton(flu,moderate_cough).
Goal: sympton(cold , runny_nose) True
Introduction To Term and different
data structures in Prolog
Term
 Data structures in PROLOG = Terms
 Term is a basic data structure in Prolog.
i.e., everything including program and data is
expressed in form of term.
 The generic name for all forms of Prolog data is
"term".
Prolog Data structure/Data Type
Term/data object
simple object structure
constants variables
Atoms numbers
Atom
 Atom is non-numeric literal constants.
 Atom can be constructed in three ways.
 Atoms can be strings of following characters.
1. upper-case letters (A,B,
..Z)
2. Lowe-case lettres (a,b,
..z)
3. Digits (0,1,2
.9)
4. Special characters (+ , _ , * , / , < , > , = , : , . , & , _ , ~ )
Atom
1. String of letters , digits and underscore character, ‘_’
starting with a lowercase letter.
Ex, anna
nil
x25
x_25
x_25AB
x_
x_ _y
alpha_beta_procedure
miss_Jones
sarah_jones
Atom
2. Strings of special characters
Ex, <--->
======>


::=
When using atom of this form, some care is necessary
because some string of special characters already
have a predefined meaning; an example is ‘:-’
Atom
3. String of characters enclosed in single quotes.
This is useful if we want, for ex, to have an atom that
starts with a capital letter.
Ex, ‘Tom’
‘South_America’
‘Sarah Jones’
Confusing!!!!! How????
Atoms and arguments are different things! Atoms
may be used as arguments but not all arguments are
automatically atoms!
Number
 Number include integer numbers and real numbers.
Ex, 1
1313
0
-97
3.14
-0.0035
100.2
Variables
 Variables are string of letters, digits and underscore
characters. They are start with an upper-case letter
or and underscore character.
Ex, X
Result
Object2
Participant_list
ShoppingList
_x23
_23
Ground Term
 The variable free term is known as ground term.
 Ex,
person(peter, mueller, date(27, 11, 2007))
Prolog Program
domains
disease , indication=symbol
predicates
sympton(disease,indication)
clauses
sympton(chicken_pox,high_fever).
sympton(chicken_pox, chills).
sympton(flu,chills).
sympton(cold,mild_body_ache).
sympton(cold, runny_nose).
sympton(flu,runny_nose).
sympton(flu,moderate_couh).
Goal: sympton(Disease, runny_nose)
Disease=cold
Disease=flu
2 solutions
Anonymous Variable / Don’t care variable
 Goal: symptom(_,chills)
True
 Goal: likes(jane,_). (Jane likes everything)
 Because there is no variable, there is no binding; if prolog
program can match the relation name and the last
argument, the goal succeeds.
 Prolog doesn’t tell you which disease had the symptom
chills, as it would have if you had used ordinary variable.
 Note that in a clause, the anonymous variable stands for
all values, while in a goal it is satistied if at least one
value corresponds to it.
Think Think
.
 Can we use underscore in a clause ??
 If yes, what is impact of it

Structure
 Structure objects are objects that have several components.
 The components themselves can, in turn be structure.
 For ex, the date can be viewed as structure with three
components: day,month,year.
 Although composed of several components , structures are
treated in the program as single objects.
 In order to combine the component into single we choose a
functor.
 For Ex, date(1,may,2001)
 All the components in this example are constants (2 integer
and one atom).
 Date is functor.
Which of the following are syntactically correct
prolog object? What kind of object are they?
1. Diana
2. diana
3. ‘Diana’
4. _diana
5. ‘Diana goes south’
6. goes(diana,south)
7. 45
8. 5(X,Y)
9. +(north,west)
10. three(Black(Cats))
1. Variable
2. atom
3. atom
4. variable
5. atom
6. structure
7. number
8. incorrect
9. structure
10. structure
Compound Goal
domains
disease , indication=symbol
predicates
sympton(disease,indication)
clauses
sympton(chicken_pox,high_fever).
sympton(chicken_pox, chills).
sympton(flu,chills).
sympton(cold,mild_body_ache).
sympton(cold, runny_nose).
sympton(flu,runny_nose).
sympton(flu,moderate_couh).
Goal: symptom(Disease,runny_nose) and
symptom(Disease,mild_body_ache)
Disease=cold
1 solution
True
Prolog
work from
left to
right for
proving
compound
goal.
Backtracking : Heart of Prolog Program
domains
disease , indication=symbol
predicates
sympton(disease,indication)
clauses
sympton(chicken_pox,high_fever).
symptom(viral_syndrom,mild_body_ache)
sympton(chicken_pox, chills).
sympton(flu,chills).
sympton(cold, runny_nose).
sympton(flu,runny_nose).
sympton(flu,moderate_couh).
Symptom(viral_syndrom,runny_nose).
Goal: symptom(Disease,runny_nose) and
symptom(Disease,mild_body_ache)
Disease=cold
Disease=flu
Disease=
viral_syndrom
Backtracking
 The solution of compound goal proceeds from left to
right
 If any condition in the chain fails, prolog backtracks
to the previous condition, tries to prove it again with
another variable binding, then moves forward again
to see if the failed condition will succeed with the
new binding,
 Prolog moves relentlessly forward and backward
through the conditions, trying every variable binding
in an attempt to get the goal to succeed in many ways
as possible.
Rules in Prolog
Rules
 A rule is an expression that indicates that the truth of
a particular fact depends upon one or more other
facts.
 Ex,
A and B are sisters if
A and B are both female and
they have the same father and
they have the same mother and
A is not the same as B
 Sister(A,B):-
female(A),female(B),father(X,A),father(X,B),mother
(Y,A),mother(Y,B),not_same(A,B).
Rules
 The symbol ‘ :- ‘ is known as turnstile or break
operator.
 ‘ :- ’ is pronounced as ‘ If ’.
 Syntax:
head_of_the_rule :- body_of_the_rule
(a.k.a) (a.k.a)
conclusion antecedent
(consist of)
premises 1,premises 2, 
premises n
Rules
 Each premises are connected by the word ‘and’ or
‘conjunction’ (^).
 The ‘goal’ is true if all the conditions specified for the
goal are true.
 If all the premises are true, the conclusion is true; if
any premises fails the conclusion fails.
 A rule expresses a relationship between facts. Any
given prolog program is simply a database
(collection of clauses) of facts and rules.
Conjunction and disjunction in rule
 A comma expresses an ‘and’ relationship and
semicolon expresses an ‘or’ relationship.
 Holiday in college if
there is Sunday or
there is public holiday
 Holiday(college):-day(Sunday);day(public_holiday)
OR
Holiday(college):-day(Sunday).
Holiday(college):-day(public_holiday)
Formal Reasoning
 The process of using rules and facts to solve a
problem is called formal reasoning.
Some important points about prolog execution
Rules
 In medical diagnosis system you have seen how the
variable are used.
 There are no “global” variable; that is, variables that
maintain a value throughout the entire program’s
execution.
 A variable is local to the clause of which they are part.
 Even if the same variable name is used in another clause ,
it is not the same variable.
 Ex, daughter(X):-father(Roy),mother(Sue).
daughter(X):-father(Bob),mother(Lice).
A term is said to be unify if
..
 Unification is a pattern matching process.
1. Both terms appear in predicates that have the same
number of arguments (i.e. same arity), and both
terms appear in the same position in their
predicate.
2. Both terms appear as argument of the same type - a
symbol type can only unify with a symbol and so
on.
3. All subterms unify with each other .
Rules for Unification (1)
1. A variable that is free will unify with any term that
satisfies the preceding conditions. After unification, the
variable is bound to the value of the term.
likes(ron,br0om).
likes(harry,X):-likes(ron,X).
Goal: likes(harry,broom)
Here Variable X is bounded with the value Broom.
Rules for Unification (2)
2. A constant can unify with itself or any free variable.
If the constant is unified with variable, the variable
will be bound to the value of the constant.
Ex, likes(harry,school).
Goal : likes(harry,school)
Ex, likes(harry,school).
Goal: likes(harry,X).
Rules for Unification (3)
3. A free variable will unify with any other free
variable. After unifying, the two variable will act as
one. If one of the variable becomes bound, the other
will be bound to the same value.
Ex,
likes(ron,broom).
likes(harry,X):-likes(ron,X).
Goal: likes(harry,broom).
Instantiation & Unification Exercise
Unify With Result
Likes(jem,piano) Likes(jem,X) X=piano
yes
Likes(jem,X) Likes(Y,piano) Y=jem
yes
Unifiability : Binding, substitution and unifiers
 Testing equality of terms with variables:
 Terms T1 and T2 are unifiable if there is a substitution that makes them
equal!
 A binding is an association of a variable to a term .
Two sample bindings: Name mueller and MM 11
 A substitution is a set of bindings
A sample substitution: {Name mueller, MM 11 }
 A unifier is a substitution that makes two terms equal
The above substitution is a unifier for the two person/3 terms above
The ‘not’ predicate
 To explicitly express that in the database particular
fact is not true.
 To try this use built-in ‘not’ predicate.
 The ‘not’ predicate cannot be used to express a fact
or appear in the head of a rule. It can only be used in
a premises.
 Ex, replace(right_speaker) :-
not(is(right_speaker,functional).
 In the case if is(right_speaker,functional) is in
database the rule will fail.
‘not’ predicate
 Be careful in your program design to distinguish
between the use of the not predicate and the
omission of facts from the databse.
 If a fact is not in the database, prolog considers it
false.
 However this falsity based on absence from the
database is not sufficient to prove a not premise.
 The absence of the is fact above would not prove the
not premise in the rule.
Which are the connectors available that connects
the clauses
 implication (:-)
 conjunction (,)
 disjunction (;)
Scope of variable in prolog
 The scope of a variable is the clause in which it
appears

More Related Content

PPTX
Lect 02 first portion
PPTX
5. gray level transformation
PPT
Theory of Computation Unit 5
PPT
Introduction to logic and prolog - Part 1
PDF
Frequency Domain FIltering.pdf
PDF
Dictionary Based Compression
PPTX
1. digital image processing
PPT
Image enhancement
Lect 02 first portion
5. gray level transformation
Theory of Computation Unit 5
Introduction to logic and prolog - Part 1
Frequency Domain FIltering.pdf
Dictionary Based Compression
1. digital image processing
Image enhancement

What's hot (20)

PDF
Operations in Digital Image Processing + Convolution by Example
PPT
Knowledge Representation & Reasoning
PDF
Lecture 15 DCT, Walsh and Hadamard Transform
PPTX
From NLP to text mining
PDF
Chap7 2 Ecc Intro
PDF
First order logic
PPT
Wavelet transform in image compression
PPT
Natural language processing
PPTX
Regular Expression to Finite Automata
PDF
Planning Agent
PPTX
Operator precedance parsing
PPTX
BPMN and DMN for Processing Business Data with Camunda
PDF
Lexical analysis - Compiler Design
PPTX
Unit 4 sp macro
PPTX
Three Address code
PDF
Difference Between CISC RISC, Harward & Von-neuman
PPTX
Edge detection
PPSX
Image processing on matlab presentation
PPT
adder and subtractor
Operations in Digital Image Processing + Convolution by Example
Knowledge Representation & Reasoning
Lecture 15 DCT, Walsh and Hadamard Transform
From NLP to text mining
Chap7 2 Ecc Intro
First order logic
Wavelet transform in image compression
Natural language processing
Regular Expression to Finite Automata
Planning Agent
Operator precedance parsing
BPMN and DMN for Processing Business Data with Camunda
Lexical analysis - Compiler Design
Unit 4 sp macro
Three Address code
Difference Between CISC RISC, Harward & Von-neuman
Edge detection
Image processing on matlab presentation
adder and subtractor
Ad

Similar to Prolog PPT_merged.pdf (20)

PPTX
Plc part 4
PPTX
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
PDF
Prolog,Prolog Programming IN AI.pdf
PPT
________ ________1.ppt
PPT
Introduction to prolog
PPT
Introduction to prolog
PPT
Introduction toprolog
PPT
Introduction to prolog
PPT
Introduction toprolog
PPT
Introduction toprolog
PPT
Introduction toprolog
PPTX
Prolog final
PPTX
ProLog (Artificial Intelligence) Introduction
PPTX
Unit 3 Prolog.pptxUnit 3 Prolog.pptxUnit 3 Prolog.pptxUnit 3 Prolog.pptxUnit
PPTX
Prolog Programming : Basics
PPTX
Ics1019 ics5003
PPTX
Ics1019 ics5003
PPTX
PROLOG: Introduction To Prolog
PDF
ICS1019.pdf
PPT
Chaps 1-3-ai-prolog
 
Plc part 4
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
Prolog,Prolog Programming IN AI.pdf
________ ________1.ppt
Introduction to prolog
Introduction to prolog
Introduction toprolog
Introduction to prolog
Introduction toprolog
Introduction toprolog
Introduction toprolog
Prolog final
ProLog (Artificial Intelligence) Introduction
Unit 3 Prolog.pptxUnit 3 Prolog.pptxUnit 3 Prolog.pptxUnit 3 Prolog.pptxUnit
Prolog Programming : Basics
Ics1019 ics5003
Ics1019 ics5003
PROLOG: Introduction To Prolog
ICS1019.pdf
Chaps 1-3-ai-prolog
 
Ad

More from 20CE112YASHPATEL (8)

PPTX
InternetOFThings_network_communicqtionCoAP_.pptx
PPTX
Data_Processing_and_communication_bluetooth.pptx
PPTX
DLP_Presentation.pptx
PPTX
Communication Technologies in IOT.pptx
PPTX
RFID.pptx
PPTX
Network_Layer_and_Internet_Protocols_IPv.pptx
PPTX
Project.pptx
PPTX
Money Manager Presentation-2.pptx
InternetOFThings_network_communicqtionCoAP_.pptx
Data_Processing_and_communication_bluetooth.pptx
DLP_Presentation.pptx
Communication Technologies in IOT.pptx
RFID.pptx
Network_Layer_and_Internet_Protocols_IPv.pptx
Project.pptx
Money Manager Presentation-2.pptx

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
history of c programming in notes for students .pptx
PPT
Introduction Database Management System for Course Database
PDF
System and Network Administraation Chapter 3
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
AI in Product Development-omnex systems
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
history of c programming in notes for students .pptx
Introduction Database Management System for Course Database
System and Network Administraation Chapter 3
Odoo POS Development Services by CandidRoot Solutions
How to Migrate SBCGlobal Email to Yahoo Easily
Adobe Illustrator 28.6 Crack My Vision of Vector Design
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Understanding Forklifts - TECH EHS Solution
VVF-Customer-Presentation2025-Ver1.9.pptx
ai tools demonstartion for schools and inter college
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Odoo Companies in India – Driving Business Transformation.pdf
Online Work Permit System for Fast Permit Processing
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PTS Company Brochure 2025 (1).pdf.......
ISO 45001 Occupational Health and Safety Management System
AI in Product Development-omnex systems

Prolog PPT_merged.pdf

  • 2. Different programming languages to implement AI problem  AIML (Artificial Intelligence Markup Language)  IPL (Information Processing Language)  LISP  PROLOG (Programming in Logic)  Planner  Haskell  Wolfram Language
  • 3. History of prolog  developed in 1972  by Alain Colmerauer and P. Roussel  at the University of Marseilles in France.
  • 4. Prolog  Prolog is programming language.  Prolog (pro + log = programming in logic)  Prolog = logic + programming
  • 6. Who is father of Roy? Bob is male. May is Female. Bo is Jill’s brother. Kath is Bo’s sister. Bo is father of Ali. Ron is brother of Ali. Ali is sister of Ron. Bo is husband of Joy. Joy is Bo’s wife. Ben id Jill’s husband. Jill is Ben’s wife. Bo is father of Roy. Jeff is kath’s husband. Kath is Jeff’s wife. Tim is brother of Roy. Beth is daughter of Jeff and Kath. Tim is son of Jeff and Kath .
  • 7. Declarative v/s Imperative Declarative Language Imperative Language Programmer specifies what is to be computed Programmer specifies how this to be computed A set of statements A sequence of commands Can be divided in to Functional Language and Logic Programming Divided in to procedural Language and Object Oriented Language Logic Programming (PROLOG) Functional Language (Haskell) Procedural Language (C , PASCAL) Object Oriented Language (C++, Java)
  • 8. Why Prolog in Intelligent? Because
.. It approaches the problems in the same way that the human does.
  • 9. Applications for prolog ‱ Expert Systems ‱ Natural Language Processing ‱ Robotics ‱ Gaming and Simulation
  • 10. Facts, Objects and Predicates
  • 11. Collection of knowledge - database  The main part of prolog program consist of collection of knowledge about specific subject.  This collection is known as database.  Database can be expressed in facts and rules.
  • 12. Knowledge Base Bob is male. Bo is father of Ron. Statements like this is known as facts. Natural Language Translation in Prolog Bob is male. Male(bob). May is Female. Female(May). Bo is Jill’s brother. Brother(Bo,Jill). Bo is husband of Joy. Husband(Bo,Joy). Jill is Ben’s wife. Wife(Jill,Ben). Jeff is kath’s husband Husband(Jeff,Kath). Beth is daughter of Jeff and Kath. Daughter(Beth,Jeff,Kath).
  • 13. Expressing Facts  English- The right speaker is dead. Prolog- is (right_speaker, dead)  English- Bill is an employee. Prolog- employee(Bill)  English- Bob is married to Marry. Prolog- married_to(bob,mary)  English- The speaker is defective. Prolog- defective(speaker)  English- Tom is student. Prolog- student(Tom)  English- Ganesh is son of Shiva and Parvati. Prolog- son(Ganesh, Shiva, Parvati)
  • 14. Some Facts about “Fact”  Full stop “.” at the end of every fact.  Functors must begin with a small letter.  There must never be a space between the functor and the opening bracket!  The number of argument in a fact is called “arity”.  Eg. Female (mary) is an instance of female/1.  Where female is functor and 1 is arity.  Husband(Jeff,Kath) is an instance of Husband/2.  Daughter(Beth,Jeff,Kath) is an instance of daughter/3.  Arity is useful to say female/1 is different than female/2.
  • 16. clauses  In Prolog, rules (and facts) are called clauses.  Clause in Prolog is a unit of information in a Prolog program ending with a full stop ("."). A clause may be a fact, like: likes(mary, pizza). food(pizza).  Notice the period at the end of clause.  The above clauses are also known as base clause.
  • 18. Predicate  Has_a(bill,computer).  The entire expression before the period (.) is known as predicate.  A predicate is a function with a value of true or false.  Predicate express a property or a relationship.  Here “has_a” is name of predicate.
  • 20. Relations  is (reight_speaker,dead)  The Word “is” is the relation in the example.  A relation is a name that defines the way in which a collection of objects (or objects and variables referring to objects) belong together. Fact Relation has_a(bill,computer). has_a is_a(collie,dog). is_a likes(sue,chocolate). likes
  • 21. Argument  Employee(bill)  Eligible(mary)  Marital_status(joyce,married)  The elements within the parenthesis are arguments of the predicate.
  • 23. Object and variable  Is(right_speaker,dead)  An object is the name of an element of a certain type.  It represents an entity or a property of an entity in the real world.  Here right_speaker and dead are object  Likes(X,Y)  Here X and Y are variable with specific data type.
  • 24. Several Things about clauses  A given relation can have any number of objects. A predicate can have any number of arguments, including zero. The number of argument is called the arity of the predicate.  An object name can represent a physical entity (Bob, automobiles) or an abstract concept (defective). It can be a noun, adverb or adjective. Indeed, it can any sequence of characters.  Objects are always singular. Ex, automobiles is considered as singular object.  Certain names are reserved in Prolog and should not be used for object names. Ex, abs, not, fail ,if , display  The prolog expression does not have to contain all the words of the English expression.
  • 25. Think and give Answer
  Likes( tom, janet)  Likes( janet , tom)  Both predicates are same????
  • 27. Parts of Prolog Program  A standard prolog program consist of 4 parts. 1. Domains 2. Predicates 3. Clauses 4. goal
  • 28. Structure of Prolog Program /* comments */ % A full line comment domains // 
.. // predicates //



.// clauses //



.// Goal //




..//
  • 29. Turbo Prolog domain Type  Char : Single character enclosed between single quotation mars. Ex: ‘A’ , ’X’ , ’W’  Integer: Integer from -32768 to +32768 Ex : 1, 735,1001  Real : floating point number Ex: 2.54, 6,78  Symbol : character sequence of letters, numbers and underscores with the first character as lower case letter. Ex: status, disease  File: symbolic file name. Ex, file.txt
  • 30. Domains  Frank is a male who is 45 years old. person(Frank,male,45) person(symbol , symbol , integer) domains Name , gender=symbol Age=integer
  • 31. domains  The domains section is where you declare any domains you're using that aren't Prolog's standard domains.  Domains enable you to give distinctive names to different kinds of data that would otherwise look alike.  It is sometimes useful to declare a domain when you want to clarify portions of the predicates section. Declaring your own domains helps document the predicates that you define by giving a useful name to the argument type.
  • 32. Predicates  Frank is a male who is 45 years old. person(Frank,male,45) person(symbol , symbol , integer) predicates person(symbol , symbol , integer)  Frank is a male who is 45 years old. person(Frank,male,45) person(symbol , symbol , integer)
  • 33. Predicates  If you define your own predicate in the clauses section of a Prolog program, you must declare it in a predicates section, or Prolog won't know what you're talking about.  When you declare a predicate, you tell Prolog which domains the arguments of that predicate belong to.  You don't need to declare any of Prolog's built- in predicates that you use in your program
  • 34. clauses : An instance of predicate  Frank is a male who is 45 years old. person(Frank,male,45) clauses person(frank,male,45) person(sue,female,26) person(bob,male,39)
  • 35. clauses  The clauses section is where you put all the facts and rules that make up your program.  Clauses for a given predicate must be placed together in the clauses section; a sequence of clauses defining a predicate is called a procedure.
  • 36. A simple Family Tree without Rules Goal: 1. female(sophia) 2. Parent(charles, james) 3. Male(james) 4. Parent(george, charles) 5. Female(charles) domains mname , fname = symbol Predicates male(mname) female(fname) clauses male(james). male(charles). male(charles). male(james). male(george). female(catherine). female(elizabeth). female(sophia). parent(charles, james). parent(elizabeth, james). parent(charles2, charles). parent(catherine, charles). parent(james, charles). parent(sophia, elizabeth). parent(george, sophia).
  • 37. A simple Prolog Program domains disease , indication=symbol predicates sympton(disease,indication) clauses sympton(chicken_pox,high_fever). sympton(chicken_pox, chills). sympton(flu,chills). sympton(cold,mild_body_ache). sympton(cold, runny_nose). sympton(flu,runny_nose). sympton(flu,moderate_cough). Goal: sympton(cold , runny_nose) True
  • 38. Introduction To Term and different data structures in Prolog
  • 39. Term  Data structures in PROLOG = Terms  Term is a basic data structure in Prolog. i.e., everything including program and data is expressed in form of term.  The generic name for all forms of Prolog data is "term".
  • 40. Prolog Data structure/Data Type Term/data object simple object structure constants variables Atoms numbers
  • 41. Atom  Atom is non-numeric literal constants.  Atom can be constructed in three ways.  Atoms can be strings of following characters. 1. upper-case letters (A,B,
..Z) 2. Lowe-case lettres (a,b,
..z) 3. Digits (0,1,2
.9) 4. Special characters (+ , _ , * , / , < , > , = , : , . , & , _ , ~ )
  • 42. Atom 1. String of letters , digits and underscore character, ‘_’ starting with a lowercase letter. Ex, anna nil x25 x_25 x_25AB x_ x_ _y alpha_beta_procedure miss_Jones sarah_jones
  • 43. Atom 2. Strings of special characters Ex, <---> ======> 
 ::= When using atom of this form, some care is necessary because some string of special characters already have a predefined meaning; an example is ‘:-’
  • 44. Atom 3. String of characters enclosed in single quotes. This is useful if we want, for ex, to have an atom that starts with a capital letter. Ex, ‘Tom’ ‘South_America’ ‘Sarah Jones’
  • 45. Confusing!!!!! How???? Atoms and arguments are different things! Atoms may be used as arguments but not all arguments are automatically atoms!
  • 46. Number  Number include integer numbers and real numbers. Ex, 1 1313 0 -97 3.14 -0.0035 100.2
  • 47. Variables  Variables are string of letters, digits and underscore characters. They are start with an upper-case letter or and underscore character. Ex, X Result Object2 Participant_list ShoppingList _x23 _23
  • 48. Ground Term  The variable free term is known as ground term.  Ex, person(peter, mueller, date(27, 11, 2007))
  • 49. Prolog Program domains disease , indication=symbol predicates sympton(disease,indication) clauses sympton(chicken_pox,high_fever). sympton(chicken_pox, chills). sympton(flu,chills). sympton(cold,mild_body_ache). sympton(cold, runny_nose). sympton(flu,runny_nose). sympton(flu,moderate_couh). Goal: sympton(Disease, runny_nose) Disease=cold Disease=flu 2 solutions
  • 50. Anonymous Variable / Don’t care variable  Goal: symptom(_,chills) True  Goal: likes(jane,_). (Jane likes everything)  Because there is no variable, there is no binding; if prolog program can match the relation name and the last argument, the goal succeeds.  Prolog doesn’t tell you which disease had the symptom chills, as it would have if you had used ordinary variable.  Note that in a clause, the anonymous variable stands for all values, while in a goal it is satistied if at least one value corresponds to it.
  • 51. Think Think
.  Can we use underscore in a clause ??  If yes, what is impact of it

  • 52. Structure  Structure objects are objects that have several components.  The components themselves can, in turn be structure.  For ex, the date can be viewed as structure with three components: day,month,year.  Although composed of several components , structures are treated in the program as single objects.  In order to combine the component into single we choose a functor.  For Ex, date(1,may,2001)  All the components in this example are constants (2 integer and one atom).  Date is functor.
  • 53. Which of the following are syntactically correct prolog object? What kind of object are they? 1. Diana 2. diana 3. ‘Diana’ 4. _diana 5. ‘Diana goes south’ 6. goes(diana,south) 7. 45 8. 5(X,Y) 9. +(north,west) 10. three(Black(Cats)) 1. Variable 2. atom 3. atom 4. variable 5. atom 6. structure 7. number 8. incorrect 9. structure 10. structure
  • 54. Compound Goal domains disease , indication=symbol predicates sympton(disease,indication) clauses sympton(chicken_pox,high_fever). sympton(chicken_pox, chills). sympton(flu,chills). sympton(cold,mild_body_ache). sympton(cold, runny_nose). sympton(flu,runny_nose). sympton(flu,moderate_couh). Goal: symptom(Disease,runny_nose) and symptom(Disease,mild_body_ache) Disease=cold 1 solution True Prolog work from left to right for proving compound goal.
  • 55. Backtracking : Heart of Prolog Program domains disease , indication=symbol predicates sympton(disease,indication) clauses sympton(chicken_pox,high_fever). symptom(viral_syndrom,mild_body_ache) sympton(chicken_pox, chills). sympton(flu,chills). sympton(cold, runny_nose). sympton(flu,runny_nose). sympton(flu,moderate_couh). Symptom(viral_syndrom,runny_nose). Goal: symptom(Disease,runny_nose) and symptom(Disease,mild_body_ache) Disease=cold Disease=flu Disease= viral_syndrom
  • 56. Backtracking  The solution of compound goal proceeds from left to right  If any condition in the chain fails, prolog backtracks to the previous condition, tries to prove it again with another variable binding, then moves forward again to see if the failed condition will succeed with the new binding,  Prolog moves relentlessly forward and backward through the conditions, trying every variable binding in an attempt to get the goal to succeed in many ways as possible.
  • 58. Rules  A rule is an expression that indicates that the truth of a particular fact depends upon one or more other facts.  Ex, A and B are sisters if A and B are both female and they have the same father and they have the same mother and A is not the same as B  Sister(A,B):- female(A),female(B),father(X,A),father(X,B),mother (Y,A),mother(Y,B),not_same(A,B).
  • 59. Rules  The symbol ‘ :- ‘ is known as turnstile or break operator.  ‘ :- ’ is pronounced as ‘ If ’.  Syntax: head_of_the_rule :- body_of_the_rule (a.k.a) (a.k.a) conclusion antecedent (consist of) premises 1,premises 2, 
premises n
  • 60. Rules  Each premises are connected by the word ‘and’ or ‘conjunction’ (^).  The ‘goal’ is true if all the conditions specified for the goal are true.  If all the premises are true, the conclusion is true; if any premises fails the conclusion fails.  A rule expresses a relationship between facts. Any given prolog program is simply a database (collection of clauses) of facts and rules.
  • 61. Conjunction and disjunction in rule  A comma expresses an ‘and’ relationship and semicolon expresses an ‘or’ relationship.  Holiday in college if there is Sunday or there is public holiday  Holiday(college):-day(Sunday);day(public_holiday) OR Holiday(college):-day(Sunday). Holiday(college):-day(public_holiday)
  • 62. Formal Reasoning  The process of using rules and facts to solve a problem is called formal reasoning.
  • 63. Some important points about prolog execution Rules  In medical diagnosis system you have seen how the variable are used.  There are no “global” variable; that is, variables that maintain a value throughout the entire program’s execution.  A variable is local to the clause of which they are part.  Even if the same variable name is used in another clause , it is not the same variable.  Ex, daughter(X):-father(Roy),mother(Sue). daughter(X):-father(Bob),mother(Lice).
  • 64. A term is said to be unify if
..  Unification is a pattern matching process. 1. Both terms appear in predicates that have the same number of arguments (i.e. same arity), and both terms appear in the same position in their predicate. 2. Both terms appear as argument of the same type - a symbol type can only unify with a symbol and so on. 3. All subterms unify with each other .
  • 65. Rules for Unification (1) 1. A variable that is free will unify with any term that satisfies the preceding conditions. After unification, the variable is bound to the value of the term. likes(ron,br0om). likes(harry,X):-likes(ron,X). Goal: likes(harry,broom) Here Variable X is bounded with the value Broom.
  • 66. Rules for Unification (2) 2. A constant can unify with itself or any free variable. If the constant is unified with variable, the variable will be bound to the value of the constant. Ex, likes(harry,school). Goal : likes(harry,school) Ex, likes(harry,school). Goal: likes(harry,X).
  • 67. Rules for Unification (3) 3. A free variable will unify with any other free variable. After unifying, the two variable will act as one. If one of the variable becomes bound, the other will be bound to the same value. Ex, likes(ron,broom). likes(harry,X):-likes(ron,X). Goal: likes(harry,broom).
  • 68. Instantiation & Unification Exercise Unify With Result Likes(jem,piano) Likes(jem,X) X=piano yes Likes(jem,X) Likes(Y,piano) Y=jem yes
  • 69. Unifiability : Binding, substitution and unifiers  Testing equality of terms with variables:  Terms T1 and T2 are unifiable if there is a substitution that makes them equal!  A binding is an association of a variable to a term . Two sample bindings: Name mueller and MM 11  A substitution is a set of bindings A sample substitution: {Name mueller, MM 11 }  A unifier is a substitution that makes two terms equal The above substitution is a unifier for the two person/3 terms above
  • 70. The ‘not’ predicate  To explicitly express that in the database particular fact is not true.  To try this use built-in ‘not’ predicate.  The ‘not’ predicate cannot be used to express a fact or appear in the head of a rule. It can only be used in a premises.  Ex, replace(right_speaker) :- not(is(right_speaker,functional).  In the case if is(right_speaker,functional) is in database the rule will fail.
  • 71. ‘not’ predicate  Be careful in your program design to distinguish between the use of the not predicate and the omission of facts from the databse.  If a fact is not in the database, prolog considers it false.  However this falsity based on absence from the database is not sufficient to prove a not premise.  The absence of the is fact above would not prove the not premise in the rule.
  • 72. Which are the connectors available that connects the clauses  implication (:-)  conjunction (,)  disjunction (;)
  • 73. Scope of variable in prolog  The scope of a variable is the clause in which it appears