q2a(X,Y) :- students(S,X,Y,_), enrolls(S,T,L), courses(T,L,C,_,_,_,_),
            catalog(C,'Automata').
answer2a(L) :- setof([X,Y],q2a(X,Y),L).

q2b(X,Y) :- catalog(X,Y), courses(T,L,X,_,_,_,_), enrolls(S,T,L),
            students(S,'Thomas','Timothy',_).
answer2b(L) :- setof([X,Y],q2b(X,Y),L).

q2c(X) :- students(X,_,_,_), not enrolls(X,'f96',_).
answer2c(L) :- setof([X],q2c(X),L).

q2d(X) :- enrolls(X,T1,L1), enrolls(X,T2,L2),
          courses(T1,L1,'csc226',_,_,_,_), 
          courses(T2,L2,'csc227',_,_,_,_). 
answer2d(L) :- setof([X],q2d(X),L).

q2e(X) :- enrolls(X,T1,L1), courses(T1,L1,'csc226',_,_,_,_). 
q2e(X) :- enrolls(X,T1,L1), courses(T1,L1,'csc227',_,_,_,_). 
answer2e(L) :- setof([X],q2e(X),L).

q2f_temp1(X,Y) :- students(X,_,_,_), catalog(Y,_).
q2f_temp2(X,Y) :- enrolls(X,T,L), courses(T,L,Y,_,_,_,_).
q2f_temp3(X) :- q2f_temp1(X,Y), not q2f_temp2(X,Y).
q2f(X) :- students(X,_,_,_), not q2f_temp3(X).
answer2f(L) :- setof([X],q2f(X),L).

q2g(X,Y) :- students(S,X,Y,_), not enrolls(S,_,_).
answer2g(L) :- setof([X,Y],q2g(X,Y),L).

output([]) :- !.
output([X|L]) :- write(X), nl, output(L).

go2 :- tell(f2),
answer2a(L1),write('Answer to Q2a'), nl,output(L1), nl,
answer2b(L2),write('Answer to Q2b'), nl,output(L2), nl,
answer2c(L3),write('Answer to Q2c'), nl,output(L3), nl,
answer2d(L4),write('Answer to Q2d'), nl,output(L4), nl,
answer2e(L5),write('Answer to Q2e'), nl,output(L5), nl,
answer2f(L6),write('Answer to Q2f'), nl,output(L6), nl,
answer2g(L7),write('Answer to Q2g'), nl,output(L7), nl,
told.