I’m using: Swish
I was showing someone interesting features of prolog and started with bidirectional append. Since the standard way with unification in the arguments looks unfamiliar
to people, I made the arguments explicit, and created an infinite loop. Tracing did
not get me anywhere, I can see the lists are getting larger with unbound variables,
but I don’t know Prolog well enough to see why.
Could someone please explain what is happening append2 and append3 below,
and ideally, how I could have discovered it using Swish visualizations?
My initial example and variations are as follows.
% standard append
append1([], R, R).
append1([H | T], R, [H | O]) :- append1(T, R, O).
% infinite loop. Why?
append2([], R, R).
append2(L, R, O) :- L = [H | T], append2(T, R, O1), O = [H | O1].
% similar, but does not infinite loop. Due to binding order??
append3([], R, R).
append3(L, R, O) :- L = [H | T], O = [H | O1], append3(T, R, O1).
% infinite loop happens with append2(X, Y, [1, 2, 3, 4, 5]),