PLOGHELP SYNTAX Simon Nichols May 1987 A description of the syntax of POPLOG Prolog. CONTENTS - (Use g to access required sections) -- INTRODUCTION -- COMMENTS -- TERMS -- CONSTANTS -- VARIABLES -- COMPOUND TERMS -- LISTS -- STRINGS -- OPERATORS -- SYNTAX ERRORS -- RELATED DOCUMENTATION -- INTRODUCTION ------------------------------------------------------- The syntax of POPLOG Prolog is similar to Edinburgh (or DEC10) syntax, except that it differs in two main respects. The first is that white space is not significant. In the Edinburgh standard it is used in two places: firstly, the presence or absence of blank space between a functor name or an operator and a following opening parenthesis is used to resolve the potential ambiguity associated with prefix operators. This is overcome in POPLOG Prolog by the following rule: if an operator is written in normal functional from it must be enclosed in quotes to disambiguate it from an operator. The second place where white space is significant in the Edinburgh syntax is after a clause terminator '.' where it is compulsory. In POPLOG Prolog, the clause terminator is disambiguated from the atom '.' by context. The second respect in which POPLOG Prolog differs from the Edinburgh form is in the lexical syntax of atoms. In POPLOG Prolog, escape sequences are allowed in quoted atoms to enable control characters to be embedded in them. If you would prefer standard Edinburgh syntax, it is available by loading a library: see PLOGHELP * EDINSYNT. -- COMMENTS ----------------------------------------------------------- POPLOG Prolog supports two kinds of comments. The first form uses the comment brackets: /* ... */ These comments can stretch over any number of lines, all text between the brackets being ignored by the compiler. The second form is the "end of line comment", which extends from an opening symbol to the end of the line. The opening symbol for this sort of comment is a percent sign in standard Prolog, but POPLOG Prolog also allows three (or more) semicolons in common with the other POPLOG languages: p(X), % this is an end of line comment q(Y). ;;; and so is this -- TERMS -------------------------------------------------------------- Prolog data objects are called terms. A term is either a constant, a variable or a compound term. -- CONSTANTS ---------------------------------------------------------- A constant is a number or an atom. A number can be any of the numerical data types supported by POPLOG, i.e. an integer, a biginteger, a decimal, a ddecimal (double decimal), a ratio or a complex number. For example: 1234567 123456789123456789 99.6123 99.6123456789 An atom is any sequence of characters: however, it must be put in single quotes unless it is: -- a sequence of alphabetic or numeric characters starting with a lower case letter; -- a sequence of sign characters (characters like '+', '-', '#', '$', '&', etc); -- a single separator character (characters like '!' or ';'), but not '|', ',' or any bracket character '(', ')', '[', ']', '{' or '}'. Valid unquoted atoms are: p assert + +++ ! [] The last of the above examples is a special case, and represents the empty list. The following atoms must be quoted: 'fi_+' 'a b' '123' 'Assert' '!!' Quoted atoms may include escape sequences starting with a backslash character; these are the same as those used in strings (see below). Each escape sequence represents a particular control character, which is embedded in the atom, thus: \t a tab \s a space \b a backspace \n a newline \r a carriage return \' a single quote mark \\ a backslash For other control characters, use \^ before the capital letter. For example: \^A is CTRL-A \^B is CTRL-B etc. Note that '\^?' is the code for the DEL character (127). If you precede any other character with a backslash, the effect is to embed just the character (not the backslash) in the atom. Therefore, the following are equivalent: 1 + 2 and '\+'(1, 2) \+(p) and '\\+'(p) See REF ITEMISE for details of the classification of characters and details of number formats. -- VARIABLES ---------------------------------------------------------- Variables are distinguished by an initial upper case letter or by the initial character "_", for example: X X1 Result _1 _result The anonymous variable is indicated by the underscore character "_". -- COMPOUND TERMS ----------------------------------------------------- Compound terms are the structured data objects of Prolog. A compound term comprises a FUNCTOR (called the PRINCIPAL functor of the term) and a sequence of one or more terms called ARGUMENTS. A functor is characterised by its NAME, which is an atom, and its ARITY or number of arguments. For example, the following is a compound term whose functor is named 'append' of arity 3, and whose arguments are X, Y and Z: append(X,Y,Z) An atom is considered to be a functor of arity 0. -- LISTS -------------------------------------------------------------- A list is either: 1. The atom '[]' representing the empty list; or 2. A compound term with functor '.' (cons) and two arguments which are the head and tail of the list. A list of the first three natural numbers could therefore be written: .(1, .(2, .(3, []))) Fortunately, a special notation exits for lists: they are written in square brackets, with each element separated by a comma. The above example is thus: [1,2,3] If the last element of a list is non-nil, it is separated from the preceding element(s) by a vertical bar instead of a comma. The pair .(1,2) is thus written: [1|2] It is common for the last element of a list to be a variable, especially in the head of a clause. For example: [X|L] [a,b|L] Lists are always printed using list notation. It can be noted in passing that the internal representation of lists is not the same as that of terms, i.e. lists are not implemented as nested terms of the form '.'(_,_). However this is invisible to the programmer and does not affect the functionality of POPLOG Prolog, other than improving its space and time efficiency. -- STRINGS ------------------------------------------------------------ Strings are a further special notation for lists of integers which correspond to ASCII codes (see HELP * ASCII). Strings are written in double quotes. For example: "Prolog" represents exactly the same list as: [80,114,111,108,111,103] and in fact will be printed in this form. The same escape sequences which can be used in atoms can also be used in strings, such as: "Prolog\n" See PLOGHELP * STRINGS for more details. -- OPERATORS ---------------------------------------------------------- Like lists and strings, operators are also a notational convenience. Certain functors may be written as operators. Binary functors (functors of arity 2) may be declared as INFIX operators, and unary functors (arity 1) as PREFIX or POSTFIX operators, using op/3. Thus, assuming '+' has been declared as an infix operator and '-' as a prefix operator, you can write X + Y -X rather than +(X,Y) -(X) See PLOGHELP * OP for a full description of operators, and how to declare them. N.B. If an operator is written in normal functional from it must be enclosed in quotes to disambiguate it from an operator. Thus, X + Y may alternatively be written as: '+'(X,Y) A consequence of this is that a functor whose name is a quoted atom may not be an operator. -- SYNTAX ERRORS ------------------------------------------------------ Syntax errors are detected when reading. Any term read-in by the evaluable predicate read/1 (including clauses and directives) which is syntactically incorrect results in an error message being printed as soon as it is read. The error message identifies the nature of the error, the lexical item which triggered the error and also prints the sequence of items read-in up to and including the offending item, marking the actual point where the error was detected. For example, typing: member(X, [1,2,3). results in the error: ;;; PROLOG SYNTAX ERROR: , | or ] expected in list ;;; ITEM: ) ;;; PARSING: member ( X , [ 1 , 2 , 3 <> ) If the system is consulting or reconsulting from a file other than the special file "user", the file name and the line number where the error occurred are also displayed. The clause in which the error occurred is ignored and any subsequent clauses are read-in normally. -- RELATED DOCUMENTATION ---------------------------------------------- HELP * ASCII ASCII representation of characters in POPLOG. REF * ITEMISE Classification of characters in POPLOG and details of number formats. PLOGHELP * EDINSYNT A library which provides standard Edinburgh syntax in POPLOG Prolog. PLOGHELP * OP How to declare operators, and an explanation of what is meant by the precedence and type of an operator. PLOGHELP * OPERATORS Operator declarations made when the Prolog system is loaded. PLOGHELP * STRINGS Strings in Prolog. --- C.all/plog/help/syntax --- Copyright University of Sussex 1990. All rights reserved. ----------