I'm writing a Prolog meta-interpreter that needs to obtain the source code of a predicate that I defined. I thought this would be possible using expand_term/2
, but it returned a recursive data structure instead of the predicate's source code:
:- initialization(main).
main :- expand_term(quadratic_formula(X,A,B,C) :- Z,Z),writeln(Z).
%This prints @(S_1,[S_1=(quadratic_formula(_3068,_3090,_3112,_3134):-S_1)]) instead of the predicate's source code.
quadratic_formula(X,A,B,C) :-
X is -B + sqrt(B*B-4*A*C)/2*A;
X is -B - sqrt(B*B-4*A*C)/2*A.
Is there some other way to obtain the source code of a user-defined predicate?
Do you mean getting the same source code as produced by listing/1? If this is the case, you can simply use listing/1 in conjunction with with_output_to/2:
with_output_to(atom(SourceCode), listing(quadratic_formula)).
来源:https://stackoverflow.com/questions/46556721/printing-the-source-code-of-a-predicate-in-prolog