term_expansion(module_x:predicate(_X), true) -- doesnt expand if included in module_y

term_expansion/2 simply processes the output of read/1. Modules are not involved, except for which term_expansion/2 rules are visible and the order in which they are handled. First it calls term_expansion/2 in the module being loaded. The result is passed to user:term_expansion/2 and the result thereof to system:term_expansion/0. Next DCG translation and finally (dict) function expansion is done. The final output is passed to the backend compiler.

The predicate prolog_load_context/2 can be useful to examine the context in which the expansion takes place.

1 Like