trealla-prolog / trealla

A compact, efficient Prolog interpreter written in plain-old C.
MIT License
252 stars 11 forks source link

Prolog logical variable phantom aliasing #511

Closed Jean-Luc-Picard-2021 closed 3 months ago

Jean-Luc-Picard-2021 commented 3 months ago

Running this Sudoku compiler:

$ ./tpl -v
Trealla Prolog (c) Infradig 2020-2023, v2.39.3
$ ./tpl
?- ['problems.p'].
   true.
?- ['sudoku_comp.p'].
   true.

It shows me two Prolog logical variable phantom aliasings. The output has two rows, where the first component of the pair is instantiatiated:

?- problem(1, M), sudoku(M, [], L), order(L, [], R), member(X, R), write(X), nl, fail.
[..]
16-[8,_48,_53,_54,_10,5,_19,_27,_31,_39,_45,_48,_49,5,_50,_51,_52,9,_53,_54]
[...]
20-[5,9,_28,_22,_21,_4,_11,_14,_49,1,_40,_32,_28,_27,_26,_25,4,_24,_23,_22,_21]

This doesn't happen when running the code with SWI-Prolog or Scryer Prolog. And shouldn't happen from the logical point of view of the program.

Source code:

problems.p.log sudoku_comp.p.log

Jean-Luc-Picard-2021 commented 3 months ago

Among the problems are a few difficult Sudokus. SWI-Prolog is quite speedy:

/* SWI-Prolog, 9.3.0 */
?- problem(K, M), sudoku(M, [], L), write(K), write(': '),
    catch(call_with_time_limit(30, time(once(label(L)))), _, true), fail.
0: % 2,027,477 inferences, 0.125 CPU in 0.125 seconds (100% CPU, 16219816 Lips)
1: % 415,010 inferences, 0.031 CPU in 0.026 seconds (120% CPU, 13280320 Lips)
2: % 496,747 inferences, 0.031 CPU in 0.031 seconds (100% CPU, 15895904 Lips)
3: % 22,855,386 inferences, 1.438 CPU in 1.437 seconds (100% CPU, 15899399 Lips)
4: % 510,445 inferences, 0.031 CPU in 0.032 seconds (97% CPU, 16334240 Lips)
5: % 573,806 inferences, 0.047 CPU in 0.036 seconds (130% CPU, 12241195 Lips)
6: % 1,233,472 inferences, 0.078 CPU in 0.077 seconds (101% CPU, 15788442 Lips)
7: % 2,896,959 inferences, 0.188 CPU in 0.180 seconds (104% CPU, 15450448 Lips)
8: % 22,554,967 inferences, 1.438 CPU in 1.426 seconds (101% CPU, 15690412 Lips)
false.

Scryer Prolog can do it as well, but doesn't show the same speed:

/* Scryer Prolog, 0.9.4 */
?- problem(K, M), sudoku(M, [], L), write(K), write(': '),
     time(once(label(L))), fail.
0:    % CPU time: 0.389s, 3_849_360 inferences
1:    % CPU time: 0.085s, 833_653 inferences
2:    % CPU time: 0.098s, 969_108 inferences
3:    % CPU time: 6.101s, 65_972_570 inferences
4:    % CPU time: 0.095s, 947_345 inferences
5:    % CPU time: 0.104s, 1_034_263 inferences
6:    % CPU time: 0.267s, 2_781_348 inferences
7:    % CPU time: 0.885s, 9_481_421 inferences
8:    % CPU time: 11.370s, 123_245_732 inferences
   false.
Jean-Luc-Picard-2021 commented 3 months ago

Ok, I see the bug is already fixed. After git pull and make the bug is gone!

/* Trealla Prolog, 2.47.3 */
?- problem(K, M), sudoku(M, [], L), write(K), write(': '), time(once(label(L))), fail.
0: % Time elapsed 0.758s, 5896677 Inferences, 7.781 MLips
1: % Time elapsed 0.146s, 1170172 Inferences, 8.039 MLips
2: % Time elapsed 0.175s, 1404339 Inferences, 8.043 MLips
3: % Time elapsed 7.985s, 63548556 Inferences, 7.959 MLips
4: % Time elapsed 0.180s, 1440365 Inferences, 8.005 MLips
5: % Time elapsed 0.202s, 1622466 Inferences, 8.043 MLips
6: % Time elapsed 0.425s, 3469804 Inferences, 8.156 MLips
7: % Time elapsed 1.000s, 8113473 Inferences, 8.112 MLips
8: % Time elapsed 7.614s, 62905244 Inferences, 8.261 MLips
   false.

Closing this ticket.