So why does equal([1,2,3], X)
not terminate universally with your code?
Let's look at a failure-slice of your code! What are failure slices? Here's the tag info:
A failure-slice is a fragment of a Prolog program obtained by adding some goals false
. Failure-slices help to localize reasons for universal non-termination of a pure monotonic Prolog program. They also help to give a lower bound for the number of inferences needed. It is a concrete program-slicing technique.
To create a failure slice:
- we insert
false
goals into the program
- while making sure that the fragment does not terminate with above goal.
del(_, [], []) :- false.
del(X, [X|T], T) :- false.
del(X, [H|T], [H|T1]) :- false,
dif(X, H), % note that the OP originally used `X \= H`
del(X, T, T1).
member(X, [X|_]).
member(X, [_|T]) :-
member(X, T).
equal([], []) :- false.
equal([X], [X]) :- false.
equal([H1|T], L2) :-
member(H1, L2), false,
del(H1, L2, L3),
equal(T, L3).
?- equal([1,2,3], _), false. % note that `false` is redundant ...
** LOOPS ** % ... as above `equal/2` cannot succeed.
So... what does above failure slice tell us? It says:
- To make the goal
equal([1,2,3], X)
terminate universally ...
- ... we must change at least one of the remaining parts (the ones not
striked-through)!