AI-Prolog
view release on metacpan or search on metacpan
lib/AI/Prolog.pm view on Meta::CPAN
package AI::Prolog;
$VERSION = '0.741'; ## no critic
use strict;
use Carp qw( confess carp croak );
use Hash::Util 'lock_keys';
use Exporter::Tidy shortcuts => [qw/Parser Term Engine/];
use aliased 'AI::Prolog::Parser';
use aliased 'AI::Prolog::Term';
use aliased 'AI::Prolog::Engine';
use Text::Quote;
use Regexp::Common;
# they don't want pretty printed strings if they're using this interface
Engine->formatted(0);
# Until (and unless) we figure out the weird bug that prevents some values
# binding in the external interface, we need to stick with this as the default
Engine->raw_results(1);
sub new {
my ( $class, $program ) = @_;
my $self = bless {
_prog => Parser->consult($program),
_query => undef,
_engine => undef,
} => $class;
lock_keys %$self;
return $self;
}
sub do {
my ( $self, $query ) = @_;
$self->query($query);
1 while $self->results;
$self;
}
sub query {
my ( $self, $query ) = @_;
# make that final period optional
$query .= '.' unless $query =~ /\.$/;
$self->{_query} = Term->new($query);
unless ( defined $self->{_engine} ) {
# prime the pump
$self->{_engine} = Engine->new( @{$self}{qw/_query _prog/} );
}
$self->{_engine}->query( $self->{_query} );
return $self;
}
sub results {
my $self = shift;
unless ( defined $self->{_query} ) {
croak "You can't fetch results because you have not set a query";
}
$self->{_engine}->results;
}
sub trace {
my $self = shift;
if (@_) {
$self->{_engine}->trace(shift);
return $self;
}
return $self->{_engine}->trace;
}
sub raw_results {
my $class = shift;
if (@_) {
Engine->raw_results(shift);
return $class;
}
return Engine->raw_results;
}
my $QUOTER;
sub quote {
my ( $proto, $string ) = @_;
$QUOTER = Text::Quote->new unless $QUOTER;
return $QUOTER->quote_simple($string);
}
lib/AI/Prolog.pm view on Meta::CPAN
This method quotes a Perl string to allow C<AI::Prolog> to treat it as a proper
Prolog term (and not worry about it accidentally being treated as a variable if
it begins with an upper-case letter).
my $perl6 = AI::Prolog->quote('Perl 6'); # returns 'Perl 6' (with quotes)
$prolog->query(qq'can_program("ovid",$perl6).');
At the present time, quoted strings may use single or double quotes as strings.
This is somewhat different from standard Prolog which treats a double-quoted
string as a list of characters.
Maybe called on an instance (the behavior is unchanged).
=head2 C<list(@list)>.
Turns a Perl list into a Prolog list and makes it suitable for embedding into
a program. This will quote individual variables, unless it thinks they are
a number. If you wish numbers to be quoted with this method, you will need to
quote them manually.
This method does not add the list brackets.
my $list = AI::Prolog->list(qw/foo Bar 7 baz/);
# returns: 'foo', 'Bar', 7, 'baz'
$prolog->query(qq/append(X,Y,[$list])./);
May be called on an instance (the behavior is unchanged).
=head1 INSTANCE METHODS
=head2 C<do($query_string)>
This method is useful when you wish to combine the C<query()> and C<results()>
methods but don't care about the results returned. Most often used with the
C<assert(X)> and C<retract(X)> predicates.
$prolog->do('assert(loves(ovid,perl)).');
This is a shorthand for:
$prolog->query('assert(loves(ovid,perl)).');
1 while $prolog->results;
This is important because the C<query()> method merely builds the query. Not
until the C<results()> method is called is the command actually executed.
=head2 C<query($query_string)>
After instantiating an C<AI::Prolog> object, use this method to query it.
Queries currently take the form of a valid prolog query but the final period
is optional:
$prolog->query('grandfather(Ancestor, julie).');
This method returns C<$self>.
=head2 C<results>
After a query has been issued, this method will return results satisfying the
query. When no more results are available, this method returns C<undef>.
while (my $result = $prolog->results) {
# [ 'grandfather', $ancestor, 'julie' ]
print "$result->[1] is a grandfather of julie.\n";
}
If C<raw_results> is false, the return value will be a "result" object with
methods corresponding to the variables. This is currently implemented as a
L<Hash::AsObject|Hash::AsObject> so the caveats with that module apply.
Please note that this interface is experimental and may change.
$prolog->query('steals("Bad guy", STUFF, VICTIM)');
while (my $r = $prolog->results) {
print "Bad guy steals %s from %s\n", $r->STUFF, $r->VICTIM;
}
See C<raw_results> for an alternate way of generating output.
=head1 BUGS
See L<AI::Prolog::Builtins|AI::Prolog::Builtins> and
L<AI::Prolog::Engine|AI::Prolog::Engine> for known bugs and limitations. Let
me know if (when) you find them. See the built-ins TODO list before that,
though.
=head1 TODO
=over 4
=item * Why does this take so long to run?
perl examples/path.pl 3
On my Mac that takes over an hour to complete.
=item * Support for more builtins.
=item * Performance improvements.
I have a number of ideas for this, but it's pretty low-priority until things
are stabilized.
=item * Add "sugar" interface.
=item * Better docs.
=item * Tutorial.
=item * Data structure cookbook.
=item * Better error reporting.
=back
=head1 EXPORT
None by default. However, for convenience, you can choose ":all" functions to
be exported. That will provide you with C<Term>, C<Parser>, and C<Engine>
classes. This is not recommended and most support and documentation will now
( run in 1.006 second using v1.01-cache-2.11-cpan-d80b1682f3f )