% file:    tutors.pl
% author:  keller
% purpose: Prolog tutorial example involving tutors, dorms, etc.

:- ensure_loaded(enumerate).

% lives(N, D) means that person named N lives in dorm D

lives(john,    east).
lives(naima,   south).
lives(alice,   west).
lives(toshiko, east).
lives(roy,     north).
lives(albert,  south).

% takes(N, D, C) means that person named N takes course C in department D

takes(john,    cs,   60).
takes(naima,   cs,   60).
takes(alice,   cs,   60).
takes(toshiko, cs,    5).
takes(albert,  cs,   60).
takes(roy,     math, 55).
takes(naima,   math, 55).
takes(alice,   math, 70).
takes(toshiko, math, 80).
takes(albert,  math, 55).

% tutors(N, D, C) means that person named N tutors course C in department D

tutors(john,   cs,    5).
tutors(naima,  cs,    5).
tutors(roy,    math,  3).
tutors(alice,  math, 55).
tutors(albert, math,  4).


% livesInEast(X) means that person named X lives in east.

livesInEast(X) :- lives(X, east).


% canTutor(X, Y) means that person X can tutor person Y (because X is tutoring
% a course that Y is taking)

canTutor(X, Y) :- 
    tutors(X, Dept, Number), 
    takes(Y, Dept, Number).


% knows(X, Y) means that person X knows person Y

knows(X, Y) :- knows(X, Y, _).

% knows(X, Y, R) means that person X knows person Y for reason R
% For example, R can be 'living' because both live in the same dorm, 
% or 'tutoring' because Y can tutor X.

knows(X, Y, living) :-
    lives(X, Z),
    lives(Y, Z).

knows(X, Y, tutoring) :-
    canTutor(Y, X).

