2

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?

false
  • 10,264
  • 13
  • 101
  • 209
NxA
  • 159
  • 1
  • 12
  • 1
    Possible duplicate of http://stackoverflow.com/q/18674731/535275 – Scott Hunter Nov 14 '16 at 17:03
  • i don't know if is a duplicate... the other thread talk about list. Here is boolean logic, and looking the other thread I don't understand how to write the result on a txt. – NxA Nov 14 '16 at 19:41
  • 2
    Which results exactly? How are you going to run this program? Can you show what your text file should contain? –  Nov 14 '16 at 19:55
  • I've wrote this program: http://pastebin.com/UXEas1HJ But i cannot write in the file, because I receive error. – NxA Nov 15 '16 at 09:57

1 Answers1

2

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])
coder
  • 12,832
  • 5
  • 39
  • 53
  • 2
    Nice! You can make this even better by using `setup_call_cleanup/3` to reliably close the file under all circumstances. – mat Nov 15 '16 at 14:25
  • Yes, my mistake was really stupid, I used for the same stream two different names, about "nl" I didn't know the trick but it works perfectly. Anyway, if I want both save the result and display the result in prolog terminal (I use SWI-Prolog) what I should do? Because atm the result is only saved. – NxA Nov 15 '16 at 14:26
  • 1
    @ NxA I don't know how to return the result but you could just print it using print/1. – coder Nov 15 '16 at 14:31
  • 1
    It is much more reliable to use `writeq/1` for writing. And to finish with `.` such that the output can be read back. – false Nov 15 '16 at 17:03