简体   繁体   中英

Define a predicate inside a predicate in prolog

I'm trying to define a predicate inline to pass it to another predicate in 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).

(The syntax above is obviously wrong.)

Later on g12 would be invoked by 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).

The problem persists because i want to test some_property on some kind of an aggregation of previously defined predicates.

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

The mentioned predicates and an example of a test query:

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

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


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

How does one go about doing it?

I'm not sure to get the point, but this works :

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

Ok, may be something like that

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

Last edit (hope so) with full code :

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 => useful remarks, as usual !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM