Skip to content

Let binding pattern matching to unpack records #74

Open

Description

@ocharles

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/List
in ...

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.

Activity

  1. Gabriella439 commented on Jan 19, 2018

    @Gabriella439
    Contributor

    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:

    let inherit (someRecord) map filter span;
    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.

  2. ocharles commented on Jan 19, 2018

    @ocharles
    MemberAuthor

    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.

  3. Gabriella439 commented on Jan 19, 2018

    @Gabriella439
    Contributor

    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
  4. Gabriella439 commented on Jan 19, 2018

    @Gabriella439
    Contributor

    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:

    merge
    { Constructor1 = λ({ field1A, field1B } : …) → …
    , Constructor2 = λ({ field2A } : …) → …
    }
    …
    
  5. ocharles commented on Jan 19, 2018

    @ocharles
    MemberAuthor

    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.

  6. Gabriella439 commented on Jan 19, 2018

    @Gabriella439
    Contributor

    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

  7. Profpatsch commented on Sep 7, 2019

    @Profpatsch
    Contributor

    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, field bar exists but was not matched
    • let { foo, .. } = { foo = "abc", bar = "cde" } => no error, partial matching was requested

    This is particularly helpful if dhall detects unused bindings and warns accordingly.

  8. sjakobi commented on Dec 21, 2019

    @sjakobi
    Collaborator

    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
  9. Nadrieril commented on Dec 21, 2019

    @Nadrieril
    Member

    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:

    let { foo, bar: renamed_bar, baz: _ } = { foo = 1, bar = 2, baz = "bla" }
    in foo + renamed_bar

    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.

  10. sjakobi commented on Dec 21, 2019

    @sjakobi
    Collaborator

    @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
    
  11. ocharles commented on Dec 21, 2019

    @ocharles
    MemberAuthor
  12. sjakobi commented on Dec 21, 2019

    @sjakobi
    Collaborator
    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
  13. ocharles commented on Dec 21, 2019

    @ocharles
    MemberAuthor
  14. sjakobi commented on Dec 21, 2019

    @sjakobi
    Collaborator

    @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.

  15. 36 remaining items

  16. MonoidMusician commented on Jan 7, 2020

    @MonoidMusician
    Collaborator

    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 …

  17. Profpatsch commented on Jan 19, 2020

    @Profpatsch
    Contributor

    Bikeshedding input:

    • 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])

    Non-bikeshedding input:

  18. sjakobi commented on Jan 19, 2020

    @sjakobi
    Collaborator

    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)

  19. Profpatsch commented on Jan 19, 2020

    @Profpatsch
    Contributor
    • and its Unicode version …

    Oh right, I didn’t think about that. In that case I’m all in favor of ....

  20. singpolyma commented on Jan 22, 2020

    @singpolyma
    Collaborator

    I have concerns about ... looking like .., but I do buy that _ might be problematic so if others are happy, I'm happy.

  21. jvanbruegge commented on Mar 11, 2020

    @jvanbruegge

    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 === 1
    let example2 = assert : rec === { b = 2, c = 3 }
  22. sjakobi commented on Mar 11, 2020

    @sjakobi
    Collaborator

    @jvanbruegge Interesting! In what kind of situations would you want to use that?

  23. jvanbruegge commented on Mar 11, 2020

    @jvanbruegge

    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

  24. sjakobi commented on Mar 11, 2020

    @sjakobi
    Collaborator

    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:

    let { a, ..._ } = { a = 1 } in a
    
  25. ari-becker commented on Mar 12, 2020

    @ari-becker
    Collaborator

    @sjakobi it would be helpful for us.

    We have a pattern where we have dhall-kubernetes objects organized for export:

    let imports = ./imports.dhall
    let Prelude = imports.Prelude
    let Kubernetes = imports.Kubernetes
    let 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.map Text Text (\(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 : List Text
    } 

    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.

  26. Profpatsch commented on Apr 14, 2020

    @Profpatsch
    Contributor

    I’m noticing that in order for punning to be nice, we need record punning in function arguments,
    \({ a : Text, b : Int }) should put a and b in scope.

    Right now you have to do

    \(foo : { a : Text, b : Int }) 
      -> { a = foo.a
         , b = foo.b
    

    which means you cannot pun.

    This is similar to how { a, b }: … in nix is a function which takes an attrset and puts a and b in scope, so they can be “punned” with inherit later.

  27. Gabriella439 commented on Apr 14, 2020

    @Gabriella439
    Contributor

    @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

  28. Gabriella439 commented on Aug 24, 2021

    @Gabriella439
    Contributor

    There is a work-in-progress PR for this here: #1211

  29. self-assigned this
    on Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions