Skip to content

Commit 42eb82c

Browse files
dtonhoferJanWielemaker
authored andcommitted
DOC: PR #770 Improve docs on determinism, \+/1 and type tests.
This merges PR #770 after several edits.
1 parent a438454 commit 42eb82c

1 file changed

Lines changed: 67 additions & 21 deletions

File tree

man/builtin.plx

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,62 @@ module declaration, spy/1, and dynamic/1.
110110
\subsection{Predicate behaviour and determinism} \label{sec:determinism}
111111

112112
\index{predicate behaviour and determinism}%
113-
To describe the general behaviour of a predicate, the following vocabulary
114-
is employed. In source code, structured comments contain the corresponding
115-
keywords:
113+
\index{choicepoint}%
114+
The keywords in \tabref{determinism} may appear in the manual's predicate
115+
descriptions and in \jargon{pldoc} structured comments in source code. They
116+
describe the general behaviour of a predicate.
116117

118+
\begin{table}
117119
\begin{center}
118120
\begin{tabular}{lp{0.7\linewidth}}
119121
\hline
120122
\const{det} & A \jargon{deterministic} predicate always succeeds exactly
121123
once and does not leave a choicepoint. \\
122124
\const{semidet} & A \jargon{semi-deterministic} predicate succeeds at most
123-
once. If it succeeds it does not leave a choicepoint. \\
125+
once. If it succeeds, it does not leave a choicepoint. \\
124126
\const{nondet} & A \jargon{non-deterministic} predicate is the most general
125127
case and no claims are made on the number of solutions (which
126128
may be zero, i.e., the predicate may \jargon{fail}) and
127-
whether or not the predicate leaves an choicepoint on
128-
the last solution. \\
129+
whether or not the predicate leaves a choicepoint on
130+
its last solution. \\
129131
\const{multi} & As \const{nondet}, but succeeds at least once. \\
130-
\const{undefined} & Well founded semantics third value.
132+
\const{failure} & Always fails. \\
133+
\const{undefined} & \jargon{Well-founded semantics} ``third value''.
131134
See undefined/0. \\
132135
\hline
133136
\end{tabular}
134137
\end{center}
138+
\caption{Determinism indicators}
139+
\label{tab:determinism}
140+
\end{table}
141+
142+
``Leaving no choicepoint'' means that the system \emph{knows} that
143+
redoing the predicate will not yield any additional solutions. For
144+
example, member/2 is non-deterministic, but deterministic if the
145+
solution is the last element of the list. The predicate member/2 may not
146+
know immediately whether there are more solutions after the first one,
147+
so a choicepoint remains, even if it eventually turns out to yield
148+
nothing:
149+
150+
\begin{code}
151+
?- member(1, [2,1,3]).
152+
true ; % there may be more solutions
153+
false. % actually not
154+
\end{code}
155+
156+
If a solution is the last element of the list, there is enough information to
157+
leave no choicepoint:
158+
159+
\begin{code}
160+
?- member(1, [2,3,1]).
161+
true.
162+
\end{code}
163+
164+
Note that if the toplevel waits for input after a query this indicates
165+
that the query succeeded with a choicepoint. If the user enters \chr{*}
166+
the toplevel explains the location of the choicepoint. Alternatively,
167+
the GUI debugger may be used to examine the open choicepoints.
168+
135169

136170
\section{Character representation} \label{sec:chars}
137171

@@ -1798,15 +1832,20 @@ prolog_edit:load :-
17981832

17991833
Type tests are semi-deterministic predicates that succeed if the
18001834
argument satisfies the requested type. Type-test predicates have no
1801-
error condition and do not instantiate their argument. See also library
1802-
\pllib{error}.
1835+
error condition and do not instantiate their argument. They have no
1836+
first-order ``logical'' interpretation; instead they inspect the state
1837+
of the computation at call time and are mainly used in \jargon{clause
1838+
guards} and \jargon{assertions}. See also library \pllib{error},
1839+
must_be/2 and assertion/1.
18031840

18041841
\begin{description}
18051842
\predicate[ISO]{var}{1}{@Term}
1806-
True if \arg{Term} currently is a free variable.
1843+
True if \arg{Term} currently is a free variable. The compiler warns if
1844+
\arg{Term} is syntactically not a variable.
18071845

18081846
\predicate[ISO]{nonvar}{1}{@Term}
1809-
True if \arg{Term} currently is not a free variable.
1847+
True if \arg{Term} currently is not a free variable. This is the logical
1848+
complement of var/1: \exam{var(X)} is true iff \exam{nonvar(X)} fails.
18101849

18111850
\predicate[ISO]{integer}{1}{@Term}
18121851
True if \arg{Term} is bound to an integer.
@@ -1861,13 +1900,14 @@ rational (rational/1) and blob (blob/2). In addition, the symbol
18611900
\secref{ext-lists}.
18621901

18631902
\predicate[ISO]{compound}{1}{@Term}
1864-
True if \arg{Term} is bound to a compound term. See also functor/3
1865-
=../2, compound_name_arity/3 and compound_name_arguments/3.
1903+
True if \arg{Term} is bound to a compound term. See also
1904+
compound_name_arity/3, compound_name_arguments/3, functor/3 and
1905+
\predref{=..}{2}.
18661906

18671907
\predicate[ISO]{callable}{1}{@Term}
18681908
True if \arg{Term} is bound to an atom or a compound term. This was
18691909
intended as a type-test for arguments to call/1, call/2 etc. Note that
1870-
callable only tests the \jargon{surface term}. Terms such as (22,true)
1910+
callable only tests the \jargon{surface term}. Terms such as \exam{(22,true)}
18711911
are considered callable, but cause call/1 to raise a type error.
18721912
Module-qualification of meta-argument (see meta_predicate/1) using
18731913
\functor{:}{2} causes callable to succeed on any
@@ -1893,15 +1933,17 @@ True if \arg{Term} holds no free variables. See also nonground/2
18931933
and term_variables/2.
18941934

18951935
\predicate{cyclic_term}{1}{@Term}
1896-
True if \arg{Term} contains cycles, i.e.\ is an infinite term.
1936+
True if \arg{Term} contains cycles, i.e.\ is an infinite term (also known
1937+
as a \jargon{rational tree}).
18971938
See also acyclic_term/1 and \secref{cyclic}.%
18981939
\footnote{The predicates cyclic_term/1 and acyclic_term/1 are
18991940
compatible with SICStus Prolog. Some Prolog systems
19001941
supporting cyclic terms use \nopredref{is_cyclic}{1}.}
19011942

19021943
\predicate[ISO]{acyclic_term}{1}{@Term}
19031944
True if \arg{Term} does not contain cycles, i.e.\ can be processed
1904-
recursively in finite time. See also cyclic_term/1 and \secref{cyclic}.
1945+
recursively in finite time. This includes \arg{Term} being an unbound
1946+
variable. See also cyclic_term/1 and \secref{cyclic}.
19051947
\end{description}
19061948

19071949
\section{Comparison and Unification of Terms} \label{sec:compare}
@@ -2379,7 +2421,8 @@ $X=a$ and $X=b$, while \verb$optional(member(X,[]))$ succeeds without
23792421
binding $X$.
23802422

23812423
\prefixop[ISO]{\+}{:Goal}
2382-
True if `Goal' cannot be proven (mnemonic: \chr{+} refers to {\em
2424+
True if \arg{Goal} cannot be proven, i.e.\ if the attempt to prove
2425+
\arg{Goal} fails in finite time (mnemonic: \chr{+} refers to {\em
23832426
provable} and the backslash (\chr{\}) is normally used to
23842427
indicate negation in Prolog). In contrast to the ISO standard, but
23852428
compatible with several other Prolog systems, SWI-Prolog implements
@@ -2389,10 +2432,13 @@ provable} and the backslash (\chr{\}) is normally used to
23892432
if such a variable is at runtime bound to a (\predref{!}{0}), the
23902433
cut is scoped to the call/1 call rather than the enclosing \predref{\+}{1}.
23912434

2392-
Many Prolog implementations (including SWI-Prolog) provide not/1. The
2393-
not/1 alternative is deprecated due to its strong link to logical
2394-
negation.
2395-
2435+
Many Prolog implementations (including SWI-Prolog, see \secref{metacall})
2436+
provide the equivalent predicate not/1. The not/1 alternative is deprecated
2437+
because it is easily read as \jargon{strong negation} (``it is
2438+
known/provable that not \ldots'') rather than the intended \jargon{weak
2439+
negation}, also known as \jargon{default negation} (``it is not
2440+
known/provable that \ldots''). See also tnot/1, implementing negation
2441+
using \jargon{Well Founded Semantics}.
23962442
\end{description}
23972443

23982444
\section{Meta-Call Predicates} \label{sec:metacall}

0 commit comments

Comments
 (0)