prolog - Deducing facts from a set via a predicate by metaprogramming -



prolog - Deducing facts from a set via a predicate by metaprogramming -

suppose have set of literals (represented list instance) , predicate specified dynamically, want produce set of literals contains previous ones in add-on ones can deducted applying predicate set.

an example, having defined predicate

pred(a, b) :- base(a, b). pred(a, c) :- base(a, b), pred(b, c).

and supposing such signature predicate

deduce_set(+set, +pred, ?deducedset)

the next statement holds (is true):

deduce_set([base(a,b), base(a,c), base(b,d), base(d, e)], pred/2, [base(a,b), base(a,c), base(b,d), base(d,e), pred(a,d), pred(a,e), pred(b,e)] ).

what efficient , general way so? i've been thinking like:

asserting literals in set call pred if succeeds assert head collect asserted facts in resulting set , set list

isn't there improve way?

update solution, improve defined capellic, using metaprogramming can't cope vars in set under object identity. workaround this?

you can utilize findall/3 (or better, findall/4), avoiding problem discriminate (for instance) instances of pred/2 need remove before retrying deductive step.

deduce_set(base, pred/arity, res) :- functor(p, pred, arity), % how 'undo' without description? % retractall(base(_,_)), setof(f-a, m^(member(m, base), functor(m, f, a)), desc), maplist(retractdesc, desc), maplist(assertz, base), findall(p, p, all), append(base, all, res). retractdesc(f-a) :- functor(p, f, a), retractall(p).

i'll add together description of base of operations elements, know clear before running (of course of study obtained using setof(f-a,m^(member(m,base),functor(m,f,a)),desc) )

pred(a, b) :- base(a, b). pred(a, c) :- base(a, b), pred(b, c). test :- deduce_set([base(a,b), base(a,c), base(b,d), base(d, e)], pred/2, r), r = [base(a,b), base(a,c), base(b,d), base(d,e), pred(a,d), pred(a,e), pred(b,e)].

note test/0 fail, because homecoming set doesn't match expected list.

?- test. base(a,b) base(a,c) base(b,d) base(d,e) pred(a,b) pred(a,c) pred(b,d) pred(d,e) pred(a,d) pred(a,e) pred(b,e) false.

generally, suggest utilize datalog task, informal description seems suspiciously similar. see des 'free use' , feature rich system.

prolog metaprogramming

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -