如何通过 shell 调用在 SWI-Prolog 中显示具有多个自由变量的模式匹配目标的结果? [英] How do I show the results of pattern-matching goals with several free variables in SWI-Prolog from a shell invocation?
问题描述
让我们使用以下序言基础:
父亲(阿纳金天行者,莱娅公主).父亲(阿纳金天行者,卢克天行者).说哦不(卢克天行者).
sdesciencelover 问如何在 swi-prolog 中从 shell 调用中显示模式匹配目标的结果,并得到一个答案,对查询进行手动转换,以发出写入.
swipl -q -s kb.pl -t "father(anakinSkywalker,X), writeln(X), false"
结果:
莱娅公主卢克·天行者
当一个人只有几个查询和一个自由变量时,这很好用,但是手动转换每个查询变得乏味,如果我们想要正确输出每个变量的名称及其结果,很快就会变得非常烦人.例如运行查询father(AVariable,Another)
,需要这样写:
swipl -q -s kb.pl -t "father(AVariable,Another), write('AVariable='), write(AVariable), write(', Another='), writeln(Another), 错误的"
结果:
AVariable=anakinSkywalker,Another=莱娅公主AVariable=anakinSkywalker,另一个=lukeSkywalker
我尝试通过管道向它提供命令,但效果不佳(我无法检测到它何时完成写入结果,因此它之后会挂起,并且没有换行符分隔答案):
(echo "father(X,Y)."; while true; do echo ";"; done) |swipl -q -s kb.pl
结果:
X = 阿纳金天行者,Y = 公主莱娅 X = 阿纳金天行者,Y = 卢克天行者.swipl 挂在这里,需要用 Control-C 停止.
我知道我可以使用 sed
脚本来预处理查询,添加必要的代码以大写字母打印变量,但是处理复杂查询需要大量的工作,例如必须满足两个谓词的地方:
father(X,Y),说OhNo(Y).
为了总是给出正确的结果,我们需要为 prolog 编写一个解析器,这将是无用的工作,因为 prolog 已经知道如何交互地做到这一点.
所以这是我的问题:有没有办法告诉 GNU prolog 或 SWI prolog(或任何其他可以轻松安装在 linux 上的免费版本)运行一些查询并打印结果,只是就像他们以交互方式做的那样,但不需要我手动输入(或复制粘贴)每个查询?
一种将一系列查询存储在文件中(在 kb.pl
文件或辅助文件中)并运行它们的方法,显示它们结果会更好.
您可以在 GNU 中使用命令行选项 --query-goal
.像这样:
Let's use the following prolog base :
father(anakinSkywalker, princessLeia).
father(anakinSkywalker, lukeSkywalker).
saysOhNo(lukeSkywalker).
sdesciencelover asked how to show the results of pattern-matching goals in swi-prolog from a shell invocation, and got an answer giving a manual transformation on the query, to isue a write.
swipl -q -s kb.pl -t "father(anakinSkywalker,X), writeln(X), false"
Result:
princessLeia
lukeSkywalker
This works fine when one only has a few queries with a single free variable, but manually transforming each one becomes tedious, and if we want proper output with the name of each variable along with its result, it soon becomes very annoying. For example to run the query father(AVariable, Another)
, one needs to write:
swipl -q -s kb.pl -t "father(AVariable,Another), write('AVariable='), write(AVariable), write(', Another='), writeln(Another), false"
Result:
AVariable=anakinSkywalker, Another=princessLeia
AVariable=anakinSkywalker, Another=lukeSkywalker
I tried to feed it the commands from a pipe, but it doesn't work great (I can't detect when it has finished writing the results, so it just hangs afterwards, and no newline separates the answers) :
(echo "father(X,Y)."; while true; do echo ";"; done) | swipl -q -s kb.pl
Result :
X = anakinSkywalker,
Y = princessLeia X = anakinSkywalker,
Y = lukeSkywalker.
swipl hangs here, and needs to be stopped with Control-C.
I know I could use a sed
script to pre-process queries, adding the necessary code to print the variables in capital letters, but it would need a fair amount of work to work on complex queries, for example where two predicates must be satisfied :
father(X,Y), saysOhNo(Y).
To always give correct results, one would need to write a parser for prolog, which would be useless work since prolog already know how to do this interactively.
So here's my question : is there a way to tell GNU prolog or SWI prolog (or any other free version that can be easily installed on linux) to run some queries and print the results, just like they would do interactively, but without requiring me to type (or copy-paste) each query by hand ?
Edit : a way to store a series of queries in a file (either in the kb.pl
file or an auxiliary file) and run them all, showing their results would be even better.
You can use the command-line option --query-goal
in GNU. Like so:
$ echo a| gprolog --query-goal 'X = 1 ; X =2' GNU Prolog 1.4.1 By Daniel Diaz Copyright (C) 1999-2012 Daniel Diaz | ?- X = 1 ; X =2. X = 1 ? a X = 2 yes
这篇关于如何通过 shell 调用在 SWI-Prolog 中显示具有多个自由变量的模式匹配目标的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!