dhall-lang
dhall-lang copied to clipboard
Syntactic sugar for record literals with repeated values
Summary
Allow
{ [ a, b ] = x }
as a shortcut for
{ a = x, b = x }
Motivation
The main purpose is to support a variant of or-patterns in merge-expressions. A repetitive merge-expression like
merge { A = x, B = y, C = z, D = y, E = y } r
can be shortened to
merge { A = x, C = z, [ B, D, E ] = y } r
I contrast to wildcards (#961), merges using this feature aren't prone to subtle bugs when new constructors are added to the union type of the scrutinee.
Desugaring
I think this could be implemented as a step before the dotted field expansion. So { [ a, b.c ] = x } would be expanded to { a = x, b.c = x }.
In principle it seems possible to allow the field groupings within dotted field paths, e.g. { a.[ b, c ] = x } or even { a.[ b, c.[ d, e.f ] ].g = x }. But this would make the implementation more complicated and increase the potential for confusion.
Does anyone know a language that has a similar feature BTW?
@sjakobi: I'm not aware of this feature at the record level, but as you noted in #961 there are languages that support this at the pattern level
It's still not clear to me what problem this feature is trying to solve, though. As I noted in #961, I don't think this addresses the original use case, as pattern matches still require O(N) code complexity before and after this feature (where N = the number of alternatives)
Most of the space savings of this features can be obtained by defining a shared right-hand-side with a let binding:
let x = largeExpression
in { A = x, B = x, C = x, D = x, E = x }
I think the main idea is to offer an alternative to wildcards that is shorter than spelling out the handler for each alternative separately. It's also useful to show that many constructors share the same handler.
@sjakobi as in languages with or-patterns? Or such combined records.
@sjakobi as in languages with or-patterns? Or such combined records.
@vmchale Could you clarify your question? I don't understand it.
Oh, as in I was wondering if you were looking for examples of languages with or-patterns.
Oh, as in I was wondering if you were looking for examples of languages with or-patterns.
So far I'm aware of Rust, the GHC proposal and Dickinson (which you mentioned in https://discourse.dhall-lang.org/t/or-patterns-in-dhall/338). Are there more languages?
I'm more curious about languages that have similar short-hands for records or dictionaries. I guess Python's dictionary comprehensions would count too.
I think OCaml has them as a full-fledged feature! But I do not know of any short-hands for records, unfortunately.
Since we keep wanting better pattern-matching features, could we maybe drop the merge keyword entirely in favor of some real pattern-matching syntax? e.g.
let bar = match foo {
Some x => x+1,
None => 0
}
in ...
Then we could add or-patterns easily, and this would open the door to wildcards, nested patterns and more if we want those. That's a big step away from dhall's current minimalism though. But since dhall is already quite a beast to implement anyways, I don't know how much to weigh this requirement against potential big usability improvements.
@Nadrieril: Yeah, it really comes down to whether or not it is difficult for other implementations to keep up with the standard. I personally have no issue implementing these features, but I don't know how the other language bindings feel.