Because I've moved, I don't yet have 'net access at home and it appears that, for varying reasons, getting it is particularly annoying. As a result, I've been doing some work at home, but not uploading it to the CPAN or github. Basically:
package My::Database; use AI::Logic::Database variables => [qw{Person}], predicates => [qw{male/1 female/1 married/2 husband/1}]; male { 'frank' }; male { 'tony' }; male { 'bill' }; male { 'tom' }; male { 'alvin' }; female { 'sally' }; female { 'sarah' }: married { 'frank', 'sarah' }; married { 'tom', 'alvin' }; Rule { husband { Person } => male { Person }, married { Person, Any }; }
The syntax looks strange, but if you know Prolog, the above should look strange, but be familiar. To query it, issue queries with callbacks to handle successful queries:
use AI::Logic 'My::Database'; my $frank = Var 'frank'; male($frank, sub { print 'frank is male' }); my @males; # get all males my $male = Var; male($male, sub { push @males => $male->value }); my $is = 'is not'; my $name = get_some_guys_name(); my $husband = Var $name; husband($husband, sub { $is = 'is' }); print "$name $is_husband a husband\n"'
Some of you may have noticed that 'alvin' will never be listed as a husband, but that's because I don't have disjunction ('or') figured out. I know how to do it, but I don't know the right syntax to make it obvious. For the time being, adding an additional rule solves this case (and is logically equivalent).
# handle the case of 'alvin' Rule { husband { Person } => male { Person }, married { Any, Person }; }
The cut operator (!) should be trivial to implement, but it will be the Cut keyword, and lists will eventually be added, at which point the system might even be vaguely complete. I doubt it will be fast, but I suspect it will be a heck of a lot faster than AI::Prolog. If I'm really, really lucky, it might even be fast enough for some production work (I've a few optimizations in mind, but I've no intention of implementing any until I have more work done on this).
This is a generalization of Adrian Howard's continuation-based logic solver.
Look forward to seeing this on github/CPAN
Re:Shiny... wants code!
Ovid on 2009-06-05T14:18:24
I might be able to get it out there this weekend as my girlfriend's flat has internet, but I'll be pretty busy running around London handling errands.