Term & Goal Expansion
33.1 term_expansion/2 — compile-time code transformation
:- multifile user:term_expansion/2.
% Expand "person(Name, Age)" facts into two facts each
user:term_expansion(person(N, A), [name(N), age(N, A)]).
person(ann, 30). % becomes name(ann). age(ann, 30).33.2 goal_expansion/2 — rewrite goals in clause bodies
user:goal_expansion(square(X, Y), Y is X * X). % inline at compile time33.3 DSLs via operators + expansion
:- op(1150, fx, rule).
:- op(1100, xfx, then).
term_expansion((rule If then Then), (Then :- If)).
rule (bird(X), \+ penguin(X)) then flies(X).33.4 Other compile-time tools
library(apply_macros) expands maplist/N calls into specialized helper predicates.
library(yall) lambdas are expanded at compile time where possible.
DCG translation itself is a term expansion (dcg_translate_rule/2).
Conditional compilation: :- if(Cond). ... :- elif(C2). ... :- else. ... :- endif.