Skip to content

replikativ/datalog-parser

Repository files navigation

datalog-parser

A Datalog parser. This parser is used by Datahike and follows the Datalog dialect of Datomic.

Note: This repository has been moved from the lambdaforge organization to replikativ. So, you will find older releases of the parser at the lambdaforge clojars page.

Usage

Add the current release of org.replikativ/datalog-parser to your deps.edn:

org.replikativ/datalog-parser {:mvn/version "0.2.XX"}

Parsing

(require '[datalog.parser :as parser])

(parser/parse '[:find ?x :in $ ?y :where [?x :z ?y]])

;;=> (namespaces omitted for brevity)
;; #Query{:qfind  #FindRel{:elements [#Variable{:symbol ?x}]}
;;        :qwith  nil
;;        :qin    [#BindScalar{:variable #SrcVar{:symbol $}}
;;                 #BindScalar{:variable #Variable{:symbol ?y}}]
;;        :qwhere [#Pattern{:source #DefaultSrc{}
;;                          :pattern [#Variable{:symbol ?x}
;;                                    #Constant{:value  :z}
;;                                    #Variable{:symbol ?y}]}]}

Unparsing

Convert parsed query records back into the query DSL. This enables programmatic query modification:

(require '[datalog.unparser :as unparser])

;; Round-trip: parse, modify, unparse
(let [parsed (parser/parse '[:find ?x :in $ :where [?x :name "Ivan"]])]
  (unparser/unparse parsed))
;;=> [:find ?x :in $ :where [?x :name "Ivan"]]

;; Rules round-trip
(let [rules (parser/parse-rules '[[(ancestor ?e1 ?e2) [?e1 :parent ?e2]]
                                  [(ancestor ?e1 ?e2) [?e1 :parent ?t]
                                   (ancestor ?t ?e2)]])]
  (unparser/unparse-rules rules))
;;=> [[(ancestor ?e1 ?e2) [?e1 :parent ?e2]]
;;    [(ancestor ?e1 ?e2) [?e1 :parent ?t] (ancestor ?t ?e2)]]

For more examples look at the tests.

License

Copyright © 2020-2026 Christian Weilbach et al.

This program and the accompanying materials are made available under the terms of the Eclipse Public License 1.0.

About

Generic datalog parser compliant to datomic, datascript and datahike queries.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors