- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在组装一个简单的元解释器,它输出证明的步骤。我无法将证明步骤作为输出参数。我的谓词 explain1
以我想要的详细形式返回证明,但不是作为输出参数。我的谓词 explain2
将证明作为输出参数返回,但没有达到我想要的详细程度。是否可以修改 explain2
使其产生与 explain1
一样多的信息?我不需要它来输出文本“Explaining...”和“Explanation...”,只是实际的 explanans 和 explanandum。
程序底部的玩具数据(“如果健康和富有,那么快乐”)只是一个示例,其想法是拥有一个包含更多关于其他事物的事实的数据库。我想尝试制作一个接受效果的谓词,例如happy(john)
,并返回一个解释。所以 explain
的 E
参数应该由用户输入;因此,另一个查询可能是 explain(_, smokes(mary), _)
等等。我无法直接从 explain 中的 C
和 E
变量中得到我想要的,因为我希望程序在 Proof 过程中输出步骤,其中 C
和 E
各不相同,例如“有钱又健康,那么快乐;赢了,那么富有;TRUE,那么富有;TRUE,那么快乐”等等。 IE。返回导致结果的所有因果关系。
优秀site Markus Triska 对此有一些详细信息,但我无法根据我的问题调整该代码。
如有任何帮助,我们将不胜感激!
谢谢/JCR
我的程序:
main1:-explain1(_, happy(john), _), fail.
main2:-explain2(_, happy(john), _, T), writeln(T), fail.
explain1(C, E, P):-
C = ['True'],
p(C, E, P),
write('Explaining '), write(E),
write('. An explanation is: '), write(C),
write(' with probability '), write(P), nl.
explain1(C, E, P):-
p(C, E, P),
not(C = ['True']),
write('Explaining '), write(E),
write('. An explanation is: '), write(C),
write(' with probability '), write(P), nl.
explain1(C, E, P):-
p(C0, E, P0),
maplist(explain1, C1, C0, P1),
flatten(C1, C),
append([P0], P1, P2),
flatten(P2, P3),
foldl(multiply, P3, 1, P),
write('Explaining '), write(E),
write('. An explanation is: '), write(C),
write(' with probability '), write(P), nl.
explain2(C, E, P, T):-
C = ['True'],
p(C, E, P),
T = [C, E, P].
explain2(C, E, P, T):-
p(C, E, P),
not(C = ['True']),
T = [C, E, P].
explain2(C, E, P, T):-
p(C0, E, P0),
maplist(explain2, C1, C0, P1, _),
flatten(C1, C),
append([P0], P1, P2),
flatten(P2, P3),
foldl(multiply, P3, 1, P),
T = [C, E, P].
multiply(V1, V2, R) :- R is V1 * V2.
p(['True'], wins(john), 0.7).
p([wins(john)], rich(john), 0.3).
p(['True'], healthy(john), 0.9).
p([rich(john), healthy(john)], happy(john), 0.6).
main1 的输出:
Explaining happy(john). An explanation is: [rich(john), healthy(john)] with probability 0.6
Explaining rich(john). An explanation is: [wins(john)] with probability 0.3
Explaining healthy(john). An explanation is: [True] with probability 0.9
Explaining happy(john). An explanation is: [wins(john), True] with probability 0.162
Explaining wins(john). An explanation is: [True] with probability 0.7
Explaining rich(john). An explanation is: [True] with probability 0.21
Explaining healthy(john). An explanation is: [True] with probability 0.9
Explaining happy(john). An explanation is: [True, True] with probability 0.1134
main2 的输出:
[[rich(john), healthy(john)], happy(john), 0.6]
[[wins(john), True], happy(john), 0.162]
[[True, True], happy(john), 0.1134]
最佳答案
我不清楚这个元解释器的概率部分,但实际上我认为这对你的问题来说是偶然的,所以我将尝试勾勒出我将如何处理这个问题。
您可以将call/1
视为Prolog 的原型(prototype)解释器,因为它只是证明一个目标。所以看起来你想要的 API 是类似 prove(+Goal, -Proof)
的东西,其中 Goal 像 call/1
一样得到证明,但你得到第二件事,某种证明。
当普通 Prolog 看到类似 Goal1, Goal2
的表达式时,您可以认为它扩展为 call(Goal1), call(Goal2)
。那么在这种情况下,您的证明返回元解释器会做什么呢?它应该证明这两个目标,然后以某种方式组合这些“子证明”。
所有这些向我表明,您的概念中缺少的东西是,证明的结构是什么?我会仔细考虑你要返回什么样的东西,因为如果你不想要一个字符串,你会想要一些你可以更容易遍历的东西。它最终可能会具有类似于 Prolog 所做的树结构(除了没有失败分支)。因此,我希望它具有某种嵌套,并且它肯定可以以某种方式“类似于”调用堆栈,尽管我希望这会限制它对您的实用性(您将如何有效地遍历该树以进行通用查询?)。
让我们考虑一下您的基本情况。大概是这样的:
prove(true, true) :- !.
True 本质上是真的,因为它是真的。
我感兴趣的下一个案例是“和”。
prove((G1, G2), (P1, P2)) :-
!,
prove(G1, P1),
prove(G2, P2).
这看起来相当重复,但真正的关键思想是我们将 G1 和 G2 的证明与证明中的 (P1, P2)
结合起来。
下一个案例可能是“或”:
prove((G1;_), P1) :- prove(G1, P1).
prove((_;G2), P2) :- !, prove(G2, P2).
这是我们丢失失败分支的部分。如果第一个分支成功,它的证明将出现在结果中;如果第二个分支成功,它的证明将出现在结果中。但它们永远不会两者出现在结果中。
最后,我们必须根据 a question I asked some time ago 处理内置函数和用户谓词:
prove(H, subproof(H, Subproof)) :- clause(H, Body), prove(Body, Subproof).
prove(H, builtin(H)) :- call(H).
至此,我们有了一个生成非常简单证明的元解释器。我将添加一些子句,然后使用我们的元解释器进行尝试:
mortal(X) :- man(X).
man(socrates).
这是查询:
?- prove((member(X, [bread,socrates]), mortal(X)), Proof).
X = socrates,
Proof = (builtin(member(socrates, [bread, socrates])),
subproof(mortal(socrates),
subproof(man(socrates), true)))
由于我还不明白的原因,member/2
的使用会在第二次查询时爆炸。我有 opened a question about that on the SWI forum并会在我发现那里发生的事情时更新此答案。
更新。该问题与 library(lists)
的自动加载有关,这在您使用 member/2
时发生。在第一次调用时,member/2
没有子句,所以它进入 call/1
,调用自动加载器,然后将其作为内置调用。在随后的尝试中,member/2
有子句,但它们的主体涉及 lists
模块中的谓词,并且此元解释器无法正确处理模块。一个快速而肮脏的解决方案是将第三个子句更改为:
prove(H, subproof(H, Subproof)) :-
\+ predicate_property(H, imported_from(_)),
clause(H, Body),
prove(Body, Subproof).
希望对您有所帮助!
关于prolog - Prolog 元解释器中作为输出参数的证明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55483479/
我在一个项目中工作,该项目需要 SQL 结果的最佳性能,并且希望优化查询,但经过反复试验后,我在 IN 方面遇到了一些问题。 -- THIS RETURNS NO RESULTS AT ALL. SE
在尝试创建一个实际上非常简单的 SQL 语句时,我发现自己迷失了方向。 我有一个包含 3 个表的数据库: 食谱 - 存储一些用于 cooking 的食谱名称 配料食谱 - 将配料与食谱链接 成分 -
我正在尝试理解 PHP 中的 Hebrev 函数。 https://php.net/manual/en/function.hebrevc.php 它说:“将逻辑希伯来语文本转换为视觉文本”。但我不明白
嗨,我在 Grid view 的 android 文档中发现了一段代码对于以下代码。 gridview.setOnItemClickListener(new OnItemClickListener()
谁能解释一下 InfiniBand 是什么?与以太网相比的主要区别是什么,这些差异如何使其比以太网更快? 在官方description从 mellanox 写到 Introduce InfiniBan
这个问题已经有答案了: How are java increment statements evaluated in complex expressions (1 个回答) 已关闭 8 年前。 我知道
我正在阅读 MySQL 教程,我遇到了这个: SELECT /*! SQL_NO_CACHE */ user FROM users; 为什么优化提示 SQL_NO_CACHE 包含在: /*!
我无法理解$(this),我做了一个剪刀石头布的版本,并应用了 jQuery 让用户在计算机上选择按钮选项。我希望有人能解释一下 $(this) 指的是什么,它是 btn-primary 吗?该函数在
我不是很确定 while(choice == 1 || choice ==2);谁能解释一下。我明白这一点 if(choice ==1) displayMonthly(rainfall); e
let flyRight = CABasicAnimation(keyPath: "position.x") flyRight.toValue = view.bounds.size.width/2 f
目录 解释:int型默认值为0 但我们尝试发现并不能通过: 原因: int的默认值为0,而Integer的默认值为null
我正在处理一个查询,自从一个 SSRS 服务器传输到另一个服务器后,它似乎没有按预期执行,并且 where 语句的一部分中出现了以下行 找出不同之处,或者至少从我能找到的地方来看。 where COA
我正在制作一个退回检测程序,读取退回邮件。我们的设置是发送电子邮件,在发送的邮件中添加一个 noreply@domain.tl。一些收件人不再存在,因此我们想要读取退回邮件,并检测它发送给谁。我已经崩
我有一个关于公式通过控制点弯曲的问题。 如您所知,HTML Canvas 有 quadraticCurveTo(x1, y1, x2, y2)与 x1 and x2作为控制点。 但是,当您尝试使用它绘
我有一个 Emakefile看起来像: %% -- %% %% -- {'/Users/user/projects/custom_test/trunk/*', [debug_info, {out
我有一个非常简单的问题。这不仅适用于 spray-json,而且我已经阅读了 argonaut 和 circe 的类似声明。所以请赐教。 在 spray-json 中,我遇到了 There is no
我正在为视频添加水印。我试图让水印与视频尺寸成比例。我已经使用 scale2ref 看到了十几个不同的答案,但没有解释实际发生了什么,所以我发现很难知道如何实现/更改配置以适应我的情况。 当前覆盖命令
因为我正在学习语言,所以我在玩 Haskell,我只是发现了一些我不理解的东西,我找不到解释。如果我尝试运行此代码: map (`div` 0) [1,2,3,4] 我得到一个除以 0 的异常,这是预
我正在寻找解决错误对象引用未设置到对象实例的步骤/指南。以及问题发生原因的解释。 我正在寻找更一般的解释,所以如果我收到错误,我应该采取什么步骤来查找问题。我经常看到有人提供特定代码段的帖子,而其他人
我最近想升级我的知识React ,所以我从组件生命周期方法开始。让我好奇的第一件事是这个componentWillReceiveProps .所以,文档说当组件接收新的(不一定是更新的) Prop 时
我是一名优秀的程序员,十分优秀!