chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog...
Transcript of chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog...
![Page 1: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/1.jpg)
Learn Prolog Now!
![Page 2: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/2.jpg)
SWI Prolog
• Freely available Prolog interpreter • Works with
– Linux, – Windows, or – Mac OS
• There are many more Prolog interpreters
• Not all are ISO compliant/free
![Page 3: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/3.jpg)
Lecture 1
• Theory – Introduction to Prolog – Facts, Rules and Queries – Prolog Syntax
• Exercises – Exercises of LPN chapter 1 – Practical work
![Page 4: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/4.jpg)
Aim of this lecture (1/2)
• Give some simple examples of Prolog programs
• Discuss the three basic constructs in Prolog: – Facts – Rules – Queries
![Page 5: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/5.jpg)
Aim of this lecture (2/2)
• Introduce other concepts, such as – the role of logic – unification with the help of variables
• Begin the systematic study of Prolog by defining – terms – atoms, and – variables
![Page 6: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/6.jpg)
Prolog
• "Programming with Logic" • Very different from other programming
languages – Declarative (not procedural) – Recursion (no “for” or “while” loops) – Relations (no functions) – Unification
![Page 7: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/7.jpg)
History of Prolog
1972 1977 1980 1980s/1990s 2005
first Prolog interpreter by Alain Colmerauer and
Philippe Roussel
![Page 8: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/8.jpg)
History of Prolog
1972 1977 1980 1980s/1990s 2005
implementation of DEC10 compiler by David H.D. Warren
![Page 9: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/9.jpg)
History of Prolog
1972 1977 1980 1980s/1990s 2005
Definite Clause Grammars implementation by
Pereira and Warren
![Page 10: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/10.jpg)
History of Prolog
1972 1977 1980 1980s/1990s 2005
Prolog grows in popularity especially in Japan and Europe
![Page 11: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/11.jpg)
History of Prolog
1972 1977 1980 1980s/1990s 2005
Prolog used to program natural
language interface in International Space
Station by NASA
![Page 12: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/12.jpg)
History of Prolog
1972 1977 1980 1980s/1990s 2011
Parts of IBM’s Watson QA
supercomputer were coded in
Prolog
![Page 13: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/13.jpg)
Prolog and Web Applications
• prolog programs are often smaller • smallness encourages well written code • hence, easier to maintain
Source: http://www.pathwayslms.com/swipltuts/
![Page 14: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/14.jpg)
Basic idea of Prolog
• Describe the situation of interest • Ask a question • Prolog:
– logically deduces new facts about the situation we described
– gives us its deductions back as answers
![Page 15: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/15.jpg)
Consequences
• Think declaratively, not procedurally – Challenging – Requires a different mindset
• High-level language – Not as efficient as, say, C – Good for rapid prototyping – Useful in many AI applications
(knowledge representation, inference)
![Page 16: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/16.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
![Page 17: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/17.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?-
![Page 18: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/18.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- woman(mia).
![Page 19: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/19.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- woman(mia). yes ?-
![Page 20: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/20.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- woman(mia). yes ?- playsAirGuitar(jody).
![Page 21: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/21.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- woman(mia). yes ?- playsAirGuitar(jody). yes ?-
![Page 22: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/22.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- woman(mia). yes ?- playsAirGuitar(jody). yes ?- playsAirGuitar(mia).
![Page 23: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/23.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- woman(mia). yes ?- playsAirGuitar(jody). yes ?- playsAirGuitar(mia). no
![Page 24: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/24.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- tattoed(jody).
![Page 25: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/25.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- tattoed(jody). no ?-
![Page 26: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/26.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- tattoed(jody). ERROR: predicate tattoed/1 not defined. ?-
![Page 27: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/27.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- party.
![Page 28: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/28.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- party. yes ?-
![Page 29: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/29.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- rockConcert.
![Page 30: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/30.jpg)
Knowledge Base 1
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
?- rockConcert. no ?-
![Page 31: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/31.jpg)
Knowledge Base 2
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
![Page 32: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/32.jpg)
Knowledge Base 2
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
fact
![Page 33: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/33.jpg)
Knowledge Base 2
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
fact fact
![Page 34: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/34.jpg)
Knowledge Base 2
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
fact fact
rule
![Page 35: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/35.jpg)
Knowledge Base 2
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
fact fact
rule rule
![Page 36: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/36.jpg)
Knowledge Base 2
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
fact fact
rule rule
rule
![Page 37: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/37.jpg)
Knowledge Base 2
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
head body
![Page 38: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/38.jpg)
Knowledge Base 2
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
?-
![Page 39: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/39.jpg)
Knowledge Base 2
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
?- playsAirGuitar(mia). yes ?-
![Page 40: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/40.jpg)
Knowledge Base 2
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
?- playsAirGuitar(mia). yes ?- playsAirGuitar(yolanda). yes
![Page 41: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/41.jpg)
Clauses
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
There are five clauses in this knowledge base: two facts, and three rules.
The end of a clause is marked with a full stop.
![Page 42: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/42.jpg)
Predicates
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
There are three predicates in this knowledge base:
happy, listens2music, and playsAirGuitar
![Page 43: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/43.jpg)
Knowledge Base 3
happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).
![Page 44: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/44.jpg)
Expressing Conjunction
happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).
The comma “," expresses conjunction in Prolog
![Page 45: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/45.jpg)
Knowledge Base 3
happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(vincent).
![Page 46: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/46.jpg)
Knowledge Base 3
happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(vincent). no ?-
![Page 47: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/47.jpg)
Knowledge Base 3
happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(butch).
![Page 48: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/48.jpg)
Knowledge Base 3
happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(butch). yes ?-
![Page 49: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/49.jpg)
Expressing Disjunction
happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).
happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch); listens2music(butch).
![Page 50: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/50.jpg)
Prolog and Logic
• Clearly, Prolog has something to do with logic...
• Use of inference (modus ponens) • Negation (?)
Prolog Logic Implication A :- B B A Conjunction A,B A ∧ B Disjunction A;B A ∨ B
![Page 51: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/51.jpg)
Knowledge Base 4
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
![Page 52: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/52.jpg)
Prolog Variables
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
?- woman(X).
![Page 53: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/53.jpg)
Variable Instantiation
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
?- woman(X). X=mia
![Page 54: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/54.jpg)
Asking Alternatives
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
?- woman(X). X=mia;
![Page 55: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/55.jpg)
Asking Alternatives
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
?- woman(X). X=mia; X=jody
![Page 56: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/56.jpg)
Asking Alternatives
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
?- woman(X). X=mia; X=jody; X=yolanda
![Page 57: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/57.jpg)
Asking Alternatives
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
?- woman(X). X=mia; X=jody; X=yolanda; no
![Page 58: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/58.jpg)
Knowledge Base 4
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
?- loves(marsellus,X), woman(X).
![Page 59: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/59.jpg)
Knowledge Base 4
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
?- loves(marsellus,X), woman(X). X=mia yes ?-
![Page 60: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/60.jpg)
Knowledge Base 4
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
?- loves(pumpkin,X), woman(X).
![Page 61: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/61.jpg)
Knowledge Base 4
woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
?- loves(pumpkin,X), woman(X). no ?-
![Page 62: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/62.jpg)
Knowledge Base 5
loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z).
![Page 63: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/63.jpg)
Knowledge Base 5
loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z).
?- jealous(marsellus,W).
![Page 64: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/64.jpg)
Knowledge Base 5
loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z).
?- jealous(marsellus,W). W=vincent ?-
![Page 65: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/65.jpg)
Syntax of Prolog
• Q: What exactly are facts, rules and queries built out of?
• A: Prolog terms
![Page 66: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/66.jpg)
Prolog terms
Terms
Simple Terms Complex Terms
Constants Variables
Atoms Numbers
Terms
Simple Terms Complex Terms
Constants Variables
Atoms Numbers
![Page 67: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/67.jpg)
Atoms
• A sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with a lowercase letter – Examples: butch, big_kahuna_burger, playGuitar
![Page 68: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/68.jpg)
Atoms
• A sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with a lowercase letter – Examples: butch, big_kahuna_burger, playGuitar
• An arbitrary sequence of characters enclosed in single quotes – Examples: 'Vincent', 'Five dollar shake', '@$%'
![Page 69: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/69.jpg)
Atoms
• A sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with a lowercase letter – Examples: butch, big_kahuna_burger, playGuitar
• An arbitrary sequence of characters enclosed in single quotes – Examples: 'Vincent', 'Five dollar shake', '@$%'
• A sequence of special characters
– Examples: : , ; . :-
![Page 70: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/70.jpg)
Numbers
• Integers: 12, -34, 22342
• Floats: 34573.3234, 0.3435
![Page 71: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/71.jpg)
Variables
• A sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with either an uppercase letter or an underscore
• Examples: X, Y, Variable, Vincent, _tag
![Page 72: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/72.jpg)
Complex Terms
• Atoms, numbers and variables are building blocks for complex terms
• Complex terms are built out of a functor directly followed by a sequence of arguments – Arguments are put in round brackets,
separated by commas – The functor must be an atom
![Page 73: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/73.jpg)
Examples of complex terms
• Examples we have seen before: – playsAirGuitar(jody) – loves(vincent, mia) – jealous(marsellus, W)
• Complex terms inside complex terms: – hide(X,father(father(father(butch))))
![Page 74: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/74.jpg)
Arity
• The number of arguments a complex term has is called its arity
• Examples: woman(mia) is a term with arity 1 loves(vincent,mia) has arity 2 father(father(butch)) arity 1
![Page 75: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/75.jpg)
Arity is important
• You can define two predicates with the same functor but with different arity
• Prolog would treat this as two different predicates!
• In Prolog documentation, arity of a predicate is usually indicated with the suffix "/" followed by a number to indicate the arity
![Page 76: chapter 1 (PDF) - Learn Prolog · PDF fileSWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a79218c7f8b9a07628c9726/html5/thumbnails/76.jpg)
Example of Arity
• This knowledge base defines – happy/1 – listens2music/1 – playsAirGuitar/1
happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).