3

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?

Makyen
  • 31,849
  • 12
  • 86
  • 121
Lennart
  • 9,657
  • 16
  • 68
  • 84

2 Answers2

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
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