Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 855
Rihards Krišlauks Avatar asked Feb 17 '13 00:02

Rihards Krišlauks


1 Answers

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 !

like image 161
joel76 Avatar answered Sep 25 '22 10:09

joel76