Generating and export predicates: compile_aux_clauses(+Clauses)

Hello,

For an API I want to generate (domain specific) predicates dynamically that I want to export.

For example, suppose I have a table of terms, and for each term I want to generate an exported predicates, as so:

gen_table(cond1, arg11).
gen_table(cond2,  arg21).

I want to generate something like this:

cond1(X, Y) :-
   process(X, cond1, arg11, Y).

cond1(X, Y) :-
  process(X, cond2, arg21, Y).

The main idea is to avoid a generic predicate API names with table lookup and instead offer a syntactically domain specifically named predicate for an API.

process(X, Y) :-
   gen_table(X, Arg),
  process(X, Arg, Y).

The alterantive right now is to write “manually” API predicates for each table entry, which seems useful for the API user but a lot of duplication from the coding point of view.

Can this be done?

Edit:

Modeled on expand_maplist, i tried the following:

https://cplint.eu/pldoc/doc/SWI/library/apply_macros.pl?show=src

init_compile :-
	Clause1 = (check_me(X) :- integer(X)),
	compile_aux_clauses([Clause1]),
	check_me(1).

But, compile_aux_clauses fails.

couldn’t get this to work with compile_aux_clauses

I guess, the simple alternative is to write another program that simply generates a module which i then include in the other program …

thanks,

Dan

I my case, I wrap compile_aux_clauses/1 in ignore/1 since a long time ago (10 years?). I don’t remember the reason why doing so. Anyway no problem has been found so far for my purpose.

compile_aux_clauses/1 is det, so no need for ignore. Well, I don’t think it does a lot of error checking, so it may fail on illegal input.

How exactly does valid input look like … i tried to glean it from the apply macro code but didn’t succeed …

It is either a valid Prolog source term or a list thereof. It will probably only fail on an invalid list, e.g. [p|q].