Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Thursday January 13, 2005
11:49 PM

Regular expressions for arbitrary data structures

[ #22708 ]

OK, I finally got off my butt and checked out Language::Prolog::Yaswi. The docs need some work and it helps if you know Prolog, and if you use a 5.1 or 5.2 version of SWI-Prolog (compiled with thread support: ./configure --enable-mt) and a version of Perl compiled with ithreads. However, if you jump through all of those hoops, you can figure out who's a thief.

steals(PERP, STUFF) :-
    thief(PERP),
    valuable(STUFF),
    owns(VICTIM,STUFF),
    not(knows(PERP,VICTIM)).

thief(badguy).
valuable(gold).
valuable(rubies).
owns(merlyn,gold).
owns(ovid,rubies).
knows(badguy,merlyn).

In short, I finally admitted defeat. Implementing a predicate logic engine in pure Perl that is easy to use and extensible involves understanding streams, patterns, a pattern matcher and generating arbitrary streams from complex data structure and handling unification without the benefit of tail call recursion optimizations. In short, to fully enjoy the First Order Predicate Calculus in pure Perl involves writing an NFA regex engine that operates on data structures instead of strings.

I am not that smart.

I went ahead and used the SWI-Prolog version. And it works :)

#!/usr/local/bin/perl
use strict;
use warnings;

use Language::Prolog::Yaswi qw/:query :assert/;
use Language::Prolog::Types::overload;
use Language::Prolog::Sugar
    functors => [qw/steals thief valuable owns knows/],
    functors => { NOT => 'not' }, # bugfix:  This was in chains
    vars     => [qw/PERP STUFF VICTIM/],
    chains   => {
        AND => ',',
    };

swi_assert(
    steals(PERP, STUFF) =>
        AND(
            valuable(STUFF),
            thief(PERP),
            owns(VICTIM, STUFF),
            NOT(knows(PERP, VICTIM)),
        ));

swi_facts(
    thief('badguy'),
    valuable('gold'),
    valuable('rubies'),
    owns(qw/merlyn gold/),
    owns(qw/ovid rubies/),
    knows(qw/badguy merlyn/),
);

swi_set_query(steals('badguy', STUFF));
while(swi_next) {
    print swi_var(STUFF), $/;
}

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.