Lisp HUG Maillist Archive

defrule rhs semantics

Hi. In the KW documentation for defrule, I read that

"(<lisp-expr> <term>*) binds the result or results of calling <lisp-expr> to
the <term> s with execution of the rule terminating if any bindings fail (if
no <term> s are given execution will always continue)."

I read this to mean that if <lisp-expr> returns NIL for any of the bindings,
then this rule's execution will stop. That's what I want, but apparently I
misunderstand the doc. Given this scenario:

(defrule frodo
  :forward
  (hobbit ?hobbit)
  -->
  ((find-frodo ?hobbit) ?frodo)
  ((format t "Rule FRODO fired!~%")))

I had hoped that if FIND-FRODO returned NIL, the ?frodo binding would fail
and execution of this rule's rhs would not continue. However, that isn't the
case; execution continues regardless, so I clearly don't understand.

Please offer clarification if you can. Thanks...

David E. Young
Bloodhound Software, Inc.
http://bloodhoundinc.com

"For wisdom is more precious than rubies,
and nothing you desire can compare with her."
  -- Prov. 8:11

"But all the world understands my language."
  -- Franz Joseph Haydn (1732-1809)


Re: defrule rhs semantics

>>>>> On Mon, 15 Mar 2004 13:42:50 -0500, "Young, David" <deyoung@bloodhoundinc.com> said:

    David> Hi. In the KW documentation for defrule, I read that
    David> "(<lisp-expr> <term>*) binds the result or results of calling <lisp-expr> to
    David> the <term> s with execution of the rule terminating if any bindings fail (if
    David> no <term> s are given execution will always continue)."

    David> I read this to mean that if <lisp-expr> returns NIL for any of the bindings,
    David> then this rule's execution will stop. That's what I want, but apparently I
    David> misunderstand the doc. Given this scenario:

    David> (defrule frodo
    David>   :forward
    David>   (hobbit ?hobbit)
    --> 
    David>   ((find-frodo ?hobbit) ?frodo)
    David>   ((format t "Rule FRODO fired!~%")))

    David> I had hoped that if FIND-FRODO returned NIL, the ?frodo binding would fail
    David> and execution of this rule's rhs would not continue. However, that isn't the
    David> case; execution continues regardless, so I clearly don't understand.

    David> Please offer clarification if you can. Thanks...

If you use the above form:

   ((find-frodo ?hobbit) ?frodo)

Then the result of the call to FIND-FRODO is unified (Prolog style)
with the variable ?frodo. If you have not previously bound ?FRODO to
anything then as an unbound variable it will unify with anything
including NIL.

The doc you quote has says nothing about the NIL case as it is no
different from any other value. The only special case the doc mentions
is:

   ((find-frodo ?hobbit))

This is given nothing to unify with and so always succeeds. This form
is mainly used for side-effects.

If you want to be sure you have found a value then you could say
something like:

   ((find-frodo ?hobbit) ?frodo)
   (test ?frodo)

However, in this kind of example I would have expected that Frodoness
is something that can be determined either declaratively or
heuristically within the LHS.

__Jason


Updated at: 2020-12-10 08:57 UTC