Skip to content

Commit

Permalink
* src/main/clojure/clojure/core/logic/bench.clj: remove ?var from exa…
Browse files Browse the repository at this point in the history
…mples and documentation
  • Loading branch information
swannodette committed Nov 28, 2011
1 parent 428f9f2 commit b1ab2db
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 70 deletions.
38 changes: 19 additions & 19 deletions README.md
Expand Up @@ -41,15 +41,15 @@ A classic AI program:
([[:middle :onbox :middle :hasnot]
:grasp
[:middle :onbox :middle :has]])
([[?pos :onfloor ?pos ?has]
([[pos :onfloor pos has]
:climb
[?pos :onbox ?pos ?has]])
([[?pos1 :onfloor ?pos1 ?has]
[pos :onbox pos has]])
([[pos1 :onfloor pos1 has]
:push
[?pos2 :onfloor ?pos2 ?has]])
([[?pos1 :onfloor ?box ?has]
[pos2 :onfloor pos2 has]])
([[pos1 :onfloor box has]
:walk
[?pos2 :onfloor ?box ?has]]))
[pos2 :onfloor box has]]))

(defne cangeto [state out]
([[_ _ _ :has] true])
Expand All @@ -73,7 +73,7 @@ The core.logic version is almost equally succinct:
```clj
(defne appendo [x y z]
([() _ y])
([[?a . ?d] _ [?a . ?r]] (appendo ?d y ?r)))
([[a . d] _ [a . r]] (appendo d y r)))
```

Here's a simple type inferencer for the simply typed lambda calculus based on a version originally written in Prolog:
Expand All @@ -83,22 +83,22 @@ Here's a simple type inferencer for the simply typed lambda calculus based on a
(use 'clojure.core.logic)

(defna findo [x l o]
([_ [[?y :- o] . _] _]
(project [x ?y] (== (= x ?y) true)))
([_ [_ . ?c] _] (findo x ?c o)))
([_ [[y :- o] . _] _]
(project [x y] (== (= x y) true)))
([_ [_ . c] _] (findo x c o)))

(defn typedo [c x t]
(conda
[(lvaro x) (findo x c t)]
[(matche [c x t]
([_ [[?x] :>> ?a] [?s :> ?t]]
([_ [[y] :>> a] [s :> t]]
(fresh [l]
(conso [?x :- ?s] c l)
(typedo l ?a ?t)))
([_ [:apply ?a ?b] _]
(conso [y :- s] c l)
(typedo l a t)))
([_ [:apply a b] _]
(fresh [s]
(typedo c ?a [s :> t])
(typedo c ?b s))))]))
(typedo c a [s :> t])
(typedo c b s))))]))

(comment
;; ([_.0 :> _.1])
Expand Down Expand Up @@ -211,13 +211,13 @@ core.logic has Prolog-type DCG syntax for parsing:
([[:d 'a]] '[a]))

(def-->e noun-phrase [n]
([[:np ?d ?n]] (det ?d) (noun ?n)))
([[:np d n]] (det d) (noun n)))

(def-->e verb-phrase [n]
([[:vp ?v ?np]] (verb ?v) (noun-phrase ?np)))
([[:vp v np]] (verb v) (noun-phrase np)))

(def-->e sentence [s]
([[:s ?np ?vp]] (noun-phrase ?np) (verb-phrase ?vp)))
([[:s np vp]] (noun-phrase np) (verb-phrase vp)))

(run* [parse-tree]
(sentence parse-tree '[the bat eats a cat] []))
Expand Down
36 changes: 18 additions & 18 deletions src/main/clojure/clojure/core/logic.clj
Expand Up @@ -1219,7 +1219,9 @@
:else p))

(defn- lvar-sym? [s]
(= (first (str s)) \?))
(and (symbol? s)
(not= s '.)
(not (contains? *locals* s))))

(defn- extract-vars
([p]
Expand Down Expand Up @@ -1280,7 +1282,7 @@

(defn- handle-clauses [t as cs]
`(~t
~@(map (handle-clause as) cs)))
~@(doall (map (handle-clause as) cs))))

;; name-with-attributes by Konrad Hinsen, from clojure.contrib.def
(defn- name-with-attributes
Expand Down Expand Up @@ -1312,9 +1314,10 @@

(defn- defnm [t n & rest]
(let [[n [as & cs]] (name-with-attributes n rest)]
(if-let [tabled? (-> n meta :tabled)]
`(def ~n (tabled [~@as] ~(handle-clauses t as cs)))
`(defn ~n [~@as] ~(handle-clauses t as cs)))))
(binding [*locals* (disj (set as) '_)]
(if-let [tabled? (-> n meta :tabled)]
`(def ~n (tabled [~@as] ~(handle-clauses t as cs)))
`(defn ~n [~@as] ~(handle-clauses t as cs))))))

;; =============================================================================
;; Useful goals
Expand Down Expand Up @@ -1354,14 +1357,13 @@
"Define a goal fn. Supports pattern matching. All
patterns will be tried. See conde."
[& rest]
(binding [*locals* (dissoc &env '_)]
(apply defnm `conde rest)))
(apply defnm `conde rest))

(defmacro matche
"Pattern matching macro. All patterns will be tried.
See conde."
[xs & cs]
(binding [*locals* (dissoc &env '_)]
(binding [*locals* (disj (set xs) '_)]
(handle-clauses `conde xs cs)))

;; -----------------------------------------------------------------------------
Expand All @@ -1373,25 +1375,23 @@
(defmacro defna
"Define a soft cut goal. See conda."
[& rest]
(binding [*locals* (dissoc &env '_)]
(apply defnm `conda rest)))
(apply defnm `conda rest))

(defmacro defnu
"Define a committed choice goal. See condu."
[& rest]
(binding [*locals* (dissoc &env '_)]
(apply defnm `condu rest)))
(apply defnm `condu rest))

(defmacro matcha
"Define a soft cut pattern match. See conda."
[xs & cs]
(binding [*locals* (dissoc &env '_)]
(binding [*locals* (disj (set xs) '_)]
(handle-clauses `conda xs cs)))

(defmacro matchu
"Define a committed choice goal. See condu."
[xs & cs]
(binding [*locals* (dissoc &env '_)]
(binding [*locals* (disj (set xs) '_)]
(handle-clauses `condu xs cs)))

;; ==============================================================================
Expand All @@ -1400,16 +1400,16 @@
(defne membero
"A relation where l is a collection, such that l contains x"
[x l]
([_ [x . ?tail]])
([_ [?head . ?tail]]
(membero x ?tail)))
([_ [x . tail]])
([_ [head . tail]]
(membero x tail)))

(defne appendo
"A relation where x, y, and z are proper collections,
such that z is x appended to y"
[x y z]
([() _ y])
([[?a . ?d] _ [?a . ?r]] (appendo ?d y ?r)))
([[a . d] _ [a . r]] (appendo d y r)))

;; =============================================================================
;; Rel
Expand Down
60 changes: 30 additions & 30 deletions src/main/clojure/clojure/core/logic/bench.clj
Expand Up @@ -34,10 +34,10 @@

(defne nrevo [l o]
([() ()])
([[?a . ?d] _]
([[a . d] _]
(fresh [r]
(nrevo ?d r)
(appendo r [?a] o))))
(nrevo d r)
(appendo r [a] o))))

(comment
;; we can run backwards, unlike Prolog
Expand All @@ -61,8 +61,8 @@
;; =============================================================================

(defne righto [x y l]
([_ _ [x y . ?r]])
([_ _ [_ . ?r]] (righto x y ?r)))
([_ _ [x y . r]])
([_ _ [_ . r]] (righto x y r)))

(defn nexto [x y l]
(conde
Expand Down Expand Up @@ -110,19 +110,19 @@

(defne nqueenso [l]
([()])
([[[?x ?y] . ?others]]
(nqueenso ?others)
(membero ?y [1 2 3 4 5 6 7 8])
(noattacko [?x ?y] ?others)))
([[[x y] . others]]
(nqueenso others)
(membero y [1 2 3 4 5 6 7 8])
(noattacko [x y] others)))

(defne noattacko [q others]
([_ ()])
([[?x ?y] [[?x1 ?y1] . ?others]]
(!= ?y ?y1)
(project [?y ?y1 ?x ?x1]
(!= (- ?y1 ?y) (- ?x1 ?x))
(!= (- ?y1 ?y) (- ?x ?x1)))
(noattacko [?x ?y] ?others)))
([[x y] [[x1 y1] . others]]
(!= y y1)
(project [y y1 x x1]
(!= (- y1 y) (- x1 x))
(!= (- y1 y) (- x x1)))
(noattacko [x y] others)))

(defn solve-nqueens []
(run* [q]
Expand Down Expand Up @@ -167,7 +167,7 @@

(defne takeouto [x l y]
([_ [x . y] _])
([_ [?h . ?t] [?h . ?r]] (takeouto x ?t ?r)))
([_ [h . t] [h . r]] (takeouto x t r)))

(defn digito [x l y]
(takeouto x l y))
Expand All @@ -178,7 +178,7 @@
(a/> x 0)))

(defne do-send-moolao [q l ll]
([[?send ?more ?money] _ _]
([[send more money] _ _]
(fresh [s e n d m o r y
l1 l2 l3 l4 l5 l6 l7 l8 l9]
(first-digito s l l1)
Expand All @@ -190,11 +190,11 @@
(digito r l6 l7)
(digito y l7 l8)
(project [s e n d m o r y]
(== ?send (+ (* s 1000) (* e 100) (* n 10) d))
(== ?more (+ (* m 1000) (* o 100) (* r 10) e))
(== ?money (+ (* m 10000) (* o 1000) (* n 100) (* e 10) y))
(project [?send ?more]
(== ?money (+ ?send ?more)))))))
(== send (+ (* s 1000) (* e 100) (* n 10) d))
(== more (+ (* m 1000) (* o 100) (* r 10) e))
(== money (+ (* m 10000) (* o 1000) (* n 100) (* e 10) y))
(project [send more]
(== money (+ send more)))))))

(defn send-money-quicklyo [send more money]
(fresh [l]
Expand All @@ -220,16 +220,16 @@

(defne qsorto [l r r0]
([[] _ r])
([[?x . ?lr] _ _]
([[x . lr] _ _]
(fresh [l1 l2 r1]
(partitiono ?lr ?x l1 l2)
(partitiono lr x l1 l2)
(qsorto l2 r1 r0)
(qsorto l1 r (lcons ?x r1)))))
(qsorto l1 r (lcons x r1)))))

(defne partitiono [a b c d]
([[?x . ?l] _ [?x . ?l1] _]
([[x . l] _ [x . l1] _]
(conda
((project [?x b]
(== (<= ?x b) true))
(partition ?l b ?l1 d))
(partition ?l b c d))))
((project [x b]
(== (<= x b) true))
(partition l b l1 d))
(partition l b c d))))
7 changes: 4 additions & 3 deletions src/test/clojure/clojure/core/logic/tests.clj
Expand Up @@ -1171,12 +1171,13 @@
(defne ^:tabled dummy
"Docstring"
[x l]
([_ [x . ?tail]])
([_ [?head . ?tail]]
(membero x ?tail)))
([_ [x . tail]])
([_ [head . tail]]
(membero x tail)))

(deftest test-metadata-defne
(is (= (-> #'dummy meta :tabled)
true))
(is (= (-> #'dummy meta :doc)
"Docstring")))

0 comments on commit b1ab2db

Please sign in to comment.