gpt4 book ai didi

prolog - 在跟踪元解释器序言中实现剪切

转载 作者:行者123 更新时间:2023-12-04 07:53:08 24 4
gpt4 key购买 nike

我有这个跟踪元解释器,它是从先前的问题Prolog unbind bound variable更改而来的。

我不懂如何解释切工。感谢用户@false谁告诉我该cut实现不好,我的问题是,我应该如何在这个meta解释器中实现cut?

%tracer
mi_trace(Goal):-
mi_trace(Goal, 0).

mi_trace(V, _):-
var(V), !, throw(error(instantiation_error, _)).
mi_trace(true, _Depth):-!, true.
mi_trace(fail, _Depth):-!, fail.
mi_trace(A > B, _Depth):-!, A > B.
mi_trace(A < B, _Depth):-!, A < B.
mi_trace(A =< B, _Depth):-!, A =< B.
mi_trace(A >= B, _Depth):-!, A >= B.
mi_trace(A = B, _Depth):-!, A = B.
mi_trace(A is B, _Depth):-!, A is B.
mi_trace(\+A, _Depth):-!, \+mi_trace(A, _Depth).
mi_trace(!, _Depth):-!, fail. % <- this is wrong
mi_trace((Goal1, Goal2), Depth):-
!,
mi_trace(Goal1, Depth),
mi_trace(Goal2, Depth).
mi_trace(Goal, Depth):-
display('Call: ', Goal, Depth),
redo_clause(Depth, Goal, Body),
Depth1 is Depth + 1,
mi_trace(Body, Depth1),
display('Exit: ', Goal, Depth).
mi_trace(Goal, Depth):-
display('Fail: ', Goal, Depth),
fail.

redo_clause(Depth, Goal, Body) :-
findall(Goal-Body, clause(Goal, Body), [First|Rest]),
( Goal-Body = First
; length(Rest, RL), length(RestC, RL),
member(Goal-Body,RestC),
display('Redo: ', Goal, Depth),
Rest = RestC
).

display(Message, Goal, Depth):-
tab(Depth), write(Depth), write(': '), write(Message),
write(Goal), nl.

trace_query(In, Out, Query):-
consult(In),
tell(Out),
call_with_depth_limit(findall(Query, mi_trace(Query), _Solutions), 30, _XMessage),
writeln(_XMessage),
writeln(_Solutions),
told,
unload_file(In),
true.

最佳答案

让我从一个简单的实现开始,该实现对许多程序有效,但不适用于所有程序。

使用catch/3throw/1
这种方法绝对是在ISO Prolog中实现剪切的最简单方法。但是,它不是非常有效。基本思想是cut只会成功,只有在回溯时,cut才会失败,直到最后一次调用mi_call/1为止。请注意,只有mi_call/1构造才能捕获这些削减。因此,所有用户定义的目标都必须使用mi_call/1进行包装。像setof/3这样的内置函数也是如此。

一个简单的实现是:

mi_cut.
mi_cut :- throw(cut).

mi_call(Goal) :-
catch(Goal, cut, fail).

在您的元解释器中,将两个规则交换为:
mi_trace(!, _Depth):-!, ( true ; throw(cut) ).
...
mi_trace(Goal, Depth):-
display('Call: ', Goal, Depth),
Depth1 is Depth + 1,
catch(
( redo_clause(Depth, Goal, Body),
mi_trace(Body, Depth1)
),
cut,
fail),
display('Exit: ', Goal, Depth).

这几乎适用于所有程序。除了那些, throw(cut)自己。还是想捕捉所有异常。正是这些微小的小事情使一般的实现变得更加复杂。

在您的跟踪器中,您暂时尚未实现 call/1catch/3throw/1,因此这些问题将不会显示-您仅会得到一个错误。
(也许TBC)

关于prolog - 在跟踪元解释器序言中实现剪切,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27235148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com