AI-Prolog
view release on metacpan or search on metacpan
lib/AI/Prolog/Builtins.pod view on Meta::CPAN
=item mod/2
Succeeds if both terms are bound. The value of the term is X % Y. (modulus)
Use with C<is(X,Y)>.
is(X, mod(N,3)).
This is the internal form of the infix operator:
N % 3.
=item mult/2
Succeeds if both terms are bound. The value of the term is X * Y.
Use with C<is(X,Y)>.
is(X, mult(N,3)).
This is the internal form of the infix operator:
N * 3.
=item ne/2
Succeeds if C<X> and C<Y> cannot be proven to be equal.
This is the internal form of the infix operator:
X \= Y.
=item nl/0
Prints a newline.
=item not/1
Succeeds if C<X> cannot be proven. This is not negation as we're used to
seeing it in procedural languages.
=item notrace/0
Turns off tracing of Prolog's attempt to satisfy goals.
=item once/1
Stop solving for C<X> if C<X> succeeds. Defined as:
once(X) :- X, !;
=item or/2
Succeeds as a goal if either C<X> or C<Y> succeeds.
=item plus/2
Succeeds if both terms are bound. The value of the term is X + Y.
Use with C<is(X,Y)>.
is(X, plus(N,1)).
=item print/1
Prints the current Term. If the term is an unbound variable, it will print the
an underscore followed by the internal variable number (e.g., "_284").
print(ovid). % prints "ovid"
print("Something"). % prints "Something"
print(Something). % prints whatever variable Something is bound to
=item println/1
Same as C<print(Term)>, but automatically prints a newline at the end.
=item pow/2
Succeeds if both terms are bound. The value of the term is X ** Y
(X raised to the Y power).
Use with C<is(X,Y)>.
=item retract/1
Remove facts from the database. You cannot remove rules. This may change in
the future. See C<assert(X)>.
retract(loves(ovid,java)).
=item trace/0
Turns on tracing of Prolog's attempt to satisfy goals.
=item true/0
True goal. Automatically succeeds.
=item var/1
Succeeds if X is an unbound variable. Otherwise, this goal fails.
=item write/1
Prints the current Term. If the term is an unbound variable, it will print the
an underscore followed by the internal variable number (e.g., "_284").
write(ovid). % prints "ovid"
write("Something"). % prints "Something"
write(Something). % prints whatever variable Something is bound to
=item writeln/1
Same as C<write(Term)>, but automatically prints a newline at the end.
=back
=head1 LIMITATIONS
These are known limitations that I am not terribly inclined to fix. See the
TODO list for those I am inclined to fix.
IF -> THEN; ELSE not allowed.
Use C<if(IF, THEN, ELSE)> instead.
Chaining terms with a semicolon for "or" does not work. Use C<or/2> instead.
=head1 TODO
There are many things on this list. The core functionality is there, but I do
want you to be aware of what's coming.
=over 4
=item Improve printing.
There are some bugs with printing and escaping characters. Maybe I'll look
into them :)
=item More builtins.
Currently, we only have a tiny subset of builtins available. More are coming.
=back
=head1 MATH
Since version .70, math is fully available in C<AI::Prolog>. Note that math is
implemented via the
L<AI::Prolog::Parser::PreProcessor::Math|AI::Prolog::Parser::PreProcessor::Math>
module. This module rewrites Prolog math to an internal, predicate-based form
with the L<AI::Prolog::Parser|AI::Prolog::Parser> can parse. This may cause
confusion when debugging.
X is 5 + 7.
% internally converted to
% is(X, plus(5, 7)).
The math predicates are officially deprecated and I<cannot> be used in the same
expression with regular Prolog math.
Number may be integers, floats, doubles, etc. A number that starts with a
minus sign (-) is considered negative. No number may end in a decimal point as
the period is interpreted as the end of a clause. The following is therefore a
syntax error:
X is 5. + 7.
Unfortunately, the parser doesn't yet yell about that. We'll try and figure
out why later.
Omit the period after the number or put a zero after it:
X is 5.0 + 7.
X is 5 + 7.
Because numbers use Perl scalars, you may mix types (ints and floats) and they
will behave as you expect in Perl.
Precedence is C<*> and C</>, left to right, followed by C<+> and C<->, left to
right followed by C<%>, left to right. (I probably should change that.)
Naturally, parentheses may be used for grouping:
X is 3 * 5 + 2. % is(X, plus(mult(3, 5), 2)).
X is 3 * (5 + 2). % is(X, mult(3, plus(5, 2))).
When using math, note that C<is> is similar to Perl's assignment operator, C<=>.
This can be confusing.
X is 3 + 2.
Sets C<X> to the value of C<5>.
If C<X> is already instantiated, this goal succeeds if the value of C<X> is the
value of the result of the right-hand side of the equation. Internally, if X is not
instantiated, it looks like this:
( run in 1.873 second using v1.01-cache-2.11-cpan-39bf76dae61 )