Fork me on GitHub
#core-logic
<
2017-10-23
>
xtreak2909:10:26

How can I get all the successions in the conde elements as below I need to store the values of z as a list. Similar to https://en.wikibooks.org/wiki/Prolog/Recursive_Rules where all the ancestors are listed

(defn route [x y]
  (conde
   [(c x y)]
   [(fresh [z]
      (c x z)
      (route z y))]))

(def facts
  (pldb/db
   [c 'a 'b]
   [c 'a 'x]
   [c 'b 'c]
   [c 'c 'd]
   [c 'd 'e]))

(pldb/with-db facts
                 (run* [q]
                   (fresh [x y]
                     (== x 'a)
                     (== y 'e)
                     (route x y)
                     (== q  (route x y)))))
I need the result as [[a b][b c][c d][d e]] . Is it possible with core.logic ?

xtreak2909:10:09

It seems it's not possible with core.logic : https://stackoverflow.com/questions/12563351/listing-unique-dag-parents-with-core-logic?rq=1 . Can someone explain the answer where David Nolen points out passing it to set constructor ?

norman13:10:02

I’d have to think some, but a couple notes: First, goals are not functions that return values. (== q (route x y)) is nonsensical on core.logic.

norman13:10:17

If this is easily double you’d need:

norman13:10:39

(route r x y) which asserts r is a route between x and y

norman13:10:55

given the provided facts

norman13:10:04

Then you’d assert (route q ’a ’e)

norman13:10:41

Otherwise you can’t generate the path, only the fact that a path exists

xtreak2914:10:36

@norman Thanks I am currently a newbie to core.logic and still trying to grasp it. I was trying to convert the Prolog examples of recursive relations to core.logic and also found the definition of relations using functions at https://github.com/swannodette/logic-tutorial/blob/master/src/logic_tutorial/tut1.clj#L18 and thought it's right to use functions as goals. The closest I have got to working is as below :

(run-db* facts [q]
                        (fresh [x]
                          (== x 'a)
                          (route x q))) ;; (x b c d e)

norman14:10:19

Yes, that route is just a fact that asserts a route exists