Syntax

The syntax of Prolog programs resembles the syntax of logic programs as given before, except for some small notational differences such as the symbol separating the head from the body of a clause and the use of a dot to indicate the end of a clause. Below we list the BNF-grammar rules that describe the syntax of Prolog clauses. Terminal symbols are written bold-face. \prologindex{BNF grammer} \oprog{terms}{ \begin{tabbing} \small term ::= constant | variable | compound \mbox{\hspace{1.5cm}\\ compound ::= functor \bnfc{(} arguments \bnfc{)}\\ arguments ::= term | term \bnfc{,} arguments\\ \end{tabbing} } $=P \prologindex{terms} Terms are defined in the usual way. Prolog is, however, more liberal in what it considers as constants. Any quoted expression like 'AzY' or '*!\&' is a constant. Prolog also allows the use of a so-called anonymous variable, which is written as an underscore as for example in s(_). The anonymous variable is treated as an ordinary logical variable except that it will never become bound. Another difference is that arithmetical expressions, such as $1 + 2 and X * 5 may occur as terms. These expressions are read respectively as the terms +(1,2) and *(X,5) and are not interpreted unless they occur as arguments of the system-predicate is that may be used to evaluate these expressions. \prologindex{clauses} $= \oprog{clauses}{ \begin{tabbing} \small literal ::= predicate | predicate \bnfc{(} arguments \bnfc{)}\\ fact ::= literal\bnfc{.}\\ rule ::= head \bnfc{\bnfneck} body \bnfc{.}\\ head ::= literal\\ body ::= literal | literal \bnfc{,} body\\ \\ goal ::= \bnfc{?-} body \bnfc{.}\\ clause ::= fact | rule | goal \end{tabbing} } Clauses represent either facts, rules or goals. When Prolog-systems are used interactively, which is often the case, the symbol ?- indicating a goal is usually given as a system prompt. A fact is written as a literal ended by a dot. However, a fact may also be written in a rule-format by substituting the literal true for the body. $=

Extra-logical features

\prologindex{extra logical features} In the sections that follow, we will discuss the features that make Prolog a convenient programming language. Some of the features are rather harmless since they do not affect the logical semantics of a program. Examples of these are the list manipulation primitives and the bagof predicate that allows to collect a bag of items satisfying some predicate. Other features however, such as the cut (section cut) and negation by failure (negation), do not have a straightforward logical interpretation. These features are used to control the inference, that is the search for solutions and the amount of backtracking. Features enabling meta-programming and dynamic program modification have a similar effect of obscuring the declarative semantics. Despite the obvious arguments against the inclusion of these features, they are nevertheless often used and are an essential part of Prolog as a programming language.