Search code examples
syntaxprologcurly-braces

Meaning of curly brakets in Prolog


What is the meaning of curly brackets in Prolog:

{a,b,c}

I see I can use them, but do they describe tuples or what?


Solution

  • Prolog is almost old as C... from the very beginning, it took a peculiar approach to syntax. Since it's a so called homoiconic language, everything is a term. Therefore, we are sure that {a,b,c} is also a term. In my old Prolog interpreter, I handled '{' and '}' as a separate pair of operators, so being able to process DCG rules, as explained in Clocksin/Mellish Programming in Prolog appendix D (beware, I googled for authors and title, the book is an unofficial copy, and the book I used was much older, maybe 1985...)

    Let's explore some syntax on SWI-Prolog REPL:

    ?- functor({a,b,c},F,N).
    F = {},
    N = 1.
    

    so, {a,b,c} it's just a compound, and a,b,c its argument:

    ?- {a,b,c} =.. Syntax.
    Syntax = [{}, (a, b, c)].
    

    also write_canonical helps when exploring syntax details, but in this case it doesn't make so apparent what the functor is:

    ?- write_canonical({a,b,c}).
    {','(a,','(b,c))}
    

    A noteworthy SWI-Prolog extension, dicts, uses {} to build a clean object representation...