Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all prolog rules in database

Tags:

prolog

Suppose I have a facts db filled with at least:

fact1(A) :- !, A=ok.
fact2(B) :- !, B=ok.

How can I enumerate through all the facts in this db? Ideally I'd have a predicate that I could use:

?- all_rules( Head :- Tail).
Head=fact1(_G100),
Tail=(!, _G100=ok) ;
Head=fact2(_G101),
Tail=(!, _G101=ok)
....followed by all other predicates in other modules loaded...

I found current_predicate/1, but I can't figure out what this is actually doing...

like image 287
DaveEdelstein Avatar asked Jun 06 '11 15:06

DaveEdelstein


2 Answers

It depends on the precise Prolog system you are using. As long, as you only want to look at the definitions, listing/0 works in many systems. But listing/0 only prints a text. clause/2 often works only for predicates declared dynamically.

like image 170
false Avatar answered Oct 06 '22 12:10

false


Maybe something like this:

?- current_predicate(Name/Arity),
   functor(Pred, Name, Arity),
   nth_clause(Pred, Index, Ref),
   clause(Head, Body, Ref).

Read more in Examining the program.

like image 22
Kaarel Avatar answered Oct 06 '22 12:10

Kaarel