Prolog (swi-pl) 中 length/2 的逻辑推理数
Number of logical inferences of length/2 in Prolog (swi-pl)
我希望内置的 length/2 谓词在逻辑推理的数量上是线性的。然而,它似乎是恒定的:
?- length(L,10),time(length(L,X)).
% 2 inferences, 0.000 CPU in 0.000 seconds (63% CPU, 142857 Lips)
?- length(L,20),time(length(L,X)).
% 2 inferences, 0.000 CPU in 0.000 seconds (62% CPU, 153846 Lips)
?- length(L,30),time(length(L,X)).
% 2 inferences, 0.000 CPU in 0.000 seconds (65% CPU, 111111 Lips)
难道是程序委托给了C?我在 SWIPL 代码库中找不到相关代码。
length/2 在 init.pl 的第 3230 行附近有一个浅显但功能强大的界面。从那里它调用 '$skip_list'(Length0, List, Tail)
,列表 C 接口的 'swiss knife'。
您可以在 src/pl-prims.c,第 2377 行及以下内容中找到它:
/** '$skip_list'(-Length, +Xs0, -Xs) is det.
Xs0, Xs is a pair of list differences. Xs0 is the input list and Xs is
the minimal remaining list. Examination of Xs permits to classify the
list Xs0:
Xs | list type of Xs0 | Length
[] ... | well formed | length
Var ... | partial | elements skipped
[_|_] ... | infinite | upper bound for cycle
Term ... | malformed | elements skipped
*/
PRED_IMPL("$skip_list", 3, skip_list, 0)
...
我希望内置的 length/2 谓词在逻辑推理的数量上是线性的。然而,它似乎是恒定的:
?- length(L,10),time(length(L,X)).
% 2 inferences, 0.000 CPU in 0.000 seconds (63% CPU, 142857 Lips)
?- length(L,20),time(length(L,X)).
% 2 inferences, 0.000 CPU in 0.000 seconds (62% CPU, 153846 Lips)
?- length(L,30),time(length(L,X)).
% 2 inferences, 0.000 CPU in 0.000 seconds (65% CPU, 111111 Lips)
难道是程序委托给了C?我在 SWIPL 代码库中找不到相关代码。
length/2 在 init.pl 的第 3230 行附近有一个浅显但功能强大的界面。从那里它调用 '$skip_list'(Length0, List, Tail)
,列表 C 接口的 'swiss knife'。
您可以在 src/pl-prims.c,第 2377 行及以下内容中找到它:
/** '$skip_list'(-Length, +Xs0, -Xs) is det.
Xs0, Xs is a pair of list differences. Xs0 is the input list and Xs is
the minimal remaining list. Examination of Xs permits to classify the
list Xs0:
Xs | list type of Xs0 | Length
[] ... | well formed | length
Var ... | partial | elements skipped
[_|_] ... | infinite | upper bound for cycle
Term ... | malformed | elements skipped
*/
PRED_IMPL("$skip_list", 3, skip_list, 0)
...