Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

I am trying to model a graph using core.logic and would like to match all nodes that have edges to two other nodes. I have come up with the below but it returns two results instead of the desired one, as [2 3 4] and [2 4 3] are equivalent. How would I constrain the query to only return the desired result?

(use ' clojure.core.logic.pldb)

(db-rel edge a b)

(def g
  (db
   [edge 1 2]
   [edge 2 3]
   [edge 3 4]
   [edge 2 4]))

(with-db g
  (run* [q]
    (fresh [x y z]
      (edge x y)
      (edge x z)
      (!= y z)
      (== q [x y z]))))

1 Answer

+1 vote
by
user> (require '[clojure.core.logic.fd :as fd])
nil
user> (with-db g
  (run* [q]
    (fresh [x y z]
      (fd/in x y z (fd/interval 1 5))
      (edge x y)
      (edge x z)
      (fd/< y z)
      (== q [x y z]))))
([2 3 4])
...