
%%CPSC 312 2018 - Midterm #3 - A Solution

% Question 1

:- dynamic n,v.
       
p :- m,c.
p :- r,s.
m :- n.
c.
r :- t.
r :- w.
t :- v.
w.
s :- a.
a.

% The simplest model with m is false, has m and n false, and all other atoms are true

/*
yes :- p       
yes :- r,s        m :- r,s
yes :- w,s        r :- w
yes :- s          w
yes :- a          s :- a
yes :-            a

*/

% Question 2

/*
[trace]  ?- p.
   Call: p 
   Call: m **
   Call: n 
   Fail: n **
   Fail: m 
   Redo: p 
   Call: r  **
   Call: t 
   Call: v  
   Fail: v 
   Fail: t **
   Redo: r 
   Call: w 
   Exit: w 
   Exit: r 
   Call: s  **
   Call: a 
   Exit: a 
   Exit: s **
   Exit: p 
true.
*/


% name(P,F,L) means person P's first name is F and last name is L
name(davidp, "David", "Poole").
name(ainaz , "Ainaz", "Hajimoradlou").
name(liran, "Liran", "Li").
name(rui, "Rui", "Ge").


% office_hour(P, D, S, F) means the office hours for person P are are on day D from S to F (on a 24 hour clock).
% This is taken from the course home page
office_hour(davidp, wednesday, 15, 16).
office_hour(ainaz, thursday, 16, 17).
office_hour(ainaz, tuesday, 17, 18).
office_hour(liran, monday, 11, 12).
office_hour(liran, wednesday, 17, 18).
office_hour(rui, thursday, 10, 11).
office_hour(rui, friday, 16, 17).

% twoofficehours(D) is true if there are two office hours on day D starting at different times
twoofficehours(D) :-
    office_hour(_,D,H1,_),
    office_hour(_,D,H2,_),
    H1 < H2.

% David wrote H1 < H2 because (a) the hours need to be different and (b) we only want one proof for each pair not two


% office_hours_on_same_day(F1,F2) is true if F1 and F2 are the first names of two people that have office hours 
office_hours_on_same_day(F1,F2) :-
    office_hour(P1,D,_,_),
    office_hour(P2,D,_,_),
    dif(P1,P2),
    name(P1,F1,_),
    name(P2,F2,_).


% Question 4

%delall(E,L,R)
delall(_,[],[]).
delall(E,[E|T],R) :- delall(E,T,R).
delall(E,[H|T],[H|R]) :- dif(E,H), delall(E,T,R).

%delall(a,[a,v,a,t,a,r],R).
% delall(a,[f,U,n],R).

/*
?- delall(X,[a,b,c,a],R).
X = a,
R = [b, c] ;
X = b,
R = [a, c, a] ;
X = c,
R = [a, b, a] ;
R = [a, b, c, a],
dif(X, a),
dif(X, c),
dif(X, b),
dif(X, a).

*/
