Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

A Basic Prolog Implementation in Ruby

By Peter Cooper / November 2, 2006

Ruby genius Mauricio Fernandez has been playing with an attempt at processing Prolog using Ruby found on a Japanese coding site and has come up with tiny_prolog.rb.

require 'tiny_prolog_ext'

# rules
# read as "X and Y are siblings if Z is the parent of both"
sibling[:X,:Y] < <= [ parent[:Z,:X], parent[:Z,:Y], noteq[:X,:Y] ]
parent[:X,:Y] <<= father[:X,:Y]
parent[:X,:Y] <<= mother[:X,:Y]

# facts: rules with "no preconditions"
father["matz", "Ruby"].fact
mother["Trude", "Sally"].fact
father["Tom", "Sally"].fact
father["Tom", "Erica"].fact
father["Mike", "Tom"].fact

query sibling[:X, "Sally"]
# >> 1 sibling["Erica", "Sally"]

If you know what Prolog is, or you have an interest in micro-languages and Ruby, it's well worth a read as Mauricio looks into solving problems using logic that Prolog was designed for.


  1. Pat Eyler says:

    This is a language combination that has drawn looks from several people recently. It might be worth looking at: Jamis Buck's thoughts and Richard Dale's blog post for some additional ideas. Hopefully Maurcio, Jamis, and Richard can spend some time talking through their individual plans and come up with some really cool prolog in Ruby stuff.

  2. Pawel says:

    now, how do you write this in Lisp?

Other Posts to Enjoy

Twitter Mentions