pseudofish

powered by text files

clojure

Prolog in Clojure

After reading Peter Novig’s classic, Paradigms of AI Programming, I decided to try porting his implementation of Prolog in Lisp to Clojure.

The initial results are available on GitHub.

The interpreter allows solving of simple logic problems:

(<- (witch ?x) (burns ?x) (female ?x))
(<- (burns ?x) (wooden ?x))
(<- (wooden ?x) (floats ?x))
(<- (floats ?x) (sameweight Duck ?x))
(<- (female Girl))
(<- (sameweight Duck Girl))

(?- (witch Girl))

Porting this turned out to be an interesting challenge. There were a few Lisp functions that weren’t available in Clojure, along with some subtle changes in idiom.

This gave me an opportunity to dive further into Clojure and think about how to implement the algorithms and data structures in a way that made sense, yet was still close to the original.

There is still much to do, as the book further develops the interpreter with user interaction and then refactors it into a compiler. Should keep me busy for a while longer!

Note: Please don’t consider this production code. If you want to look deeper at logic programming, consider either a real Prolog implementation, the core.logic library in Clojure or check out Mercury.