But why does list_to_set/2 demand 2 sorts?
Because, as the description says, we are checking for same elements *in the same order*.
You see by inspecting the source code:
list_to_set(List, Set) :- must_be(list, List), % preserve the original positions of elements number_list(List, 1, Numbered), % fisrt sort by value in order to remove duplicates sort(1, @=<, Numbered, ONum), remove_dup_keys(ONum, NumSet), % then sort by original position, since we're % comparing the order of the elements as well sort(2, @=<, NumSet, ONumSet), pairs_keys(ONumSet, Set).