SlideShare ist ein Scribd-Unternehmen logo
1 von 38
Shivani Saluja
Assistant Professor
IMSEC GHAZIABAD
Introduction
 SWI_PROLOG
 Fundamentals of PROLOG
 Applications
 References

Open source software is software that
can be freely used, changed, and
shared (in modified or unmodified
form) by anyone.





PROgramming in LOGic
Declarative language
Emphasis on what rather than how
It is widely used in the field of AI
Problem in Declarative Form

Logic Machine

Basic Machine
SWI-Prolog offers a comprehensive FREE SOFTWARE
Prolog environment.
 Link for downloading:
http://www.swi-prolog.org/download/stable
 A Self-installing executable for MS-Windows: swipl-win.exe
 Works on Windows XP
 LINUX versions are also available.

Facts
 Rules
 Query
 Unification
 Resolution
 Backtracing
 Cuts and negations








Facts are statements about what is true about a problem,
instead of instructions how to accomplish the solution.
The Prolog system uses the facts to work out how to
accomplish the solution by searching through the space of
possible solutions.
It is defined by an identifier followed by an n-tuple of
constants.
A relation identifier is referred to as a predicate
When a tuple of values is in a relation we say the tuple
satisfies the predicate.





Names of relationship and objects must begin with a lowercase letter.
Relationship is written first (typically the predicate of the
sentence).
Objects are written separated by commas and are enclosed
by a pair of round brackets.
The full stop character ‘.’ must come at the end of a fact.
Examples
facts

Predicate

Interpretation

valuable(gold)

Gold is valuable.

owns(john,gold)

John owns gold.

father(john,mary)

John is the father of
Mary
John gives the book to
Mary

gives (john,book,mary)


Specifies under what conditions a tuple of values satisfies a
predicate.



The basic building block of a rule is called an atom

Atom :- Atom1, ..., Atomn
If each of Atom1,...,Atomn is true, then Atom is also true.

Rules specify:
 If-then conditions

I use an umbrella if there is a rain

use(i, umbrella) :- occur(rain).
 Generalizations

All men are mortal

mortal(X) :- man(X).
 Definitions
 An animal is a bird if it has feathers
 bird(X) :- animal(X), has_feather(X).
Syntax of rule
 <head> :- <body>
 Read ‘:-’ as ‘if’.
 likes(john,X) :- likes(X,cricket).
“John likes X if X likes cricket”.
i.e., “John likes anyone who likes cricket”.

 Rules always end with ‘.’
There are two types of queries:
 Ground Query
 edge(a,b)
 This query is called a ground query because it consists only
of value identifiers as parameters to the predicate.
 a ground query is posed we expect a yes/no answer.
 Non Ground Query
 They have variables as parameters
 tedge(a,X)


Always begin with a capital letter





?- likes (john,X).
?- likes (john, Something).

But not


?- likes (john,something)




Facts: ()
◦ likes(john,mary).
◦ likes(john,X). % Variables begin with capital
Queries
◦ ?- likes(X,Y).
◦ X=john, Y=Mary. % hit “;” for more
◦ ?- likes(X,X).
◦ X=john.




Rules
◦ likes(john,X) :- likes(X,wine). % :- = if
◦ likes(john,X):- female(X), likes(X,john).
Query: ? - likes(john,Y).
◦ Y = bill ;
◦ no
Predicate
Procedure for elephant
Facts

Clauses
Rule

elephant(george).
elephant(forge).
elephant(X) :- grey(X), mammal(X), hasTrunk(X).
?- elephant(george).
Queries

yes
?- elephant(jane).
Replies

no




Conjunction of predicates is represented as a sequence of
structures, separated by commas”,”.
It is referred as “AND”
sister_of (X,Y):- female (X), parents (X, M, F),





Disjunction of predicates is represented as a sequence of
structures, separated by semicolon”;”.
It is referred as “OR”
friend(ram,shyam):friend(shyam,sita);friend(shyam,mohan).









Questions based on facts are answered by matching
Unification is the name given to the way Prolog does its
matching.
Two facts match if their predicates are same (spelt the
same way) and the arguments each are same.
If matched, prolog answers yes, else no.
No does not mean falsity
This means not provable from the given facts.
Question Answering in presence of
rules
Facts
 male (ram).
 male (shyam).
 female (sita).
 female (gita).
 parents (shyam, gita, ram).
 parents (sita, gita, ram).
Rule:sister_of (X,Y):- female (X), parents (X, M, F),parents (Y, M, F).
X is a sister of Y is X is a female and X and Y have same parents
Backtracking
?- sister_of (sita, shyam)

female(sita)

parents(sita,M,F)

parents(shyam,M,F)

parents(shyam,gita,ram)
parents(sita,gita,ram)
success
Question Answering: wh-type: whose
sister is sita?
?- ?- sister_of (sita, X)

female(sita)

parents(sita,M,F)

parents(Y,M,F)

parents(Y,gita,ram)
parents(sita,gita,ram)
parents(shyam,gita,ram)
Success
Y=shyam








Prolog provides a number of basic arithmetic tools.
Arithmetic examples
Prolog Notation
6+2=8
8 is 6+2.
6 ∗ 2 = 12
12 is 6*2.
Answers to arithmetic questions by using ariables. For
example:
?- X is 6+2.
X=8
Prolog’s computation
 Depth First Search
 Pursues a goal till the end
 Conditional AND; falsity of any goal prevents satisfaction of
further clauses.
 Conditional OR; satisfaction of any goal prevents further clauses
being evaluated.
Control flow (top level)
Given
g:- a, b, c.

(1)

g:- d, e, f; g. (2)
If prolog cannot satisfy (1), control will automatically fall through to
(2).
Control Flow within a rule
Taking (1),
g:- a, b, c.
If a succeeds, prolog will try to satisfy b, succeding which c will be
tried.
For ANDed clauses, control flows forward till the ‘.’, iff the current
clause is true.
For ORed clauses, control flows forward till the ‘.’, iff the current
clause evaluates to false.
On Failure
REDO the immediately preceding goal.

Always place the more general rule AFTER a specific rule











Automatic backtracking is one of the most characteristic
features of Prolog.
Backtracking can lead to inefficiency.
Prolog can waste time exploring possibilities that lead
nowhere.
Cut is a goal that always succeeds
Commits Prolog to the choices that were made since the
parent goal was called
CUTS are used control over this aspect of its behaviour
p(X):- b(X), c(X), !, d(X), e(X).








consider the following piece of cut-free code:
p(X):- a(X).
p(X):- b(X), c(X), d(X), e(X).
p(X):- f(X).
a(1). b(1).
c(1).
d(2). e(2). f(3).
b(2).
c(2).
For query p(X) we will get the following responses:
X = 1 ;
X = 2 ;
X = 3 ;
no
Here is the search tree that explains how Prolog finds these three
solutions. Note that it has to backtrack once, namely when it enters the
second clause for p/1 and decides to unify the first goal
with b(1) instead of b(2) .





Consider the following code:
enjoys(vincent,X) :- big_kahuna_burger(X),!,fail.
enjoys(vincent,X) :- burger(X).
burger(X) :- big_mac(X).
burger(X) :- big_kahuna_burger(X).
burger(X) :- whopper(X).
big_mac(a).
big_kahuna_burger(b).
big_mac(c).
whopper(d).
Using Negation

   enjoys(vincent,X) :- burger(X), neg(big_kahuna_burger(X)).
Predicate Calculus
 Introduction through an example (Zohar Manna, 1974):
 Problem: A, B and C belong to the Himalayan club. Every
member in the club is either a mountain climber or a skier or
both. A likes whatever B dislikes and dislikes whatever B likes.
A likes rain and snow. No mountain climber likes rain. Every
skier likes snow. Is there a member who is a mountain climber
and not a skier?
 Given knowledge has:
 Facts
 Rules








Compute_length ([],0).
Compute_length ([Head|Tail], Length):Compute_length (Tail,Tail_length),
Length is Tail_length+1.
High level explanation:
The length of a list is 1 plus the length of the tail of the list,
obtained by removing the first element of the list.
This is a declarative description of the computation.
◦ Expert Systems
Inferencing)

(Knowledge

Representation

◦ Natural Language Processing
◦ Definite Clause Grammar
◦ http://www.learnprolognow.org/lpnpage.php?
pagetype=html&pageid=lpn-htmlch8

and
www.swi-prolog.org/
 http://www.learnprolognow.org/

THANKYOU

Weitere ähnliche Inhalte

Was ist angesagt?

10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prologbaran19901990
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
Branch and bound method
Branch and bound methodBranch and bound method
Branch and bound methodVidhyaSenthil
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic ProgrammingSahil Kumar
 
Pattern matching
Pattern matchingPattern matching
Pattern matchingshravs_188
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...Ashish Duggal
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting classgiridaroori
 

Was ist angesagt? (20)

10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prolog
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
prolog ppt
prolog pptprolog ppt
prolog ppt
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Branch and bound method
Branch and bound methodBranch and bound method
Branch and bound method
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Pattern matching
Pattern matchingPattern matching
Pattern matching
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
 
Set in discrete mathematics
Set in discrete mathematicsSet in discrete mathematics
Set in discrete mathematics
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 

Ähnlich wie Prolog basics

Ähnlich wie Prolog basics (20)

An introduction to Prolog language slide
An introduction to Prolog language slideAn introduction to Prolog language slide
An introduction to Prolog language slide
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 
#8 formal methods – pro logic
#8 formal methods – pro logic#8 formal methods – pro logic
#8 formal methods – pro logic
 
Prolog2 (1)
Prolog2 (1)Prolog2 (1)
Prolog2 (1)
 
lect14-semantics.ppt
lect14-semantics.pptlect14-semantics.ppt
lect14-semantics.ppt
 
L03 ai - knowledge representation using logic
L03 ai - knowledge representation using logicL03 ai - knowledge representation using logic
L03 ai - knowledge representation using logic
 
Prolog 01
Prolog 01Prolog 01
Prolog 01
 
ppt
pptppt
ppt
 
ppt
pptppt
ppt
 
Lecture12 xing
Lecture12 xingLecture12 xing
Lecture12 xing
 
Optimization of probabilistic argumentation with Markov processes
Optimization of probabilistic argumentation with Markov processesOptimization of probabilistic argumentation with Markov processes
Optimization of probabilistic argumentation with Markov processes
 
Logic 2
Logic 2Logic 2
Logic 2
 
Predicates
PredicatesPredicates
Predicates
 
Plc part 4
Plc  part 4Plc  part 4
Plc part 4
 
1004_theorem_proving_2018.pptx on the to
1004_theorem_proving_2018.pptx on the to1004_theorem_proving_2018.pptx on the to
1004_theorem_proving_2018.pptx on the to
 
Per3 logika
Per3 logikaPer3 logika
Per3 logika
 
Fol
FolFol
Fol
 
FOLBUKCFAIZ.pptx
FOLBUKCFAIZ.pptxFOLBUKCFAIZ.pptx
FOLBUKCFAIZ.pptx
 
Quantification
QuantificationQuantification
Quantification
 

Mehr von shivani saluja

Reinforcement learning
Reinforcement learningReinforcement learning
Reinforcement learningshivani saluja
 
supervised and unsupervised learning
supervised and unsupervised learningsupervised and unsupervised learning
supervised and unsupervised learningshivani saluja
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learningshivani saluja
 

Mehr von shivani saluja (6)

Reinforcement learning
Reinforcement learningReinforcement learning
Reinforcement learning
 
Regression
RegressionRegression
Regression
 
Decision tree
Decision treeDecision tree
Decision tree
 
supervised and unsupervised learning
supervised and unsupervised learningsupervised and unsupervised learning
supervised and unsupervised learning
 
Bayes and naive bayes
Bayes and naive bayesBayes and naive bayes
Bayes and naive bayes
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 

Kürzlich hochgeladen

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Kürzlich hochgeladen (20)

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

Prolog basics

  • 2. Introduction  SWI_PROLOG  Fundamentals of PROLOG  Applications  References 
  • 3. Open source software is software that can be freely used, changed, and shared (in modified or unmodified form) by anyone.
  • 4.     PROgramming in LOGic Declarative language Emphasis on what rather than how It is widely used in the field of AI Problem in Declarative Form Logic Machine Basic Machine
  • 5. SWI-Prolog offers a comprehensive FREE SOFTWARE Prolog environment.  Link for downloading: http://www.swi-prolog.org/download/stable  A Self-installing executable for MS-Windows: swipl-win.exe  Works on Windows XP  LINUX versions are also available. 
  • 6. Facts  Rules  Query  Unification  Resolution  Backtracing  Cuts and negations 
  • 7.      Facts are statements about what is true about a problem, instead of instructions how to accomplish the solution. The Prolog system uses the facts to work out how to accomplish the solution by searching through the space of possible solutions. It is defined by an identifier followed by an n-tuple of constants. A relation identifier is referred to as a predicate When a tuple of values is in a relation we say the tuple satisfies the predicate.
  • 8.     Names of relationship and objects must begin with a lowercase letter. Relationship is written first (typically the predicate of the sentence). Objects are written separated by commas and are enclosed by a pair of round brackets. The full stop character ‘.’ must come at the end of a fact.
  • 9. Examples facts Predicate Interpretation valuable(gold) Gold is valuable. owns(john,gold) John owns gold. father(john,mary) John is the father of Mary John gives the book to Mary gives (john,book,mary)
  • 10.  Specifies under what conditions a tuple of values satisfies a predicate.  The basic building block of a rule is called an atom Atom :- Atom1, ..., Atomn If each of Atom1,...,Atomn is true, then Atom is also true. 
  • 11. Rules specify:  If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).  Generalizations  All men are mortal  mortal(X) :- man(X).  Definitions  An animal is a bird if it has feathers  bird(X) :- animal(X), has_feather(X).
  • 12. Syntax of rule  <head> :- <body>  Read ‘:-’ as ‘if’.  likes(john,X) :- likes(X,cricket). “John likes X if X likes cricket”. i.e., “John likes anyone who likes cricket”.  Rules always end with ‘.’
  • 13. There are two types of queries:  Ground Query  edge(a,b)  This query is called a ground query because it consists only of value identifiers as parameters to the predicate.  a ground query is posed we expect a yes/no answer.  Non Ground Query  They have variables as parameters  tedge(a,X)
  • 14.  Always begin with a capital letter    ?- likes (john,X). ?- likes (john, Something). But not  ?- likes (john,something)
  • 15.   Facts: () ◦ likes(john,mary). ◦ likes(john,X). % Variables begin with capital Queries ◦ ?- likes(X,Y). ◦ X=john, Y=Mary. % hit “;” for more ◦ ?- likes(X,X). ◦ X=john.
  • 16.   Rules ◦ likes(john,X) :- likes(X,wine). % :- = if ◦ likes(john,X):- female(X), likes(X,john). Query: ? - likes(john,Y). ◦ Y = bill ; ◦ no
  • 19.   Conjunction of predicates is represented as a sequence of structures, separated by commas”,”. It is referred as “AND” sister_of (X,Y):- female (X), parents (X, M, F),   Disjunction of predicates is represented as a sequence of structures, separated by semicolon”;”. It is referred as “OR” friend(ram,shyam):friend(shyam,sita);friend(shyam,mohan).
  • 20.       Questions based on facts are answered by matching Unification is the name given to the way Prolog does its matching. Two facts match if their predicates are same (spelt the same way) and the arguments each are same. If matched, prolog answers yes, else no. No does not mean falsity This means not provable from the given facts.
  • 21. Question Answering in presence of rules Facts  male (ram).  male (shyam).  female (sita).  female (gita).  parents (shyam, gita, ram).  parents (sita, gita, ram). Rule:sister_of (X,Y):- female (X), parents (X, M, F),parents (Y, M, F). X is a sister of Y is X is a female and X and Y have same parents
  • 22. Backtracking ?- sister_of (sita, shyam) female(sita) parents(sita,M,F) parents(shyam,M,F) parents(shyam,gita,ram) parents(sita,gita,ram) success
  • 23. Question Answering: wh-type: whose sister is sita? ?- ?- sister_of (sita, X) female(sita) parents(sita,M,F) parents(Y,M,F) parents(Y,gita,ram) parents(sita,gita,ram) parents(shyam,gita,ram) Success Y=shyam
  • 24.       Prolog provides a number of basic arithmetic tools. Arithmetic examples Prolog Notation 6+2=8 8 is 6+2. 6 ∗ 2 = 12 12 is 6*2. Answers to arithmetic questions by using ariables. For example: ?- X is 6+2. X=8
  • 25. Prolog’s computation  Depth First Search  Pursues a goal till the end  Conditional AND; falsity of any goal prevents satisfaction of further clauses.  Conditional OR; satisfaction of any goal prevents further clauses being evaluated.
  • 26. Control flow (top level) Given g:- a, b, c. (1) g:- d, e, f; g. (2) If prolog cannot satisfy (1), control will automatically fall through to (2).
  • 27. Control Flow within a rule Taking (1), g:- a, b, c. If a succeeds, prolog will try to satisfy b, succeding which c will be tried. For ANDed clauses, control flows forward till the ‘.’, iff the current clause is true. For ORed clauses, control flows forward till the ‘.’, iff the current clause evaluates to false. On Failure REDO the immediately preceding goal. Always place the more general rule AFTER a specific rule
  • 28.        Automatic backtracking is one of the most characteristic features of Prolog. Backtracking can lead to inefficiency. Prolog can waste time exploring possibilities that lead nowhere. Cut is a goal that always succeeds Commits Prolog to the choices that were made since the parent goal was called CUTS are used control over this aspect of its behaviour p(X):- b(X), c(X), !, d(X), e(X).
  • 29.      consider the following piece of cut-free code: p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). d(2). e(2). f(3). b(2). c(2). For query p(X) we will get the following responses: X = 1 ; X = 2 ; X = 3 ; no Here is the search tree that explains how Prolog finds these three solutions. Note that it has to backtrack once, namely when it enters the second clause for p/1 and decides to unify the first goal with b(1) instead of b(2) .
  • 30.
  • 31.
  • 32.
  • 33.    Consider the following code: enjoys(vincent,X) :- big_kahuna_burger(X),!,fail. enjoys(vincent,X) :- burger(X). burger(X) :- big_mac(X). burger(X) :- big_kahuna_burger(X). burger(X) :- whopper(X). big_mac(a). big_kahuna_burger(b). big_mac(c). whopper(d). Using Negation    enjoys(vincent,X) :- burger(X), neg(big_kahuna_burger(X)).
  • 34. Predicate Calculus  Introduction through an example (Zohar Manna, 1974):  Problem: A, B and C belong to the Himalayan club. Every member in the club is either a mountain climber or a skier or both. A likes whatever B dislikes and dislikes whatever B likes. A likes rain and snow. No mountain climber likes rain. Every skier likes snow. Is there a member who is a mountain climber and not a skier?  Given knowledge has:  Facts  Rules
  • 35.        Compute_length ([],0). Compute_length ([Head|Tail], Length):Compute_length (Tail,Tail_length), Length is Tail_length+1. High level explanation: The length of a list is 1 plus the length of the tail of the list, obtained by removing the first element of the list. This is a declarative description of the computation.
  • 36. ◦ Expert Systems Inferencing) (Knowledge Representation ◦ Natural Language Processing ◦ Definite Clause Grammar ◦ http://www.learnprolognow.org/lpnpage.php? pagetype=html&pageid=lpn-htmlch8 and