2

So, I have some Prolog code that models variable assignments, such as in a programming language, and tries to see if they are compatible with one another. So,

assigned(X, X).
and(P, Q) :- P, Q.
or(P, Q) :- P.
or(P, Q) :- Q.

% and then we should test an expression like this:
and(assigned(X, 5), assigned(X, 6)).

So that last expression fails, since X can't be assigned to both 5 and 6.

Now, what I want to do is have this test a single expression (which can and should be hardcoded into the file), and then simply print out whether or not it's able to be satisfied. But it seems that SWIPL really wants me to run interactively. Ideally, something like this:

> ./test-assignments
false.

Surely this is possible? I'm at my wit's end on this.

Peter
  • 1,349
  • 11
  • 21

1 Answers1

7

There are several ways to get an SWI-Prolog program to run directly from the shell. You can look at this question and the answers:

How to run SWI-Prolog from the command line?

My personal preference now is to have a file example.pl like this:

:- set_prolog_flag(verbose, silent).
:- initialization(main).

main :-
    format('Example script~n'),
    current_prolog_flag(argv, Argv),
    format('Called with ~q~n', [Argv]),
    halt.
main :-
    halt(1).

which I then run from the command line with:

$ swipl example.pl and a few arguments
Example script
Called with [and,a,few,arguments]

The initialization directive tells the interpreter which goal to evaluate after it loads the program: here it is main/0. The name main is a convention, it could have been called anything else.

See the linked question and answers for other options.

Community
  • 1
  • 1