swi-prolog

How to call a predicate with arguments from command line and output its solution?


My actual application is more complicated than the following illustrative predicate but I chose it as it is because it covers various features which I would need to handle. I tried around with initialization and some of the swipl attributes but cannot really get it to work and also can't find good documentation on it.

Assuming this is stored in a file called app.pl:

t([X1, X2|[]], X3, Y) :- 
    Y1 is X1 + X2, 
    Y2 is 2 * X3, 
    Y = [Y1, Y2]
.

What I eventually want to be able to do is something like that (this is just plausible pseudo code):

swipl -f app.pl -c 't([1,2],3,Y)'

And then receive the response Y = [3,6] for further processing on STDOUT.

I don't really want to introduce necessarily a main procedure. Though, it would also be interesting to see how that would work, too. But my eventual goal is to call individual predicates from Python or Bash for testing.

In fewer words, I want to emulate a single invocation in a swipl REPL session from the Linux command line.

--

I found a Python package called pyswip which seems to be actively maintained and might be how I do it (not sure, yet). But I'd anyway still be interested in a command line solution because that might be relevant in several other circumstances.


Solution

  • As of now - this is the recommended [1] way to do it:

    āžœ swipl -g "t([1,2],3,Y), writeln(Y)" -t halt app.pl
    [3,6]
    

    1: https://swi-prolog.discourse.group/t/how-to-call-a-predicate-with-arguments-from-command-line-and-output-its-solution/6919/5