Backchaining Code
backchain works as follows: if the goal is known to be a fact, return true. Otherwise, try rules to see if some rule has the goal as conclusion and has premises that are true (using backchain).
(defn backchain [goal rules facts] ; true if (or (member goal facts) ; goal is known fact (some ; or there is some rule (fn [rule] ; that concludes (and (= (first rule) goal) ; goal (every? ; and every premise (fn [premise] ; can be proved (backchain premise rules facts)) (rest rule)))) rules)) )
>(backchain 'e '((c a b) (e c d)) '(a b d)) true
This form of backchaining is useful when there are relatively few rules but many facts, e.g. stored in a separate database.