Prolog: reverse ([], A) vs reverse (A, [])

I can not say anything: if I give Prolog reverse([], A)., it works fine, if I give it reverse(A, []).and answer it ;on the first assumption that it freezes!

Why? (The same result for both GNU Prolog and SICStus Prolog!)

aioobe@r60:~$ prolog
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?- reverse([], A).

A = []

yes
| ?- reverse(A, []).

A = [] ? ;

Fatal Error: global Qaru (size: 32768 Kb,
environment variable used: GLOBALSZ)

aioobe@r60:~$
+3
source share
2 answers

Looks like over optimization for the inline predicate for me. The same problem occurs regardless of the contents of the list in the second argument. Based on the GProlog manual, this is a mistake. Please note that the pattern forreverse -

reverse(?list, ?list)

And further, which ?means that an argument can be created or a variable.

SWI-Prolog version 5.6.64 gives the expected result.

?- reverse([], A).
A = [].

?- reverse(A, []).
A = [] ;
false.
+4

, , 1 (.. (+,)), ), , . :

reverse(L, R) :- reverse_1(L, [], R).

reverse_1([], X, X).      % <-- doesn't loop on unbound arg #1 if this clause cuts
reverse_1([A|As], X, R) :-
   reverse_1(As, [A|X], R).
0

Source: https://habr.com/ru/post/1764157/


All Articles