Printing the source code of a predicate in Prolog

扶醉桌前 提交于 2019-12-08 01:59:45

问题


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?


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!