http://www.perlmonks.org?node_id=191060

belkajm sent me a great bit of code to handle the unification problem in AI::Perlog, of which you can download the latest copy at this link. Unfortunately, belkajm didn't make his post a root node, which I think it should have been because it was a very well-explained solution to the problem. Go vote for it :)

To show how interesting this is (to me, at least), consider the problem listed in Who's a thief?. The code I have below solves the listed problem. You can compare this to code in AI in Perl - proof of concept. That code is much clumsier, less scalable, and not as easy to modify.

Also, I want to take a closer look at the code that arhuman put together in AI::Perlog(2) unification proposition and an explanatory follow-up. It has some interesting features that I hadn't considered, but also haven't had the time to delve into. All in all, I have to say "thanks" to both arhuman and belkajm for their help and am looking forward to seeing more.

Enjoy!

#!/usr/bin/perl -w use strict; use AI::Perlog; my $pg = AI::Perlog->new; load_data(); my $acts_of_horror = who_steals_from_whom(); unless ( @$acts_of_horror ) { print "Looks like everybody's safe\n"; } else { foreach my $crime ( @$acts_of_horror ) { my ( $thief, $victim ) = @$crime; print "$thief will steal from $victim.\n"; } } exit; sub who_steals_from_whom { my $thieves = $pg->thief( '$person' ); my $victims = $pg->owns( qw/ $person _ / ); my @crimes; foreach my $victim ( @$victims ) { my $curr_victim = $victim->{person}; foreach my $thief ( @$thieves ) { my $curr_thief = $thief->{person}; if (rich($curr_victim) and ! $pg->knows($curr_thief,$curr_ +victim)){ push @crimes => [$curr_thief, $curr_victim]; } } } return \@crimes; } sub rich { my $person = shift; my $items = $pg->owns( $person, '$item' ); local $_; foreach ( @$items ) { return 1 if $pg->valuable( $_->{ item } ); } return; } sub load_data { while ( <DATA> ) { chomp; next if ( ! $_ or substr( $_, 0, 1 ) eq '#' ); my @fact = split /\t/; $pg->add_fact( @fact ); } } __DATA__ owns merlyn gold owns Ovid pocket lint owns Ovid cheap whiskey owns kudra domination fund valuable gold valuable domination fund thief badguy thief badgirl # :) knows badgirl merlyn

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.