- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个跟踪元解释器,它是从先前的问题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/3
和throw/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/1
,
catch/3
和
throw/1
,因此这些问题将不会显示-您仅会得到一个错误。
关于prolog - 在跟踪元解释器序言中实现剪切,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27235148/
欢迎!这个系列的博文会带你使用golang语言来编写一个你自己的文本编辑器。 首先想说说写这个系列文章的动机。 其实作为校招生加入某头部互联网大厂一转眼已经快4年了。可以说该大厂算是比较早的用g
我想在序言中向数据库添加文本。就像是 adding :- tell('a.txt'), write('abc'), told. 但不会覆盖a.txt。我试过像这样使用append: append('a
我正在开发一款名为“狼山羊白菜”的益智游戏。编程语言是 Prolog。 change(e,w). change(w,e). move([X,X,Goat,Cabbage],wolf,[Y,Y,Goat
我对 Prolog 很陌生。 我有这样的图: edge(a,e). edge(e,f). edge(f,d). edge(d,a). 我将传递闭包定义为: p(X,Y) :- edge(X,Y). t
我想知道如何使用我拥有的元素获得一个漂亮的外观列表!我一直在尝试查找有关此内容的内容,但我发现的实际示例似乎都没有实际工作 这是我在网上找到的一种方法: create_list(Item, List,
我有一个使用约束用Sicstus Prolog编写的程序。 我的目标是使用labeling/2和其他方法来获取变量的随机实例化。 例子: X #> 2, Y #= 2*X, Z #<10 如果我用 L
我想知道,我知道你可以使用 assert添加事实或规则或其他任何内容,如果您已将谓词声明为 -:dynamic ,但这仅允许所做的更改仅保留在该 session 中,例如如果关闭 Prolog 窗口,
关于如何检索列表中重复次数最多的元素的任何想法。 即如下所示, ?- maxRepeated([1,2,7,3,6,1,2,2,3],M). M = 2. 最佳答案 我非常喜欢 Prolog 的关系功
my_reverse(L1,L2) :- my_rev2(L1,L2,[]). my_rev2([],L2,L2). my_rev2([X|Xs],L2,Acc) :- my_rev2(Xs,L2,[
我正在编写一个序言程序,该程序将检查两个数学表达式是否实际上相同。例如,如果我的数学表达式目标是:(a + b) + c,则以下任何表达式都被视为相同: (a+b)+c a+(b+c) (b+a)+c
我必须编写一个计算魔法矩阵的程序,我已经编写了我的代码并且它可以工作,但是我的排列非常慢。我需要一个更快的人可以帮助我请 这是代码: diabolico([A,B,C,D,E,F,G,H,I,J,K,
关于如何检索列表中重复次数最多的元素的任何想法。 即如下所示, ?- maxRepeated([1,2,7,3,6,1,2,2,3],M). M = 2. 最佳答案 我非常喜欢 Prolog 的关系功
我正在关注一个要求定义对称关系的 Prolog 教程。 例如,假设您在知识库中有以下事实: colleague(john,bob). 您还希望 colleague(bob,john) 是真实的,对于所
笔者从大学时期就开始接触的前端,在刚去实习的时候就被导师安排去做内网的一个小富文本工具。之后从毕业后干的第一份工作游戏客户端,到现在做着可视化相关的前端工作,都有在做富文本相关的内容。可以说是和 富文
我有一个对列表,想找到具有相应第一个值的元素。 ?- findconn1(9, [(9,[23,33]),(42,[21,322])], R). 所以在这种情况下,我希望结果是 (9,[23,23])
我试图让我的 ARM 围绕一些基本的序言,但在这个过程中有点挣扎。具体来说 - 我试图通过一个项目列表并将其逐项复制到一个新列表中。我可以让它反转,但我发现在不反转的情况下这样做会更棘手。 我一直在尝
当我的 friend 在学校开始学习 Prolog 时,我取笑他学习了无用的语言。然而,他向我展示了一些我什至不知道可能的东西;我想知道这个技术是从哪里来的。 技术是这样的: permutation(
我有下一条规则 % Signature: natural_number(N)/1 % Purpose: N is a natural number. natural_number(0). natura
我在理解为什么我在 prolog 中的代码根据我放置规则的顺序做某事时遇到了一些麻烦。 这是我的数据库: parent(tom, bob). parent(tom, liz). parent(mary
我正在尝试学习一些关于 swi-prolog 的知识(除了基本的、无用的程序)。 谁能解释(也许用伪代码)这个数独求解器和相关函数在做什么?如果您需要更多引用,可以在 swi-prolog 的 CLP
我是一名优秀的程序员,十分优秀!