0

Is there a way so that portray_clause is written to a variable instead of standard output?

?- portray_clause(f(x):app(X, Y, Z)).
  f(x):app(_, _, _). 
  true.

?- portray_clause(Output, f(x):-app(X, Y, Z)).
  ERROR: Arguments are not sufficiently instantiated

Thank you in advance

false
  • 10,264
  • 13
  • 101
  • 209
  • what do you need to achieve? Printing to another stream (different from std out) or storing a structure representing a clause? – rano Dec 14 '13 at 13:22
  • I need to store a structure that represents the "pretty output" from portray_clause. So if it prints "f(x):app(_, _, _)." I need Ouput = f(x):app(_, _, _). – Matthias Van Eeghem Dec 14 '13 at 13:31
  • Prolog is homoiconic, if you can pass your clause to the portray_clause predicate, you can store it somewhere else – rano Dec 14 '13 at 13:43
  • Yes but I want to save the "pretty" clause, not the clause that I pass on to it. – Matthias Van Eeghem Dec 14 '13 at 13:45
  • Can you provide more examples? It's very unclear what you're trying to achieve. What forms can the clause take? Can it have any number of unbound variables of any name? Etc... – lurker Dec 14 '13 at 14:25
  • The clause can have any name, any number of bound/unbound variables, and any body. Basically any valid clause that is passed on to portray_clause gets printed in a pretty form. That works. I now want that output to be stored in a complex term instead of it being printed. – Matthias Van Eeghem Dec 14 '13 at 14:30
  • That was my attempted code, I'm not sure what the Stream is. – Matthias Van Eeghem Dec 14 '13 at 14:54
  • I think people may be misunderstanding me: portray_clause/2 is actually a built in, I'm not trying to write my own, I simply want to catch its output – Matthias Van Eeghem Dec 14 '13 at 14:57
  • @DmitriChubarov: If you have a question, ask it in a new question. Don't just edit existing questions! – false Mar 02 '14 at 12:55
  • @false The question was in my review queue with close votes suggesting that it is unclear what is being asked. My intention was to clarify the question. Hope that explains the changes that I made. – Dima Chubarov Mar 02 '14 at 14:43

1 Answers1

3

If you are using SWI-Prolog, you can use with_output_to/2. To write, for example, to an atom, you say:

?- with_output_to(atom(A), portray_clause(foo(X) :- bar)).
A = 'foo(_) :-\n\tbar.\n'.
  • @MatthiasVanEeghem You should tag your question [swi-prolog], as this is SWI-Prolog extension. –  Dec 14 '13 at 17:44