Anderson Green
Anderson Green

Reputation: 31810

Printing the source code of a predicate in Prolog

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?

Upvotes: 2

Views: 356

Answers (1)

code_x386
code_x386

Reputation: 798

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)).

Upvotes: 2

Related Questions