Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; Backward Chaining
- (ns backward)
- (import '(java.util.concurrent Executors ExecutorCompletionService))
- (declare start solve-goal solve-rule collect unify)
- ; Choose between using parallel collect or sequential collect.
- (def use-parallel-collect true)
- (def completion-service (ExecutorCompletionService.
- (Executors/newCachedThreadPool)))
- (defn start []
- (print "Enter query: ") (flush)
- (let [query (read-line)
- truth (solve-goal (symbol query))]
- (printf "Answer is: %s\n" truth)))
- (defn fetch-rules [goal]
- (case goal
- g '[[a,b,c],[d,e,f]]
- a '[[]]
- b '[[]]
- c '[[]]
- d '[[]]
- e '[[]]
- f '[[]]
- true (throw (Exception. "No applicable rules"))))
- (defn solve-goal [goal]
- (let [rules (fetch-rules goal)]
- (if (some empty? rules)
- 1.0 ; return a truth value
- (do
- (doseq [rule rules]
- (.submit completion-service (solve-rule rule)))
- (.get (.take completion-service))))))
- ; Move minimization logic here from collect.
- (defn solve-rule [rule-tail]
- (let [truths (collect solve-goal rule-tail)]
- (apply min truths)))
- ; Accumulate results
- (def collect (if use-parallel-collect pmap map))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement