Search code examples
prologgnu-prolog

Integer square root works in SWI-Prolog and YAP, but but not in GNU-Prolog


I tested the following code by adding it to the user file in swipl, gprolog, and yap:

isqrt(N, _) :-
    N < 0, !, fail. 
isqrt(N, N) :-
    N < 2.
isqrt(N, R) :-
    X is N,
    Y is (N // 2),
    isqrt(N, X, Y, R).

isqrt(_, X, Y, X) :- 
    Y >= X.
isqrt(N, _, Y, R) :-
    Z is ((Y + N // Y) // 2),
    isqrt(N, Y, Z, R).

This works as expected in swipl and yap, but in gprolog I get the following error message for N > 1:

uncaught exception: error(existence_error(procedure,isqrt/0),isqrt/0)

This is strange to me because none of the predicates in my code rely on isqrt/0. Could this be a bug in GNU-Prolog? What can I do to as a workaround?


Edit: Here's exactly what I do to produce this error in gprolog on ubuntu:

$ gprolog
GNU Prolog 1.4.5 (64 bits)
Compiled Feb  5 2017, 10:30:08 with gcc
By Daniel Diaz
Copyright (C) 1999-2016 Daniel Diaz
| ?- [user].
compiling user for byte code...
isqrt(N, _) :-
    N < 0, !, fail. 

isqrt(N, N) :-
    N < 2.

isqrt(N, R) :-
    X is N,
    Y is (N // 2),
    isqrt(N, X, Y, R).

isqrt(_, X, Y, X) :- 
    Y >= X.

isqrt(N, _, Y, R) :-
    Z is ((Y + N // Y) // 2),
    isqrt(N, Y, Z, R).

user compiled, 17 lines read - 1656 bytes written, 10751 ms

yes
| ?- isqrt(100, X).
uncaught exception: error(existence_error(procedure,isqrt/0),isqrt/0)

Solution

  • There have been some reports, including in the GNU Prolog mailing list, of similar errors under Linux, notably Ubuntu/kubuntu:

    http://lists.gnu.org/archive/html/bug-prolog/2018-09/msg00002.html

    In the report cases, compiling GNU Prolog from the sources solved the problem.