Prolog Knowledge Base – Student-Course System
Question
Create a Prolog knowledge base for a student-course system with the following
requirements:
1. There are students, courses, and teachers.
2. Each teacher teaches one or more courses.
3. Students can be enrolled in one or more courses.
4. Define a rule teacher_of(Teacher, Student) that succeeds if a teacher teaches a course the
student is enrolled in.
5. Define a rule classmates(X, Y) that is true if X and Y are different students enrolled in the
same course.
Then answer the following queries:
a) Who teaches John?
b) Are John and Sara classmates?
c) List all classmates of Lee.
d) What course does Ms. Jones teach?
Prolog Code
% --- Facts ---
student(john).
student(sara).
student(lee).
course(math).
course(physics).
teaches(mr_smith, math).
teaches(ms_jones, physics).
enrolled(john, math).
enrolled(sara, math).
enrolled(lee, physics).
% --- Rules ---
% Rule 1: Teacher of a student
teacher_of(Teacher, Student) :-
teaches(Teacher, Course),
enrolled(Student, Course).
% Rule 2: Classmates
classmates(X, Y) :-
enrolled(X, Course),
enrolled(Y, Course),
X \= Y.
Example Queries & Answers
?- teacher_of(Who, john).
% Who = mr_smith.
?- classmates(john, sara).
% true.
?- classmates(lee, Who).
% false (no classmates in physics).
?- teaches(ms_jones, What).
% What = physics.