Want some logic programming without leaving the comfort of your irb session?
I've taken the interpreter I found in a
couplearticles in Japanese, added
syntactic sugar and some new predicates. It's quite powerful now; you can find
below some logical programs, including symbolic differentiation and
simplification.
If you want to play along, you're going to need my modified
tiny_prolog.rb and a few new predicates defined in
tiny_prolog_ext.rb.
Syntax
Logical code looks like this with the tiny Prolog-ish interpreter in Ruby:
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"].factmother["Trude","Sally"].factfather["Tom","Sally"].factfather["Tom","Erica"].factfather["Mike","Tom"].factquerysibling[:X,"Sally"]# >> 1 sibling["Erica", "Sally"]
I've added some sugar to the original interpreter, so there's no need to
predeclare the predicates, and rules/facts can be specified more naturally.
So you basically define facts with
predicate[1,:X].fact # predicate[1,:X] holds for any X
predicate[2, 1].fact # predicate[2,1] holds