Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting goal_expansion/6 to compile time only

In SICStus Prolog, there is a hook for expanding a goal: goal_expansion/6 which is called both at compile time and at runtime during metacalling. These calls incur quite some runtime overhead which slows down metacalling. The purpose of my expansion is optimization only. So semantically the goals and expanded goals are equivalent.

How can I disable such calls at runtime?

(It seems I would have to abolish goal_expansion/6 which looks a bit crass to me. It would also hamper lightweight recompilation).

like image 957
false Avatar asked Mar 30 '16 19:03

false


2 Answers

The idiomatic way is to load the compile-time-only code using load_files/3 with option when(compile_time). Unfortunately, this does not help if you want to (re)compile in the same process in which you then run your code.

Using abolish to remove the definition of goal_expansion/5 is also not ideal (since it will be gone if you then re-compile). It is not as bad/crass as it seems, though: goal_expansion/5 is per module, so you can abolish it without worrying that you destroy some functionality in some other module.

like image 149
Per Mildner Avatar answered Nov 07 '22 07:11

Per Mildner


A workaround would be to call the prolog_load_context/2 predicate. Something like:

goal_expansion(...) :-
    prolog_load_context(source, _),
    % compile-time; expand the goal
    ... .

The prolog_load_context/2 predicate only succeeds at compile time.

like image 4
Paulo Moura Avatar answered Nov 07 '22 05:11

Paulo Moura