Next: , Previous: Tokens, Up: Syntax   [Contents]


2.5 Terms

Syntactically, terms in Mercury are exactly the same as in ISO Prolog, except that as extensions we permit higher-order terms and the introduction of infix operators by the use of grave accents (backquotes), as described below, and we support an extended set of builtin operators. See Builtin operators. Also, the constructor for list terms in Mercury is [|]/2, not ./2 as in Prolog.

Note, however, that the meaning of some terms in Mercury is different to that in Prolog. See Data-terms.

A term is either a variable or a functor.

A functor is an integer, a float, a string, a name, a compound term, or a higher-order term.

A compound term is a simple compound term, a list term, a tuple term, an operator term, or a parenthesized term.

A simple compound term is a name followed without any intervening whitespace by an open parenthesis (i.e. an open_ct token), a sequence of argument terms separated by commas, and a close parenthesis.

A list term is an open square bracket (i.e. an open_list token) followed by a sequence of argument terms separated by commas, optionally followed by a vertical bar (i.e. a ht_sep token) followed by a term, followed by a close square bracket (i.e. a close_list token). An empty list term is an open_list token followed by a close_list token. List terms are parsed as follows:

parse('[' ']') = [].
parse('[' List) = parse_list(List).
parse_list(Head ',' Tail) = '[|]'(parse_term(Head), parse_list(Tail)).
parse_list(Head '|' Tail ']') = '[|]'(parse_term(Head), parse_term(Tail)).
parse_list(Head ']') = '[|]'(parse_term(Head), []).

The following terms are all equivalent:

[1, 2, 3]
[1, 2, 3 | []]
[1, 2 | [3]]
[1 | [2, 3]]
'[|]'(1, '[|]'(2, '[|]'(3, [])))

A tuple term is a left curly bracket (i.e. an open_curly token) followed by a sequence of argument terms separated by commas, and a right curly bracket. For example, {1, '2', "three"} is a valid tuple term.

An operator term is a term specified using operator notation, as in Prolog. Operators can also be formed by enclosing a name, a module qualified name (see The module system), or a variable between grave accents (backquotes). Any name or variable may be used as an operator in this way. If fun is a variable or name, then a term of the form X `fun` Y is equivalent to fun(X, Y). The operator is left associative and binds more tightly than every operator other than ‘^’ (see Builtin operators).

A parenthesized term is just an open parenthesis followed by a term and a close parenthesis.

A higher-order term is a “closure” term, which can be any term other than a name or an operator term, followed without any intervening whitespace by an open parenthesis (i.e. an open_ct token), a sequence of argument terms separated by commas, and a close parenthesis. A higher-order term is equivalent to a simple compound term whose functor is the empty name, and whose arguments are the closure term followed by the argument terms of the higher-order term. That is, a term such as Term(Arg1, …, ArgN) is parsed as ''(Term, Arg1, …, ArgN). Note that the closure term can be a parenthesized term; for example, (Term ^ FieldName)(Arg1, Arg2) is a higher-order term, and so it gets parsed as if it were ''((Term ^ FieldName), Arg1, Arg2).


Next: , Previous: Tokens, Up: Syntax   [Contents]