本文介绍了在 Prolog 中打印谓词的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Prolog 元解释器,它需要获取我定义的谓词的源代码.我认为使用 expand_term/2 可以做到这一点,但它返回了一个递归数据结构,而不是谓词的源代码:

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 生成的源代码相同的源代码吗?如果是这种情况,您可以简单地将 listing/1 与 with_output_to/2 结合使用:

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

这篇关于在 Prolog 中打印谓词的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 15:57