Use Prolog from Clojure via Projog.
This library provides a convenient Clojure wrapper to JVM Prolog implementation. Clojure data and select Java classes are roundtripped to Prolog terms and back.
Query can be passed in as a Clojure vector or a Prolog string.
See clj-prolog.core
namespace and tests for more examples..
Queries can be Clojure vectors:
(with-prolog
(consult-string "mortal(X) :- human(X). human(socrates).")
(q [:mortal :Name])) ;; => ({:Name :socrates})
Or Prolog source:
(with-prolog
(q "between(1,7,X), 0 is X mod 2.")) ;; => ({:X 2} {:X 4} {:X 6})
Conjunction and disjunction work in vectors as well:
(with-prolog
(q [:and
[:between 1 100 :X]
[:or
[:=< :X 2]
[:>= :X 99]]])) ;; => ({:X 1} {:X 2} {:X 99} {:X 100})
Type mapping from Clojure data to Prolog terms is made to be convenient to use from Clojure.
Keywords are mapped to Prolog atoms and vectors to compound terms. Some compound terms are reserved for mapping different Clojure or Java types.
Note if the first element of a vector is a keyword, it is turned into a compound term, otherwise a list.
Clojure/Java type | Prolog term type | Example |
---|---|---|
keyword | atom/variable | :foo => foo |
long | long | 42 => 42 |
double | double | 6.66 => 6.66 |
vector | compound term | [:foo 123 :bar] => foo(123,bar) |
list | list | (list 1 2 3) => [1,2,3] |
string | compound term | "Hello" => string(Hello) |
map | compound term | {:foo 42 :bar "something"} => map([foo-42,bar-string(something)]) |
Date | compound term | #inst "2024-05-09T05:08:58.458-00:00" => timestamp(1715231338458) |
LocalDate | compound term | (LocalDate/of 2024 5 9) => date(2024, 5, 9) |
LocalTime | compound term | (LocalTime/parse "10:45") => time(10,45,0,0) |
LocalDateTime | compound term | (LocalDateTime/of (LocalDate/of 2024 5 9) (LocalTime/of 10 45)) => datetime(date(2024,5,9),time(10,45,0,0)) |
MIT for this wrapper code. Projog implementation is Apache-2.0.
- Passthrough CLP variables in clj/prolog conversion
- Convert empty Prolog list back to
nil
in Clojure - Map keyword beginning with uppercase to variable
- Add sudoku solver CLP test
- Add helper for single value producing function as predicate
- Initial implementation work, added type mappings
- Basic consultation of files and strings and queries work