Define a predicate inside a predicate in a prolog

I am trying to define an inline predicate 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 incorrect.)

In the future, g12 will 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 any 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 specified 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 to do it?

+6
source share
1 answer

I'm not sure I get it, but this works:

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

Ok maybe something like this

 :- 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 notes as usual!

+2
source

All Articles