number_of_modules(N) :-
findall(M, studies(_,M), Ms),
sort(Ms, SortedMs),
length(SortedMs, N).
?- number_of_modules(N).
N = 3.
sort/2
removes duplicate elements.
The arguments of findall/3
are, from left to right, (1) a template for the answer which is to be collected, (2) the goal from which the answer is drawn, (3) the a list of all answers. So you could, for instance, label each module as such by using a different template in (1):
number_of_modules(N, SortedMs) :-
findall(module(M), studies(_,M), Ms),
sort(Ms, SortedMs),
length(SortedMs, N).
?- number_of_modules(N, Ms).
N = 3,
Ms = [module(da), module(plc), module(se)].
Documentation on this and related predicates can be found in section 4.3 of the manual.