The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Logic programming in Ruby: a tiny prolog interpreter and symbolic computation

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
Logic programming in Ruby: a tiny prolog interpreter and symbolic computation Posted: Oct 28, 2006 12:11 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: Logic programming in Ruby: a tiny prolog interpreter and symbolic computation
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

Want some logic programming without leaving the comfort of your irb session?

I've taken the interpreter I found in a couple articles 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"].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"]

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

and rules with

predicate[:VAR1, :VAR2] <<= [ other[:VAR1], predicates[:VAR2] ]

meaning that predicate[:VAR1,:VAR2] holds if both other[:VAR1] and predicates[:VAR2] do.


Read more...

Read: Logic programming in Ruby: a tiny prolog interpreter and symbolic computation

Topic: RubyConf 2006 Previous Topic   Next Topic Topic: RubyOSA 0.1.0

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use