Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Clojure HTML

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.



A Logic Programming library for Clojure. At its heart is an original implementation of miniKanren as described in William Byrd’s dissertation Relational Programming in miniKanren: Techniques, Applications, and Implementations. It’s also described in great detail in the The Reasoned Schemer. However, do note that the version that appears in The Reasoned Schemer is an earlier implementation and differs from the one on which this library is based.

Performance is a central concern of this project. Anything that makes it slower will probably not be adopted. Anything that makes it faster without overly complicating the implementation will be considered. It would be interesting to see how we fare on the standard Prolog benchmarks. Currently, on my machine, solving the classic Zebra puzzle 1000 times takes SWI-Prolog about 6 seconds, it takes logos.minikanren ~2.1s without occurs-check.


A classic AI program:

(ns bratko-logos.monkey-banana
  (:require [logos.minikanren :as mk]
            [logos.match :as m]))

(m/defne moveo [before action after]
  ([[:middle :onbox :middle :hasnot]
    [:middle :onbox :middle :has]])
  ([[?pos :onfloor ?pos ?has]
    [?pos :onbox ?pos ?has]])
  ([[?pos1 :onfloor ?pos1 ?has]
    [?pos2 :onfloor ?pos2 ?has]])
  ([[?pos1 :onfloor ?box ?has]
    [?pos2 :onfloor ?box ?has]]))

(m/defne cangeto [state out]
  ([[_ _ _ :has] true])
  ([_ _] (mk/exist [action next]
                   (moveo state action next)
                   (cangeto next out))))

(mk/run 1 [q]
      (cangeto [:atdoor :onfloor :atwindow :hasnot] q)) ; (true)

A classic Prolog program:

append ([] ,Y,Y).
append([A|D],Y2,[A|R]) :− append(D,Y2,R).

The Logos version is almost equally succinct:

(defne appendo [x y z]
    ([() _ y])
    ([[?a . ?d] _ [?a . ?r]] (appendo ?d y ?r)))


Logos as of version 0.5.4 supports tabling. Certain kinds of logic programs that would not terminate in Prolog will terminate in Logos if you create a tabled goal.

(defne arco [x y]
  ([:a :b])
  ([:b :a])
  ([:b :d]))

(def patho
  (tabled [x y]
     ((arco x y))
     ((exist [z]
        (arco x z)
        (patho z y))))))

;; (:b :a :d)
(run* [q] (patho :a q))


Logos supports disequality constraints.

(run* [q]
  (exist [x y]
    (!= [x 2] [y 1])
    (== x 1)
    (== y 3)
    (== q [x y]))) ; ([1 3])

(run* [q]
  (exist [x y]
    (!= [x 2] [y 1])
    (== x 1)
    (== y 2)
    (== q [x y]))) ; ()


Logos comes with a unifier that can be used much like core.unify:

(unifier' '(?x ?y ?z) '(1 2 ?y)) ; (1 2 _.0)

The above is quite slow since we have to walk the data structure and replace the logic var symbols. It’s more efficient to prep the expressions before hand if you’re going to be unifying the same expressions over and over again.

(let [[u w] (map prep ['(?x ?y) (1 2)])]
  (unifier u w))

Defining facts

Sometimes it’s useful to create a list of facts that you want to run queries over. Use defrel and fact. Facts are just tuples and Logos will index them by the first element.

(defrel man p)
(fact man 'Bob)
(fact man 'John)
(fact man 'Ricky)

(defrel woman p)
(fact woman 'Mary)
(fact woman 'Martha)
(fact woman 'Lucy)
(defrel likes p1 p2)
(fact likes 'Bob 'Mary)
(fact likes 'John 'Martha)
(fact likes 'Ricky 'Lucy)

(defrel fun p)
(fact fun 'Lucy)

(run* [q]
  (exist [x y]
    (fun y)
    (likes x y)
    (== q [x y]))) ; ([Ricky Lucy])


This library is under heavy development as I cover the ideas in Byrd’s thesis and other sources on logic programming. It currently only supports the Clojure 1.3.0 alphas.

This is not the first implementation of miniKanren in Clojure. Jim Duey’s version can be found here. His work on that got me interested in logic programming in the first place.


The core Prolog aspect of Logos is nearing completion. The following are tentative future directions:

  • Negation – Stratified Negation as provided by XSB ?
  • Constraint Logic Programming – Constraint Handling Rules (CHR) is particularly inspiring


YourKit has has given me a free license for their profiler, greatly simplifying the profiling of Logos performance.

YourKit is kindly supporting open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of innovative and intelligent tools for profiling Java and .NET applications. Take a look at YourKit’s leading software products:


I stayed pretty true to the ideas of the original implementation. There are however several key differences. Unification uses protocols in order leverage the full speed of the host. Clojure’s cons operator differs significantly from Scheme’s so I added the LConsSeq protocol. Sequences which end in a logic variables can be represented by using lcons

(lcons 'a (lvar 'b)) ; (a . <lvar:b>)

Logic variables are instances of LVar not vectors as they are in Scheme.

The goal and goal constructor portion has been written from scratch on top of the protocols and makes liberal use of lazy sequences to prevent stack overflow.

Currently the Substitutions deftype uses clojure.lang.PersistentHashMap internally. This may be replaced with something that provides better performance for triangular substitutions.


  • Simplicity. Optimizations should not destroy the ideas behind the original design. Any person willing to take take the time to understand the original Scheme implementation should have little trouble understanding how Logos is put together.
  • Performance. This implementation is faster than miniKanren under Racket and seems to be close to performnace of miniKanren recorded by the original designers when running under Chez Scheme.
  • Emphasis on pure relational programming.


I will only take contributions from people who have submitted Clojure CAs.

Interesting Questions

  • Can we avoid unification? Instead use map/filter when possible which are 10X faster?
  • Compilation to pure optimized Clojure ?
  • Compilation to byte-code ?
  • Fork/join ?


Copyright © 2010 David Nolen

Distributed under the Eclipse Public License, the same as Clojure.

Something went wrong with that request. Please try again.