aLu
aLu

Reputation: 55

Unifying dynamic predicates in SWI Prolog

I am calling a SWI Prolog engine from a C++ dll and I would like to be able to access all asserted/dynamic predicates (similar to what listing normally does).

In GNU Prolog I would call the dynamic/1 predicate with a Variable and Prolog would unify it with all dynamic predicate indicators. In the next step I would call these predicates with variables and get a complete list.

Unfortunately I can't do that in SWI Prolog (ERROR: Arguments are not sufficiently instantiated). Is there another way?

Upvotes: 2

Views: 479

Answers (1)

user206428
user206428

Reputation:

Why not just roll your own, for example:

get_dynamic_predicates(M, H, B, R) :-
    current_predicate(_, P),
    strip_module(P, M, H),
    predicate_property(P, dynamic), 
    \+ predicate_property(P, built_in), 
    \+ predicate_property(P, imported_from(_)),
    clause(P, B, R).

Calling this backtracks to bind instances of predicates with head H in module M with body B and clause reference R. If all you wanted to retrieve are the names of the dynamic predicates, you could call this to retrieve the set of all bindings for H, or the functors thereof.

Modify the predicate properties to those you'd prefer (or better yet, pass them in as arguments); the above definition will retrieve all user-defined dynamic predicates, which is what I'm guessing you're after.

Upvotes: 2

Related Questions