S0rin
S0rin

Reputation: 1293

Assert higher-order clause in Prolog

This works:

assert(p(X) :- q(X)).

This does not work:

P = p,Q = q, assert(P(X) :- Q(X)).

How can I make the latter work?

Upvotes: 1

Views: 776

Answers (1)

user1812457
user1812457

Reputation:

You need to make the terms first; you can use the "univ" operator, =.. for this:

?- P = p, Q = q, Head =.. [P, X], Body =.. [Q, X], assertz((Head :- Body)).
P = p,
Q = q,
Head = p(X),
Body = q(X).

?- listing(p/1).
:- dynamic p/1.

p(A) :-
    q(A).

You need the second pair of parentheses in most implementations, apparently. You will need them anyway if you had for example a conjunction in the body.

?- assertz(a :- b).
true.

?- assertz(a :- b, c).
ERROR: assertz/2: Uninstantiated argument expected, found c (2-nd argument)
?- assertz((a :- b, c)).
true.

Upvotes: 4

Related Questions