$=P

The {\em bagof} predicate

\prologindex{{\it bagof}} Many programs in Prolog rely on backtracking to get hold of the successive bindings that may arise from evaluating a goal. For instance, when evaluating the goal ?- member(X,[1,2,3]), member(X,[2,3,4]). the variable X is first bound to 1 and only when the second goal fails the first goal will backtrack to the next solution and bind X to 2. Most Prolog systems however provide a setof or bagof predicate that allows to compute a set or a bag of solutions.\ftn{ A bag differs from a set in that it may contain multiple identical elements. In Prolog bags and sets are represented by lists. } For instance, to compute all the permutations of a given list L = [1,2,3] we may state the goal .ds bag.pl which results in a list R that contains all the permutations of L. Naturally, the bagof predicate may only be used when there are finitely many solutions, and for that matter not too many.