You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Let binding pattern matching to unpack records #74
The ability to unpack a record through a let-binding is tremendously powerful for building a lightweight module system. I could envision doing something like:
let { map, filter, span } = http://prelude.dhall/Listin...
Right now, my only option is to import each function individually. Not only is that more verbose, it makes it much harder to ensure consistency. While the above functions are separate, one could imagine an import where we want to make sure that both function comes from the same "version" of a library, and the only make sense (operationally) if we're using the same version.
Reacted by Kay Gosho, Devin Smith, George Thomas, Gytis Ivaskevicius and toastal
There's a correlated issue: currently Dhall records don't support types as fields (which you would probably want if you used this for a module system), but that is an old restriction that I might be able to lift now that Dhall supports type synonyms. I just need to do some additional work to verify that Dhall is sound with generalized records.
I think this is a good idea, but I just need to think a little bit more about what other changes to the language that it would entail. For example, this overlaps substantially with the let inherit feature in Nix:
letinherit(someRecord)mapfilterspan;in...
... which makes me suspect that if we add this then people may also begin to request support for inherit-like functionality for Dhall records (which I also think is probably a good idea), but if we do that then I would like the syntax to be consistent between the two like in Nix.
inherit I care less about - the pattern matching we can do in both Elm and Haskell, and if I could only have one, I'd much rather the pattern matching. But it's a fair point.
I did consider types, and I would like them to be in records, but it's not essential. If it could be done, that would be terrific.
Another thing to consider: if we can pattern match in a let-binding, should we be able to do so in an anonymous function, too?
For example:
λ({ map, filter, span } :…) →…
In fact, there are a lot of other places in the grammar that use label that might benefit from the ability to replace a label with a set of labels, such as:
Accessing multiple fields. For example record.{map, filter, span} might project out just those fields
Defining multiple fields of record literals of record types. For example { {x, y, z} = 2 } might set all three fields with the same value
Defining multiple types of union alternatives. For example < {x, y, z} : {} > would be a convenient short-hand syntax for defining an enum
The other reason I brought up anonymous functions specifically is that they would play nice with merge expressions to more closely resemble Haskell-style pattern matching:
The projection is a pretty cool idea that I could see having use in https://github.com/ocharles/dhallql, if I ever pick that project up again.
For the others I'm less opinionated on. I thought it might be best to just do this to let as it keeps the language change small and we can always add in the extra pattern matching later. It's much harder to take it out if it's not a good feature.
Yeah, to clarify, we don't have to implement all of those features in one go. But I'd like to do some look-ahead design so that if we do add those features later on we aren't painting ourselves into a corner with a bad initial choice of syntax
If we have exhaustiveness checking, I believe we'll also need a way of expressing "I know this field exists but I don't want to use it" without opting out of the exhaustive check completely via ...
Maybe this syntax could work for that:
let { foo, bar, (baz) } = { foo =1, bar =2, baz ="bla" }
in foo + bar
More generally we may want to rename the fields; this would make it closer to a form of pattern-matching. We'd get something like that, similar to what Rust does:
Where the _ is just a binding to a variable named _, but by convention this indicates that the variable is not used, and would replace you (baz) suggestion.
On Sat, 21 Dec 2019, 4:06 pm Simon Jakobi, ***@***.***> wrote:
@Nadrieril <https://github.com/Nadrieril> Ah, yes, that's better!
dhall lint should also detect unused bindings here, and turn e.g.
let { foo, bar } = { foo = 1, bar = 2 }
in foo
into
let { foo, bar: _ } = { foo = 1, bar = 2 }
in foo
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#74?email_source=notifications&email_token=AAAFDDXQNFUDMDTG4U4BBKLQZY5JBA5CNFSM4EMUCI2KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEHO6YVY#issuecomment-568192087>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAFDDXPFC7BEQIN2NPTJHLQZY5JBANCNFSM4EMUCI2A>
.
On Sat, 21 Dec 2019, 4:20 pm Simon Jakobi, ***@***.***> wrote:
let { foo, bar as renamed_bar, baz as _ } = { foo = 1, bar = 2, baz = "bla" }
in foo + renamed_bar
seems reasonable to me, although I don't have much of a preference.
I'm actually not a huge fan of the
baz as _
bit. I think I'd slightly prefer
let { foo, bar as renamed_bar, (baz) } = { foo = 1, bar = 2, baz = "bla" }
in foo + renamed_bar
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#74?email_source=notifications&email_token=AAAFDDSCHOVBWDY6RGJI6PDQZY66FA5CNFSM4EMUCI2KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEHO7APA#issuecomment-568193084>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAFDDQY6SX4FX2AU2NBFWTQZY66FANCNFSM4EMUCI2A>
.
It would work the same way for foralls as for lambdas, e.g. forall ({ a, b, c } : { a : Type, b : Type, c : Type }) -> (a -> b) -> (b -> c) -> (a -> c). That way all the methods of introducing variables into context would be consistent.
I feel like I had more comments, but I can't remember them right now …
I’d prefer .. over ..., simply because Haskell and Rust use it.
I prefer let { foo as bar } = { foo = 42 };, over let { foo = bar } = { foo = 42; } because
There’s 3 = in the second one, two of them must have an expression on the right side, the other one has a new symbol name on the right side
I could never remember which one is the old and which the new name for the = form, the as form doesn’t have that problem.
I like the idea of explicitely ignoring fields to keep exhaustiveness-checking
The syntax idea with parens { foo, (bar) } is not good though, because () should be reserved for grouping of expressions (and the eye parses it as that).
The syntax should be { foo, bar as _ } and _ the special “throw value away” indicator.
that could be a tad long in practice, so we should consider { foo, ^bar } (like inversions in regex groups [^abc])
Seems like we're pretty divided about the question of .. or ... for "ignore the remaining fields".
To quantify our preferences, please react on this comment
with 🚀 if you prefer ..
with 😄 if you prefer ... and its Unicode version …
with 👀 if you want something else (and specify what you want in a comment)
The outcome will be non-binding of course – it's just to inform the final decision.
(If you think that this vote is a bad idea, please also say so in a comment)
Reacted by Simon Jakobi, gabby, Alessandro Marrella, Jonas Weber, ScribblyBirb, Profpatsch, Ari Becker, Ollie Charles, Stephen Paul Weber and Cristian Filipov
Sorry if I missed this, but in addition to using ... to ignore fields, I would really like to be able use ...<ident> to collect all unused field in a new record. For example:
let myRec = { a =1, b =2, c =3 }
let { a, ...rec } = myRec
let example1 = assert : a ===1let example2 = assert :rec=== { b =2, c =3 }
for example when having a function that takes a record as argument when most of the content is just for a second function you want to call. You could splice off the fields you want and give the rest to next call
I'd love to see a more concrete motivating example!
It feels a bit like a "negated" variant of field projection. Given a record r of type
{ a : Bool, b : Text, c : Natural }
where you want to keep only the a and b fields, you could write either
r.{ a, b }
or
let { c, ...r2 } = r in r2
I think you can do something similar in Python, but I'd think that many of the Python use cases wouldn't translate so well to Dhall due to the stricter type system.
If we want to support this, we could consider re-using it for the "ignore the remaining fields" thing, with
let { a, ..._ } = { a = 1, b = 2 } in a
I'd argue that we should still disallow it when no fields are left. E.g. this should be illegal:
We have a pattern where we have dhall-kubernetes objects organized for export:
let imports =./imports.dhall
letPrelude= imports.PreludeletKubernetes= imports.Kuberneteslet foo = { service =./foo/service.dhall , deployment =./foo/deployment.dhall , objects = [ "service" , "deployment" ] }
let bar = { service =./bar/service.dhall , deployment =./bar/deployment.dhall , objects = [ "service" , "deployment" ] }
let prependPrefix =\(prefix:Text) ->\(objects:Text) ->Prelude.List.mapTextText (\(object:Text) ->"${prefix}.${object}") objects
in
{ foo = foo.{service, deployment}
, bar = bar.{service, deployment}
, objects = prependPrefix "foo" foo.objects # prependPrefix "bar" bar.objects
} : { foo : { service :Kubernetes.Service.Type, deployment:Kubernetes.Deployment.Type }
, bar : { service :Kubernetes.Service.Type, deployment:Kubernetes.Deployment.Type }
, objects :ListText
}
The idea being to run dhall-to-json on the whole lot and then use jq to iterate over objects so that they can be passed to kubectl apply -f.
The issue we currently have is in the foo.{service.deployment} part - adding manifests to foo forces changes in the exporting record (it's not a big deal but whatever).
Being able to write let { objects, foo } = ./foo.dhall let foo-objects = objects and repeating for bar solves that nicely.
@Profpatsch: I agree that is something we should get to, but I want to note that once you have let-related punning then you get halfway there by permitting:
\(foo : { a :Text, b :Int }) ->let { a, b } = foo in…
Indeed, support for punning function arguments could be standardized in such a way that it desugars to let punning
Activity
Gabriella439 commentedon Jan 19, 2018
There's a correlated issue: currently Dhall records don't support types as fields (which you would probably want if you used this for a module system), but that is an old restriction that I might be able to lift now that Dhall supports type synonyms. I just need to do some additional work to verify that Dhall is sound with generalized records.
I think this is a good idea, but I just need to think a little bit more about what other changes to the language that it would entail. For example, this overlaps substantially with the
let inheritfeature in Nix:... which makes me suspect that if we add this then people may also begin to request support for
inherit-like functionality for Dhall records (which I also think is probably a good idea), but if we do that then I would like the syntax to be consistent between the two like in Nix.ocharles commentedon Jan 19, 2018
inheritI care less about - the pattern matching we can do in both Elm and Haskell, and if I could only have one, I'd much rather the pattern matching. But it's a fair point.I did consider types, and I would like them to be in records, but it's not essential. If it could be done, that would be terrific.
Gabriella439 commentedon Jan 19, 2018
Another thing to consider: if we can pattern match in a
let-binding, should we be able to do so in an anonymous function, too?For example:
λ({ map, filter, span } : …) → …In fact, there are a lot of other places in the grammar that use
labelthat might benefit from the ability to replace a label with a set of labels, such as:record.{map, filter, span}might project out just those fields{ {x, y, z} = 2 }might set all three fields with the same value< {x, y, z} : {} >would be a convenient short-hand syntax for defining an enumGabriella439 commentedon Jan 19, 2018
The other reason I brought up anonymous functions specifically is that they would play nice with
mergeexpressions to more closely resemble Haskell-style pattern matching:ocharles commentedon Jan 19, 2018
The projection is a pretty cool idea that I could see having use in https://github.com/ocharles/dhallql, if I ever pick that project up again.
For the others I'm less opinionated on. I thought it might be best to just do this to
letas it keeps the language change small and we can always add in the extra pattern matching later. It's much harder to take it out if it's not a good feature.Gabriella439 commentedon Jan 19, 2018
Yeah, to clarify, we don't have to implement all of those features in one go. But I'd like to do some look-ahead design so that if we do add those features later on we aren't painting ourselves into a corner with a bad initial choice of syntax
Profpatsch commentedon Sep 7, 2019
I would also add an exhaustiveness check if
..is not used (see https://discourse.dhall-lang.org/t/exhaustive-record-field-matching/75/1 for motivation).e.g.
let { foo } = { foo = "abc", bar = "cde" }=> error, fieldbarexists but was not matchedlet { foo, .. } = { foo = "abc", bar = "cde" }=> no error, partial matching was requestedThis is particularly helpful if dhall detects unused bindings and warns accordingly.
sjakobi commentedon Dec 21, 2019
If we have exhaustiveness checking, I believe we'll also need a way of expressing "I know this field exists but I don't want to use it" without opting out of the exhaustive check completely via
...Maybe this syntax could work for that:
Nadrieril commentedon Dec 21, 2019
More generally we may want to rename the fields; this would make it closer to a form of pattern-matching. We'd get something like that, similar to what Rust does:
Where the
_is just a binding to a variable named_, but by convention this indicates that the variable is not used, and would replace you(baz)suggestion.sjakobi commentedon Dec 21, 2019
@Nadrieril Ah, yes, that's better!
dhall lintshould also detect unused bindings here, and turn e.g.into
ocharles commentedon Dec 21, 2019
sjakobi commentedon Dec 21, 2019
seems reasonable to me, although I don't have much of a preference.
I'm actually not a huge fan of the
bit. I think I'd slightly prefer
ocharles commentedon Dec 21, 2019
sjakobi commentedon Dec 21, 2019
@ocharles The problem with
..or...is that it opts out of the exhaustiveness check, which is reasonable for imports, but not for some other uses.36 remaining items
MonoidMusician commentedon Jan 7, 2020
It would work the same way for foralls as for lambdas, e.g.
forall ({ a, b, c } : { a : Type, b : Type, c : Type }) -> (a -> b) -> (b -> c) -> (a -> c). That way all the methods of introducing variables into context would be consistent.I feel like I had more comments, but I can't remember them right now …
Profpatsch commentedon Jan 19, 2020
Bikeshedding input:
..over..., simply because Haskell and Rust use it.let { foo as bar } = { foo = 42 };, overlet { foo = bar } = { foo = 42; }because=in the second one, two of them must have an expression on the right side, the other one has a new symbol name on the right side=form, theasform doesn’t have that problem.{ foo, (bar) }is not good though, because()should be reserved for grouping of expressions (and the eye parses it as that).{ foo, bar as _ }and_the special “throw value away” indicator.{ foo, ^bar }(like inversions in regex groups[^abc])Non-bikeshedding input:
with, see my comment on the other issue.sjakobi commentedon Jan 19, 2020
Seems like we're pretty divided about the question of
..or...for "ignore the remaining fields".To quantify our preferences, please react on this comment
.....and its Unicode version…The outcome will be non-binding of course – it's just to inform the final decision.
(If you think that this vote is a bad idea, please also say so in a comment)
Profpatsch commentedon Jan 19, 2020
Oh right, I didn’t think about that. In that case I’m all in favor of
....singpolyma commentedon Jan 22, 2020
I have concerns about
...looking like.., but I do buy that_might be problematic so if others are happy, I'm happy.jvanbruegge commentedon Mar 11, 2020
Sorry if I missed this, but in addition to using
...to ignore fields, I would really like to be able use...<ident>to collect all unused field in a new record. For example:sjakobi commentedon Mar 11, 2020
@jvanbruegge Interesting! In what kind of situations would you want to use that?
jvanbruegge commentedon Mar 11, 2020
for example when having a function that takes a record as argument when most of the content is just for a second function you want to call. You could splice off the fields you want and give the rest to next call
sjakobi commentedon Mar 11, 2020
I'd love to see a more concrete motivating example!
It feels a bit like a "negated" variant of field projection. Given a record
rof typewhere you want to keep only the
aandbfields, you could write eitheror
I think you can do something similar in Python, but I'd think that many of the Python use cases wouldn't translate so well to Dhall due to the stricter type system.
If we want to support this, we could consider re-using it for the "ignore the remaining fields" thing, with
I'd argue that we should still disallow it when no fields are left. E.g. this should be illegal:
ari-becker commentedon Mar 12, 2020
@sjakobi it would be helpful for us.
We have a pattern where we have
dhall-kubernetesobjects organized for export:The idea being to run
dhall-to-jsonon the whole lot and then usejqto iterate overobjectsso that they can be passed tokubectl apply -f.The issue we currently have is in the
foo.{service.deployment}part - adding manifests tofooforces changes in the exporting record (it's not a big deal but whatever).Being able to write
let { objects, foo } = ./foo.dhall let foo-objects = objectsand repeating forbarsolves that nicely.Profpatsch commentedon Apr 14, 2020
I’m noticing that in order for punning to be nice, we need record punning in function arguments,
\({ a : Text, b : Int })should putaandbin scope.Right now you have to do
which means you cannot pun.
This is similar to how
{ a, b }: …in nix is a function which takes an attrset and putsaandbin scope, so they can be “punned” withinheritlater.Gabriella439 commentedon Apr 14, 2020
@Profpatsch: I agree that is something we should get to, but I want to note that once you have
let-related punning then you get halfway there by permitting:Indeed, support for punning function arguments could be standardized in such a way that it desugars to
letpunningGabriella439 commentedon Aug 24, 2021
There is a work-in-progress PR for this here: #1211
Rename `intercalate` to `concatSep` (dhall-lang#74)