next up previous contents
Next: functor/3 Up: Powerful Features ---Splitting Previous: Powerful Features ---Splitting

clause/2

 
happy(X):-

healthy(X),

wealthy(X).

happy(jim).

The goal clause(happy(X),Y) produces
 
Y = healthy(X), wealthy(X)

on redoing,
 
Y = true

Note the second answer returns a body of true for the clause happy(jim).

For SICStus (and Quintus), the first argument of clause/2 must specify at least the principle functor. That is, a call such as clause(X,Y) will fail.

However, for many Prolog systems, any calling pattern can be used: this means that we can also extract all the clauses which are facts with the goal clause(X,true).

Before we show how to get round this limitation in SICSTUS, we illustrate with a simplified version of listing/0 which we name list/0:

 
list:-

clause(X,Y),

write_clause(X,Y),

fail.

list.

write_clause(X,Y):-

write((X:-Y)),

nl. [-5pt]

Now this can be made to work for SICStus by using predicate_property/2. This predicate can be called as in:
 
?- predicate_property(X,interpreted).

and X will be bound to the head of the first clause found that is ``interpreted''gif. So the amended code for list/0 is:
 
list:-

predicate_property(X,interpreted),

clause(X,Y),

write_clause(X,Y),

fail.

list.

Note however that this fails to print the final `.' of a clause and that it also prints facts as if they were rules with their body equal to true. We can improve on this a little by changing write_clause/2.

 
write_clause(X,true):-

write(X),

write('.'),nl.

write_clause(X,Y):-

\+(Y=true),

write(X),

write((:-)),nl,

write_body(Y).

write_body(Y):-

write(' '),

write(Y),

write('.'),nl.

Note that we have used \+/1 to make the code determinate. If we wanted to put each subgoal on a separate line then we could rewrite write_body/1.



Paul Brna
Mon May 24 20:14:48 BST 1999