我通过将以下代码添加到 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)