在 SWI Prolog 中定义长度时出错
Error when defining length in SWI Prolog
我在名为 test.pl
:
的文件中定义了一个名为 length
的过程
% Finds the length of a list.
length([], 0).
length([_ | Tail], N) :-
length(Tail, N1),
N is 1 + N1.
当程序运行使用SWI-Prolog(prolog test.pl
)时,出现如下错误:
ERROR: /home/user/test.pl:2:
No permission to modify static procedure `length/2'
Defined at /usr/lib/swi-prolog/boot/init.pl:3496
ERROR: /home/user/test.pl:3:
No permission to modify static procedure `length/2'
Defined at /usr/lib/swi-prolog/boot/init.pl:3496
我尝试将过程名称从 length
更改为 mylength
,错误消失了。这个错误是什么意思?我可以定义一个名为 length
的过程吗?如果没有,为什么不能做到?
没错。 length/2
is a built-in predicate: length(?List, ?Int) is True if Int 表示元素个数在 列表 中。所以这个名字已经被使用了。
length/2 未在 Prolog 中定义,但它是本机(高效)列表实现的浅层接口的一部分。您应该使用指令 redefine_system_predicate.
比如保存在文件中redef_length.pl
:- redefine_system_predicate(length(?,?)).
% Finds the length of a list.
length([], 0).
length([_ | Tail], N) :-
length(Tail, N1),
N is 1 + N1.
再咨询一下
?- [test/prolog/redef_length].
true.
?- trace.
true.
[trace] ?- length(A,B).
Call: (8) length(_1476, _1478) ? creep
Exit: (8) length([], 0) ? creep
A = [],
B = 0 ;
Redo: (8) length(_1476, _1478) ? creep
Call: (9) length(_1718, _1738) ? creep
Exit: (9) length([], 0) ? creep
Call: (9) _1478 is 1+0 ? creep
Exit: (9) 1 is 1+0 ? creep
Exit: (8) length([_1716], 1) ? creep
A = [_1716],
B = 1
我在名为 test.pl
:
length
的过程
% Finds the length of a list.
length([], 0).
length([_ | Tail], N) :-
length(Tail, N1),
N is 1 + N1.
当程序运行使用SWI-Prolog(prolog test.pl
)时,出现如下错误:
ERROR: /home/user/test.pl:2:
No permission to modify static procedure `length/2'
Defined at /usr/lib/swi-prolog/boot/init.pl:3496
ERROR: /home/user/test.pl:3:
No permission to modify static procedure `length/2'
Defined at /usr/lib/swi-prolog/boot/init.pl:3496
我尝试将过程名称从 length
更改为 mylength
,错误消失了。这个错误是什么意思?我可以定义一个名为 length
的过程吗?如果没有,为什么不能做到?
没错。 length/2
is a built-in predicate: length(?List, ?Int) is True if Int 表示元素个数在 列表 中。所以这个名字已经被使用了。
length/2 未在 Prolog 中定义,但它是本机(高效)列表实现的浅层接口的一部分。您应该使用指令 redefine_system_predicate.
比如保存在文件中redef_length.pl
:- redefine_system_predicate(length(?,?)).
% Finds the length of a list.
length([], 0).
length([_ | Tail], N) :-
length(Tail, N1),
N is 1 + N1.
再咨询一下
?- [test/prolog/redef_length].
true.
?- trace.
true.
[trace] ?- length(A,B).
Call: (8) length(_1476, _1478) ? creep
Exit: (8) length([], 0) ? creep
A = [],
B = 0 ;
Redo: (8) length(_1476, _1478) ? creep
Call: (9) length(_1718, _1738) ? creep
Exit: (9) length([], 0) ? creep
Call: (9) _1478 is 1+0 ? creep
Exit: (9) 1 is 1+0 ? creep
Exit: (8) length([_1716], 1) ? creep
A = [_1716],
B = 1