SlideShare a Scribd company logo
1 of 51
3-Dec-21
Prolog
Programming in Logic
SWI-Prolog
 SWI-Prolog is a good, standard Prolog for Windows
and Linux
 It's licensed under GPL, therefore free
 Downloadable from: http://www.swi-prolog.org/
Syllogisms
 “Prolog” is all about programming in logic.
 Aristotle described syllogisms 2300 years ago
 Sample syllogism:
 Socrates is a man.
 All men are mortal.
 Therefore, Socrates is mortal.
 This is logic. Can Prolog do it?
Forward and backward reasoning
 A syllogism gives two premises, then asks, "What can
we conclude?"
 This is forward reasoning -- from premises to conclusions
 it's inefficient when you have lots of premises
 Instead, you ask Prolog specific questions
 Prolog uses backward reasoning -- from (potential)
conclusions to facts
Syllogisms in Prolog
Syllogism
Socrates is a man.
All men are mortal.
Is Socrates mortal?
man(socrates).
mortal(X) :- man(X).
?- mortal(socrates).
Prolog
Facts, rules, and queries
 Fact: Socrates is a man.
 man(socrates).
 Rule: All men are mortal.
 mortal(X) :- man(X).
 Query: Is Socrates mortal?
 mortal(socrates).
 Queries have the same form as facts
Running Prolog I
 Create your "database" (program) in any editor
 Save it as text only, with a .pl extension
 Here's the complete program:
man(socrates).
mortal(X) :- man(X).
Running Prolog II
 Prolog is completely interactive. Begin by
 Double-clicking on your .pl file, or
 Double-clicking on the Prolog application and consulting your
file at the ?- prompt:
 ?- consult('C:My Programsadv.pl').
 Then, ask your question at the prompt:
 ?- mortal(socrates).
 Prolog responds:
 Yes
Prolog is a theorem prover
 Prolog's "Yes" means "I can prove it" --
Prolog's "No" means "I can't prove it"
 ?- mortal(plato).
No
 This is the closed world assumption: the Prolog
program knows everything it needs to know
 Prolog supplies values for variables when it can
 ?- mortal(X).
X = socrates
Syntax I: Structures
 A structure consists of a name and zero or more
arguments.
 Omit the parentheses if there are no arguments
 Example structures:
 sunshine
 man(socrates)
 path(garden, south, sundial)
Syntax II: Base Clauses
 A base clause is just a structure, terminated with a
period.
 A base clause represents a simple fact.
 Example base clauses:
 debug_on.
 loves(john, mary).
 loves(mary, bill).
Syntax III: Nonbase Clauses
 A nonbase clause is a structure, a turnstile :-
(meaning “if”), and a list of structures.
 Example nonbase clauses:
 mortal(X) :- man(X).
 mortal(X) :- woman(X).
 happy(X) :- healthy(X), wealthy(X), wise(X).
 The comma between structures means “and”
Syntax IV: Predicates
 A predicate is a collection of clauses with the same
functor (name) and arity (number of arguments).
 loves(john, mary).
loves(mary, bill).
loves(chuck, X) :- female(X), rich(X).
Syntax V: Programs
 A program is a collection of predicates.
 Predicates can be in any order.
 Clauses within a predicate are used in the order in which
they occur.
Syntax VI: Variables and atoms
 Variables begin with a capital letter:
X, Socrates, _result
 Atoms do not begin with a capital letter:
x, socrates
 Atoms containing special characters, or beginning with
a capital letter, must be enclosed in single quotes:
 'C:My Documentsexamples.pl'
Syntax VII: Strings are atoms
 In a quoted atom, a single quote must be doubled or
backslashed:
 'Can''t, or won't?'
 Backslashes in file names must also be doubled:
 'C:My Documentsexamples.pl'
Common problems
 Capitalization is meaningful!
 No space is allowed between a functor and its
argument list:
man(socrates), not man (socrates).
 Double quotes indicate a list of ASCII character
values, not a string
 Don’t forget the period! (But you can put it on the next
line.)
Backtracking
 loves(chuck, X) :- female(X), rich(X).
 female(jane).
 female(mary).
 rich(mary).
 ---------- Suppose we ask: loves(chuck, X).
 female(X) = female(jane), X = jane.
 rich(jane) fails.
 female(X) = female(mary), X = mary.
 rich(mary) succeeds.
Backtracking and Beads
 Each Prolog call is like a “bead” in a string of beads:
call
fail
exit
redo
 Each structure has four ports: call, exit, redo, fail
 Exit ports connect to call ports;
fail ports connect to redo ports
Calls as nested beads
loves(chuck, X) :- female(X), rich(X).
loves(chuck, X)
female(X) rich(X)
call
fail
exit
redo
Additional answers
 female(jane).
female(mary).
female(susan).
 ?- female(X).
 X = jane ;
 X = mary
 Yes
female(jane)
female(mary)
female(susan)
female(X)
Readings
 loves(chuck, X) :- female(X), rich(X).
 Declarative reading: Chuck loves X if X is female and
rich.
 Approximate procedural reading: To find an X that
Chuck loves, first find a female X, then check that X is
rich.
 Declarative readings are almost always preferred.
Monotonic logic
 Standard logic is monotonic: once you prove something
is true, it is true forever
 Logic isn't a good fit to reality
 If the wallet is in the purse, and the purse in is the car,
we can conclude that the wallet is in the car
 But what if we take the purse out of the car?
Nonmonotonic logic
 Prolog uses nonmonotonic logic
 Facts and rules can be changed at any time
 such facts and rules are said to be dynamic
 assert(...) adds a fact or rule
 retract(...) removes a fact or rule
 assert and retract are said to be extralogical
predicates
Examples of assert and retract
 assert(man(plato)).
 assert((loves(chuck,X) :- female(X), rich(X))).
 retract(man(plato)).
 retract((loves(chuck,X) :- female(X), rich(X))).
 Notice that we use double parentheses for rules
 this is to avoid a minor syntax problem
 assert(foo :- bar, baz).
 How many arguments did we give to assert?
Limitations of backtracking
 In Prolog, backtracking over something generally
undoes it
 Output can't be undone by backtracking
 Neither can assert and retract be undone by
backtracking
 Perform any necessary testing before you use write, nl,
assert, or retract
Modeling “real life”
 Real life isn't monotonic; things change
 Prolog is superb for modeling change
 Games are often a model of real (or fantasy!) life
 Prolog is just about ideal for adventure games
Starting Prolog
 [Macintosh:~] dave% prolog
% library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,928 bytes
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.1)
Copyright (c) 1990-2010 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
 ?- consult('C:_Prologdragon.pl').
 % C:_Prologdragon.pl compiled 0.00 sec, 14,560 bytes
Yes
Instructions
 ?- start.
 Enter commands using standard Prolog syntax.
Available commands are:
start. -- to start the game.
n. s. e. w. -- to go in that direction.
take(Object). -- to pick up an object.
drop(Object). -- to put down an object.
use(Object). -- to use an object.
attack. -- to attack an enemy.
look. -- to look around you again.
instructions. -- to see this message again.
halt. -- to end the game and quit.
Starting out
 You are in a meadow. To the north is the dark
mouth of a cave; to the south is a small building.
Your assignment, should you decide to accept it,
is to recover the famed Bar-Abzad ruby and
return it to this meadow.
Yes
Going south
 ?- s.
 You are in a small building. The exit is to the
north. The room is devoid of furniture, and the
only feature seems to be a small door to the east.
There is a flashlight here.
Yes
Taking things, locked doors
 ?- take(flashlight).
 OK.
Yes
 ?- e.
 The door appears to be locked.
You can't go that way.
Yes
Some time later...
 ?- use(key).
 The closet is no longer locked.
Yes
 Later still...
 ?- look.
 You are in a big, dark cave. The air is fetid.
There is a chest here.
Essential facts
 Where I am at present:
 i_am_at(meadow).
 Where other things are at:
 at(flashlight, building).
 What I am holding:
 holding(key).
 Which facts may be changed:
 :- dynamic i_am_at/1, at/2, holding/1.
Input and output
 Input is unpleasant; we avoid it by giving commands
(as questions) directly to Prolog
 take(flashlight).
 write(...) outputs its one argument
 nl ends the line (writes a newline)
 describe(closet) :-
write('You are in an old storage closet.'),
nl.
The map
cave_entrance cave
meadow
building closet
N
W E
S
Implementing the map
 path(cave, w, cave_entrance).
path(cave_entrance, e, cave).
 path(meadow, s, building).
path(building, n, meadow).
 Could have done this instead:
 path(cave, w, cave_entrance).
path(X, e, Y) :- path(Y, w, X).
listing
 listing(predicate) is a good way to examine the
current state of the program
 ?- listing(at).
 at(key, cave_entrance).
at(flashlight, building).
at(sword, closet).
Yes
North, south, east, west
 The commands n, s, e, w all call go.
 n :- go(n).
s :- go(s).
e :- go(e).
w :- go(w).
go
 go(Direction) :-
i_am_at(Here),
path(Here, Direction, There),
retract(i_am_at(Here)),
assert(i_am_at(There)),
look.
 go(_) :-
write('You can''t go that way.').
take
 take(X) :-
i_am_at(Place),
at(X, Place),
retract(at(X, Place)),
assert(holding(X)),
write('OK.'),
nl.
You can't always take
take(A) :-
holding(A),
write('You're already holding it!'), nl.
take(A) :- (actually take something, as before).
take(A) :-
write('I don't see it here.'), nl.
Making things fail
 A predicate will fail if it doesn't succeed
 You can explicitly use fail
 fail works like this:
 This often isn't strong enough; it doesn't force the
entire predicate to fail
fail
call
fail
cut
 The "cut," written ! , is a commit point
 It commits to the clause in which it occurs, and
 everything before it in that clause
 Using cut says: Don't try any other clauses, and don't
backtrack past the cut
!
call exit
cut-fail
 The cut-fail combination: !, fail means really fail
 It commits to this clause, then fails
 This means no other clauses of this predicate will be
tried, so the predicate as a whole fails
A locked door
 path(building, e, closet) :-
locked(closet),
write('The door appears to be locked.'), nl,
!, fail.
path(building, e, closet).
 If the closet door isn't locked, the first clause fails
"normally," and the second clause is used
 If the closet door is locked, the cut prevents the
second clause from ever being reached
Dropping objects
drop(A) :-
holding(A),
i_am_at(B),
retract(holding(A)),
assert(at(A, B)),
write('OK.'), nl.
drop(A) :-
write('You aren't holding it!'), nl.
What else is Prolog good for?
 Prolog is primarily an AI (Artificial Intelligence)
language
 It's second only to LISP in popularity
 It's more popular in Britain than in the U.S.
 Prolog is also a very enjoyable language in which to
program (subjective opinion, obviously!)
Prolog vs. LISP
 Unlike LISP, Prolog provides:
 built-in theorem proving
 built in Definite Clause Grammars, good for parsing natural
language
 If you just want to use these tools, Prolog is arguably
better
 If you want to build your own theorem prover or parser,
LISP is clearly better
The End

More Related Content

What's hot

Introduction to prolog
Introduction to prologIntroduction to prolog
Introduction to prologHarry Potter
 
Artificial intelligence Prolog Language
Artificial intelligence Prolog LanguageArtificial intelligence Prolog Language
Artificial intelligence Prolog LanguageREHMAT ULLAH
 
Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Sabu Francis
 
09 logic programming
09 logic programming09 logic programming
09 logic programmingsaru40
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologDataminingTools Inc
 
Prolog Programming Language
Prolog Programming  LanguageProlog Programming  Language
Prolog Programming LanguageReham AlBlehid
 
PROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In PrologPROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In PrologPROLOG CONTENT
 
PROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologPROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologDataminingTools Inc
 
Prolog programming
Prolog programmingProlog programming
Prolog programmingHarry Potter
 
Babar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and RepresentationBabar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and RepresentationPierre de Lacaze
 
Regular expression
Regular expressionRegular expression
Regular expressionRajon
 

What's hot (20)

Introduction to prolog
Introduction to prologIntroduction to prolog
Introduction to prolog
 
Prolog 7-Languages
Prolog 7-LanguagesProlog 7-Languages
Prolog 7-Languages
 
Introduction to Prolog
Introduction to PrologIntroduction to Prolog
Introduction to Prolog
 
Artificial intelligence Prolog Language
Artificial intelligence Prolog LanguageArtificial intelligence Prolog Language
Artificial intelligence Prolog Language
 
Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1
 
prolog ppt
prolog pptprolog ppt
prolog ppt
 
Prolog
PrologProlog
Prolog
 
09 logic programming
09 logic programming09 logic programming
09 logic programming
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In Prolog
 
Logic Programming and ILP
Logic Programming and ILPLogic Programming and ILP
Logic Programming and ILP
 
Prolog Programming Language
Prolog Programming  LanguageProlog Programming  Language
Prolog Programming Language
 
PROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In PrologPROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In Prolog
 
Overview prolog
Overview prologOverview prolog
Overview prolog
 
PROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologPROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In Prolog
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
Babar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and RepresentationBabar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and Representation
 
Inteligencia artificial
Inteligencia artificialInteligencia artificial
Inteligencia artificial
 
First order logic
First order logicFirst order logic
First order logic
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Invitation to Scala
Invitation to ScalaInvitation to Scala
Invitation to Scala
 

Similar to Prolog 01

Clojure made simple - Lightning talk
Clojure made simple - Lightning talkClojure made simple - Lightning talk
Clojure made simple - Lightning talkJohn Stevenson
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Clojure made really really simple
Clojure made really really simpleClojure made really really simple
Clojure made really really simpleJohn Stevenson
 
Declarative Datalog Debugging for Mere Mortals
Declarative Datalog Debugging for Mere MortalsDeclarative Datalog Debugging for Mere Mortals
Declarative Datalog Debugging for Mere MortalsBertram Ludäscher
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer AlgorithmsAlaa Al-Makhzoomy
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Bryan O'Sullivan
 
hello the code given is mostly complete but I need help with the Sol.pdf
hello the code given is mostly complete but I need help with the Sol.pdfhello the code given is mostly complete but I need help with the Sol.pdf
hello the code given is mostly complete but I need help with the Sol.pdfalmonardfans
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospectivechenge2k
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Philip Schwarz
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class PatternsJohn De Goes
 
SOFIE - A Unified Approach To Ontology-Based Information Extraction Using Rea...
SOFIE - A Unified Approach To Ontology-Based Information Extraction Using Rea...SOFIE - A Unified Approach To Ontology-Based Information Extraction Using Rea...
SOFIE - A Unified Approach To Ontology-Based Information Extraction Using Rea...Tobias Wunner
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)William Narmontas
 

Similar to Prolog 01 (20)

Clojure made simple - Lightning talk
Clojure made simple - Lightning talkClojure made simple - Lightning talk
Clojure made simple - Lightning talk
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Clojure made really really simple
Clojure made really really simpleClojure made really really simple
Clojure made really really simple
 
Prolog2 (1)
Prolog2 (1)Prolog2 (1)
Prolog2 (1)
 
Declarative Datalog Debugging for Mere Mortals
Declarative Datalog Debugging for Mere MortalsDeclarative Datalog Debugging for Mere Mortals
Declarative Datalog Debugging for Mere Mortals
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer Algorithms
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
 
hello the code given is mostly complete but I need help with the Sol.pdf
hello the code given is mostly complete but I need help with the Sol.pdfhello the code given is mostly complete but I need help with the Sol.pdf
hello the code given is mostly complete but I need help with the Sol.pdf
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospective
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
 
SOFIE - A Unified Approach To Ontology-Based Information Extraction Using Rea...
SOFIE - A Unified Approach To Ontology-Based Information Extraction Using Rea...SOFIE - A Unified Approach To Ontology-Based Information Extraction Using Rea...
SOFIE - A Unified Approach To Ontology-Based Information Extraction Using Rea...
 
20 prolog5
20 prolog520 prolog5
20 prolog5
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 

Recently uploaded

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 

Recently uploaded (20)

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 

Prolog 01

  • 2.
  • 3. SWI-Prolog  SWI-Prolog is a good, standard Prolog for Windows and Linux  It's licensed under GPL, therefore free  Downloadable from: http://www.swi-prolog.org/
  • 4. Syllogisms  “Prolog” is all about programming in logic.  Aristotle described syllogisms 2300 years ago  Sample syllogism:  Socrates is a man.  All men are mortal.  Therefore, Socrates is mortal.  This is logic. Can Prolog do it?
  • 5. Forward and backward reasoning  A syllogism gives two premises, then asks, "What can we conclude?"  This is forward reasoning -- from premises to conclusions  it's inefficient when you have lots of premises  Instead, you ask Prolog specific questions  Prolog uses backward reasoning -- from (potential) conclusions to facts
  • 6. Syllogisms in Prolog Syllogism Socrates is a man. All men are mortal. Is Socrates mortal? man(socrates). mortal(X) :- man(X). ?- mortal(socrates). Prolog
  • 7. Facts, rules, and queries  Fact: Socrates is a man.  man(socrates).  Rule: All men are mortal.  mortal(X) :- man(X).  Query: Is Socrates mortal?  mortal(socrates).  Queries have the same form as facts
  • 8. Running Prolog I  Create your "database" (program) in any editor  Save it as text only, with a .pl extension  Here's the complete program: man(socrates). mortal(X) :- man(X).
  • 9. Running Prolog II  Prolog is completely interactive. Begin by  Double-clicking on your .pl file, or  Double-clicking on the Prolog application and consulting your file at the ?- prompt:  ?- consult('C:My Programsadv.pl').  Then, ask your question at the prompt:  ?- mortal(socrates).  Prolog responds:  Yes
  • 10. Prolog is a theorem prover  Prolog's "Yes" means "I can prove it" -- Prolog's "No" means "I can't prove it"  ?- mortal(plato). No  This is the closed world assumption: the Prolog program knows everything it needs to know  Prolog supplies values for variables when it can  ?- mortal(X). X = socrates
  • 11. Syntax I: Structures  A structure consists of a name and zero or more arguments.  Omit the parentheses if there are no arguments  Example structures:  sunshine  man(socrates)  path(garden, south, sundial)
  • 12. Syntax II: Base Clauses  A base clause is just a structure, terminated with a period.  A base clause represents a simple fact.  Example base clauses:  debug_on.  loves(john, mary).  loves(mary, bill).
  • 13. Syntax III: Nonbase Clauses  A nonbase clause is a structure, a turnstile :- (meaning “if”), and a list of structures.  Example nonbase clauses:  mortal(X) :- man(X).  mortal(X) :- woman(X).  happy(X) :- healthy(X), wealthy(X), wise(X).  The comma between structures means “and”
  • 14. Syntax IV: Predicates  A predicate is a collection of clauses with the same functor (name) and arity (number of arguments).  loves(john, mary). loves(mary, bill). loves(chuck, X) :- female(X), rich(X).
  • 15. Syntax V: Programs  A program is a collection of predicates.  Predicates can be in any order.  Clauses within a predicate are used in the order in which they occur.
  • 16. Syntax VI: Variables and atoms  Variables begin with a capital letter: X, Socrates, _result  Atoms do not begin with a capital letter: x, socrates  Atoms containing special characters, or beginning with a capital letter, must be enclosed in single quotes:  'C:My Documentsexamples.pl'
  • 17. Syntax VII: Strings are atoms  In a quoted atom, a single quote must be doubled or backslashed:  'Can''t, or won't?'  Backslashes in file names must also be doubled:  'C:My Documentsexamples.pl'
  • 18. Common problems  Capitalization is meaningful!  No space is allowed between a functor and its argument list: man(socrates), not man (socrates).  Double quotes indicate a list of ASCII character values, not a string  Don’t forget the period! (But you can put it on the next line.)
  • 19. Backtracking  loves(chuck, X) :- female(X), rich(X).  female(jane).  female(mary).  rich(mary).  ---------- Suppose we ask: loves(chuck, X).  female(X) = female(jane), X = jane.  rich(jane) fails.  female(X) = female(mary), X = mary.  rich(mary) succeeds.
  • 20. Backtracking and Beads  Each Prolog call is like a “bead” in a string of beads: call fail exit redo  Each structure has four ports: call, exit, redo, fail  Exit ports connect to call ports; fail ports connect to redo ports
  • 21. Calls as nested beads loves(chuck, X) :- female(X), rich(X). loves(chuck, X) female(X) rich(X) call fail exit redo
  • 22. Additional answers  female(jane). female(mary). female(susan).  ?- female(X).  X = jane ;  X = mary  Yes female(jane) female(mary) female(susan) female(X)
  • 23. Readings  loves(chuck, X) :- female(X), rich(X).  Declarative reading: Chuck loves X if X is female and rich.  Approximate procedural reading: To find an X that Chuck loves, first find a female X, then check that X is rich.  Declarative readings are almost always preferred.
  • 24. Monotonic logic  Standard logic is monotonic: once you prove something is true, it is true forever  Logic isn't a good fit to reality  If the wallet is in the purse, and the purse in is the car, we can conclude that the wallet is in the car  But what if we take the purse out of the car?
  • 25. Nonmonotonic logic  Prolog uses nonmonotonic logic  Facts and rules can be changed at any time  such facts and rules are said to be dynamic  assert(...) adds a fact or rule  retract(...) removes a fact or rule  assert and retract are said to be extralogical predicates
  • 26. Examples of assert and retract  assert(man(plato)).  assert((loves(chuck,X) :- female(X), rich(X))).  retract(man(plato)).  retract((loves(chuck,X) :- female(X), rich(X))).  Notice that we use double parentheses for rules  this is to avoid a minor syntax problem  assert(foo :- bar, baz).  How many arguments did we give to assert?
  • 27. Limitations of backtracking  In Prolog, backtracking over something generally undoes it  Output can't be undone by backtracking  Neither can assert and retract be undone by backtracking  Perform any necessary testing before you use write, nl, assert, or retract
  • 28. Modeling “real life”  Real life isn't monotonic; things change  Prolog is superb for modeling change  Games are often a model of real (or fantasy!) life  Prolog is just about ideal for adventure games
  • 29. Starting Prolog  [Macintosh:~] dave% prolog % library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,928 bytes Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.1) Copyright (c) 1990-2010 University of Amsterdam, VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details.  ?- consult('C:_Prologdragon.pl').  % C:_Prologdragon.pl compiled 0.00 sec, 14,560 bytes Yes
  • 30. Instructions  ?- start.  Enter commands using standard Prolog syntax. Available commands are: start. -- to start the game. n. s. e. w. -- to go in that direction. take(Object). -- to pick up an object. drop(Object). -- to put down an object. use(Object). -- to use an object. attack. -- to attack an enemy. look. -- to look around you again. instructions. -- to see this message again. halt. -- to end the game and quit.
  • 31. Starting out  You are in a meadow. To the north is the dark mouth of a cave; to the south is a small building. Your assignment, should you decide to accept it, is to recover the famed Bar-Abzad ruby and return it to this meadow. Yes
  • 32. Going south  ?- s.  You are in a small building. The exit is to the north. The room is devoid of furniture, and the only feature seems to be a small door to the east. There is a flashlight here. Yes
  • 33. Taking things, locked doors  ?- take(flashlight).  OK. Yes  ?- e.  The door appears to be locked. You can't go that way. Yes
  • 34. Some time later...  ?- use(key).  The closet is no longer locked. Yes  Later still...  ?- look.  You are in a big, dark cave. The air is fetid. There is a chest here.
  • 35. Essential facts  Where I am at present:  i_am_at(meadow).  Where other things are at:  at(flashlight, building).  What I am holding:  holding(key).  Which facts may be changed:  :- dynamic i_am_at/1, at/2, holding/1.
  • 36. Input and output  Input is unpleasant; we avoid it by giving commands (as questions) directly to Prolog  take(flashlight).  write(...) outputs its one argument  nl ends the line (writes a newline)  describe(closet) :- write('You are in an old storage closet.'), nl.
  • 38. Implementing the map  path(cave, w, cave_entrance). path(cave_entrance, e, cave).  path(meadow, s, building). path(building, n, meadow).  Could have done this instead:  path(cave, w, cave_entrance). path(X, e, Y) :- path(Y, w, X).
  • 39. listing  listing(predicate) is a good way to examine the current state of the program  ?- listing(at).  at(key, cave_entrance). at(flashlight, building). at(sword, closet). Yes
  • 40. North, south, east, west  The commands n, s, e, w all call go.  n :- go(n). s :- go(s). e :- go(e). w :- go(w).
  • 41. go  go(Direction) :- i_am_at(Here), path(Here, Direction, There), retract(i_am_at(Here)), assert(i_am_at(There)), look.  go(_) :- write('You can''t go that way.').
  • 42. take  take(X) :- i_am_at(Place), at(X, Place), retract(at(X, Place)), assert(holding(X)), write('OK.'), nl.
  • 43. You can't always take take(A) :- holding(A), write('You're already holding it!'), nl. take(A) :- (actually take something, as before). take(A) :- write('I don't see it here.'), nl.
  • 44. Making things fail  A predicate will fail if it doesn't succeed  You can explicitly use fail  fail works like this:  This often isn't strong enough; it doesn't force the entire predicate to fail fail call fail
  • 45. cut  The "cut," written ! , is a commit point  It commits to the clause in which it occurs, and  everything before it in that clause  Using cut says: Don't try any other clauses, and don't backtrack past the cut ! call exit
  • 46. cut-fail  The cut-fail combination: !, fail means really fail  It commits to this clause, then fails  This means no other clauses of this predicate will be tried, so the predicate as a whole fails
  • 47. A locked door  path(building, e, closet) :- locked(closet), write('The door appears to be locked.'), nl, !, fail. path(building, e, closet).  If the closet door isn't locked, the first clause fails "normally," and the second clause is used  If the closet door is locked, the cut prevents the second clause from ever being reached
  • 48. Dropping objects drop(A) :- holding(A), i_am_at(B), retract(holding(A)), assert(at(A, B)), write('OK.'), nl. drop(A) :- write('You aren't holding it!'), nl.
  • 49. What else is Prolog good for?  Prolog is primarily an AI (Artificial Intelligence) language  It's second only to LISP in popularity  It's more popular in Britain than in the U.S.  Prolog is also a very enjoyable language in which to program (subjective opinion, obviously!)
  • 50. Prolog vs. LISP  Unlike LISP, Prolog provides:  built-in theorem proving  built in Definite Clause Grammars, good for parsing natural language  If you just want to use these tools, Prolog is arguably better  If you want to build your own theorem prover or parser, LISP is clearly better