From dynamic to static predicates and vice versa?

Hello,

The following is a template for the program I am developing.

:- dynamic special_predicate/some_arity.
program:-
 	do_little_work_that_involves_using_assert_retract_over_the_special_predicate,
 	compile_predicates([special_predicate/some_arity]),
 	do_a_lot_of_work_that_does_not_need_the_special_predicate_to_be_dynamic,
 	% make special_predicate/some_arity dynamic again
 	do_little_work_that_involves_using_assert_retract_over_the_special_predicate.

Is there anyway to make a predicate dynamic after it has been compiled? If not, is there a better approach to coding my program to maximize performance by sandwhich-compiling my “special_predicate” instead of interpreting it during the heavy-duty evaluation part?

Thanks,
Amine.

Why not have two predicates – one dynamic and one compiled?
One clause of the dynamic predicate just calls the compiled predicate (or vice versa).

One way of creating a predicate then compiling it (I haven’t verified this): write the clauses to a temp file, abolish it, then consult the file. Even better, can you split things into two separate programs? (E.g.: program1 generates predicates that program2 uses. This has the advantage that you only run program1 when its inputs change)

In the current implementation of SWI-Prolog there is internally no difference between dynamic and static code. Once upon a time dynamic predicates required locking for access by multiple threads. That has gone. Predicates, both dynamic and static are now executed purely read-only. As a result, compile_predicates/1 is close to a no-op, simply removing the dynamic flag and dynamic/1 does the opposite. Both can be used on an existing predicate, and the only effect is that retract/1 and assert/1 raise an exception on static code.

Well, there is one small exception: the system will try to find dedicated clause indexing schemas for static predicates with a few clauses only for static code. The two most notable examples are predicates with exactly one clause and predicates switching on [] vs [_|_]. More patterns are likely to follow. Dynamic code does do the normal JIT indexing aiming at many clauses as well as simple first argument indexing.

I’m not claiming there will never be differences. I don’t see any reason to re-introduce differences though.

1 Like

Thanks for the help everybody. Your suggestions are priceless!

I conclude that the execution-time of compiled SWI-Prolog code (for a given input vector) is not affected by whether or not predicates are declared as dynamic, and therefore, there is no point in me trying to compile dynamic predicates during execution to enhance performance.

1 Like
?- vm_list(ack/3).

First call it to materialize the supervisor. This code though behaves exactly the same whether static or dynamic. There is no optimization. Only if M \= 0 it won’t try the first clause due to first argument indexing.