Professional Documents
Culture Documents
Tutorial 7
Tutorial 7
Tutorial 7
Tutorial -7
Question:
1. Define the logic programming paradigm.
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).
pwd.
It shows your current working directory. To change it to 'C:/bin/swipl/6',
working_directory(_,'C:/bin/swipl/6').
pwd. To confirm
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.
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.
Once you have a knowledge base to consult, you can issue a query such as:
enrolled(X,ee271).
;
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.
% 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).
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).