What is the meaning of a double slash `//` after the predicate name in Prolog, appearing in the context of DCGs?

Viewed 427

Some "predicate indicators" (this is the ISO Standard terminology for a syntactic name/arity expression denoting a predicate or functor (both of these terms are equivalent) are not content with a single slash, but actually take two. This always occurs in the context of DCGs. Examples:

  • syntax_error//1: "Throw the syntax error Error at the current location of the input. This predicate is designed to be called from the handler of phrase_from_file/3."
  • js_expression(+Expression)//: "Emit a single JSON argument."
2 Answers

According to the recent drafts WDTR 13211-3 (3.19) this is called a non-terminal indicator. Similar to a predicate indicator (3.131) it is used to denote one particular non-terminal.

Note that most implementations translate a non-terminal nt//n to a predicate nt/n+2. You cannot rely on the precise way of translation, though. And thus the outcome of calling a non-terminal directly by calling the corresponding predicate, that is, with the same name and two extra arguments is not defined. In particular the second additional argument has to be handled with care. A direct use might violate steadfastness, in particular when using .

What is the meaning of a double slash // after the predicate name in Prolog, appearing in the context of DCGs?

It is used by the term rewrite system of Prolog (SWI-Prolog src), but for a person it lets you know that the predicate is a DCG and has two hidden arguments added to the end of the predicate.

For example here is a very simple DCG that has 1 visible argument.

simple_dcg(X) -->
    { X is 1 + 2 }.

When the listing is seen

?- listing(simple_dcg).
simple_dcg(X, A, B) :-
    X is 1+2,
    B=A.

true.

the two extra hidden arguments (A, B) appear.


If you have been following my EDCG questions on SWI-Prolog forum then you know it can get much more complicated.

Related