Hi, quick question
main(X) :-
foo(a,X),
foo(b,X).
foo(V,[V]).
foo(V,[V|_]).
foo(V,[_|Rest]) :- foo(V,Rest).
we get X = [a,b] but I’m a little confused how backtracking works in this case given my (mis)understanding of DFS.
Is this not how DFS backtracking should work in this case?
-
X = [a]. -
X = [a], X = [b]. % fail -
X = [a], X = [b|_]. % fail -
X = [a], X = [_|[b]]. % fail -
X = [a], X = [_|[b|_]]. % fail -
X = [a], X = [_|[_|[b]]]. % fail -
X = [a], X = [_|[_|[b|_]]]. % fail -
X = [a], X = [_|[_|[_|[b]]]]. % fail
and so on.
If we’re making a recursive call to foo, which itself has to exhaustively search each foo rule, which includes a recursive call to foo, which itself has to exhaustively search each foo rule …. then how do we ever backtrack to foo(a,X) unifying as X = [a|_]?