NAME

AI::Prolog::Builtins - Builtin predicates that AI::Prolog supports

GRAMMAR

The language is given by the following simple grammar (pulled directly from Dr. Winikoff's page):

Program   ::= Rule | Rule Program
Query     ::= Term

Rule      ::= Term . | Term :- Terms .
Terms     ::= Term   | Term , Terms
Term      ::= Number | Variable | AtomName | AtomName(Terms)
                | [] | [Terms] | [Terms | Term]
                | print(Term) | nl | eq(Term , Term)
                | if(Term , Term , Term) | or(Term , Term ) 
                | not(Term) | call(Term) | once(Term)
Number    ::= Digit | Digit Number
Digit     ::= 0 | ... | 9
AtomName  ::= LowerCase NameChars
Variable  ::= UpperCase NameChars
NameChars ::= NameChar | NameChar NameChars
NameChar  ::= a | ... | z | A | ... | Z | Digit

Comments

Comments begin with a % and terminate at the end of the line or begin with /* and terminate with */.

Variables

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

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").

Miscellaneous

This will not work:

p(X) :- X. /* does not work */

Use this instead:

p(X) :- call(X).

BUILTINS

  • call(X).

    Invokes X as a goal.

  • eq(X, Y).

    Succeeds if X and Y are equal.

  • if(X, Y, Z).

    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)).
  • nl

    Prints a newline.

  • not(X).

    Succeeds if X cannot be proven. This is not negation as we're used to seeing it in procedural languages.

  • once(X)

    Stop solving for X if X succeeds. Defined as:

    once(X) :- X, !;
  • or(X, Y)

    Succeeds as a goal if either X or Y succeeds.

  • print(Term).

    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 

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 if(IF, THEN, ELSE) instead.

= and \= not available.

Use eq(X,Y) and not(eq(X,Y)) instead.

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.

  • Anonymous variables

    steals(badguy, _).

    Currently, that doesn't work. Make up a variable name and only use it once.

  • Improve printing.

    There are some bugs with printing and escaping characters. Maybe I'll look into them :)

  • Math.

    Math is hard. So we don't have it, but we will and we'll probably have it in the form of system predicates that call out to Perl. We can define most of what we need logically, but it's very slow and complicated, so we won't do that.

  • Database access

    No, we're not talking DBI. We're talking about clause, assert and retract. We don't have that yet.

  • Add cut.

    Currently, we can use once(X) to simulate it. Think of this predicate as being equivalent to:

    once(X) :- X, !;

BUGS

I'm sure there are bugs. I don't know of any right now. Let me know if (when) you find them. See the TODO list before that, though.

SEE ALSO

W-Prolog: http://goanna.cs.rmit.edu.au/~winikoff/wp/

Michael Barták's online guide to programming Prolog: http://kti.ms.mff.cuni.cz/~bartak/prolog/index.html

AUTHOR

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 AND LICENSE

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.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 135:

=over should be: '=over' or '=over positive_number'