About the usage of load_files/2 with stream option

I’m using: SWI-Prolog version 9.0.4

I want the code to: use the predicate load_files/2 with a string stream. To make a stream from a string, I used open_string/2. When compiling the code of the string, I used the option [stream(Stream)] but I don’t understand what the first argument “Files” is for. As the document said, I just put an arbitrary atom as Files.

But what I’m getting is: I got an error message " Syntax error: Unbalanced operator".

My code looks like this:

...
    open_string( CutStr, Stream),
    load_files( benders, [stream(Stream)]),
    close( Stream),
...

I will not try to answer your question directly because I don’t use load_files/2 in the manner you note to have enough confidence an answer would be correct, however

sometimes seeing actual examples helps.

The first example is the one closest to the code you noted in your question; not saying this is what you seek but a working example of how the predicates you noted work together.


The code that implements the stream option


Note: Many of the snippets above are zoomed in to show the use of the load_files/2 predicate. To truly understand what is happening in the code click on the link to see the file and then look at the predicate(s) that make use of the noted code.

The 5th example is quite close to mine. A strange thing is that my code is working with no error in SWI PROLOG version 9.1.12 (development version). I used this code snippet:

    open_string("h( X1, X2) :-
		   X1 + X2 #= 7,
		   fd_dom( X1, D1),
		   fd_dom( X2, D2),   
		   writeln( D1),
		   writeln( D2).",
		Stream),
    load_files( load, [stream(Stream)]),
    close( Stream),

I just used an arbitrary atom “load” but it doesn’t make any errors. Is it just a small bug?

Thank you for your response.

The question is of course whether the syntax error comes from the code loading the string or the string itself. It should normally give some context about the syntax error.

P.s. the atom is for SWI-Prolog’s source information. It notably allows for “reconsult” of the content and also supports unload_file/1.

I got the source of the error. As you said, it is not a problem about “Files” but the string streamed. In the dynamic code generation, it met an exceptional state and did not generate a code correctly. So the string of the code was syntactically wrong.

Thank you so much.

Good :slight_smile: Note that generating program text and compiling this is typically not the best solution. Typically there are more efficient ways to get the right stuff into Prolog that is not sensitive to syntax issues and therefore not sensitive to injection attacks. It all depends what you are trying to do though …

Thank you for your explanation. I tried to construct terms but variables become different and I couldn’t find the best way to unifying different variables. Also in my case, I don’t need to worry about injection attacks because this code generation is used in computation. My project is making a new solver for constrained dynamic programming problems. Currently, using string is an easy way to deal with variables. I am not sure it is computationally efficient, though.

Thank you so much for your help.