Prolog syntax (approximate)

A program, or database, in Prolog consists of one or more predicates; each predicate consists of one or more clauses. A clause is a base clause if it is unconditionally true, that is, it has no ``if part.''

	<program> ::= <predicate> | <program><predicate>

	<predicate> ::= <clause> | <predicate><clause>

	<clause> ::= <base clause> | <nonbase clause>

Two clauses belong to the same predicate if they have the same functor (name) and the same arity (number of arguments). Thus, mother(jane) and mother(jane, jim) are different predicates.

	<base clause> ::= <structure> .

	<nonbase clause> ::= <structure> :- <structures> .

	<structures> ::= <structure> | <structure> , <structures>

A structure is a functor followed by zero or more arguments; the arguments are enclosed in parentheses and separated by commas. There must be no space between the functor and the opening parenthesis! If there are no arguments, the parentheses are omitted.

	<structure> ::= <name> | <name> ( <arguments> )

	<arguments> ::= <argument> | <argument> , <arguments>

Arguments may be any legal Prolog values or variables.

A variable is written as a sequence of letters and digits, beginning with a capital letter. The underscore (_) is considered to be a capital letter.

An atom is any sequence of letters and digits, beginning with a lowercase letter. Alternatively, an atom is any sequence of characters, enclosed by single quotes ('); an internal single quote must be doubled. Examples: cat, r124c41, max_value, maxValue, 'max value', 'Don''t go'.

As syntactic sugar, Prolog allows certain infix operators: ',' (comma), ';' (semicolon), ':-' (turnstile), +, -, *, /, =, ==, and many others. These are the same as the operator written as the functor of a structure; for example, 2+2 is the same as '+'(2,2).

Comments begin with the characters /* and end with */. Comments are not restricted to a single line, but may not be nested.

Example: /* This is a comment. */

Previous page
Table of contents
Next page

Copyright © 1995 by David Matuszek
All rights reserved.
Last updated July 15, 1995