How can I write the following rule in PROLOG: if P then not Q
I understand that you can easily write if P then Q the predicates like q(X) :- p(X)>
"...if P then not Q" can be represented via the ->
if-then control-flow predicate (e.g., GNU) , along with the \+
negation (or 'not-provable') operator (e.g., GNU), as follows:
(P -> \+ Q),
Note that, typically, \+
will implement what is known as negation-as-failure; i.e., the subgoal/expression \+ Q
will succeed iff Q
cannot. Note that the evaluation of Q
under \+
will not affect the bindings of any variables present in the expression Q
at execution.
For example, consider:
foo(a).
bar(b).
Given these facts, the following hold:
foo(a) -> \+ bar(a). % succeeds, as bar(a) is not provable.
foo(a) -> \+ bar(b). % fails, as bar(b) is provable.
foo(a) -> \+ bar(X). % fails, as bar(X) is provable; note that X remains unbound.
foo(X) -> \+ bar(X). % succeeds, as bar(X) where X unified to 'a' isn't provable.
Implementing something akin to \+ q(X) :- p(X)
as you might want (in terms of a 'rule') isn't straightforward, as you describe, however a potential hack is:
q(X) :- p(X), !, fail.
This definition will only reflect the intention that q(X)
is to fail for all X
where p(X)
succeeds iff it is asserted before any other clauses of q(X)
, but may not be ideal.