AI::Prolog::Builtins - Builtin predicates that AI::Prolog supports
$Id: Builtins.pod,v 1.9 2005/08/06 23:28:40 ovid Exp $
Comments begin with a %
and terminate at the end of the line or begin with /*
and terminate with */
.
As in Prolog, all variables begin with an upper-case letter and are not quoted. In the following example, STUFF
is a variable.
steals(badguy, STUFF, "Some rich person").
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, badguy
and Some rich person
are both constants:
steals(badguy, STUFF, "Some rich person").
This will not work:
p(X) :- X. /* does not work */
Use this instead:
p(X) :- call(X).
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 cut.pl
program in the examples/
directory that comes with this distribution.
Add new facts to the database. Only facts can be added, not rules. This may change in the future. See retract(X)
.
assert(loves(ovid,perl)).
Invokes X
as a goal.
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.
Succeeds if both terms are bound. The value of the term is X / Y. Use with is(X,Y)
.
is(X, div(N,3)).
This is the internal form of the infix operator:
N / 3.
Succeeds if X
and Y
are equal.
This is the internal form of the infix operator:
X == Y.
This goal always fails. Useful when you've reached a condition you know should not succeed.
kill(Hero, Beast) :- not(has_weapon(Hero)), fail.
Succeeds if both terms are bound and X >= Y.
This is the internal form of the infix operator:
X >= Y.
Succeeds if both terms are bound and X > Y.
This is the internal form of the infix operator:
X > Y.
In the aiprolog
shell, exist shell. Currently has no other effect.
If X
succeeds as a goal, try Y
as a goal. Otherwise, try Z
.
thief(badguy). steals(PERP, X) :- if(thief(PERP), eq(X,rubies), eq(X,nothing)).
If X is unbound and Y is bound to a number, the goal succeeds and X becomes bound to the value of Y. Otherwise, succeeds if both terms are bound, numbers, and equal.
All other conditions result in failure.
This is the internal form of the infix operator:
X is Y.
Succeeds if both terms are bound and X <= Y.
This is the internal form of the infix operator:
X <= Y.
Dumps a listing of all user-defined predicates and how they are defined.
Dumps a listing of the requested predicate. X
must a variable or string instantiated in functor/arity) form. Note that, unlike most Prolog's, this means that the followig will not work:
listing(foo/2).
Use this instead:
listing('foo/2').
Succeeds if both terms are bound and X < Y.
This is the internal form of the infix operator:
X < Y.
Succeeds if both terms are bound. The value of the term is X - Y. Use with is(X,Y)
.
is(X, minus(N,1)).
This is the internal form of the infix operator:
N - 1.
Succeeds if both terms are bound. The value of the term is X % Y. (modulus) Use with is(X,Y)
.
is(X, mod(N,3)).
This is the internal form of the infix operator:
N % 3.
Succeeds if both terms are bound. The value of the term is X * Y. Use with is(X,Y)
.
is(X, mult(N,3)).
This is the internal form of the infix operator:
N * 3.
Succeeds if X
and Y
cannot be proven to be equal.
This is the internal form of the infix operator:
X \= Y.
Prints a newline.
Succeeds if X
cannot be proven. This is not negation as we're used to seeing it in procedural languages.
Turns off tracing of Prolog's attempt to satisfy goals.
Stop solving for X
if X
succeeds. Defined as:
once(X) :- X, !;
Succeeds as a goal if either X
or Y
succeeds.
Succeeds if both terms are bound. The value of the term is X + Y. Use with is(X,Y)
.
is(X, plus(N,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
Same as print(Term)
, but automatically prints a newline at the end.
Succeeds if both terms are bound. The value of the term is X ** Y (X raised to the Y power). Use with is(X,Y)
.
Remove facts from the database. You cannot remove rules. This may change in the future. See assert(X)
.
retract(loves(ovid,java)).
Turns on tracing of Prolog's attempt to satisfy goals.
True goal. Automatically succeeds.
Succeeds if X is an unbound variable. Otherwise, this goal fails.
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
Same as write(Term)
, but automatically prints a newline at the end.
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 if(IF, THEN, ELSE)
instead.
Chaining terms with a semicolon for "or" does not work. Use or/2
instead.
There are many things on this list. The core functionality is there, but I do want you to be aware of what's coming.
There are some bugs with printing and escaping characters. Maybe I'll look into them :)
Currently, we only have a tiny subset of builtins available. More are coming.
Since version .70, math is fully available in AI::Prolog
. Note that math is implemented via the AI::Prolog::Parser::PreProcessor::Math module. This module rewrites Prolog math to an internal, predicate-based form with the 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 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 *
and /
, left to right, followed by +
and -
, left to right followed by %
, 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 is
is similar to Perl's assignment operator, =
. This can be confusing.
X is 3 + 2.
Sets X
to the value of 5
.
If X
is already instantiated, this goal succeeds if the value of 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:
is(5, plus(3,2)).
The =
operator tries to unify the left-hand side with the right-hand side:
X = 3 + 2.
If X
is already instantiated, this goal succeeds if the value of X
is the same goal as the right-hand side of the equation. Internally, if X is not instantiated, it looks like this:
eq(plus(3,2), plus(3,2)).
When you first start using Prolog, you probably was is
instead of =
.
Logical comparisons are straightforward:
3 >= X. Y > (4 + 3) * X. X == Y. % a test for equality X \= Y. % Not equal. See caveats for ne/2 % etc.
Currently we cannot directly access bound variables in the tail of a list used in a query when using unformatted results:
$prolog->query('foo([Head|Tail], bar).'); while (my $result = $prolog->results) { print Dumper $result->Head; print Dumper $result->Tail; # doesn't work }
You'll have to switch to raw_results
and parse the data manually:
$prolog->raw_results(1); $prolog->query('foo([Head|Tail], bar).'); while (my $result = $prolog->results) { print Dumper $result; }
W-Prolog: http://goanna.cs.rmit.edu.au/~winikoff/wp/
X-Prolog: http://www.iro.umontreal.ca/~vaucher/XProlog/
Roman Barták's online guide to programming Prolog: http://kti.ms.mff.cuni.cz/~bartak/prolog/index.html
Curtis "Ovid" Poe, <moc tod oohay ta eop_divo_sitruc>
Reverse the name to email me.
This work is based on W-Prolog, http://goanna.cs.rmit.edu.au/~winikoff/wp/, by Dr. Michael Winikoff. Many thanks to Dr. Winikoff for granting me permission to port this.
Copyright 2005 by Curtis "Ovid" Poe
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.