Skip to content

Refactoring proposal on Library name #8567

Open

Description

@theobat

While trying to move stack closer to cabal design (to enable backpack in stack in the long term), I stumbled upon this in cabal :

-- In PackageDescription
library :: Maybe Library
subLibraries :: [Library]

And then there is :

-- in Library
libName :: LibraryName

-- in LibraryName
data LibraryName = LMainLibName
                 | LSubLibName UnqualComponentName

Why don't we statically enforce the Main vs Sub nature of the Library ?

data LibraryMain = LMain
data LibrarySub = LSub UnqualComponentName

-- and : 
library :: Maybe (Library LibraryMain)
subLibraries :: [Library LibrarySub]

-- and : 
data Library libType = Library { libName :: libType, etc }

It seems like a relatively simple change but I'm still not very familiar with the cabal codebase yet so ... what's your opinion ?

Activity

  1. theobat commented on Nov 3, 2022

    @theobat
    Author

    It seems a bit more involved than I thought, Library type beeing used a lot, but i can propose a draft if people see the interest ?

  2. fgaz commented on Nov 3, 2022

    @fgaz
    Member

    Can we instead unify library and subLibraries into a single libraries :: [Library] field, or maybe even :: Map LibraryName Library?

    I didn't dare change it when I added the LibraryName type, but as far as I know the distinction isn't really needed.

  3. theobat commented on Nov 3, 2022

    @theobat
    Author

    Well they have differences that we can statically enforce since sub libraries must have a name (which currently is not enforced) and main libraries cannot have a name (identically not enforced).
    So joining them in the same list is going against their current structural differences, in my opinion.
    Now of course, maybe that's not what was intended and we allow main libraries to have a name and give the Library type a simple isMain boolean, but in the current situation my proposal seems like a better fit.

    PS : Also, having a list like you said enable yet another structural violation that is main libraries must be unique, if they exist.

    PS2: About the Map data structure it solves the unicity and naming requirements above, but it means we pay extra for acessing the main library which I don't find particularly good since I expect main libraries to be accessed very often... Also It allows for different library names in the keys and in the value which is arguably weird as a structure (not that it's a real risk, just that it's a bit hard to grasp on first sight, I think).

  4. theobat commented on Nov 11, 2022

    @theobat
    Author

    jaror on discourse proposed an even better design with GADTs :
    https://discourse.haskell.org/t/why-dont-we-narrow-type-arguments-more-frequently/5299/4

    I'm still motivated to implement this but I'm waiting for some sort of validation ?

  5. fgaz commented on Nov 12, 2022

    @fgaz
    Member

    More type safety is nice, and I welcome any improvement in that direction, but the increase in complexity is nonzero and we fist have to ask ourselves why we need it.

    In this case, we are trying to enforce the difference between main (or unnamed) libraries and "sublibraries" (or named libraries), but since both can be public now is there an actual difference in how we use them? There is only one that I can think of: the main unnamed library defaults to visibility: public, while named libraries default to visibility: private. That difference is already handled where we do the defaulting, by looking at the library name, and I'm not sure it could be made safer through types.
    Then, I expect the rest of the code that uses library and subLibraries to be like

    doThing library
    doThing <$> subLibraries

    with no special treatment for library at all. If that's the case, I think merging the two fields is perfectly fine.
    If there are other differences, it could even be a bug: anywhere a main/named library can be used, I expect to be able to use a sub/unnamed library!

    The only way to find out is to actually try making the change.

  6. fgaz commented on Nov 12, 2022

    @fgaz
    Member

    Now of course, maybe that's not what was intended and we allow main libraries to have a name and give the Library type a simple isMain boolean

    My point is that we don't even need that. We can just look at the name.

    Also, having a list like you said enable yet another structural violation that is main libraries must be unique, if they exist.

    subLibraries already violates the constraint of uniqueness of library names.

    About the Map data structure it solves the unicity and naming requirements above, but it means we pay extra for acessing the main library which I don't find particularly good since I expect main libraries to be accessed very often

    That's a good point but we'd have to prove that with benchmarks.

    Tbh I think a list is fine. The map doesn't really solve the duplication problem at the type level because of the duplication you point out, and I'd expect its size to be (typically) small enough that a list would actually be faster than a map and comparable to the current situation.

  7. theobat commented on Nov 12, 2022

    @theobat
    Author

    My point is that we don't even need that. We can just look at the name.

    And my point is we should not even need to look at the name since the cabal file tell us by construction which is a main and which is a sub.

    subLibraries already violates the constraint of uniqueness of library names.

    I didn't mention sub library names uniqueness here, I mentioned the fact that you can't have unnamed sublibraries. Maybe I'm missing something ? If not my point stands.

    That's a good point but we'd have to prove that with benchmarks.
    Tbh I think a list is fine. The map doesn't really solve the duplication problem at the type level because of the duplication you point out, and I'd expect its size to be (typically) small enough that a list would actually be faster than a map and comparable to the current situation.

    Ok I understand your position and everything, but really I'm not interested in merging the two distinct data structures (made distinct by cabal in its very file design) as identical in the PackageDescription, I feel this is would not be an improvement at all.
    Besides I don't see how this is a simplification, if almost all operations are targeting both, we can simply create a function merging the two in 2 seconds and use it everywhere it make sense (if that's no already the case), while retaining the potential for easy differentiation if needed :

    allLibraries pkg  = catMap (library pkg) ++ subLibraries pkg

    I mean, again, maybe this distinction is in itself bad and the very cabal file structure should be revisioned to merge their layout/distinctions, but for now, it's just merging actual differences we can statically enforce. In the stack codebase there are maybe a hundred places where we specifically target the main library. Maybe that's just because stack is old and clunky I don't know, but having to run a find search in a list with a boolean everywhere we used to simply apply a function does not seem like progress to me.

    So I'll leave it to this then. Have a nice day.

  8. fgaz commented on Nov 12, 2022

    @fgaz
    Member

    And my point is we should not even need to look at the name since the cabal file tell us by construction which is a main and which is a sub.

    What construction? In my comment I've been calling them "named" and "unnamed" exactly because with the multiple public libraries feature there isn't a meaningful distinction anymore. They differ in name, just like library foo and library bar do, and in default(!) visibility.

    I didn't mention sub library names uniqueness here

    No, but I argue that library name uniqueness is a superset of main library uniqueness.

    Ok I understand your position and everything, but really I'm not interested in merging the two distinct data structures (made distinct by cabal in its very file design) as identical in the PackageDescription, I feel this is would not be an improvement at all.
    [...]
    I mean, again, maybe this distinction is in itself bad and the very cabal file structure should be revisioned to merge their layout/distinctions

    There is no distinction between the two structures. Both are Library, and both are described in the same (admittedly outdated) doc section. The layout is the same.

    I do argue that the distinction of fields is bad, but changing that won't touch the .cabal file format.

    Besides I don't see how this is a simplification, if almost all operations are targeting both, we can simply create a function merging the two in 2 seconds and use it everywhere it make sense (if that's no already the case), while retaining the potential for easy differentiation if needed

    With the current design, I think that differentiation shouldn't be needed at all. It'd be like having a libraryNamedFoo field that's only for libraries named "foo".

    There is a difference, but that's only in how we produce the library, specifically in how we set the default visibility. It shouldn't make a difference at all when processing a library externally (that's where a GADT would be most useful), because you'd have to look up the visibility anyway.

    (by the way, the function you describe is Distribution/Types/PackageDescription.allLibraries)

    In the stack codebase there are maybe a hundred places where we specifically target the main library. Maybe that's just because stack is old and clunky I don't know, but having to run a find search in a list with a boolean everywhere we used to simply apply a function does not seem like progress to me.

    Let's not be antagonistic, it just has yet to catch up with the latest Cabal features. Here's how the cabal format evolved:

    1. One library per package
    2. Multiple sublibraries for private use, distinct from the main, public library <- stack is here
    3. With visibility, the concepts of "main library" and "sublibrary" are unified <- Cabal and cabal-install are here

    In (2) it makes sense to have two different fields, in (3) not so much in my opinion. Of course breaking backward compatibility is not pleasant, but the feature has been there for a while, and I think a cleanup is worthwhile. You write yourself that there's an effort to "move stack closer to cabal design".

    So I'll leave it to this then. Have a nice day.

    My comment wasn't meant to discourage you, quite the opposite. I'm interested in cleaning up those fields.
    If you can show an example of code that can be made safer with the change you propose, and that would be worsened by dropping the distinction, by all means let's do it.

  9. theobat commented on Nov 12, 2022

    @theobat
    Author

    @fgaz, I really appreciate the time you take to fill in the gap between your knowledge and my understanding.

    I do argue that the distinction of fields is bad, but changing that won't touch the .cabal file format.

    And I argue that the PackageDescription should be a strict mirror of the acceptable structure in the cabal file. Admittedly, It's already not the case now (as per your comment on uniqueness), but merging libraries into a list is strictly worse from this perspective. And honestly, this is not even a matter of type safety, it's rather a materialization of some constraints and assumptions that are widespread in the codebase.
    The Map is the only interesting option following this principle (and would be interesting for all component lists indeed), interestingly enough, there is a lookupComponent function but it doesn't seem to be used much.

    If you can show an example of code that can be made safer with the change you propose, and that would be worsened by dropping the distinction, by all means let's do it.

    Ok sounds good, I'll look at the state of the main/sub distinction use in the codebase 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions