SHOW:
|
|
- or go back to the newest paste.
| 1 | ;; Backward Chaining | |
| 2 | ||
| 3 | (ns backward) | |
| 4 | (import '(java.util.concurrent Executors ExecutorCompletionService)) | |
| 5 | ||
| 6 | (declare start solve-goal solve-rule collect unify) | |
| 7 | ||
| 8 | ; Choose between using parallel collect or sequential collect. | |
| 9 | (def use-parallel-collect true) | |
| 10 | ||
| 11 | (def completion-service (ExecutorCompletionService. | |
| 12 | (Executors/newCachedThreadPool))) | |
| 13 | ||
| 14 | (defn start [] | |
| 15 | (print "Enter query: ") (flush) | |
| 16 | (let [query (read-line) | |
| 17 | truth (solve-goal (symbol query))] | |
| 18 | (printf "Answer is: %s\n" truth))) | |
| 19 | ||
| 20 | (defn fetch-rules [goal] | |
| 21 | (case goal | |
| 22 | g '[[a,b,c],[d,e,f]] | |
| 23 | a '[[]] | |
| 24 | b '[[]] | |
| 25 | c '[[]] | |
| 26 | d '[[]] | |
| 27 | e '[[]] | |
| 28 | f '[[]] | |
| 29 | - | true (throw (Exception. "No applicable rules")))) |
| 29 | + | true (throw (Exception. "No applicable rule")))) |
| 30 | ||
| 31 | (defn solve-goal [goal] | |
| 32 | (let [rules (fetch-rules goal)] | |
| 33 | (if (some empty? rules) | |
| 34 | 1.0 ; return a truth value | |
| 35 | (do | |
| 36 | (doseq [rule rules] | |
| 37 | - | (.submit completion-service (solve-rule rule))) |
| 37 | + | (.submit completion-service #(solve-rule rule))) |
| 38 | (.get (.take completion-service)))))) | |
| 39 | ||
| 40 | ; Move minimization logic here from collect. | |
| 41 | (defn solve-rule [rule-tail] | |
| 42 | (let [truths (collect solve-goal rule-tail)] | |
| 43 | (apply min truths))) | |
| 44 | ||
| 45 | ; Accumulate results | |
| 46 | (def collect (if use-parallel-collect pmap map)) |