[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Prolog like facilities.

John Carter

11/4/2003 11:51:00 PM

2 Answers

Fred Werne

11/5/2003 9:25:00 PM

0

John Carter schrieb:
> Anybody have any Prolog alike goal finding / backtracking type ruby
> module available? (Couldn't spot any on RAA or rubyforge.)

I'm interessted in a framework for backtracking in ruby, too.
I think, using the Regexp Class an their methods will help, solving this
problem in a general way.

Fred from Wuppertal, Germany

avi

11/6/2003 3:51:00 AM

0

John Carter <john.carter@tait.co.nz> wrote in message news:<Pine.LNX.4.58.0311051245420.24475@parore.tait.co.nz>...

> Anybody have any Prolog alike goal finding / backtracking type ruby
> module available? (Couldn't spot any on RAA or rubyforge.)

This isn't really prolog alike, but it's a nice simple backtracking
facility. To test it, try this:

amb = Amb.new
x = amb.one_of(1..10)
y = amb.one_of(1..10)
amb.assert(x + y == 7)
amb.assert(x - y == 1)
puts [x,y] #=> 4, 3

Or, say, this:

if amb.maybe
x = 1
y = 3
else
x = 7
y = 10
end

amb.assert(x > 1)
puts y #=> 10

amb.assert(y < 10) #=> error

To read more about it, search for "john mccarthy's amb" or see
http://gd.tuwien.ac.at/languages/scheme/tutorial-dsitaram/t-y-scheme-Z...

Cheers,
Avi

-----------------
#amb.rb
#Avi Bryant, Nov. 2003

class Amb
def initialize
@failureContinuation = proc{raise "Amb goal tree exhausted"}
end

def fail
@failureContinuation.call(nil)
end

def assert(bool)
fail unless bool
end

def choose(procArray)
kPrev = @failureContinuation
callcc do |kEntry|
procArray.each do |p|
callcc do |kNext|
@failureContinuation = proc do |v|
@failureContinuation = kPrev
kNext.call(v)
end
kEntry.call(p.call)
end
end
kPrev.call(nil)
end
end

def one_of(enumerable)
choose(enumerable.collect{|ea| proc{ea}})
end

def maybe
one_of([true, false])
end
end