Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a predicate functor based on a variable instead of a name

Tags:

prolog

Let's say I've declared predicates a and c as follows:

a(b).

c(D, E) :- D(E).

I'd like to believe that c(a, b) succeeds, as D(E) matches a(b) if D is bound to a and E is bound to b, but (in SWI Prolog anyway) syntax checking is looking for an operator following D in the definition of c, so apparently there's a rule that unification binds variables only to arguments, not functors. Is there some trick for asking the question posed by c, in which the identity of a predicate is one of the unknowns?

like image 464
Lori Avatar asked Dec 19 '25 04:12

Lori


1 Answers

You can make use of call/2 predicate [swi-doc] to call a predicate with the given name and parameters. So your c/2 predicate is equivalent to call/2:

c(D, E) :-
    call(D, E).
like image 51
Willem Van Onsem Avatar answered Dec 21 '25 21:12

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!