Term expansion before compiling in GNU Prolog

71 views Asked by At

Given the following tuple of directive + predicate:

:- web_resource(plaintext/1, [content_type(text/plain)]).
plaintext(Result) :-
    ...code...

how do I tell gplc to transform this code into

plaintext([headers(['Content-Type'-'text/plain']),
           payload(Payload)]) :-
    old_plaintext(Payload).

old_plaintext(Payload) :-
    ...code...

before compiling it? old_payload can be defined recursively.

1

There are 1 answers

0
Paulo Moura On

GNU Prolog term-expansion is currently limited. But you can use Logtalk (which supports GNU Prolog) term-expansion mechanism to achieve that transformation. In a nutshell, define a Logtalk hook object with your expansion rules and then expand your Prolog source files, by using the logtalk_compile/2 predicate. Something like:

:- object(my_expansion,
    implements(expading)).

    term_expansion((:- web_resource(Name/Arity, Headers) Clauses) :-
        % construct clauses
        ...

:- end_object.

and then:

$ gplgt
...
| ?- {my_expansion}.
...

| ?- logtalk_compile('my_source_file.pl', [hook(my_expansion), scratch_directory('.']).
...

The last query will result in a new Prolog file saved in the current directory with the expansion results. You can them compile this file using gplc as usual. For more details on the portable Logtalk term-expansion mechanism see: https://logtalk.org/manuals/userman/expansion.html