I'm currently trying to learn some Prolog (using ECLiPSe). From time to time I come across the clause/2 predicate, but I fail to understand what it is used for. I read some references (e.g. this one), but I still don't get in what case it could be useful. Can someone provide me with an easy example or explanation of this?
Asked
Active
Viewed 3,292 times
2 Answers
3
This predicate allows metaprogramming, i.e. reasoning about your Prolog program.
SWI-Prolog uses clause/2
in, a.o., the explain
predicate:
?- explain(member).
"member" is an atom
Referenced from 12-th clause of pce_meta:pce_to_pl_type/3
lists:member/2 is a predicate defined in
c:/program files/swi-prolog/library/lists.pl:81
Referenced from 1-th clause of prolog_dialect:source_exports/2
Referenced from 1-th clause of pce_config:term_description/2
Referenced from 1-th clause of pce_config:config_attribute/2
Referenced from 1-th clause of pce_config:load_config_key/2
Referenced from 1-th clause of pce_config:term_description/3
Referenced from 1-th clause of pce_config:current_config_path/1
Referenced from 4-th clause of persistent_frame:has_specifier/1
true.
and in the implementation of Constraint Handling Rules. I suspect it's also useful for doing inductive logic programming and various other Prolog extensions.
For a thorough introduction to metaprogramming in Prolog, see The Art of Prolog by Sterling and Shapiro.

Fred Foo
- 355,277
- 75
- 744
- 836
-
So, the predicate allows to check whether there is a dynamic clause, that would succeed with the given head and body? – Lennart May 11 '11 at 09:55
-
1@redfalcon: not necessarily a dynamic clause, it also finds static clauses. – Fred Foo May 11 '11 at 16:29
1
one use is a really elegant quine :b
quine :-
clause(quine, A),
portray_clause((quine:-A)).
found here
which is of course a case of meta-programming as larsmans said

Thanos Tintinidis
- 5,828
- 1
- 20
- 31