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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|