How do I show the results of pattern-matching goals with several free variables in SWI-Prolog from a shell invocation?

假如想象 提交于 2019-12-04 16:18:12

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
Georges Dupéron

So far, here are the methods I found :

In gprolog

Using false's answer, I found that one must add a line at the top of the kb.pl file:

a(_) :- fail.

and then use ./query.sh kb.pl "father(X,Y), saysOhNo(Y)", where query.sh is:

#!/bin/sh
echo "a(fail)." | gprolog --query-goal "consult('$1'), $2"

When the query returns immediately (i.e. no results or a single result and gprolog managed to detect it was the last one), this will run the query consult('kb.pl'), actual_query., and then run the query a(fail). which will simply print an extraneous no on the console, thanks to the always-false predicate we added at the top of the file.

When gprolog asks what to do (i.e. several results, or a single result and gprolog couldn't detect it was the last one), this will run the query consult('kb.pl'), actual_query., read the a which asks gprolog to print all results, and then it will run the query (fail). which will simply print an extraneous no on the console, because these are just grouping parenthesis, so the query is equivalent to fail..

In xsb

One can use ./query.sh kb.pl "father(X,Y), saysOhNo(Y)", where query.sh is:

#!/bin/sh
(echo "consult('$1'), ${2%.}."; yes halt.) | xsb --noprompt --quietload --nobanner

When xsb asks what to do next, if the user types a non-empty string, followed by enter, it will print the next result, otherwise it will stop searching solutions to the current query. Therefore, with the yes halt. command, we type an infinite stream of non-empty lines. xsb will print all results to the query (each time reading halt., so as it is a non-empty string, it will continue with the next result), and return to its prompt. Then, the following halt. it receives will tell it to quit.

In swi-prolog

I haven't found a solution yet.

[rant]All this would be so much simpler, if the people building prolog implementations actually thought about using them non-interactively, like it's possible with most other languages.[/rant]

jdavid_1385

You may have found a solution for your problem but anyway, here goes my approach. You can always recurse to the bagof built-in predicate. You may read what it does in the docs, this way you will learn more about it.

swipl -q -s starwars.pl -t "bagof(X, Y^father(X,Y), BagOfFathers), bagof(Y, X^father(X,Y), BagOfChildren), writeln(BagOfFathers), writeln(BagOfChildren)."
[anakinSkywalker,anakinSkywalker]
[princessLeia,lukeSkywalker]

You can also process it later as a mapping or whatever you want, the relations are 1:1 (No sure if is the correct way of stating it but I hope you get it)

you can use the following bash script for swi-prolog:

#!/bin/sh

exec swipl -q  -f none -g "load_files([interface],[silent(true)])" \
         -t interface:get_args -- $*

this will load the file interface.pl and call the predicate get_args/0
to get the command line arguments you can call:

current_prolog_flag(argv, Arguments)

of course you can change the names of the predicates/files loaded.
the silent(true) arguments suppresses informational messages such as the intro text

edit: the error message you get is cause you probably dont have an interface.pl file (neither a get_args/0 predicate). you will have to replace interface with kb (or however you name the file) and interface:get_args with kb:father(X,Y), saysOhNo(Y) or use an auxiliary predicate within your prolog file such as run(X,Y):- father(X,Y), saysOhNo(Y) (which may be kinda cleaner)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!