command-line-interfaceswi-prologclpr

SWI-Prolog: How to write a solution to the command line output?


I am using SWI-Prolog with the clpr library for solving constraints over real numbers. I do this by calling SWI-Prolog from the command line and parsing the output by another program.

For example, to solve something like {F = 1.8 * C + 32}, {C = 25}. I generate the following command:

swipl \
  -g "use_module(library(clpr))" \
  -g "{F = 1.8 * C + 32}, {C = 25}, write(\"F -> \"), write(F), write(\"\\n\")" \
  -g halt

And the output from SWI-Prolog is:

F -> 77.0

This works great if the result is a plain number but not if the result is again a constraint (or a more complex solution in general). For example, for {X > 3}, {Y < 5}, {X + Y = 10}. I get the solution {Y < 5.0, X = 10.0 - Y} in the SWI-Prolog environment but I did not found a way to write this to the command line output. Is there a way to do this?


Solution

  • You can use dump/3 predicate, for example:

    {X > 3}, {Y < 5}, {X + Y = 10}, dump([X,Y], [x,y], L), write(L).
    

    produces:

     [y=10.0-x,x>5.0]