example([(balaur, 1), (dwarf, 13), (hobbit, 1), (wizard, 1), (foods, 5)]). find(X, MSet, Count) :- member((X, Count), MSet). cardinal(MSet, Card) :- findall(C, member((_, C), MSet), Counts), sum_list(Counts, Card). add(X, MSet, [(X, 1) | MSet]) :- \+ member((X, _), MSet). add(X, MSet, [(X, NC) | Rest]) :- member((X, C), MSet), NC is C + 1, findall((A, B), (member((A, B), MSet), A \= X), Rest). intersect(MS1, MS2, MS) :- findall((X, Count), ( member((X, C1), MS1), member((X, C2), MS2), Count is min(C1, C2) ), MS).