SWI-Prolog / swipl-devel

SWI-Prolog Main development repository
http://www.swi-prolog.org
Other
951 stars 171 forks source link

library(yall) failing with running foldl backwards? #948

Closed K9-guardian closed 2 years ago

K9-guardian commented 2 years ago

Version: 8.2.4

OS: Windows 10

I made this toy program to show that map could be implemented with foldl by setting the output to be the empty list.

folder(G_2, X, [Y|Ys], Ys) :- call(G_2, X, Y).

map(G_2, Xs, Ys) :- foldl(folder(G_2), Xs, Ys, []).

This works fine, but when I try to do the same with a lambda, it does not work on the initial repl. This is my file map.pl.

map(G_2, Xs, Ys) :- foldl({G_2}/[X, [Y|Ys], Ys]>>call(G_2, X, Y), Xs, Ys, []).

This is what I do from my terminal (powershell).

PS C:\Users\K9guardian\Documents\Code\advent-of-code\2016> swipl .\map.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.2.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

1 ?- map(succ, [1,2,3], Xs).
false.

Note that this works after a successful make/1 call. That is, when I do some edit to change the timestamp such as adding a space, and then makeing, and running this test again, it works.

PS C:\Users\K9guardian\Documents\Code\advent-of-code\2016> swipl .\map.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.2.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

1 ?- map(succ, [1,2,3], Xs).
false.

2 ?- make.
% c:/users/k9guardian/documents/code/advent-of-code/2016/map compiled 0.00 sec, 1 clauses
true.

3 ?- map(succ, [1,2,3], Xs).
Xs = [2, 3, 4].

The only change to map.pl was adding a space at the very end of the file.

map(G_2, Xs, Ys) :- foldl({G_2}/[X, [Y|Ys], Ys]>>call(G_2, X, Y), Xs, Ys, []). 
JanWielemaker commented 2 years ago

Unfortunately, library(yall) has slightly different semantics depending on whether it is compiled or meta-called. That seems hard to fix according to its author. The advertised semantics is that of the compiled version and therefore one must always make sure that the libraries containing the used meta-predicates and library(yall) itself are explicitly loaded for getting the correct result (as well as for performance as meta-called yall is pretty slow).