In the examples below, assume the database contains
?- dynamic(test/1). ?- public(test/2). test(1). test(1). % Note that this is asserted twice test(2) :- atom(a). test(3). test(a, b). test(a, c).
Also, assume that the standard input stream will yield the following when read
(where <space>
stands for a single space).
<space>a(b, C). /* This is a comment */ abc.
abolish / 1
abolish(PredicateIndicator)
abolish(test/1)
- totally removes the predicate test
with arity 1 (and hence all clauses for it).arg / 3
arg(Number, Compound, Term)
Term
with the argument of Compound
specified by Number.
arg(2, a(b, c, d), X)
- instantiates X
to b.
=:= / 2
Exp1 =:= Exp2
Exp1
and Exp2,
then succeed if values are equal.2 + 2 =:= 1 + 3
- succeeds.1 + 1 =:= 2 + 2
- fails.2 + 2 =:= 1 + 1
- fails.=\= / 2
Exp1 =\= Exp2
Exp1
and Exp2,
then succeed if values differ.2 + 2 =\= 1 + 3
- fails.1 + 1 =\= 2 + 2
- succeeds.2 + 2 =\= 1 + 1
- succeeds.> / 2
Exp1 > Exp2
Exp1
and Exp2,
then succeed if value of the first is greater.2 + 2 > 1 + 3
- fails.1 + 1 > 2 + 2
- fails.2 + 2 > 1 + 1
- succeeds.>= / 2
Exp1 >= Exp2
Exp1
and Exp2,
then succeed if the value of the first is greater than or equal to the second.2 + 2 >= 1 + 3
- succeeds.1 + 1 >= 2 + 2
- fails.2 + 2 >= 1 + 1
- succeeds.< / 2
Exp1 < Exp2
Exp1
and Exp2,
then succeed if value of the first is smaller.2 + 2 < 1 + 3
- fails.1 + 1 < 2 + 2
- succeeds.2 + 2 < 1 + 1
- fails.=< / 2
Exp1 =< Exp2
Exp1
and Exp2,
then succeed if the value of the first is less than or equal to the second.2 + 2 =< 1 + 3
- succeeds.1 + 1 =< 2 + 2
- succeeds.2 + 2 =< 1 + 1
- fails.asserta / 1
asserta(FactOrRule)
asserta(test(0))
- adds the fact test(0)
as the first clause for test/1.
asserta(test(X) :- X < 0)
- adds the rule test(X)
:-
X
<
0
as the first clause for test/1.
assertz / 1
assertz(FactOrRule)
assertz(test(4))
- adds the fact test(4)
as the last clause for test/1.
assertz(test(X) :- X > 3)
- adds the rule test(X)
:-
X
>
3
as the last clause for test/1.
atom / 1
atom(Term)
Term
is an atom.atom(a)
- succeeds.atom(1)
- fails.atom(A)
- fails.atomic / 1
atomic(Term)
Term
is atomic.atomic(a)
- succeeds.atomic(1)
- succeeds.atomic(a(b))
- fails.atomic[a, b]
- fails.atomic(A)
- fails.call / 1
call(Goal)
Goal,
and succeeds if Goal
succeeds. During backtracking, Goal
can be redone. Opaque to cuts in the subgoal.call(test(X))
- instantiates X
to 1
intially, and to 1,
2,
and 3
on backtracking.clause / 2
clause(Head, Body)
Head
and whose body unifies with Body.
On backtracking, finds the next such clauses. (A fact is considered to be a clause whose body is true.)
clause(test(X), B)
- instantiates X
to 1
and B
to true.
On backtracking, instantiates them to 1
and true
again, then to 2
and atom(a)
and then to 3
and true.
, / 2
GoalA, GoalB
GoalA
and, if it succeeds, then calls GoalB,
succeeding only if both succeed. On backtracking, tries to redo GoalB
and, if this fails, tries to redo GoalA.
Transparent to cuts occurring in either subgoal.test(X), write(X)
- writes 1.
On backtracking, writes (in turn) 1,
2,
3.
! / 0
!
!
- performs a cut operation.; / 2
GoalA ; GoalB
GoalA
and, if it fails, then calls GoalB,
succeeding if either succeeds. On backtracking, tries to redo the goal that succeeded; if this was GoalA
and GoalA
cannot be redone, then tries GoalB.
Transparent to cuts occurring in either subgoal (but a cut in GoalA
will preclude trying GoalB.)
atom(1) ; write('Failed')
- writes Failed but succeeds.fail / 0
fail
fail
- fails.functor / 3
functor(Term, Functor, Arity)
Term
is a compound, unifies Functor
with its functor and Arity
with its arity; if Term
is atomic, unifies Functor
with Term
and Arity
with 0; if Term
is uninstantiated, unifies it with a compound term whose functor is Functor
and which has Arity
fresh uninstantiated arguments.functor(a(b, c), F, A)
- instantiates F
to a
and A
to 2.
functor(T, a, 2)
- instantiates T
to a(_0,
_1)
where _0
and _1
are distinct fresh variables.halt / 0
halt
halt
- exits PrologJ with status 0.halt / 1
halt(Status)
halt(1)
- exits PrologJ with status 1.integer / 1
integer(Term)
Term
is an integer.integer(1)
- succeeds.integer(1.0)
- fails.integer(a(b))
- fails.integer(A)
- fails.is / 2
Term is Expression
Expression
and unifies the result with Term.
X is 2 + 2
- instantiates X
to 4.
nl / 0
nl
nl
- writes a newline to the current output stream.nonvar / 1
nonvar(Term)
Term
is not an uninstantiated variable.nonvar(1)
- succeeds.nonvar(a(b))
- succeeds.nonvar(A)
- fails.\= / 2
Term1 \= Term2
Term1
and Term2
can be unified; succeeds if they cannot be. a(X, c) \= a(b, Y)
- fails.a \= 1
- succeeds.op / 3
op(Priority, Associativity, Symbol)
Priority
> 0) or removes an operator (if Priority
= 0). Priority must be >= 0 and <= 1200. Possible associativities are fx
(prefix non-associative), fy
(prefix right associative), xfx
(infix non-associative), yfx
(infix left associative), xfy
(infix right associative), yfy
(typically not used), xf
(posfix non-associative), yf
(postfix left associative).op(1000, xfy, foo)
- makes foo
an infix operator with priority 1000 and left associativity.op(0, xfy, +)
- causes +
to no longer be considered an infix operator.put / 1
put(Code)
put_code/1)
Writes the character whose ASCII code is Code
to the current output stream.put(65)
- writes A
to the current output stream.read / 1
read(Term)
Term.
read(T)
- instantiates T
to a(b,
_0).
read(T)
- (after the above) instantiates T
to abc.
repeat / 0
repeat
!.
repeat, test(X), write(X), X > 2
- writes 112.
retract / 1
retract(FactOrRule)
FactOrRule.
(The forms Fact
and Fact
:-
true
are equivalent.) On backtracking, removes the next such clauses. retract(test(X))
- instantiates X
to 1.
On backtracking, instantiates X
to 1
again, then 3,
in each case removing the corresponding clause. [test(2)
:-
atom(a)
is not affected.]retract(test(X) :- B)
- instantiates X
to 2
and B
to atom(a),
and removes the corresponding clause. Succeeds just once.retractall / 1
retractall(FactOrRule)
FactOrRule.
(The forms Fact
and Fact
:-
true
are equivalent.) Not redoable. Always succeeds, even if no clauses match.retractall(test(X))
- removes the clauses test(1)
(both) and test(3)
from the database, but leaves test(2)
:-
atom(a).
retractall(test(X) :- B)
- removes the clause test(2)
:-
atom(a)
from the database, but leaves the other test/1
clauses alone.retractall(nonexistent)
- succeeds without changing the database.== / 2
Term1 == Term2
Term1
and Term2
are identical (without any unifications).a(b, c) == a(b, c)
- succeedsa(b, X) == a(b, c)
- fails.a(b, c) == a(b, X)
- fails.\== / 2
Term1 \== Term2
Term1
and Term2
are not identical.a(b, c) \== a(b, c)
- fails.a(b, X) \== a(b, c)
- succeeds.a(b, c) \== a(b, X)
- succeeds.true / 0
true
true
- succeeds.= / 2
Term1 = Term2
Term1
with Term2.
a(X, c) = a(b, Y)
- instantiates X
to b
and Y
to c.
a = 1
- fails.=.. / 2
Term =.. List
Term
is instantiated, unifies List
with Term
represented as a list. If Term
is uninstantiated, unifies it with a term constructed from List.
At least one of Term
and List
must be instantiated.a(b, c) =.. L
- instantiates L
to [
a,
b,
c
].1=.. L
- instantiates L
to [
1
].
T =..[ a, b c ]
- instantiates T
to a(b,
c).
T =..[ 1 ]
- instantiates T
to 1.
var / 1
var(Term)
Term
is an uninstantiated variable.var(A)
- succeeds.var(1)
- fails.write / 1
write(Term)
Term
to the current output stream, using operator and list notation as appropriate, but not quotes.write(1)
- writes 1
to the current output stream.write([ 2 + 2, a(b) ]
- writes [
2
+
2,
a(b)
]
to the current output stream.write('Hello, world')
- writes Hello, World (with no quotes) to the current output stream.writeq / 1
writeq(Term)
Term
to the current output stream, but atoms that would need to be quoted to be accepted by read
are quotedwriteq('Hello, world')
- writes 'Hello,
World'
(with quotes) to the current output stream.writeq((a, b))
- writes a
','
b
to the current output stream.writeq([ 2 + 2, a(b) ]
- writes [
2
+
2,
a(b)
]
to the current output stream.