Hi. Is there a specific reason why trailing commas are not supported? Do they create ambiguities in Prolog’s syntax, contradict the specification or something else?
You mean as in [a,b,c,]
being equivalent to [a,b,c]
?
Yes. In many programming languages it is considered conventional to use them for multiline expressions for consistency and ease of editing as in:
[
something,
something_else,
finally,
]
So, I was a bit surprised to discover they are not supported in Prolog. Is there a technical limitation, do they engender some class of errors maybe? Or is it just the way it is?
Your dealing with a language that has an operator grammar. The other languages probably don’t. There’s huge advantages to an operator grammar where you get define your own operators. This is from my Prolog manual (SWIPL may be different):
Note that the comma (‘,’) that is used to separate functor arguments, or list elements, is actually an operator: ‘,’/2 with priority 1000 and associativity xfy. This means that any term with a principle functor of 1000 or more in an argument or list will have to be written with parenthesis.
Trailing commas are great for developer ergonomics. If you want to write declarative structures (for example, something HTML-like) in Prolog, having to manage commas when moving content around is a pretty annoying waste of time.
I wonder if there’s a workaround where ','
can be treated as a postfix op (in additional to its infix) and effectively be a no-op?
One ugly trick is to do something like this:
X = [ a
, b
, c
]
The only popular language that I can think of that allows trailing commas is Python – I agree that it’s handy, but trailing commas do complicate the grammar (e.g., you’re not allowed to write [1,,2]
or [1,2,,]
or [,]
or [,1,2]
… and things get more complicated with tuples inside parentheses such as (1,2)
and (1,)
).
I’m not sure it is that hard. Inside parenthesis, etc., the SWI-Prolog parser looks for the closing token, i.e., ‘)’, for lists ‘|’ or ‘]’, etc. In theory a rule saying we also match using ,' <white>* <end>
probably has limited impact.
We also have JavaScript Those are the only two I know that have it.
Often more seriously is subgoal reordering or commenting the last subgoal. Some people solve that using the code below. I don’t really like it Rarely I use it temporary if I’m experimenting.
p :-
q,
r,
true.
Presumably the parser could also easily handle (but would anyone use it?):
p :-
q,
r,
.
Isn’t the newest trend, your ugly list trick, to write it, no trailing comma needed:
p
:- q
, r
, s
, t
.
See also:
https://twitter.com/HappywalkerRBLX/status/1265765519338110977
Not just Python and JavaScript, but also Ruby, Go, and Rust. They also support trailing commas for function call parameters.
Often more seriously is subgoal reordering or commenting the last subgoal.
I agree @peter.ludemann’s example is not pretty, but it works and is better than nothing. I might un-indent the period if it existed, personally.
Isn’t the newest trend, your ugly list trick, to write it, no trailing comma needed:
This only converts “unable to reorder last subgoal” to “unable to reorder first subgoal”, unfortunately.
The requested trailing comma might also be named trailing Oxford comma.
Added below for illustration in a natural language context: