在prolog中的谓词中定义谓词

时间:2013-02-17 00:36:36

标签: lambda prolog metaprogramming predicate

我正在尝试将谓词内联定义为将其传递给Prolog中的另一个谓词。

% Test if a "product" of graphs G1 and G2 has a "mini-loop" starting from Q
test_property_combined(G1,G2,Q):-
    (g12(Q1,Q2) :- combine(G1,G2,Q1,Q2)),
    some_property(g12,Q).

(上面的语法显然是错误的。)

稍后g12

将调用call
% Test if a graph G has a "mini-loop" starting from Q
some_property(G,Q):-
    Goal1 =.. [G,Q,C],
    Goal2 =.. [G,C,Q],
    call(Goal1),
    call(Goal2).

问题仍然存在,因为我想在某种先前定义的谓词聚合上测试some_property

% Create a "product" of graphs G1 and G2
combine(G1,G2,(Q1,Q2),(Q3,Q4)):-
    Goal1 =.. [G1,Q1,Q3],
    Goal2 =.. [G2,Q2,Q4],
    call(Goal1),
    call(Goal2).

提到的谓词和测试查询的示例:

% g1 and g2 are graphs
g1(a,b).
g1(b,a).

g2(c,d).
g2(d,c).


?- test_property_combined(g1,g2,(a,c)).

如何去做呢?

1 个答案:

答案 0 :(得分:2)

我不确定这一点,但这有效:

test_property_combined(G1,G2,Q):-
    assert((g12(Q1,Q2) :- combine(G1,G2,Q1,Q2))),
    some_property(g12,Q).

好的,可能是那样的

:- use_module(library(lambda)).

test_property_combined(G1,G2,Q):-
    % (g12(Q1,Q2) :- combine(G1,G2,Q1,Q2)),
    Pred = \Z^T^combine(G1,G2,Z,T),
    some_property(Pred,Q).


combine(G1,G2,(Q1,Q2),(Q3,Q4)):-
    Goal1 =.. [G1,Q1,Q3],
    Goal2 =.. [G2,Q2,Q4],
    call(Goal1),
    call(Goal2).


some_property(G,Q):-
    call(G, Q, C),
    call(G, C, Q).

最后编辑(希望如此),完整代码:

test_property_combined(G1,G2,Q):-
    some_property(combine(G1,G2),Q).

combine(G1,G2,(Q1,Q2),(Q3,Q4)):-
    call(G1,Q1,Q3),
    call(G2,Q2,Q4).

some_property(G,Q):-
    call(G, Q, C),
    call(G, C, Q).

g1(a,b).
g1(b,a).

g2(c,d).
g2(d,c).

@false =>有用的评论,像往常一样!

相关问题