Variable unbinding

34 views
Skip to first unread message

iancu denis

unread,
Jan 15, 2020, 9:09:34 PM1/15/20
to CLIPSESG
Let's say for example I have the following facts

(deffacts num
   
(number 4)
   
(number 5)
   
(number 9)
)

Create an empty variable

(bind ?numbers (create$)
)

And have the following rule to try to add each number to the variable

(defrule build-numbers
   
?number-fact<-(number ?c)
   
=>
   
(bind ?numbers (insert$ ?numbers 999 ?c))
   
(retract ?number-fact)
)

If I check before runing if the variable is properly bound by printing it, i get no error
(printout t   "Numbers "  ?numbers crlf
)

However when I run the example, I get the following error
[PRCCODE5] Variable numbers unbound.
[PRCCODE4] Execution halted during the actions of defrule build-numbers.
I notice the variable getting unbound after doing a (reset), so I bind it afterwards, but why does it also unbind when doing (run)? Is there a way around this?

CLIPS Support

unread,
Jan 16, 2020, 1:00:27 PM1/16/20
to CLIPSESG
Variables bound at the Read-Eval-Print-Loop (REPL) are local to the REPLL will not be in the scope of any code within other constructs. You can define a defglobal if you want to create a variable with global scope. In your code, rather than using a global variable, you could just create a fact for storing the numbers:

         CLIPS (6.31 6/12/19)
CLIPS> 
(deffacts num
    (number 4)
    (number 5)
    (number 9)
    (numbers)
)
CLIPS> 
(defrule build-numbers
    ?number-fact<-(number ?c)
    ?numbers <- (numbers $?n)
    =>
    (retract ?numbers ?number-fact)
    (assert (numbers ?n ?c)))
CLIPS> (reset)
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-7     (numbers 4 5 9)
For a total of 2 facts.
CLIPS> 
Reply all
Reply to author
Forward
0 new messages