happy(X):-The goal clause(happy(X),Y) produces
Y = healthy(X), wealthy(X)on redoing,
Y = trueNote 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:-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''. So the amended code for list/0 is:
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):-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.