Search code examples
prologclause

Understanding the clause/2 predicate


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?


Solution

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