# Term expansion when loading compiled qlf files

**URL:** <https://swi-prolog.discourse.group/t/term-expansion-when-loading-compiled-qlf-files/7575>\
**Category:** General\
**Created:** [July 8, 2024, 3:01pm UTC](https://swi-prolog.discourse.group/t/term-expansion-when-loading-compiled-qlf-files/7575 "2024-07-08T15:01:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xabush](https://avatars.discourse-cdn.com/v4/letter/x/bc8723/32.png) [@xabush](https://swi-prolog.discourse.group/u/xabush)\
**Post date:** [July 8, 2024, 3:01pm UTC](https://swi-prolog.discourse.group/t/term-expansion-when-loading-compiled-qlf-files/7575/1 "2024-07-08T15:01:53Z")

</div>

Hi,

I’m facing an issue with dynamically declaring predicates `multifile` using the `term_expansion/2` hook when loading `qlf` files. The approach works fine if I run `consult/1` on the non-compiled `.pl` code. Here is the `term_expansion/2` declaration I’m using:

```prolog
%Defined in a file named hook.pl
term_expansion(Head, [:- multifile(Declaration), Head]) :-
    Head \= begin_of_file,
    Head \= end_of_file,
    Head \= :-(_),
    prolog_load_context(source, Source),
    functor(Head, Name, Arity),
    ( seen_predicate(Source, Name, Arity) ->
      : false
    ; assertz(seen_predicate(Source, Name, Arity)), 
        Declaration = [Name/Arity]
    ).

```

To reproduce the warnings I get, assume we have two files `fileA.pl` and `fileB.pl` with the following contents:

```prolog
%fileA.pl

likes(boy, apple).
eats(boy, apple).

```

```prolog
%fileB.pl
likes(boy, orange).

```

Now, in the SWI-Prolog top-level, after loading `hook.pl` (which contains the `term_expansion/2` declaration), if I run `consult("./fileA.pl") followed by `consult(“./fileB.pl”), everything works as expected.

However, if I compile the files whith `qcompile/1` and load them with `consult("./fileA.qlf")` followed by `consult("./fileB.qlf")`, I get the following warning:

> Warning: Redefined static procedure likes/2  
> Warning: Previously defined at /path/to/fileA.pl:1

For small files I can just ignore the warnings. However, when loading large files that contain predicates declared across files, the SWI-Prolog toplevel shows the above warnings and **takes too long to load the `qlf` files.** For instance, for a file that takes two minutes to load the `pl` version, it takes more than an hour and half to load the `qlf` version if the warning has been thrown.

How do I fix this? That is make `term_expansion/2` work with `qcompiled` files.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [July 8, 2024, 3:22pm UTC](https://swi-prolog.discourse.group/t/term-expansion-when-loading-compiled-qlf-files/7575/2 "2024-07-08T15:22:22Z")

</div>

> [@xabush](#):
>
> How do I fix this? That is make `term_expansion/2` work with `qcompiled` files.

It should work fine, provided you make sure `hook.pl` is loaded when running qcompile/1. qcompile/1 translates the file to a qlf file that basically contains the VM instructions (byte codes). If you do any program transformations, the code to do these transformations should be loaded when compiling.
