Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

So, after asking how one would program Who's a thief? in Perl, I decided to take a stab at it. Eventually, I plan to make this a more general module, but for now, this is a proof of concept. First, let's change the problem. I want to know who will steal from whom. Here's the Prolog (see the above link for an explanation, if it's not clear):

owns( merlyn, gold ). owns( ovid, pocketLint ). owns( ovid, cheapWhiskey ). owns( kudra, dominationFund ). valuable( gold ). valuable( dominationFund ). thief( badguy ). rich( X ) :- owns( X, Y ), valuable( Y ). steals_from( X, Y ) :- thief( X ), rich( Y ).

The last rule states that X will steal from Y if X is a thief and Y is rich.

To create this, I first created a %facts hash and added the 'owns', 'valuable', and 'thief' facts to it. Then, I created the rules for steals_from and rich. Asking the question, who will the badguy steal from becomes as simple as steals_from( $badguy, $victim ).

The code is pretty much a hack, but it's a start. This is not nearly as elegant as Abigail-II's regex solution for the n-queens problem, but I think it's a bit easier to understand.

Run the code below and it prints the following:

badguy will steal from merlyn badguy will not steal from ovid badguy will steal from kudra
#!/usr/bin/perl -w use strict; my %facts; # poor ( X ) :- # not rich( X ). my @owns = ( 2, merlyn => 'gold', ovid => 'pocket lint', ovid => 'cheap whiskey', kudra => 'domination fund' ); my @valuable = ( 1, 'gold', 'domination fund' ); my @thief = ( 1, 'badguy' ); $facts{ owns } = add_fact( \@owns ); $facts{ valuable } = add_fact( \@valuable ); $facts{ thief } = add_fact( \@thief ); foreach ( qw/ merlyn ovid kudra / ) { my $modifier = steals_from( 'badguy', $_ ) ? '' : ' not'; print "badguy will$modifier steal from $_\n"; } sub steals_from { # steals_from( X, Y ) :- # thief( X ), # rich( Y ). my ( $perp, $victim ) = @_; return thief( $perp ) && rich( $victim ); } sub thief { my $perp = shift; return grep { $_->[0] eq $perp } @{$facts{thief}}; } sub rich { # rich ( X ) :- # owns( X, Y ), # valuable( Y ). my $person = shift; my @results; my @items = map { $_->[1] } grep { $_->[0] eq $person } @{$facts{owns}}; foreach my $item ( @items ) { push @results => $item if grep { $_->[0] eq $item } @{$facts{valuable}}; } return @results ? 1 : 0; } sub add_fact { my $rule = shift; my $args = shift @$rule; my @rule; for ( my $i = 0; $i < @$rule; $i = $i + $args ) { my @args = @$rule[ $i .. $i + ( $args - 1 ) ]; push @rule => \@args; } return \@rule; }

Incidentally, for a hint on how Abigail-II' regex works, print the regex and look at the lines which resemble the following:

(?(?{attack $q [3], $q [4]})x|)

Basically, it says if one queen can attack another, match 'x' to the target string, otherwise, match the empty string. Since the target string is the empty string, this forces the backtracking. All in all, a very elegant bit of code. While my code does not even remotely resemble that beauty, I am considering adding regex backtracking support to mine in the future.


Update: Fixed typo that Courage pointed out.

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

In reply to AI in Perl - proof of concept by Ovid

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2019-07-16 19:05 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found