There are several possibilities to make the goal swap(List, List1)
fail. Either List
is a list of length 0 or 1 ; or it does not contain two immediately succeeding elements where the second is smaller than the first.
The cut is placed in such a manner that it both cuts swap/2
and the alternative of bubblesort/2
.
This is a good example, where a "deep cut" (cutting deep into swap/2
) still works somewhat nicely. However, such situations are very rare. Most of the time, the cut cuts too much. The largest majority of such programs is very brittle to use, even more so, if the second argument is given already. They are often not steadfast.
Ah, I almost missed it: Even in this program, we have bubblesort(nonlist,L)
succeeding, or bubblesort([1|nonlist],L)
which probably is not intended and leads to subtle programming errors.
There is also another reason why this program does not present the ideal logic programming style: The second rule of bubblesort/2
when read alone says: Everything is a sorted list`. To understand this, we have to read both rules at the same time and narrow it down to Everything but ....
In English, does this mean that bubble-sort needs to continue doing swaps until no more swaps are possible, but then needs to terminate? Or does it mean that every-time a successful swap has been done, there's no use backtracking over that success?
It is the first procedural meaning that applies here. And certainly, backtracking over the success to the second clause of bubblesort/2
would be an error.
A further quite unintuitive detail which is not specific to the cut, is that in addition to numbers, the program also succeeds for expressions like bubblesort([1,1+1],L)
which again might lead to subtle differences.