Chapter 4

Built-in Prolog Goals


This chapter contains a descriptive summary of most of the important built-in Prolog goals. 

4.1 Utility goals

help(S)
S should be a symbolic atom, e.g., help(assert).

halt
Stops Prolog, resume operating system.

trace, notrace
Turns trace on and off, respectively.

4.2 Universals

true
Always succeeds as a goal.

fail
Always fails as a goal.

4.3 Loading Prolog programs

consult(F)
Loads program from the file F. F should be bound to file designator expression, e.g., F='[root.programs.prolog]prog.pro' or F='/home/user/prolog/sample.pl', depending on the file system.

reconsult(F)
Like consult except that each predicate already defined has its definition replaced by the new defintion being loaded.

[F1,F2,...]
Bracket notation, meaning consult F1, then consult F2, then ...

4.4 Arithmetic goals

N > M , N < M, N =< M, N >= M
These relations test what they look like they test. Of course, M and N should be bound to numbers in order for these goals to either succeed or fail (and not to cause error).

4.5 Testing types

atom(X)
Tests whether X is bound to a symbolic atom. For example,
?- atom(foot).
yes
?- atom(3).
no
?- atom('foot').
yes
?- atom("foot").
no
integer(X)
Tests whether X is bound to an integer.

real(X)
Tests whether X is bound to a real number.

string(X)
Tests whether X is bound to a string.

4.6 Equality of Prolog expressions

X = Y, X \=Y
Tests whether X and Y can be unified, or cannot, respectively. For example,
?- [X,Y|R] = [a,b,c].
X = a, Y = b, R = [c]
?- [X,Y,Z] = [a,b].
no
X ==Y, X \== Y
Tests whether X and Y are currently co-bound, i.e., have been bound to or share same value, or not, respectively. For example,
?- X = 3, Y = 1 * 3, X == Y.
no
?- X = a, [Y|_]= [a,b,c], X == Y.
X = a, Y = a

4.7 Control

call(P)
Force P to be a goal; succeed if P does, else fail.

!
Prolog cut predicate.

4.8 Testing for variables

ground(G)
Tests whether G has unbound logical variables.

var(X)
Tests whether X is bound to a Prolog variable.

4.9 Assert and retract

asserta(C)
Assert clause C into database above other clauses with the same key predicate. The key predicate of a clause is the first predicate encountered when the clause is read from left to right.

assertz(C), assert(C)
Assert clause C into database below other clauses with the same key predicate.

retract(C)
Retract C from the database. C must be sufficiently instantiated to determine the predicate key.

4.10 Binding a logical variable to a numeric value

X is E
Binds logical variable V to the numerical value of E. The expression E must either be a number or be a number-valued expression, conventionally parenthesized, such as, for example, (2*Z + W)/ (4* Z -W), assuming that Z and W themselves are bound to numbers at the time of evaluating this expression.

4.11 Negation as failure

not(Q), \+Q
Negation-as-failure, as if defined by:
not(Q) :- call(Q), !, fail.
not(Q).

4.12 Input/output

seeing(X)
Succeeds if X is (or can be) bound to current read port. X=user is keybord input.

see(X)
Opens port for input file bound to X. Subsequent input for 'read' is then taken from that port.

seen
Closes any selected input port/file, and causes 'read' to look at user.

read(X)
Reads Prolog type expression from current port, storing value in X.

telling(X)
Succeeds if X is (or can be) bound to current output port; X=user is screen.

tell(X)
Opens port for output file bound to X. Subsequent output from 'write' or 'display' is sent to that port.

told
Closes any selected output port/file and reverts to screen output.

write(E)
Writes Prolog expression bound to E into current output port.

nl
Next line (line feed).

tab(N)
Write N spaces to selected output port.

4.13 Prolog terms and clauses as data

clause(H,B)
Retrieves clauses in memory whose head matches H and body matches B. H must be sufficiently instantiated to determine the main predicate of the head.

functor(E,F,N)
E must be bound to a functor expression of the form 'f(...)'. F will be bound to 'f', and N will be bound to the number of arguments that f has.

arg(N,E,A)
E must be bound to a functor expression, N is a whole number, and A will be bound to the Nth argument of E (or fail).

=..
'univ' converts between term and list. For example,

?- parent(a,X) = .. L.
L = [parent, a, _X001]
?- P=..[parent,jack,mary].
P= parent(jack,mary)
?- yes = .. L.
L = [yes]
?- P=..[fact].
P= fact

4.14 Prolog Operators

:- op(P,T,O).
Declare an operator symbol. For example, with source program ...
:- op(500,xfx,'has_color').
a has_color red.
b has_color blue.
Then ...
?- b has_color C.
C = red
?- What has_color red.
What = a
P is precedence, an integer. Larger P has less precedence (ability to group). Precedence number values for built-ins depend upon the actual Prolog system.  User needs to find out what these values are.  (See the reference materials or use the help facility with keyword 'operator').

T is operator type:

xfx infix nonassociative
xfy infix right-associative
yfx infix left-associative
fx prefix nonassociative
fy prefix right-associative
xf postfix nonassociative
yf postfix left-associative
O is the name of the operator. Which symbols are usable may depend upon the Prolog expression reader (i.e., which kind of Prolog you are using).

Built-ins, in order of precedence ("ability to group", least ability first):

            :-                      xfx,  fx     larger P value

            ?-                      fx 

            ;                       xfy 

            ,                       xfy 

            not                     fy 

            is, =.. , <, etc.       xfx 

            +,  -                   yfx, fx 

            *,  /                   yfx 

            ^                       xfy         smaller P value

The variability between Prolog systems regarding whether operators are treated as functors or as "special" sequences, and the precedence values assigned to built-ins and how they are supposed to be read is one of the most frustrating aspects of Prolog (in my opinion).  Sorry: There is no easy way around this quagmire.


Prolog Tutorial Contents