prolog
TRANSCRIPT
![Page 1: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/1.jpg)
Introduction to Prolog
Amey Karkare
karkare @ cse.iitb.ac.in
Dept of CSE, IIT Bombay
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.1/41
![Page 2: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/2.jpg)
Goals
• Introduce basic concepts in Prolog
• Start with XSB Prolog interpreter• Share some tips and tricks of Prolog
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.2/41
![Page 3: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/3.jpg)
Goals
• Introduce basic concepts in Prolog• Start with XSB Prolog interpreter
• Share some tips and tricks of Prolog
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.2/41
![Page 4: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/4.jpg)
Goals
• Introduce basic concepts in Prolog• Start with XSB Prolog interpreter• Share some tips and tricks of Prolog
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.2/41
![Page 5: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/5.jpg)
Prolog System
PrologDataBase
PrologInterpreter
(XSB)
Programmer
User
.P
QueryAnswer
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.3/41
![Page 6: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/6.jpg)
A Real Life Example
CSE Courses in Semester 2• Courses Offered
Course# Offered By Course# Offered By
CS 606 ranade CS 613 asCS 630 sundar CS 638 udayCS 686 rkj CS 152 asCS 154 as CS 324 udayCS 394 uday CS 640 krishnas
• Students Enrolled
Course Students
CS 638 5007, 5020, 5026, 5601, 5044, 5401, . . .CS 612 5601, 5401, 5044, 5003, 5023, 5024, . . .. . . . . .
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.4/41
![Page 7: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/7.jpg)
A Real Life Example
CSE Courses in Semester 2• Courses Offered
Course# Offered By Course# Offered By
CS 606 ranade CS 613 asCS 630 sundar CS 638 udayCS 686 rkj CS 152 asCS 154 as CS 324 udayCS 394 uday CS 640 krishnas
• Students Enrolled
Course Students
CS 638 5007, 5020, 5026, 5601, 5044, 5401, . . .CS 612 5601, 5401, 5044, 5003, 5023, 5024, . . .. . . . . .
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.4/41
![Page 8: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/8.jpg)
A Real Life Example
CSE Courses in Semester 2• Courses Offered
Course# Offered By Course# Offered By
CS 606 ranade CS 613 asCS 630 sundar CS 638 udayCS 686 rkj CS 152 asCS 154 as CS 324 udayCS 394 uday CS 640 krishnas
• Students Enrolled
Course Students
CS 638 5007, 5020, 5026, 5601, 5044, 5401, . . .CS 612 5601, 5401, 5044, 5003, 5023, 5024, . . .. . . . . .
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.4/41
![Page 9: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/9.jpg)
A Real Life Example
CSE Courses in Semester 2• Courses Offered
Course# Offered By Course# Offered By
CS 606 ranade CS 613 asCS 630 sundar CS 638 udayCS 686 rkj CS 152 asCS 154 as CS 324 udayCS 394 uday CS 640 krishnas
• Students Enrolled
Course Students
CS 638 5007, 5020, 5026, 5601, 5044, 5401, . . .CS 612 5601, 5401, 5044, 5003, 5023, 5024, . . .. . . . . .
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.4/41
![Page 10: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/10.jpg)
A Real Life Example (Contd. . . )
• Simple Queries:
◦ Who teaches CS 154?◦ What is the course offered by Prof. rkj?◦ Who are the students enrolled in CS 638?
• Complex Queries:◦ List all professors who teach student with roll # 5044.◦ What are all the courses offered by Prof. uday?◦ Make pairs of students enrolled in CS 638, such that
both students in a pair are not enrolled in same course(Of course, other than CS638).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.5/41
![Page 11: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/11.jpg)
A Real Life Example (Contd. . . )
• Simple Queries:◦ Who teaches CS 154?
◦ What is the course offered by Prof. rkj?◦ Who are the students enrolled in CS 638?
• Complex Queries:◦ List all professors who teach student with roll # 5044.◦ What are all the courses offered by Prof. uday?◦ Make pairs of students enrolled in CS 638, such that
both students in a pair are not enrolled in same course(Of course, other than CS638).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.5/41
![Page 12: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/12.jpg)
A Real Life Example (Contd. . . )
• Simple Queries:◦ Who teaches CS 154?◦ What is the course offered by Prof. rkj?
◦ Who are the students enrolled in CS 638?
• Complex Queries:◦ List all professors who teach student with roll # 5044.◦ What are all the courses offered by Prof. uday?◦ Make pairs of students enrolled in CS 638, such that
both students in a pair are not enrolled in same course(Of course, other than CS638).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.5/41
![Page 13: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/13.jpg)
A Real Life Example (Contd. . . )
• Simple Queries:◦ Who teaches CS 154?◦ What is the course offered by Prof. rkj?◦ Who are the students enrolled in CS 638?
• Complex Queries:◦ List all professors who teach student with roll # 5044.◦ What are all the courses offered by Prof. uday?◦ Make pairs of students enrolled in CS 638, such that
both students in a pair are not enrolled in same course(Of course, other than CS638).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.5/41
![Page 14: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/14.jpg)
A Real Life Example (Contd. . . )
• Simple Queries:◦ Who teaches CS 154?◦ What is the course offered by Prof. rkj?◦ Who are the students enrolled in CS 638?
• Complex Queries:
◦ List all professors who teach student with roll # 5044.◦ What are all the courses offered by Prof. uday?◦ Make pairs of students enrolled in CS 638, such that
both students in a pair are not enrolled in same course(Of course, other than CS638).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.5/41
![Page 15: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/15.jpg)
A Real Life Example (Contd. . . )
• Simple Queries:◦ Who teaches CS 154?◦ What is the course offered by Prof. rkj?◦ Who are the students enrolled in CS 638?
• Complex Queries:◦ List all professors who teach student with roll # 5044.
◦ What are all the courses offered by Prof. uday?◦ Make pairs of students enrolled in CS 638, such that
both students in a pair are not enrolled in same course(Of course, other than CS638).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.5/41
![Page 16: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/16.jpg)
A Real Life Example (Contd. . . )
• Simple Queries:◦ Who teaches CS 154?◦ What is the course offered by Prof. rkj?◦ Who are the students enrolled in CS 638?
• Complex Queries:◦ List all professors who teach student with roll # 5044.◦ What are all the courses offered by Prof. uday?
◦ Make pairs of students enrolled in CS 638, such thatboth students in a pair are not enrolled in same course(Of course, other than CS638).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.5/41
![Page 17: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/17.jpg)
A Real Life Example (Contd. . . )
• Simple Queries:◦ Who teaches CS 154?◦ What is the course offered by Prof. rkj?◦ Who are the students enrolled in CS 638?
• Complex Queries:◦ List all professors who teach student with roll # 5044.◦ What are all the courses offered by Prof. uday?◦ Make pairs of students enrolled in CS 638, such that
both students in a pair are not enrolled in same course(Of course, other than CS638).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.5/41
![Page 18: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/18.jpg)
A Real Life Example (Contd. . . )
• Simple Queries:◦ Who teaches CS 154?◦ What is the course offered by Prof. rkj?◦ Who are the students enrolled in CS 638?
• Complex Queries:◦ List all professors who teach student with roll # 5044.◦ What are all the courses offered by Prof. uday?◦ Make pairs of students enrolled in CS 638, such that
both students in a pair are not enrolled in same course(Of course, other than CS638).
Need to encode the data for Prolog Interpreter.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.5/41
![Page 19: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/19.jpg)
Time to learn Prolog syntax!
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.6/41
![Page 20: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/20.jpg)
Prolog Syntax: Atoms
• Building blocks of Prolog.
• lowercase letter followed by lower/uppercase letter,underscore and digits, e.g. course, cs638, len N.
• anything enclosed in single quotes(’), e.g.’Dept of CSE’, ’&%$ >’, ’Address’.
• string of special characters (Reserved words), e.g. :-, ;, .,,.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.7/41
![Page 21: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/21.jpg)
Prolog Syntax: Atoms
• Building blocks of Prolog.• lowercase letter followed by lower/uppercase letter,
underscore and digits, e.g. course, cs638, len N.
• anything enclosed in single quotes(’), e.g.’Dept of CSE’, ’&%$ >’, ’Address’.
• string of special characters (Reserved words), e.g. :-, ;, .,,.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.7/41
![Page 22: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/22.jpg)
Prolog Syntax: Atoms
• Building blocks of Prolog.• lowercase letter followed by lower/uppercase letter,
underscore and digits, e.g. course, cs638, len N.• anything enclosed in single quotes(’), e.g.’Dept of CSE’, ’&%$ >’, ’Address’.
• string of special characters (Reserved words), e.g. :-, ;, .,,.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.7/41
![Page 23: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/23.jpg)
Prolog Syntax: Atoms
• Building blocks of Prolog.• lowercase letter followed by lower/uppercase letter,
underscore and digits, e.g. course, cs638, len N.• anything enclosed in single quotes(’), e.g.’Dept of CSE’, ’&%$ >’, ’Address’.
• string of special characters (Reserved words), e.g. :-, ;, .,,.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.7/41
![Page 24: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/24.jpg)
Prolog Syntax: Numbers
• integers – . . . , -3, -2, -1, 0, 1, 2, 3, . . .
• floating point numbers – 3.1416, 2.7138, 0.001• We will be working mostly with integers.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.8/41
![Page 25: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/25.jpg)
Prolog Syntax: Numbers
• integers – . . . , -3, -2, -1, 0, 1, 2, 3, . . .• floating point numbers – 3.1416, 2.7138, 0.001
• We will be working mostly with integers.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.8/41
![Page 26: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/26.jpg)
Prolog Syntax: Numbers
• integers – . . . , -3, -2, -1, 0, 1, 2, 3, . . .• floating point numbers – 3.1416, 2.7138, 0.001• We will be working mostly with integers.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.8/41
![Page 27: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/27.jpg)
Prolog Syntax: Variables
• uppercase letter followed by lower/uppercase letter,underscore and digits, e.g. Course, Cs638, Len.
• underscore followed by lower/uppercase letter, underscoreand digits, e.g. course, 638, Len.
• (underscore only) is special anonymous variable.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.9/41
![Page 28: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/28.jpg)
Prolog Syntax: Variables
• uppercase letter followed by lower/uppercase letter,underscore and digits, e.g. Course, Cs638, Len.
• underscore followed by lower/uppercase letter, underscoreand digits, e.g. course, 638, Len.
• (underscore only) is special anonymous variable.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.9/41
![Page 29: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/29.jpg)
Prolog Syntax: Variables
• uppercase letter followed by lower/uppercase letter,underscore and digits, e.g. Course, Cs638, Len.
• underscore followed by lower/uppercase letter, underscoreand digits, e.g. course, 638, Len.
• (underscore only) is special anonymous variable.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.9/41
![Page 30: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/30.jpg)
Prolog Syntax: Variables
• uppercase letter followed by lower/uppercase letter,underscore and digits, e.g. Course, Cs638, Len.
• underscore followed by lower/uppercase letter, underscoreand digits, e.g. course, 638, Len.
• (underscore only) is special anonymous variable.
Attention: Two occurrences of are different variables.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.9/41
![Page 31: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/31.jpg)
Prolog Syntax: Complex Terms
• functor followed by arguments, e.g.brother(ram, lakshman)capital(india, delhi)set level(X, next(Current))
• functor must be an atom.• arguments can be any kind of prolog terms (atom, number,
variable, complex term).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.10/41
![Page 32: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/32.jpg)
Prolog Syntax: Complex Terms
• functor followed by arguments, e.g.brother(ram, lakshman)capital(india, delhi)set level(X, next(Current))
• functor must be an atom.
• arguments can be any kind of prolog terms (atom, number,variable, complex term).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.10/41
![Page 33: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/33.jpg)
Prolog Syntax: Complex Terms
• functor followed by arguments, e.g.brother(ram, lakshman)capital(india, delhi)set level(X, next(Current))
• functor must be an atom.• arguments can be any kind of prolog terms (atom, number,
variable, complex term).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.10/41
![Page 34: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/34.jpg)
Prolog Syntax: Facts
• Complex terms followed by full-stop(.).• Used to state unconditional truth.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.11/41
![Page 35: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/35.jpg)
Example: Facts
% offered by(+Course, +Prof):% database of courses offered by professors.offered by(cs606, ranade).offered by(cs613, as).offered by(cs630, sundar).offered by(cs638, uday).offered by(cs686, rkj).offered by(cs152, as).offered by(cs154, as).offered by(cs324, uday).offered by(cs394, uday).offered by(cs640, krishnas).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.12/41
![Page 36: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/36.jpg)
Example: Facts (Contd. . . )
% enrolled(+Course, +RollNo):% database of students enrolled in coursesenrolled(cs638, 5007).enrolled(cs638, 5020).enrolled(cs638, 5026).enrolled(cs638, 5601).enrolled(cs638, 5044).enrolled(cs638, 5401)....enrolled(cs612, 5601).enrolled(cs612, 5401).enrolled(cs612, 5044).enrolled(cs612, 5003).enrolled(cs612, 5023).enrolled(cs612, 5024)....
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.13/41
![Page 37: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/37.jpg)
Prolog Syntax: Queries
• Complex terms followed by full-stop(.).
Wait!!! Isn’t that what a Fact is?
• Queries are not unconditional truth, but a question asked.Prolog interpreter need to infer the truth of queries.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.14/41
![Page 38: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/38.jpg)
Prolog Syntax: Queries
• Complex terms followed by full-stop(.).
Wait!!! Isn’t that what a Fact is?
• Queries are not unconditional truth, but a question asked.Prolog interpreter need to infer the truth of queries.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.14/41
![Page 39: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/39.jpg)
Prolog Syntax: Queries
• Complex terms followed by full-stop(.).
Wait!!! Isn’t that what a Fact is?
• Queries are not unconditional truth, but a question asked.Prolog interpreter need to infer the truth of queries.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.14/41
![Page 40: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/40.jpg)
XSB Quick-start
• Save the facts in a file, say course.P.
• Run XSB (command xsb) from shell.• load the file in XSB.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.15/41
![Page 41: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/41.jpg)
XSB Quick-start
• Save the facts in a file, say course.P.• Run XSB (command xsb) from shell.
• load the file in XSB.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.15/41
![Page 42: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/42.jpg)
XSB Quick-start
• Save the facts in a file, say course.P.• Run XSB (command xsb) from shell.
• load the file in XSB.
$ xsb
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.15/41
![Page 43: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/43.jpg)
XSB Quick-start
• Save the facts in a file, say course.P.• Run XSB (command xsb) from shell.
• load the file in XSB.
$ xsb[xsb configuration loaded][sysinitrc loaded]XSB Version 2.7.1 (Kinryo) of March 5, 2005[i686-pc-linux-gnu; mode: optimal; engine: slg-wam; gc: indirection; scheduling: local]
| ?-
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.15/41
![Page 44: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/44.jpg)
XSB Quick-start
• Save the facts in a file, say course.P.• Run XSB (command xsb) from shell.• load the file in XSB.
$ xsb[xsb configuration loaded][sysinitrc loaded]XSB Version 2.7.1 (Kinryo) of March 5, 2005[i686-pc-linux-gnu; mode: optimal; engine: slg-wam; gc: indirection; scheduling: local]
| ?-
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.15/41
![Page 45: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/45.jpg)
XSB Quick-start
• Save the facts in a file, say course.P.• Run XSB (command xsb) from shell.• load the file in XSB.
$ xsb[xsb configuration loaded][sysinitrc loaded]XSB Version 2.7.1 (Kinryo) of March 5, 2005[i686-pc-linux-gnu; mode: optimal; engine: slg-wam; gc: indirection; scheduling: local]
| ?- [’course.P’].
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.15/41
![Page 46: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/46.jpg)
XSB Quick-start
• Save the facts in a file, say course.P.• Run XSB (command xsb) from shell.• load the file in XSB.
$ xsb[xsb configuration loaded][sysinitrc loaded]XSB Version 2.7.1 (Kinryo) of March 5, 2005[i686-pc-linux-gnu; mode: optimal; engine: slg-wam; gc: indirection; scheduling: local]
| ?- [’course.P’].[Compiling ./course][course compiled, cpu time used: 0.0360 seconds][course loaded]yes| ?-
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.15/41
![Page 47: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/47.jpg)
Example: Queries
| ?- offered by(cs154, X).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 48: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/48.jpg)
Example: Queries
| ?- offered by(cs154, X).X = as
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 49: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/49.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?-
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 50: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/50.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 51: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/51.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 52: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/52.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?-
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 53: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/53.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 54: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/54.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 55: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/55.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 56: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/56.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 57: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/57.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 58: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/58.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 59: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/59.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026;
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 60: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/60.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026;X = 5601
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 61: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/61.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026;X = 5601;
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 62: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/62.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026;X = 5601;X = 5044
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 63: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/63.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026;X = 5601;X = 5044;
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 64: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/64.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026;X = 5601;X = 5044;X = 5401
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 65: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/65.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026;X = 5601;X = 5044;X = 5401;
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 66: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/66.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026;X = 5601;X = 5044;X = 5401;no| ?-
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 67: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/67.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026;X = 5601;X = 5044;X = 5401;no| ?- offered by(cs158, X).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 68: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/68.jpg)
Example: Queries
| ?- offered by(cs154, X).X = asyes| ?- offered by(X, rkj).X = cs686yes| ?- enrolled(cs638, X).X = 5007;X = 5020;X = 5026;X = 5601;X = 5044;X = 5401;no| ?- offered by(cs158, X).no| ?-
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41
![Page 69: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/69.jpg)
Points to Note
• Note the . (full-stop) at the end of queries (and facts).• Answer is typically given as a binding to variable(s).• yes means prolog was able to find an answer.• no means prolog could not find an answer.• Entering ; asks prolog to search for more answers, i.e. a
different binding to variables satisfying the query.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.17/41
![Page 70: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/70.jpg)
Prolog Syntax: Rules
• Rules are conditional truth!, i.e. if something thensomething-else.
• The syntax is: Head :- Body.• Head is a complex term.• Body is a sequence of complex terms (possibly only one),
separated by commas(,).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.18/41
![Page 71: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/71.jpg)
Example: Rules
• Conjunctionteaches(Prof, Student) :-
offered by(Course, Prof),enrolled(Course, Student).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.19/41
![Page 72: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/72.jpg)
Example: Rules
• Disjunctionlikes compiler(Student) :-
enrolled(cs638, X).likes compiler(Student) :-
enrolled(cs613, X).
• Attention:Make sure the different bodies of disjunctive clauses are:◦ Mutually exclusive - unless you are really sure you want
multiple, non-deterministic answers◦ Exhaustive - cover all the cases, possibly implicitly
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.20/41
![Page 73: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/73.jpg)
Example: Rules
• Disjunctionlikes compiler(Student) :-
enrolled(cs638, X).likes compiler(Student) :-
enrolled(cs613, X).
• Attention:Make sure the different bodies of disjunctive clauses are:
◦ Mutually exclusive - unless you are really sure you wantmultiple, non-deterministic answers
◦ Exhaustive - cover all the cases, possibly implicitly
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.20/41
![Page 74: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/74.jpg)
Example: Rules
• Disjunctionlikes compiler(Student) :-
enrolled(cs638, X).likes compiler(Student) :-
enrolled(cs613, X).
• Attention:Make sure the different bodies of disjunctive clauses are:◦ Mutually exclusive - unless you are really sure you want
multiple, non-deterministic answers
◦ Exhaustive - cover all the cases, possibly implicitly
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.20/41
![Page 75: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/75.jpg)
Example: Rules
• Disjunctionlikes compiler(Student) :-
enrolled(cs638, X).likes compiler(Student) :-
enrolled(cs613, X).
• Attention:Make sure the different bodies of disjunctive clauses are:◦ Mutually exclusive - unless you are really sure you want
multiple, non-deterministic answers◦ Exhaustive - cover all the cases, possibly implicitly
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.20/41
![Page 76: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/76.jpg)
Example: Rules
• Negationdislikes compiler(Student) :-
not(likes compiler(Student)).
• Warning:Negation may not work as expected (Why?)
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.21/41
![Page 77: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/77.jpg)
Example: Rules
• Negationdislikes compiler(Student) :-
not(likes compiler(Student)).
• Warning:Negation may not work as expected (Why?)
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.21/41
![Page 78: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/78.jpg)
Matching
• The process of finding solutions to a query.
• = predicate tests if its arguments match (note that we saymatch, not equal).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.22/41
![Page 79: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/79.jpg)
Matching
• The process of finding solutions to a query.• = predicate tests if its arguments match (note that we say
match, not equal).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.22/41
![Page 80: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/80.jpg)
Matching: Atoms, Variables
• Same atoms match. e.g.cs613 matches cs613, i.e. cs613 = cs613
’cs638’ = cs638 (Surprised?)54 does not match ’54’ (Surprised Again?)
• A variable matches any other term.X = cs638Z = YW = left(right(building(bricks,walls)))= 5004
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.23/41
![Page 81: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/81.jpg)
Matching: Atoms, Variables
• Same atoms match. e.g.cs613 matches cs613, i.e. cs613 = cs613
’cs638’ = cs638 (Surprised?)54 does not match ’54’ (Surprised Again?)
• A variable matches any other term.X = cs638Z = YW = left(right(building(bricks,walls)))= 5004
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.23/41
![Page 82: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/82.jpg)
Matching: Complex Terms
• Two complex term match if:◦ they have same functor, and◦ they have same arity, and◦ their corresponding arguments match, without any clash.
left(X) = left(right(building(bricks)))father(X, dashrath) \= father(ram, X)father( , dashrath) = father(ram, ) (Why?)id(1, 1) = id(X, X)
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.24/41
![Page 83: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/83.jpg)
Prolog: Arithmetic
• Arithmetic operators:+, -, *, /, // (integer division), mod
• is operator is required to force evaluation.• Only right hand side of is is evaluated.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.25/41
![Page 84: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/84.jpg)
Prolog: Arithmetic
• Arithmetic operators:+, -, *, /, // (integer division), mod
• is operator is required to force evaluation.• Only right hand side of is is evaluated.
| ?- X = 3+2.X = 3 + 2yes
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.25/41
![Page 85: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/85.jpg)
Prolog: Arithmetic
• Arithmetic operators:+, -, *, /, // (integer division), mod
• is operator is required to force evaluation.
• Only right hand side of is is evaluated.
| ?- X = 3+2.X = 3 + 2yes
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.25/41
![Page 86: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/86.jpg)
Prolog: Arithmetic
• Arithmetic operators:+, -, *, /, // (integer division), mod
• is operator is required to force evaluation.
• Only right hand side of is is evaluated.
| ?- X = 3+2.X = 3 + 2yes
| ?- Y is mod(3,4).Y = 3yes
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.25/41
![Page 87: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/87.jpg)
Prolog: Arithmetic
• Arithmetic operators:+, -, *, /, // (integer division), mod
• is operator is required to force evaluation.• Only right hand side of is is evaluated.
| ?- X = 3+2.X = 3 + 2yes
| ?- Y is mod(3,4).Y = 3yes
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.25/41
![Page 88: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/88.jpg)
Prolog: Arithmetic
• Arithmetic operators:+, -, *, /, // (integer division), mod
• is operator is required to force evaluation.• Only right hand side of is is evaluated.
| ?- X = 3+2.X = 3 + 2yes
| ?- Y is mod(3,4).Y = 3yes
| ?- 5 is 6 - 1.yes| ?- 6 - 1 is 5.no
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.25/41
![Page 89: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/89.jpg)
Prolog: More Arithmetic
• Comparison operators:<, =<, >=, >, =:=, =\=
• Both sides of the operators are evaluated.| ?- 2+3 < 5+1.yes
| ?- X < 3+3.++Error[XSB/Runtime/P]: [Instantiation] Error:Uninstantiated Arithmetic Expression
| ?- X = 4, X < 3+3.X = 4yes
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.26/41
![Page 90: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/90.jpg)
Prolog: More Arithmetic
• Comparison operators:<, =<, >=, >, =:=, =\=
• Both sides of the operators are evaluated.| ?- 2+3 < 5+1.yes
| ?- X < 3+3.++Error[XSB/Runtime/P]: [Instantiation] Error:Uninstantiated Arithmetic Expression
| ?- X = 4, X < 3+3.X = 4yes
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.26/41
![Page 91: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/91.jpg)
Recursion
• A predicate defined in terms of itself.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.27/41
![Page 92: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/92.jpg)
Recursion
• A predicate defined in terms of itself.
path(X, Y) :-edge(X, Y).
path(X, Y) :-edge(X, Z),path(Z, Y).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.27/41
![Page 93: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/93.jpg)
Recursion
• A predicate defined in terms of itself.
path(X, Y) :-edge(X, Y).
path(X, Y) :-edge(X, Z),path(Z, Y).
ancestor(X, Y) :-father(X, Y).
ancestor(X, Y) :-father(X, Z),ancestor(Z, Y).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.27/41
![Page 94: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/94.jpg)
Lists
• Intuitively, a collection of data.• Can be empty or non-empty• empty list, denoted by []• non-empty list consists of:
◦ Head: A prolog term (may be a list itself!)◦ Tail: A list (possibly empty)
• Single element list has empty tail.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.28/41
![Page 95: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/95.jpg)
List Examples
• List of integers:[1, 2, 3, 4]Head = 1, Tail = [2, 3, 4]
• Mixed list:[1, a, ’!@#’, edge(n0, n1)]Head = 1, Tail = [a, ’!@#’, edge(n0, n1)]
• List of list of characters:[[a,p,p,l,e], [b,a,t], [c,a,t]]Head = [a,p,p,l,e], Tail = [[b,a,t], [c,a,t]]
• Single element list:[[]]Head = [], Tail = []
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.29/41
![Page 96: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/96.jpg)
List Examples
• List of integers:[1, 2, 3, 4]Head = 1, Tail = [2, 3, 4]
• Mixed list:[1, a, ’!@#’, edge(n0, n1)]Head = 1, Tail = [a, ’!@#’, edge(n0, n1)]
• List of list of characters:[[a,p,p,l,e], [b,a,t], [c,a,t]]Head = [a,p,p,l,e], Tail = [[b,a,t], [c,a,t]]
• Single element list:[[]]Head = [], Tail = []
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.29/41
![Page 97: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/97.jpg)
List Examples
• List of integers:[1, 2, 3, 4]Head = 1, Tail = [2, 3, 4]
• Mixed list:[1, a, ’!@#’, edge(n0, n1)]Head = 1, Tail = [a, ’!@#’, edge(n0, n1)]
• List of list of characters:[[a,p,p,l,e], [b,a,t], [c,a,t]]Head = [a,p,p,l,e], Tail = [[b,a,t], [c,a,t]]
• Single element list:[[]]Head = [], Tail = []
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.29/41
![Page 98: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/98.jpg)
List Examples
• List of integers:[1, 2, 3, 4]Head = 1, Tail = [2, 3, 4]
• Mixed list:[1, a, ’!@#’, edge(n0, n1)]Head = 1, Tail = [a, ’!@#’, edge(n0, n1)]
• List of list of characters:[[a,p,p,l,e], [b,a,t], [c,a,t]]Head = [a,p,p,l,e], Tail = [[b,a,t], [c,a,t]]
• Single element list:[[]]Head = [], Tail = []
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.29/41
![Page 99: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/99.jpg)
Lists (Contd. . . )
• Most important (Only!) data structure in Prolog• Matching a list: | operator separates a list into its head and
tail. Head/tail matched using prolog matching rules.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.30/41
![Page 100: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/100.jpg)
Lists (Contd. . . )
• Most important (Only!) data structure in Prolog• Matching a list: | operator separates a list into its head and
tail. Head/tail matched using prolog matching rules.| ?- [H | T] = [1,2,3,4].H = 1T = [2,3,4]yes
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.30/41
![Page 101: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/101.jpg)
Lists (Contd. . . )
• Most important (Only!) data structure in Prolog• Matching a list: | operator separates a list into its head and
tail. Head/tail matched using prolog matching rules.| ?- [H | T] = [1,2,3,4].H = 1T = [2,3,4]yes| ?- [H | [2,3,4]] = [1,2,3,4].H = 1yes
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.30/41
![Page 102: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/102.jpg)
Lists (Contd. . . )
• Most important (Only!) data structure in Prolog• Matching a list: | operator separates a list into its head and
tail. Head/tail matched using prolog matching rules.| ?- [H | T] = [1,2,3,4].H = 1T = [2,3,4]yes| ?- [H | [2,3,4]] = [1,2,3,4].H = 1yes| ?- [1 | [X | [3, Y]]] = [1,2,3,4].X = 2Y = 4yes
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.30/41
![Page 103: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/103.jpg)
Working with Lists: Example
% filter odd(+InList, -OddList)% copies odd numbers from InList to OddList.%
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.31/41
![Page 104: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/104.jpg)
Working with Lists: Example
% filter odd(+InList, -OddList)% copies odd numbers from InList to OddList.%filter odd([], []).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.31/41
![Page 105: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/105.jpg)
Working with Lists: Example
% filter odd(+InList, -OddList)% copies odd numbers from InList to OddList.%filter odd([], []).
filter odd([H | TIn], OddList) :-odd(H),filter odd(TIn, TOddList),OddList = [H | TOddList].
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.31/41
![Page 106: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/106.jpg)
Working with Lists: Example
% filter odd(+InList, -OddList)% copies odd numbers from InList to OddList.%filter odd([], []).
filter odd([H | TIn], OddList) :-odd(H),filter odd(TIn, TOddList),OddList = [H | TOddList].
filter odd([H | TIn], OddList) :-not(odd(H)),filter odd(TIn, TOddList),OddList = TOddList.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.31/41
![Page 107: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/107.jpg)
Working with Lists
• Typical pattern for processing lists:◦ Handle the case when list is empty.◦ Non empty list: Separate the head and the tail.
• Handle the case when head satisfies some propertyP .
• Handle the case when head does not satisfy P . Inother words, it satisfies not(P ).
• Handle rest of the list (which is now smaller than theoriginal list) recursively.
• Note that the pattern makes sure that clauses are mutuallyexclusive as well as exhaustive.
• One or more cases may be handled implicitly.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.32/41
![Page 108: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/108.jpg)
Working with Lists: Example Revised
% filter odd(+InList, -OddList)% copies odd numbers from InList to OddList.%filter odd([], []).
filter odd([H | TIn], [H | TOddList]) :-odd(H),filter odd(TIn, TOddList).
filter odd([H | TIn], TOddList) :-not(odd(H)),filter odd(TIn, TOddList).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.33/41
![Page 109: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/109.jpg)
Working with Lists: Exercise
• Write the following functions:
% member(+Element, +List)% true if Element belongs to List.
% subset(+Subset, +Set).% true if Subset is a subset of Set.% Set and Subset are given as lists.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.34/41
![Page 110: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/110.jpg)
Working with Lists: Solutions
member(X, [X| ]).member(X, [ |L]) :-
member(X, L).
subset([], S).subset([E|Es], S):-
member(E, S),subset(Es, S).
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.35/41
![Page 111: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/111.jpg)
Working with Lists: Solutions
member(X, [X| ]).member(X, [ |L]) :-
member(X, L).
subset([], S).subset([E|Es], S):-
member(E, S),subset(Es, S).
Note:• [] case handled implicitly in member.• member rules not mutually exclusive.• In subset, case of not(member(E, S)) handled
implicitly.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.35/41
![Page 112: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/112.jpg)
More Points to Note
• In Prolog, there is no strict classification of arguments asinput or output.enrolled(X, 5401) gives all courses taken by 5401.enrolled(cs638, X) gives all students enrolled incs638.
• However, for some rules, some arguments act as input,others as output, some as both.In powerset(S, PS), typically S is input, and PS output.In member(E, List), typically List is input. E can beeither input or output.
• Prolog programs may become very confusing and difficult tounderstand, if not written properly. It is very important tofollow a good, consistent coding style.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.36/41
![Page 113: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/113.jpg)
More Points to Note
• In Prolog, there is no strict classification of arguments asinput or output.enrolled(X, 5401) gives all courses taken by 5401.enrolled(cs638, X) gives all students enrolled incs638.
• However, for some rules, some arguments act as input,others as output, some as both.In powerset(S, PS), typically S is input, and PS output.In member(E, List), typically List is input. E can beeither input or output.
• Prolog programs may become very confusing and difficult tounderstand, if not written properly. It is very important tofollow a good, consistent coding style.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.36/41
![Page 114: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/114.jpg)
More Points to Note
• In Prolog, there is no strict classification of arguments asinput or output.enrolled(X, 5401) gives all courses taken by 5401.enrolled(cs638, X) gives all students enrolled incs638.
• However, for some rules, some arguments act as input,others as output, some as both.In powerset(S, PS), typically S is input, and PS output.In member(E, List), typically List is input. E can beeither input or output.
• Prolog programs may become very confusing and difficult tounderstand, if not written properly. It is very important tofollow a good, consistent coding style.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.36/41
![Page 115: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/115.jpg)
Collecting Solutions
• How to get all solutions to a query? In a neat, usable form.
• Three builtin predicates:findall, setof, bagof
• Compute a list of all solutions to query.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.37/41
![Page 116: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/116.jpg)
Collecting Solutions
• How to get all solutions to a query? In a neat, usable form.• Three builtin predicates:findall, setof, bagof
• Compute a list of all solutions to query.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.37/41
![Page 117: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/117.jpg)
Collecting Solutions
• How to get all solutions to a query? In a neat, usable form.• Three builtin predicates:findall, setof, bagof
• Compute a list of all solutions to query.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.37/41
![Page 118: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/118.jpg)
Collecting Solutions: Example
% powerset(+Set, -PowerSet).% compute Powerset of a Set.powerset(S, PS) :-
findall(SubS, subset(SubS, S), PS).
Semantics of findall, setof and bagof differ a little.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.38/41
![Page 119: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/119.jpg)
References
• More information about XSB, coding guidelines, online booketc. athttp://www.cse.iitb.ac.in/ karkare/Gc/prolog/
• Contact karkare @ cse.iitb.ac.in for comments,suggestions, problems.
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.39/41
![Page 120: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/120.jpg)
Topics Not Covered
• I/O, File I/O:read, write, tell etc
• Storage predicates:storage insert keypair,storage delete keypair,storage find keypair
• Importing/Exporting modules and functions:import, export, consult
• Debugging:trace, notrace, spy, nospy
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.40/41
![Page 121: Prolog](https://reader033.vdocuments.site/reader033/viewer/2022061201/5477f45cb4af9fad158b457c/html5/thumbnails/121.jpg)
Happy Prologging
January 12, 2006 c©Amey Karkare, 2006 Prolog – p.41/41