AI-Prolog
view release on metacpan or search on metacpan
lib/AI/Prolog/Builtins.pod view on Meta::CPAN
=head1 NAME
AI::Prolog::Builtins - Builtin predicates that AI::Prolog supports
=head1 REVISION
$Id: Builtins.pod,v 1.9 2005/08/06 23:28:40 ovid Exp $
=head2 Comments
Comments begin with a C<%> and terminate at the end of the line or begin with
C</*> and terminate with C<*/>.
=head2 Variables
As in Prolog, all variables begin with an upper-case letter and are not quoted.
In the following example, C<STUFF> is a variable.
steals(badguy, STUFF, "Some rich person").
=head2 Constants
Constants begin with lower-case letters. If you need a constant that begins
with an upper-case letter or contains spaces or other non-alphanumeric
characters, enclose the constant in single or double quotes The quotes will
not be included in the constant.
In the following example, C<badguy> and C<Some rich person> are both constants:
steals(badguy, STUFF, "Some rich person").
=head2 Miscellaneous
This will not work:
p(X) :- X. /* does not work */
Use this instead:
p(X) :- call(X).
=head1 BUILTINS
=over 4
=item !/0
The "cut" operator. This is used when you wish to tell Prolog that you only
need to satisfy a goal once. For example, if you wish to deny someone the
right to rent videos if they have overdue videos, you might use the cut
operator as soon as you see they have any overdue video. The fact that they
have more than one overdue video doesn't matter.
See the C<cut.pl> program in the C<examples/> directory that comes with this
distribution.
=item assert/1
Add new facts to the database. Only facts can be added, not rules. This may
change in the future. See C<retract(X)>.
assert(loves(ovid,perl)).
=item call/1
Invokes C<X> as a goal.
=item consult/1
Supplied the name of a file containing Prolog code, this will consult the
Prolog code in the file and add its contents to the current knowledgebase.
Will warn if the file cannot be opened.
=item div/2
Succeeds if both terms are bound. The value of the term is X / Y.
Use with C<is(X,Y)>.
is(X, div(N,3)).
This is the internal form of the infix operator:
N / 3.
=item eq/2
Succeeds if C<X> and C<Y> are equal.
This is the internal form of the infix operator:
X == Y.
=item fail/0
This goal always fails. Useful when you've reached a condition you
know should not succeed.
kill(Hero, Beast) :-
not(has_weapon(Hero)), fail.
=item ge/2
Succeeds if both terms are bound and X >= Y.
This is the internal form of the infix operator:
X >= Y.
=item gt/2
Succeeds if both terms are bound and X > Y.
This is the internal form of the infix operator:
X > Y.
=item halt/1
lib/AI/Prolog/Builtins.pod view on Meta::CPAN
=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
( run in 0.464 second using v1.01-cache-2.11-cpan-2398b32b56e )