Tutorial 7

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Advanced Programming Language Concepts (CT006-3-3) Prolog

Tutorial -7
Question:
1. Define the logic programming paradigm.

2. Download from https://www.swi-prolog.org/download/stable


Install to i.e., C:\bin\swipl

After installation:
Use your text editor to create a new knowledge base text file, copy the following content into it
and save it as 'C:/bin/swipl/6/instructor.pl' .

% Fact
instructor(perkowski, ee271).
instructor(perkowski, ee171).
instructor(perkowski, ee478).
enrolled(alan-cheng, ee475).
enrolled(matthew, ee171).
enrolled(alan-cheng,ee171).
enrolled(alan-cheng,ee271).
enrolled(chris-clark,ee271).
enrolled(edison-tsai, ee171).
enrolled(chris-clark, ee171).
% Rule
teaches(Professor, Student) :- instructor(Professor,Class), enrolled(Student,Class).

In the SWI-Prolog system, ?-

pwd.
It shows your current working directory. To change it to 'C:/bin/swipl/6',

working_directory(_,'C:/bin/swipl/6').
pwd. To confirm

To use instructor.pl as knowledge base,

consult("C:/bin/swipl/6/instructor.pl").
instructor.pl is a knowledge base file which consist of
Prolog facts – a database of predicates and associations.
Prolog rules – define new predicates by using Prolog facts.
Note: Prolog considers capital letters to denote variables, not predicates.

Level 3 Asia Pacific University of Technology and Innovation Page 1 of 2


Advanced Programming Language Concepts (CT006-3-3) Prolog

Prolog Queries
A query searches the database for the first fact that satisfies its goal.
If a fact is found then it either unifies the variable with a constant or Prolog returns true. / yes.
If a fact is not found that meets that condition then Prolog returns false. / no.

Disjunction and Conjunction


Use a semi-colon to request subsequent answers. In other words, a semi-colon signifies
disjunction.
A comma signifies conjunction.

Once you have a knowledge base to consult, you can issue a query such as:

enrolled(X,ee271).
;

Level 3 Asia Pacific University of Technology and Innovation Page 2 of 2


Advanced Programming Language Concepts (CT006-3-3) Prolog

Prolog rules:
teaches(Professor, Student) :- instructor(Professor,Class), enrolled(Student,Class).
This is to say that an instructor only teaches if he teaches a class and students are
enrolled in that class.

teaches (Professor, Student) :- instructor (Professor, Class), enrolled (Student,Class)


teaches (Professor, Student) <- instructor (Professor, Class), enrolled (Student,Class)
instructor (Professor, Class), enrolled (Student,Class) -> teaches (Professor, Student)

Level 3 Asia Pacific University of Technology and Innovation Page 3 of 2


Advanced Programming Language Concepts (CT006-3-3) Prolog

3. In PROLOG, consult instructorx.pl

In the Prolog system,


i. enrolled(X, cs365).
ii. classmates(joseph, danielle,C).
iii. classmates(joseph, jessica, C).

% Fact
instructor(perkowski, ee271).
instructor(perkowski, ee171).
instructor(perkowski, ee478).
instructor(bebis, cs365).
instructor(looney, cs311).
instructor(yuksel, cs446).
instructor(helfand, cs493).
instructor(quint, math486).
enrolled(jeske, ee171).
enrolled(greenwood, ee171).
enrolled(alan-cheng,ee171).
enrolled(alan-cheng,ee271).
enrolled(chris-clark,ee271).
enrolled(edison-tsai, ee171).
enrolled(chris-clark, ee171).
enrolled(ben, cs365).
enrolled(bill, cs365).
enrolled(bill, cs446).
enrolled(brian, cs311).
enrolled(brian, cs365).
enrolled(brittney, cs311).
enrolled(brittney, cs365).
enrolled(brittney, cs446).
enrolled(cody, cs311).
enrolled(cody, cs365).
enrolled(danielle, cs365).
enrolled(danielle, cs446).
enrolled(danielle, cs493).
enrolled(david, cs365).
enrolled(javier, cs365).
enrolled(jeffrey, cs365).
enrolled(jessica, cs311).
enrolled(jessica, cs446).
enrolled(jessica, math486).
enrolled(joel, cs365).
enrolled(joseph, cs311).

Level 3 Asia Pacific University of Technology and Innovation Page 4 of 2


Advanced Programming Language Concepts (CT006-3-3) Prolog

enrolled(joseph, cs365).
enrolled(joseph, cs446).
enrolled(joseph, cs493).
enrolled(joseph, math486).
enrolled(kellen, cs365).
enrolled(matts, cs311).
enrolled(matts, cs365).
enrolled(mattw, cs311).
enrolled(mattw, cs365).
enrolled(mattw, cs446).
enrolled(miran, cs365).
enrolled(ryan, cs365).
enrolled(samuel, cs365).
enrolled(shane, cs311).
enrolled(shane, cs365).
enrolled(shane, cs446).
enrolled(tiffany, cs311).
enrolled(tiffany, cs365).
enrolled(tiffany, cs446).
% Rule
teaches(Professor, Student) :- instructor(Professor,Class), enrolled(Student,Class).
classmates(S1, S2) :- enrolled(S1,C), enrolled(S2,C).
classmates(S1, S2, C) :- enrolled(S1,C), enrolled(S2,C).

Level 3 Asia Pacific University of Technology and Innovation Page 5 of 2

You might also like