Search code examples
clipsexpert-system

Undefined variable * referenced in RHS of defrule


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?


Solution

  • 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.