;; bottles1.clp

;; Solution based on rule firing
;;
(deffunction bottles ( ?X )
  (reset)
  (assert (stock ?X))
  (run))

(defrule empty-stock ""
  (declare (salience 10))
  ?f <- (stock 0)
  =>
  (printout t "Now, they are all gone." crlf)
  (retract ?f)
  )

(defrule bottle ""
  (declare (salience 10))
  (stock ?in)
  (test (> ?in 0))
  =>
  (if (> ?in 1)
    then
    (printout t ?in " bottles of beer on the wall. ")
    else
    (printout t "1 bottle of beer on the wall. "))
  )

(defrule take_one ""
  ?f <- (stock ?in)
  =>
  (printout t "Take one down, and pass it around." crlf)
  (retract ?f)
  (bind ?out (- ?in 1))
  (assert (stock ?out))
  )
