首页 文章

整数平方根在SWI-Prolog和YAP中有效,但在GNU-Prolog中没有

提问于
浏览
0

我通过将其添加到swipl,gprolog和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).

这在swipl和yap中按预期工作,但在gprolog中我得到N> 1的以下错误消息:

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

这对我来说很奇怪,因为我的代码中没有一个谓词依赖于 isqrt/0 . 这可能是GNU-Prolog中的一个错误吗?作为解决方法,我该怎么办?


编辑:这正是我在ubuntu上的gprolog中产生这个错误的方法:

$ 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)

1 回答

相关问题