Search code examples
prologclpb

Print the results in a txt file with prolog


I'm using Prolog with clpd to solve boolean problems. I have rules like this one below:

:- use_module(library(clpb)).

fun(A, B, C, D, E) :-
    sat(A + B + C, D),
    sat(E),
    labeling([A, B, C, D, E]);

Is possible to print the results in a file? How can I do?


Solution

  • Your code had some simple mistakes. You could try this version (changed some small things):

    :- use_module(library(clpb)).
    
    fun(A, B, C, D, E) :-
        open('test1234.txt',write,ID),
         (  sat(A + B + C + D),
            sat(E),
            labeling([A, B, C, D, E]),
            write(ID, labeling([A, B, C, D, E]) ),nl(ID), fail
            ;   close(ID)
          ).
    

    Now if you query:

    ?- fun(A,B,C,D,E).
    true.
    

    "test1234.txt" will e created in your current working directory. The "test1234.txt" file contains:

    labeling([0,0,0,1,1])
    labeling([0,0,1,0,1])
    labeling([0,0,1,1,1])
    labeling([0,1,0,0,1])
    labeling([0,1,0,1,1])
    labeling([0,1,1,0,1])
    labeling([0,1,1,1,1])
    labeling([1,0,0,0,1])
    labeling([1,0,0,1,1])
    labeling([1,0,1,0,1])
    labeling([1,0,1,1,1])
    labeling([1,1,0,0,1])
    labeling([1,1,0,1,1])
    labeling([1,1,1,0,1])
    labeling([1,1,1,1,1])