Giter Site home page Giter Site logo

clj-prolog's Introduction

clj-prolog: Clojure interface to Prolog

test workflow

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.

Usage

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

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

Licence

MIT for this wrapper code. Projog implementation is Apache-2.0.

Changes

2024-05-10

  • 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

2024-05-09

  • Initial implementation work, added type mappings
  • Basic consultation of files and strings and queries work

clj-prolog's People

Contributors

tatut avatar

Stargazers

Zachary Teo avatar Lee Won Jun avatar Tamizhvendan S avatar Cam Peterson avatar  avatar Matus Kmit avatar Paula Gearon avatar Bennet Carpenter avatar Tuuli Mutkala avatar Muhammad Ridho avatar Jorge Fernando Gómez avatar Jeff Carpenter avatar Mark Stuart avatar Tommi Reiman avatar Dan avatar Dominic Monroe avatar  avatar Nikola Vojičić avatar Kimmo Koskinen avatar Renier Strydom avatar Simon Gray avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.