Q:

Nested Prolog queries (anding between different conditions)

I'm working on some queries about this code in Prolog:
student(ali).
student(sami).
student(rami).
student(mousa).
student(muna).
student(amal).
student(omar).

course(ai).
course(java).
course(calculus2).
course(calculus1).
course(robots).

teacher(hashem).
teacher(mohammad).
teacher(ibrahim).
teacher(kareem).

prerequest(ai,java).
prerequest(calculus2,calculus1).
prerequest(robots,ai).

study(ali,ai).
study(ali,java).
study(sami,java).
study(rami,calculus2).
study(mousa,ai).
study(muna,java).
study(amal,calculus1).
study(omar,robots).

teach(hashem,ai).
teach(mohammad,java).
teach(ibrahim,calculus2).
teach(ibrahim,calculus1).
teach(kareem,robots).

teacher_of(X, Y):-
    study(X, Z),
    teach(Z, Y).

Now, I want to find all teachers of student “Ali”?
I've tried this before:
?- teacher_of(ali, B).

but it gives me an error, and a false word (as answer) at the end of message.
Note I'm using this online editor: here.
What do you suggest guys?
Thanks in advance.

A:

You had to write:
teacher_of(X,Y):-
    study(X,Z),
    teach(Y,Z).

?- teacher_of(ali, B).
B = hashem
B = mohammad

(you swapped Z and Y in teach/2. To find all teacher, simply use findall/3:
?- findall(B,teacher_of(ali, B),LB)

LB = [hashem, mohammad]

