Here, I tried to solve some simple string constraints in Prolog. While this problem seems to be straightforward, the main
predicate still does not print the value of FinalString
, which should be "hello world"
. Is it possible for Prolog to solve constraints that contain string concatenations, like this one?
:- set_prolog_flag(verbose,silent).
:- prompt(_, '').
:- use_module(library(readutil)).
main :-
append("hello ", B, FinalString),
append(A, "world", FinalString),
append(A, B, FinalString),
writeln(FinalString),
true.
:-main.
If you do experiments as a beginner, better stick to using the prolog-toplevel. In this manner you can rapidily identify the problem. Since you are most probably using SWI7 - like so:
So if this is false, lets generalize it, by removing the rightmost goal:
Nah ...
The actual problem is that double quotes in SWI have now a non-conforming meaning. To get traditional behavior, call SWI with command line option
--traditional
and:For more on this flag see this answer.
Now your original query produces a sensible answer:
... or does it? It finds the answer you want, but at the same time, when being asked whether or not there is a further answer, it loops! Such problems can be easily detected when using the toplevel shell, but remain undetected with a script as yours. This loop is an inherent problem. There is no easy way out, because the fact that there is exactly one solution is caused by all three goals together.
If you really like to write it as a script, rather write it like so: