Prolog

121
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

Transcript of Prolog

Page 1: Prolog

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

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

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

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

Prolog System

PrologDataBase

PrologInterpreter

(XSB)

Programmer

User

.P

QueryAnswer

January 12, 2006 c©Amey Karkare, 2006 Prolog – p.3/41

Page 6: Prolog

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

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

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

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

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

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

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

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

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

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

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

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

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

Time to learn Prolog syntax!

January 12, 2006 c©Amey Karkare, 2006 Prolog – p.6/41

Page 20: Prolog

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Example: Queries

| ?- offered by(cs154, X).

January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41

Page 48: Prolog

Example: Queries

| ?- offered by(cs154, X).X = as

January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41

Page 49: Prolog

Example: Queries

| ?- offered by(cs154, X).X = asyes| ?-

January 12, 2006 c©Amey Karkare, 2006 Prolog – p.16/41

Page 50: Prolog

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Recursion

• A predicate defined in terms of itself.

January 12, 2006 c©Amey Karkare, 2006 Prolog – p.27/41

Page 92: Prolog

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Happy Prologging

January 12, 2006 c©Amey Karkare, 2006 Prolog – p.41/41