Search code examples
prologdcg

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


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."

Solution

  • 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.