Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print formatted clause 1

Status
Not open for further replies.

gianvito

Programmer
Mar 4, 2006
4
IT
Hello,
I use YAP Prolog. I discovered that portray_clause/2 can print formatted clauses.
For example portray_clause((p(_15231):-q(_15231)) prints:
p(A):-
q(A).

Well...if in my code I have the head in a variable and the body in another variable, for example (X = p(_15231) and Y = q(_15231)), how can I concatenate them or create a unique clause before printing it using portray_clause/2?

Thanks
 
Maybe this is useful (query the Prolog prompt):

Code:
?- X = p(_15231), Y = q(_15231), portray_clause(X :- Y).
 
Even clearer:

Code:
?- X = p(_15231), Y = q(_15231), Z = (X :- Y), portray_clause(Z).
 
Mmm...ok thanks...it works.
Btw I forgot to say that I have a list of terms.

Let's say I have X = p(_15231), Y = [q(_15231),r(_15231,_11255)).

If I do Z = (X :- Y), portray_clause(Z), I get this:
p(A):-
[q(A),r(A,_)].

I'd like to have this:
p(A):-
q(A),
r(A,_).

Any solution?
Thank you
 
You have to convert the Y list of terms into a sequence of the same terms concatenated using commas (logical ANDs).

My first idea would be to create a clause called, let's say, make_comma_sequence:

Code:
make_comma_sequence([X], X) :- !.
make_comma_sequence([H | T], (H, BodyT)) :-
	make_comma_sequence(T, BodyT).

The result of converting [H | T] would be (H, BodyT) where BodyT is the result of converting T alone.

Once you have make_comma_sequence, you use it like this to solve your problem:

Code:
test :-
	X = p(_15231),
	Y = [q(_15231), r(_15231, _11255)],
	make_comma_sequence(Y, SequenceY),
	Z = (X :- SequenceY),
	portray_clause(Z).

Then query 'test' at the Prolog prompt :)
 
Thank you! It was exactly what I was searching for ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top