1

I have these facts

(person "Mark")
(person "Nail")
(person "Stark")
(person "Maria")

(married "Stark" "Maria")

I defined the following rule

(defrule single
(not (married ?x ?))
(not (married ? ?x))
=> (assert (single ?X)))

This gives me the following error:

Undefined variable ?x references in RHS if defrule

Everything is working fine why if I use this:

(defrule single
(person ?x)
(not (married ?x ?))
(not (married ? ?x))
=> (assert (single ?X)))

Why?

1 Answer 1

2

The not conditional element (CE) is an existential query. It checks that there are no facts matching the CE. If nothing matches the not CE, then the variables bound inside that CE have no meaning outside of the CE.

This dialogue illustrates the issue with your first rule:

Person 1: Do you know anyone who's married?
Person 2: No.
Person 1: OK, then that person is single.

The phrase "that person" makes no sense here because "anyone" doesn't refer to a specific person.

Your second rule works because the first pattern is matched to a specific person that can be referred to by the subsequent patterns:

Person 1: Do you know a person?
Person 2: Yes.
Person 1: Is that person married?
Person 2: No.
Person 1: OK, then that person is single.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.