How do I show the results of pattern-matching goal

2019-05-23 16:01发布

问题:

I am wondering how one gets output from SWI-Prolog when invoking it from the shell.

Say I have a simple knowledge base, kb.pl:

dad(elvis, lisaMarie).
dad(john, julian).

I can invoke SWI-Prolog from the shell:

$ swipl --quiet -s kb.pl -t listing

and a listing of my knowledge base is printed to stdout. If I try this:

$ swipl --quiet -s kb.pl -t "dad(elvis, X)"
$ echo $?
0

No output is printed, but I know that it found matches because I get zero when I then query for the return code. Similarly:

$ swipl --quiet -s kb.pl -t "dad(morrisey, X)"
$ echo $?
1

Shows that that the Prolog is correctly failing to find a matching fact.

My question is this: How do I get all the matches to print, so that from the shell I can see output like when I am in the Prolog environment? E.g.

$ swipl --quiet -s kb.pl -t "dad(elvis,X)" --magicdust
X = lisaMarie.

I don't think --quiet is the problem. It is just suppressing prolog startup messages. See SWI-Prolog Command-Line Options

回答1:

Print it yourself, for example with:

$ swipl -q -s kb.pl -t "dad(elvis,X), writeln(X), false"