Atom to Rule

Hello,

How do I turn an atom – that I generate at runtime – and which has the format:
'solve(A):-ask_for_help(A).'

into a clause that I can assert in the knowledge base so that I can use it as part of my program?

I apologize if the answer is blindingly obvious. The extra-logical stuff in Prolog always KOs me.

A bit of background: In my program, and at runtime; I need to generate a constraint-satisfaction problem and solve it. It is not possible to statically generate the CSP as it depends on the problem that the program tries to solve.

Using diverse atom processing built-in predicates, I am able to generate an atom like this:
'solve_csp(A):-clpr:{A=2*B},clpr:{A+B=9}.'

Then, when it comes to asserting it in the knowledge base using assert/1, it gets asserted as an atom – which it is. However, I want it asserted as a predicate that can be evaluated.

Many thanks.

Basically you can solve your problem using the good old read/1, which embeds a full Prolog parser, followed by assert/1.

SWI-Prolog offers a lot of improvements, for instance I would first try read_term_from_atom.

?- [library(clpr)].
true.

?- read_term_from_atom('solve_csp(A):-clpr:{A=2*B},clpr:{A+B=9}.',Rule,[]),assertz(Rule).
Rule =  (solve_csp(_9620):-clpr:{_9620=2*_9626}, clpr:{_9620+_9626=9}).

?- solve_csp(X).
X = 6.0.

Most likely, your problem arises from having too many choices. Try to input read_ in the Search Documentation: box appearing upper right in the docs.

Thank you very much! That works perfectly.