如何调用目标并将逻辑推理的数量绑定到 Prolog 中的变量?
How to call a goal and bind the number of logical inferences to a variable in Prolog?
根据手册,time(+Goal)
执行 Goal
并打印,除其他外,使用的逻辑推理的数量。
如何将逻辑推理的数量绑定到一个变量?
您可以 'install' 多个系统接口的挂钩,其中 message_hook, just declaring it in module user. Now, filter for Kind=information
and Term=time(NumInferences,_,_,_)
and store it in a global variable。
以下代码特定于 SWI-Prolog。目前,许多其他 Prolog 不允许计算推理的数量,主要是由于许多不同的优化会模糊该数字。
:- meta_predicate(call_inferences(0, -)).
call_inferences(Goal_0, Inferences) :-
statistics(inferences, I0),
Goal_0,
statistics(inferences, I1),
Inferences is I1-I0-1.
用法:
?- call_inferences(true,N).
N = 1.
?- call_inferences(nreverse([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],L), N).
L = [30, 29, 28, 27, 26, 25, 24, 23, 22|...],
N = 496.
根据手册,time(+Goal)
执行 Goal
并打印,除其他外,使用的逻辑推理的数量。
如何将逻辑推理的数量绑定到一个变量?
您可以 'install' 多个系统接口的挂钩,其中 message_hook, just declaring it in module user. Now, filter for Kind=information
and Term=time(NumInferences,_,_,_)
and store it in a global variable。
以下代码特定于 SWI-Prolog。目前,许多其他 Prolog 不允许计算推理的数量,主要是由于许多不同的优化会模糊该数字。
:- meta_predicate(call_inferences(0, -)).
call_inferences(Goal_0, Inferences) :-
statistics(inferences, I0),
Goal_0,
statistics(inferences, I1),
Inferences is I1-I0-1.
用法:
?- call_inferences(true,N).
N = 1.
?- call_inferences(nreverse([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],L), N).
L = [30, 29, 28, 27, 26, 25, 24, 23, 22|...],
N = 496.