Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From defrel and facts to core.logic.pldb

Would appreciate any help on how this code should be changed with regards to the deprecation of defrel and facts and the move to pldb?

Here's the code:

(defrel parent x y)
(facts parent ’[[dave kaylen]
                [frank dave]])

(defn grandparent
    [x y]
    (fresh [z]
        (parent x z)
        (parent z y)))

;; In the REPL
user> (run* [q]
          (fresh [x y]
              (grandparent x y)
              (== q [x y])))
;; Result
([frank kaylen])    
like image 941
pkothbauer Avatar asked Dec 19 '22 14:12

pkothbauer


1 Answers

(ns your.ns.here
  (:require [clojure.core.logic.pldb :as pldb]
            [clojure.core.logic :refer :all]))

(pldb/db-rel parent p1 p2)

(def facts
  (pldb/db
    [parent 'dave 'kaylen]
    [parent 'frank 'dave]))

(defn grandparent
  [x y]
  (fresh [z]
         (parent x z)
         (parent z y)))

(pldb/with-db facts
              (doall (run* [q]
                           (fresh [x y]
                                  (grandparent x y)
                                  (== q [x y])))))
=> ([frank kaylen])

See the pldb tests from core.logic source for more examples

like image 165
NielsK Avatar answered Jan 14 '23 10:01

NielsK