core-logic

xtreak29 2017-10-23T09:13:26.000375Z

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 ?

xtreak29 2017-10-23T09:37:09.000328Z

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 ?

2017-10-23T13:37:02.000429Z

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.

2017-10-23T13:37:17.000528Z

If this is easily double you’d need:

2017-10-23T13:37:39.000376Z

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

2017-10-23T13:37:55.000072Z

given the provided facts

2017-10-23T13:39:04.000113Z

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

2017-10-23T13:40:41.000136Z

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

xtreak29 2017-10-23T14:34:36.000548Z

@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)

2017-10-23T14:49:19.000379Z

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