我期望内置长度/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代码库中找不到相关的代码。
发布于 2016-05-07 14:58:46
在init.pl中,length/2在第3230行附近有一个浅而强大的界面。在这里,它称'$skip_list'(Length0, List, Tail)为list C界面的“瑞士刀”。
您可以在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)
...https://stackoverflow.com/questions/37088981
复制相似问题