7

我正在尝试定义一个谓词内联以将其传递给 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)).

怎么做呢?

4

1 回答 1

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 => 有用的评论,像往常一样!

于 2013-02-17T09:07:26.333 回答