【发布时间】:2019-04-02 20:59:10
【问题描述】:
我正在组装一个简单的元解释器,它输出证明的步骤。我无法将证明步骤作为输出参数。我的谓词explain1 以我想要的详细形式返回证明,但不是作为输出参数。我的谓词explain2 将证明作为输出参数返回,但没有达到我想要的详细程度。可以修改explain2 使其产生与explain1 一样多的信息吗?我不需要它来输出文本“Explaining...”和“Explanation...”,只需要实际的 explanans 和 explanandum。
程序底部的玩具数据(“如果健康和富有,那么快乐”)只是一个示例,其想法是建立一个包含更多关于其他事物的事实的数据库。我想尝试制作一个接受效果的谓词,例如happy(john),并返回解释。所以explain 的E 参数应该由用户输入;因此,另一个查询可能是explain(_, smokes(mary), _) 等等。我无法直接从解释中的C 和E 变量中得到我想要的,因为我希望程序输出证明过程中的步骤,其中C 和E 不同,例如“富足健康,如此幸福;赢如此富;真如此富;真如此幸福”等等。 IE。返回导致结果的所有因果关系。
Markus Triska 的出色 site 对此有一些详细信息,但我无法使该代码适应我的问题。
任何帮助将不胜感激!
谢谢/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]
【问题讨论】:
-
我很困惑,在我看来
explain(E, happy(john), P)成功并将E与您想要的解释和P与概率统一起来。我不确定您为什么要在main1/0中丢弃这些值,而它们似乎正是您想要的。 -
@DanielLyons。我编辑了我的帖子以澄清我的意思。如果我使用
explain(E, happy(john), P),我只会得到直接涉及快乐的解释。但我希望它返回导致快乐的所有链接,即富有如此快乐,但也赢得如此富有,等等。也许我遗漏了一些明显的东西(我认为我的大脑过热了,试图理解递归:-)) -
在这几个月里阅读你所有的问题时,我知道你专注于一个特定的问题,但我仍然无法弄清楚到底是什么。虽然这个其他answer 不是这个问题的答案,但它可能是您解决问题的途径。
-
@GuyCoder 感谢您的建议!我试图拼凑一些代表科学理论和理由的东西;例如给出解释,做出预测等等:-)我认为逻辑编程非常棒!顺便感谢您的耐心等待!
-
我不想在 Prolog 标签上这么说,但您可能需要考虑神经网络。具体来说,大约一年前,我读了一篇关于粒子物理学家的论文,他需要检验一个假设但无法进行实验。因此,他们在不同的实验、输入、过程、结果上训练了一个神经网络,它创建了一个可能证明或反驳他们的假设的实验。如果你找到了这篇论文,请告诉我我需要为我正在做的其他事情。
标签: prolog metaprogramming interpreter inference