Foreach/2 query doesn't bind results to variables

Hello, everyone. This is my first post in this fantastic community.

I have the following database:


student(cristian, spanish).
student(maria, english).
student(felipe, italian).
student(juan, spanish).

studies(cristian, english).
studies(cristian, portuguese).
studies(maria, hebrew).
studies(juan, english).
studies(wendy, hebrew). 

I would like to know if every student whose is a native speaker of X studies the language Y.

I would expect to bind results to variables in the query, something like:

?- foreach(student(S, X), studies(S, Y)).

I would appreciate any insights. Perhaps, I am not using the correct predicate.

If a “forall” query fails, then try to find a counter-example:

?- student(S, X), \+ studies(S, Y).
S = felipe,
X = italian ;
false.
1 Like

I am not sure if this is for what you are asking, but I feel something related.

% ?-	findall(C, student(_, C), Cs), sort(Cs, Cs0),
%		findall(L, studies(_, L), Ls), sort(Ls, Ls0),
%		findall(X-Y, (	member(X, Cs0),
%					member(Y, Ls0),
%					forall(student(S, X), studies(S, Y))),
%			Ans),
%		sort(Ans, Ans0), 
%		maplist(writeln, Ans0).
%@ english-hebrew
%@ spanish-english
%@ Cs = [spanish, english, italian, spanish],
%@ Cs0 = [english, italian, spanish],
%@ Ls = [english, portuguese, hebrew, english, hebrew],
%@ Ls0 = [english, hebrew, portuguese],
%@ Ans = Ans0, Ans0 = [english-hebrew, spanish-english].
1 Like

Thank you, everyone.

This is the final rule:

desiredLanguage(X, Y) :- forall(student(S, X), studies(S, Y)).

So, I would like to query:

?- desiredLanguage(X, Y)

and get the possible combinations that make that predicate true. Of course, it’s correct but forall predicate doesn’t bind results to variables. Do I have any alternatives ?

Thank you. How can I test it ?

I was able to run it.