Zulip Chat Archive

Stream: lean4

Topic: Trailing comma for lists


Leni Aniva (Oct 07 2023 at 00:16):

Would it make sense to create an RFC to allow lists to have a trailing comma? For example, this is not legal Lean 4:

let li = [
  item1,
  item2,
]

but if it were legal, adding new elements would not require modifications to existing lines:

let li = [
  item1,
  item2,
  item3,
  item4,
]

Using a trailing comma is supported in many languages and simplifies diff generation and automatic code generation.

I encountered an use case for it while writing unit tests.

James Gallicchio (Oct 07 2023 at 05:02):

FWIW, I actually feel less need for trailing commas in lean than other langs. If you're doing auto code generation, and you're using Lean's metaprogramming stuff, the $[ whatever ],* combinator just does the right thing, which is really helpful.

That said, I'm very much not opposed to this :) if core devs felt particularly wild, I'd even ask for leading commas, so I can write my preferred list syntax of

let list := [
  , item1
  , item2
]

which is, of course, controversial, but every functional programmer has their quirks...

James Gallicchio (Oct 07 2023 at 05:05):

or, even more out there, maybe we re-visit bulleted lists and provide bulleted list syntax for list literals, which I would very much like.

Mario Carneiro (Oct 07 2023 at 05:47):

lean has built-in support for optional trailing commas with the (p),*,? syntax, we just need to use it more in the grammar

Mario Carneiro (Oct 07 2023 at 05:47):

I think the pattern match should still Just Work in that case

Leni Aniva (Oct 07 2023 at 21:39):

Mario Carneiro said:

lean has built-in support for optional trailing commas with the (p),*,? syntax, we just need to use it more in the grammar

can you give an example of this? edit: nvm found it

Leni Aniva (Oct 07 2023 at 21:39):

James Gallicchio said:

FWIW, I actually feel less need for trailing commas in lean than other langs. If you're doing auto code generation, and you're using Lean's metaprogramming stuff, the $[ whatever ],* combinator just does the right thing, which is really helpful.

That said, I'm very much not opposed to this :) if core devs felt particularly wild, I'd even ask for leading commas, so I can write my preferred list syntax of

let list := [
  , item1
  , item2
]

which is, of course, controversial, but every functional programmer has their quirks...

I've only seen this style in C++ initializer lists where the first separator is decreed to be :

Leni Aniva (Oct 07 2023 at 22:17):

RFC: https://github.com/leanprover/lean4/issues/2635

Ioannis Konstantoulas (Oct 09 2023 at 17:23):

I have read the leading comma referred to as "Haskell-style", and so assumed it is used in Haskell code bases, but I have no clue.

Joachim Breitner (Oct 09 2023 at 19:22):

Haskell allows neither leading nor trailing comma, so a common style there is

let list :=
  [ item1
  , item2
  ]

Mario Carneiro (Oct 09 2023 at 19:23):

the leading comma thing looks like a trick people invented to make the no trailing comma issue more palatable

Leni Aniva (Oct 10 2023 at 05:04):

Joachim Breitner said:

Haskell allows neither leading nor trailing comma, so a common style there is

let list :=
  [ item1
  , item2
  ]

This is the style some people use to write C++ initializer lists so new fields don't mess up the diff

Leni Aniva (Oct 18 2023 at 22:33):

https://github.com/leanprover/lean4/pull/2643
I got it working for everything except for arrays. The analogous change to arrays breaks deriving ToJson for some reason


Last updated: Dec 20 2023 at 11:08 UTC