2 Syntax

This section describes the concrete syntax of Qu-Prolog 9.6

2.1 Constants

2.1.1 Atoms

There are four syntactic forms for atoms.

  1. A lower case letter followed by any sequence consisting of "_" and alphanumeric characters.
    For example:
    true
    semester_1
  2. Any combination of the following set of graphic characters.
    |-/+*<=>#@$\^&~`:.?
    For example:
    @<=
    ?=
    Note: This is a non-standard use of '|', which is usually reserved exclusively for list construction. A consequence is that extra spaces or brackets may be needed when constructing lists whose elements contain graphic characters.
    For example:
    [+ |X]
  3. Any sequence of characters enclosed by "'" (single quote). Single quote can be included in the sequence by writing the quote twice. "\" indicates an escape sequence, where the escape characters are case insensitive. The possible escape characters are:
    newline Meaning: Continuation.
    ^ Meaning: Same as d.
    ^character Meaning: Control character.
    dd Meaning: A two digit octal number.
    a Meaning: Alarm (ASCII = 7).
    b Meaning: Backspace (ASCII = 8).
    c Meaning: Continuation.
    d Meaning: Delete (ASCII = 127).
    e Meaning: Escape (ASCII = 27).
    f Meaning: Formfeed (ASCII = 12).
    n Meaning: Newline (ASCII = 10).
    odd Meaning: A two digits octal number.
    r Meaning: Return (ASCII = 13)
    s Meaning: Space (ASCII = 32).
    t Meaning: Horizontal tab (ASCII = 9).
    v Meaning: Vertical tab (ASCII = 11).
    xdd Meaning: A two digit hexadecimal number.

    Here are a few examples of quoted atoms.
    'hi!'
    'they''re'
    '\n'

  4. One of the following.
    !
    ;
    []
    {}

2.1.2 Numbers

The available range of integers is -(2^31-1) to 2^31-1 on a 32 bit machine and -(2^63-1) to 2^63-1 on a 64 bit machine. Integers can be represented in any of the following ways.

  1. Any sequence of numeric characters. This method denotes the number in decimal, or base 10.
    For example:
    123
  2. Base'Number, where Base ranges from 2 to 36 and Number can have any sequence of alphanumeric characters. Both upper and lower case alphabetic characters in Number are used to represent the appropriate digit when Base is greater than 10.
    For example, integer value 10 can be written as:
    2'1010
    16'A
    16'a
  3. Binary numbers can also be represented in the form 0b followed by binary digits. Similarly octal and hexadecimal numbers can be represented by 0o or 0x followed by digits.
    For example
    0b1011
    0o3170
    0x3afd
  4. 0'Character gives the character code of Character.
    For example,
    0'A
    gives the ASCII character code 65.
Double precision floating point numbers are also available and are represented using either a decimal point or scientific notation.

2.2 Variables

2.2.1 Meta Variables

Meta variables are available in three syntactic forms.

  1. An upper case letter followed by any sequence consisting of "_" and alphanumeric characters.
    For example:
    VarList
    Term1
  2. "_", followed by an upper case letter, and then any sequence consisting of "_" and alphanumeric characters.
    For example:
    _Dictionary
    _X_1
  3. "_" alone denotes an anonymous variable.

2.2.2 Object Variables

Object variable names adhere to the following EBNF grammar:


obvar-name   ---> ['!']['_'] obvar-prefix obvar-suffix
obvar-prefix ---> (lower-case-letter)+
obvar-suffix ---> (letter | digit | '_')*

Notes:

  1. When the '!' is omitted, the object-variable-prefix must have been previously declared with the predicates obvar_prefix/[1,2].
  2. When the '_' is used, the object variable is said to be anonymous.

Examples (where x is predeclared as an object-variable-prefix):
x
x0
!_y (anonymous)
!y_0_1

2.3 Compound Terms

2.3.1 Functional Notation

The compound terms are represented in this notation. A compound term is composed of a functor and a sequence of one or more arguments, which are enclosed in a pair of parenthesis. The functor and each of the arguments can be any term. For example:
sibling(jack, jill)
sort(qsort)(InList, OutList)
Functor(X, Y, Z)

In the first example, sibling is the functor and the arity, the number of arguments, of the term is 2. sort(qsort) is the functor of the second example and the functor itself is a compound term with sort as the functor.

A compound term has at least one argument.

2.3.2 Expressions

If the functor of a compound term is declared as an operator by op/[3,4], terms may be written in the style of an expression. The expression is parsed according to the precedence and associativity of the operators. For example, + and * are infix operators while \+ is a prefix operator.
Number + 2 * 3
\+ X

2.3.3 Lists

Lists are a special kind of compound term. Lists have "." as the functor and two arguments. A special list notation is provided where the elements of a list are enclosed by a pair of "[" "]" (square brackets). The elements are separated from each other by a comma. The tail of the list, which is a term, not a sequence of terms, can be separated from the rest of the list by a "|".

For example, the following represent the same list.
[apple, orange, banana]
[apple, orange|[banana]]
[apple|[orange|[banana]]]

The atom [] represents the empty list.

2.3.4 Strings

Any sequence of characters enclosed by """ is considered as a strings. Strings are semantically the same as lists of ASCII codes but are stored more efficiently. Consequently the empty string ("") is parsed as the empty list ([]) and strings can unify with lists.

Example:
| ?- X = "hello", X = [H|T].

X = "hello"
H = 104
T = [101, 108, 108, 111]

| ?- X = "".

X = []

2.3.5 {}-Lists

A special syntactic form recognized by the Qu-Prolog parser is that of {}-lists. The syntax for a {}-list is a '{' followed by a collection of terms each terminated by a full stop and white spaces followed by '}'. The full stop and white space after that last term is optional. A {}-list is represented in Qu-Prolog as a compound term whose functor is {} and whose only argument is a "comma pair" representing the elements of the {}-list.

For example, the following are {}-lists together with their internal representation.

{a. b. }    {}((a , b))
{a. b. c}   {}((a , (b , c)))
{a}         {}(a)

One use of this notation is for grouping predicate definitions together, for example as class methods, and using term expansion rules to transform the resulting {}-lists into programs.

2.4 Quantified Terms

There are two syntactic forms for quantified terms.

  1. Any quantified term can be represented by Q BV Body provided its quantifier Q has been declared by op/[3,4]. BV is a (possibly open) list of bound object variables, and Body is the body, which is another term. Each bound variable can, optionally, be followed by a ":" and a term. If there is only one bound variable, the list notation can be dropped.
    For example:
    all x flower(x, red)
    exist [x:Type] all [lo:int, hi:int] lo < f(x) < hi
  2. The escape sequence "!!" may be used to introduce a quantified term whose quantifier might not have been previously declared or whose quantifier is not an atom.
    For example,
    !!q x A
    !!Q A B
    !!integral(A,B) x T

2.5 Substitutions

The general form of a substitution term is
[t_1/x_1, ..., t_n/x_n]t_m

where t_i are terms and x_i are object variables.

The substitution [t_1/x_1, ..., t_n/x_n] is applied to the term t_m.

For example:
[f(a)/x, t/y]g(X)
[[b/z]x/y][t/z]h(A)

2.6 Programs

A program is composed of a number of predicate definitions. Each predicate definition is made up of a number of clauses. Each clause has a head and an optional body. The head is either an atom or a compound term whose functor is an atom, and the body may be an atom, a meta variable or a compound term. The head and the body are connected together by ":-".


Table of Contents Getting Started Syntax Built-in Predicates Standard Operators Notation Index