Skip to content

attrsets: clarify that mapAttrs maps over *leaf* attrs#212189

Merged
roberth merged 2 commits intomasterfrom
unknown repository
Jan 25, 2023
Merged

attrsets: clarify that mapAttrs maps over *leaf* attrs#212189
roberth merged 2 commits intomasterfrom
unknown repository

Conversation

@ghost
Copy link

@ghost ghost commented Jan 22, 2023

Description of changes
Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@ghost ghost requested review from edolstra, infinisil and nbp as code owners January 22, 2023 23:26
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Jan 22, 2023
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
@ghost
Copy link
Author

ghost commented Jan 25, 2023

@roberth, related to your suggestion, it appears that we do not have any kind of mapAttrs that applies the function to every node (i.e. no leaf/interior distinction). Is that correct? I went looking for one when I wrote #212186 but didn't find anything.

Is there a reason it was omitted? Everybody who needs it seems to write their own recursion.

@roberth
Copy link
Member

roberth commented Jan 25, 2023

I think that's correct. It may have to do with the lack of static or nominal types, which means that we can't consistently distinguish between an attrset that's part of a tree structure and an attrset that represents something else. We do have those predicate functions (Attrs -> Bool, List String -> Attrs -> Bool) for that, but they add a lot of noise. At that point you might as well write the algorithm yourself, with a more easily understandable end result without the indirection.

Everybody who needs it seems to write their own recursion.

In a language like Haskell, recursion schemes are kind of nice, but Nix is a different language, where different things are useful, and different things are idiomatic.

You could write a library that represents trees in a safe way, supporting a nice function like foldTree :: (AttrSet a -> a) -> Tree -> a which can hold any type a safely without a predicate function, but I think users wouldn't like the Tree representation.

@roberth roberth merged commit 683887a into NixOS:master Jan 25, 2023
@ghost ghost deleted the pr/lib/mapAttrsRecursive/comment branch January 25, 2023 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant