3

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.

vasily
  • 2,850
  • 1
  • 24
  • 40

1 Answers1

1

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

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33