在 SWI-Prolog 中实现部分评估
Implementing partial evaluation in SWI-Prolog
我正在为 Prolog 查询编写部分求值器。我尝试使用 expand_goal/2
扩展查询,但在这种情况下它只是将 Input
与 Output
统一起来:
:- initialization(main).
main :-
Input=is_between(1,A,3),expand_goal(Input,Output),writeln(Output).
is_between(A,B,C) :-
B>A,B<C.
我也试过使用term_expansion/2
,但这会导致程序失败:
:- initialization(main).
main :-
Input=is_between(1,A,3),term_expansion(Input,Output),writeln(Output).
is_between(A,B,C) :-
B>A,B<C.
SWI-Prolog 是否有内置谓词可以在运行时执行查询的宏扩展,就像我在这里尝试做的那样?
可以使用内置的 clause/2
predicate. This predicate expands the clause like a hygienic macro:
扩展 Prolog 子句
:- initialization(main).
main :- clause(is_between(1,2,3),B),writeln(B).
is_between(A,B,C) :- A<B,C>B.
此示例打印 1<2,3>2
.
可以使用 findall/3 谓词扩展多个子句:
:- initialization(main).
main :- find_all_clauses(is_between(1,2,3),B),writeln(B).
find_all_clauses(Predicate,Output) :-
findall(Predicate1,clause(Predicate,Predicate1),Output1),
list_to_disjunction(Output1,Output).
list_to_disjunction([A],A).
list_to_disjunction([A|B],(A;B1)) :- list_to_disjunction(B,B1).
is_between(A,B,C) :- A<B,C>B.
is_between(A,B,C) :- B>A,B<C.
此示例打印 1<2,3>2;2>1,2<3
.
我也写了another partial evaluator that expands the goals recursively. There are a few other open-source libraries for partial evaluation in Prolog, such as ecce and logen.
我正在为 Prolog 查询编写部分求值器。我尝试使用 expand_goal/2
扩展查询,但在这种情况下它只是将 Input
与 Output
统一起来:
:- initialization(main).
main :-
Input=is_between(1,A,3),expand_goal(Input,Output),writeln(Output).
is_between(A,B,C) :-
B>A,B<C.
我也试过使用term_expansion/2
,但这会导致程序失败:
:- initialization(main).
main :-
Input=is_between(1,A,3),term_expansion(Input,Output),writeln(Output).
is_between(A,B,C) :-
B>A,B<C.
SWI-Prolog 是否有内置谓词可以在运行时执行查询的宏扩展,就像我在这里尝试做的那样?
可以使用内置的 clause/2
predicate. This predicate expands the clause like a hygienic macro:
:- initialization(main).
main :- clause(is_between(1,2,3),B),writeln(B).
is_between(A,B,C) :- A<B,C>B.
此示例打印 1<2,3>2
.
可以使用 findall/3 谓词扩展多个子句:
:- initialization(main).
main :- find_all_clauses(is_between(1,2,3),B),writeln(B).
find_all_clauses(Predicate,Output) :-
findall(Predicate1,clause(Predicate,Predicate1),Output1),
list_to_disjunction(Output1,Output).
list_to_disjunction([A],A).
list_to_disjunction([A|B],(A;B1)) :- list_to_disjunction(B,B1).
is_between(A,B,C) :- A<B,C>B.
is_between(A,B,C) :- B>A,B<C.
此示例打印 1<2,3>2;2>1,2<3
.
我也写了another partial evaluator that expands the goals recursively. There are a few other open-source libraries for partial evaluation in Prolog, such as ecce and logen.